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 flag. 43 static final boolean DESKTOP_MODE_SUPPORTED = SystemProperties.getBoolean( 44 "persist.wm.debug.desktop_mode", false) || SystemProperties.getBoolean( 45 "persist.wm.debug.desktop_mode_2", false); 46 // Override default freeform task width when desktop mode is enabled. In dips. 47 private static final int DESKTOP_MODE_DEFAULT_WIDTH_DP = SystemProperties.getInt( 48 "persist.wm.debug.desktop_mode.default_width", 840); 49 // Override default freeform task height when desktop mode is enabled. In dips. 50 private static final int DESKTOP_MODE_DEFAULT_HEIGHT_DP = SystemProperties.getInt( 51 "persist.wm.debug.desktop_mode.default_height", 630); 52 53 private StringBuilder mLogBuilder; 54 55 @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)56 public int onCalculate(@Nullable Task task, @Nullable ActivityInfo.WindowLayout layout, 57 @Nullable ActivityRecord activity, @Nullable ActivityRecord source, 58 @Nullable ActivityOptions options, @Nullable ActivityStarter.Request request, int phase, 59 LaunchParamsController.LaunchParams currentParams, 60 LaunchParamsController.LaunchParams outParams) { 61 62 initLogBuilder(task, activity); 63 int result = calculate(task, layout, activity, source, options, request, phase, 64 currentParams, outParams); 65 outputLog(); 66 return result; 67 } 68 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)69 private int calculate(@Nullable Task task, @Nullable ActivityInfo.WindowLayout layout, 70 @Nullable ActivityRecord activity, @Nullable ActivityRecord source, 71 @Nullable ActivityOptions options, @Nullable ActivityStarter.Request request, int phase, 72 LaunchParamsController.LaunchParams currentParams, 73 LaunchParamsController.LaunchParams outParams) { 74 75 if (task == null) { 76 appendLog("task null, skipping"); 77 return RESULT_SKIP; 78 } 79 if (phase != PHASE_BOUNDS) { 80 appendLog("not in bounds phase, skipping"); 81 return RESULT_SKIP; 82 } 83 if (!task.isActivityTypeStandard()) { 84 appendLog("not standard activity type, skipping"); 85 return RESULT_SKIP; 86 } 87 if (!currentParams.mBounds.isEmpty()) { 88 appendLog("currentParams has bounds set, not overriding"); 89 return RESULT_SKIP; 90 } 91 92 // Copy over any values 93 outParams.set(currentParams); 94 95 // Update width and height with default desktop mode values 96 float density = (float) task.getConfiguration().densityDpi / DENSITY_DEFAULT; 97 final int width = (int) (DESKTOP_MODE_DEFAULT_WIDTH_DP * density + 0.5f); 98 final int height = (int) (DESKTOP_MODE_DEFAULT_HEIGHT_DP * density + 0.5f); 99 outParams.mBounds.right = width; 100 outParams.mBounds.bottom = height; 101 102 // Center the task in window bounds 103 Rect windowBounds = task.getWindowConfiguration().getBounds(); 104 outParams.mBounds.offset(windowBounds.centerX() - outParams.mBounds.centerX(), 105 windowBounds.centerY() - outParams.mBounds.centerY()); 106 107 appendLog("setting desktop mode task bounds to %s", outParams.mBounds); 108 109 return RESULT_DONE; 110 } 111 initLogBuilder(Task task, ActivityRecord activity)112 private void initLogBuilder(Task task, ActivityRecord activity) { 113 if (DEBUG) { 114 mLogBuilder = new StringBuilder( 115 "DesktopModeLaunchParamsModifier: task=" + task + " activity=" + activity); 116 } 117 } 118 appendLog(String format, Object... args)119 private void appendLog(String format, Object... args) { 120 if (DEBUG) mLogBuilder.append(" ").append(String.format(format, args)); 121 } 122 outputLog()123 private void outputLog() { 124 if (DEBUG) Slog.d(TAG, mLogBuilder.toString()); 125 } 126 } 127