Android O版本 应用锁功能
公司在开发android O版本,分配了一个应用锁功能给我,现在在这里跟大家一起分享
需求:应用锁功能(主要功能是在打开某个app的锁的时候,弹出密码锁)
心路历程:看到这个功能*时间想的是在哪个位置拦截可以挡住所有的app,左思右想决定在Activity.java里面来写,因为所有的Activity肯定会继承这个类。想通了这一点其他的也就一步步迎刃而解。
主要逻辑:Activity onResume 的时候去get当前的包名,然后跟存在数据库里面加锁的包名遍历对比,如果有包含在数据库里,即弹出密码界面,如果没有,正常打开Activity。
主要代码:frameworks\base\core\java\android\app\Activity.java
//add by illa for applock
KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
Intent intentKeyguard = mKeyguardManager.createConfirmDeviceCredentialIntent(null, null);
String lastPackageName = Settings.System.getString(getContentResolver(), “lastName”);
android.util.Log.d(“illa”,”onResume!”+”\ngetPackageName() = “+getPackageName()+”\tlastPackageName = “+lastPackageName);
android.util.Log.d(“illa”,”onResume!”+”\ngetClassName = “+getComponentName().getClassName());
List<String> lockPackageName = new ArrayList<String>();
lockPackageName = getLockedPkgs();
/*android.util.Log.d(“illa”,”List<String> size = “+lockPackageName.size());
android.util.Log.i(“illa”, “lockPackageName.size() > 0 = ” + (lockPackageName.size() > 0));
android.util.Log.i(“illa”, “getComponentName().getClassName() = ” + (getComponentName().getClassName().equals(“com.android.settings.applock.SetLockApp”)));
android.util.Log.i(“illa”, “(intentKeyguard != null) = ” + (intentKeyguard != null));
android.util.Log.i(“illa”, “(lastPackageName == null) = ” + (lastPackageName == null));
android.util.Log.i(“illa”, “mKeyguardManager.inKeyguardRestrictedInputMode = ” + !mKeyguardManager.inKeyguardRestrictedInputMode());*/
if((lockPackageName.size() > 0) && (getComponentName().getClassName().equals(“com.android.settings.applock.SetLockApp”))
&& (intentKeyguard != null) &&(lastPackageName == null)&& !mKeyguardManager.inKeyguardRestrictedInputMode()){ //
intentKeyguard.putExtra(“packagename”, getPackageName());
intentKeyguard.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentKeyguard.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intentKeyguard, 11111);
android.util.Log.i(“illa”, “sendBroadcast(intent)”);
}
/*android.util.Log.i(“illa”, “lockPackageName.contains(getPackageName()) = ” + lockPackageName.contains(getPackageName()));
android.util.Log.i(“illa”, “intentKeyguard != null = ” + (intentKeyguard != null));
android.util.Log.i(“illa”, “lastPackageName == null = ” + (lastPackageName == null));
android.util.Log.i(“illa”, “equals InCallActivity = ” + !getComponentName().getClassName().equals(“com.android.incallui.InCallActivity”));
android.util.Log.i(“illa”, “!mKeyguardManager.inKeyguardRestrictedInputMode()) = ” + !mKeyguardManager.inKeyguardRestrictedInputMode());*/
if (lockPackageName.contains(getPackageName()) && intentKeyguard != null &&
lastPackageName == null && !getComponentName().getClassName().equals(“com.android.incallui.InCallActivity”)
&& !mKeyguardManager.inKeyguardRestrictedInputMode()) {
intentKeyguard.putExtra(“packagename”, getPackageName());
intentKeyguard.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentKeyguard.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intentKeyguard, 11111);
android.util.Log.i(“illa”, “sendBroadcast(intent)”);
}
String launcherPackagename = getLauncherPackageName(this);
if(launcherPackagename != null) {
if(getPackageName().equals(launcherPackagename)) {
//Settings.System.putString(getContentResolver(), “lastName”, null);
Intent intent = new Intent(“fprint.receiver.update.last_package_name”);
intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
intent.setPackage(“com.android.settings”);
sendBroadcastAsUser(intent, UserHandle.SYSTEM);
android.util.Log.i(“illa”, “last name = null”);
}
数据库那块的代码 就不贴了, 后续补充如何写数据库,
这里用到了一个内容提供者读取数据库代码如下
private Uri uri = Uri.parse(“content://com.android.settings.lockapp_provider/lockapp”);
private String COLUMN_PKGNAME = “pkgname”;
private String COLUMN_ISLOCKED = “islocked”;
private ArrayList<String> getLockedPkgs(){
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, null);
ArrayList<String>lockPkgs = new ArrayList<String>();
if(cursor != null){
// cursor.moveToFirst();
int pkg_index = cursor.getColumnIndex(COLUMN_PKGNAME);
int lock_index = cursor.getColumnIndex(COLUMN_ISLOCKED);
while (cursor.moveToNext()) {
String pkgName = cursor.getString(pkg_index);
String isLocked = cursor.getString(lock_index);
Log.e(“illa”, “pkgName->”+pkgName + ” || isLocked:” + isLocked);
if(“1”.equals(isLocked)){
lockPkgs.add(pkgName);
}
}
cursor.close();
}
return lockPkgs;
}
今天就到这里,希望以上代码可以对阅读者有所启发。我们来日方长!