29
29
import java .util .List ;
30
30
import java .util .Map ;
31
31
import java .util .Set ;
32
+ import java .util .concurrent .ExecutorService ;
33
+ import java .util .concurrent .Executors ;
32
34
33
35
public class SettingsActivity extends AppCompatActivity {
34
36
private static final String TAG = "SettingsActivity" ;
@@ -186,6 +188,8 @@ protected static class PackageListFragment extends PreferenceFragmentCompat
186
188
private final static String PREF_VPN_APPLICATION_SORTBY = "pref_vpn_application_app_sortby" ;
187
189
private final static String PREF_VPN_APPLICATION_ORDERBY = "pref_vpn_application_app_orderby" ;
188
190
191
+ private final AsyncTaskProgress task ;
192
+
189
193
private MyApplication .VPNMode mode ;
190
194
private MyApplication .AppSortBy appSortBy = MyApplication .AppSortBy .APPNAME ;
191
195
private MyApplication .AppOrderBy appOrderBy = MyApplication .AppOrderBy .ASC ;
@@ -194,6 +198,7 @@ protected static class PackageListFragment extends PreferenceFragmentCompat
194
198
public PackageListFragment (MyApplication .VPNMode mode ) {
195
199
super ();
196
200
this .mode = mode ;
201
+ this .task = new AsyncTaskProgress (this );
197
202
}
198
203
199
204
@ Override
@@ -264,14 +269,18 @@ protected void filter(String filter, final MyApplication.AppSortBy sortBy, final
264
269
storeSelectedPackageSet (selected );
265
270
266
271
this .removeAllPreferenceScreen ();
267
- final AsyncTaskProgress task = new AsyncTaskProgress (this );
268
- task .execute ("" );
272
+ // final AsyncTaskProgress task = new AsyncTaskProgress(this);
273
+ task .execute ();
269
274
// this.filterPackagesPreferences(filter, sortBy, orderBy);
270
275
}
271
276
272
277
@ Override
273
278
public void onPause () {
274
279
super .onPause ();
280
+ if (this .task != null ) {
281
+ this .task .cancel (true );
282
+ }
283
+
275
284
Set <String > selected = this .getAllSelectedPackageSet ();
276
285
storeSelectedPackageSet (selected );
277
286
SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (MyApplication .getInstance ().getApplicationContext ());
@@ -475,6 +484,11 @@ public boolean onClose() {
475
484
}
476
485
}
477
486
487
+ /*
488
+ * AsyncTask
489
+ * https://developer.android.com/reference/android/os/AsyncTask
490
+ * Deprecated in API level R
491
+ * */
478
492
public static class AsyncTaskProgress extends AsyncTask <String , String , List <PackageInfo >> {
479
493
480
494
final PackageListFragment packageFragment ;
@@ -512,6 +526,11 @@ public int compare(PackageInfo o1, PackageInfo o2) {
512
526
t2 = o2 .packageName ;
513
527
break ;
514
528
}
529
+ // try {
530
+ // Thread.sleep(100);
531
+ // } catch (InterruptedException e) {
532
+ // e.printStackTrace();
533
+ // }
515
534
if (MyApplication .AppOrderBy .ASC .equals (orderBy ))
516
535
return t1 .compareTo (t2 );
517
536
else
@@ -520,6 +539,7 @@ public int compare(PackageInfo o1, PackageInfo o2) {
520
539
});
521
540
final Map <String , Boolean > installedPackageMap = new HashMap <>();
522
541
for (final PackageInfo pi : installedPackages ) {
542
+ if (isCancelled ()) continue ;
523
543
// exclude self package
524
544
if (pi .packageName .equals (MyApplication .getInstance ().getPackageName ())) {
525
545
continue ;
0 commit comments