ActionBarPolicy.java revision da10fdd1400ecfd8d7f2e55651dd528d0614dfc5
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
17package android.support.v7.internal.view;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
22import android.support.v7.appcompat.R;
23
24/**
25 * Allows components to query for various configuration policy decisions about how the action bar
26 * should lay out and behave on the current device.
27 */
28public class ActionBarPolicy {
29
30    private Context mContext;
31
32    public static ActionBarPolicy get(Context context) {
33        return new ActionBarPolicy(context);
34    }
35
36    private ActionBarPolicy(Context context) {
37        mContext = context;
38    }
39
40    public int getMaxActionButtons() {
41        return mContext.getResources().getInteger(R.integer.max_action_buttons);
42    }
43
44    public boolean showsOverflowMenuButton() {
45        // Permanent menu key is always present on pre-HC devices
46        // return !ViewConfiguration.get(mContext).hasPermanentMenuKey();
47        return false;
48    }
49
50    public int getEmbeddedMenuWidthLimit() {
51        return mContext.getResources().getDisplayMetrics().widthPixels / 2;
52    }
53
54    public boolean hasEmbeddedTabs() {
55        final int targetSdk = mContext.getApplicationInfo().targetSdkVersion;
56        // DISABLED: Compatibility implementation will not be used on JB
57        /*if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN) {
58          return mContext.getResources().getBoolean(R.bool.action_bar_embed_tabs);
59        }*/
60
61        // The embedded tabs policy changed in Jellybean; give older apps the old policy
62        // so they get what they expect.
63        return mContext.getResources().getBoolean(R.bool.action_bar_embed_tabs_pre_jb);
64    }
65
66    public int getTabContainerHeight() {
67        TypedArray a = mContext.obtainStyledAttributes(null, R.styleable.ActionBar,
68                R.attr.actionBarStyle, 0);
69        int height = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
70        Resources r = mContext.getResources();
71        if (!hasEmbeddedTabs()) {
72            // Stacked tabs; limit the height
73            height = Math.min(height,
74                    r.getDimensionPixelSize(R.dimen.action_bar_stacked_max_height));
75        }
76        a.recycle();
77        return height;
78    }
79
80    public boolean enableHomeButtonByDefault() {
81        // Older apps get the home button interaction enabled by default.
82        // Newer apps need to enable it explicitly.
83        return mContext.getApplicationInfo().targetSdkVersion < 14; // ICE_CREAM_SANDWICH
84    }
85
86    public int getStackedTabMaxWidth() {
87        return mContext.getResources().getDimensionPixelSize(
88                R.dimen.action_bar_stacked_tab_max_width);
89    }
90}
91