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