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写

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="3dp"
    android:background="@android:color/transparent"
    android:src="@drawable/debug_mode_cn"
    android:alpha="0.5"
/>

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

   <java-symbol type="layout" name="safe_mode" />
   <java-symbol type="layout" name="debug_mode_en" />
   <java-symbol type="layout" name="debug_mode_cn" />
   <java-symbol type="layout" name="simple_list_item_2_single_choice" />

  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. 效果图
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值