Android graphics: How an alert window is displayed?

Android graphics: How an alert window is displayed?

 

一、initilization and data structure.

AlertDialog inherites from Dialog and it hold a member of AlertController mAlert.

 

 

In dialog.java, Dialog in constrcuted. it creates a new phoneWindow, and store it in it's mWindow member.

 the window is registered in WMS. so it can interact with WMS to handle the input from users.

 

 

    frameworks/base/policy/src/com/android/internal/policy/impl/Policy.java
        frameworks/base/core/java/android/view/Window.java
 

 

 

164        Window w = PolicyManager.makeNewWindow(mContext);
                         mWindow = w;
                           w.setWindowManager(mWindowManager, null, null);
  
in AlertDiaog constructor, Dialog's phoneWindow will be passed to AlertController's mWindow
 
alertDialog.java
  AlertDialog(Context context, int theme, boolean createThemeContextWrapper) {
            mAlert = new AlertController(getContext(), this, getWindow());
}

 

 

二、show() function.

how the dialog window is shown??

you need to invoke the dialog.show() to display the dialog window, before this, you need to 

setup the contents of dialog.

the code snippet is like:

 1 AlertDialog.Builder ad = new AlertDialog.Builder(cxt);
 2         
 3         ad.setTitle(title);
 4         ad.setMessage(message);
 5         
 6         ad.setPositiveButton(
 7                 b1string,
 8                 new DialogInterface.OnClickListener() {
 9                     
10                     @Override
11                     public void onClick(DialogInterface arg0, int arg1) {
12                         // TODO Auto-generated method stub
13                         
14                     }
15                 }
16                 );
17                     
18         ad.show();

 

I add a function to print the call stack :

E/SS ( 1973): com.android.internal.policy.impl.PhoneWindow$DecorView.setWindowBackground(PhoneWindow.java:2348)

 

E/SS      ( 1973): com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2843)
E/SS      ( 1973): com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:2884)
E/SS      ( 1973): com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
E/SS      ( 1973): com.android.internal.app.AlertController.installContent(AlertController.java:262)
E/SS      ( 1973): android.app.AlertDialog.onCreate(AlertDialog.java:337)
E/SS      ( 1973): android.app.Dialog.dispatchOnCreate(Dialog.java:355)
E/SS      ( 1973): android.app.Dialog.show(Dialog.java:260)

 

in the show() , 
1) it first invokes the AlertDialog.onCreate function to installDecor(), which generates the new decorview(), and inflate the view with the contents
 
2)
the decorView which acts as the root of the view, is registered to the Window manager.
 
 
   public void show() {

             mWindowManager.addView(mDecor, l);
}
 
 
addview里面会生成新的viewroot
 
frameworks/base/core/java/android/view/WindowManagerGlobal.java
 
 public void addView(View view, ViewGroup.LayoutParams params,
                                   Display display, Window parentWindow) {


         root = new ViewRootImpl(view.getContext(), display);



}
the role of viewroot is to interact with WMS, so it can receives key input from users, also it can request WMS to do something.
   a Viewroot has a new layer, surface , phone window here. After this, it can invoke performTraversal() to ask Choreographer to draw the image.
 
 
 

posted on 2013-08-18 15:47  keniee  阅读(405)  评论(0编辑  收藏  举报