1/* 2 * Copyright (C) 2013 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 18package android.support.v4.app; 19 20import android.R; 21import android.app.ActionBar; 22import android.app.Activity; 23import android.content.Context; 24import android.content.res.TypedArray; 25import android.graphics.drawable.Drawable; 26import android.os.Build; 27import android.util.Log; 28import android.view.View; 29import android.view.ViewGroup; 30import android.widget.ImageView; 31 32import java.lang.reflect.Method; 33 34/** 35 * This class encapsulates some awful hacks. 36 * 37 * Before JB-MR2 (API 18) it was not possible to change the home-as-up indicator glyph 38 * in an action bar without some really gross hacks. Since the MR2 SDK is not published as of 39 * this writing, the new API is accessed via reflection here if available. 40 */ 41class ActionBarDrawerToggleHoneycomb { 42 private static final String TAG = "ActionBarDrawerToggleHoneycomb"; 43 44 private static final int[] THEME_ATTRS = new int[] { 45 R.attr.homeAsUpIndicator 46 }; 47 48 public static Object setActionBarUpIndicator(Object info, Activity activity, 49 Drawable drawable, int contentDescRes) { 50 if (info == null) { 51 info = new SetIndicatorInfo(activity); 52 } 53 final SetIndicatorInfo sii = (SetIndicatorInfo) info; 54 if (sii.setHomeAsUpIndicator != null) { 55 try { 56 final ActionBar actionBar = activity.getActionBar(); 57 sii.setHomeAsUpIndicator.invoke(actionBar, drawable); 58 sii.setHomeActionContentDescription.invoke(actionBar, contentDescRes); 59 } catch (Exception e) { 60 Log.w(TAG, "Couldn't set home-as-up indicator via JB-MR2 API", e); 61 } 62 } else if (sii.upIndicatorView != null) { 63 sii.upIndicatorView.setImageDrawable(drawable); 64 } else { 65 Log.w(TAG, "Couldn't set home-as-up indicator"); 66 } 67 return info; 68 } 69 70 public static Object setActionBarDescription(Object info, Activity activity, 71 int contentDescRes) { 72 if (info == null) { 73 info = new SetIndicatorInfo(activity); 74 } 75 final SetIndicatorInfo sii = (SetIndicatorInfo) info; 76 if (sii.setHomeAsUpIndicator != null) { 77 try { 78 final ActionBar actionBar = activity.getActionBar(); 79 sii.setHomeActionContentDescription.invoke(actionBar, contentDescRes); 80 if (Build.VERSION.SDK_INT <= 19) { 81 // For API 19 and earlier, we need to manually force the 82 // action bar to generate a new content description. 83 actionBar.setSubtitle(actionBar.getSubtitle()); 84 } 85 } catch (Exception e) { 86 Log.w(TAG, "Couldn't set content description via JB-MR2 API", e); 87 } 88 } 89 return info; 90 } 91 92 public static Drawable getThemeUpIndicator(Activity activity) { 93 final TypedArray a = activity.obtainStyledAttributes(THEME_ATTRS); 94 final Drawable result = a.getDrawable(0); 95 a.recycle(); 96 return result; 97 } 98 99 private static class SetIndicatorInfo { 100 public Method setHomeAsUpIndicator; 101 public Method setHomeActionContentDescription; 102 public ImageView upIndicatorView; 103 104 SetIndicatorInfo(Activity activity) { 105 try { 106 setHomeAsUpIndicator = ActionBar.class.getDeclaredMethod("setHomeAsUpIndicator", 107 Drawable.class); 108 setHomeActionContentDescription = ActionBar.class.getDeclaredMethod( 109 "setHomeActionContentDescription", Integer.TYPE); 110 111 // If we got the method we won't need the stuff below. 112 return; 113 } catch (NoSuchMethodException e) { 114 // Oh well. We'll use the other mechanism below instead. 115 } 116 117 final View home = activity.findViewById(android.R.id.home); 118 if (home == null) { 119 // Action bar doesn't have a known configuration, an OEM messed with things. 120 return; 121 } 122 123 final ViewGroup parent = (ViewGroup) home.getParent(); 124 final int childCount = parent.getChildCount(); 125 if (childCount != 2) { 126 // No idea which one will be the right one, an OEM messed with things. 127 return; 128 } 129 130 final View first = parent.getChildAt(0); 131 final View second = parent.getChildAt(1); 132 final View up = first.getId() == android.R.id.home ? second : first; 133 134 if (up instanceof ImageView) { 135 // Jackpot! (Probably...) 136 upIndicatorView = (ImageView) up; 137 } 138 } 139 } 140} 141