Skip to content

Commit

Permalink
bugfix pref
Browse files Browse the repository at this point in the history
  • Loading branch information
isayan committed Sep 22, 2019
1 parent 4e205fa commit 400b758
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Readme-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Disallow Application と Allow Application の2つのモードがあります
画面上部の検索アイコン(![Menu](images/Search.png))から、アプリケーションを絞り込めます。
アプリケーション名に指定したキーワードを含む、アプリケーションのみが表示されます。

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

* アプリ名で並べ替え
* アプリケーション名でアプリケーションリストを並べ替えます。
Expand Down
2 changes: 1 addition & 1 deletion android_app/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
applicationId "tun.proxy"
minSdkVersion 21
targetSdkVersion 27
versionCode 100011
versionCode 100012
versionName VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
Expand Down
4 changes: 2 additions & 2 deletions android_app/app/src/main/java/tun/proxy/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void onClick(View v) {
// if (intent != null) {
// startActivityForResult(intent, REQUEST_CERT);
// } else {
startVpn();
startVpn();
// }
// }
}
Expand Down Expand Up @@ -272,7 +272,7 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
switch (requestCode) {
case 8000: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
requestPermission();
}
Expand Down
9 changes: 4 additions & 5 deletions android_app/app/src/main/java/tun/proxy/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import tun.utils.CertificateUtil;

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

public enum AppSortBy {APPNAME, PKGNAME};

// public final String vpn_mode_key[] = {VPNMode.DISALLOW.name(), VPNMode.ALLOW.name()};

private final String pref_key[] = {"vpn_disallowed_application", "vpn_allowed_application"};

public VPNMode loadVPNMode() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String vpn_mode = sharedPreferences.getString("vpn_connection_mode", MyApplication.VPNMode.DISALLOW.name());
return VPNMode.valueOf ( vpn_mode );
return VPNMode.valueOf(vpn_mode);
}

public void storeVPNMode(VPNMode mode) {
Expand All @@ -64,7 +63,7 @@ public void storeVPNMode(VPNMode mode) {

public Set<String> loadVPNApplication(VPNMode mode) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Set<String> preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet<String>() );
Set<String> preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet<String>());
return preference;
}

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

}
151 changes: 89 additions & 62 deletions android_app/app/src/main/java/tun/proxy/SimplePreferenceFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.*;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SimplePreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
import static android.preference.Preference.*;

public class SimplePreferenceFragment extends PreferenceFragment
implements OnPreferenceClickListener {
public static final String VPN_CONNECTION_MODE = "vpn_connection_mode";
public static final String VPN_DISALLOWED_APPLICATION_LIST = "vpn_disallowed_application_list";
public static final String VPN_ALLOWED_APPLICATION_LIST = "vpn_allowed_application_list";
Expand All @@ -34,8 +36,8 @@ public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);

/* Allowed / Disallowed Application */
final ListPreference pkg_selection = (ListPreference) this.findPreference(VPN_CONNECTION_MODE);
pkg_selection.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
final ListPreference prefPackage = (ListPreference) this.findPreference(VPN_CONNECTION_MODE);
prefPackage.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
if (preference instanceof ListPreference) {
Expand All @@ -54,23 +56,23 @@ public boolean onPreferenceChange(Preference preference, Object value) {
return true;
}
});
pkg_selection.setSummary(pkg_selection.getEntry());
prefPackage.setSummary(prefPackage.getEntry());
PreferenceScreen disallow = (PreferenceScreen) findPreference(VPN_DISALLOWED_APPLICATION_LIST);
PreferenceScreen allow = (PreferenceScreen) findPreference(VPN_ALLOWED_APPLICATION_LIST);
disallow.setEnabled(MyApplication.VPNMode.DISALLOW.name().equals(pkg_selection.getValue()));
allow.setEnabled(MyApplication.VPNMode.ALLOW.name().equals(pkg_selection.getValue()));
disallow.setEnabled(MyApplication.VPNMode.DISALLOW.name().equals(prefPackage.getValue()));
allow.setEnabled(MyApplication.VPNMode.ALLOW.name().equals(prefPackage.getValue()));

