Skip to content

Commit c9ccf6d

Browse files
author
isayan
committed
support for android 9.0
1 parent 1623972 commit c9ccf6d

19 files changed

+116
-110
lines changed

Readme-ja.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ TunProxyアプリを起動すると以下の画面が起動します。
1919
![Tun Proxy](images/TunProxy.png)
2020

2121
* Proxy address (host:port)
22-
* 接続先のプロキシサーバを ** IPアドレス:ポート番号 ** の形式で指定します。
22+
* 接続先のプロキシサーバを **IPアドレス:ポート番号** の形式で指定します。
2323
IPアドレスはIPv4形式で記載する必要があります。
2424

2525
* [Start] ボタン
@@ -35,7 +35,7 @@ TunProxyアプリを起動すると以下の画面が起動します。
3535

3636
VPNの接続設定を行います。
3737

38-
![Menu Settings](images/Menu-Settings.png)![Menu Settings](images/Menu-Settings-sub.png)
38+
![Menu Settings](images/Menu-Settings.png)![Menu Settings](images/Menu-Settings-app.png)
3939

4040
Disallow Application と Allow Application の2つのモードがありますが、同時に指定することはできません。
4141
このためどちらのモードで動作させたいかを選択する必要があります。

Readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ When you start the TunProxy application, the following screen will be launched.
1919
![Tun Proxy](images/TunProxy.png)
2020

2121
* Proxy address (host:port)
22-
* Specify the destination proxy server in the format ** IP address:port number **.
22+
* Specify the destination proxy server in the format **IP address:port number**.
2323
The IP address must be described in IPv4 format.
2424

2525
* [Start] button
@@ -35,7 +35,7 @@ Application settings can be made from the menu icon (![Menu](images/Menu.png)) a
3535

3636
Configure VPN service settings.
3737

38-
![Menu Settings](images/Menu-Settings.png) => ![Menu Settings](images/Menu-Settings-sub.png)
38+
![Menu Settings](images/Menu-Settings.png) => ![Menu Settings](images/Menu-Settings-app.png)
3939

