RecentsConfiguration.java revision ad6f276670443ac8f9b73497dae185bb005c3a76
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 filteringCurrentViewsMinAnimDuration;
51    public int filteringNewViewsMinAnimDuration;
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 taskBarViewDefaultTextColor;
85    public int taskBarViewLightTextColor;
86    public int taskBarViewDarkTextColor;
87    public int taskBarViewHighlightColor;
88
89    /** Task bar animations */
90    public int taskBarEnterAnimDuration;
91    public int taskBarEnterAnimDelay;
92    public int taskBarExitAnimDuration;
93    public int taskBarDismissDozeDelaySeconds;
94
95    /** Nav bar scrim */
96    public int navBarScrimEnterDuration;
97
98    /** Launch states */
99    public boolean launchedWithAltTab;
100    public boolean launchedFromAppWithThumbnail;
101    public boolean launchedFromAppWithScreenshot;
102    public boolean launchedFromHome;
103
104    /** Dev options */
105    public boolean developerOptionsEnabled;
106
107    /** Private constructor */
108    private RecentsConfiguration() {}
109
110    /** Updates the configuration to the current context */
111    public static RecentsConfiguration reinitialize(Context context) {
112        if (sInstance == null) {
113            sInstance = new RecentsConfiguration();
114        }
115        sInstance.update(context);
116        return sInstance;
117    }
118
119    /** Returns the current recents configuration */
120    public static RecentsConfiguration getInstance() {
121        return sInstance;
122    }
123
124    /** Updates the state, given the specified context */
125    void update(Context context) {
126        Resources res = context.getResources();
127        DisplayMetrics dm = res.getDisplayMetrics();
128        mDisplayMetrics = dm;
129
130        // Animations
131        animationPxMovementPerSecond =
132                res.getDimensionPixelSize(R.dimen.recents_animation_movement_in_dps_per_second);
133
134        // Interpolators
135        fastOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
136                com.android.internal.R.interpolator.fast_out_slow_in);
137        fastOutLinearInInterpolator = AnimationUtils.loadInterpolator(context,
138                com.android.internal.R.interpolator.fast_out_linear_in);
139        linearOutSlowInInterpolator = AnimationUtils.loadInterpolator(context,
140                com.android.internal.R.interpolator.linear_out_slow_in);
141        quintOutInterpolator = AnimationUtils.loadInterpolator(context,
142                com.android.internal.R.interpolator.decelerate_quint);
143
144        // Filtering
145        filteringCurrentViewsMinAnimDuration =
146                res.getInteger(R.integer.recents_filter_animate_current_views_min_duration);
147        filteringNewViewsMinAnimDuration =
148                res.getInteger(R.integer.recents_filter_animate_new_views_min_duration);
149
150        // Insets
151        displayRect.set(0, 0, dm.widthPixels, dm.heightPixels);
152
153        // Layout
154        isLandscape = res.getConfiguration().orientation ==
155                Configuration.ORIENTATION_LANDSCAPE;
156        transposeRecentsLayoutWithOrientation =
157                res.getBoolean(R.bool.recents_transpose_layout_with_orientation);
158
159        // Search bar
160        searchBarSpaceHeightPx = res.getDimensionPixelSize(R.dimen.recents_search_bar_space_height);
161
162        // Update the search widget id
163        SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0);
164        searchBarAppWidgetId = settings.getInt(Constants.Values.App.Key_SearchAppWidgetId, -1);
165
166        // Task stack
167        TypedValue widthPaddingPctValue = new TypedValue();
168        res.getValue(R.dimen.recents_stack_width_padding_percentage, widthPaddingPctValue, true);
169        taskStackWidthPaddingPct = widthPaddingPctValue.getFloat();
170        taskStackMaxDim = res.getInteger(R.integer.recents_max_task_stack_view_dim);
171        taskStackTopPaddingPx = res.getDimensionPixelSize(R.dimen.recents_stack_top_padding);
172
173        // Task view animation and styles
174        taskViewEnterFromHomeDuration =
175                res.getInteger(R.integer.recents_animate_task_enter_from_home_duration);
176        taskViewEnterFromHomeDelay =
177                res.getInteger(R.integer.recents_animate_task_enter_from_home_delay);
178        taskViewExitToHomeDuration =
179                res.getInteger(R.integer.recents_animate_task_exit_to_home_duration);
180        taskViewRemoveAnimDuration =
181                res.getInteger(R.integer.recents_animate_task_view_remove_duration);
182        taskViewRemoveAnimTranslationXPx =
183                res.getDimensionPixelSize(R.dimen.recents_task_view_remove_anim_translation_x);
184        taskViewRoundedCornerRadiusPx =
185                res.getDimensionPixelSize(R.dimen.recents_task_view_rounded_corners_radius);
186        taskViewHighlightPx = res.getDimensionPixelSize(R.dimen.recents_task_view_highlight);
187        taskViewTranslationZMinPx = res.getDimensionPixelSize(R.dimen.recents_task_view_z_min);
188        taskViewTranslationZIncrementPx =
189                res.getDimensionPixelSize(R.dimen.recents_task_view_z_increment);
190        taskViewShadowOutlineBottomInsetPx =
191                res.getDimensionPixelSize(R.dimen.recents_task_view_shadow_outline_bottom_inset);
192
193        // Task bar colors
194        taskBarViewDefaultBackgroundColor =
195                res.getColor(R.color.recents_task_bar_default_background_color);
196        taskBarViewDefaultTextColor =
197                res.getColor(R.color.recents_task_bar_default_text_color);
198        taskBarViewLightTextColor =
199                res.getColor(R.color.recents_task_bar_light_text_color);
200        taskBarViewDarkTextColor =
201                res.getColor(R.color.recents_task_bar_dark_text_color);
202        taskBarViewHighlightColor =
203                res.getColor(R.color.recents_task_bar_highlight_color);
204
205        // Task bar animations
206        taskBarEnterAnimDuration =
207                res.getInteger(R.integer.recents_animate_task_bar_enter_duration);
208        taskBarEnterAnimDelay =
209                res.getInteger(R.integer.recents_animate_task_bar_enter_delay);
210        taskBarExitAnimDuration =
211                res.getInteger(R.integer.recents_animate_task_bar_exit_duration);
212        taskBarDismissDozeDelaySeconds =
213                res.getInteger(R.integer.recents_task_bar_dismiss_delay_seconds);
214
215        // Nav bar scrim
216        navBarScrimEnterDuration =
217                res.getInteger(R.integer.recents_nav_bar_scrim_enter_duration);
218
219        // Check if the developer options are enabled
220        ContentResolver cr = context.getContentResolver();
221        developerOptionsEnabled = Settings.Global.getInt(cr,
222                Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
223
224        if (Console.Enabled) {
225            Console.log(Constants.Log.UI.MeasureAndLayout,
226                    "[RecentsConfiguration|orientation]", isLandscape ? "Landscape" : "Portrait",
227                    Console.AnsiGreen);
228        }
229    }
230
231    /** Updates the system insets */
232    public void updateSystemInsets(Rect insets) {
233        systemInsets.set(insets);
234    }
235
236    /** Updates the search bar app widget */
237    public void updateSearchBarAppWidgetId(Context context, int appWidgetId) {
238        searchBarAppWidgetId = appWidgetId;
239        SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0);
240        settings.edit().putInt(Constants.Values.App.Key_SearchAppWidgetId,
241                appWidgetId).apply();
242    }
243
244    /** Called when the configuration has changed, and we want to reset any configuration specific
245     * members. */
246    public void updateOnConfigurationChange() {
247        launchedWithAltTab = false;
248        launchedFromAppWithThumbnail = false;
249        launchedFromAppWithScreenshot = false;
250        launchedFromHome = false;
251    }
252
253    /** Returns whether the search bar app widget exists. */
254    public boolean hasSearchBarAppWidget() {
255        return searchBarAppWidgetId >= 0;
256    }
257
258    /** Returns whether the nav bar scrim should be visible. */
259    public boolean hasNavBarScrim() {
260        return !transposeRecentsLayoutWithOrientation || !isLandscape;
261    }
262
263    /**
264     * Returns the task stack bounds in the current orientation. These bounds do not account for
265     * the system insets.
266     */
267    public void getTaskStackBounds(int width, int height, Rect taskStackBounds) {
268        if (hasSearchBarAppWidget()) {
269            Rect searchBarBounds = new Rect();
270            getSearchBarBounds(width, height, searchBarBounds);
271            if (isLandscape && transposeRecentsLayoutWithOrientation) {
272                // In landscape, the search bar appears on the left, so shift the task rect right
273                taskStackBounds.set(searchBarBounds.width(), 0, width, height);
274            } else {
275                // In portrait, the search bar appears on the top, so shift the task rect below
276                taskStackBounds.set(0, searchBarBounds.height(), width, height);
277            }
278        } else {
279            taskStackBounds.set(0, 0, width, height);
280        }
281    }
282
283    /**
284     * Returns the search bar bounds in the current orientation.  These bounds do not account for
285     * the system insets.
286     */
287    public void getSearchBarBounds(int width, int height, Rect searchBarSpaceBounds) {
288        // Return empty rects if search is not enabled
289        if (!Constants.DebugFlags.App.EnableSearchLayout) {
290            searchBarSpaceBounds.set(0, 0, 0, 0);
291            return;
292        }
293
294        if (isLandscape && transposeRecentsLayoutWithOrientation) {
295            // In landscape, the search bar appears on the left
296            searchBarSpaceBounds.set(0, 0, searchBarSpaceHeightPx, height);
297        } else {
298            // In portrait, the search bar appears on the top
299            searchBarSpaceBounds.set(0, 0, width, searchBarSpaceHeightPx);
300        }
301    }
302}
303