[tor-commits] [orbot/master] Fixes #180 removes Spinner from HostedServices

n8fr8 at torproject.org n8fr8 at torproject.org
Wed Oct 7 12:37:35 UTC 2020


commit b0ec2facc33c2010065c766ada38c6936c2e5651
Author: bim <dsnake at protonmail.com>
Date:   Sun Sep 20 22:51:19 2020 -0400

    Fixes #180 removes Spinner from HostedServices
---
 app-mini/src/main/res/values-es/arrays.xml         |  4 -
 app-mini/src/main/res/values/arrays.xml            |  4 -
 .../ui/hiddenservices/HiddenServicesActivity.java  | 85 +++++++++++-----------
 .../hiddenservices/adapters/OnionListAdapter.java  | 14 ++--
 .../main/res/layout/layout_hs_list_view_main.xml   | 35 ++++++++-
 app/src/main/res/menu/hs_menu.xml                  | 10 +--
 app/src/main/res/values-es/arrays.xml              |  9 ---
 app/src/main/res/values/arrays.xml                 |  9 ---
 8 files changed, 80 insertions(+), 90 deletions(-)

diff --git a/app-mini/src/main/res/values-es/arrays.xml b/app-mini/src/main/res/values-es/arrays.xml
index c8149515..5fb5fe38 100644
--- a/app-mini/src/main/res/values-es/arrays.xml
+++ b/app-mini/src/main/res/values-es/arrays.xml
@@ -2,8 +2,4 @@
 
 <resources>
 
-    <string-array name="array_hs_types">
-		<item>Usuarios</item>
-		<item>Aplicaciones</item>
-	</string-array>
 </resources>
diff --git a/app-mini/src/main/res/values/arrays.xml b/app-mini/src/main/res/values/arrays.xml
index d5c86e36..5fb5fe38 100644
--- a/app-mini/src/main/res/values/arrays.xml
+++ b/app-mini/src/main/res/values/arrays.xml
@@ -2,8 +2,4 @@
 
 <resources>
 
-    <string-array name="array_hs_types">
-		<item>@string/user_services</item>
-		<item>@string/app_services</item>
-	</string-array>
 </resources>
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java
index 1a49dc0c..6430d48d 100644
--- a/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java
+++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/HiddenServicesActivity.java
@@ -11,14 +11,11 @@ import android.os.Handler;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
-import android.widget.AdapterView;
-import android.widget.ArrayAdapter;
 import android.widget.ListView;
-import android.widget.Spinner;
+import android.widget.RadioButton;
 
 import androidx.appcompat.app.AppCompatActivity;
 import androidx.appcompat.widget.Toolbar;
-import androidx.core.view.MenuItemCompat;
 
 import com.google.android.material.floatingactionbutton.FloatingActionButton;
 
@@ -33,17 +30,21 @@ import org.torproject.android.ui.hiddenservices.permissions.PermissionManager;
 import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
 
 public class HiddenServicesActivity extends AppCompatActivity {
+    private static final int REQUEST_CODE_READ_ZIP_BACKUP = 125;
     public final int WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR = 1;
     private ContentResolver mResolver;
     private OnionListAdapter mAdapter;
+    private RadioButton radioShowUserServices, radioShowAppServices;
     private FloatingActionButton fab;
-
     private String mWhere = HSContentProvider.HiddenService.CREATED_BY_USER + "=1";
+    private static final String BUNDLE_KEY_SHOW_USER_SERVICES = "show_user_services";
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.layout_hs_list_view);
+        radioShowUserServices = findViewById(R.id.radioUserServices);
+        radioShowAppServices = findViewById(R.id.radioAppServices);
 
         Toolbar toolbar = findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
@@ -57,14 +58,15 @@ public class HiddenServicesActivity extends AppCompatActivity {
             dialog.show(getSupportFragmentManager(), "HSDataDialog");
         });
 
-        mAdapter = new OnionListAdapter(
-                this,
-                mResolver.query(HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, mWhere, null, null),
-                0);
+        mAdapter = new OnionListAdapter(this, mResolver.query(HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, mWhere, null, null), 0);
 
         mResolver.registerContentObserver(HSContentProvider.CONTENT_URI, true, new HSObserver(new Handler()));
 
         ListView onion_list = findViewById(R.id.onion_list);
