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.os.Build;
23import android.support.v4.view.ViewConfigurationCompat;
24import android.support.v7.appcompat.R;
25import android.view.ViewConfiguration;
26
27/**
28 * Allows components to query for various configuration policy decisions about how the action bar
29 * should lay out and behave on the current device.
30 *
31 * @hide
32 */
33public class ActionBarPolicy {
34
35    private Context mContext;
36
37    public static ActionBarPolicy get(Context context) {
38        return new ActionBarPolicy(context);
39    }
40
41    private ActionBarPolicy(Context context) {
42        mContext = context;
43    }
44
45    public int getMaxActionButtons() {
46        return mContext.getResources().getInteger(R.integer.abc_max_action_buttons);
47    }
48
49    public boolean showsOverflowMenuButton() {
50        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
51            return true;
52        } else {
53            return !ViewConfigurationCompat.hasPermanentMenuKey(ViewConfiguration.get(mContext));
54        }
55    }
56
57    public int getEmbeddedMenuWidthLimit() {
58        return mContext.getResources().getDisplayMetrics().widthPixels / 2;
59    }
60
61    public boolean hasEmbeddedTabs() {
62        final int targetSdk = mContext.getApplicationInfo().targetSdkVersion;
63        if (targetSdk >= Build.VERSION_CODES.JELLY_BEAN) {
64            return mContext.getResources().getBoolean(R.bool.abc_action_bar_embed_tabs);
65        }
66
67        // The embedded tabs policy changed in Jellybean; give older apps the old policy
68        // so they get what they expect.
69        return mContext.getResources().getBoolean(R.bool.abc_action_bar_embed_tabs_pre_jb);
70    }
71
72    public int getTabContainerHeight() {
73        TypedArray a = mContext.obtainStyledAttributes(null, R.styleable.ActionBar,
74                R.attr.actionBarStyle, 0);
75        int height = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
76        Resources r = mContext.getResources();
77        if (!hasEmbeddedTabs()) {
78            // Stacked tabs; limit the height
79            height = Math.min(height,
80                    r.getDimensionPixelSize(R.dimen.abc_action_bar_stacked_max_height));
81        }
82        a.recycle();
83        return height;
84    }
85
86    public boolean enableHomeButtonByDefault() {
87        // Older apps get the home button interaction enabled by default.
88        // Newer apps need to enable it explicitly.
89        return mContext.getApplicationInfo().targetSdkVersion <
90                Build.VERSION_CODES.ICE_CREAM_SANDWICH;
91    }
92
93    public int getStackedTabMaxWidth() {
94        return mContext.getResources().getDimensionPixelSize(
95                R.dimen.abc_action_bar_stacked_tab_max_width);
96    }
97}
98