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

Commit

Permalink
camera works on foreground
Browse files Browse the repository at this point in the history
Signed-off-by: Jerry Chen <jerryc05@qq.com>
  • Loading branch information
Jerry Chen committed Jul 9, 2019
1 parent 7d324c5 commit 3a7bc22
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .idea/dictionaries/Jerry.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "jerryc05.unlockme"
minSdkVersion 19
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("jerryc05.unlockme", appContext.getPackageName());
}
}
21 changes: 19 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
package="jerryc05.unlockme"
tools:ignore="GoogleAppIndexingWarning">

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
android:name="android.hardware.camera2"
android:required="true" />

<application
android:hardwareAccelerated="false"
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
Expand All @@ -19,6 +24,18 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<receiver
android:name=".receivers.MyDAReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_policy" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
</application>
</manifest>
150 changes: 145 additions & 5 deletions app/src/main/java/jerryc05/unlockme/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,154 @@
package jerryc05.unlockme;


import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import jerryc05.unlockme.receivers.MyDAReceiver;

@SuppressWarnings("NullableProblems")
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
private final static String TAG =
MainActivity.class.getName();
private DevicePolicyManager devicePolicyManager;
private ComponentName componentName;
private final static int REQUEST_CODE_CAMERA = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new Thread(new Runnable() {
@Override
public void run() {
handleDeviceAdmin();
findViewById(R.id.activity_main_button_takePhoto)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
takePhoto();
}
});
}
}).start();
}

@Override
protected void onResume() {
super.onResume();

if (isDeviceAdminDisabled())
requireDeviceAdmin();
}

@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults) {
if (requestCode == REQUEST_CODE_CAMERA) {
final String granted = grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED
? "Camera Permission Granted √"
: "Camera Permission Denied ×";
if (BuildConfig.DEBUG)
Log.d(TAG, "onRequestPermissionsResult: " + granted);
Toast.makeText(this,
granted, Toast.LENGTH_SHORT)
.show();
takePhoto();
}
}

void takePhoto() {
if (BuildConfig.DEBUG)
Log.d(TAG, "takePhoto: ");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
Manifest.permission.CAMERA))
new AlertDialog.Builder(this)
.setTitle("Permission Required")
.setMessage(getResources().getString(R.string.permission_camera_explanation))
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
requestPermissions(
new String[]{Manifest.permission.CAMERA},
REQUEST_CODE_CAMERA);
}
})
.show();
else
requestPermissions(
new String[]{Manifest.permission.CAMERA},
REQUEST_CODE_CAMERA);

} else {
final int REQUEST_IMAGE_CAPTURE = 1;

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}

void handleDeviceAdmin() {
devicePolicyManager = (DevicePolicyManager)
getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(
getApplicationContext(), MyDAReceiver.class);

if (isDeviceAdminDisabled())
requireDeviceAdmin();
}

private boolean isDeviceAdminDisabled() {
final boolean disabled = devicePolicyManager == null ||
!devicePolicyManager.isAdminActive(componentName);

runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,
disabled ? "Device Admin Disabled ×"
: "Device Admin Enabled √", Toast.LENGTH_SHORT)
.show();
}
});
return disabled;
}

private void requireDeviceAdmin() {
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getResources().getString(R.string.device_admin_explanation));

runOnUiThread(new Runnable() {
@Override
public void run() {
startActivity(intent);
}
});
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/jerryc05/unlockme/receivers/MyDAReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package jerryc05.unlockme.receivers;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
import android.util.Log;

import jerryc05.unlockme.BuildConfig;

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

private final static String TAG = MyDAReceiver.class.getName();
private static int failedAttempt;

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

if (BuildConfig.DEBUG)
Log.d(TAG, "onPasswordFailed: " + ++failedAttempt);
}

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

if (failedAttempt > 0) {

}
failedAttempt = 0;
}
}
9 changes: 3 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
<Button
android:id="@+id/activity_main_button_takePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text="Hello World!" />
android:text="Take Photo!" />
</RelativeLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">UnlockMe</string>
<string name="device_admin_explanation">This app needs Device Admin to work properly.</string>
<string name="permission_camera_explanation">This app needs Camera permission to work properly.</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/device_policy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<device-admin>
<uses-policies>
<watch-login />
</uses-policies>
</device-admin>
4 changes: 0 additions & 4 deletions app/src/test/java/jerryc05/unlockme/ExampleUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}

0 comments on commit 3a7bc22

Please sign in to comment.