Android Framework中的PolicyManager簡介

PolicyManager類位於framework\base\core\java\com\android\internal\policy目錄中的PolicyManager.java文件中。PolicyManager主要用於創建Window類、LayoutInflater類和WindowManagerPolicy類,它扮演著簡單工廠模式中的工廠類角色,而抽象產品角色由IPolicy接口實現,具體產品角色由Policy類實現。它們的關系如下圖所示:

 

 

從下面三個類的代碼可以看出,都使用瞭hide註解,因此,這三個類都是不對外公開的API,隻限於Framework內部使用。

抽象產品類IPolicy實現如下(IPolicy.java):

[java]
 package com.android.internal.policy;
 
 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.Window;
 import android.view.WindowManagerPolicy;
 
 /**
  * {@hide}
  */
 
 /* The implementation of this interface must be called Policy and contained
  * within the com.android.internal.policy.impl package */
 public interface IPolicy {
     public Window makeNewWindow(Context context);
 
     public LayoutInflater makeNewLayoutInflater(Context context);
 
     public WindowManagerPolicy makeNewWindowManager();
 }
 package com.android.internal.policy;
 
import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.Window;
 import android.view.WindowManagerPolicy;
 
/**
  * {@hide}
  */
 
/* The implementation of this interface must be called Policy and contained
  * within the com.android.internal.policy.impl package */
 public interface IPolicy {
     public Window makeNewWindow(Context context);
 
    public LayoutInflater makeNewLayoutInflater(Context context);
 
    public WindowManagerPolicy makeNewWindowManager();
 }
 
 具體產品類Policy實現如下(Policy.java):
 
[java]
 package com.android.internal.policy.impl;
 
 import android.content.Context;
 import android.util.Log;
 
 import com.android.internal.policy.IPolicy;
 import com.android.internal.policy.impl.PhoneLayoutInflater;
 import com.android.internal.policy.impl.PhoneWindow;
 import com.android.internal.policy.impl.PhoneWindowManager;
 
 /**
  * {@hide}
  */
 
 // Simple implementation of the policy interface that spawns the right 
 // set of objects 
 public class Policy implements IPolicy {
     private static final String TAG = "PhonePolicy";
 
     //需要在Policy類創建時預先加載的一些類 
     private static final String[] preload_classes = {
         "com.android.internal.policy.impl.PhoneLayoutInflater",
         "com.android.internal.policy.impl.PhoneWindow",
         "com.android.internal.policy.impl.PhoneWindow$1",
         "com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback",
         "com.android.internal.policy.impl.PhoneWindow$DecorView",
         "com.android.internal.policy.impl.PhoneWindow$PanelFeatureState",
  "com.android.internal.policy.impl.PhoneWindow$PanelFeatureState$SavedState",
     };
 
     static {
         // 出於性能考慮,在Policy類加載時提前加載會用到的相關類 
         for (String s : preload_classes) {
             try {
         //加載指定類到Java虛擬機中,並執行類中的靜態代碼段 
                 Class.forName(s);
             } catch (ClassNotFoundException ex) {
                 Log.e(TAG, "Could not preload class for phone policy: " + s);
             }
         }
     }
 
     public PhoneWindow makeNewWindow(Context context) {
         return new PhoneWindow(context);
     }
 
     public PhoneLayoutInflater makeNewLayoutInflater(Context context) {
         return new PhoneLayoutInflater(context);
     }
 
     //PhoneWindowManager實現瞭WindowManagerPolicy接口     
     //這裡返回值直接寫成WindowManagerPolicy更合理些 
     public PhoneWindowManager makeNewWindowManager() {
         return new PhoneWindowManager();
     }
 }
 package com.android.internal.policy.impl;
 
import android.content.Context;
 import android.util.Log;
 
import com.android.internal.policy.IPolicy;
 import com.android.internal.policy.impl.PhoneLayoutInflater;
 import com.android.internal.policy.impl.PhoneWindow;
 import com.android.internal.policy.impl.PhoneWindowManager;
 
/**
  * {@hide}
  */
 
