android系统添加水印

  1. 寻找安全模式显示的入口

android系统添加水印_第1张图片
如图所示,左下角有安全模式的水印,仿照此方法,重新布局我们需求中的水印即可。
显示的代码在SystemServer.java中

if (safeMode) {
    mActivityManagerService.showSafeModeOverlay();
} else if (debugMode) { // 水印显示入口
    mActivityManagerService.showDebugModeOverlay();
}

依葫芦画瓢写出入口,debugMode可以用属性控制

boolean disableAtlas = SystemProperties.getBoolean("config.disable_atlas", false);
final boolean debugMode = SystemProperties.getBoolean("ro.show.debug_mode", false); // 属性 
    try {
		Slog.i(TAG, "Reading configuration...");
  1. 显示布局
    在ActivityManagerService.java中添加UI显示
    public final void showDebugModeOverlay() {
        java.util.Locale locale = mContext.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        int resorce = com.android.internal.R.layout.debug_mode_en;
        if ("zh".equals(language)) {
            resorce = com.android.internal.R.layout.debug_mode_cn;
        }
        View v = LayoutInflater.from(mContext).inflate(resorce, null);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        lp.format = debugModeView.getBackground().getOpacity();
        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
        ((WindowManager)mContext.getSystemService(
                Context.WINDOW_SERVICE)).addView(v, lp);
    }

从上面的代码可以看出,我们判断了系统的语言,在中文的时候显示中文水印,其他语言则显示英文水印,debug_mode_cn.xml也模仿safe_mode.xml写





值得注意的是,我们在framework添加layout,则需要声明:

   
   
   
   

  1. 水印资源
    盗图啦,UI工程师牛逼(别人做的还是不上传了)
  2. 随语言实时切换
    以上功能基本实现了,大家可能注意到,切换语言后只有重启才能生效,实时切换并不会改变,所以我在又花了点时间研究一下。在语言切换的时候刷新UI
                         null, AppOpsManager.OP_NONE, false, false, MY_PID,
                         Process.SYSTEM_UID, UserHandle.USER_ALL);
                 if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
+                    Message msg = mHandler.obtainMessage(UPDATE_DEBUG_MODE_MSG);
+                    mHandler.sendMessage(msg);
                     intent = new Intent(Intent.ACTION_LOCALE_CHANGED);
                     intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
                     broadcastIntentLocked(null, null, intent,

                 }
                 break;
             }
+            case UPDATE_DEBUG_MODE_MSG: {
+                synchronized (ActivityManagerService.this) {
+                    showDebugModeOverlay();
+                }
+                break;
             }
+            }
         }
     };

     static final int SEND_LOCALE_TO_MOUNT_DAEMON_MSG = 47;
     static final int DISMISS_DIALOG_MSG = 48;
     static final int NOTIFY_TASK_STACK_CHANGE_LISTENERS_MSG = 49;
+    static final int UPDATE_DEBUG_MODE_MSG = 50;
 
     static final int FIRST_ACTIVITY_STACK_MSG = 100;
     static final int FIRST_BROADCAST_QUEUE_MSG = 200;

最终代码`

                 Context.WINDOW_SERVICE)).addView(v, lp);
     }
 
+    private View debugModeView = null;
+    public final void showDebugModeOverlay() {
+        if (null != debugModeView) { // 先删除
+            ((WindowManager)mContext.getSystemService(
+                Context.WINDOW_SERVICE)).removeView(debugModeView);
+        }
+        java.util.Locale locale = mContext.getResources().getConfiguration().locale;
+        String language = locale.getLanguage();
+        int resorce = com.android.internal.R.layout.debug_mode_en;
+        if ("zh".equals(language)) {
+            resorce = com.android.internal.R.layout.debug_mode_cn;
+        }
+        debugModeView = LayoutInflater.from(mContext).inflate(resorce, null);
+        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
+        lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
+        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
+        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
+        lp.gravity = Gravity.BOTTOM;
+        lp.format = debugModeView.getBackground().getOpacity();
+        lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
+                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
+        lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
+        ((WindowManager)mContext.getSystemService(
+                Context.WINDOW_SERVICE)).addView(debugModeView, lp); // 后添加
+    }
+
     public void noteWakeupAlarm(IIntentSender sender, int sourceUid, String sourcePkg) {
         if (!(sender instanceof PendingIntentRecord)) {
             return;

  1. 效果图
    android系统添加水印_第2张图片

你可能感兴趣的:(android_system)