android 拦截第三方应用点击事件

2025-09-04  本文已影响0人  gale_小米
image.png
不过gms的,直接再源码里面改

Index: PermissionController/src/com/android/permissioncontroller/safetycenter/ui/IssueCardPreference.java
===================================================================
--- PermissionController/src/com/android/permissioncontroller/safetycenter/ui/IssueCardPreference.java  (revision 9152)
+++ PermissionController/src/com/android/permissioncontroller/safetycenter/ui/IssueCardPreference.java  (working copy)
@@ -59,6 +59,8 @@
 import com.google.android.material.shape.ShapeAppearanceModel;
 
 import java.util.Objects;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
 
 /** A preference that displays a card representing a {@link SafetyCenterIssue}. */
 @RequiresApi(TIRAMISU)
@@ -175,6 +177,7 @@
                 ((LinearLayout) holder.findViewById(R.id.issue_card_action_button_list));
         buttonList.removeAllViews(); // This view may be recycled from another issue
 
+        Log.e(TAG,"getActions="+mIssue.getActions());
         for (int i = 0; i < mIssue.getActions().size(); i++) {
             SafetyCenterIssue.Action action = mIssue.getActions().get(i);
             ActionButtonBuilder builder =
@@ -473,9 +476,16 @@
             setButtonColors(button);
             setButtonLayout(button);
             button.setText(mAction.getLabel());
+            Log.e(TAG,"getLabel="+mAction.getLabel()+",getId="+mAction.getId());
             button.setEnabled(!mAction.isInFlight());
             button.setOnClickListener(
                     view -> {
+                         AlertDialog.Builder normalDialog = new AlertDialog.Builder(mContext);
+                    normalDialog.setTitle(mContext.getString(R.string.review_button_continue));
+                    normalDialog.setMessage(mContext.getString(R.string.review_button_continue));
+                    normalDialog.setPositiveButton(mContext.getString(R.string.ok) , new DialogInterface.OnClickListener() {
+                        @Override
+                    public void onClick(DialogInterface dialog, int which) {      
                         if (SdkLevel.isAtLeastU()
                                 && mAction.getConfirmationDialogDetails() != null) {
                             ConfirmActionDialogFragment.newInstance(
@@ -506,8 +516,18 @@
                                             mIssue,
                                             mIsDismissed);
                         }
+
+            }});
+            normalDialog.setNegativeButton(mContext.getString(R.string.cancel) , new DialogInterface.OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialog, int which) {
+                            dialog.dismiss();
+                        }
                     });
+            normalDialog.create().show();
 
+                    });
+
             maybeAddSpaceToView(buttonList);
             buttonList.addView(button);
         }

过GMS的Google包就从activity源码里面添加拦截事件

Index: core/java/android/app/Activity.java
===================================================================
--- core/java/android/app/Activity.java (revision 9152)
+++ core/java/android/app/Activity.java (working copy)
@@ -194,7 +194,11 @@
 
 import android.util.DisplayMetrics;//AW_CODE;jscese
 
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.widget.TextView;
 

 
+   android.widget.LinearLayout mButtonList;
+   // 临时存储待处理的点击事件
+    private View pendingClickView;
+    private int buttonParentId;
+    private android.widget.Button issueCardActionListButton;

@CallSuper
    protected void onResume() {
        if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " + this);
        dispatchActivityResumed();
        mActivityTransitionState.onResume(this);
        getAutofillClientController().onActivityResumed();

        notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_RESUME);

        mCalled = true;
+        String myClassName = mComponent.getClassName();
+        if (myClassName.contains("safetycenter.ui.SafetyCenterActivity")) {
+           initIssue_card_action_button_list(1000);
+        }
    }

 
