Introduction to policymanager in Android framework

Source: Internet
Author: User

The policymanager class is located in the policymanager. Java file in the framework \ base \ core \ Java \ com \ Android \ internal \ Policy directory. Policymanager is mainly used to create the window class, layoutinflater class, And windowmanagerpolicy class. It plays the factory class role in the simple factory mode, while the abstract Product role is implemented by the ipolicy interface, the specific product role is implemented by the Policy class. Shows the relationship between them:

We can see from the code of the following three classes that all use hide annotations. Therefore, these three classes are APIs that are not open to the public and are only used within the framework.

Abstract product class ipolicy implementation is as follows (ipolicy. Java ):

Package COM. android. internal. policy; </P> <p> Import android. content. context; <br/> Import android. view. layoutinflater; <br/> Import android. view. window; <br/> Import android. view. windowmanagerpolicy; </P> <p>/** <br/> * {@ hide} <br/> */</P> <p>/* The implementation of this interface must be called policy and contained <br/> * Within the com. android. internal. policy. impl package */<br/> Public interface ipolicy {<br/> Public window makenewwindow (context); </P> <p> Public layoutinflater makenewlayoutinflater (context ); </P> <p> Public windowmanagerpolicy makenewwindowmanager (); <br/>}< br/>

The specific policy implementation of the product class is as follows (policy. Java ):

Package COM. android. internal. policy. impl; </P> <p> Import android. content. context; <br/> Import android. util. log; </P> <p> Import COM. android. internal. policy. ipolicy; <br/> Import COM. android. internal. policy. impl. phonelayoutinflater; <br/> Import COM. android. internal. policy. impl. phonewindow; <br/> Import COM. android. internal. policy. impl. phonewindowmanager; </P> <p>/** <br/> * {@ hide} <br/> */</P> <p> // simple implementation of the Policy Interface that spawns the right <br/> // set of objects <br/> public class policy implements ipolicy {<br/> Private Static final string tag = "phonepolicy "; </P> <p> // classes to be preloaded when the policy class is created <br/> Private Static final string [] preload_classes ={< br/> "com. android. internal. policy. impl. phonelayoutinflater ", <br/>" com. android. internal. policy. impl. phonewindow ", <br/>" com. android. internal. policy. impl. phonewindow $1 ", <br/>" com. android. internal. policy. impl. phonewindow $ contextmenucallback ", <br/>" com. android. internal. policy. impl. phonewindow $ decorview ", <br/>" com. android. internal. policy. impl. phonewindow $ panelfeaturestate ", <br/>" com. android. internal. policy. impl. phonewindow $ panelfeaturestate $ savedstate ", <br/>}; </P> <p> static {<br/> // for performance consideration, classes used during policy class loading <br/> for (string S: preload_classes) {<br/> try {<br/> // load the specified class to the Java Virtual Machine and execute the static code segment in the class <br/> class. forname (s); <br/>} catch (classnotfoundexception ex) {<br/> log. E (TAG, "cocould not preload class for phone policy:" + S ); <br/>}</P> <p> Public phonewindow makenewwindow (context) {<br/> return New phonewindow (context); <br/>}</P> <p> Public phonelayoutinflater makenewlayoutinflater (context) {<br/> return New phonelayoutinflater (context ); <br/>}</P> <p> // phonewindowmanager implements the windowmanagerpolicy interface. <br/> // the return value is written as windowmanagerpolicy. <br/> Public phonewindowmanager makenewwindowmanager () {<br/> return New phonewindowmanager (); <br/>}< br/>

The factory-class policymanager implementation is as follows (policymanager. 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 {// when the runtime dynamically loads ipolicy implementation class try {// when the policy class is loaded, the static code segment class policyclass = Class is executed. forname (policy_impl_class_name); spolicy = (ipolicy) policyclass. newinstance ();} catch (classnotfoundexception ex) {Throw new runtimeexception (policy_impl_class_name + "cocould not be loaded", ex);} catch (instantiationexception ex) {Throw new runtimeexception (Response + "cocould not be instantiated", ex);} catch (illegalaccessexception ex) {Throw new runtimeexception (policy_impl_class_name + "cocould not be instantiated ", ex) ;}}// the constructor is private, which is a singleton class private policymanager () {}// the static methods to spawn new policy-specific objects public static window makenewwindow (context) {return spolicy. makenewwindow (context);} public static layoutinflater makenewlayoutinflater (context) {return spolicy. makenewlayoutinflater (context);} public static windowmanagerpolicy makenewwindowmanager () {return spolicy. makenewwindowmanager () ;}</PRE> <p>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.