RecentsConfiguration.java revision 6074c5c15117bb17defb90bdb9fe247d0df47c70
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.app.ActivityManager;
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;
31import com.android.systemui.recents.misc.Console;
32import com.android.systemui.recents.misc.SystemServicesProxy;
33
34
35/** A static Recents configuration for the current context
36 * NOTE: We should not hold any references to a Context from a static instance */
37public class RecentsConfiguration {
38    static RecentsConfiguration sInstance;
39    static int sPrevConfigurationHashCode;
40
41    /** Animations */
42    public float animationPxMovementPerSecond;
43
44    /** Interpolators */
45    public Interpolator fastOutSlowInInterpolator;
46    public Interpolator fastOutLinearInInterpolator;
47    public Interpolator linearOutSlowInInterpolator;
48    public Interpolator quintOutInterpolator;
49
50    /** Filtering */
51    public int filteringCurrentViewsAnimDuration;
52    public int filteringNewViewsAnimDuration;
53
54    /** Insets */
55    public Rect systemInsets = new Rect();
56    public Rect displayRect = new Rect();
57
58    /** Layout */
59    boolean isLandscape;
60    boolean hasTransposedSearchBar;
61    boolean hasTransposedNavBar;
62
63    /** Loading */
64    public int maxNumTasksToLoad;
65
66    /** Search bar */
67    int searchBarAppWidgetId = -1;
68    public int searchBarSpaceHeightPx;
69
70    /** Task stack */
71    public int taskStackScrollDuration;
72    public int taskStackMaxDim;
73    public int taskStackTopPaddingPx;
74    public float taskStackWidthPaddingPct;
75    public float taskStackOverscrollPct;
76
77    /** Task view animation and styles */
78    public int taskViewEnterFromHomeDelay;
79    public int taskViewEnterFromHomeDuration;
80    public int taskViewEnterFromHomeStaggerDelay;
81    public int taskViewExitToHomeDuration;
82    public int taskViewRemoveAnimDuration;
83    public int taskViewRemoveAnimTranslationXPx;
84    public int taskViewTranslationZMinPx;
85    public int taskViewTranslationZMaxPx;
86    public int taskViewRoundedCornerRadiusPx;
87    public int taskViewHighlightPx;
88    public int taskViewAffiliateGroupEnterOffsetPx;
89    public float taskViewThumbnailAlpha;
90
91    /** Task bar colors */
92    public int taskBarViewDefaultBackgroundColor;
93    public int taskBarViewLightTextColor;
94    public int taskBarViewDarkTextColor;
95    public int taskBarViewHighlightColor;
96    public float taskBarViewAffiliationColorMinAlpha;
97
98    /** Task bar size & animations */
99    public int taskBarHeight;
100    public int taskBarEnterAnimDuration;
101    public int taskBarEnterAnimDelay;
102    public int taskBarExitAnimDuration;
103    public int taskBarDismissDozeDelaySeconds;
104
105    /** Lock to app */
106    public int taskViewLockToAppButtonHeight;
107    public int taskViewLockToAppShortAnimDuration;
108    public int taskViewLockToAppLongAnimDuration;
109
110    /** Nav bar scrim */
111    public int navBarScrimEnterDuration;
112
113    /** Launch states */
114    public boolean launchedWithAltTab;
115    public boolean launchedWithNoRecentTasks;
116    public boolean launchedFromAppWithThumbnail;
117    public boolean launchedFromAppWithScreenshot;
118    public boolean launchedFromHome;
119    public int launchedToTaskId;
120
121    /** Misc **/
122    public int altTabKeyDelay;
123
124    /** Dev options and global settings */
125    public boolean lockToAppEnabled;
126    public boolean developerOptionsEnabled;
127    public boolean debugModeEnabled;
128
129    /** Private constructor */
130    private RecentsConfiguration(Context context) {
131        // Properties that don't have to be reloaded with each configuration change can be loaded
132        // here.
133
134        // Interpolators
135        fastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
136                com.android.internal.R.interpolator.fast_out_slow_in);
137        fastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
138                com.android.internal.R.interpolator.fast_out_linear_in);
139        linearOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
140                com.android.internal.R.interpolator.linear_out_slow_in);
141        quintOutInterpolator = AnimationUtils.loadInterpolator(context,
142                com.android.internal.R.interpolator.decelerate_quint);
143    }
144
145    /** Updates the configuration to the current context */
146    public static RecentsConfiguration reinitialize(Context context, SystemServicesProxy ssp) {
147        if (sInstance == null) {
148            sInstance = new RecentsConfiguration(context);
149        }
150        int configHashCode = context.getResources().getConfiguration().hashCode();
151        if (sPrevConfigurationHashCode != configHashCode) {
152            sInstance.update(context);
153            sPrevConfigurationHashCode = configHashCode;
154        }
155        sInstance.updateOnReinitialize(context, ssp);
156        return sInstance;
157    }
158
159    /** Returns the current recents configuration */
160    public static RecentsConfiguration getInstance() {
161        return sInstance;
162    }
163
164    /** Updates the state, given the specified context */
165    void update(Context context) {
166        SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0);
167        Resources res = context.getResources();
168        DisplayMetrics dm = res.getDisplayMetrics();
169
170        // Debug mode
171        debugModeEnabled = settings.getBoolean(Constants.Values.App.Key_DebugModeEnabled, false);
172        if (debugModeEnabled) {
173            Console.Enabled = true;
174        }
175
176        // Layout
177        isLandscape = res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
178        hasTransposedSearchBar = res.getBoolean(R.bool.recents_has_transposed_search_bar);
179        hasTransposedNavBar = res.getBoolean(R.bool.recents_has_transposed_nav_bar);
180
181        // Insets
182        displayRect.set(0, 0, dm.widthPixels, dm.heightPixels);
183
184        // Animations
185        animationPxMovementPerSecond =
186                res.getDimensionPixelSize(R.dimen.recents_animation_movement_in_dps_per_second);
187
188        // Filtering
189        filteringCurrentViewsAnimDuration =
190                res.getInteger(R.integer.recents_filter_animate_current_views_duration);
191        filteringNewViewsAnimDuration =
192                res.getInteger(R.integer.recents_filter_animate_new_views_duration);
193
194        // Loading
195        maxNumTasksToLoad = ActivityManager.getMaxRecentTasksStatic();
196
197        // Search Bar
198        searchBarSpaceHeightPx = res.getDimensionPixelSize(R.dimen.recents_search_bar_space_height);
199        searchBarAppWidgetId = settings.getInt(Constants.Values.App.Key_SearchAppWidgetId, -1);
200
201        // Task stack
202        taskStackScrollDuration =
203                res.getInteger(R.integer.recents_animate_task_stack_scroll_duration);
204        TypedValue widthPaddingPctValue = new TypedValue();
205        res.getValue(R.dimen.recents_stack_width_padding_percentage, widthPaddingPctValue, true);
206        taskStackWidthPaddingPct = widthPaddingPctValue.getFloat();
207        TypedValue stackOverscrollPctValue = new TypedValue();
208        res.getValue(R.dimen.recents_stack_overscroll_percentage, stackOverscrollPctValue, true);
209        taskStackOverscrollPct = stackOverscrollPctValue.getFloat();
210        taskStackMaxDim = res.getInteger(R.integer.recents_max_task_stack_view_dim);
211        taskStackTopPaddingPx = res.getDimensionPixelSize(R.dimen.recents_stack_top_padding);
212
213        // Task view animation and styles
214        taskViewEnterFromHomeDelay =
215                res.getInteger(R.integer.recents_animate_task_enter_from_home_delay);
216        taskViewEnterFromHomeDuration =
217                res.getInteger(R.integer.recents_animate_task_enter_from_home_duration);
218        taskViewEnterFromHomeStaggerDelay =
219                res.getInteger(R.integer.recents_animate_task_enter_from_home_stagger_delay);
220        taskViewExitToHomeDuration =
221                res.getInteger(R.integer.recents_animate_task_exit_to_home_duration);
222        taskViewRemoveAnimDuration =
223                res.getInteger(R.integer.recents_animate_task_view_remove_duration);
224        taskViewRemoveAnimTranslationXPx =
225                res.getDimensionPixelSize(R.dimen.recents_task_view_remove_anim_translation_x);
226        taskViewRoundedCornerRadiusPx =
227                res.getDimensionPixelSize(R.dimen.recents_task_view_rounded_corners_radius);
228        taskViewHighlightPx = res.getDimensionPixelSize(R.dimen.recents_task_view_highlight);
229        taskViewTranslationZMinPx = res.getDimensionPixelSize(R.dimen.recents_task_view_z_min);
230        taskViewTranslationZMaxPx = res.getDimensionPixelSize(R.dimen.recents_task_view_z_max);
231        taskViewAffiliateGroupEnterOffsetPx =
232                res.getDimensionPixelSize(R.dimen.recents_task_view_affiliate_group_enter_offset);
233        TypedValue thumbnailAlphaValue = new TypedValue();
234        res.getValue(R.dimen.recents_task_view_thumbnail_alpha, thumbnailAlphaValue, true);
235        taskViewThumbnailAlpha = thumbnailAlphaValue.getFloat();
236
237        // Task bar colors
238        taskBarViewDefaultBackgroundColor =
239                res.getColor(R.color.recents_task_bar_default_background_color);
240        taskBarViewLightTextColor =
241                res.getColor(R.color.recents_task_bar_light_text_color);
242        taskBarViewDarkTextColor =
243                res.getColor(R.color.recents_task_bar_dark_text_color);
244        taskBarViewHighlightColor =
245                res.getColor(R.color.recents_task_bar_highlight_color);
246        TypedValue affMinAlphaPctValue = new TypedValue();
247        res.getValue(R.dimen.recents_task_affiliation_color_min_alpha_percentage, affMinAlphaPctValue, true);
248        taskBarViewAffiliationColorMinAlpha = affMinAlphaPctValue.getFloat();
249
250        // Task bar size & animations
251        taskBarHeight = res.getDimensionPixelSize(R.dimen.recents_task_bar_height);
252        taskBarEnterAnimDuration =
253                res.getInteger(R.integer.recents_animate_task_bar_enter_duration);
254        taskBarEnterAnimDelay =
255                res.getInteger(R.integer.recents_animate_task_bar_enter_delay);
256        taskBarExitAnimDuration =
257                res.getInteger(R.integer.recents_animate_task_bar_exit_duration);
258        taskBarDismissDozeDelaySeconds =
259                res.getInteger(R.integer.recents_task_bar_dismiss_delay_seconds);
260
261        // Lock to app
262        taskViewLockToAppButtonHeight =
263                res.getDimensionPixelSize(R.dimen.recents_task_view_lock_to_app_button_height);
264        taskViewLockToAppShortAnimDuration =
265                res.getInteger(R.integer.recents_animate_lock_to_app_button_short_duration);
266        taskViewLockToAppLongAnimDuration =
267                res.getInteger(R.integer.recents_animate_lock_to_app_button_long_duration);
268
269        // Nav bar scrim
270        navBarScrimEnterDuration =
271                res.getInteger(R.integer.recents_nav_bar_scrim_enter_duration);
272
273        // Misc
274        altTabKeyDelay = res.getInteger(R.integer.recents_alt_tab_key_delay);
275    }
276
277    /** Updates the system insets */
278    public void updateSystemInsets(Rect insets) {
279        systemInsets.set(insets);
280    }
281
282    /** Updates the search bar app widget */
283    public void updateSearchBarAppWidgetId(Context context, int appWidgetId) {
284        searchBarAppWidgetId = appWidgetId;
285        SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0);
286        settings.edit().putInt(Constants.Values.App.Key_SearchAppWidgetId,
287                appWidgetId).apply();
288    }
289
290    /** Updates the states that need to be re-read whenever we re-initialize. */
291    void updateOnReinitialize(Context context, SystemServicesProxy ssp) {
292        // Check if the developer options are enabled
293        developerOptionsEnabled = ssp.getGlobalSetting(context,
294                Settings.Global.DEVELOPMENT_SETTINGS_ENABLED) != 0;
295        lockToAppEnabled = ssp.getSystemSetting(context,
296                Settings.System.LOCK_TO_APP_ENABLED) != 0;
297    }
298
299    /** Called when the configuration has changed, and we want to reset any configuration specific
300     * members. */
301    public void updateOnConfigurationChange() {
302        launchedWithAltTab = false;
303        launchedWithNoRecentTasks = false;
304        launchedFromAppWithThumbnail = false;
305        launchedFromAppWithScreenshot = false;
306        launchedFromHome = false;
307        launchedToTaskId = -1;
308    }
309
310    /** Returns whether the search bar app widget exists. */
311    public boolean hasSearchBarAppWidget() {
312        return searchBarAppWidgetId >= 0;
313    }
314
315    /** Returns whether the status bar scrim should be animated when shown for the first time. */
316    public boolean shouldAnimateStatusBarScrim() {
317        return launchedFromHome;
318    }
319
320    /** Returns whether the status bar scrim should be visible. */
321    public boolean hasStatusBarScrim() {
322        return !launchedWithNoRecentTasks;
323    }
324
325    /** Returns whether the nav bar scrim should be animated when shown for the first time. */
326    public boolean shouldAnimateNavBarScrim() {
327        return true;
328    }
329
330    /** Returns whether the nav bar scrim should be visible. */
331    public boolean hasNavBarScrim() {
332        // Only show the scrim if we have recent tasks, and if the nav bar is not transposed
333        return !launchedWithNoRecentTasks && (!hasTransposedNavBar || !isLandscape);
334    }
335
336    /** Returns whether the current layout is horizontal. */
337    public boolean hasHorizontalLayout() {
338        return isLandscape && hasTransposedSearchBar;
339    }
340
341    /**
342     * Returns the task stack bounds in the current orientation. These bounds do not account for
343     * the system insets.
344     */
345    public void getTaskStackBounds(int windowWidth, int windowHeight, int topInset, int rightInset,
346                                   Rect taskStackBounds) {
347        Rect searchBarBounds = new Rect();
348        getSearchBarBounds(windowWidth, windowHeight, topInset, searchBarBounds);
349        if (isLandscape && hasTransposedSearchBar) {
350            // In landscape, the search bar appears on the left, but we overlay it on top
351            taskStackBounds.set(0, topInset, windowWidth - rightInset, windowHeight);
352        } else {
353            // In portrait, the search bar appears on the top (which already has the inset)
354            taskStackBounds.set(0, searchBarBounds.bottom, windowWidth, windowHeight);
355        }
356    }
357
358    /**
359     * Returns the search bar bounds in the current orientation.  These bounds do not account for
360     * the system insets.
361     */
362    public void getSearchBarBounds(int windowWidth, int windowHeight, int topInset,
363                                   Rect searchBarSpaceBounds) {
364        // Return empty rects if search is not enabled
365        int searchBarSize = searchBarSpaceHeightPx;
366        if (!Constants.DebugFlags.App.EnableSearchLayout || !hasSearchBarAppWidget()) {
367            searchBarSize = 0;
368        }
369
370        if (isLandscape && hasTransposedSearchBar) {
371            // In landscape, the search bar appears on the left
372            searchBarSpaceBounds.set(0, topInset, searchBarSize, windowHeight);
373        } else {
374            // In portrait, the search bar appears on the top
375            searchBarSpaceBounds.set(0, topInset, windowWidth, topInset + searchBarSize);
376        }
377    }
378}
379