Skip to content

Commit e59d25c

Browse files
author
isayan
committed
support order by / sort by
1 parent 4fe186a commit e59d25c

File tree

5 files changed

+111
-33
lines changed

5 files changed

+111
-33
lines changed

android_app/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
applicationId "tun.proxy"
99
minSdkVersion 21
1010
targetSdkVersion 29
11-
versionCode 100210
11+
versionCode 100220
1212
versionName VERSION_NAME
1313
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1414
externalNativeBuild {

android_app/app/src/main/java/tun/proxy/MyApplication.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import java.util.Set;
99

1010
public class MyApplication extends Application {
11-
public static final String PREF_VPN_MODE = "vpn_connection_mode";
11+
private final static String PREF_VPN_MODE = "pref_vpn_connection_mode";
12+
private final static String PREF_APP_KEY[] = {"pref_vpn_disallowed_application", "pref_vpn_allowed_application"};
1213

1314
private static MyApplication instance;
1415

@@ -23,10 +24,9 @@ public void onCreate() {
2324
}
2425

2526
public enum VPNMode {DISALLOW, ALLOW};
26-
2727
public enum AppSortBy {APPNAME, PKGNAME};
28-
29-
private final String pref_key[] = {"vpn_disallowed_application", "vpn_allowed_application"};
28+
public enum AppOrderBy {ASC, DESC};
29+
public enum AppFiltertBy {APPNAME, PKGNAME};
3030

3131
public VPNMode loadVPNMode() {
3232
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
@@ -37,21 +37,20 @@ public VPNMode loadVPNMode() {
3737
public void storeVPNMode(VPNMode mode) {
3838
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
3939
final SharedPreferences.Editor editor = prefs.edit();
40-
editor.putString(PREF_VPN_MODE, mode.name());
40+
editor.putString(PREF_VPN_MODE, mode.name()).apply();
4141
return;
4242
}
4343

4444
public Set<String> loadVPNApplication(VPNMode mode) {
4545
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
46-
Set<String> preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet<String>());
46+
Set<String> preference = prefs.getStringSet(PREF_APP_KEY[mode.ordinal()], new HashSet<String>());
4747
return preference;
4848
}
4949

5050
public void storeVPNApplication(VPNMode mode, final Set<String> set) {
5151
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
5252
final SharedPreferences.Editor editor = prefs.edit();
53-
editor.putStringSet(pref_key[mode.ordinal()], set);
54-
editor.commit();
53+
editor.putStringSet(PREF_APP_KEY[mode.ordinal()], set).apply();
5554
return;
5655
}
5756

android_app/app/src/main/java/tun/proxy/SettingsActivity.java

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Context;
55
import android.content.DialogInterface;
66
import android.content.Intent;
7+
import android.content.SharedPreferences;
78
import android.content.pm.PackageInfo;
89
import android.content.pm.PackageManager;
910
import 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);

android_app/app/src/main/res/menu/menu_search.xml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,50 @@
88
app:showAsAction="always"
99
android:orderInCategory="100"
1010
app:actionViewClass="android.widget.SearchView" />
11-
1211
<group
1312
android:id="@+id/menu_sort_group"
1413
android:checkableBehavior="single"
1514
android:enabled="true"
1615
android:menuCategory="container"
16+
android:orderInCategory="110"
1717
android:visible="true">
1818
<item
1919
android:id="@+id/menu_sort_app_name"
2020
android:checked="true"
2121
android:title="sort by app name"
22+
android:orderInCategory="111"
2223
app:showAsAction="never" />
2324
<item
2425
android:id="@+id/menu_sort_pkg_name"
2526
android:title="sort by package name"
27+
android:orderInCategory="112"
2628
app:showAsAction="never" />
2729
</group>
28-
30+
<item
31+
android:id="@+id/menu_sort_by"
32+
android:title="order by"
33+
android:orderInCategory="200"
34+
app:showAsAction="never">
35+
<menu>
36+
<group
37+
android:id="@+id/menu_order_group"
38+
android:checkableBehavior="single"
39+
android:enabled="true"
40+
android:menuCategory="container"
41+
android:orderInCategory="210"
42+
android:visible="true">
43+
<item
44+
android:id="@+id/menu_sort_order_asc"
45+
android:checked="true"
46+
android:title="Asc"
47+
android:orderInCategory="211"
48+
app:showAsAction="never" />
49+
<item
50+
android:id="@+id/menu_sort_order_desc"
51+
android:title="Desc"
52+
android:orderInCategory="212"
53+
app:showAsAction="never" />
54+
</group>
55+
</menu>
56+
</item>
2957
</menu>

android_app/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ android.useAndroidX=true
1919
android.enableJetifier=true
2020

2121
APP_NAME=TunProxy
22-
VERSION_NAME=1.2.1
22+
VERSION_NAME=1.2.2
2323

2424
productKeyStore=../changeit.jks
2525
productKeyAlias=key0

0 commit comments

Comments
 (0)