1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wm;
18 
19 import static android.util.DisplayMetrics.DENSITY_DEFAULT;
20 
21 import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
22 import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
23 
24 import android.annotation.Nullable;
25 import android.app.ActivityOptions;
26 import android.content.pm.ActivityInfo;
27 import android.graphics.Rect;
28 import android.os.SystemProperties;
29 import android.util.Slog;
30 
31 import com.android.server.wm.LaunchParamsController.LaunchParamsModifier;
32 
33 /**
34  * The class that defines default launch params for tasks in desktop mode
35  */
36 public class DesktopModeLaunchParamsModifier implements LaunchParamsModifier {
37 
38     private static final String TAG =
39             TAG_WITH_CLASS_NAME ? "DesktopModeLaunchParamsModifier" : TAG_ATM;
40     private static final boolean DEBUG = false;
41 
42     // Desktop mode feature flags.
43     private static final boolean DESKTOP_MODE_PROTO1_SUPPORTED =
44             SystemProperties.getBoolean("persist.wm.debug.desktop_mode", false);
45     private static final boolean DESKTOP_MODE_PROTO2_SUPPORTED =
46             SystemProperties.getBoolean("persist.wm.debug.desktop_mode_2", false);
47     // Override default freeform task width when desktop mode is enabled. In dips.
48     private static final int DESKTOP_MODE_DEFAULT_WIDTH_DP = SystemProperties.getInt(
49             "persist.wm.debug.desktop_mode.default_width", 840);
50     // Override default freeform task height when desktop mode is enabled. In dips.
51     private static final int DESKTOP_MODE_DEFAULT_HEIGHT_DP = SystemProperties.getInt(
52             "persist.wm.debug.desktop_mode.default_height", 630);
53 
54     private StringBuilder mLogBuilder;
55 
56     @Override
onCalculate(@ullable Task task, @Nullable ActivityInfo.WindowLayout layout, @Nullable ActivityRecord activity, @Nullable ActivityRecord source, @Nullable ActivityOptions options, @Nullable ActivityStarter.Request request, int phase, LaunchParamsController.LaunchParams currentParams, LaunchParamsController.LaunchParams outParams)57     public int onCalculate(@Nullable Task task, @Nullable ActivityInfo.WindowLayout layout,
58             @Nullable ActivityRecord activity, @Nullable ActivityRecord source,
59             @Nullable ActivityOptions options, @Nullable ActivityStarter.Request request, int phase,
60             LaunchParamsController.LaunchParams currentParams,
61             LaunchParamsController.LaunchParams outParams) {
62 
63         initLogBuilder(task, activity);
64         int result = calculate(task, layout, activity, source, options, request, phase,
65                 currentParams, outParams);
66         outputLog();
67         return result;
68     }
69 
calculate(@ullable Task task, @Nullable ActivityInfo.WindowLayout layout, @Nullable ActivityRecord activity, @Nullable ActivityRecord source, @Nullable ActivityOptions options, @Nullable ActivityStarter.Request request, int phase, LaunchParamsController.LaunchParams currentParams, LaunchParamsController.LaunchParams outParams)70     private int calculate(@Nullable Task task, @Nullable ActivityInfo.WindowLayout layout,
71             @Nullable ActivityRecord activity, @Nullable ActivityRecord source,
72             @Nullable ActivityOptions options, @Nullable ActivityStarter.Request request, int phase,
73             LaunchParamsController.LaunchParams currentParams,
74             LaunchParamsController.LaunchParams outParams) {
75 
76         if (task == null) {
77             appendLog("task null, skipping");
78             return RESULT_SKIP;
79         }
80         if (!task.isActivityTypeStandardOrUndefined()) {
81             appendLog("not standard or undefined activity type, skipping");
82             return RESULT_SKIP;
83         }
84         if (phase < PHASE_WINDOWING_MODE) {
85             appendLog("not in windowing mode or bounds phase, skipping");
86             return RESULT_SKIP;
87         }
88 
89         // Copy over any values
90         outParams.set(currentParams);
91 
92         // In Proto2, trampoline task launches of an existing background task can result in the
93         // previous windowing mode to be restored even if the desktop mode state has changed.
94         // Let task launches inherit the windowing mode from the source task if available, which
95         // should have the desired windowing mode set by WM Shell. See b/286929122.
96         if (DESKTOP_MODE_PROTO2_SUPPORTED && source != null && source.getTask() != null) {
97             final Task sourceTask = source.getTask();
98             outParams.mWindowingMode = sourceTask.getWindowingMode();
99             appendLog("inherit-from-source=" + outParams.mWindowingMode);
100         }
101 
102         if (phase == PHASE_WINDOWING_MODE) {
103             return RESULT_DONE;
104         }
105 
106         if (!currentParams.mBounds.isEmpty()) {
107             appendLog("currentParams has bounds set, not overriding");
108             return RESULT_SKIP;
109         }
110 
111         // Update width and height with default desktop mode values
112         float density = (float) task.getConfiguration().densityDpi / DENSITY_DEFAULT;
113         final int width = (int) (DESKTOP_MODE_DEFAULT_WIDTH_DP * density + 0.5f);
114         final int height = (int) (DESKTOP_MODE_DEFAULT_HEIGHT_DP * density + 0.5f);
115         outParams.mBounds.right = width;
116         outParams.mBounds.bottom = height;
117 
118         // Center the task in window bounds
119         Rect windowBounds = task.getWindowConfiguration().getBounds();
120         outParams.mBounds.offset(windowBounds.centerX() - outParams.mBounds.centerX(),
121                 windowBounds.centerY() - outParams.mBounds.centerY());
122 
123         appendLog("setting desktop mode task bounds to %s", outParams.mBounds);
124 
125         return RESULT_DONE;
126     }
127 
initLogBuilder(Task task, ActivityRecord activity)128     private void initLogBuilder(Task task, ActivityRecord activity) {
129         if (DEBUG) {
130             mLogBuilder = new StringBuilder(
131                     "DesktopModeLaunchParamsModifier: task=" + task + " activity=" + activity);
132         }
133     }
134 
appendLog(String format, Object... args)135     private void appendLog(String format, Object... args) {
136         if (DEBUG) mLogBuilder.append(" ").append(String.format(format, args));
137     }
138 
outputLog()139     private void outputLog() {
140         if (DEBUG) Slog.d(TAG, mLogBuilder.toString());
141     }
142 
143     /** Whether desktop mode is supported. */
isDesktopModeSupported()144     static boolean isDesktopModeSupported() {
145         return DESKTOP_MODE_PROTO1_SUPPORTED || DESKTOP_MODE_PROTO2_SUPPORTED;
146     }
147 }
148