系统窗口Toast创建过程

1.系统窗户的含义

1.系统窗口不依赖于应用,而应用窗口需要有个Activity与之对应
2.系统窗口由系统创建,应用程序没有权限创建,除了这三个系统窗口TYPE_TOAST,TYPE_INPUT_METHOD,TYPE_WALLPAPTER.

因为在Wms调用addWindow()添加窗口时,会调用WindowManagerPolicy的checkAddPermission()方法检查权限
WindowManagerPolicy的真正实现类是PhoneWindowManager.代码如下

 public int checkAddPermission(WindowManager.LayoutParams attrs, int[] outAppOp) {
    int type = attrs.type;

    outAppOp[0] = AppOpsManager.OP_NONE;

    if (type < WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW
            || type > WindowManager.LayoutParams.LAST_SYSTEM_WINDOW) {
        return WindowManagerGlobal.ADD_OKAY;
    }
    String permission = null;
    switch (type) {
        case TYPE_TOAST:
            // XXX right now the app process has complete control over
            // this...  should introduce a token to let the system
            // monitor/control what they are doing.
            outAppOp[0] = AppOpsManager.OP_TOAST_WINDOW; //toast需要该权限
            break;
        case TYPE_DREAM:
        case TYPE_INPUT_METHOD:
        case TYPE_WALLPAPER:
        case TYPE_PRIVATE_PRESENTATION:
        case TYPE_VOICE_INTERACTION:
            // The window manager will check these. //以上参数不需要权限
            break;
        case TYPE_PHONE:
        case TYPE_PRIORITY_PHONE:
        case TYPE_SYSTEM_ALERT:
        case TYPE_SYSTEM_ERROR:
        case TYPE_SYSTEM_OVERLAY:
            permission = android.Manifest.permission.SYSTEM_ALERT_WINDOW;
            outAppOp[0] = AppOpsManager.OP_SYSTEM_ALERT_WINDOW; //以上参数需要SYSTEM_ALERT_WINDOW 权限,而
            break;
        default:
            permission = android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
    }
    if (permission != null) { //如果对应权限没有.则不通过
        if (mContext.checkCallingOrSelfPermission(permission)
                != PackageManager.PERMISSION_GRANTED) {
            return WindowManagerGlobal.ADD_PERMISSION_DENIED;
        }
    }
    return WindowManagerGlobal.ADD_OKAY;
}

2.Toast调用流程

系统窗口Toast创建过程_第1张图片

1.首先调用 Toast.makeText().show();

   public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
    Toast result = new Toast(context); //1.新建toast对象

    LayoutInflater inflate = (LayoutInflater)  
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null); //2.加载指定view界面
    TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message); 
    tv.setText(text);
    //3.界面和toast绑定
    result.mNextView = v;
    result.mDuration = duration;
    return result;
}

public void show() {
    if (mNextView == null) {
        throw new RuntimeException("setView must have been called");
    }

    INotificationManager service = getService();//1.得到通知管理器服务.这是一个远程服务.
    String pkg = mContext.getOpPackageName();
    TN tn = mTN;  //2.tn是一个stub类,是一个客户端Binder,用来控制Toast的显示隐藏.
    tn.mNextView = mNextView;//3. mNtextView表示接下来要显示的Toast

    try {
        service.enqueueToast(pkg, tn, mDuration);
//4.通过通知管理器把Toast加入到管理器的消息队列中,这是一个IPC调用,在服务端有个mToastQueue队列管理所有的客户端Toast. 服务端会执行
showNextToastLocked()方法,从mToastQueue中取出最前边的Toast,然后调用它的tn对象的show()方法,tn的show()方法真正完成向Wms添加窗口
,第二步会发送一个异步消息,也就是在Toast显示一段时间后清除该函数,既cancelToastLocked();
cancelToastLocked()方法第一步会取出mToastQueue中第一个Toast对象,调用他的tn对象的hide()方法,使其通知Wms移除该Toast,第二步则是从mToastQueue中移除该Toast对象,并检查是否还有Toast对象,有的话继续执行showNextToastLocked()方法.这样,客户端显示Toast的工作被串行化,以保证只有一个该类型窗口.
    } catch (RemoteException e) {
        // Empty
    }
}

2.tn的添加窗口.通过show方法.然后通过handler 调用了handleShow()方法.

 public void handleShow() {
       
        if (mView != mNextView) { //1.判断下一个toast的view 和当前显示的是否不同.不同的话,移除现在的,显示下一个.
            // remove the old view if necessary
            handleHide(); //2.移除当前窗口
            mView = mNextView;
            Context context = mView.getContext().getApplicationContext()//3.得到context和包名
            String packageName = mView.getContext().getOpPackageName();
            if (context == null) {
                context = mView.getContext();
            }
              //3.得到wms
            mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
            if (mView.getParent() != null) { //4.如果他的父节点不为空,则需要先删除它在添加.不然会报错
                mWM.removeView(mView);
            }      
            mWM.addView(mView, mParams);
        }
    }

2.tn的添加删除.通过hide方法.然后通过handler 调用了handleHide()方法.

 public void handleHide() {
        if (mView != null) {
            // note: checking parent() just to make sure the view has
            // been added...  i have seen cases where we get here when
            // the view isn't yet added, so let's try not to crash.
            if (mView.getParent() != null) { //他有父节点,说明被添加为窗口了.删除就可以了
                if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
                mWM.removeView(mView);
            }
            mView = null;
        }
    }

你可能感兴趣的:(系统窗口Toast创建过程)