This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jerry Chen <jerryc05@qq.com>
- Loading branch information
Jerry Chen
committed
Jul 9, 2019
1 parent
7d324c5
commit 3a7bc22
Showing
11 changed files
with
260 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -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
34
app/src/main/java/jerryc05/unlockme/receivers/MyDAReceiver.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,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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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> |
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,5 @@ | ||
<device-admin> | ||
<uses-policies> | ||
<watch-login /> | ||
</uses-policies> | ||
</device-admin> |
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