RecentsConfiguration.java revision b01ed681fe97ff5e98471c120ff9581a78db13c5
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 public Rect systemInsets = new Rect(); 41 public Rect displayRect = new Rect(); 42 43 boolean isLandscape; 44 boolean transposeSearchLayoutWithOrientation; 45 int searchBarAppWidgetId = -1; 46 47 public float animationPxMovementPerSecond; 48 49 public Interpolator fastOutSlowInInterpolator; 50 public Interpolator fastOutLinearInInterpolator; 51 public Interpolator linearOutSlowInInterpolator; 52 53 public int filteringCurrentViewsMinAnimDuration; 54 public int filteringNewViewsMinAnimDuration; 55 public int taskBarEnterAnimDuration; 56 public int taskBarExitAnimDuration; 57 public int taskStackScrollDismissInfoPaneDistance; 58 public int taskStackMaxDim; 59 public int taskViewInfoPaneAnimDuration; 60 public int taskViewRemoveAnimDuration; 61 public int taskViewRemoveAnimTranslationXPx; 62 public int taskViewTranslationZMinPx; 63 public int taskViewTranslationZIncrementPx; 64 public int taskViewRoundedCornerRadiusPx; 65 public int searchBarSpaceHeightPx; 66 67 public int taskBarViewDefaultBackgroundColor; 68 public int taskBarViewDefaultTextColor; 69 public int taskBarViewLightTextColor; 70 public int taskBarViewDarkTextColor; 71 72 public boolean launchedFromAltTab; 73 public boolean launchedWithThumbnailAnimation; 74 75 public boolean developerOptionsEnabled; 76 77 /** Private constructor */ 78 private RecentsConfiguration() {} 79 80 /** Updates the configuration to the current context */ 81 public static RecentsConfiguration reinitialize(Context context) { 82 if (sInstance == null) { 83 sInstance = new RecentsConfiguration(); 84 } 85 sInstance.update(context); 86 return sInstance; 87 } 88 89 /** Returns the current recents configuration */ 90 public static RecentsConfiguration getInstance() { 91 return sInstance; 92 } 93 94 /** Updates the state, given the specified context */ 95 void update(Context context) { 96 Resources res = context.getResources(); 97 DisplayMetrics dm = res.getDisplayMetrics(); 98 mDisplayMetrics = dm; 99 100 isLandscape = res.getConfiguration().orientation == 101 Configuration.ORIENTATION_LANDSCAPE; 102 transposeSearchLayoutWithOrientation = 103 res.getBoolean(R.bool.recents_transpose_search_layout_with_orientation); 104 if (Console.Enabled) { 105 Console.log(Constants.Log.UI.MeasureAndLayout, 106 "[RecentsConfiguration|orientation]", isLandscape ? "Landscape" : "Portrait", 107 Console.AnsiGreen); 108 } 109 110 displayRect.set(0, 0, dm.widthPixels, dm.heightPixels); 111 animationPxMovementPerSecond = 112 res.getDimensionPixelSize(R.dimen.recents_animation_movement_in_dps_per_second); 113 filteringCurrentViewsMinAnimDuration = 114 res.getInteger(R.integer.recents_filter_animate_current_views_min_duration); 115 filteringNewViewsMinAnimDuration = 116 res.getInteger(R.integer.recents_filter_animate_new_views_min_duration); 117 taskBarEnterAnimDuration = 118 res.getInteger(R.integer.recents_animate_task_bar_enter_duration); 119 taskBarExitAnimDuration = 120 res.getInteger(R.integer.recents_animate_task_bar_exit_duration); 121 taskStackScrollDismissInfoPaneDistance = res.getDimensionPixelSize( 122 R.dimen.recents_task_stack_scroll_dismiss_info_pane_distance); 123 taskStackMaxDim = res.getInteger(R.integer.recents_max_task_stack_view_dim); 124 taskViewInfoPaneAnimDuration = 125 res.getInteger(R.integer.recents_animate_task_view_info_pane_duration); 126 taskViewRemoveAnimDuration = 127 res.getInteger(R.integer.recents_animate_task_view_remove_duration); 128 taskViewRemoveAnimTranslationXPx = 129 res.getDimensionPixelSize(R.dimen.recents_task_view_remove_anim_translation_x); 130 taskViewRoundedCornerRadiusPx = 131 res.getDimensionPixelSize(R.dimen.recents_task_view_rounded_corners_radius); 132 taskViewTranslationZMinPx = res.getDimensionPixelSize(R.dimen.recents_task_view_z_min); 133 taskViewTranslationZIncrementPx = 134 res.getDimensionPixelSize(R.dimen.recents_task_view_z_increment); 135 searchBarSpaceHeightPx = res.getDimensionPixelSize(R.dimen.recents_search_bar_space_height); 136 137 taskBarViewDefaultBackgroundColor = 138 res.getColor(R.color.recents_task_bar_default_background_color); 139 taskBarViewDefaultTextColor = 140 res.getColor(R.color.recents_task_bar_default_text_color); 141 taskBarViewLightTextColor = 142 res.getColor(R.color.recents_task_bar_light_text_color); 143 taskBarViewDarkTextColor = 144 res.getColor(R.color.recents_task_bar_dark_text_color); 145 146 fastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context, 147 com.android.internal.R.interpolator.fast_out_slow_in); 148 fastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context, 149 com.android.internal.R.interpolator.fast_out_linear_in); 150 linearOutSlowInInterpolator = AnimationUtils.loadInterpolator(context, 151 com.android.internal.R.interpolator.linear_out_slow_in); 152 153 // Check if the developer options are enabled 154 ContentResolver cr = context.getContentResolver(); 155 developerOptionsEnabled = Settings.Global.getInt(cr, 156 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; 157 158 // Update the search widget id 159 SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0); 160 searchBarAppWidgetId = settings.getInt(Constants.Values.App.Key_SearchAppWidgetId, -1); 161 } 162 163 /** Updates the system insets */ 164 public void updateSystemInsets(Rect insets) { 165 systemInsets.set(insets); 166 } 167 168 /** Updates the search bar app widget */ 169 public void updateSearchBarAppWidgetId(Context context, int appWidgetId) { 170 searchBarAppWidgetId = appWidgetId; 171 SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0); 172 settings.edit().putInt(Constants.Values.App.Key_SearchAppWidgetId, 173 appWidgetId).apply(); 174 } 175 176 /** Called when the configuration has changed, and we want to reset any configuration specific 177 * members. */ 178 public void updateOnConfigurationChange() { 179 launchedFromAltTab = false; 180 launchedWithThumbnailAnimation = false; 181 } 182 183 /** Returns whether the search bar app widget exists */ 184 public boolean hasSearchBarAppWidget() { 185 return searchBarAppWidgetId >= 0; 186 } 187 188 /** 189 * Returns the task stack bounds in the current orientation. These bounds do not account for 190 * the system insets. 191 */ 192 public void getTaskStackBounds(int width, int height, Rect taskStackBounds) { 193 if (hasSearchBarAppWidget()) { 194 Rect searchBarBounds = new Rect(); 195 getSearchBarBounds(width, height, searchBarBounds); 196 if (isLandscape && transposeSearchLayoutWithOrientation) { 197 // In landscape, the search bar appears on the left, so shift the task rect right 198 taskStackBounds.set(searchBarBounds.width(), 0, width, height); 199 } else { 200 // In portrait, the search bar appears on the top, so shift the task rect below 201 taskStackBounds.set(0, searchBarBounds.height(), width, height); 202 } 203 } else { 204 taskStackBounds.set(0, 0, width, height); 205 } 206 } 207 208 /** 209 * Returns the search bar bounds in the current orientation. These bounds do not account for 210 * the system insets. 211 */ 212 public void getSearchBarBounds(int width, int height, Rect searchBarSpaceBounds) { 213 // Return empty rects if search is not enabled 214 if (!Constants.DebugFlags.App.EnableSearchLayout) { 215 searchBarSpaceBounds.set(0, 0, 0, 0); 216 return; 217 } 218 219 if (isLandscape && transposeSearchLayoutWithOrientation) { 220 // In landscape, the search bar appears on the left 221 searchBarSpaceBounds.set(0, 0, searchBarSpaceHeightPx, height); 222 } else { 223 // In portrait, the search bar appears on the top 224 searchBarSpaceBounds.set(0, 0, width, searchBarSpaceHeightPx); 225 } 226 } 227 228 /** Converts from DPs to PXs */ 229 public int pxFromDp(float size) { 230 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 231 size, mDisplayMetrics)); 232 } 233 /** Converts from SPs to PXs */ 234 public int pxFromSp(float size) { 235 return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 236 size, mDisplayMetrics)); 237 } 238} 239