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