RecentsConfiguration.java revision 1e8d71b605b4872e93200706a80a88a3ff25498c
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.util.DisplayMetrics;
25import android.util.TypedValue;
26import android.view.animation.AnimationUtils;
27import android.view.animation.Interpolator;
28import com.android.systemui.R;
29
30
31/** A static Recents configuration for the current context
32 * NOTE: We should not hold any references to a Context from a static instance */
33public class RecentsConfiguration {
34    static RecentsConfiguration sInstance;
35
36    DisplayMetrics mDisplayMetrics;
37
38    public Rect systemInsets = new Rect();
39    public Rect displayRect = new Rect();
40
41    boolean isLandscape;
42    boolean transposeSearchLayoutWithOrientation;
43    int searchBarAppWidgetId = -1;
44
45    public float animationPxMovementPerSecond;
46
47    public Interpolator defaultBezierInterpolator;
48
49    public int filteringCurrentViewsMinAnimDuration;
50    public int filteringNewViewsMinAnimDuration;
51    public int taskBarEnterAnimDuration;
52    public int taskBarExitAnimDuration;
53    public int taskStackScrollDismissInfoPaneDistance;
54    public int taskStackMaxDim;
55    public int taskViewInfoPaneAnimDuration;
56    public int taskViewRemoveAnimDuration;
57    public int taskViewRemoveAnimTranslationXPx;
58    public int taskViewTranslationZMinPx;
59    public int taskViewTranslationZIncrementPx;
60    public int taskViewRoundedCornerRadiusPx;
61    public int searchBarSpaceHeightPx;
62
63    public int taskBarViewDefaultBackgroundColor;
64    public int taskBarViewDefaultTextColor;
65    public int taskBarViewLightTextColor;
66    public int taskBarViewDarkTextColor;
67
68    public boolean launchedFromAltTab;
69    public boolean launchedWithThumbnailAnimation;
70
71    /** Private constructor */
72    private RecentsConfiguration() {}
73
74    /** Updates the configuration to the current context */
75    public static RecentsConfiguration reinitialize(Context context) {
76        if (sInstance == null) {
77            sInstance = new RecentsConfiguration();
78        }
79        sInstance.update(context);
80        return sInstance;
81    }
82
83    /** Returns the current recents configuration */
84    public static RecentsConfiguration getInstance() {
85        return sInstance;
86    }
87
88    /** Updates the state, given the specified context */
89    void update(Context context) {
90        Resources res = context.getResources();
91        DisplayMetrics dm = res.getDisplayMetrics();
92        mDisplayMetrics = dm;
93
94        isLandscape = res.getConfiguration().orientation ==
95                Configuration.ORIENTATION_LANDSCAPE;
96        transposeSearchLayoutWithOrientation =
97                res.getBoolean(R.bool.recents_transpose_search_layout_with_orientation);
98        Console.log(Constants.Log.UI.MeasureAndLayout,
99                "[RecentsConfiguration|orientation]", isLandscape ? "Landscape" : "Portrait",
100                Console.AnsiGreen);
101
102        displayRect.set(0, 0, dm.widthPixels, dm.heightPixels);
103        animationPxMovementPerSecond =
104                res.getDimensionPixelSize(R.dimen.recents_animation_movement_in_dps_per_second);
105        filteringCurrentViewsMinAnimDuration =
106                res.getInteger(R.integer.recents_filter_animate_current_views_min_duration);
107        filteringNewViewsMinAnimDuration =
108                res.getInteger(R.integer.recents_filter_animate_new_views_min_duration);
109        taskBarEnterAnimDuration =
110                res.getInteger(R.integer.recents_animate_task_bar_enter_duration);
111        taskBarExitAnimDuration =
112                res.getInteger(R.integer.recents_animate_task_bar_exit_duration);
113        taskStackScrollDismissInfoPaneDistance = res.getDimensionPixelSize(
114                R.dimen.recents_task_stack_scroll_dismiss_info_pane_distance);
115        taskStackMaxDim = res.getInteger(R.integer.recents_max_task_stack_view_dim);
116        taskViewInfoPaneAnimDuration =
117                res.getInteger(R.integer.recents_animate_task_view_info_pane_duration);
118        taskViewRemoveAnimDuration =
119                res.getInteger(R.integer.recents_animate_task_view_remove_duration);
120        taskViewRemoveAnimTranslationXPx =
121                res.getDimensionPixelSize(R.dimen.recents_task_view_remove_anim_translation_x);
122        taskViewRoundedCornerRadiusPx =
123                res.getDimensionPixelSize(R.dimen.recents_task_view_rounded_corners_radius);
124        taskViewTranslationZMinPx = res.getDimensionPixelSize(R.dimen.recents_task_view_z_min);
125        taskViewTranslationZIncrementPx =
126                res.getDimensionPixelSize(R.dimen.recents_task_view_z_increment);
127        searchBarSpaceHeightPx = res.getDimensionPixelSize(R.dimen.recents_search_bar_space_height);
128
129        taskBarViewDefaultBackgroundColor =
130                res.getColor(R.color.recents_task_bar_default_background_color);
131        taskBarViewDefaultTextColor =
132                res.getColor(R.color.recents_task_bar_default_text_color);
133        taskBarViewLightTextColor =
134                res.getColor(R.color.recents_task_bar_light_text_color);
135        taskBarViewDarkTextColor =
136                res.getColor(R.color.recents_task_bar_dark_text_color);
137
138        defaultBezierInterpolator = AnimationUtils.loadInterpolator(context,
139                        com.android.internal.R.interpolator.fast_out_slow_in);
140
141        // Update the search widget id
142        SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0);
143        searchBarAppWidgetId = settings.getInt(Constants.Values.App.Key_SearchAppWidgetId, -1);
144    }
145
146    /** Updates the system insets */
147    public void updateSystemInsets(Rect insets) {
148        systemInsets.set(insets);
149    }
150
151    /** Updates the search bar app widget */
152    public void updateSearchBarAppWidgetId(Context context, int appWidgetId) {
153        searchBarAppWidgetId = appWidgetId;
154        SharedPreferences settings = context.getSharedPreferences(context.getPackageName(), 0);
155        settings.edit().putInt(Constants.Values.App.Key_SearchAppWidgetId,
156                appWidgetId).apply();
157    }
158
159    /** Returns whether the search bar app widget exists */
160    public boolean hasSearchBarAppWidget() {
161        return searchBarAppWidgetId >= 0;
162    }
163
164    /**
165     * Returns the task stack bounds in the current orientation. These bounds do not account for
166     * the system insets.
167     */
168    public void getTaskStackBounds(int width, int height, Rect taskStackBounds) {
169        if (hasSearchBarAppWidget()) {
170            Rect searchBarBounds = new Rect();
171            getSearchBarBounds(width, height, searchBarBounds);
172            if (isLandscape && transposeSearchLayoutWithOrientation) {
173                // In landscape, the search bar appears on the left, so shift the task rect right
174                taskStackBounds.set(searchBarBounds.width(), 0, width, height);
175            } else {
176                // In portrait, the search bar appears on the top, so shift the task rect below
177                taskStackBounds.set(0, searchBarBounds.height(), width, height);
178            }
179        } else {
180            taskStackBounds.set(0, 0, width, height);
181        }
182    }
183
184    /**
185     * Returns the search bar bounds in the current orientation.  These bounds do not account for
186     * the system insets.
187     */
188    public void getSearchBarBounds(int width, int height, Rect searchBarSpaceBounds) {
189        // Return empty rects if search is not enabled
190        if (!Constants.DebugFlags.App.EnableSearchLayout) {
191            searchBarSpaceBounds.set(0, 0, 0, 0);
192            return;
193        }
194
195        if (isLandscape && transposeSearchLayoutWithOrientation) {
196            // In landscape, the search bar appears on the left
197            searchBarSpaceBounds.set(0, 0, searchBarSpaceHeightPx, height);
198        } else {
199            // In portrait, the search bar appears on the top
200            searchBarSpaceBounds.set(0, 0, width, searchBarSpaceHeightPx);
201        }
202    }
203
204    /** Converts from DPs to PXs */
205    public int pxFromDp(float size) {
206        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
207                size, mDisplayMetrics));
208    }
209    /** Converts from SPs to PXs */
210    public int pxFromSp(float size) {
211        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
212                size, mDisplayMetrics));
213    }
214}
215