1 /* 2 * Copyright 2011 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 17 package com.example.android.actionbarcompat; 18 19 import android.app.Activity; 20 import android.os.Build; 21 import android.os.Bundle; 22 import android.view.Menu; 23 import android.view.MenuInflater; 24 25 /** 26 * An abstract class that handles some common action bar-related functionality in the app. This 27 * class provides functionality useful for both phones and tablets, and does not require any Android 28 * 3.0-specific features, although it uses them if available. 29 * 30 * Two implementations of this class are {@link ActionBarHelperBase} for a pre-Honeycomb version of 31 * the action bar, and {@link ActionBarHelperHoneycomb}, which uses the built-in ActionBar features 32 * in Android 3.0 and later. 33 */ 34 public abstract class ActionBarHelper { 35 protected Activity mActivity; 36 37 /** 38 * Factory method for creating {@link ActionBarHelper} objects for a 39 * given activity. Depending on which device the app is running, either a basic helper or 40 * Honeycomb-specific helper will be returned. 41 */ 42 public static ActionBarHelper createInstance(Activity activity) { 43 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 44 return new ActionBarHelperICS(activity); 45 } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 46 return new ActionBarHelperHoneycomb(activity); 47 } else { 48 return new ActionBarHelperBase(activity); 49 } 50 } 51 52 protected ActionBarHelper(Activity activity) { 53 mActivity = activity; 54 } 55 56 /** 57 * Action bar helper code to be run in {@link Activity#onCreate(android.os.Bundle)}. 58 */ 59 public void onCreate(Bundle savedInstanceState) { 60 } 61 62 /** 63 * Action bar helper code to be run in {@link Activity#onPostCreate(android.os.Bundle)}. 64 */ 65 public void onPostCreate(Bundle savedInstanceState) { 66 } 67 68 /** 69 * Action bar helper code to be run in {@link Activity#onCreateOptionsMenu(android.view.Menu)}. 70 * 71 * NOTE: Setting the visibility of menu items in <em>menu</em> is not currently supported. 72 */ 73 public boolean onCreateOptionsMenu(Menu menu) { 74 return true; 75 } 76 77 /** 78 * Action bar helper code to be run in {@link Activity#onTitleChanged(CharSequence, int)}. 79 */ 80 protected void onTitleChanged(CharSequence title, int color) { 81 } 82 83 /** 84 * Sets the indeterminate loading state of the item with ID {@link R.id.menu_refresh}. 85 * (where the item ID was menu_refresh). 86 */ 87 public abstract void setRefreshActionItemState(boolean refreshing); 88 89 /** 90 * Returns a {@link MenuInflater} for use when inflating menus. The implementation of this 91 * method in {@link ActionBarHelperBase} returns a wrapped menu inflater that can read 92 * action bar metadata from a menu resource pre-Honeycomb. 93 */ 94 public MenuInflater getMenuInflater(MenuInflater superMenuInflater) { 95 return superMenuInflater; 96 } 97 } 98