+        boolean selectUserServices = savedInstanceState == null || savedInstanceState.getBoolean(BUNDLE_KEY_SHOW_USER_SERVICES);
+        if (selectUserServices) radioShowUserServices.setChecked(true);
+        else radioShowAppServices.setChecked(true);
+        filterServices(selectUserServices);
         onion_list.setAdapter(mAdapter);
 
         onion_list.setOnItemClickListener((parent, view, position, id) -> {
@@ -83,7 +85,12 @@ public class HiddenServicesActivity extends AppCompatActivity {
         });
     }
 
-    
+    @Override
+    protected void onSaveInstanceState(Bundle icicle) {
+        super.onSaveInstanceState(icicle);
+        icicle.putBoolean(BUNDLE_KEY_SHOW_USER_SERVICES, radioShowUserServices.isChecked());
+    }
+
     @Override
     protected void attachBaseContext(Context base) {
         super.attachBaseContext(LocaleHelper.onAttach(base));
@@ -92,38 +99,9 @@ public class HiddenServicesActivity extends AppCompatActivity {
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.menu.hs_menu, menu);
-
-        MenuItem item = menu.findItem(R.id.hs_type);
-        Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
-
-        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.array_hs_types, android.R.layout.simple_spinner_item);
-
-        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
-        spinner.setAdapter(adapter);
-        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
-            @Override
-            public void onItemSelected(AdapterView<?> arg0, View v, int pos, long id) {
-                if (pos == 0) {
-                    mWhere = HSContentProvider.HiddenService.CREATED_BY_USER + "=1";
-                    fab.show();
-                } else {
-                    mWhere = HSContentProvider.HiddenService.CREATED_BY_USER + "=0";
-                    fab.hide();
-                }
-
-                mAdapter.changeCursor(mResolver.query(HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, mWhere, null, null));
-            }
-
-            @Override
-            public void onNothingSelected(AdapterView<?> arg0) {
-                // Do nothing
-            }
-        });
-
         return true;
     }
 
