目录 | ||
---|---|---|
|
Android N引入了Multi-Window,Freeform(自由窗口)模式是其中的一种。自由窗口模式下可以实现窗口的自由缩放、移动。而我们知道每个应用都有一个窗口,所以当所有的应用窗口都没有独占屏幕的时候,就可以同时在屏幕自由的显示所有应用,达到跟PC桌面一样的效果,当然这个自由窗口受限于screen width等窗口width相关的配置,并不等同于开发者里面的强制桌面模式。
如果你想要在大/超大屏幕上使用安卓系统,freeform是比较合适的,虽然兼容适配特性还没有TV,phone等设备好,但实际在系统层面的支持已经有具备了相当的能力。
本文会介绍freeform模式的功能开启方式,在framework的代码实现细节和提供给用户和开发者使用的功能特性。
1. freeform功能开启
官方开启功能的方法是在 N上的使用的,后续的版本加入了复杂的多屏窗口逻辑,开启方法最好是参考AOSP里面开启freeform的模拟器的配置,以下是基于安卓12,参考goldfish的tablet freeform 模拟器配置,配置被WindowManagerService和ActivityTaskManagerService 读取后做实现,具体可以查代码逻辑。
services/core/java/com/android/server/wm/WindowManagerService.java
代码块 | ||
---|---|---|
| ||
Settings.Global.putString(resolver, Settings.Global.DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, "1");
Settings.Global.putString(resolver, Settings.Global.DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, "1");
Settings.Global.putString(resolver, Settings.Global.DEVELOPMENT_WM_DISPLAY_SETTINGS_PATH, "vendor/etc/display_settings_freeform.xml"); |
data/etc/android.software.freeform_window_management.xml
代码块 | ||
---|---|---|
| ||
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<permissions>
<feature name="android.software.freeform_window_management" />
</permissions> |
services/core/java/com/android/server/wm/ActivityTaskManagerService.java
代码块 | ||
---|---|---|
| ||
public void retrieveSettings(ContentResolver resolver) {
Settings.Global.putInt(resolver, DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, 1);
Settings.Global.putInt(resolver, DEVELOPMENT_FORCE_RESIZABLE_ACTIVITIES, 1); |
display_settings_freeform.xml
代码块 | ||
---|---|---|
| ||
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<display-settings>
<!-- Use physical port number instead of local id -->
<config identifier="1" />
<!-- Set windowing mode to WINDOWING_MODE_FREEFORM -->
<display name="port:0"
windowingMode="5" />
</display-settings> |
overlay/frameworks/base/core/res/res/values/config.xml
代码块 | ||
---|---|---|
| ||
<!-- The device supports freeform window management. Windows have title bars and can be moved
and resized. If you set this to true, you also need to add
PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT feature to your device specification.
The duplication is necessary, because this information is used before the features are
available to the system.-->
<bool name="config_freeformWindowManagement">true</bool> |
在device.mk里面修改
代码块 |
---|
PRODUCT_COPY_FILES += \
frameworks/native/data/etc/android.software.freeform_window_management.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.software.freeform_window_management.xml \
device/xxx/display_settings_freeform.xml:$(TARGET_COPY_OUT_VENDOR)/etc/display_settings_freeform.xml \ |
以上配置方法可以实现在不同设备上实现freeform,不局限于特定配置的应用,也不局限于特定配置的设备,效果如下:
...
2. freeform功能实现
activity其实提供了自由窗口的启动方式,系统应用可以直接调用,三方应用则可以通过反射方式(如果在高版本不行,建议自行研究黑科技绕过系统反射限制):
代码块 | ||
---|---|---|
| ||
//freeform模式
private static final int WINDOWING_MODE_FREEFORM = 5;
//获取屏幕高宽
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int screenWidth = metric.widthPixels;
int screenHeight = metric.heightPixels;
Intent intent = new Intent(MainActivity.this, FreeformActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT | Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityOptions activityOptions = ActivityOptions.makeBasic();
//设置为freeform模式
try {
Method method = ActivityOptions.class.getMethod("setLaunchWindowingMode", int.class);
method.invoke(activityOptions, WINDOWING_MODE_FREEFORM);
} catch (Exception e) {
e.printStackTrace();
}
//freeform模式下自由窗口的大小
int freeformWidth = 400;
int freeformHeight = 400;
//居中显示
int left = screenWidth / 2 - freeformWidth / 2;
int top = screenHeight / 2 - freeformHeight / 2;
int right = screenWidth / 2 + freeformWidth / 2;
int bottom = screenHeight / 2 + freeformHeight / 2;
activityOptions.setLaunchBounds(new Rect(left,top,right,bottom));
Bundle bundle = activityOptions.toBundle();
startActivity(intent,bundle);
这是单个activity实现freeform的方式,如何实现所有activity,需要检查WMS和ATMS代码逻辑。当WMS和ATMS保存freeform的配置的时候,实际是保存到settings数据库了,那数据在哪里使用。
/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
void updateFreeformWindowManagement() {
ContentResolver resolver = mContext.getContentResolver();
final boolean freeformWindowManagement = mContext.getPackageManager().hasSystemFeature(
FEATURE_FREEFORM_WINDOW_MANAGEMENT) || Settings.Global.getInt(
resolver, DEVELOPMENT_ENABLE_FREEFORM_WINDOWS_SUPPORT, 0) != 0;
if (mAtmService.mSupportsFreeformWindowManagement != freeformWindowManagement) {
mAtmService.mSupportsFreeformWindowManagement = freeformWindowManagement;
synchronized (mGlobalLock) {
// Notify the root window container that the display settings value may change.
mRoot.onSettingsRetrieved();
}
}
} |
在当前类freeformWindowManagement/mSupportsFreeformWindowManagement都没有地方使用,只是保存了数据库,让mRoot.onSettingsRetrieved();
/frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java
代码块 | ||
---|---|---|
| ||
void onSettingsRetrieved() {
final int numDisplays = mChildren.size();
for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
final DisplayContent displayContent = mChildren.get(displayNdx);
final boolean changed = mWmService.mDisplayWindowSettings.updateSettingsForDisplay(
displayContent);
if (!changed) {
continue;
}
displayContent.reconfigureDisplayLocked();
// We need to update global configuration as well if config of default display has
// changed. Do it inline because ATMS#retrieveSettings() will soon update the
// configuration inline, which will overwrite the new windowing mode.
if (displayContent.isDefaultDisplay) {
final Configuration newConfig = mWmService.computeNewConfiguration(
displayContent.getDisplayId());
mWmService.mAtmService.updateConfigurationLocked(newConfig, null /* starting */,
false /* initLocale */);
}
}
} |
遍历displaycontent,更新settings的数据,在函数updateSettingsForDisplay执行,而displaycontent就是display在framework层的数据结构,window属于display的下层数据结构,即一个系统可以有很多display(默认只有一个),有很多window都显示在这个display上,window的configuration都依赖于displaycontent。
先看一下mDisplayWindowSettings,他持有的SettingsProvider就是数据提供者,真正实现类是DisplayWindowSettingsProvider,描述如下:
代码块 | ||
---|---|---|
| ||
/**
* Implementation of {@link SettingsProvider} that reads the base settings provided in a display
* settings file stored in /vendor/etc and then overlays those values with the settings provided in
* /data/system.
*
* @see DisplayWindowSettings
*/
class DisplayWindowSettingsProvider implements SettingsProvider {
private static final String TAG = TAG_WITH_CLASS_NAME
? "DisplayWindowSettingsProvider" : TAG_WM;
private static final String DATA_DISPLAY_SETTINGS_FILE_PATH = "system/display_settings.xml";
private static final String VENDOR_DISPLAY_SETTINGS_FILE_PATH = "etc/display_settings.xml"; |
也就是说对display settings的初始化配置,可以在这两个文件里面修改和覆盖,然后在WMS初始化的时候也可以用代码动态设置,这是大的逻辑,当然具体能支持配置哪些参数,需要检查里面的代码。
updateSettingsForDisplay更新了display 的windowingmode, e.g. if this device is PC and if this device supports freeform。windowingmode即是描述显示模式。
代码块 |
---|
/**
* Updates settings for the given display after system features are loaded into window manager
* service, e.g. if this device is PC and if this device supports freeform.
*
* @param dc the given display.
* @return {@code true} if any settings for this display has changed; {@code false} if nothing
* changed.
*/
boolean updateSettingsForDisplay(DisplayContent dc) {
if (dc.getWindowingMode() != getWindowingModeLocked(dc)) {
// For the time being the only thing that may change is windowing mode, so just update
// that.
dc.setWindowingMode(getWindowingModeLocked(dc));
return true;
}
return false;
} |
可以看到这个mSupportsFreeformWindowManagement,这里的判断是否支持,是否设置freeform,然后返回结果。这个逻辑是针对所有display的,当然也会针对所有window,activity。
代码块 | ||
---|---|---|
| ||
private int getWindowingModeLocked(SettingsProvider.SettingsEntry settings, DisplayContent dc) {
int windowingMode = settings.mWindowingMode;
// This display used to be in freeform, but we don't support freeform anymore, so fall
// back to fullscreen.
if (windowingMode == WindowConfiguration.WINDOWING_MODE_FREEFORM
&& !mService.mAtmService.mSupportsFreeformWindowManagement) {
return WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
}
// No record is present so use default windowing mode policy.
if (windowingMode == WindowConfiguration.WINDOWING_MODE_UNDEFINED) {
windowingMode = mService.mAtmService.mSupportsFreeformWindowManagement
&& (mService.mIsPc || dc.forceDesktopMode())
? WindowConfiguration.WINDOWING_MODE_FREEFORM
: WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
}
return windowingMode;
} |
对于新建的window,windowstate是一个window的数据结构。系列第一篇写到window在WMS和客户端的创建过程:
代码块 | ||
---|---|---|
| ||
WindowManager: WindowState:Window{18bd0d7 u0 Splash Screen com.android.messaging}
WindowManager: java.lang.Throwable
WindowManager: at com.android.server.wm.WindowState.<init>(WindowState.java:751)
WindowManager: at com.android.server.wm.WindowManagerService.addWindow(WindowManagerService.java:1562)
WindowManager: at com.android.server.wm.Session.addToDisplay(Session.java:159)
WindowManager: at android.view.ViewRootImpl.setView(ViewRootImpl.java:923)
WindowManager: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:387)
WindowManager: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:96)
WindowManager: at com.android.server.policy.PhoneWindowManager.addSplashScreen(PhoneWindowManager.java:2802)
WindowManager: at com.android.server.wm.SplashScreenStartingData.createStartingSurface(SplashScreenStartingData.java:56)
WindowManager: at com.android.server.wm.AppWindowToken$1.run(AppWindowToken.java:2258)
WindowManager: at android.os.Handler.handleCallback(Handler.java:883)
WindowManager: at android.os.Handler.dispatchMessage(Handler.java:100)
WindowManager: at android.os.Looper.loop(Looper.java:221)
WindowManager: at android.os.HandlerThread.run(HandlerThread.java:67)
WindowManager: at com.android.server.ServiceThread.run(ServiceThread.java:44)
WindowManager: WindowState:Window{5a02cc1 u0 com.android.messaging/com.android.messaging.ui.conversationlist.ConversationListActivity}
WindowManager: java.lang.Throwable
WindowManager: at com.android.server.wm.WindowState.<init>(WindowState.java:751)
WindowManager: at com.android.server.wm.WindowManagerService.addWindow(WindowManagerService.java:1562)
WindowManager: at com.android.server.wm.Session.addToDisplay(Session.java:159)
WindowManager: at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:525)
WindowManager: at com.android.server.wm.Session.onTransact(Session.java:134)
WindowManager: at android.os.Binder.execTransactInternal(Binder.java:1021)
WindowManager: at android.os.Binder.execTransact(Binder.java:994) |
window相关的配置保存在Configuration包括res,locale等等
代码块 |
---|
private final Configuration mTempConfiguration = new Configuration(); |
而window布局相关的配置在WindowConfiguration
代码块 |
---|
public final WindowConfiguration windowConfiguration = new WindowConfiguration(); |
比如WindowingMode
代码块 |
---|
/** The current windowing mode of the configuration. */
private @WindowingMode int mWindowingMode; |
上文的reconfigureDisplayLocked也会填充WindowConfiguration的数据
代码块 | ||
---|---|---|
| ||
void reconfigureDisplayLocked() {
if (!isReady()) {
return;
}
configureDisplayPolicy();
setLayoutNeeded();
boolean configChanged = updateOrientation();
final Configuration currentDisplayConfig = getConfiguration();
mTmpConfiguration.setTo(currentDisplayConfig);
computeScreenConfiguration(mTmpConfiguration);
final int changes = currentDisplayConfig.diff(mTmpConfiguration);
configChanged |= changes != 0;
if (configChanged) {
mWaitingForConfig = true;
if (mTransitionController.isShellTransitionsEnabled()) {
requestChangeTransitionIfNeeded(changes, null /* displayChange */);
} else if (mLastHasContent) {
mWmService.startFreezingDisplay(0 /* exitAnim */, 0 /* enterAnim */, this);
}
sendNewConfiguration();
}
mWmService.mWindowPlacerLocked.performSurfacePlacement();
} |
当取出Configuration使用时,会把mTempConfiguration从全局Configuration来更新
代码块 | ||
---|---|---|
| ||
public Configuration getConfiguration() {
// If the process has not registered to any display area to listen to the configuration
// change, we can simply return the mFullConfiguration as default.
if (!registeredForDisplayAreaConfigChanges()) {
return super.getConfiguration();
}
// We use the process config this window is associated with as the based global config since
// the process can override its config, but isn't part of the window hierarchy.
mTempConfiguration.setTo(getProcessGlobalConfiguration());
mTempConfiguration.updateFrom(getMergedOverrideConfiguration());
return mTempConfiguration;
} |
也就是拿的default display的配置
代码块 | ||
---|---|---|
| ||
/**
* Current global configuration information. Contains general settings for the entire system,
* also corresponds to the merged configuration of the default display.
*/
Configuration getGlobalConfiguration() {
// Return default configuration before mRootWindowContainer initialized, which happens
// while initializing process record for system, see {@link
// ActivityManagerService#setSystemProcess}.
return mRootWindowContainer != null ? mRootWindowContainer.getConfiguration()
: new Configuration();
} |
总的来说,windowingmode会保存到WMS和displaycontent,这样创建新的display和window的时候,都会使用配置的windowingmode。
3. freeform功能特性
这里部分介绍一下freeform模式与fullscreen模式相比独有的特性,这些特性部分工作得很好,也可以针对性的进行二次开发,更好的满足需求。
3.1 标题栏DecorCaptionView
我们知道所有的activity都有一个 PhoneWindow
,在创建的时候,会创建 DecorView
,DecorView
会判断是否要创立一个 DecorCaptionView
。 然后在 DecorView
中动态改变中,有参数变量,经过onWindowSystemUiVisibilityChanged
(View的可见性改变) 以及 onConfigurationChanged
(各种触发装备改变的条件) 的回调,对DecorView
进行更新是否要新建一个小窗的视图。
/frameworks/base/core/java/com/android/internal/policy/DecorView.java、
代码块 | ||
---|---|---|
| ||
private void updateDecorCaptionStatus(Configuration config) {
final boolean displayWindowDecor = config.windowConfiguration.hasWindowDecorCaption()
&& !isFillingScreen(config);
if (mDecorCaptionView == null && displayWindowDecor) {
// Configuration now requires a caption.
final LayoutInflater inflater = mWindow.getLayoutInflater();
mDecorCaptionView = createDecorCaptionView(inflater);
if (mDecorCaptionView != null) {
if (mDecorCaptionView.getParent() == null) {
addView(mDecorCaptionView, 0,
new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}
removeView(mContentRoot);
mDecorCaptionView.addView(mContentRoot,
new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT));
}
} else if (mDecorCaptionView != null) {
// We might have to change the kind of surface before we do anything else.
mDecorCaptionView.onConfigurationChanged(displayWindowDecor);
enableCaption(displayWindowDecor);
}
} |
/frameworks/base/core/java/com/android/internal/widget/DecorCaptionView.java
代码块 | ||
---|---|---|
| ||
// True if the window is being dragged.
private boolean mDragging = false;
private boolean mOverlayWithAppContent = false;
private View mCaption;
private View mContent;
private View mMaximize;
private View mClose;
// Fields for detecting drag events.
private int mTouchDownX;
private int mTouchDownY;
private boolean mCheckForDragging;
private int mDragSlop; |
DecorCaptionView提供了标题,最大化,关闭,移动窗口等功能
代码块 | ||
---|---|---|
| ||
public boolean onSingleTapUp(MotionEvent e) {
if (mClickTarget == mMaximize) {
toggleFreeformWindowingMode();
} else if (mClickTarget == mClose) {
mOwner.dispatchOnWindowDismissed(
true /*finishTask*/, false /*suppressWindowTransition*/);
}
return true;
} |
关闭和最大化功能的实现如上,移动窗口则是如下函数实现
代码块 | ||
---|---|---|
| ||
case MotionEvent.ACTION_MOVE:
if (!mDragging && mCheckForDragging && (fromMouse || passedSlop(x, y))) {
mCheckForDragging = false;
mDragging = true;
startMovingTask(e.getRawX(), e.getRawY());
// After the above call the framework will take over the input.
// This handler will receive ACTION_CANCEL soon (possible after a few spurious
// ACTION_MOVE events which are safe to ignore).
}
break; |
startMovingTask不单需要使用的DecorCaptionview上,如果当前是小窗模式,同时也没有显示DecorCaptionview,应用的其他view也可能需要移动整个应用,这时候就需要使用这个函数。
代码块 | ||
---|---|---|
| ||
public final boolean startMovingTask(float startX, float startY) {
if (ViewDebug.DEBUG_POSITIONING) {
Log.d(VIEW_LOG_TAG, "startMovingTask: {" + startX + "," + startY + "}");
}
try {
return mAttachInfo.mSession.startMovingTask(mAttachInfo.mWindow, startX, startY);
} catch (RemoteException e) {
Log.e(VIEW_LOG_TAG, "Unable to start moving", e);
}
return false;
} |
3.2 窗口边框ViewOutlineProvider
目前国内的厂商的小窗口,都是依照DecorCaptionView自定义了一个view,来包裹在外面实现边框等的美化。
比如制作边框圆角,是经过 View 的 setOutlineProvider,提供了一个android.view.ViewOutlineProvider,来设置android.graphics.Outline实现圆角或者其他模式。
代码块 | ||
---|---|---|
| ||
@Retention(RetentionPolicy.SOURCE)
@IntDef(flag = false,
value = {
MODE_EMPTY,
MODE_ROUND_RECT,
MODE_PATH,
})
public @interface Mode {} |
3.3 全局显示策略DisplayPolicy
作为系统里边操控显现,dock栏、状况栏、导航栏的风格的首要类, 在每次制作布局之后,都会走到如下applyPostLayoutPolicyLw
函数,进行显示规则堆叠之后,在导航栏更新透明度的时候,将其标记中不透明的深色背景和淡色背景清空。
代码块 | ||
---|---|---|
| ||
// Check if the freeform window overlaps with the navigation bar area. // 查看自在窗口是否与导航栏区域堆叠。
if (!mIsFreeformWindowOverlappingWithNavBar && win.inFreeformWindowingMode()
&& win.mActivityRecord != null && isOverlappingWithNavBar(win)) { // 假如窗口是自在窗口,而且窗口和导航栏堆叠
mIsFreeformWindowOverlappingWithNavBar = true;
} |
/frameworks/base/services/core/java/com/android/server/wm/DisplayPolicy.java
代码块 | ||
---|---|---|
| ||
/**
* @return the current visibility flags with the nav-bar opacity related flags toggled based
* on the nav bar opacity rules chosen by {@link #mNavBarOpacityMode}.
*/
private int configureNavBarOpacity(int appearance, boolean multiWindowTaskVisible,
boolean freeformRootTaskVisible) {
final boolean drawBackground = drawsBarBackground(mNavBarBackgroundWindow);
if (mNavBarOpacityMode == NAV_BAR_FORCE_TRANSPARENT) {
if (drawBackground) {
appearance = clearNavBarOpaqueFlag(appearance);
}
} else if (mNavBarOpacityMode == NAV_BAR_OPAQUE_WHEN_FREEFORM_OR_DOCKED) {
if (multiWindowTaskVisible || freeformRootTaskVisible) {
if (mIsFreeformWindowOverlappingWithNavBar) { // 假如自在窗口和导航栏堆叠
appearance = clearNavBarOpaqueFlag(appearance); // 使导航栏变成不透明的纯深色背景和淡色背景。
} |
更新高度和背景阴影
代码块 | ||
---|---|---|
| ||
private void updateElevation() {
final int windowingMode =
getResources().getConfiguration().windowConfiguration.getWindowingMode();
final boolean renderShadowsInCompositor = mWindow.mRenderShadowsInCompositor;
// If rendering shadows in the compositor, don't set an elevation on the view // 假如在合成器中烘托暗影,请不要在视图上设置高度
if (renderShadowsInCompositor) {
return;
}
float elevation = 0;
final boolean wasAdjustedForStack = mElevationAdjustedForStack;
// Do not use a shadow when we are in resizing mode (mBackdropFrameRenderer not null)
// since the shadow is bound to the content size and not the target size. // 当咱们处于调整巨细办法(mBackdropFrameRenderer不为空)时不要运用暗影,由于暗影绑定到内容巨细而不是方针巨细。
if ((windowingMode == WINDOWING_MODE_FREEFORM) && !isResizing()) {
elevation = hasWindowFocus() ?
DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP : DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP; // 假如有焦点,则为DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP=20,否则为DECOR_SHADOW_UNFOCUSED_HEIGHT_IN_DIP=5
// Add a maximum shadow height value to the top level view.
// Note that pinned stack doesn't have focus
// so maximum shadow height adjustment isn't needed. // 为顶级视图增加最大暗影高度值。注意,固定仓库没有焦点,因而不需求最大暗影高度调整。
// TODO(skuhne): Remove this if clause once b/22668382 got fixed.
if (!mAllowUpdateElevation) {
elevation = DECOR_SHADOW_FOCUSED_HEIGHT_IN_DIP;
}
// Convert the DP elevation into physical pixels.
elevation = dipToPx(elevation);
mElevationAdjustedForStack = true;
} |
3.4 拖拽DragState和窗口层级Dimmer(此部分内容待验证,仅做记录)
...
借助 Android 拖放框架,您可以向应用中添加交互式拖放功能。通过拖放,用户可以在应用中的 View 之间复制或拖动文本、图片、对象(可以通过 URI 表示的任何内容),也可以使用多窗口模式在应用之间拖动这些内容。
https://developer.android.google.cn/guide/topics/ui/drag-drop?hl=en
DragState 类主要用于管理和跟踪拖拽操作的状态。拖拽操作通常涉及用户在屏幕上拖动某个视图或对象,并在特定位置释放它。以下是 DragState 类的主要作用和功能:
管理拖拽动作的状态:DragState 类负责跟踪拖拽操作的各个状态,例如开始拖拽、拖拽中、拖拽结束等。它可以存储和更新与拖拽状态相关的信息。
处理拖拽手势:这个类可能包含了处理用户拖拽手势的逻辑,例如监测用户手指的移动、计算拖拽物体的位置、响应用户的拖拽操作等。
协调拖拽操作:
DragState
类可能与其他系统组件(如窗口管理器或视图系统)协同工作,以确保拖拽操作在屏幕上正确执行。它可能需要调整被拖拽物体的位置,更新UI,或者触发其他操作。提供拖拽状态信息:这个类通常会提供有关拖拽状态的信息,例如拖拽物体的位置、拖拽的类型、拖拽的源对象等。这些信息可以供其他组件使用。
处理拖拽的释放:当用户释放拖拽物体时,DragState 类可能需要执行特定的操作,例如将拖拽物体放置在新的位置、触发操作、或者完成拖拽操作。
支持拖拽的可视化效果:在一些情况下,DragState 类可能需要管理与拖拽相关的可视化效果,例如拖拽物体的影子或者拖拽物体的预览。
代码块 | ||
---|---|---|
| ||
DragState
notifyLocationLocked(float, float)
WindowState touchedWin = mDisplayContent.getTouchableWinAtPointLocked(x, y);
notifyDropLocked(float, float)
final WindowState touchedWin = mDisplayContent.getTouchableWinAtPointLocked(x, y);
WindowManagerService
updatePointerIcon(IWindow)
displayContent.getTouchableWinAtPointLocked(mouseX, mouseY);
restorePointerIconLocked(DisplayContent, float, float)
displayContent.getTouchableWinAtPointLocked(latestX, latestY);
|
Dimmer 类的作用是为实现窗口容器(WindowContainer
)添加“调暗层”(DimLayer)支持,通过在不同的 Z 层级上创建具有不同透明度的黑色层,从而实现调暗的效果。该类主要用于在窗口容器中管理和应用调暗效果。
该类的主要方法和功能如下:
dimAbove
和dimBelow
方法 这些方法用于在指定的窗口容器上方或下方添加调暗层。可以设置调暗层的透明度(alpha)和相对于指定容器的 Z 层级。resetDimStates
该方法标记所有的调暗状态为等待下一次调用 updateDims 时完成。在调用子容器的 prepareSurfaces 之前调用此方法,以允许子容器继续请求保持调暗。updateDims 方法 在调用子容器的 prepareSurfaces 后,通过此方法来更新调暗层的位置和尺寸。根据容器的更新,它可以设置调暗层的位置、大小以及调整动画。
stopDim 方法 用于停止调暗效果,可以在不再需要调暗时调用。
内部类 DimState 表示调暗状态的内部类,包含调暗层的 SurfaceControl、调暗状态等信息。
Dimmer
类用于管理和应用调暗效果,使窗口容器在不同 Z 层级上添加透明度变化的调暗层,以达到调暗的效果。 在 Android 系统中用于处理窗口切换、过渡动画以及调整显示效果时很有用, 如果要对小窗进行圆角的处理,这一层也是要处理的, 不然会出现黑色矩形边的问题。
其他需要适配的特性,此外还有有一些其他特性,需要开发者二次开发,才能更符合用户使用习惯。
3.5 窗口尺寸TaskLaunchParamsModifier
修改ActivityOptions里面修改activity显示的区域,最终在TaskLaunchParamsModifier生效,com.android.server.wm.TaskLaunchParamsModifier#onCalculate才是最终控制activity窗口显示各种具体数据的运算逻辑。
3.6 前后台状态ActivityStack
我们知道Android系统对activity的管理非常严格,这种严格有时候让有一些基本能力都具备,既可以理解成安全性考虑,也可以理解成谷歌犯懒,只提供需要的API接口。这种严格体现在activity的前后台状态时,你会发现获得这个状态是非常难的,从系统端到应用端完全不具备这个能力。
3.7 AppTransition
修改窗口弹出来和关闭的动画来适配桌面模式,com.android.server.wm.AppTransition#loadAnimation里面定义了各种应用进出的动画,如果不对其进行适配,窗口模式应用的进出动画会按照原先手机模式来运行,并不匹配。
3.8 内存
比如JVM内存,lmkd参数最好是进行相应的调整,否则运行效率肯定不会让你满意。
总结
freeform模式控制的是窗口,他让应用像运行在桌面一样,显示区域可以自行控制,这本身就是Android系统可以实现的功能,只是窗口的交互逻辑还不完善。实现方案从效率来看也不够优秀,Java开发的图形程序可以满足基本的使用需求,但是如果作为大屏操作系统,他的运行效率、安全性,硬件适配能力是无法逾越的鸿沟,在窗口这部分核心架构使用这么久的情况下,如果没有更复杂的场景,大可能也不会进行修改了,所以在此基础上优化和发展Android的多屏多窗口能力有更多的挑战。