android10.0(Q) AOSP 增加应用锁功能

前言

应用锁的功能可以说是很普遍了,大致就是在 startActivity 对应代码处进行拦截就行。

最开始在网上找了点资料,没有能合适直接用的,就自己搞了下,这里简单做个笔记。

Android应用锁实现

那就给大伙先来个效果图先康康

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

思路分析

由于我们的目标应用是系统 Settings ,这家伙的入口不唯一,一开始是想着在 Launcher3 中进行拦截就行,

最终效果不太完美,后来改到 ActivityStarter 中 startActivity()

是怎么找到这个地方的呢?因为之前改过一个 Q 版本以上不能后台拉起 Activity 的问题,当时报错的地方

就在这里,后来想了下报错不就是被拦截了么,这样我们也可以在此处定制密码进行拦截。

1、找到拦截启动位置,startActivity()

2、读取要拉起 Activity 对应包名和需要加锁 APP 包名判断

3、在加锁清单中,弹出验证界面,验证成功,继续执行 startActivity

4、无需加锁,直接放行

上代码

1、Launcher3 中可拦截的地方

packages\apps\Launcher3\src\com\android\launcher3\Launcher.java

java
 代码解读
复制代码
public boolean startActivitySafely(View v, Intent intent, ItemInfo item,
            @Nullable String sourceContainer) {
        if (TestProtocol.sDebugTracing) {
            android.util.Log.d(TestProtocol.NO_START_TAG,
                    "startActivitySafely outer");
        }

         //cczheng add testcode
        final String packageName = intent.getComponent().getPackageName();
        android.util.Log.i("ActivityStarter111","packageName="+packageName);
        if ("com.android.settings".equals(packageName)) {
          //android.widget.Toast.makeText(this, "foo", 1000).show();
          //return true;
        }//end

        if (!hasBeenResumed()) {
            // Workaround an issue where the WM launch animation is clobbered when finishing the
            // recents animation into launcher. Defer launching the activity until Launcher is
            // next resumed.
            addOnResumeCallback(() -> startActivitySafely(v, intent, item, sourceContainer));
            UiFactory.clearSwipeSharedState(true /* finishAnimation */);
            return true;
        }

        boolean success = super.startActivitySafely(v, intent, item, sourceContainer);
        if (success && v instanceof BubbleTextView) {
            // This is set to the view that launched the activity that navigated the user away
            // from launcher. Since there is no callback for when the activity has finished
            // launching, enable the press state and keep this reference to reset the press
            // state when we return to launcher.
            BubbleTextView btv = (BubbleTextView) v;
            btv.setStayPressed(true);
            addOnResumeCallback(btv);
        }
        return success;
    }

在此处判断包名后可直接 new 对话框进行交互,优点方便简单,缺点不能完全加锁。

2、ActivityStarter 中可拦截的地方

我这里偷懒了,界面没啥美化可言,你们自己按需调整。这里面引入 R 资源估计会有问题,所以这里直接

java 代码写布局了,通过 window 方式展示。

要拦截的包名清单可以通过 ContentProvider 查询,毕竟有 Context,自己按需增加。

frameworks\base\services\core\java\com\android\server\wm\ActivityStarter.java

java
 代码解读