findPreference(VPN_DISALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this);
findPreference(VPN_ALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), MainActivity.class));
return true;
switch (id) {
case android.R.id.home:
startActivity(new Intent(getActivity(), MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
Expand Down Expand Up @@ -99,12 +101,14 @@ private void transitionFragment(PreferenceFragment nextPreferenceFragment) {
.commit();
}


@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class PackageListPreferenceFragment extends PreferenceFragment implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
public static class PackageListPreferenceFragment extends PreferenceFragment
implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
final private Map<String, Boolean> mAllPackageInfoMap = new HashMap<String, Boolean>();

private MyApplication.VPNMode mode = MyApplication.VPNMode.DISALLOW;
private MyApplication.AppSortBy appSortBy = MyApplication.AppSortBy.APPNAME;
private PreferenceScreen mRootPreferenceScreen;
private PreferenceScreen mFilterPreferenceScreen;

public static PackageListPreferenceFragment newInstance(MyApplication.VPNMode mode) {
final PackageListPreferenceFragment fragment = new PackageListPreferenceFragment();
Expand All @@ -116,8 +120,8 @@ public static PackageListPreferenceFragment newInstance(MyApplication.VPNMode mo
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mRootPreferenceScreen = getPreferenceManager().createPreferenceScreen(getActivity());
setPreferenceScreen(mRootPreferenceScreen);
mFilterPreferenceScreen = getPreferenceManager().createPreferenceScreen(getActivity());
setPreferenceScreen(mFilterPreferenceScreen);
}

private String searchFilter = "";
Expand All @@ -130,13 +134,15 @@ protected void filter(String filter) {
protected void filter(String filter, final MyApplication.AppSortBy sortBy) {
if (filter == null) {
filter = searchFilter;
}
else {
} else {
searchFilter = filter;
}
removeAllPreferenceScreen();
buildPackagesPreferences(filter, sortBy);
loadSelectedPackage();

Set<String> selected = this.getAllSelectedPackageSet();
storeSelectedPackageSet(selected);

this.removeAllPreferenceScreen();
this.filterPackagesPreferences(filter, sortBy);
}

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

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

this.searchView = (SearchView)menuItem.getActionView();
this.searchView = (SearchView) menuItem.getActionView();
this.searchView.setOnQueryTextListener(this);
this.searchView.setOnCloseListener(this);
this.searchView.setSubmitButtonEnabled(false);

}

@Override
public void onPause() {
public void onPause() {
super.onPause();
storeSelectedPackageSet(this.getSelectedPackageSet());
Set<String> selected = this.getAllSelectedPackageSet();
storeSelectedPackageSet(selected);
}

@Override
public void onResume() {
super.onResume();
Set<String> loadMap = MyApplication.getInstance().loadVPNApplication(mode);
for (String pkgName : loadMap) {
this.mAllPackageInfoMap.put(pkgName, loadMap.contains(pkgName));
}
filter(null);
}

private void removeAllPreferenceScreen() {
mRootPreferenceScreen.removeAll();
mFilterPreferenceScreen.removeAll();
}

private void buildPackagesPreferences(String filter, final MyApplication.AppSortBy sortBy) {
private void filterPackagesPreferences(String filter, final MyApplication.AppSortBy sortBy) {
final Context context = MyApplication.getInstance().getApplicationContext();
final PackageManager pm = context.getPackageManager();
final List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
Collections.sort(installedPackages, new Comparator<PackageInfo>() {
@Override
public int compare(PackageInfo o1, PackageInfo o2) {
if (sortBy == MyApplication.AppSortBy.APPNAME) {
String t1 = o1.applicationInfo.loadLabel(pm).toString();
String t2 = o2.applicationInfo.loadLabel(pm).toString();
return t1.compareTo(t2);
}
else {
String t1 = o1.packageName;
String t2 = o2.packageName;
return t1.compareTo(t2);
String t1 = "";
String t2 = "";
switch (sortBy) {
case APPNAME:
t1 = o1.applicationInfo.loadLabel(pm).toString();
t2 = o2.applicationInfo.loadLabel(pm).toString();
break;
case PKGNAME:
t1 = o1.packageName;
t2 = o2.packageName;
break;
}
return t1.compareTo(t2);
}
});
for (final PackageInfo pi : installedPackages) {
String t1 = pi.applicationInfo.loadLabel(pm).toString();
if (filter.trim().isEmpty() || t1.toLowerCase().contains(filter.toLowerCase())) {
final Preference preference = buildPackagePreferences(pm, pi);
mRootPreferenceScreen.addPreference(preference);
mFilterPreferenceScreen.addPreference(preference);
}
}
}

private Preference buildPackagePreferences(final PackageManager pm, final PackageInfo pi) {
final CheckBoxPreference p = new CheckBoxPreference(getActivity());
p.setIcon(pi.applicationInfo.loadIcon(pm));
p.setTitle(pi.applicationInfo.loadLabel(pm).toString());
p.setSummary(pi.packageName);
return p;
final CheckBoxPreference prefCheck = new CheckBoxPreference(getActivity());
prefCheck.setIcon(pi.applicationInfo.loadIcon(pm));
prefCheck.setTitle(pi.applicationInfo.loadLabel(pm).toString());
prefCheck.setSummary(pi.packageName);
boolean ckecked = this.mAllPackageInfoMap.containsKey(pi.packageName) ? this.mAllPackageInfoMap.get(pi.packageName) : false;
prefCheck.setChecked(ckecked);
OnPreferenceClickListener click = new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
mAllPackageInfoMap.put(prefCheck.getSummary().toString(), prefCheck.isChecked());
return false;
}
};
prefCheck.setOnPreferenceClickListener(click);
return prefCheck;
}

private Set<String> getSelectedPackageSet() {
Set<String> selected = new HashSet<>();
for (int i = 0; i < this.mRootPreferenceScreen.getPreferenceCount(); i++) {
Preference pref = this.mRootPreferenceScreen.getPreference(i);
private Set<String> getFilterSelectedPackageSet() {
final Set<String> selected = new HashSet<>();
for (int i = 0; i < this.mFilterPreferenceScreen.getPreferenceCount(); i++) {
Preference pref = this.mFilterPreferenceScreen.getPreference(i);
if ((pref instanceof CheckBoxPreference)) {
CheckBoxPreference pref_check = (CheckBoxPreference) pref;
if (pref_check.isChecked()) {
selected.add(pref_check.getSummary().toString());
CheckBoxPreference prefCheck = (CheckBoxPreference) pref;
if (prefCheck.isChecked()) {
selected.add(prefCheck.getSummary().toString());
}
}
}
return selected;
}

private void setSelectedPackageSet(Set<String> selected) {
for (int i = 0; i < this.mRootPreferenceScreen.getPreferenceCount(); i++) {
Preference pref = this.mRootPreferenceScreen.getPreference(i);
for (int i = 0; i < this.mFilterPreferenceScreen.getPreferenceCount(); i++) {
Preference pref = this.mFilterPreferenceScreen.getPreference(i);
if ((pref instanceof CheckBoxPreference)) {
CheckBoxPreference pref_check = (CheckBoxPreference) pref;
if (selected.contains(pref_check.getSummary())) {
pref_check.setChecked(true);
CheckBoxPreference prefCheck = (CheckBoxPreference) pref;
if (selected.contains((prefCheck.getSummary()))) {
prefCheck.setChecked(true);
}
}
}
}

private void loadSelectedPackage() {
this.getArguments();
mode = MyApplication.getInstance().loadVPNMode();
Set<String> selected = MyApplication.getInstance().loadVPNApplication(mode);
setSelectedPackageSet(selected);
private Set<String> getAllSelectedPackageSet() {
Set<String> selected = this.getFilterSelectedPackageSet();
for (Map.Entry<String, Boolean> value : this.mAllPackageInfoMap.entrySet()) {
if (value.getValue()) selected.add(value.getKey());
}
return selected;
}

private void storeSelectedPackageSet(final Set<String> set) {
Expand Down Expand Up @@ -270,8 +296,7 @@ public boolean onQueryTextSubmit(String query) {
if (!query.trim().isEmpty()) {
filter(query);
return true;
}
else {
} else {
filter("");
return true;
}
Expand All @@ -284,6 +309,8 @@ public boolean onQueryTextChange(String newText) {

@Override
public boolean onClose() {
Set<String> selected = this.getAllSelectedPackageSet();
storeSelectedPackageSet(selected);
filter("");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ private Builder getBuilder() {
MyApplication app = (MyApplication) this.getApplication();
if (app.loadVPNMode() == MyApplication.VPNMode.DISALLOW) {
Set<String> disallow = app.loadVPNApplication(MyApplication.VPNMode.DISALLOW);
Log.d(TAG, "disallowed:" + disallow.size());
Log.d(TAG, "disallowed:" + disallow.size());
builder.addDisallowedApplication(Arrays.asList(disallow.toArray(new String[0])));
} else {
Set<String> allow = app.loadVPNApplication(MyApplication.VPNMode.ALLOW);
Log.d(TAG, "allowed:" + allow.size());
builder.addAllowedApplication(Arrays.asList(allow.toArray(new String[0])));
}
} catch (PackageManager.NameNotFoundException e) {
Log.e( TAG, e.getMessage(), e);
Log.e(TAG, e.getMessage(), e);
}
}

Expand Down
Loading

0 comments on commit 400b758

Please sign in to comment.