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

Commit

Permalink
v0.3.1; separate notification channels; fix bug for back camera captu…
Browse files Browse the repository at this point in the history
…re request;remove write external storage permission
  • Loading branch information
Jerry Chen committed Jul 17, 2019
1 parent 159ce59 commit c7f45df
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 89 deletions.
3 changes: 1 addition & 2 deletions 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.3.0"
versionName "0.3.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resConfigs "xxhdpi"
}
Expand All @@ -28,7 +28,6 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-alpha-3'

Expand Down
28 changes: 16 additions & 12 deletions app/src/main/java/jerryc05/unlockme/activities/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@
import jerryc05.unlockme.helpers.camera.CameraBaseAPIClass;
import jerryc05.unlockme.services.ForegroundService;

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;

@SuppressWarnings("NullableProblems")
public final class MainActivity extends Activity
implements OnClickListener, OnCheckedChangeListener {

private final static String
TAG = MainActivity.class.getSimpleName();
public final static int
REQUEST_CODE_DEVICE_ADMIN = 0,
REQUEST_CODE_CAMERA_AND_WRITE_EXTERNAL = 1;
final static String
TAG = MainActivity.class.getSimpleName();
public final static int
REQUEST_CODE_DEVICE_ADMIN = 0,
REQUEST_CODE_CAMERA = 1;

public ReentrantLock requestDeviceAdminLock;
public static ThreadPoolExecutor threadPoolExecutor;
Expand Down Expand Up @@ -114,7 +116,7 @@ protected void onActivityResult(int requestCode, int resultCode,
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults) {
if (requestCode == REQUEST_CODE_CAMERA_AND_WRITE_EXTERNAL)
if (requestCode == REQUEST_CODE_CAMERA)
CameraBaseAPIClass.onRequestPermissionFinished(this, grantResults);
}

Expand All @@ -124,19 +126,21 @@ public void onClick(View view) {
@Override
public void run() {
final int id = view.getId();
final Intent mIntent = new Intent(MainActivity.this,
final Intent intent = new Intent(MainActivity.this,
ForegroundService.class);

if (id == R.id.activity_main_button_stopService)
stopService(mIntent);
stopService(intent);

else if (CameraBaseAPIClass.requestPermissions(
MainActivity.this)) {
CameraBaseAPIClass.isFront = id == R.id.activity_main_button_front;
intent.setAction(ACTION_CAPTURE_IMAGE);
intent.putExtra(EXTRA_CAMERA_FACING,
id == R.id.activity_main_button_front);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
startService(mIntent);
startService(intent);
else
startForegroundService(mIntent);
startForegroundService(intent);
}
}
});
Expand All @@ -161,7 +165,7 @@ public void rejectedExecution(Runnable runnable,
UserInterface.showExceptionToNotificationNoRethrow(
"ThreadPoolExecutor:\n>>> "
+ threadPoolExecutor.toString()
+ "\non MainActivity rejected:\n >>> "
+ "\non " + TAG + " rejected:\n >>> "
+ runnable.toString(),
"threadPoolExecutor#rejectedExecution()",
MainActivity.this);
Expand Down
86 changes: 44 additions & 42 deletions app/src/main/java/jerryc05/unlockme/helpers/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
*/
public final class UserInterface {

private static NotificationManager notificationManager;

@SuppressWarnings("unused")
public static void showExceptionToDialog(final Context context, final Exception e) {
if (context == null)
Expand Down Expand Up @@ -89,15 +87,14 @@ public static void showExceptionToNotificationNoRethrow(final String contentText
.setSmallIcon(R.drawable.ic_launcher_smartphone_lock_foreground)
.setSubText(subText)
.setStyle(new Notification.BigTextStyle()
.bigText(contentText));

//todo pendingintent

getNotificationManager(context).notify(-1, setNotificationChannel(builder,
getNotificationManager(context),
"Crash Report",
"Crash report notification channel for UnlockMe",
true).build());
.bigText(contentText))
.setContentIntent(getUpdateNotificationPendingIntent(context));

getNotificationManager(context).notify(title.hashCode(),
setNotificationChannel(builder, getNotificationManager(context),
"Crash Report",
"Crash report notification channel for UnlockMe",
true).build());
}

@SuppressWarnings("unused")
Expand All @@ -106,17 +103,6 @@ public static void notifyPictureToUI(final String contentText,
final Context context) {
final String title = "Picture Taken";

final Intent intent = new Intent(context, ForegroundService.class);
intent.setAction(ACTION_UPDATE_NOTIFICATION);

final PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
pendingIntent = PendingIntent.getService(context,
-1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
else
pendingIntent = PendingIntent.getForegroundService(context,
-1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

final Builder builder = new Builder(context)
.setContentTitle(title)
.setTicker(title)
Expand All @@ -125,13 +111,31 @@ public static void notifyPictureToUI(final String contentText,
.setStyle(new Notification.BigPictureStyle()
.bigPicture(BitmapFactory.decodeByteArray(
bytes, 0, bytes.length)))
.setContentIntent(pendingIntent);
.setContentIntent(getUpdateNotificationPendingIntent(context));

getNotificationManager(context).notify(-1, setNotificationChannel(builder,
getNotificationManager(context),
"Image Captured Report",
"Image captured report notification channel for UnlockMe",
false).build());
getNotificationManager(context).notify(title.hashCode(),
setNotificationChannel(builder, getNotificationManager(context),
"Image Captured Report",
"Image captured report notification channel for UnlockMe",
false).build());
}

private static PendingIntent getUpdateNotificationPendingIntent(
final Context context) {

final Intent intent = new Intent(context, ForegroundService.class);
intent.setAction(ACTION_UPDATE_NOTIFICATION);

final PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
pendingIntent = PendingIntent.getService(context,
ForegroundService.class.getSimpleName().hashCode(),
intent, PendingIntent.FLAG_UPDATE_CURRENT);
else
pendingIntent = PendingIntent.getForegroundService(context,
ForegroundService.class.getSimpleName().hashCode(),
intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}

public static void notifyToForegroundService(final Service service) {
Expand All @@ -142,11 +146,11 @@ public static void notifyToForegroundService(final Service service) {
.setSmallIcon(
R.drawable.ic_launcher_smartphone_lock_foreground);

service.startForeground(-1, UserInterface.setNotificationChannel(
builder,
getNotificationManager(service), title,
"Background service notification for UnlockMe.",
true).build());
service.startForeground(title.hashCode(),
UserInterface.setNotificationChannel(builder,
getNotificationManager(service), title,
"Background service notification for UnlockMe.",
true).build());
}

@SuppressWarnings("unused")
Expand All @@ -159,11 +163,11 @@ public static void notifyToUI(final String title,
.setContentText(contentText)
.setSmallIcon(R.drawable.ic_launcher_smartphone_lock_foreground);

getNotificationManager(context).notify(-1, setNotificationChannel(
builder, getNotificationManager(context),
"UnlockMe Notification Channel",
"Regular notification channel for UnlockMe",
true).build());
getNotificationManager(context).notify(title.hashCode(),
setNotificationChannel(builder, getNotificationManager(context),
"UnlockMe Notification Channel",
"Regular notification channel for UnlockMe",
true).build());
}

@SuppressWarnings("WeakerAccess")
Expand Down Expand Up @@ -202,9 +206,7 @@ static Builder setNotificationChannel(

@SuppressWarnings("WeakerAccess")
static NotificationManager getNotificationManager(Context context) {
if (notificationManager == null)
notificationManager = (NotificationManager)
context.getSystemService(NOTIFICATION_SERVICE);
return notificationManager;
return (NotificationManager)
context.getSystemService(NOTIFICATION_SERVICE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@
import jerryc05.unlockme.R;
import jerryc05.unlockme.helpers.UserInterface;

import static jerryc05.unlockme.activities.MainActivity.REQUEST_CODE_CAMERA_AND_WRITE_EXTERNAL;
import static jerryc05.unlockme.activities.MainActivity.REQUEST_CODE_CAMERA;

public abstract class CameraBaseAPIClass {

public static final String
SP_NAME_CAMERA = "CAMERA",
SP_KEY_PREFER_CAMERA_API_2 = "prefer_camera_api_2";
SP_KEY_PREFER_CAMERA_API_2 = "prefer_camera_api_2",
EXTRA_CAMERA_FACING = "EXTRA_CAMERA_FACING";

private static final boolean canUseCamera2 =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
public static boolean isFront = true;
private static boolean isFront = true;
@SuppressWarnings("CanBeFinal")
static int imageCount = 5;

Expand Down Expand Up @@ -104,17 +105,14 @@ public static boolean requestPermissions(final Context context) {

if (context instanceof Activity)
if (((Activity) context).shouldShowRequestPermissionRationale(
Manifest.permission.CAMERA) ||
((Activity) context).shouldShowRequestPermissionRationale(
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Manifest.permission.CAMERA)) {

((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
new AlertDialog.Builder(context)
.setTitle("Permission Required")
.setMessage("We need the following permissions to work properly:\n\n" +
"-\t\tCAMERA\n-\t\tWRITE_EXTERNAL_STORAGE")
.setMessage("We need CAMERA permissions to work properly!")
.setIcon(R.drawable.ic_round_warning_24px)
.setCancelable(false)
.setPositiveButton("OK",
Expand All @@ -125,7 +123,7 @@ public void run() {
});
} else
((Activity) context).requestPermissions(getPermissionsArray(),
REQUEST_CODE_CAMERA_AND_WRITE_EXTERNAL);
REQUEST_CODE_CAMERA);

return false;
}
Expand All @@ -139,7 +137,7 @@ static OnClickListener getRequestPermissionRationaleOnClickListener(
public void onClick(DialogInterface dialogInterface,
int i) {
activity.requestPermissions(getPermissionsArray(),
REQUEST_CODE_CAMERA_AND_WRITE_EXTERNAL);
REQUEST_CODE_CAMERA);
}
};
}
Expand All @@ -155,8 +153,8 @@ public static void onRequestPermissionFinished(final Activity activity,
grantResults[0] == PackageManager.PERMISSION_GRANTED;

final String granted_str = granted
? "Camera and Write External Storage Permissions Granted √"
: "Camera or Write External Storage Permissions Denied ×";
? "Camera Permissions Granted √"
: "Camera Permissions Denied ×";
Toast.makeText(activity, granted_str, Toast.LENGTH_SHORT).show();

if (granted)
Expand All @@ -170,13 +168,13 @@ static void saveImageToDisk(final byte[] data, final Context context) {
Locale.getDefault()).format(new Date()),
fileName = "UnlockMe_" + timeFormat + ".jpg";

// todo
// todo folder name in /picture

UserInterface.notifyPictureToUI(fileName, data, context);

final ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); // todo file name
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
values.put(MediaStore.Images.Media.IS_PENDING, 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import jerryc05.unlockme.helpers.UserInterface;
import jerryc05.unlockme.services.ForegroundService;

import static jerryc05.unlockme.services.ForegroundService.ACTION_CAPTURE_IMAGE;

@SuppressWarnings("NullableProblems")
public class MyDeviceAdminReceiver extends DeviceAdminReceiver {

Expand All @@ -19,23 +21,21 @@ public class MyDeviceAdminReceiver extends DeviceAdminReceiver {
private static int failedAttempt;

@Override
public void onPasswordFailed(Context context, Intent intent, UserHandle user) {
super.onPasswordFailed(context, intent);
public void onPasswordFailed(Context context, Intent failedIntent, UserHandle user) {
if (BuildConfig.DEBUG)
Log.d(TAG, "onPasswordFailed: ");

final Intent mIntent = new Intent(context,
final Intent serviceIntent = new Intent(context,
ForegroundService.class);
serviceIntent.setAction(ACTION_CAPTURE_IMAGE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
context.startService(mIntent);
context.startService(serviceIntent);
else
context.startForegroundService(mIntent);
context.startForegroundService(serviceIntent);
}

@Override
public void onPasswordSucceeded(Context context, Intent intent, UserHandle user) {
super.onPasswordSucceeded(context, intent, user);

if (failedAttempt > 0) {
UserInterface.notifyToUI("Unsuccessful Unlock Attempt Captured",
"UnlockMe captured " + failedAttempt +
Expand Down
Loading

0 comments on commit c7f45df

Please sign in to comment.