-    private static final int REQUEST_CODE_READ_ZIP_BACKUP = 125;
     private void doRestore() {
         Intent readFile = DiskUtils.createReadFileIntent("application/zip");
         startActivityForResult(readFile, REQUEST_CODE_READ_ZIP_BACKUP);
@@ -141,15 +119,12 @@ public class HiddenServicesActivity extends AppCompatActivity {
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
-        int id = item.getItemId();
-
-        if (id == R.id.menu_restore_backup) {
+        if (item.getItemId() == R.id.menu_restore_backup) {
             if (PermissionManager.isLollipopOrHigher()
                     && !PermissionManager.hasExternalWritePermission(this)) {
                 PermissionManager.requestExternalWritePermissions(this, WRITE_EXTERNAL_STORAGE_FROM_ACTIONBAR);
                 return true;
             }
-
             doRestore();
         }
 
@@ -177,6 +152,30 @@ public class HiddenServicesActivity extends AppCompatActivity {
         }
     }
 
+    private void filterServices(boolean showUserServices) {
+        if (showUserServices) {
+            mWhere = HSContentProvider.HiddenService.CREATED_BY_USER + "=1";
+            fab.show();
+        } else {
+            mWhere = HSContentProvider.HiddenService.CREATED_BY_USER + "=0";
+            fab.hide();
+        }
+        mAdapter.changeCursor(mResolver.query(HSContentProvider.CONTENT_URI, HSContentProvider.PROJECTION, mWhere, null, null));
+    }
+
+    public void onRadioButtonClick(View view) {
+        switch (view.getId()) {
+            case R.id.radioUserServices:
+                filterServices(true);
+                break;
+            case R.id.radioAppServices:
+                filterServices(false);
+                break;
+            default:
+                break;
+        }
+    }
+
     class HSObserver extends ContentObserver {
         HSObserver(Handler handler) {
             super(handler);
diff --git a/app/src/main/java/org/torproject/android/ui/hiddenservices/adapters/OnionListAdapter.java b/app/src/main/java/org/torproject/android/ui/hiddenservices/adapters/OnionListAdapter.java
index 0d06ce3e..1431aa7f 100644
--- a/app/src/main/java/org/torproject/android/ui/hiddenservices/adapters/OnionListAdapter.java
+++ b/app/src/main/java/org/torproject/android/ui/hiddenservices/adapters/OnionListAdapter.java
@@ -4,14 +4,15 @@ import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
-
-import androidx.appcompat.widget.SwitchCompat;
-import androidx.cursoradapter.widget.CursorAdapter;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 import android.widget.Toast;
+
+import androidx.appcompat.widget.SwitchCompat;
+import androidx.cursoradapter.widget.CursorAdapter;
+
 import org.torproject.android.R;
 import org.torproject.android.ui.hiddenservices.providers.HSContentProvider;
 
@@ -21,8 +22,7 @@ public class OnionListAdapter extends CursorAdapter {
     public OnionListAdapter(Context context, Cursor c, int flags) {
         super(context, c, flags);
 
-        cursorInflater = (LayoutInflater) context.getSystemService(
-                Context.LAYOUT_INFLATER_SERVICE);
+        cursorInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }
 
     @Override
@@ -47,9 +47,7 @@ public class OnionListAdapter extends CursorAdapter {
             ContentResolver resolver = mContext.getContentResolver();
             ContentValues fields = new ContentValues();
             fields.put(HSContentProvider.HiddenService.ENABLED, isChecked);
-            resolver.update(
-                    HSContentProvider.CONTENT_URI, fields, where, null
-            );
+            resolver.update(HSContentProvider.CONTENT_URI, fields, where, null);
 
             Toast.makeText(
                     mContext, R.string.please_restart_Orbot_to_enable_the_changes, Toast.LENGTH_LONG
diff --git a/app/src/main/res/layout/layout_hs_list_view_main.xml b/app/src/main/res/layout/layout_hs_list_view_main.xml
index 183ea28a..59ac805c 100644
--- a/app/src/main/res/layout/layout_hs_list_view_main.xml
+++ b/app/src/main/res/layout/layout_hs_list_view_main.xml
@@ -1,19 +1,46 @@
 <?xml version="1.0" encoding="utf-8"?>
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:paddingBottom="@dimen/activity_vertical_margin"
+    android:orientation="vertical"
     android:paddingLeft="@dimen/activity_horizontal_margin"
-    android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
+    android:paddingRight="@dimen/activity_horizontal_margin"
+    android:paddingBottom="@dimen/activity_vertical_margin"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     tools:context="org.torproject.android.ui.hiddenservices.HiddenServicesActivity"
     tools:showIn="@layout/layout_hs_list_view">
 
+    <RadioGroup
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginBottom="@dimen/activity_vertical_margin"
+        android:orientation="horizontal">
+
+        <TextView
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/service_type" />
+
+        <RadioButton
+            android:id="@+id/radioUserServices"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:onClick="onRadioButtonClick"
+            android:text="@string/user_services" />
+
+        <RadioButton
+            android:id="@+id/radioAppServices"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:onClick="onRadioButtonClick"
+            android:text="@string/app_services" />
+    </RadioGroup>
+
     <ListView
         android:id="@+id/onion_list"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
-</RelativeLayout>
\ No newline at end of file
+</LinearLayout>
\ No newline at end of file
diff --git a/app/src/main/res/menu/hs_menu.xml b/app/src/main/res/menu/hs_menu.xml
index e82a935d..cfe580d6 100644
--- a/app/src/main/res/menu/hs_menu.xml
+++ b/app/src/main/res/menu/hs_menu.xml
@@ -1,13 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<menu xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:app="http://schemas.android.com/apk/res-auto">
-    <item
-        android:id="@+id/hs_type"
-        android:background="#ff00"
-        android:title="@string/service_type"
-        app:actionViewClass="android.widget.Spinner"
-        app:showAsAction="always" />
-
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
     <item
         android:id="@+id/menu_restore_backup"
         android:title="@string/restore_backup" />
diff --git a/app/src/main/res/values-es/arrays.xml b/app/src/main/res/values-es/arrays.xml
deleted file mode 100644
index c8149515..00000000
--- a/app/src/main/res/values-es/arrays.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-
-<resources>
-
-    <string-array name="array_hs_types">
-		<item>Usuarios</item>
-		<item>Aplicaciones</item>
-	</string-array>
-</resources>
diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml
deleted file mode 100644
index d5c86e36..00000000
--- a/app/src/main/res/values/arrays.xml
+++ /dev/null
@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-
-<resources>
-
-    <string-array name="array_hs_types">
-		<item>@string/user_services</item>
-		<item>@string/app_services</item>
-	</string-array>
-</resources>





More information about the tor-commits mailing list