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

Commit

Permalink
v0.3.0; migrate to target android Q; support scoped storage; change f…
Browse files Browse the repository at this point in the history
…ile path;
  • Loading branch information
Jerry Chen committed Jul 17, 2019
1 parent ac14654 commit 78b5a48
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
/app/release
/app/debug
*.lnk
/.idea/codeStyles
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.2.1"
versionName "0.3.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
resConfigs "xxhdpi"
}
Expand Down

This file was deleted.

71 changes: 71 additions & 0 deletions app/src/androidTest/java/jerryc05/unlockme/InstrumentedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package jerryc05.unlockme;

import android.content.Context;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public final class InstrumentedTest {

private static Context context = ApplicationProvider.getApplicationContext();

@SuppressWarnings("ConstantConditions")
@Test
public void testURI() {
Log.e("INTERNAL_CONTENT_URI", MediaStore.Images.Media.
INTERNAL_CONTENT_URI.getPath());
Log.e("EXTERNAL_CONTENT_URI", MediaStore.Images.Media.
EXTERNAL_CONTENT_URI.getPath());
Log.e("VOLUME_INTERNAL", MediaStore.Images.Media.
getContentUri(MediaStore.VOLUME_INTERNAL).getPath());
Log.e("VOLUME_EXTERNAL", MediaStore.Images.Media.
getContentUri(MediaStore.VOLUME_EXTERNAL).getPath());
Log.e("VOLUME_EXTERNAL_PRIMARY", MediaStore.Images.Media.
getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY).getPath());

/*
E/INTERNAL_CONTENT_URI: /internal/images/media
E/EXTERNAL_CONTENT_URI: /external/images/media
E/VOLUME_INTERNAL: /internal/images/media
E/VOLUME_EXTERNAL: /external/images/media
E/VOLUME_EXTERNAL_PRIMARY: /external_primary/images/media
*/
}

@SuppressWarnings("ConstantConditions")
@Test
public void testDir() {
for (File file : context.getExternalFilesDirs(Environment.DIRECTORY_PICTURES))
Log.e("getExternalFilesDirs", file.getAbsolutePath());
Log.e("getExternalFilesDir", context.getExternalFilesDir(
Environment.DIRECTORY_PICTURES).getAbsolutePath());
for (File file : context.getExternalMediaDirs())
Log.e("getExternalMediaDirs", file.getAbsolutePath());

/*
E/getExternalFilesDirs: /storage/emulated/0/Android/data/jerryc05.unlockme/files/Pictures
E/getExternalFilesDirs: /storage/0670-0B16/Android/data/jerryc05.unlockme/files/Pictures
E/getExternalFilesDir: /storage/emulated/0/Android/data/jerryc05.unlockme/files/Pictures
E/getExternalMediaDirs: /storage/emulated/0/Android/media/jerryc05.unlockme
E/getExternalMediaDirs: /storage/0670-0B16/Android/media/jerryc05.unlockme
*/
}
}
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<uses-feature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;

import org.json.JSONArray;
Expand All @@ -34,7 +36,7 @@

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

private final static String
TAG = MainActivity.class.getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ 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),
getNotificationManager(context).notify(-1, setNotificationChannel(
builder, getNotificationManager(context),
"UnlockMe Notification Channel",
"Regular notification channel for UnlockMe",
true).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.camera2.CameraCharacteristics;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

import jerryc05.unlockme.R;
import jerryc05.unlockme.helpers.UserInterface;
Expand Down Expand Up @@ -93,12 +98,10 @@ public static boolean requestPermissions(final Context context) {
"requestPermissions() External storage not writable!"));

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
(context.checkSelfPermission(Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED &&
context.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED))
context.checkSelfPermission(Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED)
return true;

if (context instanceof Activity)
if (((Activity) context).shouldShowRequestPermissionRationale(
Manifest.permission.CAMERA) ||
Expand Down Expand Up @@ -128,9 +131,9 @@ public void run() {
}

@SuppressWarnings("WeakerAccess")
static DialogInterface.OnClickListener getRequestPermissionRationaleOnClickListener(
static OnClickListener getRequestPermissionRationaleOnClickListener(
final Activity activity) {
return new DialogInterface.OnClickListener() {
return new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(DialogInterface dialogInterface,
Expand All @@ -143,25 +146,19 @@ public void onClick(DialogInterface dialogInterface,

@SuppressWarnings("WeakerAccess")
static String[] getPermissionsArray() {
return new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
return new String[]{Manifest.permission.CAMERA};
}

public static void onRequestPermissionFinished(final Activity activity,
final int[] grantResults) {
boolean granted = true;
if (grantResults.length > 0)
for (int result : grantResults)
if (result != PackageManager.PERMISSION_GRANTED) {
granted = false;
break;
}
final boolean granted = grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED;

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

if (granted)
getImageFromDefaultCamera(activity, isFront);
}
Expand All @@ -171,24 +168,30 @@ static void saveImageToDisk(final byte[] data, final Context context) {
final String
timeFormat = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS",
Locale.getDefault()).format(new Date()),
dirName = Environment.getExternalStorageDirectory() + "/UnlockMe/",
fileName = "UnlockMe_" + timeFormat + ".jpg";

UserInterface.notifyPictureToUI(fileName, data, context);
final File
dir = new File(dirName),
file = new File(dir, fileName);

if (!dir.isDirectory() && !dir.mkdirs())
UserInterface.showExceptionToNotification(
"Cannot create path " + fileName,
"saveImageToDisk()", context);
final ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
values.put(MediaStore.Images.Media.IS_PENDING, 1);

try (final FileOutputStream fileOutputStream = new FileOutputStream(file)) {
fileOutputStream.write(data);
} catch (final Exception e) {
UserInterface.showExceptionToNotification(
e.toString(), "saveImageToDisk()", context);
final ContentResolver resolver = context.getContentResolver();
final Uri external = MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
item = Objects.requireNonNull(resolver.insert(external, values));

try (final OutputStream outputStream = resolver.openOutputStream(item)) {
Objects.requireNonNull(outputStream).write(data);
} catch (Exception e) {
UserInterface.showExceptionToNotification(e.toString(),
"saveImageToDisk()", context);
}

values.clear();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
values.put(MediaStore.Images.Media.IS_PENDING, 0);
resolver.update(item, values, null, null);
}
}

0 comments on commit 78b5a48

Please sign in to comment.