include actionbarsherlock as a part of owncloud application
[pub/Android/ownCloud.git] / actionbarsherlock / src / android / support / v4 / app / Watson.java
1 package android.support.v4.app;
2
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;
12
13 import java.util.ArrayList;
14
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";
19
20 /** Fragment interface for menu creation callback. */
21 public interface OnCreateOptionsMenuListener {
22 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater);
23 }
24 /** Fragment interface for menu preparation callback. */
25 public interface OnPrepareOptionsMenuListener {
26 public void onPrepareOptionsMenu(Menu menu);
27 }
28 /** Fragment interface for menu item selection callback. */
29 public interface OnOptionsItemSelectedListener {
30 public boolean onOptionsItemSelected(MenuItem item);
31 }
32
33 private ArrayList<Fragment> mCreatedMenus;
34
35
36 ///////////////////////////////////////////////////////////////////////////
37 // Sherlock menu handling
38 ///////////////////////////////////////////////////////////////////////////
39
40 @Override
41 public boolean onCreatePanelMenu(int featureId, Menu menu) {
42 if (DEBUG) Log.d(TAG, "[onCreatePanelMenu] featureId: " + featureId + ", menu: " + menu);
43
44 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
45 boolean result = onCreateOptionsMenu(menu);
46 if (DEBUG) Log.d(TAG, "[onCreatePanelMenu] activity create result: " + result);
47
48 MenuInflater inflater = getSupportMenuInflater();
49 boolean show = false;
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) {
55 show = true;
56 ((OnCreateOptionsMenuListener)f).onCreateOptionsMenu(menu, inflater);
57 if (newMenus == null) {
58 newMenus = new ArrayList<Fragment>();
59 }
60 newMenus.add(f);
61 }
62 }
63 }
64
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();
70 }
71 }
72 }
73
74 mCreatedMenus = newMenus;
75
76 if (DEBUG) Log.d(TAG, "[onCreatePanelMenu] fragments create result: " + show);
77 result |= show;
78
79 if (DEBUG) Log.d(TAG, "[onCreatePanelMenu] returning " + result);
80 return result;
81 }
82 return false;
83 }
84
85 @Override
86 public boolean onPreparePanel(int featureId, View view, Menu menu) {
87 if (DEBUG) Log.d(TAG, "[onPreparePanel] featureId: " + featureId + ", view: " + view + " menu: " + menu);
88
89 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
90 boolean result = onPrepareOptionsMenu(menu);
91 if (DEBUG) Log.d(TAG, "[onPreparePanel] activity prepare result: " + result);
92
93 boolean show = false;
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) {
98 show = true;
99 ((OnPrepareOptionsMenuListener)f).onPrepareOptionsMenu(menu);
100 }
101 }
102 }
103
104 if (DEBUG) Log.d(TAG, "[onPreparePanel] fragments prepare result: " + show);
105 result |= show;
106
107 result &= menu.hasVisibleItems();
108 if (DEBUG) Log.d(TAG, "[onPreparePanel] returning " + result);
109 return result;
110 }
111 return false;
112 }
113
114 @Override
115 public boolean onMenuItemSelected(int featureId, MenuItem item) {
116 if (DEBUG) Log.d(TAG, "[onMenuItemSelected] featureId: " + featureId + ", item: " + item);
117
118 if (featureId == Window.FEATURE_OPTIONS_PANEL) {
119 if (onOptionsItemSelected(item)) {
120 return true;
121 }
122
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)) {
128 return true;
129 }
130 }
131 }
132 }
133 }
134 return false;
135 }
136
137 public abstract boolean onCreateOptionsMenu(Menu menu);
138
139 public abstract boolean onPrepareOptionsMenu(Menu menu);
140
141 public abstract boolean onOptionsItemSelected(MenuItem item);
142
143 public abstract MenuInflater getSupportMenuInflater();
144 }