1 /* 2 * Copyright (C) 2021 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 android.annotation.IntDef; 20 import android.content.Context; 21 import android.graphics.Color; 22 23 import com.android.internal.R; 24 import com.android.internal.annotations.VisibleForTesting; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 29 /** Reads letterbox configs from resources and controls their overrides at runtime. */ 30 final class LetterboxConfiguration { 31 32 /** 33 * Override of aspect ratio for fixed orientation letterboxing that is set via ADB with 34 * set-fixed-orientation-letterbox-aspect-ratio or via {@link 35 * com.android.internal.R.dimen.config_fixedOrientationLetterboxAspectRatio} will be ignored 36 * if it is <= this value. 37 */ 38 static final float MIN_FIXED_ORIENTATION_LETTERBOX_ASPECT_RATIO = 1.0f; 39 40 /** Enum for Letterbox background type. */ 41 @Retention(RetentionPolicy.SOURCE) 42 @IntDef({LETTERBOX_BACKGROUND_SOLID_COLOR, LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND, 43 LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND_FLOATING, LETTERBOX_BACKGROUND_WALLPAPER}) 44 @interface LetterboxBackgroundType {}; 45 /** Solid background using color specified in R.color.config_letterboxBackgroundColor. */ 46 static final int LETTERBOX_BACKGROUND_SOLID_COLOR = 0; 47 48 /** Color specified in R.attr.colorBackground for the letterboxed application. */ 49 static final int LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND = 1; 50 51 /** Color specified in R.attr.colorBackgroundFloating for the letterboxed application. */ 52 static final int LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND_FLOATING = 2; 53 54 /** Using wallpaper as a background which can be blurred or dimmed with dark scrim. */ 55 static final int LETTERBOX_BACKGROUND_WALLPAPER = 3; 56 57 final Context mContext; 58 59 // Aspect ratio of letterbox for fixed orientation, values <= 60 // MIN_FIXED_ORIENTATION_LETTERBOX_ASPECT_RATIO will be ignored. 61 private float mFixedOrientationLetterboxAspectRatio; 62 63 // Corners radius for activities presented in the letterbox mode, values < 0 will be ignored. 64 private int mLetterboxActivityCornersRadius; 65 66 // Color for {@link #LETTERBOX_BACKGROUND_SOLID_COLOR} letterbox background type. 67 private Color mLetterboxBackgroundColor; 68 69 @LetterboxBackgroundType 70 private int mLetterboxBackgroundType; 71 72 // Blur radius for LETTERBOX_BACKGROUND_WALLPAPER option in mLetterboxBackgroundType. 73 // Values <= 0 are ignored and 0 is used instead. 74 private int mLetterboxBackgroundWallpaperBlurRadius; 75 76 // Alpha of a black scrim shown over wallpaper letterbox background when 77 // LETTERBOX_BACKGROUND_WALLPAPER option is selected for mLetterboxBackgroundType. 78 // Values < 0 or >= 1 are ignored and 0.0 (transparent) is used instead. 79 private float mLetterboxBackgroundWallpaperDarkScrimAlpha; 80 81 // Horizontal position of a center of the letterboxed app window. 0 corresponds to the left 82 // side of the screen and 1.0 to the right side. 83 private float mLetterboxHorizontalPositionMultiplier; 84 LetterboxConfiguration(Context context)85 LetterboxConfiguration(Context context) { 86 mContext = context; 87 mFixedOrientationLetterboxAspectRatio = context.getResources().getFloat( 88 R.dimen.config_fixedOrientationLetterboxAspectRatio); 89 mLetterboxActivityCornersRadius = context.getResources().getInteger( 90 R.integer.config_letterboxActivityCornersRadius); 91 mLetterboxBackgroundColor = Color.valueOf(context.getResources().getColor( 92 R.color.config_letterboxBackgroundColor)); 93 mLetterboxBackgroundType = readLetterboxBackgroundTypeFromConfig(context); 94 mLetterboxBackgroundWallpaperBlurRadius = context.getResources().getDimensionPixelSize( 95 R.dimen.config_letterboxBackgroundWallpaperBlurRadius); 96 mLetterboxBackgroundWallpaperDarkScrimAlpha = context.getResources().getFloat( 97 R.dimen.config_letterboxBackgroundWallaperDarkScrimAlpha); 98 mLetterboxHorizontalPositionMultiplier = context.getResources().getFloat( 99 R.dimen.config_letterboxHorizontalPositionMultiplier); 100 } 101 102 /** 103 * Overrides the aspect ratio of letterbox for fixed orientation. If given value is <= {@link 104 * #MIN_FIXED_ORIENTATION_LETTERBOX_ASPECT_RATIO}, both it and a value of {@link 105 * com.android.internal.R.dimen.config_fixedOrientationLetterboxAspectRatio} will be ignored and 106 * the framework implementation will be used to determine the aspect ratio. 107 */ 108 @VisibleForTesting setFixedOrientationLetterboxAspectRatio(float aspectRatio)109 void setFixedOrientationLetterboxAspectRatio(float aspectRatio) { 110 mFixedOrientationLetterboxAspectRatio = aspectRatio; 111 } 112 113 /** 114 * Gets the aspect ratio of letterbox for fixed orientation. 115 */ getFixedOrientationLetterboxAspectRatio()116 float getFixedOrientationLetterboxAspectRatio() { 117 return mFixedOrientationLetterboxAspectRatio; 118 } 119 120 /** 121 * Whether corners of letterboxed activities are rounded. 122 */ isLetterboxActivityCornersRounded()123 boolean isLetterboxActivityCornersRounded() { 124 return getLetterboxActivityCornersRadius() > 0; 125 } 126 127 /** 128 * Gets corners raidus for activities presented in the letterbox mode. 129 */ getLetterboxActivityCornersRadius()130 int getLetterboxActivityCornersRadius() { 131 return mLetterboxActivityCornersRadius; 132 } 133 134 /** 135 * Gets color of letterbox background which is used when {@link 136 * #getLetterboxBackgroundType()} is {@link #LETTERBOX_BACKGROUND_SOLID_COLOR} or as 137 * fallback for other backfround types. 138 */ getLetterboxBackgroundColor()139 Color getLetterboxBackgroundColor() { 140 return mLetterboxBackgroundColor; 141 } 142 143 /** 144 * Gets {@link LetterboxBackgroundType} specified in {@link 145 * com.android.internal.R.integer.config_letterboxBackgroundType} or over via ADB command. 146 */ 147 @LetterboxBackgroundType getLetterboxBackgroundType()148 int getLetterboxBackgroundType() { 149 return mLetterboxBackgroundType; 150 } 151 152 /** Returns a string representing the given {@link LetterboxBackgroundType}. */ letterboxBackgroundTypeToString( @etterboxBackgroundType int backgroundType)153 static String letterboxBackgroundTypeToString( 154 @LetterboxBackgroundType int backgroundType) { 155 switch (backgroundType) { 156 case LETTERBOX_BACKGROUND_SOLID_COLOR: 157 return "LETTERBOX_BACKGROUND_SOLID_COLOR"; 158 case LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND: 159 return "LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND"; 160 case LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND_FLOATING: 161 return "LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND_FLOATING"; 162 case LETTERBOX_BACKGROUND_WALLPAPER: 163 return "LETTERBOX_BACKGROUND_WALLPAPER"; 164 default: 165 return "unknown=" + backgroundType; 166 } 167 } 168 169 @LetterboxBackgroundType readLetterboxBackgroundTypeFromConfig(Context context)170 private static int readLetterboxBackgroundTypeFromConfig(Context context) { 171 int backgroundType = context.getResources().getInteger( 172 com.android.internal.R.integer.config_letterboxBackgroundType); 173 return backgroundType == LETTERBOX_BACKGROUND_SOLID_COLOR 174 || backgroundType == LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND 175 || backgroundType == LETTERBOX_BACKGROUND_APP_COLOR_BACKGROUND_FLOATING 176 || backgroundType == LETTERBOX_BACKGROUND_WALLPAPER 177 ? backgroundType : LETTERBOX_BACKGROUND_SOLID_COLOR; 178 } 179 180 /** 181 * Gets alpha of a black scrim shown over wallpaper letterbox background. 182 */ getLetterboxBackgroundWallpaperDarkScrimAlpha()183 float getLetterboxBackgroundWallpaperDarkScrimAlpha() { 184 return mLetterboxBackgroundWallpaperDarkScrimAlpha; 185 } 186 187 /** 188 * Gets blur raidus for {@link #LETTERBOX_BACKGROUND_WALLPAPER} option in {@link 189 * mLetterboxBackgroundType}. 190 */ getLetterboxBackgroundWallpaperBlurRadius()191 int getLetterboxBackgroundWallpaperBlurRadius() { 192 return mLetterboxBackgroundWallpaperBlurRadius; 193 } 194 195 /* 196 * Gets horizontal position of a center of the letterboxed app window specified 197 * in {@link com.android.internal.R.dimen.config_letterboxHorizontalPositionMultiplier} 198 * or via an ADB command. 0 corresponds to the left side of the screen and 1 to the 199 * right side. 200 * 201 * <p>This value can be outside of [0, 1] range so clients need to check and default to the 202 * central position (0.5). 203 */ getLetterboxHorizontalPositionMultiplier()204 float getLetterboxHorizontalPositionMultiplier() { 205 return mLetterboxHorizontalPositionMultiplier; 206 } 207 208 /** 209 * Overrides horizontal position of a center of the letterboxed app window. If given value < 0 210 * or > 1, then it and a value of {@link 211 * com.android.internal.R.dimen.config_letterboxHorizontalPositionMultiplier} are ignored and 212 * central position (0.5) is used. 213 */ 214 @VisibleForTesting setLetterboxHorizontalPositionMultiplier(float multiplier)215 void setLetterboxHorizontalPositionMultiplier(float multiplier) { 216 mLetterboxHorizontalPositionMultiplier = multiplier; 217 } 218 219 } 220