[tor-commits] [orbot/master] update VPN app manager to load async, and sort selected to top

n8fr8 at torproject.org n8fr8 at torproject.org
Tue Oct 31 15:59:11 UTC 2017


commit 5b9eedba1d31240cfe16179515c13953e6eca4ef
Author: n8fr8 <nathan at freitas.net>
Date:   Tue Oct 31 10:21:13 2017 -0400

    update VPN app manager to load async, and sort selected to top
---
 app/src/fullperm/AndroidManifest.xml               |   2 +-
 app/src/main/AndroidManifest.xml                   |   2 +-
 .../org/torproject/android/OrbotMainActivity.java  |   6 +-
 .../android/settings/SettingsPreferences.java      |   5 -
 .../{AppManager.java => AppManagerActivity.java}   | 129 +++++++--------------
 app/src/minimalperm/AndroidManifest.xml            |   2 +-
 6 files changed, 47 insertions(+), 99 deletions(-)

diff --git a/app/src/fullperm/AndroidManifest.xml b/app/src/fullperm/AndroidManifest.xml
index dadd11c8..0c1b4185 100644
--- a/app/src/fullperm/AndroidManifest.xml
+++ b/app/src/fullperm/AndroidManifest.xml
@@ -79,7 +79,7 @@
             android:name=".settings.SettingsPreferences"
             android:label="@string/app_name" />
         <activity
-            android:name=".ui.AppManager"
+            android:name=".ui.AppManagerActivity"
             android:label="@string/app_name"
             android:theme="@style/Theme.AppCompat" />
 
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 94c33920..387b53f2 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -71,7 +71,7 @@
             android:name=".settings.SettingsPreferences"
             android:label="@string/app_name" />
         <activity
-            android:name=".ui.AppManager"
+            android:name=".ui.AppManagerActivity"
             android:label="@string/app_name"
             android:theme="@style/Theme.AppCompat" />
 
diff --git a/app/src/main/java/org/torproject/android/OrbotMainActivity.java b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
index ed7894d3..2c45d0f6 100644
--- a/app/src/main/java/org/torproject/android/OrbotMainActivity.java
+++ b/app/src/main/java/org/torproject/android/OrbotMainActivity.java
@@ -12,9 +12,7 @@ import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.text.NumberFormat;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.Locale;
-import java.util.Random;
 import java.util.StringTokenizer;
 
 import org.json.JSONArray;
@@ -24,7 +22,7 @@ import org.torproject.android.service.TorService;
 import org.torproject.android.service.TorServiceConstants;
 import org.torproject.android.service.util.TorServiceUtils;
 import org.torproject.android.settings.SettingsPreferences;
-import org.torproject.android.ui.AppManager;
+import org.torproject.android.ui.AppManagerActivity;
 import org.torproject.android.ui.ImageProgressView;
 import org.torproject.android.ui.PromoAppsActivity;
 import org.torproject.android.ui.Rotate3dAnimation;
@@ -617,7 +615,7 @@ public class OrbotMainActivity extends AppCompatActivity
 
         if (enable) {
             if (PermissionManager.isLollipopOrHigher()) //let the user choose the apps
-                startActivityForResult(new Intent(OrbotMainActivity.this, AppManager.class), REQUEST_VPN_APPS_SELECT);
+                startActivityForResult(new Intent(OrbotMainActivity.this, AppManagerActivity.class), REQUEST_VPN_APPS_SELECT);
             else
                 startActivity(new Intent(OrbotMainActivity.this, VPNEnableActivity.class));
         } else
diff --git a/app/src/main/java/org/torproject/android/settings/SettingsPreferences.java b/app/src/main/java/org/torproject/android/settings/SettingsPreferences.java
index 74986779..a51c1097 100644
--- a/app/src/main/java/org/torproject/android/settings/SettingsPreferences.java
+++ b/app/src/main/java/org/torproject/android/settings/SettingsPreferences.java
@@ -4,21 +4,16 @@
 package org.torproject.android.settings;
 
 import android.content.Context;
-import android.content.Intent;
 import android.content.SharedPreferences;
-import android.os.Build;
 import android.os.Bundle;
-import android.preference.CheckBoxPreference;
 import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceChangeListener;
 import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceActivity;
