Android OS

系统窗口创建过程

2019-01-11  本文已影响0人  古风子

客户端通过以下方式添加系统窗口

TextView textView = LayoutInflater.from(MainActivity.this).inflate(R.layout.window_view, null);
  TextView edit = (TextView) textView.findViewById(R.id.passwordEntry);
  edit.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
  
  WindowManager.LayoutParams mLp = new WindowManager.LayoutParams();
  mLp.type = WindowManager.LayoutParams.TYPE_TOAST;
  mLp.width = WindowManager.LayoutParams.MATCH_PARENT;
  mLp.height = WindowManager.LayoutParams.MATCH_PARENT;
  mLp.flags = WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
  | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
  | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
  mLp.format = PixelFormat.TRANSLUCENT;
  mLp.gravity = Gravity.BOTTOM;
  mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
  ((WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE)).addView(textView, mLp);

之后系统的调用流程如下:

windows.png

WindowManagerServices$addWindow

    public int addWindow(Session session, IWindow client, int seq,
            WindowManager.LayoutParams attrs, int viewVisibility, int displayId,
            Rect outContentInsets, Rect outStableInsets, Rect outOutsets,
            InputChannel outInputChannel) {
       // OP_TOAST_WINDOW
       if (win.mAppOp != AppOpsManager.OP_NONE) {
       //在这里进行直接拦截,如果是要拦截的toast,设置改window不可见
       if (WmInjector.isAdToastApp(attrs)) {
           win.setAppOpVisibilityLw(false);
       } else {
          int startOpResult = mAppOps.startOpNoThrow(win.mAppOp, win.getOwningUid(),
               win.getOwningPackage());
           if ((startOpResult != AppOpsManager.MODE_ALLOWED) &&
                (startOpResult != AppOpsManager.MODE_DEFAULT)) {
                win.setAppOpVisibilityLw(false);
           }
       }
        return res;
    }
上一篇下一篇

猜你喜欢

热点阅读