复制代码

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


    //cczheng add for appLockPass S
    boolean isLocked;
    boolean isPassCheck;
    private void showAppLockPasswordWindow(Context mContext, IApplicationThread caller, Intent intent, Intent ephemeralIntent,
            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
            SafeActivityOptions options,
            boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
            TaskRecord inTask, boolean allowPendingRemoteAnimationRegistryLookup,
            PendingIntentRecord originatingPendingIntent, boolean allowBackgroundActivityStart) {

        final WindowManager.LayoutParams params =  new WindowManager.LayoutParams();
        params.width = 380;
        params.height = 230;
        params.format = Color.parseColor("#ADADAD");
        params.type = WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
        params.setTitle("AppLock");
        WindowManager mWM = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);

        final LinearLayout parentLayout = new LinearLayout(mContext);
        parentLayout.setOrientation(LinearLayout.VERTICAL);
        parentLayout.setBackgroundColor(Color.WHITE);
        LinearLayout.LayoutParams layoutParams
                = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
        parentLayout.setLayoutParams(layoutParams);

        TextView titleText = new TextView(mContext);
        LinearLayout.LayoutParams contentParams
                = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        titleText.setLayoutParams(contentParams);
        titleText.setText("Please enter app password");
        titleText.setTextColor(Color.BLACK);
        titleText.setTypeface(Typeface.create(titleText.getTypeface(), Typeface.NORMAL), Typeface.BOLD);
        titleText.setPadding(10, 10, 0, 0);
        parentLayout.addView(titleText);

        EditText passText = new EditText(mContext);
        passText.setLayoutParams(contentParams);
        passText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        passText.setTextColor(Color.BLACK);
        parentLayout.addView(passText);

        Button okBtn = new Button(mContext);
        LinearLayout.LayoutParams btnParams
                = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        btnParams.gravity = Gravity.RIGHT;
        okBtn.setLayoutParams(btnParams);
        okBtn.setBackgroundColor(Color.TRANSPARENT);
        okBtn.setText("Confirm");
        okBtn.setTextColor(Color.parseColor("#3996E8"));
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = passText.getText().toString();
                //android.widget.Toast.makeText(mContext, password, 1000).show();
                if (parentLayout!=null){
                    mWM.removeViewImmediate(parentLayout);
                    //parentLayout = null;
                }
       
                if ("123".equals(password)) {
                    isPassCheck = true;
                    startActivity(caller, intent, ephemeralIntent,
                             resolvedType, aInfo, rInfo,
                             voiceSession,  voiceInteractor,
                             resultTo,  resultWho,  requestCode,  callingPid,  callingUid,
                             callingPackage,  realCallingPid,  realCallingUid,  startFlags,
                             options,
                             ignoreTargetSecurity,  componentSpecified,  outActivity,
                             inTask,  allowPendingRemoteAnimationRegistryLookup,
                             originatingPendingIntent,  allowBackgroundActivityStart);
                }else {
                    isPassCheck = false;
                }
            }
        });
        parentLayout.addView(okBtn);

        try {
            mWM.addView(parentLayout, params);
        } catch (WindowManager.BadTokenException e) {
            e.printStackTrace();
        }
    }

    IApplicationThread mscaller; 
    Intent msintent, msephemeralIntent;
    String msresolvedType, msresultWho, mscallingPackage;
    ActivityInfo msaInfo;
    ResolveInfo msrInfo;
    int msrequestCode, mscallingPid, mscallingUid, msrealCallingPid, msrealCallingUid, msstartFlags;
    boolean msignoreTargetSecurity, mscomponentSpecified, msallowPendingRemoteAnimationRegistryLookup, msallowBackgroundActivityStart;
    IVoiceInteractionSession msvoiceSession;
    IVoiceInteractor msvoiceInteractor;
    IBinder msresultTo;
    SafeActivityOptions msoptions;
    ActivityRecord[] msoutActivity;
    TaskRecord msinTask;
    PendingIntentRecord msoriginatingPendingIntent;
    //E

    private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
            SafeActivityOptions options,
            boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
            TaskRecord inTask, boolean allowPendingRemoteAnimationRegistryLookup,
            PendingIntentRecord originatingPendingIntent, boolean allowBackgroundActivityStart) {
        mSupervisor.getActivityMetricsLogger().notifyActivityLaunching(intent);
        int err = ActivityManager.START_SUCCESS;
        // Pull the optional Ephemeral Installer-only bundle out of the options early.
        final Bundle verificationBundle
                = options != null ? options.popAppVerificationBundle() : null;

	    ................................
		
		checkedOptions = mInterceptor.mActivityOptions;
        }
        
        //cczheng add for appLockPass S
        android.util.Log.e("ActivityStarter","callingPackage="+callingPackage);
        final String packageName = intent.getComponent().getPackageName();
        android.util.Log.i("ActivityStarter","packageName="+packageName+" abort="+abort);
  
		if (("com.android.launcher3".equals(callingPackage) || "com.android.systemui".equals(callingPackage)) 
				&& "com.android.settings".equals(packageName)) {
			isLocked = true;
		}else{
			isLocked = false;
		}

		if (isPassCheck) {
			android.util.Log.i("ActivityStarter","isPassCheck pass goto activity");
			isLocked = false;
			isPassCheck = false;
		}

      mscaller = caller;
      msintent = intent;
      msephemeralIntent = ephemeralIntent;
      msresolvedType = resolvedType;
      msaInfo = aInfo;
      msvoiceSession = voiceSession;
      msvoiceInteractor = voiceInteractor;
      msresultTo = resultTo;
      msresultWho = resultWho;
      msrequestCode = requestCode;
      mscallingPid = callingPid;
      mscallingUid = callingUid;
      mscallingPackage = callingPackage;
      msrealCallingPid = realCallingPid;
      msrealCallingUid = realCallingUid;
      msstartFlags = startFlags;
      msoptions = options;
      msignoreTargetSecurity = ignoreTargetSecurity;
      mscomponentSpecified = componentSpecified;
      msoutActivity = outActivity;
      msinTask = inTask;
      msallowPendingRemoteAnimationRegistryLookup = allowPendingRemoteAnimationRegistryLookup;
      msoriginatingPendingIntent = originatingPendingIntent;
      msallowBackgroundActivityStart = allowBackgroundActivityStart;

      new android.os.Handler(android.os.Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
                if (isLocked) {
                    showAppLockPasswordWindow(mService.mContext, mscaller, msintent, msephemeralIntent,
                             msresolvedType, msaInfo, msrInfo,
                             msvoiceSession,  msvoiceInteractor,
                             msresultTo,  msresultWho,  msrequestCode,  mscallingPid,  mscallingUid,
                             mscallingPackage,  msrealCallingPid,  msrealCallingUid,  msstartFlags,
                             msoptions,
                             msignoreTargetSecurity,  mscomponentSpecified,  msoutActivity,
                             msinTask,  msallowPendingRemoteAnimationRegistryLookup,
                             msoriginatingPendingIntent,  msallowBackgroundActivityStart);
                }
            }
        });

       if (isLocked) {
           android.util.Log.d("ActivityStarter","START_SWITCHES_CANCELED=");
          return ActivityManager.START_SWITCHES_CANCELED;
       }///add EE

        if (abort) {
            if (resultRecord != null) {
                resultStack.sendActivityResultLocked(-1, resultRecord, resultWho, requestCode,
                        RESULT_CANCELED, null);
            }
            // We pretend to the caller that it was really started, but
            // they will just get a cancel result.
            ActivityOptions.abort(checkedOptions);
            return START_ABORTED;
        }
		
		
		........................

以上几个坑点说一下

callingPackage 是调用 startActivity 方法的初始者,可以看到我这里加了过滤,必须是 SystemUI 和 Launcher3

调用的情况下才进行拦截,系统启动时我们看到的安卓正在启动中就是 Settings 中的 FallbackHome,此时 callingPackage 为 null

这样会卡住进不去系统,所以需要过滤。

showAppLockPasswordWindow() 需要在 Handler 执行,ActivityStarter 中不能直接进行 UI 线程操作,不然会报错。

符合拦截条件时需要先将此次 startActivity return,异步去显示 UI 操作,根据密码结果再递归一次 startActivity

所以 showAppLockPasswordWindow() 放到了内部类中,传递参数需要为 final 或者全局变量,所以上面加了一大堆全局赋值的,参数也太多了点。

作者:cczheng
链接:https://juejin.cn/post/7389913027687350284
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值