Skip to content

Commit 400b758

Browse files
author
isayan
committed
bugfix pref
1 parent 4e205fa commit 400b758

File tree

9 files changed

+170
-141
lines changed

9 files changed

+170
-141
lines changed

Readme-ja.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Disallow Application と Allow Application の2つのモードがあります
5858
画面上部の検索アイコン(![Menu](images/Search.png))から、アプリケーションを絞り込めます。
5959
アプリケーション名に指定したキーワードを含む、アプリケーションのみが表示されます。
6060

61-
プリケーションリストは、画面上部のメニューアイコン(![メニュー]images / Menu.png))からソートできます。
61+
プリケーションリストは、画面上部のメニューアイコン(![Menu](images/Menu.png))からソートできます。
6262

6363
* アプリ名で並べ替え
6464
* アプリケーション名でアプリケーションリストを並べ替えます。

android_app/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ android {
66
applicationId "tun.proxy"
77
minSdkVersion 21
88
targetSdkVersion 27
9-
versionCode 100011
9+
versionCode 100012
1010
versionName VERSION_NAME
1111
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
externalNativeBuild {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void onClick(View v) {
6666
// if (intent != null) {
6767
// startActivityForResult(intent, REQUEST_CERT);
6868
// } else {
69-
startVpn();
69+
startVpn();
7070
// }
7171
// }
7272
}
@@ -272,7 +272,7 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
272272
switch (requestCode) {
273273
case 8000: {
274274
if (grantResults.length > 0
275-
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
275+
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
276276
} else {
277277
requestPermission();
278278
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.content.Context;
55
import android.content.SharedPreferences;
66
import android.preference.PreferenceManager;
7+
78
import tun.utils.CertificateUtil;
89

910
import java.security.cert.CertificateEncodingException;
@@ -45,14 +46,12 @@ public enum VPNMode {DISALLOW, ALLOW};
4546

4647
public enum AppSortBy {APPNAME, PKGNAME};
4748

48-
// public final String vpn_mode_key[] = {VPNMode.DISALLOW.name(), VPNMode.ALLOW.name()};
49-
5049
private final String pref_key[] = {"vpn_disallowed_application", "vpn_allowed_application"};
5150

5251
public VPNMode loadVPNMode() {
5352
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
5453
String vpn_mode = sharedPreferences.getString("vpn_connection_mode", MyApplication.VPNMode.DISALLOW.name());
55-
return VPNMode.valueOf ( vpn_mode );
54+
return VPNMode.valueOf(vpn_mode);
5655
}
5756

5857
public void storeVPNMode(VPNMode mode) {
@@ -64,7 +63,7 @@ public void storeVPNMode(VPNMode mode) {
6463

6564
public Set<String> loadVPNApplication(VPNMode mode) {
6665
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
67-
Set<String> preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet<String>() );
66+
Set<String> preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet<String>());
6867
return preference;
6968
}
7069

@@ -73,7 +72,7 @@ public void storeVPNApplication(VPNMode mode, final Set<String> set) {
7372
final SharedPreferences.Editor editor = prefs.edit();
7473
editor.putStringSet(pref_key[mode.ordinal()], set);
7574
editor.commit();
76-
return ;
75+
return;
7776
}
7877

7978
}

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

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,28 @@
33
import android.annotation.TargetApi;
44
import android.content.Context;
55
import android.content.Intent;
6-
import android.content.SharedPreferences;
76
import android.content.pm.PackageInfo;
87
import android.content.pm.PackageManager;
98
import android.os.Build;
109
import android.os.Bundle;
1110
import android.preference.*;
12-
import android.util.Log;
1311
import android.view.Menu;
1412
import android.view.MenuInflater;
1513
import android.view.MenuItem;
16-
import android.view.View;
1714
import android.widget.*;
1815

1916
import java.util.Collections;
2017
import java.util.Comparator;
18+
import java.util.HashMap;
2119
import java.util.HashSet;
2220
import java.util.List;
21+
import java.util.Map;
2322
import java.util.Set;
2423

25-
public class SimplePreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
24+
import static android.preference.Preference.*;
25+
26+
public class SimplePreferenceFragment extends PreferenceFragment
27+
implements OnPreferenceClickListener {
2628
public static final String VPN_CONNECTION_MODE = "vpn_connection_mode";
2729
public static final String VPN_DISALLOWED_APPLICATION_LIST = "vpn_disallowed_application_list";
2830
public static final String VPN_ALLOWED_APPLICATION_LIST = "vpn_allowed_application_list";
@@ -34,8 +36,8 @@ public void onCreate(Bundle savedInstanceState) {
3436
setHasOptionsMenu(true);
3537

3638
/* Allowed / Disallowed Application */
37-
final ListPreference pkg_selection = (ListPreference) this.findPreference(VPN_CONNECTION_MODE);
38-
pkg_selection.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
39+
final ListPreference prefPackage = (ListPreference) this.findPreference(VPN_CONNECTION_MODE);
40+
prefPackage.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
3941
@Override
4042
public boolean onPreferenceChange(Preference preference, Object value) {
4143
if (preference instanceof ListPreference) {
@@ -54,23 +56,23 @@ public boolean onPreferenceChange(Preference preference, Object value) {
5456
return true;
5557
}
5658
});
57-
pkg_selection.setSummary(pkg_selection.getEntry());
59+
prefPackage.setSummary(prefPackage.getEntry());
5860
PreferenceScreen disallow = (PreferenceScreen) findPreference(VPN_DISALLOWED_APPLICATION_LIST);
5961
PreferenceScreen allow = (PreferenceScreen) findPreference(VPN_ALLOWED_APPLICATION_LIST);
60-
disallow.setEnabled(MyApplication.VPNMode.DISALLOW.name().equals(pkg_selection.getValue()));
61-
allow.setEnabled(MyApplication.VPNMode.ALLOW.name().equals(pkg_selection.getValue()));
62+
disallow.setEnabled(MyApplication.VPNMode.DISALLOW.name().equals(prefPackage.getValue()));
63+
allow.setEnabled(MyApplication.VPNMode.ALLOW.name().equals(prefPackage.getValue()));
6264

6365
findPreference(VPN_DISALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this);
6466
findPreference(VPN_ALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this);
65-
6667
}
6768

6869
@Override
6970
public boolean onOptionsItemSelected(MenuItem item) {
7071
int id = item.getItemId();
71-
if (id == android.R.id.home) {
72-
startActivity(new Intent(getActivity(), MainActivity.class));
73-
return true;
72+
switch (id) {
73+
case android.R.id.home:
74+
startActivity(new Intent(getActivity(), MainActivity.class));
75+
return true;
7476
}
7577
return super.onOptionsItemSelected(item);
7678
}
@@ -99,12 +101,14 @@ private void transitionFragment(PreferenceFragment nextPreferenceFragment) {
99101
.commit();
100102
}
101103

102-
103104
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
104-
public static class PackageListPreferenceFragment extends PreferenceFragment implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
105+
public static class PackageListPreferenceFragment extends PreferenceFragment
106+
implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
107+
final private Map<String, Boolean> mAllPackageInfoMap = new HashMap<String, Boolean>();
108+
105109
private MyApplication.VPNMode mode = MyApplication.VPNMode.DISALLOW;
106110
private MyApplication.AppSortBy appSortBy = MyApplication.AppSortBy.APPNAME;
107-
private PreferenceScreen mRootPreferenceScreen;
111+
private PreferenceScreen mFilterPreferenceScreen;
108112

109113
public static PackageListPreferenceFragment newInstance(MyApplication.VPNMode mode) {
110114
final PackageListPreferenceFragment fragment = new PackageListPreferenceFragment();
@@ -116,8 +120,8 @@ public static PackageListPreferenceFragment newInstance(MyApplication.VPNMode mo
116120
public void onCreate(Bundle savedInstanceState) {
117121
super.onCreate(savedInstanceState);
118122
setHasOptionsMenu(true);
119-
mRootPreferenceScreen = getPreferenceManager().createPreferenceScreen(getActivity());
120-
setPreferenceScreen(mRootPreferenceScreen);
123+
mFilterPreferenceScreen = getPreferenceManager().createPreferenceScreen(getActivity());
124+
setPreferenceScreen(mFilterPreferenceScreen);
121125
}
122126

123127
private String searchFilter = "";
@@ -130,13 +134,15 @@ protected void filter(String filter) {
130134
protected void filter(String filter, final MyApplication.AppSortBy sortBy) {
131135
if (filter == null) {
132136
filter = searchFilter;
133-
}
134-
else {
137+
} else {
135138
searchFilter = filter;
136139
}
137-
removeAllPreferenceScreen();
138-
buildPackagesPreferences(filter, sortBy);
139-
loadSelectedPackage();
140+
141+
Set<String> selected = this.getAllSelectedPackageSet();
142+
storeSelectedPackageSet(selected);
143+
144+
this.removeAllPreferenceScreen();
145+
this.filterPackagesPreferences(filter, sortBy);
140146
}
141147

142148
@Override
@@ -147,95 +153,115 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
147153

148154
final MenuItem menuItem = menu.findItem(R.id.menu_search_item);
149155

150-
this.searchView = (SearchView)menuItem.getActionView();
156+
this.searchView = (SearchView) menuItem.getActionView();
151157
this.searchView.setOnQueryTextListener(this);
152158
this.searchView.setOnCloseListener(this);
153159
this.searchView.setSubmitButtonEnabled(false);
160+
154161
}
155162

156163
@Override
157-
public void onPause() {
164+
public void onPause() {
158165
super.onPause();
159-
storeSelectedPackageSet(this.getSelectedPackageSet());
166+
Set<String> selected = this.getAllSelectedPackageSet();
167+
storeSelectedPackageSet(selected);
160168
}
161169

162170
@Override
163171
public void onResume() {
164172
super.onResume();
173+
Set<String> loadMap = MyApplication.getInstance().loadVPNApplication(mode);
174+
for (String pkgName : loadMap) {
175+
this.mAllPackageInfoMap.put(pkgName, loadMap.contains(pkgName));
176+
}
165177
filter(null);
166178
}
167179

168180
private void removeAllPreferenceScreen() {
169-
mRootPreferenceScreen.removeAll();
181+
mFilterPreferenceScreen.removeAll();
170182
}
171183

172-
private void buildPackagesPreferences(String filter, final MyApplication.AppSortBy sortBy) {
184+
private void filterPackagesPreferences(String filter, final MyApplication.AppSortBy sortBy) {
173185
final Context context = MyApplication.getInstance().getApplicationContext();
174186
final PackageManager pm = context.getPackageManager();
175187
final List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
176188
Collections.sort(installedPackages, new Comparator<PackageInfo>() {
177189
@Override
178190
public int compare(PackageInfo o1, PackageInfo o2) {
179-
if (sortBy == MyApplication.AppSortBy.APPNAME) {
180-
String t1 = o1.applicationInfo.loadLabel(pm).toString();
181-
String t2 = o2.applicationInfo.loadLabel(pm).toString();
182-
return t1.compareTo(t2);
183-
}
184-
else {
185-
String t1 = o1.packageName;
186-
String t2 = o2.packageName;
187-
return t1.compareTo(t2);
191+
String t1 = "";
192+
String t2 = "";
193+
switch (sortBy) {
194+
case APPNAME:
195+
t1 = o1.applicationInfo.loadLabel(pm).toString();
196+
t2 = o2.applicationInfo.loadLabel(pm).toString();
197+
break;
198+
case PKGNAME:
199+
t1 = o1.packageName;
200+
t2 = o2.packageName;
201+
break;
188202
}
203+
return t1.compareTo(t2);
189204
}
190205
});
191206
for (final PackageInfo pi : installedPackages) {
192207
String t1 = pi.applicationInfo.loadLabel(pm).toString();
193208
if (filter.trim().isEmpty() || t1.toLowerCase().contains(filter.toLowerCase())) {
194209
final Preference preference = buildPackagePreferences(pm, pi);
195-
mRootPreferenceScreen.addPreference(preference);
210+
mFilterPreferenceScreen.addPreference(preference);
196211
}
197212
}
198213
}
199214

200215
private Preference buildPackagePreferences(final PackageManager pm, final PackageInfo pi) {
201-
final CheckBoxPreference p = new CheckBoxPreference(getActivity());
202-
p.setIcon(pi.applicationInfo.loadIcon(pm));
203-
p.setTitle(pi.applicationInfo.loadLabel(pm).toString());
204-
p.setSummary(pi.packageName);
205-
return p;
216+
final CheckBoxPreference prefCheck = new CheckBoxPreference(getActivity());
217+
prefCheck.setIcon(pi.applicationInfo.loadIcon(pm));
218+
prefCheck.setTitle(pi.applicationInfo.loadLabel(pm).toString());
219+
prefCheck.setSummary(pi.packageName);
220+
boolean ckecked = this.mAllPackageInfoMap.containsKey(pi.packageName) ? this.mAllPackageInfoMap.get(pi.packageName) : false;
221+
prefCheck.setChecked(ckecked);
222+
OnPreferenceClickListener click = new OnPreferenceClickListener() {
223+
@Override
224+
public boolean onPreferenceClick(Preference preference) {
225+
mAllPackageInfoMap.put(prefCheck.getSummary().toString(), prefCheck.isChecked());
226+
return false;
227+
}
228+
};
229+
prefCheck.setOnPreferenceClickListener(click);
230+
return prefCheck;
206231
}
207232

208-
private Set<String> getSelectedPackageSet() {
209-
Set<String> selected = new HashSet<>();
210-
for (int i = 0; i < this.mRootPreferenceScreen.getPreferenceCount(); i++) {
211-
Preference pref = this.mRootPreferenceScreen.getPreference(i);
233+
private Set<String> getFilterSelectedPackageSet() {
234+
final Set<String> selected = new HashSet<>();
235+
for (int i = 0; i < this.mFilterPreferenceScreen.getPreferenceCount(); i++) {
236+
Preference pref = this.mFilterPreferenceScreen.getPreference(i);
212237
if ((pref instanceof CheckBoxPreference)) {
213-
CheckBoxPreference pref_check = (CheckBoxPreference) pref;
214-
if (pref_check.isChecked()) {
215-
selected.add(pref_check.getSummary().toString());
238+
CheckBoxPreference prefCheck = (CheckBoxPreference) pref;
239+
if (prefCheck.isChecked()) {
240+
selected.add(prefCheck.getSummary().toString());
216241
}
217242
}
218243
}
219244
return selected;
220245
}
221246

222247
private void setSelectedPackageSet(Set<String> selected) {
223-
for (int i = 0; i < this.mRootPreferenceScreen.getPreferenceCount(); i++) {
224-
Preference pref = this.mRootPreferenceScreen.getPreference(i);
248+
for (int i = 0; i < this.mFilterPreferenceScreen.getPreferenceCount(); i++) {
249+
Preference pref = this.mFilterPreferenceScreen.getPreference(i);
225250
if ((pref instanceof CheckBoxPreference)) {
226-
CheckBoxPreference pref_check = (CheckBoxPreference) pref;
227-
if (selected.contains(pref_check.getSummary())) {
228-
pref_check.setChecked(true);
251+
CheckBoxPreference prefCheck = (CheckBoxPreference) pref;
252+
if (selected.contains((prefCheck.getSummary()))) {
253+
prefCheck.setChecked(true);
229254
}
230255
}
231256
}
232257
}
233258

234-
private void loadSelectedPackage() {
235-
this.getArguments();
236-
mode = MyApplication.getInstance().loadVPNMode();
237-
Set<String> selected = MyApplication.getInstance().loadVPNApplication(mode);
238-
setSelectedPackageSet(selected);
259+
private Set<String> getAllSelectedPackageSet() {
260+
Set<String> selected = this.getFilterSelectedPackageSet();
261+
for (Map.Entry<String, Boolean> value : this.mAllPackageInfoMap.entrySet()) {
262+
if (value.getValue()) selected.add(value.getKey());
263+
}
264+
return selected;
239265
}
240266

241267
private void storeSelectedPackageSet(final Set<String> set) {
@@ -270,8 +296,7 @@ public boolean onQueryTextSubmit(String query) {
270296
if (!query.trim().isEmpty()) {
271297
filter(query);
272298
return true;
273-
}
274-
else {
299+
} else {
275300
filter("");
276301
return true;
277302
}
@@ -284,6 +309,8 @@ public boolean onQueryTextChange(String newText) {
284309

285310
@Override
286311
public boolean onClose() {
312+
Set<String> selected = this.getAllSelectedPackageSet();
313+
storeSelectedPackageSet(selected);
287314
filter("");
288315
return false;
289316
}

android_app/app/src/main/java/tun/proxy/service/Tun2HttpVpnService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ private Builder getBuilder() {
152152
MyApplication app = (MyApplication) this.getApplication();
153153
if (app.loadVPNMode() == MyApplication.VPNMode.DISALLOW) {
154154
Set<String> disallow = app.loadVPNApplication(MyApplication.VPNMode.DISALLOW);
155-
Log.d(TAG, "disallowed:" + disallow.size());
155+
Log.d(TAG, "disallowed:" + disallow.size());
156156
builder.addDisallowedApplication(Arrays.asList(disallow.toArray(new String[0])));
157157
} else {
158158
Set<String> allow = app.loadVPNApplication(MyApplication.VPNMode.ALLOW);
159159
Log.d(TAG, "allowed:" + allow.size());
160160
builder.addAllowedApplication(Arrays.asList(allow.toArray(new String[0])));
161161
}
162162
} catch (PackageManager.NameNotFoundException e) {
163-
Log.e( TAG, e.getMessage(), e);
163+
Log.e(TAG, e.getMessage(), e);
164164
}
165165
}
166166

0 commit comments

Comments
 (0)