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