1 package android
.support
.v4
.app
;
3 import android
.util
.Log
;
4 import android
.view
.View
;
5 import android
.view
.Window
;
6 import com
.actionbarsherlock
.ActionBarSherlock
.OnCreatePanelMenuListener
;
7 import com
.actionbarsherlock
.ActionBarSherlock
.OnMenuItemSelectedListener
;
8 import com
.actionbarsherlock
.ActionBarSherlock
.OnPreparePanelListener
;
9 import com
.actionbarsherlock
.view
.Menu
;
10 import com
.actionbarsherlock
.view
.MenuInflater
;
11 import com
.actionbarsherlock
.view
.MenuItem
;
13 import java
.util
.ArrayList
;
15 /** I'm in ur package. Stealing ur variables. */
16 public abstract class Watson
extends FragmentActivity
implements OnCreatePanelMenuListener
, OnPreparePanelListener
, OnMenuItemSelectedListener
{
17 private static final boolean DEBUG
= false
;
18 private static final String TAG
= "Watson";
20 /** Fragment interface for menu creation callback. */
21 public interface OnCreateOptionsMenuListener
{
22 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
);
24 /** Fragment interface for menu preparation callback. */
25 public interface OnPrepareOptionsMenuListener
{
26 public void onPrepareOptionsMenu(Menu menu
);
28 /** Fragment interface for menu item selection callback. */
29 public interface OnOptionsItemSelectedListener
{
30 public boolean onOptionsItemSelected(MenuItem item
);
33 private ArrayList
<Fragment
> mCreatedMenus
;
36 ///////////////////////////////////////////////////////////////////////////
37 // Sherlock menu handling
38 ///////////////////////////////////////////////////////////////////////////
41 public boolean onCreatePanelMenu(int featureId
, Menu menu
) {
42 if (DEBUG
) Log
.d(TAG
, "[onCreatePanelMenu] featureId: " + featureId
+ ", menu: " + menu
);
44 if (featureId
== Window
.FEATURE_OPTIONS_PANEL
) {
45 boolean result
= onCreateOptionsMenu(menu
);
46 if (DEBUG
) Log
.d(TAG
, "[onCreatePanelMenu] activity create result: " + result
);
48 MenuInflater inflater
= getSupportMenuInflater();
50 ArrayList
<Fragment
> newMenus
= null
;
51 if (mFragments
.mAdded
!= null
) {
52 for (int i
= 0; i
< mFragments
.mAdded
.size(); i
++) {
53 Fragment f
= mFragments
.mAdded
.get(i
);
54 if (f
!= null
&& !f
.mHidden
&& f
.mHasMenu
&& f
.mMenuVisible
&& f
instanceof OnCreateOptionsMenuListener
) {
56 ((OnCreateOptionsMenuListener
)f
).onCreateOptionsMenu(menu
, inflater
);
57 if (newMenus
== null
) {
58 newMenus
= new ArrayList
<Fragment
>();
65 if (mCreatedMenus
!= null
) {
66 for (int i
= 0; i
< mCreatedMenus
.size(); i
++) {
67 Fragment f
= mCreatedMenus
.get(i
);
68 if (newMenus
== null
|| !newMenus
.contains(f
)) {
69 f
.onDestroyOptionsMenu();
74 mCreatedMenus
= newMenus
;
76 if (DEBUG
) Log
.d(TAG
, "[onCreatePanelMenu] fragments create result: " + show
);
79 if (DEBUG
) Log
.d(TAG
, "[onCreatePanelMenu] returning " + result
);
86 public boolean onPreparePanel(int featureId
, View view
, Menu menu
) {
87 if (DEBUG
) Log
.d(TAG
, "[onPreparePanel] featureId: " + featureId
+ ", view: " + view
+ " menu: " + menu
);
89 if (featureId
== Window
.FEATURE_OPTIONS_PANEL
) {
90 boolean result
= onPrepareOptionsMenu(menu
);
91 if (DEBUG
) Log
.d(TAG
, "[onPreparePanel] activity prepare result: " + result
);
94 if (mFragments
.mAdded
!= null
) {
95 for (int i
= 0; i
< mFragments
.mAdded
.size(); i
++) {
96 Fragment f
= mFragments
.mAdded
.get(i
);
97 if (f
!= null
&& !f
.mHidden
&& f
.mHasMenu
&& f
.mMenuVisible
&& f
instanceof OnPrepareOptionsMenuListener
) {
99 ((OnPrepareOptionsMenuListener
)f
).onPrepareOptionsMenu(menu
);
104 if (DEBUG
) Log
.d(TAG
, "[onPreparePanel] fragments prepare result: " + show
);
107 result
&= menu
.hasVisibleItems();
108 if (DEBUG
) Log
.d(TAG
, "[onPreparePanel] returning " + result
);
115 public boolean onMenuItemSelected(int featureId
, MenuItem item
) {
116 if (DEBUG
) Log
.d(TAG
, "[onMenuItemSelected] featureId: " + featureId
+ ", item: " + item
);
118 if (featureId
== Window
.FEATURE_OPTIONS_PANEL
) {
119 if (onOptionsItemSelected(item
)) {
123 if (mFragments
.mAdded
!= null
) {
124 for (int i
= 0; i
< mFragments
.mAdded
.size(); i
++) {
125 Fragment f
= mFragments
.mAdded
.get(i
);
126 if (f
!= null
&& !f
.mHidden
&& f
.mHasMenu
&& f
.mMenuVisible
&& f
instanceof OnOptionsItemSelectedListener
) {
127 if (((OnOptionsItemSelectedListener
)f
).onOptionsItemSelected(item
)) {
137 public abstract boolean onCreateOptionsMenu(Menu menu
);
139 public abstract boolean onPrepareOptionsMenu(Menu menu
);
141 public abstract boolean onOptionsItemSelected(MenuItem item
);
143 public abstract MenuInflater
getSupportMenuInflater();