@@ -185,14 +185,16 @@ public AllowedPackageListFragment() {
185185 protected static class PackageListFragment extends PreferenceFragmentCompat
186186 implements SearchView .OnQueryTextListener , SearchView .OnCloseListener {
187187 private final Map <String , Boolean > mAllPackageInfoMap = new HashMap <>();
188- private final static String PREF_VPN_APPLICATION_SORTBY = "pref_vpn_application_app_sortby" ;
189188 private final static String PREF_VPN_APPLICATION_ORDERBY = "pref_vpn_application_app_orderby" ;
189+ private final static String PREF_VPN_APPLICATION_FILTERBY = "pref_vpn_application_app_filterby" ;
190+ private final static String PREF_VPN_APPLICATION_SORTBY = "pref_vpn_application_app_sortby" ;
190191
191- private final AsyncTaskProgress task ;
192+ private AsyncTaskProgress task ;
192193
193194 private MyApplication .VPNMode mode ;
194195 private MyApplication .AppSortBy appSortBy = MyApplication .AppSortBy .APPNAME ;
195196 private MyApplication .AppOrderBy appOrderBy = MyApplication .AppOrderBy .ASC ;
197+ private MyApplication .AppSortBy appFilterBy = MyApplication .AppSortBy .APPNAME ;
196198 private PreferenceScreen mFilterPreferenceScreen ;
197199
198200 public PackageListFragment (MyApplication .VPNMode mode ) {
@@ -221,56 +223,73 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
221223 this .searchView .setOnCloseListener (this );
222224 this .searchView .setSubmitButtonEnabled (false );
223225
224- switch (this .appSortBy ) {
226+ switch (this .appOrderBy ) {
227+ case ASC : {
228+ final MenuItem menuItem = menu .findItem (R .id .menu_sort_order_asc );
229+ menuItem .setChecked (true );
230+ break ;
231+ }
232+ case DESC : {
233+ final MenuItem menuItem = menu .findItem (R .id .menu_sort_order_desc );
234+ menuItem .setChecked (true );
235+ break ;
236+ }
237+ }
238+
239+ switch (this .appFilterBy ) {
225240 case APPNAME : {
226- final MenuItem menuItem = menu .findItem (R .id .menu_sort_app_name );
241+ final MenuItem menuItem = menu .findItem (R .id .menu_filter_app_name );
227242 menuItem .setChecked (true );
228243 break ;
229244 }
230245 case PKGNAME : {
231- final MenuItem menuItem = menu .findItem (R .id .menu_sort_pkg_name );
246+ final MenuItem menuItem = menu .findItem (R .id .menu_filter_pkg_name );
232247 menuItem .setChecked (true );
233248 break ;
234249 }
235250 }
236251
237- switch (this .appOrderBy ) {
238- case ASC : {
239- final MenuItem menuItem = menu .findItem (R .id .menu_sort_order_asc );
252+ switch (this .appSortBy ) {
253+ case APPNAME : {
254+ final MenuItem menuItem = menu .findItem (R .id .menu_sort_app_name );
240255 menuItem .setChecked (true );
241256 break ;
242257 }
243- case DESC : {
244- final MenuItem menuItem = menu .findItem (R .id .menu_sort_order_desc );
258+ case PKGNAME : {
259+ final MenuItem menuItem = menu .findItem (R .id .menu_sort_pkg_name );
245260 menuItem .setChecked (true );
246261 break ;
247262 }
248263 }
249-
250264 }
251265
252266 private String searchFilter = "" ;
253267 private SearchView searchView ;
254268
255269 protected void filter (String filter ) {
256- this .filter (filter , this .appSortBy , this .appOrderBy );
270+ this .filter (filter , this .appFilterBy , this .appOrderBy , this . appSortBy );
257271 }
258272
259- protected void filter (String filter , final MyApplication .AppSortBy sortBy , final MyApplication .AppOrderBy orderBy ) {
273+ protected void filter (String filter , final MyApplication .AppSortBy filterBy , final MyApplication .AppOrderBy orderBy , final MyApplication . AppSortBy sortBy ) {
260274 if (filter == null ) {
261275 filter = searchFilter ;
262276 } else {
263277 searchFilter = filter ;
264278 }
265- this .appSortBy = sortBy ;
279+ this .appFilterBy = filterBy ;
266280 this .appOrderBy = orderBy ;
281+ this .appSortBy = sortBy ;
267282
268283 Set <String > selected = this .getAllSelectedPackageSet ();
269284 storeSelectedPackageSet (selected );
270285
271286 this .removeAllPreferenceScreen ();
272- //final AsyncTaskProgress task = new AsyncTaskProgress(this);
273- if (task .getStatus () == AsyncTask .Status .PENDING ) {
287+
288+ if (task != null && task .getStatus () == AsyncTask .Status .PENDING ) {
289+ task .execute ();
290+ }
291+ else {
292+ task = new AsyncTaskProgress (this );
274293 task .execute ();
275294 }
276295// this.filterPackagesPreferences(filter, sortBy, orderBy);
@@ -281,16 +300,17 @@ public void onPause() {
281300 super .onPause ();
282301 if (this .task != null ) {
283302 this .task .cancel (true );
303+ this .task = null ;
284304 }
285305
286306 Set <String > selected = this .getAllSelectedPackageSet ();
287307 storeSelectedPackageSet (selected );
288308 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (MyApplication .getInstance ().getApplicationContext ());
289309 SharedPreferences .Editor edit = prefs .edit ();
290- edit .putString (PREF_VPN_APPLICATION_SORTBY , this .appSortBy .name ());
291310 edit .putString (PREF_VPN_APPLICATION_ORDERBY , this .appOrderBy .name ());
311+ edit .putString (PREF_VPN_APPLICATION_FILTERBY , this .appFilterBy .name ());
312+ edit .putString (PREF_VPN_APPLICATION_SORTBY , this .appSortBy .name ());
292313 edit .apply ();
293- Log .i ("onPause" , this .appSortBy .name ());
294314 }
295315
296316 @ Override
@@ -301,11 +321,12 @@ public void onResume() {
301321 this .mAllPackageInfoMap .put (pkgName , loadMap .contains (pkgName ));
302322 }
303323 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (MyApplication .getInstance ().getApplicationContext ());
304- String appSortBy = prefs .getString (PREF_VPN_APPLICATION_SORTBY , MyApplication .AppSortBy .APPNAME .name ());
305324 String appOrderBy = prefs .getString (PREF_VPN_APPLICATION_ORDERBY , MyApplication .AppOrderBy .ASC .name ());
306- this .appSortBy = Enum .valueOf (MyApplication .AppSortBy .class , appSortBy );
325+ String appFilterBy = prefs .getString (PREF_VPN_APPLICATION_FILTERBY , MyApplication .AppSortBy .APPNAME .name ());
326+ String appSortBy = prefs .getString (PREF_VPN_APPLICATION_SORTBY , MyApplication .AppSortBy .APPNAME .name ());
307327 this .appOrderBy = Enum .valueOf (MyApplication .AppOrderBy .class , appOrderBy );
308- Log .i ("onResume" , this .appSortBy .name ());
328+ this .appFilterBy = Enum .valueOf (MyApplication .AppSortBy .class , appFilterBy );
329+ this .appSortBy = Enum .valueOf (MyApplication .AppSortBy .class , appSortBy );
309330 filter (null );
310331 }
311332
@@ -440,21 +461,31 @@ public boolean onOptionsItemSelected(MenuItem item) {
440461 case android .R .id .home :
441462 startActivity (new Intent (getActivity (), SettingsActivity .class ));
442463 return true ;
443- case R .id .menu_sort_app_name :
464+ case R .id .menu_sort_order_asc :
444465 item .setChecked (!item .isChecked ());
445- filter (null , MyApplication .AppSortBy . APPNAME , appOrderBy );
466+ filter (null , appFilterBy , MyApplication .AppOrderBy . ASC , appSortBy );
446467 break ;
447- case R .id .menu_sort_pkg_name :
468+ case R .id .menu_sort_order_desc :
448469 item .setChecked (!item .isChecked ());
449- filter (null , MyApplication .AppSortBy . PKGNAME , appOrderBy );
470+ filter (null , appFilterBy , MyApplication .AppOrderBy . DESC , appSortBy );
450471 break ;
451- case R .id .menu_sort_order_asc :
472+ case R .id .menu_filter_app_name :
452473 item .setChecked (!item .isChecked ());
453- filter (null , appSortBy , MyApplication .AppOrderBy .ASC );
474+ this .appFilterBy = MyApplication .AppSortBy .APPNAME ;
475+ //filter(null, MyApplication.AppSortBy.APPNAME, appOrderBy, appSortBy);
454476 break ;
455- case R .id .menu_sort_order_desc :
477+ case R .id .menu_filter_pkg_name :
456478 item .setChecked (!item .isChecked ());
457- filter (null , appSortBy , MyApplication .AppOrderBy .DESC );
479+ this .appFilterBy = MyApplication .AppSortBy .PKGNAME ;
480+ //filter(null, MyApplication.AppSortBy.PKGNAME, appOrderBy, appSortBy);
481+ break ;
482+ case R .id .menu_sort_app_name :
483+ item .setChecked (!item .isChecked ());
484+ filter (null , appFilterBy , appOrderBy , MyApplication .AppSortBy .APPNAME );
485+ break ;
486+ case R .id .menu_sort_pkg_name :
487+ item .setChecked (!item .isChecked ());
488+ filter (null , appFilterBy , appOrderBy , MyApplication .AppSortBy .PKGNAME );
458489 break ;
459490 }
460491 return super .onOptionsItemSelected (item );
@@ -506,10 +537,10 @@ protected void onPreExecute() {
506537
507538 @ Override
508539 protected List <PackageInfo > doInBackground (String ... params ) {
509- return filterPackages (packageFragment .searchFilter , packageFragment .appSortBy , packageFragment .appOrderBy );
540+ return filterPackages (packageFragment .searchFilter , packageFragment .appFilterBy , packageFragment .appOrderBy , packageFragment . appSortBy );
510541 }
511542
512- private List <PackageInfo > filterPackages (String filter , final MyApplication .AppSortBy sortBy , final MyApplication .AppOrderBy orderBy ) {
543+ private List <PackageInfo > filterPackages (String filter , final MyApplication .AppSortBy filterBy , final MyApplication .AppOrderBy orderBy , final MyApplication . AppSortBy sortBy ) {
513544 final Context context = MyApplication .getInstance ().getApplicationContext ();
514545 final PackageManager pm = context .getPackageManager ();
515546 final List <PackageInfo > installedPackages = pm .getInstalledPackages (PackageManager .GET_META_DATA );
@@ -564,8 +595,17 @@ protected void onPostExecute(List<PackageInfo> installedPackages) {
564595 if (pi .packageName .equals (MyApplication .getInstance ().getPackageName ())) {
565596 continue ;
566597 }
567- String t1 = pi .applicationInfo .loadLabel (pm ).toString ();
568- if (packageFragment .searchFilter .trim ().isEmpty () || t1 .toLowerCase ().contains (packageFragment .searchFilter .toLowerCase ())) {
598+ String t1 = "" ;
599+ String t2 = packageFragment .searchFilter .trim ();
600+ switch (packageFragment .appFilterBy ) {
601+ case APPNAME :
602+ t1 = pi .applicationInfo .loadLabel (pm ).toString ();
603+ break ;
604+ case PKGNAME :
605+ t1 = pi .packageName ;
606+ break ;
607+ }
608+ if (t2 .isEmpty () || t1 .toLowerCase ().contains (t2 .toLowerCase ())) {
569609 final Preference preference = packageFragment .buildPackagePreferences (pm , pi );
570610 packageFragment .mFilterPreferenceScreen .addPreference (preference );
571611 }
0 commit comments