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