Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Commit

Permalink
v0.4.0; add base activity class for convenience;
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerry Chen committed Aug 3, 2019
1 parent c291e3a commit 080a8c3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "0.4.0-alpha"
versionName "0.4.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resConfigs "xxhdpi"
}
Expand Down
36 changes: 4 additions & 32 deletions app/src/main/java/jerryc05/unlockme/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.UnknownHostException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import jerryc05.unlockme.BuildConfig;
Expand All @@ -39,6 +35,8 @@
import jerryc05.unlockme.helpers.camera.CameraBaseAPIClass;
import jerryc05.unlockme.services.ForegroundService;

import static jerryc05.unlockme.activities.MyActivity.getThreadPoolExecutor;
import static jerryc05.unlockme.activities.MyActivity.threadPoolExecutor;
import static jerryc05.unlockme.helpers.camera.CameraBaseAPIClass.EXTRA_CAMERA_FACING;
import static jerryc05.unlockme.helpers.camera.CameraBaseAPIClass.SP_KEY_PREFER_CAMERA_API_2;
import static jerryc05.unlockme.services.ForegroundService.ACTION_CAPTURE_IMAGE;
Expand All @@ -52,8 +50,7 @@ public final class MainActivity extends Activity
REQUEST_CODE_DEVICE_ADMIN = 0,
REQUEST_CODE_CAMERA_AND_WRITE_EXTERNAL = 1;

public ReentrantLock requestDeviceAdminLock; // todo
private ThreadPoolExecutor threadPoolExecutor;
public ReentrantLock requestDeviceAdminLock; // todo

@IntDef({REQUEST_CODE_DEVICE_ADMIN, REQUEST_CODE_CAMERA_AND_WRITE_EXTERNAL})
@Retention(RetentionPolicy.SOURCE)
Expand All @@ -65,7 +62,7 @@ protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

getThreadPoolExecutor().execute(
getThreadPoolExecutor(getApplicationContext()).execute(
new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -174,31 +171,6 @@ public void onCheckedChanged(@NonNull CompoundButton buttonView,
.apply();
}

private ThreadPoolExecutor getThreadPoolExecutor() {
if (threadPoolExecutor == null) {
RejectedExecutionHandler rejectedExecutionHandler
= new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable runnable,
ThreadPoolExecutor threadPoolExecutor) {
UserInterface.showExceptionToNotificationNoRethrow(
"ThreadPoolExecutor:\n>>> "
+ threadPoolExecutor.toString()
+ "\non " + TAG + " rejected:\n >>> "
+ runnable.toString(),
"threadPoolExecutor#rejectedExecution()",
getApplicationContext());
}
};
final int processorCount = Runtime.getRuntime().availableProcessors();
threadPoolExecutor = new ThreadPoolExecutor(processorCount,
2 * processorCount, 5, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1), rejectedExecutionHandler);
threadPoolExecutor.allowCoreThreadTimeOut(true);
}
return threadPoolExecutor;
}

@WorkerThread
void checkUpdate() {
URLConnectionBuilder connectionBuilder = null;
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/jerryc05/unlockme/activities/MyActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package jerryc05.unlockme.activities;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import jerryc05.unlockme.helpers.UserInterface;

abstract class MyActivity extends Activity {

private static final String TAG = "MyActivity";
static ThreadPoolExecutor threadPoolExecutor;

static ThreadPoolExecutor getThreadPoolExecutor(
@NonNull final Context context) {
if (threadPoolExecutor == null) {
RejectedExecutionHandler rejectedExecutionHandler
= new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable runnable,
ThreadPoolExecutor threadPoolExecutor) {
UserInterface.showExceptionToNotificationNoRethrow(
"ThreadPoolExecutor:\n>>> "
+ threadPoolExecutor.toString()
+ "\non " + TAG + " rejected:\n >>> "
+ runnable.toString(),
"threadPoolExecutor#rejectedExecution()",
context);
}
};
final int processorCount = Runtime.getRuntime().availableProcessors();
threadPoolExecutor = new ThreadPoolExecutor(processorCount,
2 * processorCount, 5, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1), rejectedExecutionHandler);
threadPoolExecutor.allowCoreThreadTimeOut(true);
}
return threadPoolExecutor;
}
}

0 comments on commit 080a8c3

Please sign in to comment.