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