• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 
17 package com.android.internal.view;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.content.res.Resources;
23 import android.content.res.TypedArray;
24 import android.os.Build;
25 
26 import com.android.internal.R;
27 
28 /**
29  * Allows components to query for various configuration policy decisions
30  * about how the action bar should lay out and behave on the current device.
31  */
32 public class ActionBarPolicy {
33     @UnsupportedAppUsage
34     private Context mContext;
35 
36     @UnsupportedAppUsage
get(Context context)37     public static ActionBarPolicy get(Context context) {
38         return new ActionBarPolicy(context);
39     }
40 
41     @UnsupportedAppUsage
ActionBarPolicy(Context context)42     private ActionBarPolicy(Context context) {
43         mContext = context;
44     }
45 
46     /**
47      * Returns the maximum number of action buttons that should be permitted within an action
48      * bar/action mode. This will be used to determine how many showAsAction="ifRoom" items can fit.
49      * "always" items can override this.
50      */
51     @UnsupportedAppUsage
getMaxActionButtons()52     public int getMaxActionButtons() {
53         final Configuration config = mContext.getResources().getConfiguration();
54         final int width = config.screenWidthDp;
55         final int height = config.screenHeightDp;
56         final int smallest = config.smallestScreenWidthDp;
57         if (smallest > 600 || (width > 960 && height > 720) || (width > 720 && height > 960)) {
58             // For values-w600dp, values-sw600dp and values-xlarge.
59             return 5;
60         } else if (width >= 500 || (width > 640 && height > 480) || (width > 480 && height > 640)) {
61             // For values-w500dp and values-large.
62             return 4;
63         } else if (width >= 360) {
64             // For values-w360dp.
65             return 3;
66         } else {
67             return 2;
68         }
69     }
70     @UnsupportedAppUsage
showsOverflowMenuButton()71     public boolean showsOverflowMenuButton() {
72         return true;
73     }
74 
75     @UnsupportedAppUsage
getEmbeddedMenuWidthLimit()76     public int getEmbeddedMenuWidthLimit() {
77         return mContext.getResources().getDisplayMetrics().widthPixels / 2;
78     }
79 
80     @UnsupportedAppUsage
hasEmbeddedTabs()81     public boolean hasEmbeddedTabs() {
82         final int targetSdk = mContext.getApplicationInfo().targetSdkVersion;
83         if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN) {
84             return mContext.getResources().getBoolean(R.bool.action_bar_embed_tabs);
85         }
86 
87         // The embedded tabs policy changed in Jellybean; give older apps the old policy
88         // so they get what they expect.
89         final Configuration configuration = mContext.getResources().getConfiguration();
90         final int width = configuration.screenWidthDp;
91         final int height = configuration.screenHeightDp;
92         return configuration.orientation == Configuration.ORIENTATION_LANDSCAPE ||
93                 width >= 480 || (width >= 640 && height >= 480);
94     }
95 
96     @UnsupportedAppUsage
getTabContainerHeight()97     public int getTabContainerHeight() {
98         TypedArray a = mContext.obtainStyledAttributes(null, R.styleable.ActionBar,
99                 com.android.internal.R.attr.actionBarStyle, 0);
100         int height = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
101         Resources r = mContext.getResources();
102         if (!hasEmbeddedTabs()) {
103             // Stacked tabs; limit the height
104             height = Math.min(height,
105                     r.getDimensionPixelSize(R.dimen.action_bar_stacked_max_height));
106         }
107         a.recycle();
108         return height;
109     }
110 
enableHomeButtonByDefault()111     public boolean enableHomeButtonByDefault() {
112         // Older apps get the home button interaction enabled by default.
113         // Newer apps need to enable it explicitly.
114         return mContext.getApplicationInfo().targetSdkVersion <
115                 Build.VERSION_CODES.ICE_CREAM_SANDWICH;
116     }
117 
118     @UnsupportedAppUsage
getStackedTabMaxWidth()119     public int getStackedTabMaxWidth() {
120         return mContext.getResources().getDimensionPixelSize(
121                 R.dimen.action_bar_stacked_tab_max_width);
122     }
123 }
124