4040
There are two modes, Disallow Application and Allow Application, but you can not specify them at the same time.
4141
Because of this you will have to choose whether you want to run in either mode.

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 100007
9+
versionCode 100008
1010
versionName VERSION_NAME
1111
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1212
externalNativeBuild {

android_app/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
<manifest
3+
xmlns:android="http://schemas.android.com/apk/res/android"
34
package="tun.proxy">
45

56
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ public void run() {
170170
protected void onPause() {
171171
super.onPause();
172172
statusHandler.removeCallbacks(statusRunnable);
173-
174173
unbindService(serviceConnection);
175174
}
176175

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

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@
1414
import java.util.Set;
1515

1616
public class MyApplication extends Application {
17-
private static Context context;
17+
private static MyApplication instance;
1818

19-
public static Context getContext() {
20-
return context;
19+
public static MyApplication getInstance() {
20+
return instance;
2121
}
2222

2323
@Override
2424
public void onCreate() {
2525
super.onCreate();
26-
context = getApplicationContext();
26+
instance = this;
2727
}
28+
2829
// public byte [] getTrustCA() {
2930
// try {
3031
// X509Certificate cert = CertificateUtil.getCACertificate("/sdcard/", "");
@@ -40,22 +41,37 @@ public void onCreate() {
4041
// return null;
4142
// }
4243

43-
public int getVPNMode() {
44+
public enum VPNMode {DISALLOW, ALLOW};
45+
46+
// public final String vpn_mode_key[] = {VPNMode.DISALLOW.name(), VPNMode.ALLOW.name()};
47+
48+
private final String pref_key[] = {"vpn_disallowed_application", "vpn_allowed_application"};
49+
50+
public VPNMode loadVPNMode() {
4451
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
45-
String vpn_connection_mode = sharedPreferences.getString("vpn_connection_mode", String.valueOf(SimplePreferenceFragment.PackageListPreferenceFragment.VPNMode.DISALLOW.ordinal()));
46-
return Integer.parseInt( vpn_connection_mode );
52+
String vpn_mode = sharedPreferences.getString("vpn_connection_mode", MyApplication.VPNMode.DISALLOW.name());
53+
return VPNMode.valueOf ( vpn_mode );
4754
}
4855

49-
public Set<String> getAllowedApplication() {
50-
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
51-
Set<String> allowed_preference = sharedPreferences.getStringSet("vpn_allowed_application", new HashSet<String>() );
52-
return allowed_preference;
56+
public void storeVPNMode(VPNMode mode) {
57+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
58+
final SharedPreferences.Editor editor = prefs.edit();
59+
editor.putString("vpn_connection_mode", mode.name());
60+
return;
5361
}
5462

55-
public Set<String> getDisallowedApplication() {
56-
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
57-
Set<String> disallowed_preference = sharedPreferences.getStringSet("vpn_disallowed_application", new HashSet<String>() );
58-
return disallowed_preference;
63+
public Set<String> loadVPNApplication(VPNMode mode) {
64+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
65+
Set<String> preference = prefs.getStringSet(pref_key[mode.ordinal()], new HashSet<String>() );
66+
return preference;
67+
}
68+
69+
public void storeVPNApplication(VPNMode mode, final Set<String> set) {
70+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
71+
final SharedPreferences.Editor editor = prefs.edit();
72+
editor.putStringSet(pref_key[mode.ordinal()], set);
73+
editor.commit();
74+
return ;
5975
}
6076

6177
}

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

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,54 @@
88
import android.content.pm.PackageManager;
99
import android.os.Build;
1010
import android.os.Bundle;
11-
import android.preference.CheckBoxPreference;
12-
import android.preference.ListPreference;
13-
import android.preference.Preference;
14-
import android.preference.PreferenceFragment;
15-
import android.preference.PreferenceManager;
16-
import android.preference.PreferenceScreen;
11+
import android.preference.*;
1712
import android.view.MenuItem;
1813

19-
import java.util.ArrayList;
20-
import java.util.EnumSet;
2114
import java.util.HashSet;
2215
import java.util.List;
23-
import java.util.Map;
2416
import java.util.Set;
2517

26-
import tun.utils.CertificateUtil;
27-
2818
public class SimplePreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
2919

20+
public static final String VPN_CONNECTION_MODE = "vpn_connection_mode";
21+
public static final String VPN_DISALLOWED_APPLICATION_LIST = "vpn_disallowed_application_list";
22+
public static final String VPN_ALLOWED_APPLICATION_LIST = "vpn_allowed_application_list";
23+
3024
@Override
3125
public void onCreate(Bundle savedInstanceState) {
3226
super.onCreate(savedInstanceState);
3327
addPreferencesFromResource(R.xml.preferences);
3428
setHasOptionsMenu(true);
3529

3630
/* Allowed / Disallowed Application */
37-
final ListPreference pkg_selection = (ListPreference) this.findPreference("vpn_connection_mode");
31+
final ListPreference pkg_selection = (ListPreference) this.findPreference(VPN_CONNECTION_MODE);
3832
pkg_selection.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
3933
@Override
4034
public boolean onPreferenceChange(Preference preference, Object value) {
41-
if (preference instanceof ListPreference) {
42-
ListPreference listPreference = (ListPreference) preference;
43-
int index = listPreference.findIndexOfValue((String) value);
44-
45-
PreferenceScreen disallow = (PreferenceScreen) findPreference("disallowed_application_list");
46-
PreferenceScreen allow = (PreferenceScreen) findPreference("allowed_application_list");
47-
disallow.setEnabled(index == 0);
48-
allow.setEnabled(index != 0);
35+
if (preference instanceof ListPreference) {
36+
ListPreference listPreference = (ListPreference) preference;
37+
int index = listPreference.findIndexOfValue((String) value);
4938

39+
PreferenceScreen disallow = (PreferenceScreen) findPreference(VPN_DISALLOWED_APPLICATION_LIST);
40+
PreferenceScreen allow = (PreferenceScreen) findPreference(VPN_ALLOWED_APPLICATION_LIST);
41+
disallow.setEnabled(index == 0);
42+
allow.setEnabled(index != 0);
5043

51-
// Set the summary to reflect the new value.
52-
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
44+
// Set the summary to reflect the new value.
45+
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
5346

54-
}
55-
return true;
47+
}
48+
return true;
5649
}
5750
});
5851
pkg_selection.setSummary(pkg_selection.getEntry());
59-
PreferenceScreen disallow = (PreferenceScreen) findPreference("disallowed_application_list");
60-
PreferenceScreen allow = (PreferenceScreen) findPreference("allowed_application_list");
61-
disallow.setEnabled(Integer.parseInt(pkg_selection.getValue()) == 0);
62-
allow.setEnabled(Integer.parseInt(pkg_selection.getValue()) != 0);
52+
PreferenceScreen disallow = (PreferenceScreen) findPreference(VPN_DISALLOWED_APPLICATION_LIST);
53+
PreferenceScreen allow = (PreferenceScreen) findPreference(VPN_ALLOWED_APPLICATION_LIST);
54+
disallow.setEnabled(MyApplication.VPNMode.DISALLOW.name().equals(pkg_selection.getValue()));
55+
allow.setEnabled(MyApplication.VPNMode.ALLOW.name().equals(pkg_selection.getValue()));
6356

64-
findPreference("allowed_application_list").setOnPreferenceClickListener(this);
65-
findPreference("disallowed_application_list").setOnPreferenceClickListener(this);
57+
findPreference(VPN_DISALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this);
58+
findPreference(VPN_ALLOWED_APPLICATION_LIST).setOnPreferenceClickListener(this);
6659

6760
}
6861

@@ -81,11 +74,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
8174
public boolean onPreferenceClick(Preference preference) {
8275
// keyを見てクリックされたPreferenceを特定
8376
switch (preference.getKey()) {
84-
case "allowed_application_list":
85-
transitionFragment(PackageListPreferenceFragment.newInstance(PackageListPreferenceFragment.VPNMode.ALLOW));
77+
case VPN_DISALLOWED_APPLICATION_LIST:
78+
transitionFragment(PackageListPreferenceFragment.newInstance(MyApplication.VPNMode.DISALLOW));
8679
break;
87-
case "disallowed_application_list":
88-
transitionFragment(PackageListPreferenceFragment.newInstance(PackageListPreferenceFragment.VPNMode.DISALLOW));
80+
case VPN_ALLOWED_APPLICATION_LIST:
81+
transitionFragment(PackageListPreferenceFragment.newInstance(MyApplication.VPNMode.ALLOW));
8982
break;
9083
}
9184
return false;
@@ -102,13 +95,11 @@ private void transitionFragment(PreferenceFragment nextPreferenceFragment) {
10295

10396
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
10497
public static class PackageListPreferenceFragment extends PreferenceFragment {
105-
enum VPNMode {DISALLOW, ALLOW};
106-
private VPNMode mode;
98+
private MyApplication.VPNMode mode;
10799
private PreferenceScreen mRootPreferenceScreen;
108100

109-
private String pref_key[] = {"vpn_disallowed_application", "vpn_allowed_application"};
110101

111-
public static PackageListPreferenceFragment newInstance(VPNMode mode) {
102+
public static PackageListPreferenceFragment newInstance(MyApplication.VPNMode mode) {
112103
PackageListPreferenceFragment fragment = new PackageListPreferenceFragment();
113104
fragment.mode = mode;
114105
return fragment;
@@ -122,6 +113,12 @@ public void onCreate(Bundle savedInstanceState) {
122113
setPreferenceScreen(mRootPreferenceScreen);
123114
}
124115

116+
@Override
117+
public void onPause() {
118+
super.onPause();
119+
storeSelectedPackageSet(this.getSelectedPackageSet());
120+
}
121+
125122
@Override
126123
public void onResume() {
127124
super.onResume();
@@ -135,7 +132,7 @@ private void removeAllPreferenceScreen() {
135132
}
136133

137134
private void buildPackagesPreferences() {
138-
Context context = MyApplication.getContext().getApplicationContext();
135+
Context context = MyApplication.getInstance().getApplicationContext();
139136
PackageManager pm = context.getPackageManager();
140137
List<PackageInfo> installedPackages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
141138
for (final PackageInfo pi : installedPackages) {
@@ -157,7 +154,8 @@ private Set<String> getSelectedPackageSet() {
157154
Preference pref = this.mRootPreferenceScreen.getPreference(i);
158155
if ((pref instanceof CheckBoxPreference)) {
159156
CheckBoxPreference pref_check = (CheckBoxPreference) pref;
160-
if (pref_check.isChecked()) selected.add(pref_check.getSummary().toString());
157+
if (pref_check.isChecked())
158+
selected.add(pref_check.getSummary().toString());
161159
}
162160
}
163161
return selected;
@@ -175,24 +173,21 @@ private void setSelectedPackageSet(Set<String> selected) {
175173
}
176174

177175
private void loadSelectedPackage() {
178-
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( getActivity());
179176
this.getArguments();
180-
Set<String> selected = prefs.getStringSet( pref_key[mode.ordinal()], new HashSet<String>());
177+
mode = MyApplication.getInstance().loadVPNMode();
178+
Set<String> selected = MyApplication.getInstance().loadVPNApplication(mode);
181179
setSelectedPackageSet(selected);
182180
}
183181

184182
private void storeSelectedPackageSet(final Set<String> set) {
185-
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
186-
final SharedPreferences.Editor editor = prefs.edit();
187-
editor.putStringSet(pref_key[mode.ordinal()], set);
188-
editor.commit();
183+
MyApplication.getInstance().storeVPNMode(mode);
184+
MyApplication.getInstance().storeVPNApplication(mode, set);
189185
}
190186

191187
@Override
192188
public boolean onOptionsItemSelected(MenuItem item) {
193189
int id = item.getItemId();
194190
if (id == android.R.id.home) {
195-
storeSelectedPackageSet(this.getSelectedPackageSet());
196191
startActivity(new Intent(getActivity(), SimplePreferenceActivity.class));
197192
return true;
198193
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,17 @@ private Builder getBuilder() {
150150
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
151151
try {
152152
MyApplication app = (MyApplication) this.getApplication();
153-
if (app.getVPNMode() == 0) {
154-
Set<String> disallow = app.getDisallowedApplication();
153+
if (app.loadVPNMode() == MyApplication.VPNMode.DISALLOW) {
154+
Set<String> disallow = app.loadVPNApplication(MyApplication.VPNMode.DISALLOW);
155155
Log.d(TAG, "disallowed:" + disallow.size());
156156
builder.addDisallowedApplication(Arrays.asList(disallow.toArray(new String[0])));
157157
} else {
158-
Set<String> allow = app.getAllowedApplication();
158+
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-
e.printStackTrace();
163+
Log.e( TAG, e.getMessage(), e);
164164
}
165165
}
166166

0 commit comments

Comments
 (0)