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 ):
[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 ();
}
The specific Policy implementation of the product class is as follows (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 ";
// Classes to be preloaded when the Policy class is created
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 {
// For performance consideration, the classes used to load Policy classes are loaded in advance.
For (String s: preload_classes ){
Try {
// Load the specified class to the Java Virtual Machine and execute the static code segment in the class
Class. forName (s );
} Catch (ClassNotFoundException ex ){
Log. e (TAG, "cocould 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 implements the WindowManagerPolicy Interface
// Here, the return value is written as WindowManagerPolicy, which is more reasonable.
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 ";
// Classes to be preloaded when the Policy class is created
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 {
// For performance consideration, the classes used to load Policy classes are loaded in advance.
For (String s: preload_classes ){
Try {
// Load the specified class to the Java Virtual Machine and execute the static code segment in the class
Class. forName (s );
} Catch (ClassNotFoundException ex ){
Log. e (TAG, "cocould 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 implements the WindowManagerPolicy Interface
// Here, the return value is written as WindowManagerPolicy, which is more reasonable.
Public PhoneWindowManager makeNewWindowManager (){
Return new PhoneWindowManager ();
}
}
The factory-class PolicyManager implementation is as follows (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 {
// The IPolicy implementation class is dynamically loaded during runtime
Try {
// When the Policy class is loaded, the static code segment is executed.
Class policyClass = Class. 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 (
POLICY_IMPL_CLASS_NAME + "cocould not be instantiated", ex );
} Catch (IllegalAccessException ex ){
Throw new RuntimeException (
POLICY_IMPL_CLASS_NAME + "cocould not be instantiated", ex );
}
}
// The constructor is private to ensure that it is a singleton class.
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 {
// The IPolicy implementation class is dynamically loaded during runtime
Try {
// When the Policy class is loaded, the static code segment is executed.
Class policyClass = Class. 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 (
POLICY_IMPL_CLASS_NAME + "cocould not be instantiated", ex );
} Catch (IllegalAccessException ex ){
Throw new RuntimeException (
POLICY_IMPL_CLASS_NAME + "cocould not be instantiated", ex );
}
}
// The constructor is private to ensure that it is a singleton class.
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 ();
}
}
From ASCE1885