Skip to content

Commit 6764f5a

Browse files
author
isayan
committed
support Android 11
1 parent 06dca05 commit 6764f5a

File tree

5 files changed

+166
-11
lines changed

5 files changed

+166
-11
lines changed

android_app/app/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 29
5-
buildToolsVersion "29.0.3"
4+
compileSdkVersion 30
5+
buildToolsVersion "30.0.3"
66

7-
sourceCompatibility = '11' // -source
8-
targetCompatibility = '11' // -target
7+
sourceCompatibility = '1.8' // -source
8+
targetCompatibility = '1.8' // -target
99

1010
defaultConfig {
1111
applicationId "tun.proxy"
1212
minSdkVersion 21
13-
targetSdkVersion 29
13+
targetSdkVersion 30
1414
versionCode 100260
1515
versionName VERSION_NAME
1616
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package tun.proxy;
2+
3+
import android.content.Context;
4+
import android.content.pm.PackageInfo;
5+
import android.util.Log;
6+
7+
import androidx.test.ext.junit.runners.AndroidJUnit4;
8+
import androidx.test.platform.app.InstrumentationRegistry;
9+
10+
import org.junit.After;
11+
import org.junit.Assert;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
16+
import java.util.List;
17+
18+
import tun.utils.ProgressTask;
19+
import tun.utils.Util;
20+
21+
import static org.junit.Assert.assertEquals;
22+
23+
@RunWith(AndroidJUnit4.class)
24+
public class ProgressTaskTest {
25+
private static final String TAG = "ProgressTaskTest";
26+
27+
@Before
28+
public void setUp() {
29+
30+
}
31+
32+
@After
33+
public void tearDown() {
34+
35+
}
36+
37+
@Test
38+
public void progressTask() {
39+
Log.w(TAG, "progressTask: start");
40+
41+
ProgressTask task = new ProgressTask<String, String, List<PackageInfo>>() {
42+
43+
@Override
44+
protected List<PackageInfo> doInBackground(String... var1) {
45+
for (int i = 0; i < 100; i++) {
46+
try {
47+
Thread.sleep(10);
48+
Log.d(TAG, "Progress:" + i);
49+
} catch (InterruptedException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
return null;
54+
}
55+
56+
};
57+
58+
Assert.assertEquals(task.getStatus(), ProgressTask.Status.PENDING);
59+
Log.w(TAG, "progressTask: execute");
60+
task.execute();
61+
Assert.assertEquals(task.getStatus(), ProgressTask.Status.RUNNING);
62+
63+
Log.w(TAG, "progressTask: end");
64+
65+
}
66+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.os.Bundle;
2323
import android.os.Handler;
2424
import android.os.IBinder;
25+
import android.os.Looper;
2526
import android.text.TextUtils;
2627
import android.util.Log;
2728
import android.view.View;
@@ -42,7 +43,7 @@ public class MainActivity extends AppCompatActivity implements
4243
Button start;
4344
Button stop;
4445
EditText hostEditText;
45-
Handler statusHandler = new Handler();
46+
Handler statusHandler = new Handler(Looper.getMainLooper());
4647

4748
private Tun2HttpVpnService service;
4849

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import android.content.SharedPreferences;
88
import android.content.pm.PackageInfo;
99
import android.content.pm.PackageManager;
10-
import android.os.AsyncTask;
10+
//import android.os.AsyncTask;
1111
import android.os.Build;
1212
import android.os.Bundle;
1313
import androidx.appcompat.app.ActionBar;
@@ -29,8 +29,8 @@
2929
import java.util.List;
3030
import java.util.Map;
3131
import java.util.Set;
32-
import java.util.concurrent.ExecutorService;
33-
import java.util.concurrent.Executors;
32+
33+
import tun.utils.ProgressTask;
3434

3535
public class SettingsActivity extends AppCompatActivity {
3636
private static final String TAG = "SettingsActivity";
@@ -285,7 +285,7 @@ protected void filter(String filter, final MyApplication.AppSortBy filterBy, fin
285285

286286
this.removeAllPreferenceScreen();
287287

288-
if (task != null && task.getStatus() == AsyncTask.Status.PENDING) {
288+
if (task != null && task.getStatus() == ProgressTask.Status.PENDING) {
289289
task.execute();
290290
}
291291
else {
@@ -522,7 +522,7 @@ public boolean onClose() {
522522
* https://developer.android.com/reference/android/os/AsyncTask
523523
* Deprecated in API level R
524524
* */
525-
public static class AsyncTaskProgress extends AsyncTask<String, String, List<PackageInfo>> {
525+
public static class AsyncTaskProgress extends ProgressTask<String, String, List<PackageInfo>> {
526526

527527
final PackageListFragment packageFragment;
528528

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package tun.utils;
2+
3+
import android.os.Handler;
4+
import android.os.Looper;
5+
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
9+
public abstract class ProgressTask<Params, Progress, Result> {
10+
private volatile Status mStatus = Status.PENDING;
11+
private boolean canceled = false;
12+
13+
public final ProgressTask.Status getStatus() {
14+
return mStatus;
15+
}
16+
17+
private class ProgressRunnable implements Runnable {
18+
19+
final Params [] params;
20+
21+
@SafeVarargs
22+
public ProgressRunnable(Params... params) {
23+
this.params = params;
24+
}
25+
26+
private Result result;
27+
Handler handler = new Handler(Looper.getMainLooper());
28+
29+
@Override
30+
public void run() {
31+
if (mStatus != Status.PENDING) {
32+
switch (mStatus) {
33+
case RUNNING:
34+
throw new IllegalStateException("Cannot execute task:"
35+
+ " the task is already running.");
36+
case FINISHED:
37+
throw new IllegalStateException("Cannot execute task:"
38+
+ " the task has already been executed "
39+
+ "(a task can be executed only once)");
40+
}
41+
}
42+
mStatus = Status.RUNNING;
43+
try {
44+
onPreExecute();
45+
result = doInBackground(params);
46+
} catch (Exception ex) {
47+
ex.printStackTrace();
48+
}
49+
handler.post(new Runnable() {
50+
@Override
51+
public void run() {
52+
if (!canceled) {
53+
onPostExecute(result);
54+
mStatus = Status.FINISHED;
55+
} else {
56+
onCancelled();
57+
}
58+
}
59+
});
60+
}
61+
}
62+
63+
public void execute(Params... params) {
64+
ExecutorService executorService = Executors.newSingleThreadExecutor();
65+
executorService.submit(new ProgressRunnable(params));
66+
}
67+
68+
protected void onPreExecute() {
69+
}
70+
71+
protected abstract Result doInBackground(Params... params);
72+
73+
protected void onPostExecute(Result result) {
74+
}
75+
76+
public void cancel(boolean flag) {
77+
canceled = flag;
78+
}
79+
80+
public final boolean isCancelled() {
81+
return canceled;
82+
}
83+
84+
protected void onCancelled() {
85+
}
86+
87+
public enum Status { PENDING, RUNNING, FINISHED }
88+
}

0 commit comments

Comments
 (0)