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