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