当一个类存在两个独立变化的纬度,且这两个纬度都需要进行扩展,我们可以使用桥接模式。下面来看看桥接模式的UML
Abstraction和Implementor就是两个独立纬度变化的类,Implementor相对于Abstraction是一个聚合的关系,也就是Abstraction可能拥有多个Implementor,下面我们来举个例子.
咖啡一般分为4种,大杯加糖,大杯不加糖,小杯加糖和小杯不加糖。对于大杯和小杯,加糖和不加糖其实是两个相对独立纬度的变化.下面先定一个咖啡类
public abstract class Coffee {
protected CoffeeAdditives impl;
public Coffee(CoffeeAdditives impl) {
this.impl = impl;
}
/**
* 咖啡具体是什么样的由子类决定
*/
public abstract void makeCoffee();
}
CoffeeAdditives是一种桥接的方式,咖啡分为大杯和小杯,下面继续看看大杯咖啡和小杯咖啡的定义
public class LargeCoffee extends Coffee{
public LargeCoffee(CoffeeAdditives impl) {
super(impl);
}
@Override
public void makeCoffee() {
System.out.println("大杯的"+impl.addSomething()+"咖啡");
}
}
public class SmallCoffee extends Coffee{
public SmallCoffee(CoffeeAdditives impl) {
super(impl);
}
@Override
public void makeCoffee() {
System.out.println("小杯的"+impl.addSomething()+"咖啡");
}
}
至于加糖不加糖我们通过CoffeeAdditives这种桥接方式定义
public abstract class CoffeeAdditives {
public abstract String addSomething();
}
public class Ordinary extends CoffeeAdditives {
@Override
public String addSomething() {
return "原味";
}
}
public class Sugar extends CoffeeAdditives {
@Override
public String addSomething() {
return "加糖";
}
}
最终调用:
public class Test {
public static void main(String[] args){
//原汁原味
Ordinary ordinary = new Ordinary();
//准备糖类
Sugar sugar = new Sugar();
//大杯咖啡 原味
LargeCoffee largeCoffeeOrdinary = new LargeCoffee(ordinary);
largeCoffeeOrdinary.makeCoffee();
//小杯咖啡 原味
SmallCoffee smallCoffeeOrdinary = new SmallCoffee(ordinary);
smallCoffeeOrdinary.makeCoffee();
//大杯咖啡 加糖
LargeCoffee largeCoffeeSugar = new LargeCoffee(sugar);
largeCoffeeSugar.makeCoffee();
//小杯咖啡 加糖
SmallCoffee smallCoffeeSugar = new SmallCoffee(sugar);
smallCoffeeSugar.makeCoffee();
}
}
总结
这里Coffee对应uml图中的Abstraction, CoffeeAdditives对应的是Implementor这个类,这种桥接模式很好的独立了大杯和小杯,加糖和不加糖两个纬度,你也可以添加另外一个纬度,比如说加奶不加奶,这样就是3个纬度进行桥接,当然你也可以使用继承来实现,不过总的来说桥接模式更加灵活。
Android源码中的桥接模式
比较典型的是Window与WindowManager之间的关系,它们就用到了桥接这种模式
在framework中Window和PhoneWindow构成窗口的抽象部分,其中Window类为该抽象部分的抽象接口,PhoneWindow为抽象部分具体的实现及扩展。而WindowManager则为实现部分的基类,WindowManagerImpl为实现部分具体的逻辑实现,其使用WindowManagerGlobal通过IWindowManager接口与WindowManagerService进行交互(简称WMS),并由WMS完成具体的窗口管理工作. 如下是Window与WindowManager桥梁搭建的主要代码.
public abstract class Window {
//代码省略...
public void setWindowManager(WindowManager wm, IBinder appToken, String appName) {
setWindowManager(wm, appToken, appName, false);
}
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
boolean hardwareAccelerated) {
mAppToken = appToken;
mAppName = appName;
mHardwareAccelerated = hardwareAccelerated
|| SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
if (wm == null) {
wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
}
mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
}
//代码省略...
}
关于WindowManagerService
毫不夸张的说Android中的framework层主要就是由它与另外一个系统服务AMS还有View构成,这三个模块穿插交互在整个framework中。
wms是由SystemServer启动
private void startOtherServices() {
//通过wms的静态方法main获取一个WindowManagerService对象
wm = WindowManagerService.main(context, inputManager,
mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
!mFirstBoot, mOnlyCore, new PhoneWindowManager());
//将wms添加到ServiceManager中
ServiceManager.addService(Context.WINDOW_SERVICE, wm);
}
在WindowManagerService的main中通过runWithScissors执行一个同步的task构造wms实例
public static WindowManagerService main(final Context context, final InputManagerService im,
final boolean haveInputMethods, final boolean showBootMsgs, final boolean onlyCore,
WindowManagerPolicy policy) {
DisplayThread.getHandler().runWithScissors(() ->
sInstance = new WindowManagerService(context, im, haveInputMethods, showBootMsgs,
onlyCore, policy), 0);
return sInstance;
}
WindowManagerService的构造方法大部分是一些窗口管理使用到的成员变量进行初始化.
private WindowManagerService(Context context, InputManagerService inputManager,
boolean haveInputMethods, boolean showBootMsgs, boolean onlyCore,
WindowManagerPolicy policy) {
installLock(this, INDEX_WINDOW);
mRoot = new RootWindowContainer(this);
mContext = context;
mHaveInputMethods = haveInputMethods;
mAllowBootMessages = showBootMsgs;
mOnlyCore = onlyCore;
mLimitedAlphaCompositing = context.getResources().getBoolean(
com.android.internal.R.bool.config_sf_limitedAlpha);
mHasPermanentDpad = context.getResources().getBoolean(
com.android.internal.R.bool.config_hasPermanentDpad);
mInTouchMode = context.getResources().getBoolean(
com.android.internal.R.bool.config_defaultInTouchMode);
mDrawLockTimeoutMillis = context.getResources().getInteger(
com.android.internal.R.integer.config_drawLockTimeoutMillis);
mAllowAnimationsInLowPowerMode = context.getResources().getBoolean(
com.android.internal.R.bool.config_allowAnimationsInLowPowerMode);
mMaxUiWidth = context.getResources().getInteger(
com.android.internal.R.integer.config_maxUiWidth);
mInputManager = inputManager; // Must be before createDisplayContentLocked.
mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
mDisplaySettings = new DisplaySettings();
mDisplaySettings.readSettingsLocked();
mWindowPlacerLocked = new WindowSurfacePlacer(this);
mPolicy = policy;
mTaskSnapshotController = new TaskSnapshotController(this);
LocalServices.addService(WindowManagerPolicy.class, mPolicy);
if(mInputManager != null) {
final InputChannel inputChannel = mInputManager.monitorInput(TAG_WM);
mPointerEventDispatcher = inputChannel != null
? new PointerEventDispatcher(inputChannel) : null;
} else {
mPointerEventDispatcher = null;
}
mFxSession = new SurfaceSession();
mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
mDisplays = mDisplayManager.getDisplays();
for (Display display : mDisplays) {
createDisplayContentLocked(display);
}
mKeyguardDisableHandler = new KeyguardDisableHandler(mContext, mPolicy);
mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
if (mPowerManagerInternal != null) {
mPowerManagerInternal.registerLowPowerModeObserver(
new PowerManagerInternal.LowPowerModeListener() {
@Override
public int getServiceType() {
return ServiceType.ANIMATION;
}
@Override
public void onLowPowerModeChanged(PowerSaveState result) {
synchronized (mWindowMap) {
final boolean enabled = result.batterySaverEnabled;
if (mAnimationsDisabled != enabled && !mAllowAnimationsInLowPowerMode) {
mAnimationsDisabled = enabled;
dispatchNewAnimatorScaleLocked(null);
}
}
}
});
mAnimationsDisabled = mPowerManagerInternal
.getLowPowerState(ServiceType.ANIMATION).batterySaverEnabled;
}
mScreenFrozenLock = mPowerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "SCREEN_FROZEN");
mScreenFrozenLock.setReferenceCounted(false);
mAppTransition = new AppTransition(context, this);
mAppTransition.registerListenerLocked(mActivityManagerAppTransitionNotifier);
final AnimationHandler animationHandler = new AnimationHandler();
animationHandler.setProvider(new SfVsyncFrameCallbackProvider());
mBoundsAnimationController = new BoundsAnimationController(context, mAppTransition,
AnimationThread.getHandler(), animationHandler);
mActivityManager = ActivityManager.getService();
mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
AppOpsManager.OnOpChangedInternalListener opListener =
new AppOpsManager.OnOpChangedInternalListener() {
@Override public void onOpChanged(int op, String packageName) {
updateAppOpsState();
}
};
mAppOps.startWatchingMode(OP_SYSTEM_ALERT_WINDOW, null, opListener);
mAppOps.startWatchingMode(AppOpsManager.OP_TOAST_WINDOW, null, opListener);
// Get persisted window scale setting
mWindowAnimationScaleSetting = Settings.Global.getFloat(context.getContentResolver(),
Settings.Global.WINDOW_ANIMATION_SCALE, mWindowAnimationScaleSetting);
mTransitionAnimationScaleSetting = Settings.Global.getFloat(context.getContentResolver(),
Settings.Global.TRANSITION_ANIMATION_SCALE,
context.getResources().getFloat(
R.dimen.config_appTransitionAnimationDurationScaleDefault));
setAnimatorDurationScale(Settings.Global.getFloat(context.getContentResolver(),
Settings.Global.ANIMATOR_DURATION_SCALE, mAnimatorDurationScaleSetting));
IntentFilter filter = new IntentFilter();
// Track changes to DevicePolicyManager state so we can enable/disable keyguard.
filter.addAction(ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
// Listen to user removal broadcasts so that we can remove the user-specific data.
filter.addAction(Intent.ACTION_USER_REMOVED);
mContext.registerReceiver(mBroadcastReceiver, filter);
mSettingsObserver = new SettingsObserver();
mHoldingScreenWakeLock = mPowerManager.newWakeLock(
PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG_WM);
mHoldingScreenWakeLock.setReferenceCounted(false);
mAnimator = new WindowAnimator(this);
mAllowTheaterModeWakeFromLayout = context.getResources().getBoolean(
com.android.internal.R.bool.config_allowTheaterModeWakeFromWindowLayout);
LocalServices.addService(WindowManagerInternal.class, new LocalService());
initPolicy();
// Add ourself to the Watchdog monitors.
Watchdog.getInstance().addMonitor(this);
openSurfaceTransaction();
try {
createWatermarkInTransaction();
} finally {
closeSurfaceTransaction();
}
showEmulatorDisplayOverlayIfNeeded();
}
WMS主要功能分为两方面,一是对窗口的管理;二是对事件的管理和分发。其接口方法以AIDL的方式定义在IWindowManager.aidl文件中,编译后会生成一个IWindowManager.java接口文件,这个接口文件定义了WMS绝大部分的功能方法,大家有兴趣可以自行查看。作为窗口的管理承担者,WMS中定义了许多各种不同的窗口,它们被定义在WMS的成员变量中.
下面列出一部分相关的成员变量
/**
* List of window tokens that have finished starting their application,
* and now need to have the policy remove their windows.
*/
final ArrayList<AppWindowToken> mFinishedStarting = new ArrayList<>();
/**
* List of window tokens that have finished drawing their own windows and
* no longer need to show any saved surfaces. Windows that's still showing
* saved surfaces will be cleaned up after next animation pass.
*/
final ArrayList<AppWindowToken> mFinishedEarlyAnim = new ArrayList<>();
/**
* List of app window tokens that are waiting for replacing windows. If the
* replacement doesn't come in time the stale windows needs to be disposed of.
*/
final ArrayList<AppWindowToken> mWindowReplacementTimeouts = new ArrayList<>();
/**
* Windows that are being resized. Used so we can tell the client about
* the resize after closing the transaction in which we resized the
* underlying surface.
*/
final ArrayList<WindowState> mResizingWindows = new ArrayList<>();
/**
* Windows whose animations have ended and now must be removed.
*/
final ArrayList<WindowState> mPendingRemove = new ArrayList<>();
/**
* Used when processing mPendingRemove to avoid working on the original array.
*/
WindowState[] mPendingRemoveTmp = new WindowState[20];
/**
* Windows whose surface should be destroyed.
*/
final ArrayList<WindowState> mDestroySurface = new ArrayList<>();
/**
* Windows with a preserved surface waiting to be destroyed. These windows
* are going through a surface change. We keep the old surface around until
* the first frame on the new surface finishes drawing.
*/
final ArrayList<WindowState> mDestroyPreservedSurface = new ArrayList<>();
/**
* Windows that have lost input focus and are waiting for the new
* focus window to be displayed before they are told about this.
*/
ArrayList<WindowState> mLosingFocus = new ArrayList<>();
WMS维护上述的各个成员的变量值,可以看到大量线性表的应用,不同的窗口或同一个窗口在不同的状态阶段有可能位于不同的表中。虽然窗口的状态种类繁多,但是,对于Android来讲窗口的类型主要只有两种,一种是应用窗口,我们常见的Activity所处的窗口,应用对话框窗口,应用弹出的窗口等都属于该类,与该应用相关的Window类主要是PhoneWindow, 其主要应用于手机,PhoneWindow继承于Window,其核心是DecorView, 应用窗口的添加主要就是通过WindowManager的addView方法将一个DecorView添加到WindowManager中。另一种是系统窗口,常见的屏幕顶部状态栏,底部的导航栏,桌面窗口等都是系统窗口,系统窗口没有针对性的封装类,只需要直接通过WindowManager的addView方法将一个View添加到WindowManager中即可.
wms和其它系统服务一样由SystemServer启动,其运行在系统进程里,当一个应用需要创建窗口时通过IPC请求wms生成一个窗口,尔后再由wms向应用返回和窗口交互的消息。addView的实质上是由WindowManagerGlobal的addView方法实现具体的逻辑,辗转多次后最终调用到ViewRootImpl的setView方法,在该方法通过addToDisplay方法向wms发起一个Session请求,这里要注意的是,IWindowSession方法也是一个AIDL接口文件,需要将其编译后才生成IWindowSession.java接口,这里addToDisplay方法最终会调用到Session中对应方法.
public class Session extends IWindowSession.Stub
implements IBinder.DeathRecipient {
@Override
public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs,
int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets,
Rect outOutsets, InputChannel outInputChannel) {
return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId,
outContentInsets, outStableInsets, outOutsets, outInputChannel);
}
}
最终我们回到了wms的addWindow方法.