// Simple implementation of the policy interface that spawns the right
 // set of objects
 public class Policy implements IPolicy {
     private static final String TAG = "PhonePolicy";
 
    //需要在Policy類創建時預先加載的一些類
     private static final String[] preload_classes = {
         "com.android.internal.policy.impl.PhoneLayoutInflater",
         "com.android.internal.policy.impl.PhoneWindow",
         "com.android.internal.policy.impl.PhoneWindow$1",
         "com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback",
         "com.android.internal.policy.impl.PhoneWindow$DecorView",
         "com.android.internal.policy.impl.PhoneWindow$PanelFeatureState",
  "com.android.internal.policy.impl.PhoneWindow$PanelFeatureState$SavedState",
     };
 
    static {
         // 出於性能考慮,在Policy類加載時提前加載會用到的相關類
         for (String s : preload_classes) {
             try {
      //加載指定類到Java虛擬機中,並執行類中的靜態代碼段
                 Class.forName(s);
             } catch (ClassNotFoundException ex) {
                 Log.e(TAG, "Could not preload class for phone policy: " + s);
             }
         }
     }
 
    public PhoneWindow makeNewWindow(Context context) {
         return new PhoneWindow(context);
     }
 
    public PhoneLayoutInflater makeNewLayoutInflater(Context context) {
         return new PhoneLayoutInflater(context);
     }
 
    //PhoneWindowManager實現瞭WindowManagerPolicy接口  
     //這裡返回值直接寫成WindowManagerPolicy更合理些
     public PhoneWindowManager makeNewWindowManager() {
         return new PhoneWindowManager();
     }
 }
 
 工廠類PolicyManager實現如下(PolicyManager.java):
 
[java]
 <pre class="java" name="code">package com.android.internal.policy;
 
 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.Window;
 import android.view.WindowManagerPolicy;
 
 import com.android.internal.policy.IPolicy;
 
 /**
  * {@hide}
  */
 
 public final class PolicyManager {
     private static final String POLICY_IMPL_CLASS_NAME =
         "com.android.internal.policy.impl.Policy";
 
     private static final IPolicy sPolicy;
 
     static {
        // 運行時動態裝載IPolicy實現類 
         try {
            //加載Policy類的同時會執行其中的靜態代碼段 
             Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);
             sPolicy = (IPolicy)policyClass.newInstance();
         } catch (ClassNotFoundException ex) {
             throw new RuntimeException(
                     POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);
         } catch (InstantiationException ex) {
             throw new RuntimeException(
                     POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
         } catch (IllegalAccessException ex) {
             throw new RuntimeException(
                     POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
         }
     }
 
     // 構造函數私有,保證是單例類 
     private PolicyManager() {}
 
     // The static methods to spawn new policy-specific objects 
     public static Window makeNewWindow(Context context) {
         return sPolicy.makeNewWindow(context);
     }
 
     public static LayoutInflater makeNewLayoutInflater(Context context) {
         return sPolicy.makeNewLayoutInflater(context);
     }
 
     public static WindowManagerPolicy makeNewWindowManager() {
         return sPolicy.makeNewWindowManager();
     }
 }
 <pre class="java" name="code">package com.android.internal.policy;
 
import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.Window;
 import android.view.WindowManagerPolicy;
 
import com.android.internal.policy.IPolicy;
 
/**
  * {@hide}
  */
 
public final class PolicyManager {
     private static final String POLICY_IMPL_CLASS_NAME =
         "com.android.internal.policy.impl.Policy";
 
    private static final IPolicy sPolicy;
 
    static {
        // 運行時動態裝載IPolicy實現類
         try {
            //加載Policy類的同時會執行其中的靜態代碼段
             Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);
             sPolicy = (IPolicy)policyClass.newInstance();
         } catch (ClassNotFoundException ex) {
             throw new RuntimeException(
                     POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);
         } catch (InstantiationException ex) {
             throw new RuntimeException(
                     POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
         } catch (IllegalAccessException ex) {
             throw new RuntimeException(
                     POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
         }
     }
 
    // 構造函數私有,保證是單例類
     private PolicyManager() {}
 
    // The static methods to spawn new policy-specific objects
     public static Window makeNewWindow(Context context) {
         return sPolicy.makeNewWindow(context);
     }
 
    public static LayoutInflater makeNewLayoutInflater(Context context) {
         return sPolicy.makeNewLayoutInflater(context);
     }
 
    public static WindowManagerPolicy makeNewWindowManager() {
         return sPolicy.makeNewWindowManager();
     }
 }
 
 摘自ASCE1885

發佈留言