Android O version app lock function

hi !what s up!

The company is developing the android O version and assigned an app lock function to me, and now I will share it with you here


Requirements: App lock function (the main function is to pop up the password lock when the lock of an app is opened)

Mental journey: When I saw this function, the first thing I thought about was where to block all apps. I thought about it and decided to write it in Activity.java, because all Activities will definitely inherit this class. Once you figure this out, the rest will be solved step by step.

Main logic: When Activity is onResume, get the current package name, and then traverse and compare it with the locked package name in the database. If it is included in the database, the password interface will pop up. If not, open the Activity normally.


Main code: 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");
			}


The code of the database will not be posted, and how to write the database will be added later.


A content provider is used here to read the database code as follows

	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;
	}


That's all for today, I hope the above code can inspire readers. We come to Japan for a long time!


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325795058&siteId=291194637