RecentsConfiguration.java revision 7aceb9a07fd3883c8f301ba93726d55494b0446e
1/* 2 * Copyright (C) 2014 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 17package com.android.systemui.recents; 18 19import android.content.ContentResolver; 20import android.content.Context; 21import android.content.SharedPreferences; 22import android.content.res.Configuration; 23import android.content.res.Resources; 24import android.graphics.Rect; 25import android.provider.Settings; 26import android.util.DisplayMetrics; 27import android.util.TypedValue; 28import android.view.animation.AnimationUtils; 29import android.view.animation.Interpolator; 30import com.android.systemui.R; 31 32 33/** A static Recents configuration for the current context 34 * NOTE: We should not hold any references to a Context from a static instance */ 35public class RecentsConfiguration { 36 static RecentsConfiguration sInstance; 37 38 DisplayMetrics mDisplayMetrics; 39 40 /** Animations */ 41 public float animationPxMovementPerSecond; 42 43 /** Interpolators */ 44 public Interpolator fastOutSlowInInterpolator; 45 public Interpolator fastOutLinearInInterpolator; 46 public Interpolator linearOutSlowInInterpolator; 47 public Interpolator quintOutInterpolator; 48 49 /** Filtering */ 50 public int filteringCurrentViewsAnimDuration; 51 public int filteringNewViewsAnimDuration; 52 53 /** Insets */ 54 public Rect systemInsets = new Rect(); 55 public Rect displayRect = new Rect(); 56 57 /** Layout */ 58 boolean isLandscape; 59 boolean transposeRecentsLayoutWithOrientation; 60 61 /** Search bar */ 62 int searchBarAppWidgetId = -1; 63 public int searchBarSpaceHeightPx; 64 65 /** Task stack */ 66 public int taskStackMaxDim; 67 public int taskStackTopPaddingPx; 68 public float taskStackWidthPaddingPct; 69 70 /** Task view animation and styles */ 71 public int taskViewEnterFromHomeDuration; 72 public int taskViewEnterFromHomeDelay; 73 public int taskViewExitToHomeDuration; 74 public int taskViewRemoveAnimDuration; 75 public int taskViewRemoveAnimTranslationXPx; 76 public int taskViewTranslationZMinPx; 77 public int taskViewTranslationZIncrementPx; 78 public int taskViewShadowOutlineBottomInsetPx; 79 public int taskViewRoundedCornerRadiusPx; 80 public int taskViewHighlightPx; 81 82 /** Task bar colors */ 83 public int taskBarViewDefaultBackgroundColor; 84 public int taskBarViewLightTextColor; 85 public int taskBarViewDarkTextColor; 86 public int taskBarViewHighlightColor; 87 88 /** Task bar animations */ 89 public int taskBarEnterAnimDuration; 90 public int taskBarEnterAnimDelay; 91 public int taskBarExitAnimDuration; 92 public int taskBarDismissDozeDelaySeconds; 93 94 /** Nav bar scrim */ 95 public int navBarScrimEnterDuration; 96 97 /** Launch states */ 98 public boolean launchedWithAltTab; 99 public boolean launchedWithNoRecentTasks; 100 public boolean launchedFromAppWithThumbnail; 101 public boolean launchedFromAppWithScreenshot; 102 public boolean launchedFromHome; 103 104 /** Dev options */ 105 public boolean developerOptionsEnabled; 106 public boolean debugModeEnabled; 107 108 /** Private constructor */ 109 private RecentsConfiguration(Context context) { 110 // Properties that don't have to be reloaded with each configuration change can be loaded 111 // here. 112 113 // Interpolators 114 fastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context, 115 com.android.internal.R.interpolator.fast_out_slow_in); 116 fastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context, 117 com.android.internal.R.interpolator.fast_out_linear_in); 118 linearOutSlowInInterpolator = AnimationUtils.loadInterpolator(context, 119 com.android.internal.R.interpolator.linear_out_slow_in); 120 quintOutInterpolator = AnimationUtils.loadInterpolator(context, 121 com.android.internal.R.interpolator.decelerate_quint); 122 123 // Check if the developer options are enabled 124 ContentResolver cr = context.getContentResolver(); 125 developerOptionsEnabled = Settings.Global.getInt(cr, 126 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; 127 } 128 129 /** Updates the configuration to the current context */ 130 public static RecentsConfiguration reinitialize(Context context) { 131 if (sInstance == null) { 132 sInstance = new RecentsConfiguration(context); 133 } 134 sInstance.update(context); 135 return sInstance; 136 } 137 138 /** Returns the current recents configuration */ 139 public static RecentsConfiguration getInstance() { 140 return sInstance; 141 } 142 143 /** Updates the state, given the specified context */ 144 void update(Context context) { 145 SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0); 146 Resources res = context.getResources(); 147 DisplayMetrics dm = res.getDisplayMetrics(); 148 mDisplayMetrics = dm; 149 150 // Debug mode 151 debugModeEnabled = settings.getBoolean(Constants.Values.App.Key_DebugModeEnabled, false); 152 if (debugModeEnabled) { 153 Console.Enabled = true; 154 } 155 156 // Animations 157 animationPxMovementPerSecond = 158 res.getDimensionPixelSize(R.dimen.recents_animation_movement_in_dps_per_second); 159 160 // Filtering 161 filteringCurrentViewsAnimDuration = 162 res.getInteger(R.integer.recents_filter_animate_current_views_duration); 163 filteringNewViewsAnimDuration = 164 res.getInteger(R.integer.recents_filter_animate_new_views_duration); 165 166 // Insets 167 displayRect.set(0, 0, dm.widthPixels, dm.heightPixels); 168 169 // Layout 170 isLandscape = res.getConfiguration().orientation == 171 Configuration.ORIENTATION_LANDSCAPE; 172 transposeRecentsLayoutWithOrientation = 173 res.getBoolean(R.bool.recents_transpose_layout_with_orientation); 174 175 // Search bar 176 searchBarSpaceHeightPx = res.getDimensionPixelSize(R.dimen.recents_search_bar_space_height); 177 178 // Update the search widget id 179 searchBarAppWidgetId = settings.getInt(Constants.Values.App.Key_SearchAppWidgetId, -1); 180 181 // Task stack 182 TypedValue widthPaddingPctValue = new TypedValue(); 183 res.getValue(R.dimen.recents_stack_width_padding_percentage, widthPaddingPctValue, true); 184 taskStackWidthPaddingPct = widthPaddingPctValue.getFloat(); 185 taskStackMaxDim = res.getInteger(R.integer.recents_max_task_stack_view_dim); 186 taskStackTopPaddingPx = res.getDimensionPixelSize(R.dimen.recents_stack_top_padding); 187 188 // Task view animation and styles 189 taskViewEnterFromHomeDuration = 190 res.getInteger(R.integer.recents_animate_task_enter_from_home_duration); 191 taskViewEnterFromHomeDelay = 192 res.getInteger(R.integer.recents_animate_task_enter_from_home_delay); 193 taskViewExitToHomeDuration = 194 res.getInteger(R.integer.recents_animate_task_exit_to_home_duration); 195 taskViewRemoveAnimDuration = 196 res.getInteger(R.integer.recents_animate_task_view_remove_duration); 197 taskViewRemoveAnimTranslationXPx = 198 res.getDimensionPixelSize(R.dimen.recents_task_view_remove_anim_translation_x); 199 taskViewRoundedCornerRadiusPx = 200 res.getDimensionPixelSize(R.dimen.recents_task_view_rounded_corners_radius); 201 taskViewHighlightPx = res.getDimensionPixelSize(R.dimen.recents_task_view_highlight); 202 taskViewTranslationZMinPx = res.getDimensionPixelSize(R.dimen.recents_task_view_z_min); 203 taskViewTranslationZIncrementPx = 204 res.getDimensionPixelSize(R.dimen.recents_task_view_z_increment); 205 taskViewShadowOutlineBottomInsetPx = 206 res.getDimensionPixelSize(R.dimen.recents_task_view_shadow_outline_bottom_inset); 207 208 // Task bar colors 209 taskBarViewDefaultBackgroundColor = 210 res.getColor(R.color.recents_task_bar_default_background_color); 211 taskBarViewLightTextColor = 212 res.getColor(R.color.recents_task_bar_light_text_color); 213 taskBarViewDarkTextColor = 214 res.getColor(R.color.recents_task_bar_dark_text_color); 215 taskBarViewHighlightColor = 216 res.getColor(R.color.recents_task_bar_highlight_color); 217 218 // Task bar animations 219 taskBarEnterAnimDuration = 220 res.getInteger(R.integer.recents_animate_task_bar_enter_duration); 221 taskBarEnterAnimDelay = 222 res.getInteger(R.integer.recents_animate_task_bar_enter_delay); 223 taskBarExitAnimDuration = 224 res.getInteger(R.integer.recents_animate_task_bar_exit_duration); 225 taskBarDismissDozeDelaySeconds = 226 res.getInteger(R.integer.recents_task_bar_dismiss_delay_seconds); 227 228 // Nav bar scrim 229 navBarScrimEnterDuration = 230 res.getInteger(R.integer.recents_nav_bar_scrim_enter_duration); 231 232 if (Console.Enabled) { 233 Console.log(Constants.Log.UI.MeasureAndLayout, 234 "[RecentsConfiguration|orientation]", isLandscape ? "Landscape" : "Portrait", 235 Console.AnsiGreen); 236 } 237 } 238 239 /** Updates the system insets */ 240 public void updateSystemInsets(Rect insets) { 241 systemInsets.set(insets); 242 } 243 244 /** Updates the search bar app widget */ 245 public void updateSearchBarAppWidgetId(Context context, int appWidgetId) { 246 searchBarAppWidgetId = appWidgetId; 247 SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0); 248 settings.edit().putInt(Constants.Values.App.Key_SearchAppWidgetId, 249 appWidgetId).apply(); 250 } 251 252 /** Called when the configuration has changed, and we want to reset any configuration specific 253 * members. */ 254 public void updateOnConfigurationChange() { 255 launchedWithAltTab = false; 256 launchedWithNoRecentTasks = false; 257 launchedFromAppWithThumbnail = false; 258 launchedFromAppWithScreenshot = false; 259 launchedFromHome = false; 260 } 261 262 /** Returns whether the search bar app widget exists. */ 263 public boolean hasSearchBarAppWidget() { 264 return searchBarAppWidgetId >= 0; 265 } 266 267 /** Returns whether the status bar scrim should be animated when shown for the first time. */ 268 public boolean shouldAnimateStatusBarScrim() { 269 return launchedFromHome; 270 } 271 272 /** Returns whether the status bar scrim should be visible. */ 273 public boolean hasStatusBarScrim() { 274 return !launchedWithNoRecentTasks; 275 } 276 277 /** Returns whether the nav bar scrim should be animated when shown for the first time. */ 278 public boolean shouldAnimateNavBarScrim() { 279 return true; 280 } 281 282 /** Returns whether the nav bar scrim should be visible. */ 283 public boolean hasNavBarScrim() { 284 // Only show the scrim if we have recent tasks, and if the nav bar is not transposed 285 return !launchedWithNoRecentTasks && 286 (!transposeRecentsLayoutWithOrientation || !isLandscape); 287 } 288 289 /** 290 * Returns the task stack bounds in the current orientation. These bounds do not account for 291 * the system insets. 292 */ 293 public void getTaskStackBounds(int width, int height, Rect taskStackBounds) { 294 if (hasSearchBarAppWidget()) { 295 Rect searchBarBounds = new Rect(); 296 getSearchBarBounds(width, height, searchBarBounds); 297 if (isLandscape && transposeRecentsLayoutWithOrientation) { 298 // In landscape, the search bar appears on the left, so shift the task rect right 299 taskStackBounds.set(searchBarBounds.width(), 0, width, height); 300 } else { 301 // In portrait, the search bar appears on the top, so shift the task rect below 302 taskStackBounds.set(0, searchBarBounds.height(), width, height); 303 } 304 } else { 305 taskStackBounds.set(0, 0, width, height); 306 } 307 } 308 309 /** 310 * Returns the search bar bounds in the current orientation. These bounds do not account for 311 * the system insets. 312 */ 313 public void getSearchBarBounds(int width, int height, Rect searchBarSpaceBounds) { 314 // Return empty rects if search is not enabled 315 if (!Constants.DebugFlags.App.EnableSearchLayout) { 316 searchBarSpaceBounds.set(0, 0, 0, 0); 317 return; 318 } 319 320 if (isLandscape && transposeRecentsLayoutWithOrientation) { 321 // In landscape, the search bar appears on the left 322 searchBarSpaceBounds.set(0, 0, searchBarSpaceHeightPx, height); 323 } else { 324 // In portrait, the search bar appears on the top 325 searchBarSpaceBounds.set(0, 0, width, searchBarSpaceHeightPx); 326 } 327 } 328} 329