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