-import android.util.Log;
 
 import org.torproject.android.OrbotApp;
 import org.torproject.android.R;
-import org.torproject.android.ui.AppManager;
 import org.torproject.android.service.util.TorServiceUtils;
 
 import java.util.Locale;
diff --git a/app/src/main/java/org/torproject/android/ui/AppManager.java b/app/src/main/java/org/torproject/android/ui/AppManagerActivity.java
similarity index 76%
rename from app/src/main/java/org/torproject/android/ui/AppManager.java
rename to app/src/main/java/org/torproject/android/ui/AppManagerActivity.java
index e54898de..9d909f54 100644
--- a/app/src/main/java/org/torproject/android/ui/AppManager.java
+++ b/app/src/main/java/org/torproject/android/ui/AppManagerActivity.java
@@ -6,6 +6,7 @@ package org.torproject.android.ui;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
@@ -15,38 +16,35 @@ import org.torproject.android.R;
 import org.torproject.android.service.util.TorServiceUtils;
 import org.torproject.android.service.vpn.TorifiedApp;
 
-import android.app.Activity;
+import android.app.ProgressDialog;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
+import android.os.AsyncTask;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.Menu;
-import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
-import android.widget.Button;
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
 import android.widget.CompoundButton.OnCheckedChangeListener;
 import android.widget.GridView;
 import android.widget.ImageView;
 import android.widget.ListAdapter;
-import android.widget.ListView;
 import android.widget.TextView;
 
-public class AppManager extends AppCompatActivity implements OnCheckedChangeListener, OnClickListener, OrbotConstants {
+public class AppManagerActivity extends AppCompatActivity implements OnCheckedChangeListener, OnClickListener, OrbotConstants {
 
     private GridView listApps;
+    private ListAdapter adapterApps;
     private final static String TAG = "Orbot";
     PackageManager pMgr = null;
 
@@ -61,83 +59,43 @@ public class AppManager extends AppCompatActivity implements OnCheckedChangeList
 
     }
 
-    /**
-     class OnAutoClickListener implements Button.OnClickListener {
-     private int status;
-     public OnAutoClickListener(int status){
-     this.status = status;
-     }
-     @SuppressWarnings("unchecked")
-     public void onClick(View button){
-     GridView listView;
-     ViewGroup viewGroup;
-     View parentView, currentView;
-     ArrayAdapter<TorifiedApp> adapter;
-     TorifiedApp app;
-     CheckBox box;
-     float buttonId;
-     boolean[] isSelected;
-     int posI, selectedI, lvSz;
-
-     buttonId = button.getId();
-     listView = (GridView) findViewById(R.id.applistview);
-     lvSz = listView.getCount();
-     isSelected = new boolean[lvSz];
-
-     selectedI = -1;
-
-     if (this.status == 0){
-     Log.d(TAG, "Proxifying ALL");
-     }else if (this.status == 1){
-     Log.d(TAG, "Proxifying NONE");
-     }else {
-     Log.d(TAG, "Proxifying invert");
-     }
-
-     Context context = getApplicationContext();
-     SharedPreferences prefs = TorServiceUtils.getSharedPrefs(context);
-     ArrayList<TorifiedApp> apps = getApps(context, prefs);
-     parentView = (View) findViewById(R.id.applistview);
-     viewGroup = (ViewGroup) listView;
-
-     adapter = (ArrayAdapter<TorifiedApp>) listApps.getAdapter();
-     if (adapter == null){
-     Log.w(TAG, "List adapter is null. Getting apps.");
-     loadApps(prefs);
-     adapter = (ArrayAdapter<TorifiedApp>) listApps.getAdapter();
-     }
-
-     for (int i = 0 ; i < adapter.getCount(); ++i){
-     app = (TorifiedApp) adapter.getItem(i);
-     currentView = adapter.getView(i, parentView, viewGroup);
-     box = (CheckBox) currentView.findViewById(R.id.itemcheck);
-
-     if (this.status == 0){
-     app.setTorified(true);
-     }else if (this.status == 1){
-     app.setTorified(false);
-     }else {
-     app.setTorified(!app.isTorified());
-     }
-
-     if (box != null)
-     box.setChecked(app.isTorified());
-
-     }
-     saveAppSettings(context);
-     loadApps(prefs);
-     }
-     }**/
-
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        if (item.getItemId() == android.R.id.home)
+        {
+            finish();
+            return true;
+        }
 
+        return super.onOptionsItemSelected(item);
+    }
 
     @Override
     protected void onResume() {
         super.onResume();
-        listApps = (GridView)findViewById(R.id.applistview);
-
+        listApps = (GridView) findViewById(R.id.applistview);
         mPrefs = TorServiceUtils.getSharedPrefs(getApplicationContext());
-        loadApps(mPrefs);
+
+
+        new AsyncTask<Void, Void, Void>() {
+            private ProgressDialog dialog;
+
+            protected void onPreExecute() {
+                // Pre Code
+                dialog = new ProgressDialog(AppManagerActivity.this);
+                dialog.show();
+            }
+            protected Void doInBackground(Void... unused) {
+                loadApps(mPrefs);
+                return null;
+            }
+            protected void onPostExecute(Void unused) {
+                listApps.setAdapter(adapterApps);
+                dialog.cancel();
+            }
+        }.execute();
+
+
     }
 
     SharedPreferences mPrefs = null;
