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