2525import android .view .ViewGroup ;
2626import android .widget .RelativeLayout ;
2727
28+ import com .nhaarman .supertooltips .exception .NoOverflowMenuRuntimeException ;
29+ import com .nhaarman .supertooltips .exception .NoTitleViewRuntimeException ;
30+ import com .nhaarman .supertooltips .exception .ViewNotFoundRuntimeException ;
31+
2832public class ToolTipRelativeLayout extends RelativeLayout {
2933
30- public ToolTipRelativeLayout (Context context ) {
34+ public static final String ACTION_BAR_TITLE = "action_bar_title" ;
35+ public static final String ID = "id" ;
36+ public static final String ANDROID = "android" ;
37+ public static final String ACTION_BAR = "action_bar" ;
38+ public static final String ACTION_MENU_VIEW = "ActionMenuView" ;
39+ public static final String OVERFLOW_MENU_BUTTON = "OverflowMenuButton" ;
40+
41+ public ToolTipRelativeLayout (final Context context ) {
3142 super (context );
3243 }
3344
34- public ToolTipRelativeLayout (Context context , AttributeSet attrs ) {
45+ public ToolTipRelativeLayout (final Context context , final AttributeSet attrs ) {
3546 super (context , attrs );
3647 }
3748
38- public ToolTipRelativeLayout (Context context , AttributeSet attrs , int defStyle ) {
49+ public ToolTipRelativeLayout (final Context context , final AttributeSet attrs , final int defStyle ) {
3950 super (context , attrs , defStyle );
4051 }
4152
@@ -123,7 +134,7 @@ public ToolTipView showToolTipForActionBarHome(final Activity activity, final To
123134 */
124135 @ TargetApi (11 )
125136 public ToolTipView showToolTipForActionBarTitle (final Activity activity , final ToolTip toolTip ) {
126- final int titleResId = Resources .getSystem ().getIdentifier ("action_bar_title" , "id" , "android" );
137+ final int titleResId = Resources .getSystem ().getIdentifier (ACTION_BAR_TITLE , ID , ANDROID );
127138 if (titleResId == 0 ) {
128139 throw new NoTitleViewRuntimeException ();
129140 }
@@ -153,15 +164,16 @@ public ToolTipView showToolTipForActionBarOverflowMenu(final Activity activity,
153164 }
154165
155166 @ TargetApi (11 )
156- private View findActionBarOverflowMenuView (final Activity activity ) {
167+ private static View findActionBarOverflowMenuView (final Activity activity ) {
157168 final ViewGroup decorView = (ViewGroup ) activity .getWindow ().getDecorView ();
158169
159- final int actionBarViewResId = Resources .getSystem ().getIdentifier ("action_bar" , "id" , "android" );
170+ final int actionBarViewResId = Resources .getSystem ().getIdentifier (ACTION_BAR , ID , ANDROID );
160171 final ViewGroup actionBarView = (ViewGroup ) decorView .findViewById (actionBarViewResId );
161172
162173 ViewGroup actionMenuView = null ;
163- for (int i = 0 ; i < actionBarView .getChildCount (); ++i ) {
164- if (actionBarView .getChildAt (i ).getClass ().getSimpleName ().equals ("ActionMenuView" )) {
174+ int actionBarViewChildCount = actionBarView .getChildCount ();
175+ for (int i = 0 ; i < actionBarViewChildCount ; ++i ) {
176+ if (actionBarView .getChildAt (i ).getClass ().getSimpleName ().equals (ACTION_MENU_VIEW )) {
165177 actionMenuView = (ViewGroup ) actionBarView .getChildAt (i );
166178 }
167179 }
@@ -170,9 +182,10 @@ private View findActionBarOverflowMenuView(final Activity activity) {
170182 throw new NoOverflowMenuRuntimeException ();
171183 }
172184
185+ int actionMenuChildCount = actionMenuView .getChildCount ();
173186 View overflowMenuButton = null ;
174- for (int i = 0 ; i < actionMenuView . getChildCount () ; ++i ) {
175- if (actionMenuView .getChildAt (i ).getClass ().getSimpleName ().equals ("OverflowMenuButton" )) {
187+ for (int i = 0 ; i < actionMenuChildCount ; ++i ) {
188+ if (actionMenuView .getChildAt (i ).getClass ().getSimpleName ().equals (OVERFLOW_MENU_BUTTON )) {
176189 overflowMenuButton = actionMenuView .getChildAt (i );
177190 }
178191 }
@@ -183,38 +196,4 @@ private View findActionBarOverflowMenuView(final Activity activity) {
183196
184197 return overflowMenuButton ;
185198 }
186-
187- /**
188- * A {@link RuntimeException} that is thrown if there is no {@link View}
189- * found with given resource id. You can choose to ignore this by catching
190- * the ViewNotFoundRuntimeException.
191- */
192- public class ViewNotFoundRuntimeException extends RuntimeException {
193- public ViewNotFoundRuntimeException () {
194- super ("View not found for this resource id. Are you sure it exists?" );
195- }
196- }
197-
198- /**
199- * A {@link RuntimeException} that is thrown if the title view in the
200- * {@link ActionBar} is not found. You can choose to ignore this by catching
201- * the NoTitleViewRuntimeException.
202- */
203- public class NoTitleViewRuntimeException extends RuntimeException {
204- public NoTitleViewRuntimeException () {
205- super ("No title View found. Are you sure it exists?" );
206- }
207- }
208-
209- /**
210- * A {@link RuntimeException} that is thrown if the overflow menu is not
211- * found. This happens when there simply is no overflow menu button, or the
212- * menu isn't initialised yet. You can choose to ignore this by catching the
213- * NoOverflowMenuRuntimeException.
214- */
215- public class NoOverflowMenuRuntimeException extends RuntimeException {
216- public NoOverflowMenuRuntimeException () {
217- super ("No overflow menu found. Are you sure the overflow menu button is visible? Check the docs for showToolTipForActionBarOverflowMenu(Activity, ToolTip) again!" );
218- }
219- }
220199}
0 commit comments