@@ -147,19 +105,17 @@ public class AppManager extends AppCompatActivity implements OnCheckedChangeList
     {
 
         mApps = getApps(getApplicationContext(), prefs);
-
-        /*
-        Arrays.sort(apps, new Comparator<TorifiedApp>() {
+        Collections.sort(mApps,new Comparator<TorifiedApp>() {
             public int compare(TorifiedApp o1, TorifiedApp o2) {
                 if (o1.isTorified() == o2.isTorified()) return o1.getName().compareTo(o2.getName());
                 if (o1.isTorified()) return -1;
                 return 1;
             }
-        });*/
+        });
 
         final LayoutInflater inflater = getLayoutInflater();
 
-        ListAdapter adapter = new ArrayAdapter<TorifiedApp>(this, R.layout.layout_apps_item, R.id.itemtext,mApps) {
+        adapterApps = new ArrayAdapter<TorifiedApp>(this, R.layout.layout_apps_item, R.id.itemtext,mApps) {
 
             public View getView(int position, View convertView, ViewGroup parent) {
 
@@ -189,7 +145,7 @@ public class AppManager extends AppCompatActivity implements OnCheckedChangeList
 
                     try {
                         entry.icon.setImageDrawable(pMgr.getApplicationIcon(app.getPackageName()));
-                        entry.icon.setOnClickListener(AppManager.this);
+                        entry.icon.setOnClickListener(AppManagerActivity.this);
 
                         if (entry.box != null)
                             entry.icon.setTag(entry.box);
@@ -202,7 +158,7 @@ public class AppManager extends AppCompatActivity implements OnCheckedChangeList
 
                 if (entry.text != null) {
                     entry.text.setText(app.getName());
-                    entry.text.setOnClickListener(AppManager.this);
+                    entry.text.setOnClickListener(AppManagerActivity.this);
 
                     if (entry.box != null)
                         entry.text.setTag(entry.box);
@@ -210,7 +166,7 @@ public class AppManager extends AppCompatActivity implements OnCheckedChangeList
 
 
                 if (entry.box != null) {
-                    entry.box.setOnCheckedChangeListener(AppManager.this);
+                    entry.box.setOnCheckedChangeListener(AppManagerActivity.this);
                     entry.box.setTag(app);
                     entry.box.setChecked(app.isTorified());
 
@@ -220,7 +176,6 @@ public class AppManager extends AppCompatActivity implements OnCheckedChangeList
             }
         };
 
-        listApps.setAdapter(adapter);
 
     }
 
diff --git a/app/src/minimalperm/AndroidManifest.xml b/app/src/minimalperm/AndroidManifest.xml
index d21aa11a..adf74209 100644
--- a/app/src/minimalperm/AndroidManifest.xml
+++ b/app/src/minimalperm/AndroidManifest.xml
@@ -76,7 +76,7 @@
             android:name=".settings.SettingsPreferences"
             android:label="@string/app_name" />
         <activity
-            android:name=".ui.AppManager"
+            android:name=".ui.AppManagerActivity"
             android:label="@string/app_name"
             android:theme="@style/Theme.AppCompat" />
 





More information about the tor-commits mailing list