forked from forbe/tun2http
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
isayan
committed
Jul 10, 2021
1 parent
06dca05
commit 6764f5a
Showing
5 changed files
with
166 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
android_app/app/src/androidTest/java/tun/proxy/ProgressTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package tun.proxy; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageInfo; | ||
import android.util.Log; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import androidx.test.platform.app.InstrumentationRegistry; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.List; | ||
|
||
import tun.utils.ProgressTask; | ||
import tun.utils.Util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class ProgressTaskTest { | ||
private static final String TAG = "ProgressTaskTest"; | ||
|
||
@Before | ||
public void setUp() { | ||
|
||
} | ||
|
||
@After | ||
public void tearDown() { | ||
|
||
} | ||
|
||
@Test | ||
public void progressTask() { | ||
Log.w(TAG, "progressTask: start"); | ||
|
||
ProgressTask task = new ProgressTask<String, String, List<PackageInfo>>() { | ||
|
||
@Override | ||
protected List<PackageInfo> doInBackground(String... var1) { | ||
for (int i = 0; i < 100; i++) { | ||
try { | ||
Thread.sleep(10); | ||
Log.d(TAG, "Progress:" + i); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
}; | ||
|
||
Assert.assertEquals(task.getStatus(), ProgressTask.Status.PENDING); | ||
Log.w(TAG, "progressTask: execute"); | ||
task.execute(); | ||
Assert.assertEquals(task.getStatus(), ProgressTask.Status.RUNNING); | ||
|
||
Log.w(TAG, "progressTask: end"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package tun.utils; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
public abstract class ProgressTask<Params, Progress, Result> { | ||
private volatile Status mStatus = Status.PENDING; | ||
private boolean canceled = false; | ||
|
||
public final ProgressTask.Status getStatus() { | ||
return mStatus; | ||
} | ||
|
||
private class ProgressRunnable implements Runnable { | ||
|
||
final Params [] params; | ||
|
||
@SafeVarargs | ||
public ProgressRunnable(Params... params) { | ||
this.params = params; | ||
} | ||
|
||
private Result result; | ||
Handler handler = new Handler(Looper.getMainLooper()); | ||
|
||
@Override | ||
public void run() { | ||
if (mStatus != Status.PENDING) { | ||
switch (mStatus) { | ||
case RUNNING: | ||
throw new IllegalStateException("Cannot execute task:" | ||
+ " the task is already running."); | ||
case FINISHED: | ||
throw new IllegalStateException("Cannot execute task:" | ||
+ " the task has already been executed " | ||
+ "(a task can be executed only once)"); | ||
} | ||
} | ||
mStatus = Status.RUNNING; | ||
try { | ||
onPreExecute(); | ||
result = doInBackground(params); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
handler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
if (!canceled) { | ||
onPostExecute(result); | ||
mStatus = Status.FINISHED; | ||
} else { | ||
onCancelled(); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
public void execute(Params... params) { | ||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
executorService.submit(new ProgressRunnable(params)); | ||
} | ||
|
||
protected void onPreExecute() { | ||
} | ||
|
||
protected abstract Result doInBackground(Params... params); | ||
|
||
protected void onPostExecute(Result result) { | ||
} | ||
|
||
public void cancel(boolean flag) { | ||
canceled = flag; | ||
} | ||
|
||
public final boolean isCancelled() { | ||
return canceled; | ||
} | ||
|
||
protected void onCancelled() { | ||
} | ||
|
||
public enum Status { PENDING, RUNNING, FINISHED } | ||
} |