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