[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake-mobile/master] Moved main activity code in to MainFragment to make way for up comming settings fragment



commit ec410a4b728cc5a3a4038ba46c32a7c6d66615c9
Author: Hashik Donthineni <HashikDonthineni@xxxxxxxxx>
Date:   Sun Jun 28 15:14:20 2020 +0530

    Moved main activity code in to MainFragment to make way for up comming settings fragment
---
 app/build.gradle                                   |  1 +
 .../org/torproject/snowflake/MainActivity.java     | 20 ++-----
 .../org/torproject/snowflake/MainFragment.java     | 68 ++++++++++++++++++++++
 .../snowflake/interfaces/MainFragmentCallback.java |  6 ++
 app/src/main/res/layout/activity_main.xml          | 11 ----
 app/src/main/res/layout/fragment_main_fragment.xml | 17 ++++++
 app/src/main/res/values/strings.xml                |  2 +
 7 files changed, 99 insertions(+), 26 deletions(-)

diff --git a/app/build.gradle b/app/build.gradle
index 4d13970..964695b 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -33,6 +33,7 @@ dependencies {
     implementation 'androidx.appcompat:appcompat:1.1.0'
     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
     implementation 'androidx.preference:preference:1.1.1'
+    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
     testImplementation 'junit:junit:4.12'
     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
diff --git a/app/src/main/java/org/torproject/snowflake/MainActivity.java b/app/src/main/java/org/torproject/snowflake/MainActivity.java
index 1ac43dd..d412944 100644
--- a/app/src/main/java/org/torproject/snowflake/MainActivity.java
+++ b/app/src/main/java/org/torproject/snowflake/MainActivity.java
@@ -8,16 +8,16 @@ import android.content.SharedPreferences;
 import android.graphics.Color;
 import android.os.Build;
 import android.os.Bundle;
-import android.widget.Button;
 
 import androidx.appcompat.app.AppCompatActivity;
 
 import org.torproject.snowflake.constants.ForegroundServiceConstants;
+import org.torproject.snowflake.interfaces.MainFragmentCallback;
 
 /**
  * MainActivity is the main UI of the application.
  */
-public class MainActivity extends AppCompatActivity {
+public class MainActivity extends AppCompatActivity implements MainFragmentCallback {
     private static final String TAG = "MainActivity";
     private SharedPreferences sharedPreferences;
 
@@ -25,7 +25,7 @@ public class MainActivity extends AppCompatActivity {
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
-
+        setSupportActionBar(findViewById(R.id.toolbar));
         sharedPreferences = getSharedPreferences(getString(R.string.sharedpreference_file), MODE_PRIVATE);
 
         //Creating notification channel if app is being run for the first time
@@ -34,16 +34,6 @@ public class MainActivity extends AppCompatActivity {
             //Setting initial run to false.
             sharedPreferences.edit().putBoolean(getString(R.string.initial_run_boolean), false).apply();
         }
-
-        Button startButton = findViewById(R.id.start_button);
-        startButton.setOnClickListener(v -> {
-            if (isServiceRunning()) //Toggling the service.
-                serviceToggle(ForegroundServiceConstants.ACTION_STOP);
-            else
-                serviceToggle(ForegroundServiceConstants.ACTION_START);
-        });
-        if (BuildConfig.DEBUG)
-            startButton.performClick(); //To perform an automatic click in testing environment.
     }
 
     /**
@@ -51,7 +41,7 @@ public class MainActivity extends AppCompatActivity {
      *
      * @param action An Action from ForegroundServiceConstants.
      */
-    private void serviceToggle(String action) {
+    public void serviceToggle(String action) {
         Intent serviceIntent = new Intent(MainActivity.this, MyPersistentService.class);
         serviceIntent.setAction(action);
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -66,7 +56,7 @@ public class MainActivity extends AppCompatActivity {
      *
      * @return boolean whether the service is running or not.
      */
-    private boolean isServiceRunning() {
+    public boolean isServiceRunning() {
         return sharedPreferences.getBoolean(getString(R.string.is_service_running_bool), false);
     }
 
diff --git a/app/src/main/java/org/torproject/snowflake/MainFragment.java b/app/src/main/java/org/torproject/snowflake/MainFragment.java
new file mode 100644
index 0000000..f850277
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/MainFragment.java
@@ -0,0 +1,68 @@
+package org.torproject.snowflake;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Button;
+
+import androidx.annotation.NonNull;
+import androidx.fragment.app.Fragment;
+
+import org.torproject.snowflake.constants.ForegroundServiceConstants;
+import org.torproject.snowflake.interfaces.MainFragmentCallback;
+
+/**
+ * A simple {@link Fragment} subclass.
+ * Use the {@link MainFragment#newInstance} factory method to
+ * create an instance of this fragment.
+ */
+public class MainFragment extends Fragment {
+    MainFragmentCallback callback;
+
+    public MainFragment() {
+        // Required empty public constructor
+    }
+
+    /**
+     * Use this factory method to create a new instance of
+     * this fragment.
+     *
+     * @return A new instance of fragment main_fragment.
+     */
+
+    public static MainFragment newInstance() {
+        MainFragment fragment = new MainFragment();
+        Bundle bundle = new Bundle();
+        return fragment;
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+    }
+
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        Button startButton = container.findViewById(R.id.start_button);
+        startButton.setOnClickListener(v -> {
+            if (callback.isServiceRunning()) //Toggling the service.
+                callback.serviceToggle(ForegroundServiceConstants.ACTION_STOP);
+            else
+                callback.serviceToggle(ForegroundServiceConstants.ACTION_START);
+        });
+        if (BuildConfig.DEBUG)
+            startButton.performClick(); //To perform an automatic click in testing environment.
+
+        // Inflate the layout for this fragment
+        return inflater.inflate(R.layout.fragment_main_fragment, container, false);
+    }
+
+    @Override
+    public void onAttach(@NonNull Context context) {
+        super.onAttach(context);
+        callback = (MainFragmentCallback) context;
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java b/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java
new file mode 100644
index 0000000..08e2d99
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java
@@ -0,0 +1,6 @@
+package org.torproject.snowflake.interfaces;
+
+public interface MainFragmentCallback {
+    boolean isServiceRunning();
+    void serviceToggle(String action);
+}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 916b433..abc7268 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -38,15 +38,4 @@
                 android:foreground="?android:attr/selectableItemBackground" />
         </RelativeLayout>
     </androidx.appcompat.widget.Toolbar>
-
-    <Button
-        android:id="@+id/start_button"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:foreground="?android:attr/selectableItemBackground"
-        android:text="Service Toggle"
-        app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
diff --git a/app/src/main/res/layout/fragment_main_fragment.xml b/app/src/main/res/layout/fragment_main_fragment.xml
new file mode 100644
index 0000000..7523126
--- /dev/null
+++ b/app/src/main/res/layout/fragment_main_fragment.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android";
+    xmlns:tools="http://schemas.android.com/tools";
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".MainFragment">
+
+
+    <Button
+        android:id="@+id/start_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerInParent="true"
+        android:foreground="?android:attr/selectableItemBackground"
+        android:text="Service Toggle" />
+
+</FrameLayout>
\ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index d737520..c26d83e 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -5,4 +5,6 @@
     <string name="not_channel_desc">This Channel should not be muted. The Android system will consider Snowflake not import and will kill the service if it\'s muted.</string>
     <string name="initial_run_boolean">initial_run</string>
     <string name="not_channel_name">Snowflake Service</string>
+    <!-- TODO: Remove or change this placeholder text -->
+    <string name="hello_blank_fragment">Hello blank fragment</string>
 </resources>



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits