Android Framework中的PolicyManager简介
发布日期:2021-08-28 19:37:38 浏览次数:5 分类:技术文章

本文共 4042 字,大约阅读时间需要 13 分钟。

hot3.png

PolicyManager类位于framework\base\core\java\com\android\internal\policy目录中的PolicyManager.java文件中。PolicyManager主要用于创建Window类、LayoutInflater类和WindowManagerPolicy类,它扮演着简单工厂模式中的工厂类角色,而抽象产品角色由IPolicy接口实现,具体产品角色由Policy类实现。它们的关系如下图所示:

从下面三个类的代码可以看出,都使用了hide注解,因此,这三个类都是不对外公开的API,只限于Framework内部使用。

抽象产品类IPolicy实现如下(IPolicy.java):

  1. package com.android.internal.policy;  

  2.   

  3. import android.content.Context;  

  4. import android.view.LayoutInflater;  

  5. import android.view.Window;  

  6. import android.view.WindowManagerPolicy;  

  7.   

  8. /** 

  9.  * {@hide} 

  10.  */  

  11.   

  12. /* The implementation of this interface must be called Policy and contained 

  13.  * within the com.android.internal.policy.impl package */  

  14. public interface IPolicy {  

  15.     public Window makeNewWindow(Context context);  

  16.   

  17.     public LayoutInflater makeNewLayoutInflater(Context context);  

  18.   

  19.     public WindowManagerPolicy makeNewWindowManager();  

  20. }  

具体产品类Policy实现如下(Policy.java):

  1. package com.android.internal.policy.impl;  

  2.   

  3. import android.content.Context;  

  4. import android.util.Log;  

  5.   

  6. import com.android.internal.policy.IPolicy;  

  7. import com.android.internal.policy.impl.PhoneLayoutInflater;  

  8. import com.android.internal.policy.impl.PhoneWindow;  

  9. import com.android.internal.policy.impl.PhoneWindowManager;  

  10.   

  11. /** 

  12.  * {@hide} 

  13.  */  

  14.   

  15. // Simple implementation of the policy interface that spawns the right  

  16. // set of objects  

  17. public class Policy implements IPolicy {  

  18.     private static final String TAG = "PhonePolicy";  

  19.   

  20.     //需要在Policy类创建时预先加载的一些类  

  21.     private static final String[] preload_classes = {  

  22.         "com.android.internal.policy.impl.PhoneLayoutInflater",  

  23.         "com.android.internal.policy.impl.PhoneWindow",  

  24.         "com.android.internal.policy.impl.PhoneWindow$1",  

  25.         "com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback",  

  26.         "com.android.internal.policy.impl.PhoneWindow$DecorView",  

  27.         "com.android.internal.policy.impl.PhoneWindow$PanelFeatureState",  

  28.  "com.android.internal.policy.impl.PhoneWindow$PanelFeatureState$SavedState",  

  29.     };  

  30.   

  31.     static {  

  32.         // 出于性能考虑,在Policy类加载时提前加载会用到的相关类  

  33.         for (String s : preload_classes) {  

  34.             try {  

  35.         //加载指定类到Java虚拟机中,并执行类中的静态代码段  

  36.                 Class.forName(s);  

  37.             } catch (ClassNotFoundException ex) {  

  38.                 Log.e(TAG, "Could not preload class for phone policy: " + s);  

  39.             }  

  40.         }  

  41.     }  

  42.   

  43.     public PhoneWindow makeNewWindow(Context context) {  

  44.         return new PhoneWindow(context);  

  45.     }  

  46.   

  47.     public PhoneLayoutInflater makeNewLayoutInflater(Context context) {  

  48.         return new PhoneLayoutInflater(context);  

  49.     }  

  50.   

  51.     //PhoneWindowManager实现了WindowManagerPolicy接口      

  52.     //这里返回值直接写成WindowManagerPolicy更合理些  

  53.     public PhoneWindowManager makeNewWindowManager() {  

  54.         return new PhoneWindowManager();  

  55.     }  

  56. }  