+    private void initIssue_card_action_button_list(long startTime){
+       mHandler.postDelayed(() -> {//延迟处理,避免UI未加载
+               // 资源名称(对应布局中android:id="@+id/issue_card_action_button_list")
+               buttonParentId = getResources().getIdentifier("issue_card_action_button_list","id",getPackageName());
+               if (buttonParentId==0) {//无法获取到id就读取反编译拿到的ID
+                   buttonParentId=0x7f0a024d;//2131362381;
+               }
+               if (buttonParentId>0) {
+                   // if (mButtonList ==null) {
+                       mButtonList =(android.widget.LinearLayout)findViewById(buttonParentId);
+                       //Log.e(TAG, "onResume mButtonList=" + mButtonList);
+                       if (mButtonList !=null) {
+                           //Log.e(TAG, "onResume getChildCount=" + mButtonList.getChildCount());
+                           if (mButtonList.getChildCount()==1) {
+                               initIssue_card_action_button();
+                           }else{//查找RecyclerView里面的View
+                               findRecyclerViewIssue_card_action_button_list2(1000);
+                           }
+                       }
+                   // }
+               }
+            },startTime);
+    }
+
+    private void initIssue_card_action_button(){
+       issueCardActionListButton=(android.widget.Button)mButtonList.getChildAt(0);
+       Log.e(TAG, "onResume button=" + issueCardActionListButton);
+       if (issueCardActionListButton !=null) {
+           // 拦截事件,不会触发onClick
+           issueCardActionListButton.setOnTouchListener((v, event) -> {
+               if (event.getAction() == MotionEvent.ACTION_UP) {
+                   if (pendingClickView==null) {
+                       onKeyDownTouch(v);
+                       return true;
+                   }
+               }
+               return false;
+           });
+       }
+    }
     /**
+     * 这个view首次开机是个延迟加载的,所以循环遍历初始化view 
+     */
+   private void findRecyclerViewIssue_card_action_button_list2(long startTime){
+       mHandler.postDelayed(() -> {//延迟处理,避免UI未加载
+           if (mButtonList != null) {
+               ViewGroup parentView= (ViewGroup) mButtonList.getParent().getParent();
+               if (parentView.getClass().getName().contains("androidx.recyclerview.widget.RecyclerView")) {
+                   for (int i =0;i<parentView.getChildCount();i++){
+                       if (parentView.getChildAt(i) instanceof ViewGroup) {
+                           ViewGroup childView1 = (ViewGroup) parentView.getChildAt(i);
+                           android.widget.LinearLayout _mButtonList =(android.widget.LinearLayout)childView1.findViewById(buttonParentId);
+                           if(_mButtonList ==null){
+                               continue;
+                           }
+                           mButtonList =_mButtonList;
+                           //Log.e(TAG, "onResume findRecyclerViewIssue_card_action_button_list2 =" + childView1.getClass().getName()+",_mButtonList getChildCount="+mButtonList.getChildCount()+",i="+i);
+                           if (mButtonList.getChildCount()==1) {
+                               initIssue_card_action_button();
+                               break;
+                           }
+                       }
+                   }
+               }
+               if (issueCardActionListButton==null) {//循环遍历
+                   findRecyclerViewIssue_card_action_button_list2(startTime);
+               }
+           }
+        },startTime);
+   }
+    
+
+    /**
+     * 通过反射获取当前应用R$id中的资源ID
+     * @param context 上下文(用于获取包名)
+     * @param resourceName 资源名称(如"submit_btn")
+     * @return 资源ID,获取失败返回0
+     */
+    private int getCurrentAppIdByReflection(Context context, String resourceName) {
+        try {
+            // 获取当前应用的包名
+            String packageName = context.getPackageName();
+            // R$id类的完整路径:包名.R.id
+            Class<?> rIdClass = Class.forName(packageName + ".R$id");
+            // 获取资源名称对应的字段(即ID)
+            return rIdClass.getField(resourceName).getInt(null);
+        } catch (ClassNotFoundException e) {
+            Log.e(TAG, "未找到R$id类,请检查包名是否正确", e);
+        } catch (NoSuchFieldException e) {
+            Log.e(TAG, "R$id中没有名为" + resourceName + "的资源", e);
+        } catch (IllegalAccessException e) {
+            Log.e(TAG, "访问R$id字段失败", e);
+        } catch (Exception e) {
+            Log.e(TAG, "获取资源ID失败", e);
+        }
+        return 0;
+    }
+
+
+    private void onKeyDownTouch(View view){
+        if (pendingClickView==null) {
+            pendingClickView = view; // 暂存点击的View
+            view.setClickable(false);
+        }
+        AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
+        normalDialog.setTitle(getString(com.android.internal.R.string.set_up_screen_lock_action_label));
+        normalDialog.setMessage(getString(com.android.internal.R.string.policydesc_forceLock));
+        normalDialog.setPositiveButton(getString(com.android.internal.R.string.ok) , (dialog, which) ->{
+            view.setClickable(true);
+            pendingClickView.performClick(); // 触发原onClick
+            pendingClickView=null;
+        });
+        normalDialog.setNegativeButton(getString(com.android.internal.R.string.cancel) , (dialog, which) -> {
+            dialog.dismiss();
+            view.setClickable(true);
+            pendingClickView=null;
+        });
+        normalDialog.setCancelable(false).create().show();
+    }
+

上一篇 下一篇

猜你喜欢

热点阅读