RecentsConfiguration.java revision ecd9b3031c9a322bd69380eae2810d53839e8f64
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.res.Configuration;
21import android.content.res.Resources;
22import android.graphics.Rect;
23import android.util.DisplayMetrics;
24import android.util.TypedValue;
25import com.android.systemui.R;
26
27
28/** A static Recents configuration for the current context
29 * NOTE: We should not hold any references to a Context from a static instance */
30public class RecentsConfiguration {
31    static RecentsConfiguration sInstance;
32
33    DisplayMetrics mDisplayMetrics;
34
35    public Rect systemInsets = new Rect();
36    public Rect displayRect = new Rect();
37
38    public float animationPxMovementPerSecond;
39
40    public int filteringCurrentViewsMinAnimDuration;
41    public int filteringNewViewsMinAnimDuration;
42    public int taskBarEnterAnimDuration;
43    public int taskStackScrollDismissInfoPaneDistance;
44    public int taskStackMaxDim;
45    public int taskViewInfoPaneAnimDuration;
46    public int taskViewRoundedCornerRadiusPx;
47    public int searchBarSpaceHeightPx;
48    public int searchBarSpaceEdgeMarginsPx;
49
50    public boolean launchedWithThumbnailAnimation;
51
52    /** Private constructor */
53    private RecentsConfiguration() {}
54
55    /** Updates the configuration to the current context */
56    public static RecentsConfiguration reinitialize(Context context) {
57        if (sInstance == null) {
58            sInstance = new RecentsConfiguration();
59        }
60        sInstance.update(context);
61        return sInstance;
62    }
63
64    /** Returns the current recents configuration */
65    public static RecentsConfiguration getInstance() {
66        return sInstance;
67    }
68
69    /** Updates the state, given the specified context */
70    void update(Context context) {
71        Resources res = context.getResources();
72        DisplayMetrics dm = res.getDisplayMetrics();
73        mDisplayMetrics = dm;
74
75        boolean isLandscape = res.getConfiguration().orientation ==
76                Configuration.ORIENTATION_LANDSCAPE;
77        Console.log(Constants.DebugFlags.UI.MeasureAndLayout,
78                "[RecentsConfiguration|orientation]", isLandscape ? "Landscape" : "Portrait",
79                Console.AnsiGreen);
80
81        displayRect.set(0, 0, dm.widthPixels, dm.heightPixels);
82        animationPxMovementPerSecond =
83                res.getDimensionPixelSize(R.dimen.recents_animation_movement_in_dps_per_second);
84        filteringCurrentViewsMinAnimDuration =
85                res.getInteger(R.integer.recents_filter_animate_current_views_min_duration);
86        filteringNewViewsMinAnimDuration =
87                res.getInteger(R.integer.recents_filter_animate_new_views_min_duration);
88        taskBarEnterAnimDuration =
89                res.getInteger(R.integer.recents_animate_task_bar_enter_duration);
90        taskStackScrollDismissInfoPaneDistance = res.getDimensionPixelSize(
91                R.dimen.recents_task_stack_scroll_dismiss_info_pane_distance);
92        taskStackMaxDim = res.getInteger(R.integer.recents_max_task_stack_view_dim);
93        taskViewInfoPaneAnimDuration =
94                res.getInteger(R.integer.recents_animate_task_view_info_pane_duration);
95        taskViewRoundedCornerRadiusPx =
96                res.getDimensionPixelSize(R.dimen.recents_task_view_rounded_corners_radius);
97        searchBarSpaceHeightPx = res.getDimensionPixelSize(R.dimen.recents_search_bar_space_height);
98        searchBarSpaceEdgeMarginsPx =
99                res.getDimensionPixelSize(R.dimen.recents_search_bar_space_edge_margins);
100    }
101
102    /** Updates the system insets */
103    public void updateSystemInsets(Rect insets) {
104        systemInsets.set(insets);
105    }
106
107    /** Returns the search bar bounds in the specified orientation */
108    public void getSearchBarBounds(int width, int height,
109                                   Rect searchBarSpaceBounds, Rect searchBarBounds) {
110        // Return empty rects if search is not enabled
111        if (!Constants.DebugFlags.App.EnableSearchButton) {
112            searchBarSpaceBounds.set(0, 0, 0, 0);
113            searchBarBounds.set(0, 0, 0, 0);
114            return;
115        }
116
117        // Calculate the search bar bounds, and account for the system insets
118        int edgeMarginPx = searchBarSpaceEdgeMarginsPx;
119        int availableWidth = width - systemInsets.left - systemInsets.right;
120        searchBarSpaceBounds.set(0, 0, availableWidth, 2 * edgeMarginPx + searchBarSpaceHeightPx);
121
122        // Inset from the search bar space to get the search bar bounds
123        searchBarBounds.set(searchBarSpaceBounds);
124        searchBarBounds.inset(edgeMarginPx, edgeMarginPx);
125    }
126
127    /** Converts from DPs to PXs */
128    public int pxFromDp(float size) {
129        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
130                size, mDisplayMetrics));
131    }
132    /** Converts from SPs to PXs */
133    public int pxFromSp(float size) {
134        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
135                size, mDisplayMetrics));
136    }
137}
138