工厂类PolicyManager实现如下(PolicyManager.java):

  1. <pre class="java" name="code">package com.android.internal.policy;  

  2.   

  3. import android.content.Context;  

  4. import android.view.LayoutInflater;  

  5. import android.view.Window;  

  6. import android.view.WindowManagerPolicy;  

  7.   

  8. import com.android.internal.policy.IPolicy;  

  9.   

  10. /** 

  11.  * {@hide} 

  12.  */  

  13.   

  14. public final class PolicyManager {  

  15.     private static final String POLICY_IMPL_CLASS_NAME =  

  16.         "com.android.internal.policy.impl.Policy";  

  17.   

  18.     private static final IPolicy sPolicy;  

  19.   

  20.     static {  

  21.        // 运行时动态装载IPolicy实现类  

  22.         try {  

  23.            //加载Policy类的同时会执行其中的静态代码段  

  24.             Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);  

  25.             sPolicy = (IPolicy)policyClass.newInstance();  

  26.         } catch (ClassNotFoundException ex) {  

  27.             throw new RuntimeException(  

  28.                     POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);  

  29.         } catch (InstantiationException ex) {  

  30.             throw new RuntimeException(  

  31.                     POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);  

  32.         } catch (IllegalAccessException ex) {  

  33.             throw new RuntimeException(  

  34.                     POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);  

  35.         }  

  36.     }  

  37.   

  38.     // 构造函数私有,保证是单例类  

  39.     private PolicyManager() {}  

  40.   

  41.     // The static methods to spawn new policy-specific objects  

  42.     public static Window makeNewWindow(Context context) {  

  43.         return sPolicy.makeNewWindow(context);  

  44.     }  

  45.   

  46.     public static LayoutInflater makeNewLayoutInflater(Context context) {  

  47.         return sPolicy.makeNewLayoutInflater(context);  

  48.     }  

  49.   

  50.     public static WindowManagerPolicy makeNewWindowManager() {  

  51.         return sPolicy.makeNewWindowManager();  

  52.     }  

  53. }</pre><br>  

  54. <br>  

  55. <p></p>  

  56. <p></p>  

  57. <p></p>  

  58. <pre></pre>  

  59. <pre></pre>  

更多

  • 上一篇

  • 下一篇

  • 5

转载于:https://my.oschina.net/bintojojo/blog/278615

转载地址:https://blog.csdn.net/weixin_33969116/article/details/92043207 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:elasticsearch Java API 客户端(TransportClient )
下一篇:Linux下的主机参数自测

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月02日 13时05分23秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

央行DECP开测,拉开全球货币霸权之战大幕 | 凌云时刻 2021-06-30
超越极限:阿里云最新ASPLOS论文解读 | 凌云时刻 2021-06-30
为了永不停机的计算服务 | 凌云时刻 2021-06-30
独家:为了永不停机的计算服务 - 三月月刊 2021-06-30
朴灵:云计算的开发者视界中,OpenAPI 是绝对主角 | 凌云时刻 2021-06-30
重磅:达摩院医疗AI团队CVPR'20论文解读 | 凌云时刻 2021-06-30
望月新一证明abc猜想的正确姿势 | 凌云时刻 2021-06-30
张献涛:虚拟化技术 40 年演进史 | 凌云时刻 2021-06-30
我在阿里云做产品市场:初入茅庐 | 凌云时刻 2021-06-30
阿里云云安全中心入选 Gartner CWPP 全球市场指南 | 凌云时刻 2021-06-30
阿里云超算战纪 | 凌云时刻 2019-04-27
斥资57亿美金,Facebook 入局印度 | 凌云时刻 2019-04-27
14个阿里高管的研发管理实践和思考 | 凌云时刻 2019-04-27
eBPF技术应用云原生网络实践:kubernetes网络 | 凌云时刻 2019-04-27
在售后技术服务里,Kubernetes到底是什么? | 凌云时刻 2019-04-27
VIPKID米雯娟:好公司的标准一直是“既要、又要、还要” | 凌云时刻 2019-04-27
疫情防控的“第二战场” | 凌云时刻 2019-04-27
马上上线!谷歌与苹果联手抗疫,打造基于蓝牙设备的接触史回溯 | 凌云时刻... 2019-04-27
美年健康俞熔:创业者最重要的是锻造内心、熬过拐点 | 凌云时刻 2019-04-27
我在阿里云做产品:如何打出一个爆款云服务器? | 凌云时刻 2019-04-27