44import android .content .Context ;
55import android .content .DialogInterface ;
66import android .content .Intent ;
7+ import android .content .SharedPreferences ;
78import android .content .pm .PackageInfo ;
89import android .content .pm .PackageManager ;
910import android .os .Build ;
@@ -38,18 +39,18 @@ protected void onCreate(Bundle savedInstanceState) {
3839 setContentView (R .layout .activity_settings );
3940 if (savedInstanceState == null ) {
4041 getSupportFragmentManager ()
41- .beginTransaction ()
42- .replace (R .id .activity_settings , new SettingsFragment (), "preference_root" )
43- .commit ();
42+ .beginTransaction ()
43+ .replace (R .id .activity_settings , new SettingsFragment (), "preference_root" )
44+ .commit ();
4445 } else {
4546 setTitle (savedInstanceState .getCharSequence (TITLE_TAG ));
4647 }
4748 getSupportFragmentManager ().addOnBackStackChangedListener (new FragmentManager .OnBackStackChangedListener () {
4849 @ Override
4950 public void onBackStackChanged () {
50- if (getSupportFragmentManager ().getBackStackEntryCount () == 0 ) {
51- setTitle (R .string .title_activity_settings );
52- }
51+ if (getSupportFragmentManager ().getBackStackEntryCount () == 0 ) {
52+ setTitle (R .string .title_activity_settings );
53+ }
5354 }
5455 });
5556 ActionBar actionBar = getSupportActionBar ();
@@ -162,16 +163,14 @@ public void onClick(DialogInterface dialog, int which) {
162163 }
163164
164165 @ TargetApi (Build .VERSION_CODES .HONEYCOMB )
165- public static class DisallowedPackageListFragment extends PackageListFragment
166- {
166+ public static class DisallowedPackageListFragment extends PackageListFragment {
167167 public DisallowedPackageListFragment () {
168168 super (MyApplication .VPNMode .DISALLOW );
169169 }
170170 }
171171
172172 @ TargetApi (Build .VERSION_CODES .HONEYCOMB )
173- public static class AllowedPackageListFragment extends PackageListFragment
174- {
173+ public static class AllowedPackageListFragment extends PackageListFragment {
175174 public AllowedPackageListFragment () {
176175 super (MyApplication .VPNMode .ALLOW );
177176 }
@@ -181,9 +180,12 @@ public AllowedPackageListFragment() {
181180 protected static class PackageListFragment extends PreferenceFragmentCompat
182181 implements SearchView .OnQueryTextListener , SearchView .OnCloseListener {
183182 private final Map <String , Boolean > mAllPackageInfoMap = new HashMap <>();
183+ private final static String PREF_VPN_APPLICATION_SORTBY = "pref_vpn_application_app_sortby" ;
184+ private final static String PREF_VPN_APPLICATION_ORDERBY = "pref_vpn_application_app_orderby" ;
184185
185186 private MyApplication .VPNMode mode ;
186187 private MyApplication .AppSortBy appSortBy = MyApplication .AppSortBy .APPNAME ;
188+ private MyApplication .AppOrderBy appOrderBy = MyApplication .AppOrderBy .ASC ;
187189 private PreferenceScreen mFilterPreferenceScreen ;
188190
189191 public PackageListFragment (MyApplication .VPNMode mode ) {
@@ -203,43 +205,76 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
203205 super .onCreateOptionsMenu (menu , inflater );
204206 // Menuの設定
205207 inflater .inflate (R .menu .menu_search , menu );
206-
207208 //MenuCompat.setGroupDividerEnabled(menu, true);
208209
209- final MenuItem menuItem = menu .findItem (R .id .menu_search_item );
210- this .searchView = (SearchView ) menuItem .getActionView ();
210+ final MenuItem menuSearch = menu .findItem (R .id .menu_search_item );
211+ this .searchView = (SearchView ) menuSearch .getActionView ();
211212 this .searchView .setOnQueryTextListener (this );
212213 this .searchView .setOnCloseListener (this );
213214 this .searchView .setSubmitButtonEnabled (false );
215+
216+ switch (this .appSortBy ) {
217+ case APPNAME : {
218+ final MenuItem menuItem = menu .findItem (R .id .menu_sort_app_name );
219+ menuItem .setChecked (true );
220+ break ;
221+ }
222+ case PKGNAME : {
223+ final MenuItem menuItem = menu .findItem (R .id .menu_sort_pkg_name );
224+ menuItem .setChecked (true );
225+ break ;
226+ }
227+ }
228+
229+ switch (this .appOrderBy ) {
230+ case ASC : {
231+ final MenuItem menuItem = menu .findItem (R .id .menu_sort_order_asc );
232+ menuItem .setChecked (true );
233+ break ;
234+ }
235+ case DESC : {
236+ final MenuItem menuItem = menu .findItem (R .id .menu_sort_order_desc );
237+ menuItem .setChecked (true );
238+ break ;
239+ }
240+ }
241+
214242 }
215243
216244 private String searchFilter = "" ;
217245 private SearchView searchView ;
218246
219247 protected void filter (String filter ) {
220- this .filter (filter , this .appSortBy );
248+ this .filter (filter , this .appSortBy , this . appOrderBy );
221249 }
222250
223- protected void filter (String filter , final MyApplication .AppSortBy sortBy ) {
251+ protected void filter (String filter , final MyApplication .AppSortBy sortBy , final MyApplication . AppOrderBy orderBy ) {
224252 if (filter == null ) {
225253 filter = searchFilter ;
226254 } else {
227255 searchFilter = filter ;
228256 }
229257 this .appSortBy = sortBy ;
258+ this .appOrderBy = orderBy ;
230259
231260 Set <String > selected = this .getAllSelectedPackageSet ();
232261 storeSelectedPackageSet (selected );
233262
234263 this .removeAllPreferenceScreen ();
235- this .filterPackagesPreferences (filter , sortBy );
264+ this .filterPackagesPreferences (filter , sortBy , orderBy );
236265 }
237266
238267 @ Override
239268 public void onPause () {
240269 super .onPause ();
241270 Set <String > selected = this .getAllSelectedPackageSet ();
242271 storeSelectedPackageSet (selected );
272+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (MyApplication .getInstance ().getApplicationContext ());
273+ SharedPreferences .Editor edit = prefs .edit ();
274+ edit .putString (PREF_VPN_APPLICATION_SORTBY , this .appSortBy .name ());
275+ edit .putString (PREF_VPN_APPLICATION_ORDERBY , this .appOrderBy .name ());
276+ edit .apply ();
277+ Log .i ("onPause" , this .appSortBy .name ());
243278 }
244279
245280 @ Override
@@ -249,14 +284,20 @@ public void onResume() {
249284 for (String pkgName : loadMap ) {
250285 this .mAllPackageInfoMap .put (pkgName , loadMap .contains (pkgName ));
251286 }
287+ SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (MyApplication .getInstance ().getApplicationContext ());
288+ String appSortBy = prefs .getString (PREF_VPN_APPLICATION_SORTBY , MyApplication .AppSortBy .APPNAME .name ());
289+ String appOrderBy = prefs .getString (PREF_VPN_APPLICATION_ORDERBY , MyApplication .AppOrderBy .ASC .name ());
290+ this .appSortBy = Enum .valueOf (MyApplication .AppSortBy .class , appSortBy );
291+ this .appOrderBy = Enum .valueOf (MyApplication .AppOrderBy .class , appOrderBy );
292+ Log .i ("onResume" , this .appSortBy .name ());
252293 filter (null );
253294 }
254295
255296 private void removeAllPreferenceScreen () {
256297 mFilterPreferenceScreen .removeAll ();
257298 }
258299
259- private void filterPackagesPreferences (String filter , final MyApplication .AppSortBy sortBy ) {
300+ private void filterPackagesPreferences (String filter , final MyApplication .AppSortBy sortBy , final MyApplication . AppOrderBy orderBy ) {
260301 final Context context = MyApplication .getInstance ().getApplicationContext ();
261302 final PackageManager pm = context .getPackageManager ();
262303 final List <PackageInfo > installedPackages = pm .getInstalledPackages (PackageManager .GET_META_DATA );
@@ -275,7 +316,10 @@ public int compare(PackageInfo o1, PackageInfo o2) {
275316 t2 = o2 .packageName ;
276317 break ;
277318 }
278- return t1 .compareTo (t2 );
319+ if (MyApplication .AppOrderBy .ASC .equals (orderBy ))
320+ return t1 .compareTo (t2 );
321+ else
322+ return t2 .compareTo (t1 );
279323 }
280324 });
281325
@@ -369,7 +413,6 @@ private Set<String> getAllSelectedPackageSet() {
369413 }
370414
371415 private void storeSelectedPackageSet (final Set <String > set ) {
372- MyApplication .getInstance ().storeVPNMode (this .mode );
373416 MyApplication .getInstance ().storeVPNApplication (this .mode , set );
374417 }
375418
@@ -382,11 +425,19 @@ public boolean onOptionsItemSelected(MenuItem item) {
382425 return true ;
383426 case R .id .menu_sort_app_name :
384427 item .setChecked (!item .isChecked ());
385- filter (null , MyApplication .AppSortBy .APPNAME );
428+ filter (null , MyApplication .AppSortBy .APPNAME , appOrderBy );
386429 break ;
387430 case R .id .menu_sort_pkg_name :
388431 item .setChecked (!item .isChecked ());
389- filter (null , MyApplication .AppSortBy .PKGNAME );
432+ filter (null , MyApplication .AppSortBy .PKGNAME , appOrderBy );
433+ break ;
434+ case R .id .menu_sort_order_asc :
435+ item .setChecked (!item .isChecked ());
436+ filter (null , appSortBy , MyApplication .AppOrderBy .ASC );
437+ break ;
438+ case R .id .menu_sort_order_desc :
439+ item .setChecked (!item .isChecked ());
440+ filter (null , appSortBy , MyApplication .AppOrderBy .DESC );
390441 break ;
391442 }
392443 return super .onOptionsItemSelected (item );
0 commit comments