The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package com.android.internal.policy.impl; |
| 17 | |
Dianne Hackborn | 6014527 | 2011-01-11 23:45:09 -0800 | [diff] [blame] | 18 | import static android.view.View.MeasureSpec.AT_MOST; |
| 19 | import static android.view.View.MeasureSpec.EXACTLY; |
| 20 | import static android.view.View.MeasureSpec.getMode; |
Romain Guy | cc6828c | 2010-01-08 15:06:37 -0800 | [diff] [blame] | 21 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
svetoslavganov | 491293e | 2009-04-28 19:17:02 -0700 | [diff] [blame] | 22 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
| 23 | import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN; |
| 24 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; |
| 25 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; |
Dianne Hackborn | 4bf7bcf | 2009-08-09 17:23:00 -0700 | [diff] [blame] | 26 | import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER; |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 27 | import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH; |
svetoslavganov | 491293e | 2009-04-28 19:17:02 -0700 | [diff] [blame] | 28 | |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 29 | import com.android.internal.view.RootViewSurfaceTaker; |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 30 | import com.android.internal.view.StandaloneActionMode; |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 31 | import com.android.internal.view.menu.ActionMenuView; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 32 | import com.android.internal.view.menu.ContextMenuBuilder; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 33 | import com.android.internal.view.menu.ListMenuPresenter; |
| 34 | import com.android.internal.view.menu.IconMenuPresenter; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 35 | import com.android.internal.view.menu.MenuBuilder; |
| 36 | import com.android.internal.view.menu.MenuDialogHelper; |
Adam Powell | f0ad6e6 | 2011-01-10 17:14:06 -0800 | [diff] [blame] | 37 | import com.android.internal.view.menu.MenuItemImpl; |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 38 | import com.android.internal.view.menu.MenuPopupHelper; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 39 | import com.android.internal.view.menu.MenuView; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 40 | import com.android.internal.view.menu.MenuPresenter; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 41 | import com.android.internal.view.menu.SubMenuBuilder; |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 42 | import com.android.internal.widget.ActionBarContextView; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 43 | import com.android.internal.widget.ActionBarView; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 44 | |
| 45 | import android.app.KeyguardManager; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 46 | import android.content.Context; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 47 | import android.content.res.Configuration; |
| 48 | import android.content.res.TypedArray; |
| 49 | import android.graphics.Canvas; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 50 | import android.graphics.PixelFormat; |
| 51 | import android.graphics.Rect; |
| 52 | import android.graphics.drawable.Drawable; |
| 53 | import android.media.AudioManager; |
| 54 | import android.net.Uri; |
| 55 | import android.os.Bundle; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 56 | import android.os.Parcel; |
| 57 | import android.os.Parcelable; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 58 | import android.util.AndroidRuntimeException; |
Dianne Hackborn | 6014527 | 2011-01-11 23:45:09 -0800 | [diff] [blame] | 59 | import android.util.DisplayMetrics; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 60 | import android.util.EventLog; |
| 61 | import android.util.Log; |
| 62 | import android.util.SparseArray; |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 63 | import android.util.TypedValue; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 64 | import android.view.ActionMode; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 65 | import android.view.Gravity; |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 66 | import android.view.InputQueue; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 67 | import android.view.KeyCharacterMap; |
| 68 | import android.view.KeyEvent; |
| 69 | import android.view.LayoutInflater; |
| 70 | import android.view.Menu; |
| 71 | import android.view.MenuItem; |
| 72 | import android.view.MotionEvent; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 73 | import android.view.SurfaceHolder; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 74 | import android.view.View; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 75 | import android.view.ViewGroup; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 76 | import android.view.ViewManager; |
Adam Powell | 8e55263 | 2010-08-04 15:26:22 -0700 | [diff] [blame] | 77 | import android.view.ViewStub; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 78 | import android.view.Window; |
| 79 | import android.view.WindowManager; |
svetoslavganov | 491293e | 2009-04-28 19:17:02 -0700 | [diff] [blame] | 80 | import android.view.accessibility.AccessibilityEvent; |
| 81 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 82 | import android.view.animation.Animation; |
| 83 | import android.view.animation.AnimationUtils; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 84 | import android.widget.FrameLayout; |
| 85 | import android.widget.ImageView; |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 86 | import android.widget.PopupWindow; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 87 | import android.widget.ProgressBar; |
| 88 | import android.widget.TextView; |
| 89 | |
| 90 | /** |
| 91 | * Android-specific Window. |
| 92 | * <p> |
| 93 | * todo: need to pull the generic functionality out into a base class |
| 94 | * in android.widget. |
| 95 | */ |
| 96 | public class PhoneWindow extends Window implements MenuBuilder.Callback { |
svetoslavganov | 491293e | 2009-04-28 19:17:02 -0700 | [diff] [blame] | 97 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 98 | private final static String TAG = "PhoneWindow"; |
| 99 | |
| 100 | private final static boolean SWEEP_OPEN_MENU = false; |
| 101 | |
| 102 | /** |
| 103 | * Simple callback used by the context menu and its submenus. The options |
| 104 | * menu submenus do not use this (their behavior is more complex). |
| 105 | */ |
Dianne Hackborn | 6014527 | 2011-01-11 23:45:09 -0800 | [diff] [blame] | 106 | final DialogMenuCallback mContextMenuCallback = new DialogMenuCallback(FEATURE_CONTEXT_MENU); |
| 107 | |
| 108 | final TypedValue mMinWidthMajor = new TypedValue(); |
| 109 | final TypedValue mMinWidthMinor = new TypedValue(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 110 | |
| 111 | // This is the top-level view of the window, containing the window decor. |
| 112 | private DecorView mDecor; |
| 113 | |
| 114 | // This is the view in which the window contents are placed. It is either |
| 115 | // mDecor itself, or a child of mDecor where the contents go. |
| 116 | private ViewGroup mContentParent; |
| 117 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 118 | SurfaceHolder.Callback2 mTakeSurfaceCallback; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 119 | |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 120 | InputQueue.Callback mTakeInputQueueCallback; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 121 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 122 | private boolean mIsFloating; |
| 123 | |
| 124 | private LayoutInflater mLayoutInflater; |
| 125 | |
| 126 | private TextView mTitleView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 127 | |
| 128 | private ActionBarView mActionBar; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 129 | private ActionMenuPresenterCallback mActionMenuPresenterCallback; |
| 130 | private PanelMenuPresenterCallback mPanelMenuPresenterCallback; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 131 | |
| 132 | private DrawableFeatureState[] mDrawables; |
| 133 | |
| 134 | private PanelFeatureState[] mPanels; |
| 135 | |
| 136 | /** |
| 137 | * The panel that is prepared or opened (the most recent one if there are |
| 138 | * multiple panels). Shortcuts will go to this panel. It gets set in |
| 139 | * {@link #preparePanel} and cleared in {@link #closePanel}. |
| 140 | */ |
| 141 | private PanelFeatureState mPreparedPanel; |
| 142 | |
| 143 | /** |
| 144 | * The keycode that is currently held down (as a modifier) for chording. If |
| 145 | * this is 0, there is no key held down. |
| 146 | */ |
| 147 | private int mPanelChordingKey; |
| 148 | |
| 149 | private ImageView mLeftIconView; |
| 150 | |
| 151 | private ImageView mRightIconView; |
| 152 | |
| 153 | private ProgressBar mCircularProgressBar; |
| 154 | |
| 155 | private ProgressBar mHorizontalProgressBar; |
| 156 | |
| 157 | private int mBackgroundResource = 0; |
| 158 | |
| 159 | private Drawable mBackgroundDrawable; |
| 160 | |
| 161 | private int mFrameResource = 0; |
| 162 | |
| 163 | private int mTextColor = 0; |
| 164 | |
| 165 | private CharSequence mTitle = null; |
| 166 | |
| 167 | private int mTitleColor = 0; |
| 168 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 169 | private boolean mAlwaysReadCloseOnTouchAttr = false; |
| 170 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 171 | private ContextMenuBuilder mContextMenu; |
| 172 | private MenuDialogHelper mContextMenuHelper; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 173 | private boolean mClosingActionMenu; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 174 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 175 | private int mVolumeControlStreamType = AudioManager.USE_DEFAULT_STREAM_TYPE; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 176 | |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 177 | private AudioManager mAudioManager; |
| 178 | private KeyguardManager mKeyguardManager; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 179 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 180 | public PhoneWindow(Context context) { |
| 181 | super(context); |
| 182 | mLayoutInflater = LayoutInflater.from(context); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public final void setContainer(Window container) { |
| 187 | super.setContainer(container); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public boolean requestFeature(int featureId) { |
| 192 | if (mContentParent != null) { |
| 193 | throw new AndroidRuntimeException("requestFeature() must be called before adding content"); |
| 194 | } |
| 195 | final int features = getFeatures(); |
| 196 | if ((features != DEFAULT_FEATURES) && (featureId == FEATURE_CUSTOM_TITLE)) { |
| 197 | |
| 198 | /* Another feature is enabled and the user is trying to enable the custom title feature */ |
| 199 | throw new AndroidRuntimeException("You cannot combine custom titles with other title features"); |
| 200 | } |
Adam Powell | 8f68f4f | 2011-01-16 19:55:21 -0800 | [diff] [blame] | 201 | if (((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) && |
| 202 | (featureId != FEATURE_CUSTOM_TITLE) && (featureId != FEATURE_ACTION_MODE_OVERLAY)) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 203 | |
| 204 | /* Custom title feature is enabled and the user is trying to enable another feature */ |
| 205 | throw new AndroidRuntimeException("You cannot combine custom titles with other title features"); |
| 206 | } |
Adam Powell | f4a6ec4 | 2010-08-24 14:18:10 -0700 | [diff] [blame] | 207 | if ((features & (1 << FEATURE_NO_TITLE)) != 0 && featureId == FEATURE_ACTION_BAR) { |
| 208 | return false; // Ignore. No title dominates. |
| 209 | } |
| 210 | if ((features & (1 << FEATURE_ACTION_BAR)) != 0 && featureId == FEATURE_NO_TITLE) { |
| 211 | // Remove the action bar feature if we have no title. No title dominates. |
| 212 | removeFeature(FEATURE_ACTION_BAR); |
| 213 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 214 | return super.requestFeature(featureId); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void setContentView(int layoutResID) { |
| 219 | if (mContentParent == null) { |
| 220 | installDecor(); |
| 221 | } else { |
| 222 | mContentParent.removeAllViews(); |
| 223 | } |
| 224 | mLayoutInflater.inflate(layoutResID, mContentParent); |
| 225 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 226 | if (cb != null && !isDestroyed()) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 227 | cb.onContentChanged(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void setContentView(View view) { |
Romain Guy | cc6828c | 2010-01-08 15:06:37 -0800 | [diff] [blame] | 233 | setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 238 | if (mContentParent == null) { |
| 239 | installDecor(); |
| 240 | } else { |
| 241 | mContentParent.removeAllViews(); |
| 242 | } |
| 243 | mContentParent.addView(view, params); |
| 244 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 245 | if (cb != null && !isDestroyed()) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 246 | cb.onContentChanged(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void addContentView(View view, ViewGroup.LayoutParams params) { |
| 252 | if (mContentParent == null) { |
| 253 | installDecor(); |
| 254 | } |
| 255 | mContentParent.addView(view, params); |
| 256 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 257 | if (cb != null && !isDestroyed()) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 258 | cb.onContentChanged(); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public View getCurrentFocus() { |
| 264 | return mDecor != null ? mDecor.findFocus() : null; |
| 265 | } |
| 266 | |
| 267 | @Override |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 268 | public void takeSurface(SurfaceHolder.Callback2 callback) { |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 269 | mTakeSurfaceCallback = callback; |
| 270 | } |
| 271 | |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 272 | public void takeInputQueue(InputQueue.Callback callback) { |
| 273 | mTakeInputQueueCallback = callback; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 276 | @Override |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 277 | public boolean isFloating() { |
| 278 | return mIsFloating; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Return a LayoutInflater instance that can be used to inflate XML view layout |
| 283 | * resources for use in this Window. |
| 284 | * |
| 285 | * @return LayoutInflater The shared LayoutInflater. |
| 286 | */ |
| 287 | @Override |
| 288 | public LayoutInflater getLayoutInflater() { |
| 289 | return mLayoutInflater; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void setTitle(CharSequence title) { |
| 294 | if (mTitleView != null) { |
| 295 | mTitleView.setText(title); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 296 | } else if (mActionBar != null) { |
Adam Powell | e92ea34 | 2010-07-14 14:45:50 -0700 | [diff] [blame] | 297 | mActionBar.setWindowTitle(title); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 298 | } |
| 299 | mTitle = title; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void setTitleColor(int textColor) { |
| 304 | if (mTitleView != null) { |
| 305 | mTitleView.setTextColor(textColor); |
| 306 | } |
| 307 | mTitleColor = textColor; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Prepares the panel to either be opened or chorded. This creates the Menu |
| 312 | * instance for the panel and populates it via the Activity callbacks. |
| 313 | * |
| 314 | * @param st The panel state to prepare. |
| 315 | * @param event The event that triggered the preparing of the panel. |
| 316 | * @return Whether the panel was prepared. If the panel should not be shown, |
| 317 | * returns false. |
| 318 | */ |
| 319 | public final boolean preparePanel(PanelFeatureState st, KeyEvent event) { |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 320 | if (isDestroyed()) { |
| 321 | return false; |
| 322 | } |
| 323 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 324 | // Already prepared (isPrepared will be reset to false later) |
| 325 | if (st.isPrepared) |
| 326 | return true; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 327 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 328 | if ((mPreparedPanel != null) && (mPreparedPanel != st)) { |
| 329 | // Another Panel is prepared and possibly open, so close it |
| 330 | closePanel(mPreparedPanel, false); |
| 331 | } |
| 332 | |
| 333 | final Callback cb = getCallback(); |
| 334 | |
| 335 | if (cb != null) { |
| 336 | st.createdPanelView = cb.onCreatePanelView(st.featureId); |
| 337 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 338 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 339 | if (st.createdPanelView == null) { |
| 340 | // Init the panel state's menu--return false if init failed |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 341 | if (st.menu == null || st.refreshMenuContent) { |
| 342 | if (st.menu == null) { |
| 343 | if (!initializePanelMenu(st) || (st.menu == null)) { |
| 344 | return false; |
| 345 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 346 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 347 | |
| 348 | // Call callback, and return if it doesn't want to display menu. |
| 349 | |
| 350 | // Creating the panel menu will involve a lot of manipulation; |
| 351 | // don't dispatch change events to presenters until we're done. |
| 352 | st.menu.stopDispatchingItemsChanged(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 353 | if ((cb == null) || !cb.onCreatePanelMenu(st.featureId, st.menu)) { |
| 354 | // Ditch the menu created above |
| 355 | st.menu = null; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 356 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 357 | return false; |
| 358 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 359 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 360 | st.refreshMenuContent = false; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 361 | |
| 362 | if (mActionBar != null) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 363 | if (mActionMenuPresenterCallback == null) { |
| 364 | mActionMenuPresenterCallback = new ActionMenuPresenterCallback(); |
| 365 | } |
| 366 | mActionBar.setMenu(st.menu, mActionMenuPresenterCallback); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 367 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 368 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 369 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 370 | // Callback and return if the callback does not want to show the menu |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 371 | |
| 372 | // Preparing the panel menu can involve a lot of manipulation; |
| 373 | // don't dispatch change events to presenters until we're done. |
| 374 | st.menu.stopDispatchingItemsChanged(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 375 | if (!cb.onPreparePanel(st.featureId, st.createdPanelView, st.menu)) { |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 376 | st.menu.startDispatchingItemsChanged(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 377 | return false; |
| 378 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 379 | st.menu.startDispatchingItemsChanged(); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 380 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 381 | // Set the proper keymap |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 382 | KeyCharacterMap kmap = KeyCharacterMap.load( |
| 383 | event != null ? event.getDeviceId() : KeyCharacterMap.VIRTUAL_KEYBOARD); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 384 | st.qwertyMode = kmap.getKeyboardType() != KeyCharacterMap.NUMERIC; |
| 385 | st.menu.setQwertyMode(st.qwertyMode); |
| 386 | } |
| 387 | |
| 388 | // Set other state |
| 389 | st.isPrepared = true; |
| 390 | st.isHandled = false; |
| 391 | mPreparedPanel = st; |
| 392 | |
| 393 | return true; |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public void onConfigurationChanged(Configuration newConfig) { |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 398 | // Action bars handle their own menu state |
| 399 | if (mActionBar == null) { |
| 400 | PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false); |
| 401 | if ((st != null) && (st.menu != null)) { |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 402 | if (st.isOpen) { |
| 403 | // Freeze state |
| 404 | final Bundle state = new Bundle(); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 405 | if (st.iconMenuPresenter != null) { |
| 406 | st.iconMenuPresenter.saveHierarchyState(state); |
| 407 | } |
| 408 | if (st.expandedMenuPresenter != null) { |
| 409 | st.expandedMenuPresenter.saveHierarchyState(state); |
| 410 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 411 | |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 412 | // Remove the menu views since they need to be recreated |
| 413 | // according to the new configuration |
| 414 | clearMenuViews(st); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 415 | |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 416 | // Re-open the same menu |
| 417 | reopenMenu(false); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 418 | |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 419 | // Restore state |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 420 | if (st.iconMenuPresenter != null) { |
| 421 | st.iconMenuPresenter.restoreHierarchyState(state); |
| 422 | } |
| 423 | if (st.expandedMenuPresenter != null) { |
| 424 | st.expandedMenuPresenter.restoreHierarchyState(state); |
| 425 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 426 | |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 427 | } else { |
| 428 | // Clear menu views so on next menu opening, it will use |
| 429 | // the proper layout |
| 430 | clearMenuViews(st); |
| 431 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | private static void clearMenuViews(PanelFeatureState st) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 437 | // This can be called on config changes, so we should make sure |
| 438 | // the views will be reconstructed based on the new orientation, etc. |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 439 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 440 | // Allow the callback to create a new panel view |
| 441 | st.createdPanelView = null; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 442 | |
| 443 | // Causes the decor view to be recreated |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 444 | st.refreshDecorView = true; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 445 | |
| 446 | st.clearMenuPresenters(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 447 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 448 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 449 | @Override |
| 450 | public final void openPanel(int featureId, KeyEvent event) { |
Adam Powell | f75eeb2 | 2010-08-10 15:59:40 -0700 | [diff] [blame] | 451 | if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null && |
| 452 | mActionBar.isOverflowReserved()) { |
Adam Powell | ef5a440 | 2011-02-10 17:30:36 -0800 | [diff] [blame] | 453 | if (mActionBar.getVisibility() == View.VISIBLE) { |
| 454 | // Invalidate the options menu, we want a prepare event that the app can respond to. |
| 455 | invalidatePanelMenu(FEATURE_OPTIONS_PANEL); |
| 456 | mActionBar.showOverflowMenu(); |
| 457 | } |
Adam Powell | f75eeb2 | 2010-08-10 15:59:40 -0700 | [diff] [blame] | 458 | } else { |
| 459 | openPanel(getPanelState(featureId, true), event); |
| 460 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | private void openPanel(PanelFeatureState st, KeyEvent event) { |
| 464 | // System.out.println("Open panel: isOpen=" + st.isOpen); |
| 465 | |
| 466 | // Already open, return |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 467 | if (st.isOpen || isDestroyed()) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 468 | return; |
| 469 | } |
| 470 | |
Adam Powell | 0d69fae | 2011-01-08 15:07:08 -0800 | [diff] [blame] | 471 | // Don't open an options panel for honeycomb apps on xlarge devices. |
| 472 | // (The app should be using an action bar for menu items.) |
| 473 | if (st.featureId == FEATURE_OPTIONS_PANEL) { |
| 474 | Context context = getContext(); |
| 475 | Configuration config = context.getResources().getConfiguration(); |
| 476 | boolean isXLarge = (config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == |
| 477 | Configuration.SCREENLAYOUT_SIZE_XLARGE; |
| 478 | boolean isHoneycombApp = context.getApplicationInfo().targetSdkVersion >= |
| 479 | android.os.Build.VERSION_CODES.HONEYCOMB; |
| 480 | |
| 481 | if (isXLarge && isHoneycombApp) { |
| 482 | return; |
| 483 | } |
| 484 | } |
| 485 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 486 | Callback cb = getCallback(); |
| 487 | if ((cb != null) && (!cb.onMenuOpened(st.featureId, st.menu))) { |
| 488 | // Callback doesn't want the menu to open, reset any state |
| 489 | closePanel(st, true); |
| 490 | return; |
| 491 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 492 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 493 | final WindowManager wm = getWindowManager(); |
| 494 | if (wm == null) { |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | // Prepare panel (should have been done before, but just in case) |
| 499 | if (!preparePanel(st, event)) { |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | if (st.decorView == null || st.refreshDecorView) { |
| 504 | if (st.decorView == null) { |
| 505 | // Initialize the panel decor, this will populate st.decorView |
| 506 | if (!initializePanelDecor(st) || (st.decorView == null)) |
| 507 | return; |
| 508 | } else if (st.refreshDecorView && (st.decorView.getChildCount() > 0)) { |
| 509 | // Decor needs refreshing, so remove its views |
| 510 | st.decorView.removeAllViews(); |
| 511 | } |
| 512 | |
| 513 | // This will populate st.shownPanelView |
Adam Powell | 526b931 | 2011-04-22 15:42:05 -0700 | [diff] [blame] | 514 | if (!initializePanelContent(st) || !st.hasPanelItems()) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 515 | return; |
| 516 | } |
| 517 | |
| 518 | ViewGroup.LayoutParams lp = st.shownPanelView.getLayoutParams(); |
| 519 | if (lp == null) { |
| 520 | lp = new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT); |
| 521 | } |
| 522 | |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 523 | int backgroundResId; |
Romain Guy | cc6828c | 2010-01-08 15:06:37 -0800 | [diff] [blame] | 524 | if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 525 | // If the contents is fill parent for the width, set the |
| 526 | // corresponding background |
| 527 | backgroundResId = st.fullBackground; |
| 528 | } else { |
| 529 | // Otherwise, set the normal panel background |
| 530 | backgroundResId = st.background; |
| 531 | } |
| 532 | st.decorView.setWindowBackground(getContext().getResources().getDrawable( |
| 533 | backgroundResId)); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 534 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 535 | |
| 536 | st.decorView.addView(st.shownPanelView, lp); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 537 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 538 | /* |
| 539 | * Give focus to the view, if it or one of its children does not |
| 540 | * already have it. |
| 541 | */ |
| 542 | if (!st.shownPanelView.hasFocus()) { |
| 543 | st.shownPanelView.requestFocus(); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | st.isOpen = true; |
| 548 | st.isHandled = false; |
| 549 | |
| 550 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 551 | WRAP_CONTENT, WRAP_CONTENT, |
Dianne Hackborn | d371510 | 2009-09-15 19:28:03 -0700 | [diff] [blame] | 552 | st.x, st.y, WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG, |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 553 | WindowManager.LayoutParams.FLAG_DITHER |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 554 | | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM |
| 555 | | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH, |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 556 | st.decorView.mDefaultOpacity); |
| 557 | |
| 558 | lp.gravity = st.gravity; |
| 559 | lp.windowAnimations = st.windowAnimations; |
svetoslavganov | 491293e | 2009-04-28 19:17:02 -0700 | [diff] [blame] | 560 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 561 | wm.addView(st.decorView, lp); |
| 562 | // Log.v(TAG, "Adding main menu to window manager."); |
| 563 | } |
| 564 | |
| 565 | @Override |
| 566 | public final void closePanel(int featureId) { |
Adam Powell | f75eeb2 | 2010-08-10 15:59:40 -0700 | [diff] [blame] | 567 | if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null && |
| 568 | mActionBar.isOverflowReserved()) { |
| 569 | mActionBar.hideOverflowMenu(); |
| 570 | } else if (featureId == FEATURE_CONTEXT_MENU) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 571 | closeContextMenu(); |
| 572 | } else { |
| 573 | closePanel(getPanelState(featureId, true), true); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Closes the given panel. |
| 579 | * |
| 580 | * @param st The panel to be closed. |
| 581 | * @param doCallback Whether to notify the callback that the panel was |
| 582 | * closed. If the panel is in the process of re-opening or |
| 583 | * opening another panel (e.g., menu opening a sub menu), the |
| 584 | * callback should not happen and this variable should be false. |
| 585 | * In addition, this method internally will only perform the |
| 586 | * callback if the panel is open. |
| 587 | */ |
| 588 | public final void closePanel(PanelFeatureState st, boolean doCallback) { |
| 589 | // System.out.println("Close panel: isOpen=" + st.isOpen); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 590 | if (doCallback && st.featureId == FEATURE_OPTIONS_PANEL && |
| 591 | mActionBar != null && mActionBar.isOverflowMenuShowing()) { |
| 592 | checkCloseActionMenu(st.menu); |
| 593 | return; |
| 594 | } |
| 595 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 596 | final ViewManager wm = getWindowManager(); |
| 597 | if ((wm != null) && st.isOpen) { |
| 598 | if (st.decorView != null) { |
| 599 | wm.removeView(st.decorView); |
| 600 | // Log.v(TAG, "Removing main menu from window manager."); |
| 601 | } |
| 602 | |
| 603 | if (doCallback) { |
| 604 | callOnPanelClosed(st.featureId, st, null); |
| 605 | } |
| 606 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 607 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 608 | st.isPrepared = false; |
| 609 | st.isHandled = false; |
| 610 | st.isOpen = false; |
| 611 | |
| 612 | // This view is no longer shown, so null it out |
| 613 | st.shownPanelView = null; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 614 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 615 | if (st.isInExpandedMode) { |
| 616 | // Next time the menu opens, it should not be in expanded mode, so |
| 617 | // force a refresh of the decor |
| 618 | st.refreshDecorView = true; |
| 619 | st.isInExpandedMode = false; |
| 620 | } |
| 621 | |
| 622 | if (mPreparedPanel == st) { |
| 623 | mPreparedPanel = null; |
| 624 | mPanelChordingKey = 0; |
| 625 | } |
| 626 | } |
| 627 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 628 | private void checkCloseActionMenu(Menu menu) { |
| 629 | if (mClosingActionMenu) { |
| 630 | return; |
| 631 | } |
| 632 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 633 | mClosingActionMenu = true; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 634 | mActionBar.dismissPopupMenus(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 635 | Callback cb = getCallback(); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 636 | if (cb != null && !isDestroyed()) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 637 | cb.onPanelClosed(FEATURE_ACTION_BAR, menu); |
| 638 | } |
| 639 | mClosingActionMenu = false; |
| 640 | } |
| 641 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 642 | @Override |
| 643 | public final void togglePanel(int featureId, KeyEvent event) { |
| 644 | PanelFeatureState st = getPanelState(featureId, true); |
| 645 | if (st.isOpen) { |
| 646 | closePanel(st, true); |
| 647 | } else { |
| 648 | openPanel(st, event); |
| 649 | } |
| 650 | } |
| 651 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 652 | @Override |
| 653 | public void invalidatePanelMenu(int featureId) { |
| 654 | PanelFeatureState st = getPanelState(featureId, true); |
| 655 | if (st.menu != null) { |
| 656 | st.menu.clear(); |
| 657 | } |
| 658 | st.refreshMenuContent = true; |
| 659 | st.refreshDecorView = true; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 660 | |
| 661 | // Prepare the options panel if we have an action bar |
| 662 | if ((featureId == FEATURE_ACTION_BAR || featureId == FEATURE_OPTIONS_PANEL) |
| 663 | && mActionBar != null) { |
| 664 | st = getPanelState(Window.FEATURE_OPTIONS_PANEL, false); |
| 665 | if (st != null) { |
| 666 | st.isPrepared = false; |
| 667 | preparePanel(st, null); |
| 668 | } |
| 669 | } |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 670 | } |
| 671 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 672 | /** |
| 673 | * Called when the panel key is pushed down. |
| 674 | * @param featureId The feature ID of the relevant panel (defaults to FEATURE_OPTIONS_PANEL}. |
| 675 | * @param event The key event. |
| 676 | * @return Whether the key was handled. |
| 677 | */ |
| 678 | public final boolean onKeyDownPanel(int featureId, KeyEvent event) { |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 679 | final int keyCode = event.getKeyCode(); |
| 680 | |
| 681 | if (event.getRepeatCount() == 0) { |
| 682 | // The panel key was pushed, so set the chording key |
| 683 | mPanelChordingKey = keyCode; |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 684 | |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 685 | PanelFeatureState st = getPanelState(featureId, true); |
| 686 | if (!st.isOpen) { |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 687 | return preparePanel(st, event); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 688 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * Called when the panel key is released. |
| 696 | * @param featureId The feature ID of the relevant panel (defaults to FEATURE_OPTIONS_PANEL}. |
| 697 | * @param event The key event. |
| 698 | */ |
| 699 | public final void onKeyUpPanel(int featureId, KeyEvent event) { |
| 700 | // The panel key was released, so clear the chording key |
| 701 | if (mPanelChordingKey != 0) { |
| 702 | mPanelChordingKey = 0; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 703 | |
Dianne Hackborn | 0041e97 | 2009-07-24 17:14:43 -0700 | [diff] [blame] | 704 | if (event.isCanceled()) { |
| 705 | return; |
| 706 | } |
| 707 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 708 | boolean playSoundEffect = false; |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 709 | final PanelFeatureState st = getPanelState(featureId, true); |
Adam Powell | 266b100 | 2010-08-16 15:02:07 -0700 | [diff] [blame] | 710 | if (featureId == FEATURE_OPTIONS_PANEL && mActionBar != null && |
| 711 | mActionBar.isOverflowReserved()) { |
Adam Powell | ef5a440 | 2011-02-10 17:30:36 -0800 | [diff] [blame] | 712 | if (mActionBar.getVisibility() == View.VISIBLE) { |
| 713 | if (!mActionBar.isOverflowMenuShowing()) { |
| 714 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 715 | if (cb != null && !isDestroyed() && |
Adam Powell | ef5a440 | 2011-02-10 17:30:36 -0800 | [diff] [blame] | 716 | cb.onPreparePanel(featureId, st.createdPanelView, st.menu)) { |
| 717 | playSoundEffect = mActionBar.showOverflowMenu(); |
| 718 | } |
| 719 | } else { |
| 720 | playSoundEffect = mActionBar.hideOverflowMenu(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | } else { |
| 724 | if (st.isOpen || st.isHandled) { |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 725 | |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 726 | // Play the sound effect if the user closed an open menu (and not if |
| 727 | // they just released a menu shortcut) |
| 728 | playSoundEffect = st.isOpen; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 729 | |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 730 | // Close menu |
| 731 | closePanel(st, true); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 732 | |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 733 | } else if (st.isPrepared) { |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 734 | |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 735 | // Write 'menu opened' to event log |
| 736 | EventLog.writeEvent(50001, 0); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 737 | |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 738 | // Show menu |
| 739 | openPanel(st, event); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 740 | |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 741 | playSoundEffect = true; |
| 742 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 743 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 744 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 745 | if (playSoundEffect) { |
| 746 | AudioManager audioManager = (AudioManager) getContext().getSystemService( |
| 747 | Context.AUDIO_SERVICE); |
| 748 | if (audioManager != null) { |
| 749 | audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK); |
| 750 | } else { |
| 751 | Log.w(TAG, "Couldn't get audio manager"); |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | @Override |
| 758 | public final void closeAllPanels() { |
| 759 | final ViewManager wm = getWindowManager(); |
| 760 | if (wm == null) { |
| 761 | return; |
| 762 | } |
| 763 | |
| 764 | final PanelFeatureState[] panels = mPanels; |
| 765 | final int N = panels != null ? panels.length : 0; |
| 766 | for (int i = 0; i < N; i++) { |
| 767 | final PanelFeatureState panel = panels[i]; |
| 768 | if (panel != null) { |
| 769 | closePanel(panel, true); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | closeContextMenu(); |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * Closes the context menu. This notifies the menu logic of the close, along |
| 778 | * with dismissing it from the UI. |
| 779 | */ |
| 780 | private synchronized void closeContextMenu() { |
| 781 | if (mContextMenu != null) { |
| 782 | mContextMenu.close(); |
| 783 | dismissContextMenu(); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /** |
| 788 | * Dismisses just the context menu UI. To close the context menu, use |
| 789 | * {@link #closeContextMenu()}. |
| 790 | */ |
| 791 | private synchronized void dismissContextMenu() { |
| 792 | mContextMenu = null; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 793 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 794 | if (mContextMenuHelper != null) { |
| 795 | mContextMenuHelper.dismiss(); |
| 796 | mContextMenuHelper = null; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | @Override |
| 801 | public boolean performPanelShortcut(int featureId, int keyCode, KeyEvent event, int flags) { |
| 802 | return performPanelShortcut(getPanelState(featureId, true), keyCode, event, flags); |
| 803 | } |
| 804 | |
| 805 | private boolean performPanelShortcut(PanelFeatureState st, int keyCode, KeyEvent event, |
| 806 | int flags) { |
| 807 | if (event.isSystem() || (st == null)) { |
| 808 | return false; |
| 809 | } |
| 810 | |
| 811 | boolean handled = false; |
| 812 | |
| 813 | // Only try to perform menu shortcuts if preparePanel returned true (possible false |
| 814 | // return value from application not wanting to show the menu). |
| 815 | if ((st.isPrepared || preparePanel(st, event)) && st.menu != null) { |
| 816 | // The menu is prepared now, perform the shortcut on it |
| 817 | handled = st.menu.performShortcut(keyCode, event, flags); |
| 818 | } |
| 819 | |
| 820 | if (handled) { |
| 821 | // Mark as handled |
| 822 | st.isHandled = true; |
| 823 | |
| 824 | if ((flags & Menu.FLAG_PERFORM_NO_CLOSE) == 0) { |
| 825 | closePanel(st, true); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return handled; |
| 830 | } |
| 831 | |
| 832 | @Override |
| 833 | public boolean performPanelIdentifierAction(int featureId, int id, int flags) { |
| 834 | |
| 835 | PanelFeatureState st = getPanelState(featureId, true); |
| 836 | if (!preparePanel(st, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU))) { |
| 837 | return false; |
| 838 | } |
| 839 | if (st.menu == null) { |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | boolean res = st.menu.performIdentifierAction(id, flags); |
| 844 | |
| 845 | closePanel(st, true); |
| 846 | |
| 847 | return res; |
| 848 | } |
| 849 | |
| 850 | public PanelFeatureState findMenuPanel(Menu menu) { |
| 851 | final PanelFeatureState[] panels = mPanels; |
| 852 | final int N = panels != null ? panels.length : 0; |
| 853 | for (int i = 0; i < N; i++) { |
| 854 | final PanelFeatureState panel = panels[i]; |
| 855 | if (panel != null && panel.menu == menu) { |
| 856 | return panel; |
| 857 | } |
| 858 | } |
| 859 | return null; |
| 860 | } |
| 861 | |
| 862 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
| 863 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 864 | if (cb != null && !isDestroyed()) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 865 | final PanelFeatureState panel = findMenuPanel(menu.getRootMenu()); |
| 866 | if (panel != null) { |
| 867 | return cb.onMenuItemSelected(panel.featureId, item); |
| 868 | } |
| 869 | } |
| 870 | return false; |
| 871 | } |
| 872 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 873 | public void onMenuModeChange(MenuBuilder menu) { |
| 874 | reopenMenu(true); |
| 875 | } |
| 876 | |
| 877 | private void reopenMenu(boolean toggleMenuMode) { |
Adam Powell | 25ef303 | 2011-03-18 16:08:38 -0700 | [diff] [blame] | 878 | if (mActionBar != null && mActionBar.isOverflowReserved()) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 879 | final Callback cb = getCallback(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 880 | if (!mActionBar.isOverflowMenuShowing() || !toggleMenuMode) { |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 881 | if (cb != null && !isDestroyed() && mActionBar.getVisibility() == View.VISIBLE) { |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 882 | final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true); |
| 883 | if (cb.onPreparePanel(FEATURE_OPTIONS_PANEL, st.createdPanelView, st.menu)) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 884 | cb.onMenuOpened(FEATURE_ACTION_BAR, st.menu); |
| 885 | mActionBar.openOverflowMenu(); |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | } else { |
| 889 | mActionBar.hideOverflowMenu(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 890 | if (cb != null && !isDestroyed()) { |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 891 | final PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true); |
| 892 | cb.onPanelClosed(FEATURE_ACTION_BAR, st.menu); |
| 893 | } |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 894 | } |
| 895 | return; |
| 896 | } |
| 897 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 898 | PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true); |
| 899 | |
| 900 | // Save the future expanded mode state since closePanel will reset it |
| 901 | boolean newExpandedMode = toggleMenuMode ? !st.isInExpandedMode : st.isInExpandedMode; |
| 902 | |
| 903 | st.refreshDecorView = true; |
| 904 | closePanel(st, false); |
| 905 | |
| 906 | // Set the expanded mode state |
| 907 | st.isInExpandedMode = newExpandedMode; |
| 908 | |
| 909 | openPanel(st, null); |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Initializes the menu associated with the given panel feature state. You |
| 914 | * must at the very least set PanelFeatureState.menu to the Menu to be |
| 915 | * associated with the given panel state. The default implementation creates |
| 916 | * a new menu for the panel state. |
| 917 | * |
| 918 | * @param st The panel whose menu is being initialized. |
| 919 | * @return Whether the initialization was successful. |
| 920 | */ |
| 921 | protected boolean initializePanelMenu(final PanelFeatureState st) { |
| 922 | final MenuBuilder menu = new MenuBuilder(getContext()); |
| 923 | |
| 924 | menu.setCallback(this); |
| 925 | st.setMenu(menu); |
| 926 | |
| 927 | return true; |
| 928 | } |
| 929 | |
| 930 | /** |
| 931 | * Perform initial setup of a panel. This should at the very least set the |
| 932 | * style information in the PanelFeatureState and must set |
| 933 | * PanelFeatureState.decor to the panel's window decor view. |
| 934 | * |
| 935 | * @param st The panel being initialized. |
| 936 | */ |
| 937 | protected boolean initializePanelDecor(PanelFeatureState st) { |
| 938 | st.decorView = new DecorView(getContext(), st.featureId); |
| 939 | st.gravity = Gravity.CENTER | Gravity.BOTTOM; |
| 940 | st.setStyle(getContext()); |
| 941 | |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * Initializes the panel associated with the panel feature state. You must |
| 947 | * at the very least set PanelFeatureState.panel to the View implementing |
| 948 | * its contents. The default implementation gets the panel from the menu. |
| 949 | * |
| 950 | * @param st The panel state being initialized. |
| 951 | * @return Whether the initialization was successful. |
| 952 | */ |
| 953 | protected boolean initializePanelContent(PanelFeatureState st) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 954 | if (st.createdPanelView != null) { |
| 955 | st.shownPanelView = st.createdPanelView; |
| 956 | return true; |
| 957 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 958 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 959 | if (st.menu == null) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 960 | return false; |
| 961 | } |
| 962 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 963 | if (mPanelMenuPresenterCallback == null) { |
| 964 | mPanelMenuPresenterCallback = new PanelMenuPresenterCallback(); |
| 965 | } |
| 966 | |
| 967 | MenuView menuView = st.isInExpandedMode |
| 968 | ? st.getExpandedMenuView(mPanelMenuPresenterCallback) |
| 969 | : st.getIconMenuView(mPanelMenuPresenterCallback); |
| 970 | |
| 971 | st.shownPanelView = (View) menuView; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 972 | |
| 973 | if (st.shownPanelView != null) { |
| 974 | // Use the menu View's default animations if it has any |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 975 | final int defaultAnimations = menuView.getWindowAnimations(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 976 | if (defaultAnimations != 0) { |
| 977 | st.windowAnimations = defaultAnimations; |
| 978 | } |
| 979 | return true; |
| 980 | } else { |
| 981 | return false; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | @Override |
| 986 | public boolean performContextMenuIdentifierAction(int id, int flags) { |
| 987 | return (mContextMenu != null) ? mContextMenu.performIdentifierAction(id, flags) : false; |
| 988 | } |
| 989 | |
| 990 | @Override |
| 991 | public final void setBackgroundDrawable(Drawable drawable) { |
Dianne Hackborn | a7c176c | 2009-06-22 20:56:57 -0700 | [diff] [blame] | 992 | if (drawable != mBackgroundDrawable || mBackgroundResource != 0) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 993 | mBackgroundResource = 0; |
| 994 | mBackgroundDrawable = drawable; |
| 995 | if (mDecor != null) { |
| 996 | mDecor.setWindowBackground(drawable); |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | @Override |
| 1002 | public final void setFeatureDrawableResource(int featureId, int resId) { |
| 1003 | if (resId != 0) { |
| 1004 | DrawableFeatureState st = getDrawableState(featureId, true); |
| 1005 | if (st.resid != resId) { |
| 1006 | st.resid = resId; |
| 1007 | st.uri = null; |
| 1008 | st.local = getContext().getResources().getDrawable(resId); |
| 1009 | updateDrawable(featureId, st, false); |
| 1010 | } |
| 1011 | } else { |
| 1012 | setFeatureDrawable(featureId, null); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | @Override |
| 1017 | public final void setFeatureDrawableUri(int featureId, Uri uri) { |
| 1018 | if (uri != null) { |
| 1019 | DrawableFeatureState st = getDrawableState(featureId, true); |
| 1020 | if (st.uri == null || !st.uri.equals(uri)) { |
| 1021 | st.resid = 0; |
| 1022 | st.uri = uri; |
| 1023 | st.local = loadImageURI(uri); |
| 1024 | updateDrawable(featureId, st, false); |
| 1025 | } |
| 1026 | } else { |
| 1027 | setFeatureDrawable(featureId, null); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | @Override |
| 1032 | public final void setFeatureDrawable(int featureId, Drawable drawable) { |
| 1033 | DrawableFeatureState st = getDrawableState(featureId, true); |
| 1034 | st.resid = 0; |
| 1035 | st.uri = null; |
| 1036 | if (st.local != drawable) { |
| 1037 | st.local = drawable; |
| 1038 | updateDrawable(featureId, st, false); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | @Override |
| 1043 | public void setFeatureDrawableAlpha(int featureId, int alpha) { |
| 1044 | DrawableFeatureState st = getDrawableState(featureId, true); |
| 1045 | if (st.alpha != alpha) { |
| 1046 | st.alpha = alpha; |
| 1047 | updateDrawable(featureId, st, false); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | protected final void setFeatureDefaultDrawable(int featureId, Drawable drawable) { |
| 1052 | DrawableFeatureState st = getDrawableState(featureId, true); |
| 1053 | if (st.def != drawable) { |
| 1054 | st.def = drawable; |
| 1055 | updateDrawable(featureId, st, false); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | @Override |
| 1060 | public final void setFeatureInt(int featureId, int value) { |
| 1061 | // XXX Should do more management (as with drawable features) to |
| 1062 | // deal with interactions between multiple window policies. |
| 1063 | updateInt(featureId, value, false); |
| 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * Update the state of a drawable feature. This should be called, for every |
| 1068 | * drawable feature supported, as part of onActive(), to make sure that the |
| 1069 | * contents of a containing window is properly updated. |
| 1070 | * |
| 1071 | * @see #onActive |
| 1072 | * @param featureId The desired drawable feature to change. |
| 1073 | * @param fromActive Always true when called from onActive(). |
| 1074 | */ |
| 1075 | protected final void updateDrawable(int featureId, boolean fromActive) { |
| 1076 | final DrawableFeatureState st = getDrawableState(featureId, false); |
| 1077 | if (st != null) { |
| 1078 | updateDrawable(featureId, st, fromActive); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | /** |
| 1083 | * Called when a Drawable feature changes, for the window to update its |
| 1084 | * graphics. |
| 1085 | * |
| 1086 | * @param featureId The feature being changed. |
| 1087 | * @param drawable The new Drawable to show, or null if none. |
| 1088 | * @param alpha The new alpha blending of the Drawable. |
| 1089 | */ |
| 1090 | protected void onDrawableChanged(int featureId, Drawable drawable, int alpha) { |
| 1091 | ImageView view; |
| 1092 | if (featureId == FEATURE_LEFT_ICON) { |
| 1093 | view = getLeftIconView(); |
| 1094 | } else if (featureId == FEATURE_RIGHT_ICON) { |
| 1095 | view = getRightIconView(); |
| 1096 | } else { |
| 1097 | return; |
| 1098 | } |
| 1099 | |
| 1100 | if (drawable != null) { |
| 1101 | drawable.setAlpha(alpha); |
| 1102 | view.setImageDrawable(drawable); |
| 1103 | view.setVisibility(View.VISIBLE); |
| 1104 | } else { |
| 1105 | view.setVisibility(View.GONE); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * Called when an int feature changes, for the window to update its |
| 1111 | * graphics. |
| 1112 | * |
| 1113 | * @param featureId The feature being changed. |
| 1114 | * @param value The new integer value. |
| 1115 | */ |
| 1116 | protected void onIntChanged(int featureId, int value) { |
| 1117 | if (featureId == FEATURE_PROGRESS || featureId == FEATURE_INDETERMINATE_PROGRESS) { |
| 1118 | updateProgressBars(value); |
| 1119 | } else if (featureId == FEATURE_CUSTOM_TITLE) { |
| 1120 | FrameLayout titleContainer = (FrameLayout) findViewById(com.android.internal.R.id.title_container); |
| 1121 | if (titleContainer != null) { |
| 1122 | mLayoutInflater.inflate(value, titleContainer); |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * Updates the progress bars that are shown in the title bar. |
| 1129 | * |
| 1130 | * @param value Can be one of {@link Window#PROGRESS_VISIBILITY_ON}, |
| 1131 | * {@link Window#PROGRESS_VISIBILITY_OFF}, |
| 1132 | * {@link Window#PROGRESS_INDETERMINATE_ON}, |
| 1133 | * {@link Window#PROGRESS_INDETERMINATE_OFF}, or a value |
| 1134 | * starting at {@link Window#PROGRESS_START} through |
| 1135 | * {@link Window#PROGRESS_END} for setting the default |
| 1136 | * progress (if {@link Window#PROGRESS_END} is given, |
| 1137 | * the progress bar widgets in the title will be hidden after an |
| 1138 | * animation), a value between |
| 1139 | * {@link Window#PROGRESS_SECONDARY_START} - |
| 1140 | * {@link Window#PROGRESS_SECONDARY_END} for the |
| 1141 | * secondary progress (if |
| 1142 | * {@link Window#PROGRESS_SECONDARY_END} is given, the |
| 1143 | * progress bar widgets will still be shown with the secondary |
| 1144 | * progress bar will be completely filled in.) |
| 1145 | */ |
| 1146 | private void updateProgressBars(int value) { |
| 1147 | ProgressBar circularProgressBar = getCircularProgressBar(true); |
| 1148 | ProgressBar horizontalProgressBar = getHorizontalProgressBar(true); |
| 1149 | |
| 1150 | final int features = getLocalFeatures(); |
| 1151 | if (value == PROGRESS_VISIBILITY_ON) { |
| 1152 | if ((features & (1 << FEATURE_PROGRESS)) != 0) { |
| 1153 | int level = horizontalProgressBar.getProgress(); |
| 1154 | int visibility = (horizontalProgressBar.isIndeterminate() || level < 10000) ? |
| 1155 | View.VISIBLE : View.INVISIBLE; |
| 1156 | horizontalProgressBar.setVisibility(visibility); |
| 1157 | } |
| 1158 | if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { |
| 1159 | circularProgressBar.setVisibility(View.VISIBLE); |
| 1160 | } |
| 1161 | } else if (value == PROGRESS_VISIBILITY_OFF) { |
| 1162 | if ((features & (1 << FEATURE_PROGRESS)) != 0) { |
| 1163 | horizontalProgressBar.setVisibility(View.GONE); |
| 1164 | } |
| 1165 | if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { |
| 1166 | circularProgressBar.setVisibility(View.GONE); |
| 1167 | } |
| 1168 | } else if (value == PROGRESS_INDETERMINATE_ON) { |
| 1169 | horizontalProgressBar.setIndeterminate(true); |
| 1170 | } else if (value == PROGRESS_INDETERMINATE_OFF) { |
| 1171 | horizontalProgressBar.setIndeterminate(false); |
| 1172 | } else if (PROGRESS_START <= value && value <= PROGRESS_END) { |
| 1173 | // We want to set the progress value before testing for visibility |
| 1174 | // so that when the progress bar becomes visible again, it has the |
| 1175 | // correct level. |
| 1176 | horizontalProgressBar.setProgress(value - PROGRESS_START); |
| 1177 | |
| 1178 | if (value < PROGRESS_END) { |
| 1179 | showProgressBars(horizontalProgressBar, circularProgressBar); |
| 1180 | } else { |
| 1181 | hideProgressBars(horizontalProgressBar, circularProgressBar); |
| 1182 | } |
| 1183 | } else if (PROGRESS_SECONDARY_START <= value && value <= PROGRESS_SECONDARY_END) { |
| 1184 | horizontalProgressBar.setSecondaryProgress(value - PROGRESS_SECONDARY_START); |
| 1185 | |
| 1186 | showProgressBars(horizontalProgressBar, circularProgressBar); |
| 1187 | } |
| 1188 | |
| 1189 | } |
| 1190 | |
| 1191 | private void showProgressBars(ProgressBar horizontalProgressBar, ProgressBar spinnyProgressBar) { |
| 1192 | final int features = getLocalFeatures(); |
| 1193 | if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0 && |
| 1194 | spinnyProgressBar.getVisibility() == View.INVISIBLE) { |
| 1195 | spinnyProgressBar.setVisibility(View.VISIBLE); |
| 1196 | } |
| 1197 | // Only show the progress bars if the primary progress is not complete |
| 1198 | if ((features & (1 << FEATURE_PROGRESS)) != 0 && |
| 1199 | horizontalProgressBar.getProgress() < 10000) { |
| 1200 | horizontalProgressBar.setVisibility(View.VISIBLE); |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | private void hideProgressBars(ProgressBar horizontalProgressBar, ProgressBar spinnyProgressBar) { |
| 1205 | final int features = getLocalFeatures(); |
| 1206 | Animation anim = AnimationUtils.loadAnimation(getContext(), com.android.internal.R.anim.fade_out); |
| 1207 | anim.setDuration(1000); |
| 1208 | if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0 && |
| 1209 | spinnyProgressBar.getVisibility() == View.VISIBLE) { |
| 1210 | spinnyProgressBar.startAnimation(anim); |
| 1211 | spinnyProgressBar.setVisibility(View.INVISIBLE); |
| 1212 | } |
| 1213 | if ((features & (1 << FEATURE_PROGRESS)) != 0 && |
| 1214 | horizontalProgressBar.getVisibility() == View.VISIBLE) { |
| 1215 | horizontalProgressBar.startAnimation(anim); |
| 1216 | horizontalProgressBar.setVisibility(View.INVISIBLE); |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /** |
| 1221 | * Request that key events come to this activity. Use this if your activity |
| 1222 | * has no views with focus, but the activity still wants a chance to process |
| 1223 | * key events. |
| 1224 | */ |
| 1225 | @Override |
| 1226 | public void takeKeyEvents(boolean get) { |
| 1227 | mDecor.setFocusable(get); |
| 1228 | } |
| 1229 | |
| 1230 | @Override |
| 1231 | public boolean superDispatchKeyEvent(KeyEvent event) { |
| 1232 | return mDecor.superDispatchKeyEvent(event); |
| 1233 | } |
| 1234 | |
| 1235 | @Override |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 1236 | public boolean superDispatchKeyShortcutEvent(KeyEvent event) { |
| 1237 | return mDecor.superDispatchKeyShortcutEvent(event); |
| 1238 | } |
| 1239 | |
| 1240 | @Override |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1241 | public boolean superDispatchTouchEvent(MotionEvent event) { |
| 1242 | return mDecor.superDispatchTouchEvent(event); |
| 1243 | } |
| 1244 | |
| 1245 | @Override |
| 1246 | public boolean superDispatchTrackballEvent(MotionEvent event) { |
| 1247 | return mDecor.superDispatchTrackballEvent(event); |
| 1248 | } |
| 1249 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1250 | @Override |
| 1251 | public boolean superDispatchGenericMotionEvent(MotionEvent event) { |
| 1252 | return mDecor.superDispatchGenericMotionEvent(event); |
| 1253 | } |
| 1254 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1255 | /** |
| 1256 | * A key was pressed down and not handled by anything else in the window. |
| 1257 | * |
| 1258 | * @see #onKeyUp |
| 1259 | * @see android.view.KeyEvent |
| 1260 | */ |
| 1261 | protected boolean onKeyDown(int featureId, int keyCode, KeyEvent event) { |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 1262 | /* **************************************************************************** |
| 1263 | * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES. |
| 1264 | * |
| 1265 | * If your key handling must happen before the app gets a crack at the event, |
| 1266 | * it goes in PhoneWindowManager. |
| 1267 | * |
| 1268 | * If your key handling should happen in all windows, and does not depend on |
| 1269 | * the state of the current application, other than that the current |
| 1270 | * application can override the behavior by handling the event itself, it |
| 1271 | * should go in PhoneFallbackEventHandler. |
| 1272 | * |
| 1273 | * Only if your handling depends on the window, and the fact that it has |
| 1274 | * a DecorView, should it go here. |
| 1275 | * ****************************************************************************/ |
| 1276 | |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 1277 | final KeyEvent.DispatcherState dispatcher = |
| 1278 | mDecor != null ? mDecor.getKeyDispatcherState() : null; |
| 1279 | //Log.i(TAG, "Key down: repeat=" + event.getRepeatCount() |
| 1280 | // + " flags=0x" + Integer.toHexString(event.getFlags())); |
| 1281 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1282 | switch (keyCode) { |
| 1283 | case KeyEvent.KEYCODE_VOLUME_UP: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 1284 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 1285 | case KeyEvent.KEYCODE_VOLUME_MUTE: { |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 1286 | // Similar code is in PhoneFallbackEventHandler in case the window |
| 1287 | // doesn't have one of these. In this case, we execute it here and |
| 1288 | // eat the event instead, because we have mVolumeControlStreamType |
| 1289 | // and they don't. |
| 1290 | getAudioManager().handleKeyDown(keyCode, mVolumeControlStreamType); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1291 | return true; |
| 1292 | } |
| 1293 | |
| 1294 | case KeyEvent.KEYCODE_MENU: { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1295 | onKeyDownPanel((featureId < 0) ? FEATURE_OPTIONS_PANEL : featureId, event); |
| 1296 | return true; |
| 1297 | } |
| 1298 | |
| 1299 | case KeyEvent.KEYCODE_BACK: { |
| 1300 | if (event.getRepeatCount() > 0) break; |
| 1301 | if (featureId < 0) break; |
Dianne Hackborn | fed9cb5 | 2009-09-14 21:23:11 -0700 | [diff] [blame] | 1302 | // Currently don't do anything with long press. |
| 1303 | dispatcher.startTracking(event, this); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1304 | return true; |
| 1305 | } |
| 1306 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | return false; |
| 1310 | } |
| 1311 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1312 | private KeyguardManager getKeyguardManager() { |
| 1313 | if (mKeyguardManager == null) { |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 1314 | mKeyguardManager = (KeyguardManager) getContext().getSystemService( |
| 1315 | Context.KEYGUARD_SERVICE); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1316 | } |
| 1317 | return mKeyguardManager; |
| 1318 | } |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 1319 | |
| 1320 | AudioManager getAudioManager() { |
| 1321 | if (mAudioManager == null) { |
| 1322 | mAudioManager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE); |
Mike LeBeau | bfd25ca | 2010-03-22 17:40:35 -0700 | [diff] [blame] | 1323 | } |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 1324 | return mAudioManager; |
Mike LeBeau | bfd25ca | 2010-03-22 17:40:35 -0700 | [diff] [blame] | 1325 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1326 | |
| 1327 | /** |
| 1328 | * A key was released and not handled by anything else in the window. |
| 1329 | * |
| 1330 | * @see #onKeyDown |
| 1331 | * @see android.view.KeyEvent |
| 1332 | */ |
| 1333 | protected boolean onKeyUp(int featureId, int keyCode, KeyEvent event) { |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 1334 | final KeyEvent.DispatcherState dispatcher = |
| 1335 | mDecor != null ? mDecor.getKeyDispatcherState() : null; |
| 1336 | if (dispatcher != null) { |
| 1337 | dispatcher.handleUpEvent(event); |
| 1338 | } |
| 1339 | //Log.i(TAG, "Key up: repeat=" + event.getRepeatCount() |
| 1340 | // + " flags=0x" + Integer.toHexString(event.getFlags())); |
| 1341 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1342 | switch (keyCode) { |
| 1343 | case KeyEvent.KEYCODE_VOLUME_UP: |
Jeff Brown | b0418da | 2010-11-01 15:24:01 -0700 | [diff] [blame] | 1344 | case KeyEvent.KEYCODE_VOLUME_DOWN: |
| 1345 | case KeyEvent.KEYCODE_VOLUME_MUTE: { |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 1346 | // Similar code is in PhoneFallbackEventHandler in case the window |
| 1347 | // doesn't have one of these. In this case, we execute it here and |
| 1348 | // eat the event instead, because we have mVolumeControlStreamType |
| 1349 | // and they don't. |
| 1350 | getAudioManager().handleKeyUp(keyCode, mVolumeControlStreamType); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1351 | return true; |
| 1352 | } |
| 1353 | |
| 1354 | case KeyEvent.KEYCODE_MENU: { |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 1355 | onKeyUpPanel(featureId < 0 ? FEATURE_OPTIONS_PANEL : featureId, |
| 1356 | event); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1357 | return true; |
| 1358 | } |
| 1359 | |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 1360 | case KeyEvent.KEYCODE_BACK: { |
| 1361 | if (featureId < 0) break; |
Dianne Hackborn | fed9cb5 | 2009-09-14 21:23:11 -0700 | [diff] [blame] | 1362 | if (event.isTracking() && !event.isCanceled()) { |
| 1363 | if (featureId == FEATURE_OPTIONS_PANEL) { |
| 1364 | PanelFeatureState st = getPanelState(featureId, false); |
| 1365 | if (st != null && st.isInExpandedMode) { |
| 1366 | // If the user is in an expanded menu and hits back, it |
| 1367 | // should go back to the icon menu |
| 1368 | reopenMenu(true); |
| 1369 | return true; |
| 1370 | } |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 1371 | } |
Dianne Hackborn | fed9cb5 | 2009-09-14 21:23:11 -0700 | [diff] [blame] | 1372 | closePanel(featureId); |
| 1373 | return true; |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 1374 | } |
Dianne Hackborn | fed9cb5 | 2009-09-14 21:23:11 -0700 | [diff] [blame] | 1375 | break; |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1378 | case KeyEvent.KEYCODE_SEARCH: { |
| 1379 | /* |
| 1380 | * Do this in onKeyUp since the Search key is also used for |
| 1381 | * chording quick launch shortcuts. |
| 1382 | */ |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 1383 | if (getKeyguardManager().inKeyguardRestrictedInputMode()) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1384 | break; |
| 1385 | } |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 1386 | if (event.isTracking() && !event.isCanceled()) { |
Dianne Hackborn | 0041e97 | 2009-07-24 17:14:43 -0700 | [diff] [blame] | 1387 | launchDefaultSearch(); |
| 1388 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1389 | return true; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | return false; |
| 1394 | } |
| 1395 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1396 | @Override |
| 1397 | protected void onActive() { |
| 1398 | } |
| 1399 | |
| 1400 | @Override |
| 1401 | public final View getDecorView() { |
| 1402 | if (mDecor == null) { |
| 1403 | installDecor(); |
| 1404 | } |
| 1405 | return mDecor; |
| 1406 | } |
| 1407 | |
| 1408 | @Override |
| 1409 | public final View peekDecorView() { |
| 1410 | return mDecor; |
| 1411 | } |
| 1412 | |
| 1413 | static private final String FOCUSED_ID_TAG = "android:focusedViewId"; |
| 1414 | static private final String VIEWS_TAG = "android:views"; |
| 1415 | static private final String PANELS_TAG = "android:Panels"; |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 1416 | static private final String ACTION_BAR_TAG = "android:ActionBar"; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1417 | |
| 1418 | /** {@inheritDoc} */ |
| 1419 | @Override |
| 1420 | public Bundle saveHierarchyState() { |
| 1421 | Bundle outState = new Bundle(); |
| 1422 | if (mContentParent == null) { |
| 1423 | return outState; |
| 1424 | } |
| 1425 | |
| 1426 | SparseArray<Parcelable> states = new SparseArray<Parcelable>(); |
| 1427 | mContentParent.saveHierarchyState(states); |
| 1428 | outState.putSparseParcelableArray(VIEWS_TAG, states); |
| 1429 | |
| 1430 | // save the focused view id |
| 1431 | View focusedView = mContentParent.findFocus(); |
| 1432 | if (focusedView != null) { |
| 1433 | if (focusedView.getId() != View.NO_ID) { |
| 1434 | outState.putInt(FOCUSED_ID_TAG, focusedView.getId()); |
| 1435 | } else { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 1436 | if (false) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1437 | Log.d(TAG, "couldn't save which view has focus because the focused view " |
| 1438 | + focusedView + " has no id."); |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | // save the panels |
| 1444 | SparseArray<Parcelable> panelStates = new SparseArray<Parcelable>(); |
| 1445 | savePanelState(panelStates); |
| 1446 | if (panelStates.size() > 0) { |
| 1447 | outState.putSparseParcelableArray(PANELS_TAG, panelStates); |
| 1448 | } |
| 1449 | |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 1450 | if (mActionBar != null) { |
| 1451 | outState.putBoolean(ACTION_BAR_TAG, mActionBar.isOverflowMenuShowing()); |
| 1452 | } |
| 1453 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1454 | return outState; |
| 1455 | } |
| 1456 | |
| 1457 | /** {@inheritDoc} */ |
| 1458 | @Override |
| 1459 | public void restoreHierarchyState(Bundle savedInstanceState) { |
| 1460 | if (mContentParent == null) { |
| 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | SparseArray<Parcelable> savedStates |
| 1465 | = savedInstanceState.getSparseParcelableArray(VIEWS_TAG); |
| 1466 | if (savedStates != null) { |
| 1467 | mContentParent.restoreHierarchyState(savedStates); |
| 1468 | } |
| 1469 | |
| 1470 | // restore the focused view |
| 1471 | int focusedViewId = savedInstanceState.getInt(FOCUSED_ID_TAG, View.NO_ID); |
| 1472 | if (focusedViewId != View.NO_ID) { |
| 1473 | View needsFocus = mContentParent.findViewById(focusedViewId); |
| 1474 | if (needsFocus != null) { |
| 1475 | needsFocus.requestFocus(); |
| 1476 | } else { |
| 1477 | Log.w(TAG, |
| 1478 | "Previously focused view reported id " + focusedViewId |
| 1479 | + " during save, but can't be found during restore."); |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | // restore the panels |
| 1484 | SparseArray<Parcelable> panelStates = savedInstanceState.getSparseParcelableArray(PANELS_TAG); |
| 1485 | if (panelStates != null) { |
| 1486 | restorePanelState(panelStates); |
| 1487 | } |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 1488 | |
| 1489 | if (mActionBar != null && savedInstanceState.getBoolean(ACTION_BAR_TAG)) { |
| 1490 | mActionBar.postShowOverflowMenu(); |
| 1491 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /** |
| 1495 | * Invoked when the panels should freeze their state. |
| 1496 | * |
| 1497 | * @param icicles Save state into this. This is usually indexed by the |
| 1498 | * featureId. This will be given to {@link #restorePanelState} in the |
| 1499 | * future. |
| 1500 | */ |
| 1501 | private void savePanelState(SparseArray<Parcelable> icicles) { |
| 1502 | PanelFeatureState[] panels = mPanels; |
| 1503 | if (panels == null) { |
| 1504 | return; |
| 1505 | } |
| 1506 | |
| 1507 | for (int curFeatureId = panels.length - 1; curFeatureId >= 0; curFeatureId--) { |
| 1508 | if (panels[curFeatureId] != null) { |
| 1509 | icicles.put(curFeatureId, panels[curFeatureId].onSaveInstanceState()); |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /** |
| 1515 | * Invoked when the panels should thaw their state from a previously frozen state. |
| 1516 | * |
| 1517 | * @param icicles The state saved by {@link #savePanelState} that needs to be thawed. |
| 1518 | */ |
| 1519 | private void restorePanelState(SparseArray<Parcelable> icicles) { |
| 1520 | PanelFeatureState st; |
| 1521 | for (int curFeatureId = icicles.size() - 1; curFeatureId >= 0; curFeatureId--) { |
| 1522 | st = getPanelState(curFeatureId, false /* required */); |
| 1523 | if (st == null) { |
| 1524 | // The panel must not have been required, and is currently not around, skip it |
| 1525 | continue; |
| 1526 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 1527 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1528 | st.onRestoreInstanceState(icicles.get(curFeatureId)); |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * Implementation note: call openPanelsAfterRestore later to actually open the |
| 1533 | * restored panels. |
| 1534 | */ |
| 1535 | } |
| 1536 | |
| 1537 | /** |
| 1538 | * Opens the panels that have had their state restored. This should be |
| 1539 | * called sometime after {@link #restorePanelState} when it is safe to add |
| 1540 | * to the window manager. |
| 1541 | */ |
| 1542 | private void openPanelsAfterRestore() { |
| 1543 | PanelFeatureState[] panels = mPanels; |
| 1544 | |
| 1545 | if (panels == null) { |
| 1546 | return; |
| 1547 | } |
| 1548 | |
| 1549 | PanelFeatureState st; |
| 1550 | for (int i = panels.length - 1; i >= 0; i--) { |
| 1551 | st = panels[i]; |
Dianne Hackborn | d007144 | 2009-09-25 01:35:29 -0700 | [diff] [blame] | 1552 | // We restore the panel if it was last open; we skip it if it |
| 1553 | // now is open, to avoid a race condition if the user immediately |
| 1554 | // opens it when we are resuming. |
| 1555 | if ((st != null) && !st.isOpen && st.wasLastOpen) { |
| 1556 | st.isInExpandedMode = st.wasLastExpanded; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1557 | openPanel(st, null); |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 1562 | private class PanelMenuPresenterCallback implements MenuPresenter.Callback { |
| 1563 | @Override |
| 1564 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 1565 | final Menu parentMenu = menu.getRootMenu(); |
| 1566 | final boolean isSubMenu = parentMenu != menu; |
| 1567 | final PanelFeatureState panel = findMenuPanel(isSubMenu ? parentMenu : menu); |
| 1568 | if (panel != null) { |
| 1569 | if (isSubMenu) { |
| 1570 | callOnPanelClosed(panel.featureId, panel, parentMenu); |
| 1571 | closePanel(panel, true); |
| 1572 | } else { |
| 1573 | // Close the panel and only do the callback if the menu is being |
| 1574 | // closed completely, not if opening a sub menu |
| 1575 | closePanel(panel, allMenusAreClosing); |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | @Override |
| 1581 | public boolean onOpenSubMenu(MenuBuilder subMenu) { |
| 1582 | if (subMenu == null && hasFeature(FEATURE_ACTION_BAR)) { |
| 1583 | Callback cb = getCallback(); |
| 1584 | if (cb != null && !isDestroyed()) { |
| 1585 | cb.onMenuOpened(FEATURE_ACTION_BAR, subMenu); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | return true; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | private final class ActionMenuPresenterCallback implements MenuPresenter.Callback { |
| 1594 | @Override |
| 1595 | public boolean onOpenSubMenu(MenuBuilder subMenu) { |
| 1596 | Callback cb = getCallback(); |
| 1597 | if (cb != null) { |
| 1598 | cb.onMenuOpened(FEATURE_ACTION_BAR, subMenu); |
| 1599 | return true; |
| 1600 | } |
| 1601 | return false; |
| 1602 | } |
| 1603 | |
| 1604 | @Override |
| 1605 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 1606 | checkCloseActionMenu(menu); |
| 1607 | } |
| 1608 | } |
| 1609 | |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 1610 | private final class DecorView extends FrameLayout implements RootViewSurfaceTaker { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1611 | /* package */int mDefaultOpacity = PixelFormat.OPAQUE; |
| 1612 | |
| 1613 | /** The feature ID of the panel, or -1 if this is the application's DecorView */ |
| 1614 | private final int mFeatureId; |
| 1615 | |
| 1616 | private final Rect mDrawingBounds = new Rect(); |
| 1617 | |
| 1618 | private final Rect mBackgroundPadding = new Rect(); |
| 1619 | |
| 1620 | private final Rect mFramePadding = new Rect(); |
| 1621 | |
| 1622 | private final Rect mFrameOffsets = new Rect(); |
| 1623 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1624 | private boolean mChanging; |
| 1625 | |
| 1626 | private Drawable mMenuBackground; |
| 1627 | private boolean mWatchingForMenu; |
| 1628 | private int mDownY; |
| 1629 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 1630 | private ActionMode mActionMode; |
| 1631 | private ActionBarContextView mActionModeView; |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 1632 | private PopupWindow mActionModePopup; |
Adam Powell | d3c9704 | 2011-02-28 21:25:37 -0800 | [diff] [blame] | 1633 | private Runnable mShowActionModePopup; |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 1634 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1635 | public DecorView(Context context, int featureId) { |
| 1636 | super(context); |
| 1637 | mFeatureId = featureId; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | @Override |
| 1641 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 1642 | final int keyCode = event.getKeyCode(); |
Adam Powell | 04253aa | 2010-08-18 11:59:11 -0700 | [diff] [blame] | 1643 | final int action = event.getAction(); |
| 1644 | final boolean isDown = action == KeyEvent.ACTION_DOWN; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1645 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1646 | if (isDown && (event.getRepeatCount() == 0)) { |
| 1647 | // First handle chording of panel key: if a panel key is held |
| 1648 | // but not released, try to execute a shortcut in it. |
| 1649 | if ((mPanelChordingKey > 0) && (mPanelChordingKey != keyCode)) { |
Jeff Brown | 4aed78b | 2011-01-14 17:36:55 -0800 | [diff] [blame] | 1650 | boolean handled = dispatchKeyShortcutEvent(event); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1651 | if (handled) { |
| 1652 | return true; |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | // If a panel is open, perform a shortcut on it without the |
| 1657 | // chorded panel key |
| 1658 | if ((mPreparedPanel != null) && mPreparedPanel.isOpen) { |
| 1659 | if (performPanelShortcut(mPreparedPanel, keyCode, event, 0)) { |
| 1660 | return true; |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
Adam Powell | 04253aa | 2010-08-18 11:59:11 -0700 | [diff] [blame] | 1665 | // Back cancels action modes first. |
| 1666 | if (mActionMode != null && keyCode == KeyEvent.KEYCODE_BACK) { |
| 1667 | if (action == KeyEvent.ACTION_UP) { |
| 1668 | mActionMode.finish(); |
| 1669 | } |
| 1670 | return true; |
| 1671 | } |
| 1672 | |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 1673 | if (!isDestroyed()) { |
| 1674 | final Callback cb = getCallback(); |
| 1675 | final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event) |
| 1676 | : super.dispatchKeyEvent(event); |
| 1677 | if (handled) { |
| 1678 | return true; |
| 1679 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1680 | } |
| 1681 | return isDown ? PhoneWindow.this.onKeyDown(mFeatureId, event.getKeyCode(), event) |
| 1682 | : PhoneWindow.this.onKeyUp(mFeatureId, event.getKeyCode(), event); |
| 1683 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 1684 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1685 | @Override |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 1686 | public boolean dispatchKeyShortcutEvent(KeyEvent ev) { |
Jeff Brown | 4aed78b | 2011-01-14 17:36:55 -0800 | [diff] [blame] | 1687 | // Perform the shortcut (mPreparedPanel can be null since |
| 1688 | // global shortcuts (such as search) don't rely on a |
| 1689 | // prepared panel or menu). |
| 1690 | boolean handled = performPanelShortcut(mPreparedPanel, ev.getKeyCode(), ev, |
| 1691 | Menu.FLAG_PERFORM_NO_CLOSE); |
| 1692 | if (handled) { |
| 1693 | if (mPreparedPanel != null) { |
| 1694 | mPreparedPanel.isHandled = true; |
| 1695 | } |
| 1696 | return true; |
| 1697 | } |
| 1698 | |
| 1699 | // Shortcut not handled by the panel. Dispatch to the view hierarchy. |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 1700 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 1701 | return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchKeyShortcutEvent(ev) |
| 1702 | : super.dispatchKeyShortcutEvent(ev); |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | @Override |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1706 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 1707 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 1708 | return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTouchEvent(ev) |
| 1709 | : super.dispatchTouchEvent(ev); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | @Override |
| 1713 | public boolean dispatchTrackballEvent(MotionEvent ev) { |
| 1714 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 1715 | return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchTrackballEvent(ev) |
| 1716 | : super.dispatchTrackballEvent(ev); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1717 | } |
| 1718 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1719 | @Override |
| 1720 | public boolean dispatchGenericMotionEvent(MotionEvent ev) { |
| 1721 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 1722 | return cb != null && !isDestroyed() && mFeatureId < 0 ? cb.dispatchGenericMotionEvent(ev) |
| 1723 | : super.dispatchGenericMotionEvent(ev); |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1724 | } |
| 1725 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1726 | public boolean superDispatchKeyEvent(KeyEvent event) { |
| 1727 | return super.dispatchKeyEvent(event); |
| 1728 | } |
| 1729 | |
Jeff Brown | 64da12a | 2011-01-04 19:57:47 -0800 | [diff] [blame] | 1730 | public boolean superDispatchKeyShortcutEvent(KeyEvent event) { |
| 1731 | return super.dispatchKeyShortcutEvent(event); |
| 1732 | } |
| 1733 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1734 | public boolean superDispatchTouchEvent(MotionEvent event) { |
| 1735 | return super.dispatchTouchEvent(event); |
| 1736 | } |
| 1737 | |
| 1738 | public boolean superDispatchTrackballEvent(MotionEvent event) { |
| 1739 | return super.dispatchTrackballEvent(event); |
| 1740 | } |
| 1741 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1742 | public boolean superDispatchGenericMotionEvent(MotionEvent event) { |
| 1743 | return super.dispatchGenericMotionEvent(event); |
| 1744 | } |
| 1745 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1746 | @Override |
| 1747 | public boolean onTouchEvent(MotionEvent event) { |
| 1748 | return onInterceptTouchEvent(event); |
| 1749 | } |
| 1750 | |
| 1751 | private boolean isOutOfBounds(int x, int y) { |
| 1752 | return x < -5 || y < -5 || x > (getWidth() + 5) |
| 1753 | || y > (getHeight() + 5); |
| 1754 | } |
| 1755 | |
| 1756 | @Override |
| 1757 | public boolean onInterceptTouchEvent(MotionEvent event) { |
| 1758 | int action = event.getAction(); |
| 1759 | if (mFeatureId >= 0) { |
| 1760 | if (action == MotionEvent.ACTION_DOWN) { |
| 1761 | int x = (int)event.getX(); |
| 1762 | int y = (int)event.getY(); |
| 1763 | if (isOutOfBounds(x, y)) { |
| 1764 | closePanel(mFeatureId); |
| 1765 | return true; |
| 1766 | } |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | if (!SWEEP_OPEN_MENU) { |
| 1771 | return false; |
| 1772 | } |
| 1773 | |
| 1774 | if (mFeatureId >= 0) { |
| 1775 | if (action == MotionEvent.ACTION_DOWN) { |
| 1776 | Log.i(TAG, "Watchiing!"); |
| 1777 | mWatchingForMenu = true; |
| 1778 | mDownY = (int) event.getY(); |
| 1779 | return false; |
| 1780 | } |
| 1781 | |
| 1782 | if (!mWatchingForMenu) { |
| 1783 | return false; |
| 1784 | } |
| 1785 | |
| 1786 | int y = (int)event.getY(); |
| 1787 | if (action == MotionEvent.ACTION_MOVE) { |
| 1788 | if (y > (mDownY+30)) { |
| 1789 | Log.i(TAG, "Closing!"); |
| 1790 | closePanel(mFeatureId); |
| 1791 | mWatchingForMenu = false; |
| 1792 | return true; |
| 1793 | } |
| 1794 | } else if (action == MotionEvent.ACTION_UP) { |
| 1795 | mWatchingForMenu = false; |
| 1796 | } |
| 1797 | |
| 1798 | return false; |
| 1799 | } |
| 1800 | |
| 1801 | //Log.i(TAG, "Intercept: action=" + action + " y=" + event.getY() |
| 1802 | // + " (in " + getHeight() + ")"); |
| 1803 | |
| 1804 | if (action == MotionEvent.ACTION_DOWN) { |
| 1805 | int y = (int)event.getY(); |
| 1806 | if (y >= (getHeight()-5) && !hasChildren()) { |
| 1807 | Log.i(TAG, "Watchiing!"); |
| 1808 | mWatchingForMenu = true; |
| 1809 | } |
| 1810 | return false; |
| 1811 | } |
| 1812 | |
| 1813 | if (!mWatchingForMenu) { |
| 1814 | return false; |
| 1815 | } |
| 1816 | |
| 1817 | int y = (int)event.getY(); |
| 1818 | if (action == MotionEvent.ACTION_MOVE) { |
| 1819 | if (y < (getHeight()-30)) { |
| 1820 | Log.i(TAG, "Opening!"); |
| 1821 | openPanel(FEATURE_OPTIONS_PANEL, new KeyEvent( |
| 1822 | KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU)); |
| 1823 | mWatchingForMenu = false; |
| 1824 | return true; |
| 1825 | } |
| 1826 | } else if (action == MotionEvent.ACTION_UP) { |
| 1827 | mWatchingForMenu = false; |
| 1828 | } |
| 1829 | |
| 1830 | return false; |
| 1831 | } |
| 1832 | |
| 1833 | @Override |
svetoslavganov | 491293e | 2009-04-28 19:17:02 -0700 | [diff] [blame] | 1834 | public void sendAccessibilityEvent(int eventType) { |
| 1835 | if (!AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 1836 | return; |
| 1837 | } |
| 1838 | |
| 1839 | // if we are showing a feature that should be announced and one child |
| 1840 | // make this child the event source since this is the feature itself |
| 1841 | // otherwise the callback will take over and announce its client |
| 1842 | if ((mFeatureId == FEATURE_OPTIONS_PANEL || |
| 1843 | mFeatureId == FEATURE_CONTEXT_MENU || |
| 1844 | mFeatureId == FEATURE_PROGRESS || |
| 1845 | mFeatureId == FEATURE_INDETERMINATE_PROGRESS) |
| 1846 | && getChildCount() == 1) { |
| 1847 | getChildAt(0).sendAccessibilityEvent(eventType); |
| 1848 | } else { |
| 1849 | super.sendAccessibilityEvent(eventType); |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | @Override |
| 1854 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 1855 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 1856 | if (cb != null && !isDestroyed()) { |
svetoslavganov | 491293e | 2009-04-28 19:17:02 -0700 | [diff] [blame] | 1857 | if (cb.dispatchPopulateAccessibilityEvent(event)) { |
| 1858 | return true; |
| 1859 | } |
| 1860 | } |
| 1861 | return super.dispatchPopulateAccessibilityEvent(event); |
| 1862 | } |
| 1863 | |
| 1864 | @Override |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1865 | protected boolean setFrame(int l, int t, int r, int b) { |
| 1866 | boolean changed = super.setFrame(l, t, r, b); |
| 1867 | if (changed) { |
| 1868 | final Rect drawingBounds = mDrawingBounds; |
| 1869 | getDrawingRect(drawingBounds); |
| 1870 | |
| 1871 | Drawable fg = getForeground(); |
| 1872 | if (fg != null) { |
| 1873 | final Rect frameOffsets = mFrameOffsets; |
| 1874 | drawingBounds.left += frameOffsets.left; |
| 1875 | drawingBounds.top += frameOffsets.top; |
| 1876 | drawingBounds.right -= frameOffsets.right; |
| 1877 | drawingBounds.bottom -= frameOffsets.bottom; |
| 1878 | fg.setBounds(drawingBounds); |
| 1879 | final Rect framePadding = mFramePadding; |
| 1880 | drawingBounds.left += framePadding.left - frameOffsets.left; |
| 1881 | drawingBounds.top += framePadding.top - frameOffsets.top; |
| 1882 | drawingBounds.right -= framePadding.right - frameOffsets.right; |
| 1883 | drawingBounds.bottom -= framePadding.bottom - frameOffsets.bottom; |
| 1884 | } |
| 1885 | |
| 1886 | Drawable bg = getBackground(); |
| 1887 | if (bg != null) { |
| 1888 | bg.setBounds(drawingBounds); |
| 1889 | } |
| 1890 | |
| 1891 | if (SWEEP_OPEN_MENU) { |
| 1892 | if (mMenuBackground == null && mFeatureId < 0 |
| 1893 | && getAttributes().height |
Romain Guy | cc6828c | 2010-01-08 15:06:37 -0800 | [diff] [blame] | 1894 | == WindowManager.LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1895 | mMenuBackground = getContext().getResources().getDrawable( |
| 1896 | com.android.internal.R.drawable.menu_background); |
| 1897 | } |
| 1898 | if (mMenuBackground != null) { |
| 1899 | mMenuBackground.setBounds(drawingBounds.left, |
| 1900 | drawingBounds.bottom-6, drawingBounds.right, |
| 1901 | drawingBounds.bottom+20); |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | return changed; |
| 1906 | } |
| 1907 | |
| 1908 | @Override |
Dianne Hackborn | 6014527 | 2011-01-11 23:45:09 -0800 | [diff] [blame] | 1909 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 1910 | final DisplayMetrics metrics = getContext().getResources().getDisplayMetrics(); |
| 1911 | final boolean isPortrait = metrics.widthPixels < metrics.heightPixels; |
| 1912 | |
| 1913 | final int widthMode = getMode(widthMeasureSpec); |
| 1914 | |
| 1915 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 1916 | |
| 1917 | int width = getMeasuredWidth(); |
| 1918 | boolean measure = false; |
| 1919 | |
| 1920 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, EXACTLY); |
| 1921 | |
| 1922 | final TypedValue tv = isPortrait ? mMinWidthMinor : mMinWidthMajor; |
| 1923 | |
| 1924 | if (widthMode == AT_MOST && tv.type != TypedValue.TYPE_NULL) { |
| 1925 | final int min; |
| 1926 | if (tv.type == TypedValue.TYPE_DIMENSION) { |
| 1927 | min = (int)tv.getDimension(metrics); |
| 1928 | } else if (tv.type == TypedValue.TYPE_FRACTION) { |
| 1929 | min = (int)tv.getFraction(metrics.widthPixels, metrics.widthPixels); |
| 1930 | } else { |
| 1931 | min = 0; |
| 1932 | } |
| 1933 | |
| 1934 | if (width < min) { |
| 1935 | widthMeasureSpec = MeasureSpec.makeMeasureSpec(min, EXACTLY); |
| 1936 | measure = true; |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | // TODO: Support height? |
| 1941 | |
| 1942 | if (measure) { |
| 1943 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | @Override |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1948 | public void draw(Canvas canvas) { |
| 1949 | super.draw(canvas); |
| 1950 | |
| 1951 | if (mMenuBackground != null) { |
| 1952 | mMenuBackground.draw(canvas); |
| 1953 | } |
| 1954 | } |
| 1955 | |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 1956 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1957 | @Override |
| 1958 | public boolean showContextMenuForChild(View originalView) { |
| 1959 | // Reuse the context menu builder |
| 1960 | if (mContextMenu == null) { |
| 1961 | mContextMenu = new ContextMenuBuilder(getContext()); |
| 1962 | mContextMenu.setCallback(mContextMenuCallback); |
| 1963 | } else { |
| 1964 | mContextMenu.clearAll(); |
| 1965 | } |
| 1966 | |
| 1967 | mContextMenuHelper = mContextMenu.show(originalView, originalView.getWindowToken()); |
| 1968 | return mContextMenuHelper != null; |
| 1969 | } |
| 1970 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 1971 | @Override |
| 1972 | public ActionMode startActionModeForChild(View originalView, |
| 1973 | ActionMode.Callback callback) { |
| 1974 | // originalView can be used here to be sure that we don't obscure |
| 1975 | // relevant content with the context mode UI. |
| 1976 | return startActionMode(callback); |
| 1977 | } |
| 1978 | |
| 1979 | @Override |
| 1980 | public ActionMode startActionMode(ActionMode.Callback callback) { |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 1981 | if (mActionMode != null) { |
| 1982 | mActionMode.finish(); |
| 1983 | } |
| 1984 | |
Adam Powell | 04253aa | 2010-08-18 11:59:11 -0700 | [diff] [blame] | 1985 | final ActionMode.Callback wrappedCallback = new ActionModeCallbackWrapper(callback); |
Adam Powell | bf85f43 | 2010-11-15 22:16:50 -0800 | [diff] [blame] | 1986 | ActionMode mode = null; |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 1987 | if (getCallback() != null && !isDestroyed()) { |
| 1988 | try { |
| 1989 | mode = getCallback().onWindowStartingActionMode(wrappedCallback); |
| 1990 | } catch (AbstractMethodError ame) { |
| 1991 | // Older apps might not implement this callback method. |
| 1992 | } |
Adam Powell | bf85f43 | 2010-11-15 22:16:50 -0800 | [diff] [blame] | 1993 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 1994 | if (mode != null) { |
| 1995 | mActionMode = mode; |
| 1996 | } else { |
| 1997 | if (mActionModeView == null) { |
Adam Powell | 8e55263 | 2010-08-04 15:26:22 -0700 | [diff] [blame] | 1998 | if (hasFeature(FEATURE_ACTION_MODE_OVERLAY)) { |
| 1999 | mActionModeView = new ActionBarContextView(mContext); |
Adam Powell | 7e06ea8 | 2010-12-05 18:22:52 -0800 | [diff] [blame] | 2000 | mActionModePopup = new PopupWindow(mContext, null, |
| 2001 | com.android.internal.R.attr.actionModePopupWindowStyle); |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 2002 | mActionModePopup.setLayoutInScreenEnabled(true); |
| 2003 | mActionModePopup.setClippingEnabled(false); |
| 2004 | mActionModePopup.setContentView(mActionModeView); |
| 2005 | mActionModePopup.setWidth(MATCH_PARENT); |
| 2006 | |
| 2007 | TypedValue heightValue = new TypedValue(); |
| 2008 | mContext.getTheme().resolveAttribute( |
| 2009 | com.android.internal.R.attr.actionBarSize, heightValue, false); |
| 2010 | final int height = TypedValue.complexToDimensionPixelSize(heightValue.data, |
| 2011 | mContext.getResources().getDisplayMetrics()); |
| 2012 | mActionModePopup.setHeight(height); |
Adam Powell | d3c9704 | 2011-02-28 21:25:37 -0800 | [diff] [blame] | 2013 | mShowActionModePopup = new Runnable() { |
| 2014 | public void run() { |
| 2015 | mActionModePopup.showAtLocation(PhoneWindow.DecorView.this, |
| 2016 | Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0); |
| 2017 | } |
| 2018 | }; |
Adam Powell | 8e55263 | 2010-08-04 15:26:22 -0700 | [diff] [blame] | 2019 | } else { |
| 2020 | ViewStub stub = (ViewStub) findViewById( |
| 2021 | com.android.internal.R.id.action_mode_bar_stub); |
Adam Powell | 04253aa | 2010-08-18 11:59:11 -0700 | [diff] [blame] | 2022 | if (stub != null) { |
| 2023 | mActionModeView = (ActionBarContextView) stub.inflate(); |
| 2024 | } |
Adam Powell | 8e55263 | 2010-08-04 15:26:22 -0700 | [diff] [blame] | 2025 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | if (mActionModeView != null) { |
Adam Powell | a1e6358 | 2011-01-18 16:51:22 -0800 | [diff] [blame] | 2029 | mActionModeView.killMode(); |
Adam Powell | 04253aa | 2010-08-18 11:59:11 -0700 | [diff] [blame] | 2030 | mode = new StandaloneActionMode(getContext(), mActionModeView, wrappedCallback); |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 2031 | if (callback.onCreateActionMode(mode, mode.getMenu())) { |
| 2032 | mode.invalidate(); |
| 2033 | mActionModeView.initForMode(mode); |
| 2034 | mActionModeView.setVisibility(View.VISIBLE); |
| 2035 | mActionMode = mode; |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 2036 | if (mActionModePopup != null) { |
Adam Powell | d3c9704 | 2011-02-28 21:25:37 -0800 | [diff] [blame] | 2037 | post(mShowActionModePopup); |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 2038 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 2039 | } else { |
| 2040 | mActionMode = null; |
| 2041 | } |
| 2042 | } |
| 2043 | } |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 2044 | if (mActionMode != null && getCallback() != null && !isDestroyed()) { |
Adam Powell | bf85f43 | 2010-11-15 22:16:50 -0800 | [diff] [blame] | 2045 | try { |
| 2046 | getCallback().onActionModeStarted(mActionMode); |
| 2047 | } catch (AbstractMethodError ame) { |
| 2048 | // Older apps might not implement this callback method. |
| 2049 | } |
Adam Powell | debf3be | 2010-11-15 18:58:48 -0800 | [diff] [blame] | 2050 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 2051 | return mActionMode; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2054 | public void startChanging() { |
| 2055 | mChanging = true; |
| 2056 | } |
| 2057 | |
| 2058 | public void finishChanging() { |
| 2059 | mChanging = false; |
| 2060 | drawableChanged(); |
| 2061 | } |
| 2062 | |
| 2063 | public void setWindowBackground(Drawable drawable) { |
| 2064 | if (getBackground() != drawable) { |
| 2065 | setBackgroundDrawable(drawable); |
| 2066 | if (drawable != null) { |
| 2067 | drawable.getPadding(mBackgroundPadding); |
| 2068 | } else { |
| 2069 | mBackgroundPadding.setEmpty(); |
| 2070 | } |
| 2071 | drawableChanged(); |
| 2072 | } |
| 2073 | } |
| 2074 | |
Dianne Hackborn | 63042d6 | 2011-01-26 18:56:29 -0800 | [diff] [blame] | 2075 | @Override |
| 2076 | public void setBackgroundDrawable(Drawable d) { |
| 2077 | super.setBackgroundDrawable(d); |
| 2078 | if (getWindowToken() != null) { |
| 2079 | updateWindowResizeState(); |
| 2080 | } |
| 2081 | } |
| 2082 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2083 | public void setWindowFrame(Drawable drawable) { |
| 2084 | if (getForeground() != drawable) { |
| 2085 | setForeground(drawable); |
| 2086 | if (drawable != null) { |
| 2087 | drawable.getPadding(mFramePadding); |
| 2088 | } else { |
| 2089 | mFramePadding.setEmpty(); |
| 2090 | } |
| 2091 | drawableChanged(); |
| 2092 | } |
| 2093 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2094 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2095 | @Override |
| 2096 | protected boolean fitSystemWindows(Rect insets) { |
| 2097 | mFrameOffsets.set(insets); |
| 2098 | if (getForeground() != null) { |
| 2099 | drawableChanged(); |
| 2100 | } |
| 2101 | return super.fitSystemWindows(insets); |
| 2102 | } |
| 2103 | |
| 2104 | private void drawableChanged() { |
| 2105 | if (mChanging) { |
| 2106 | return; |
| 2107 | } |
| 2108 | |
| 2109 | setPadding(mFramePadding.left + mBackgroundPadding.left, mFramePadding.top |
| 2110 | + mBackgroundPadding.top, mFramePadding.right + mBackgroundPadding.right, |
| 2111 | mFramePadding.bottom + mBackgroundPadding.bottom); |
| 2112 | requestLayout(); |
| 2113 | invalidate(); |
| 2114 | |
| 2115 | int opacity = PixelFormat.OPAQUE; |
| 2116 | |
| 2117 | // Note: if there is no background, we will assume opaque. The |
| 2118 | // common case seems to be that an application sets there to be |
| 2119 | // no background so it can draw everything itself. For that, |
| 2120 | // we would like to assume OPAQUE and let the app force it to |
| 2121 | // the slower TRANSLUCENT mode if that is really what it wants. |
| 2122 | Drawable bg = getBackground(); |
| 2123 | Drawable fg = getForeground(); |
| 2124 | if (bg != null) { |
| 2125 | if (fg == null) { |
| 2126 | opacity = bg.getOpacity(); |
| 2127 | } else if (mFramePadding.left <= 0 && mFramePadding.top <= 0 |
| 2128 | && mFramePadding.right <= 0 && mFramePadding.bottom <= 0) { |
| 2129 | // If the frame padding is zero, then we can be opaque |
| 2130 | // if either the frame -or- the background is opaque. |
| 2131 | int fop = fg.getOpacity(); |
| 2132 | int bop = bg.getOpacity(); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 2133 | if (false) |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2134 | Log.v(TAG, "Background opacity: " + bop + ", Frame opacity: " + fop); |
| 2135 | if (fop == PixelFormat.OPAQUE || bop == PixelFormat.OPAQUE) { |
| 2136 | opacity = PixelFormat.OPAQUE; |
| 2137 | } else if (fop == PixelFormat.UNKNOWN) { |
| 2138 | opacity = bop; |
| 2139 | } else if (bop == PixelFormat.UNKNOWN) { |
| 2140 | opacity = fop; |
| 2141 | } else { |
| 2142 | opacity = Drawable.resolveOpacity(fop, bop); |
| 2143 | } |
| 2144 | } else { |
| 2145 | // For now we have to assume translucent if there is a |
| 2146 | // frame with padding... there is no way to tell if the |
| 2147 | // frame and background together will draw all pixels. |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 2148 | if (false) |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2149 | Log.v(TAG, "Padding: " + mFramePadding); |
| 2150 | opacity = PixelFormat.TRANSLUCENT; |
| 2151 | } |
| 2152 | } |
| 2153 | |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 2154 | if (false) |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2155 | Log.v(TAG, "Background: " + bg + ", Frame: " + fg); |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 2156 | if (false) |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2157 | Log.v(TAG, "Selected default opacity: " + opacity); |
| 2158 | |
| 2159 | mDefaultOpacity = opacity; |
| 2160 | if (mFeatureId < 0) { |
| 2161 | setDefaultWindowFormat(opacity); |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | @Override |
| 2166 | public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 2167 | super.onWindowFocusChanged(hasWindowFocus); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2168 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2169 | // If the user is chording a menu shortcut, release the chord since |
| 2170 | // this window lost focus |
Dianne Hackborn | a207baa | 2009-09-13 16:14:44 -0700 | [diff] [blame] | 2171 | if (!hasWindowFocus && mPanelChordingKey != 0) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2172 | closePanel(FEATURE_OPTIONS_PANEL); |
| 2173 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2174 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2175 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 2176 | if (cb != null && !isDestroyed() && mFeatureId < 0) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2177 | cb.onWindowFocusChanged(hasWindowFocus); |
| 2178 | } |
| 2179 | } |
| 2180 | |
Dianne Hackborn | 63042d6 | 2011-01-26 18:56:29 -0800 | [diff] [blame] | 2181 | void updateWindowResizeState() { |
| 2182 | Drawable bg = getBackground(); |
| 2183 | hackTurnOffWindowResizeAnim(bg == null || bg.getOpacity() |
| 2184 | != PixelFormat.OPAQUE); |
| 2185 | } |
| 2186 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2187 | @Override |
| 2188 | protected void onAttachedToWindow() { |
| 2189 | super.onAttachedToWindow(); |
Dianne Hackborn | 93a0a5f | 2009-08-20 19:33:02 -0700 | [diff] [blame] | 2190 | |
Dianne Hackborn | 63042d6 | 2011-01-26 18:56:29 -0800 | [diff] [blame] | 2191 | updateWindowResizeState(); |
| 2192 | |
Dianne Hackborn | 93a0a5f | 2009-08-20 19:33:02 -0700 | [diff] [blame] | 2193 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 2194 | if (cb != null && !isDestroyed() && mFeatureId < 0) { |
Dianne Hackborn | 93a0a5f | 2009-08-20 19:33:02 -0700 | [diff] [blame] | 2195 | cb.onAttachedToWindow(); |
| 2196 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2197 | |
| 2198 | if (mFeatureId == -1) { |
| 2199 | /* |
| 2200 | * The main window has been attached, try to restore any panels |
| 2201 | * that may have been open before. This is called in cases where |
| 2202 | * an activity is being killed for configuration change and the |
| 2203 | * menu was open. When the activity is recreated, the menu |
| 2204 | * should be shown again. |
| 2205 | */ |
| 2206 | openPanelsAfterRestore(); |
| 2207 | } |
| 2208 | } |
Dianne Hackborn | 93a0a5f | 2009-08-20 19:33:02 -0700 | [diff] [blame] | 2209 | |
| 2210 | @Override |
| 2211 | protected void onDetachedFromWindow() { |
| 2212 | super.onDetachedFromWindow(); |
| 2213 | |
| 2214 | final Callback cb = getCallback(); |
Patrick Dubroy | 8620d95 | 2011-03-04 11:24:07 -0800 | [diff] [blame] | 2215 | if (cb != null && mFeatureId < 0) { |
Dianne Hackborn | 93a0a5f | 2009-08-20 19:33:02 -0700 | [diff] [blame] | 2216 | cb.onDetachedFromWindow(); |
| 2217 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 2218 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 2219 | if (mActionBar != null) { |
| 2220 | mActionBar.dismissPopupMenus(); |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 2221 | } |
Adam Powell | d3c9704 | 2011-02-28 21:25:37 -0800 | [diff] [blame] | 2222 | |
| 2223 | if (mActionModePopup != null) { |
| 2224 | removeCallbacks(mShowActionModePopup); |
| 2225 | if (mActionModePopup.isShowing()) { |
| 2226 | mActionModePopup.dismiss(); |
| 2227 | } |
| 2228 | mActionModePopup = null; |
| 2229 | } |
Dianne Hackborn | 93a0a5f | 2009-08-20 19:33:02 -0700 | [diff] [blame] | 2230 | } |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 2231 | |
| 2232 | @Override |
Dianne Hackborn | 4994c66 | 2009-09-23 22:21:23 -0700 | [diff] [blame] | 2233 | public void onCloseSystemDialogs(String reason) { |
| 2234 | if (mFeatureId >= 0) { |
| 2235 | closeAllPanels(); |
| 2236 | } |
| 2237 | } |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 2238 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 2239 | public android.view.SurfaceHolder.Callback2 willYouTakeTheSurface() { |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 2240 | return mFeatureId < 0 ? mTakeSurfaceCallback : null; |
| 2241 | } |
| 2242 | |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 2243 | public InputQueue.Callback willYouTakeTheInputQueue() { |
| 2244 | return mFeatureId < 0 ? mTakeInputQueueCallback : null; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 2247 | public void setSurfaceType(int type) { |
| 2248 | PhoneWindow.this.setType(type); |
| 2249 | } |
| 2250 | |
| 2251 | public void setSurfaceFormat(int format) { |
| 2252 | PhoneWindow.this.setFormat(format); |
| 2253 | } |
| 2254 | |
| 2255 | public void setSurfaceKeepScreenOn(boolean keepOn) { |
| 2256 | if (keepOn) PhoneWindow.this.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 2257 | else PhoneWindow.this.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 2258 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 2259 | |
| 2260 | /** |
| 2261 | * Clears out internal reference when the action mode is destroyed. |
| 2262 | */ |
| 2263 | private class ActionModeCallbackWrapper implements ActionMode.Callback { |
| 2264 | private ActionMode.Callback mWrapped; |
| 2265 | |
| 2266 | public ActionModeCallbackWrapper(ActionMode.Callback wrapped) { |
| 2267 | mWrapped = wrapped; |
| 2268 | } |
| 2269 | |
| 2270 | public boolean onCreateActionMode(ActionMode mode, Menu menu) { |
| 2271 | return mWrapped.onCreateActionMode(mode, menu); |
| 2272 | } |
| 2273 | |
| 2274 | public boolean onPrepareActionMode(ActionMode mode, Menu menu) { |
| 2275 | return mWrapped.onPrepareActionMode(mode, menu); |
| 2276 | } |
| 2277 | |
| 2278 | public boolean onActionItemClicked(ActionMode mode, MenuItem item) { |
| 2279 | return mWrapped.onActionItemClicked(mode, item); |
| 2280 | } |
| 2281 | |
| 2282 | public void onDestroyActionMode(ActionMode mode) { |
| 2283 | mWrapped.onDestroyActionMode(mode); |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 2284 | if (mActionModePopup != null) { |
Adam Powell | d3c9704 | 2011-02-28 21:25:37 -0800 | [diff] [blame] | 2285 | removeCallbacks(mShowActionModePopup); |
Adam Powell | 85446e9 | 2010-10-22 17:43:56 -0700 | [diff] [blame] | 2286 | mActionModePopup.dismiss(); |
| 2287 | } else if (mActionModeView != null) { |
| 2288 | mActionModeView.setVisibility(GONE); |
| 2289 | } |
Adam Powell | 04253aa | 2010-08-18 11:59:11 -0700 | [diff] [blame] | 2290 | if (mActionModeView != null) { |
| 2291 | mActionModeView.removeAllViews(); |
| 2292 | } |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 2293 | if (getCallback() != null && !isDestroyed()) { |
| 2294 | try { |
| 2295 | getCallback().onActionModeFinished(mActionMode); |
| 2296 | } catch (AbstractMethodError ame) { |
| 2297 | // Older apps might not implement this callback method. |
| 2298 | } |
Adam Powell | bf85f43 | 2010-11-15 22:16:50 -0800 | [diff] [blame] | 2299 | } |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 2300 | mActionMode = null; |
| 2301 | } |
| 2302 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | protected DecorView generateDecor() { |
| 2306 | return new DecorView(getContext(), -1); |
| 2307 | } |
| 2308 | |
| 2309 | protected void setFeatureFromAttrs(int featureId, TypedArray attrs, |
| 2310 | int drawableAttr, int alphaAttr) { |
| 2311 | Drawable d = attrs.getDrawable(drawableAttr); |
| 2312 | if (d != null) { |
| 2313 | requestFeature(featureId); |
| 2314 | setFeatureDefaultDrawable(featureId, d); |
| 2315 | } |
| 2316 | if ((getFeatures() & (1 << featureId)) != 0) { |
| 2317 | int alpha = attrs.getInt(alphaAttr, -1); |
| 2318 | if (alpha >= 0) { |
| 2319 | setFeatureDrawableAlpha(featureId, alpha); |
| 2320 | } |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | protected ViewGroup generateLayout(DecorView decor) { |
| 2325 | // Apply data from current theme. |
| 2326 | |
| 2327 | TypedArray a = getWindowStyle(); |
| 2328 | |
| 2329 | if (false) { |
| 2330 | System.out.println("From style:"); |
| 2331 | String s = "Attrs:"; |
| 2332 | for (int i = 0; i < com.android.internal.R.styleable.Window.length; i++) { |
| 2333 | s = s + " " + Integer.toHexString(com.android.internal.R.styleable.Window[i]) + "=" |
| 2334 | + a.getString(i); |
| 2335 | } |
| 2336 | System.out.println(s); |
| 2337 | } |
| 2338 | |
| 2339 | mIsFloating = a.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating, false); |
| 2340 | int flagsToUpdate = (FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR) |
| 2341 | & (~getForcedWindowFlags()); |
| 2342 | if (mIsFloating) { |
| 2343 | setLayout(WRAP_CONTENT, WRAP_CONTENT); |
| 2344 | setFlags(0, flagsToUpdate); |
| 2345 | } else { |
| 2346 | setFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR, flagsToUpdate); |
| 2347 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2348 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2349 | if (a.getBoolean(com.android.internal.R.styleable.Window_windowNoTitle, false)) { |
| 2350 | requestFeature(FEATURE_NO_TITLE); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2351 | } else if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionBar, false)) { |
| 2352 | // Don't allow an action bar if there is no title. |
| 2353 | requestFeature(FEATURE_ACTION_BAR); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2354 | } |
| 2355 | |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 2356 | if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionBarOverlay, false)) { |
| 2357 | requestFeature(FEATURE_ACTION_BAR_OVERLAY); |
| 2358 | } |
| 2359 | |
Adam Powell | 5d27977 | 2010-07-27 16:34:07 -0700 | [diff] [blame] | 2360 | if (a.getBoolean(com.android.internal.R.styleable.Window_windowActionModeOverlay, false)) { |
| 2361 | requestFeature(FEATURE_ACTION_MODE_OVERLAY); |
| 2362 | } |
| 2363 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2364 | if (a.getBoolean(com.android.internal.R.styleable.Window_windowFullscreen, false)) { |
| 2365 | setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN&(~getForcedWindowFlags())); |
| 2366 | } |
| 2367 | |
Dianne Hackborn | 4bf7bcf | 2009-08-09 17:23:00 -0700 | [diff] [blame] | 2368 | if (a.getBoolean(com.android.internal.R.styleable.Window_windowShowWallpaper, false)) { |
| 2369 | setFlags(FLAG_SHOW_WALLPAPER, FLAG_SHOW_WALLPAPER&(~getForcedWindowFlags())); |
| 2370 | } |
| 2371 | |
Jeff Brown | 46e7529 | 2010-11-10 16:53:45 -0800 | [diff] [blame] | 2372 | if (a.getBoolean(com.android.internal.R.styleable.Window_windowEnableSplitTouch, |
| 2373 | getContext().getApplicationInfo().targetSdkVersion |
| 2374 | >= android.os.Build.VERSION_CODES.HONEYCOMB)) { |
| 2375 | setFlags(FLAG_SPLIT_TOUCH, FLAG_SPLIT_TOUCH&(~getForcedWindowFlags())); |
| 2376 | } |
| 2377 | |
Dianne Hackborn | 6014527 | 2011-01-11 23:45:09 -0800 | [diff] [blame] | 2378 | a.getValue(com.android.internal.R.styleable.Window_windowMinWidthMajor, mMinWidthMajor); |
| 2379 | a.getValue(com.android.internal.R.styleable.Window_windowMinWidthMinor, mMinWidthMinor); |
| 2380 | |
Daniel Sandler | e02d808 | 2010-10-08 15:13:22 -0400 | [diff] [blame] | 2381 | if (getContext().getApplicationInfo().targetSdkVersion |
| 2382 | < android.os.Build.VERSION_CODES.HONEYCOMB) { |
| 2383 | addFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY); |
| 2384 | } |
| 2385 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2386 | if (mAlwaysReadCloseOnTouchAttr || getContext().getApplicationInfo().targetSdkVersion |
| 2387 | >= android.os.Build.VERSION_CODES.HONEYCOMB) { |
Dianne Hackborn | ef57575 | 2011-01-18 17:35:17 -0800 | [diff] [blame] | 2388 | if (a.getBoolean( |
| 2389 | com.android.internal.R.styleable.Window_windowCloseOnTouchOutside, |
| 2390 | false)) { |
| 2391 | setCloseOnTouchOutsideIfNotSet(true); |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2392 | } |
| 2393 | } |
| 2394 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2395 | WindowManager.LayoutParams params = getAttributes(); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2396 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2397 | if (!hasSoftInputMode()) { |
| 2398 | params.softInputMode = a.getInt( |
| 2399 | com.android.internal.R.styleable.Window_windowSoftInputMode, |
| 2400 | params.softInputMode); |
| 2401 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2402 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2403 | if (a.getBoolean(com.android.internal.R.styleable.Window_backgroundDimEnabled, |
| 2404 | mIsFloating)) { |
| 2405 | /* All dialogs should have the window dimmed */ |
| 2406 | if ((getForcedWindowFlags()&WindowManager.LayoutParams.FLAG_DIM_BEHIND) == 0) { |
| 2407 | params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND; |
| 2408 | } |
| 2409 | params.dimAmount = a.getFloat( |
| 2410 | android.R.styleable.Window_backgroundDimAmount, 0.5f); |
| 2411 | } |
| 2412 | |
| 2413 | if (params.windowAnimations == 0) { |
| 2414 | params.windowAnimations = a.getResourceId( |
| 2415 | com.android.internal.R.styleable.Window_windowAnimationStyle, 0); |
| 2416 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2417 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2418 | // The rest are only done if this window is not embedded; otherwise, |
| 2419 | // the values are inherited from our container. |
| 2420 | if (getContainer() == null) { |
| 2421 | if (mBackgroundDrawable == null) { |
| 2422 | if (mBackgroundResource == 0) { |
| 2423 | mBackgroundResource = a.getResourceId( |
| 2424 | com.android.internal.R.styleable.Window_windowBackground, 0); |
| 2425 | } |
| 2426 | if (mFrameResource == 0) { |
| 2427 | mFrameResource = a.getResourceId(com.android.internal.R.styleable.Window_windowFrame, 0); |
| 2428 | } |
| 2429 | if (false) { |
| 2430 | System.out.println("Background: " |
| 2431 | + Integer.toHexString(mBackgroundResource) + " Frame: " |
| 2432 | + Integer.toHexString(mFrameResource)); |
| 2433 | } |
| 2434 | } |
| 2435 | mTextColor = a.getColor(com.android.internal.R.styleable.Window_textColor, 0xFF000000); |
| 2436 | } |
| 2437 | |
| 2438 | // Inflate the window decor. |
| 2439 | |
| 2440 | int layoutResource; |
| 2441 | int features = getLocalFeatures(); |
| 2442 | // System.out.println("Features: 0x" + Integer.toHexString(features)); |
| 2443 | if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) { |
| 2444 | if (mIsFloating) { |
Adam Powell | 32aa2c9 | 2011-01-11 15:37:04 -0800 | [diff] [blame] | 2445 | TypedValue res = new TypedValue(); |
| 2446 | getContext().getTheme().resolveAttribute( |
| 2447 | com.android.internal.R.attr.dialogTitleIconsDecorLayout, res, true); |
| 2448 | layoutResource = res.resourceId; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2449 | } else { |
| 2450 | layoutResource = com.android.internal.R.layout.screen_title_icons; |
| 2451 | } |
Adam Powell | f4a6ec4 | 2010-08-24 14:18:10 -0700 | [diff] [blame] | 2452 | // XXX Remove this once action bar supports these features. |
| 2453 | removeFeature(FEATURE_ACTION_BAR); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2454 | // System.out.println("Title Icons!"); |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 2455 | } else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0 |
| 2456 | && (features & (1 << FEATURE_ACTION_BAR)) == 0) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2457 | // Special case for a window with only a progress bar (and title). |
| 2458 | // XXX Need to have a no-title version of embedded windows. |
| 2459 | layoutResource = com.android.internal.R.layout.screen_progress; |
| 2460 | // System.out.println("Progress!"); |
| 2461 | } else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) { |
| 2462 | // Special case for a window with a custom title. |
| 2463 | // If the window is floating, we need a dialog layout |
| 2464 | if (mIsFloating) { |
Adam Powell | 32aa2c9 | 2011-01-11 15:37:04 -0800 | [diff] [blame] | 2465 | TypedValue res = new TypedValue(); |
| 2466 | getContext().getTheme().resolveAttribute( |
| 2467 | com.android.internal.R.attr.dialogCustomTitleDecorLayout, res, true); |
| 2468 | layoutResource = res.resourceId; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2469 | } else { |
| 2470 | layoutResource = com.android.internal.R.layout.screen_custom_title; |
| 2471 | } |
Adam Powell | f4a6ec4 | 2010-08-24 14:18:10 -0700 | [diff] [blame] | 2472 | // XXX Remove this once action bar supports these features. |
| 2473 | removeFeature(FEATURE_ACTION_BAR); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2474 | } else if ((features & (1 << FEATURE_NO_TITLE)) == 0) { |
| 2475 | // If no other features and not embedded, only need a title. |
| 2476 | // If the window is floating, we need a dialog layout |
| 2477 | if (mIsFloating) { |
Adam Powell | 32aa2c9 | 2011-01-11 15:37:04 -0800 | [diff] [blame] | 2478 | TypedValue res = new TypedValue(); |
| 2479 | getContext().getTheme().resolveAttribute( |
| 2480 | com.android.internal.R.attr.dialogTitleDecorLayout, res, true); |
| 2481 | layoutResource = res.resourceId; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2482 | } else if ((features & (1 << FEATURE_ACTION_BAR)) != 0) { |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 2483 | if ((features & (1 << FEATURE_ACTION_BAR_OVERLAY)) != 0) { |
| 2484 | layoutResource = com.android.internal.R.layout.screen_action_bar_overlay; |
| 2485 | } else { |
| 2486 | layoutResource = com.android.internal.R.layout.screen_action_bar; |
| 2487 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2488 | } else { |
| 2489 | layoutResource = com.android.internal.R.layout.screen_title; |
| 2490 | } |
| 2491 | // System.out.println("Title!"); |
| 2492 | } else { |
| 2493 | // Embedded, so no decoration is needed. |
| 2494 | layoutResource = com.android.internal.R.layout.screen_simple; |
| 2495 | // System.out.println("Simple!"); |
| 2496 | } |
| 2497 | |
| 2498 | mDecor.startChanging(); |
| 2499 | |
| 2500 | View in = mLayoutInflater.inflate(layoutResource, null); |
Romain Guy | cc6828c | 2010-01-08 15:06:37 -0800 | [diff] [blame] | 2501 | decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2502 | |
| 2503 | ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT); |
| 2504 | if (contentParent == null) { |
| 2505 | throw new RuntimeException("Window couldn't find content container view"); |
| 2506 | } |
| 2507 | |
| 2508 | if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { |
| 2509 | ProgressBar progress = getCircularProgressBar(false); |
| 2510 | if (progress != null) { |
| 2511 | progress.setIndeterminate(true); |
| 2512 | } |
| 2513 | } |
| 2514 | |
| 2515 | // Remaining setup -- of background and title -- that only applies |
| 2516 | // to top-level windows. |
| 2517 | if (getContainer() == null) { |
| 2518 | Drawable drawable = mBackgroundDrawable; |
| 2519 | if (mBackgroundResource != 0) { |
| 2520 | drawable = getContext().getResources().getDrawable(mBackgroundResource); |
| 2521 | } |
| 2522 | mDecor.setWindowBackground(drawable); |
| 2523 | drawable = null; |
| 2524 | if (mFrameResource != 0) { |
| 2525 | drawable = getContext().getResources().getDrawable(mFrameResource); |
| 2526 | } |
| 2527 | mDecor.setWindowFrame(drawable); |
| 2528 | |
| 2529 | // System.out.println("Text=" + Integer.toHexString(mTextColor) + |
| 2530 | // " Sel=" + Integer.toHexString(mTextSelectedColor) + |
| 2531 | // " Title=" + Integer.toHexString(mTitleColor)); |
| 2532 | |
| 2533 | if (mTitleColor == 0) { |
| 2534 | mTitleColor = mTextColor; |
| 2535 | } |
| 2536 | |
| 2537 | if (mTitle != null) { |
| 2538 | setTitle(mTitle); |
| 2539 | } |
| 2540 | setTitleColor(mTitleColor); |
| 2541 | } |
| 2542 | |
| 2543 | mDecor.finishChanging(); |
| 2544 | |
| 2545 | return contentParent; |
| 2546 | } |
| 2547 | |
Dianne Hackborn | cfaf887 | 2011-01-18 13:57:54 -0800 | [diff] [blame] | 2548 | /** @hide */ |
| 2549 | public void alwaysReadCloseOnTouchAttr() { |
| 2550 | mAlwaysReadCloseOnTouchAttr = true; |
| 2551 | } |
| 2552 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2553 | private void installDecor() { |
| 2554 | if (mDecor == null) { |
| 2555 | mDecor = generateDecor(); |
Adam Powell | 00f4d98 | 2010-03-24 11:17:03 -0700 | [diff] [blame] | 2556 | mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2557 | mDecor.setIsRootNamespace(true); |
| 2558 | } |
| 2559 | if (mContentParent == null) { |
| 2560 | mContentParent = generateLayout(mDecor); |
| 2561 | |
| 2562 | mTitleView = (TextView)findViewById(com.android.internal.R.id.title); |
| 2563 | if (mTitleView != null) { |
| 2564 | if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) { |
| 2565 | View titleContainer = findViewById(com.android.internal.R.id.title_container); |
| 2566 | if (titleContainer != null) { |
| 2567 | titleContainer.setVisibility(View.GONE); |
| 2568 | } else { |
| 2569 | mTitleView.setVisibility(View.GONE); |
| 2570 | } |
| 2571 | if (mContentParent instanceof FrameLayout) { |
| 2572 | ((FrameLayout)mContentParent).setForeground(null); |
| 2573 | } |
| 2574 | } else { |
| 2575 | mTitleView.setText(mTitle); |
| 2576 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2577 | } else { |
| 2578 | mActionBar = (ActionBarView) findViewById(com.android.internal.R.id.action_bar); |
Adam Powell | 6dd73b4 | 2010-08-06 11:13:40 -0700 | [diff] [blame] | 2579 | if (mActionBar != null) { |
| 2580 | if (mActionBar.getTitle() == null) { |
| 2581 | mActionBar.setWindowTitle(mTitle); |
| 2582 | } |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 2583 | final int localFeatures = getLocalFeatures(); |
| 2584 | if ((localFeatures & (1 << FEATURE_PROGRESS)) != 0) { |
| 2585 | mActionBar.initProgress(); |
| 2586 | } |
| 2587 | if ((localFeatures & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) { |
| 2588 | mActionBar.initIndeterminateProgress(); |
| 2589 | } |
Adam Powell | 6dd73b4 | 2010-08-06 11:13:40 -0700 | [diff] [blame] | 2590 | // Post the panel invalidate for later; avoid application onCreateOptionsMenu |
| 2591 | // being called in the middle of onCreate or similar. |
| 2592 | mDecor.post(new Runnable() { |
| 2593 | public void run() { |
Dianne Hackborn | 291905e | 2010-08-17 15:17:15 -0700 | [diff] [blame] | 2594 | if (!isDestroyed()) { |
| 2595 | invalidatePanelMenu(FEATURE_ACTION_BAR); |
| 2596 | } |
Adam Powell | 6dd73b4 | 2010-08-06 11:13:40 -0700 | [diff] [blame] | 2597 | } |
| 2598 | }); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 2599 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | private Drawable loadImageURI(Uri uri) { |
| 2605 | try { |
| 2606 | return Drawable.createFromStream( |
| 2607 | getContext().getContentResolver().openInputStream(uri), null); |
| 2608 | } catch (Exception e) { |
| 2609 | Log.w(TAG, "Unable to open content: " + uri); |
| 2610 | } |
| 2611 | return null; |
| 2612 | } |
| 2613 | |
| 2614 | private DrawableFeatureState getDrawableState(int featureId, boolean required) { |
| 2615 | if ((getFeatures() & (1 << featureId)) == 0) { |
| 2616 | if (!required) { |
| 2617 | return null; |
| 2618 | } |
| 2619 | throw new RuntimeException("The feature has not been requested"); |
| 2620 | } |
| 2621 | |
| 2622 | DrawableFeatureState[] ar; |
| 2623 | if ((ar = mDrawables) == null || ar.length <= featureId) { |
| 2624 | DrawableFeatureState[] nar = new DrawableFeatureState[featureId + 1]; |
| 2625 | if (ar != null) { |
| 2626 | System.arraycopy(ar, 0, nar, 0, ar.length); |
| 2627 | } |
| 2628 | mDrawables = ar = nar; |
| 2629 | } |
| 2630 | |
| 2631 | DrawableFeatureState st = ar[featureId]; |
| 2632 | if (st == null) { |
| 2633 | ar[featureId] = st = new DrawableFeatureState(featureId); |
| 2634 | } |
| 2635 | return st; |
| 2636 | } |
| 2637 | |
| 2638 | /** |
| 2639 | * Gets a panel's state based on its feature ID. |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2640 | * |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2641 | * @param featureId The feature ID of the panel. |
| 2642 | * @param required Whether the panel is required (if it is required and it |
| 2643 | * isn't in our features, this throws an exception). |
| 2644 | * @return The panel state. |
| 2645 | */ |
| 2646 | private PanelFeatureState getPanelState(int featureId, boolean required) { |
| 2647 | return getPanelState(featureId, required, null); |
| 2648 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2649 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2650 | /** |
| 2651 | * Gets a panel's state based on its feature ID. |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2652 | * |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2653 | * @param featureId The feature ID of the panel. |
| 2654 | * @param required Whether the panel is required (if it is required and it |
| 2655 | * isn't in our features, this throws an exception). |
| 2656 | * @param convertPanelState Optional: If the panel state does not exist, use |
| 2657 | * this as the panel state. |
| 2658 | * @return The panel state. |
| 2659 | */ |
| 2660 | private PanelFeatureState getPanelState(int featureId, boolean required, |
| 2661 | PanelFeatureState convertPanelState) { |
| 2662 | if ((getFeatures() & (1 << featureId)) == 0) { |
| 2663 | if (!required) { |
| 2664 | return null; |
| 2665 | } |
| 2666 | throw new RuntimeException("The feature has not been requested"); |
| 2667 | } |
| 2668 | |
| 2669 | PanelFeatureState[] ar; |
| 2670 | if ((ar = mPanels) == null || ar.length <= featureId) { |
| 2671 | PanelFeatureState[] nar = new PanelFeatureState[featureId + 1]; |
| 2672 | if (ar != null) { |
| 2673 | System.arraycopy(ar, 0, nar, 0, ar.length); |
| 2674 | } |
| 2675 | mPanels = ar = nar; |
| 2676 | } |
| 2677 | |
| 2678 | PanelFeatureState st = ar[featureId]; |
| 2679 | if (st == null) { |
| 2680 | ar[featureId] = st = (convertPanelState != null) |
| 2681 | ? convertPanelState |
| 2682 | : new PanelFeatureState(featureId); |
| 2683 | } |
| 2684 | return st; |
| 2685 | } |
| 2686 | |
| 2687 | @Override |
| 2688 | public final void setChildDrawable(int featureId, Drawable drawable) { |
| 2689 | DrawableFeatureState st = getDrawableState(featureId, true); |
| 2690 | st.child = drawable; |
| 2691 | updateDrawable(featureId, st, false); |
| 2692 | } |
| 2693 | |
| 2694 | @Override |
| 2695 | public final void setChildInt(int featureId, int value) { |
| 2696 | updateInt(featureId, value, false); |
| 2697 | } |
| 2698 | |
| 2699 | @Override |
| 2700 | public boolean isShortcutKey(int keyCode, KeyEvent event) { |
| 2701 | PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, true); |
| 2702 | return st.menu != null && st.menu.isShortcutKey(keyCode, event); |
| 2703 | } |
| 2704 | |
| 2705 | private void updateDrawable(int featureId, DrawableFeatureState st, boolean fromResume) { |
| 2706 | // Do nothing if the decor is not yet installed... an update will |
| 2707 | // need to be forced when we eventually become active. |
| 2708 | if (mContentParent == null) { |
| 2709 | return; |
| 2710 | } |
| 2711 | |
| 2712 | final int featureMask = 1 << featureId; |
| 2713 | |
| 2714 | if ((getFeatures() & featureMask) == 0 && !fromResume) { |
| 2715 | return; |
| 2716 | } |
| 2717 | |
| 2718 | Drawable drawable = null; |
| 2719 | if (st != null) { |
| 2720 | drawable = st.child; |
| 2721 | if (drawable == null) |
| 2722 | drawable = st.local; |
| 2723 | if (drawable == null) |
| 2724 | drawable = st.def; |
| 2725 | } |
| 2726 | if ((getLocalFeatures() & featureMask) == 0) { |
| 2727 | if (getContainer() != null) { |
| 2728 | if (isActive() || fromResume) { |
| 2729 | getContainer().setChildDrawable(featureId, drawable); |
| 2730 | } |
| 2731 | } |
| 2732 | } else if (st != null && (st.cur != drawable || st.curAlpha != st.alpha)) { |
| 2733 | // System.out.println("Drawable changed: old=" + st.cur |
| 2734 | // + ", new=" + drawable); |
| 2735 | st.cur = drawable; |
| 2736 | st.curAlpha = st.alpha; |
| 2737 | onDrawableChanged(featureId, drawable, st.alpha); |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | private void updateInt(int featureId, int value, boolean fromResume) { |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2742 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2743 | // Do nothing if the decor is not yet installed... an update will |
| 2744 | // need to be forced when we eventually become active. |
| 2745 | if (mContentParent == null) { |
| 2746 | return; |
| 2747 | } |
| 2748 | |
| 2749 | final int featureMask = 1 << featureId; |
| 2750 | |
| 2751 | if ((getFeatures() & featureMask) == 0 && !fromResume) { |
| 2752 | return; |
| 2753 | } |
| 2754 | |
| 2755 | if ((getLocalFeatures() & featureMask) == 0) { |
| 2756 | if (getContainer() != null) { |
| 2757 | getContainer().setChildInt(featureId, value); |
| 2758 | } |
| 2759 | } else { |
| 2760 | onIntChanged(featureId, value); |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | private ImageView getLeftIconView() { |
| 2765 | if (mLeftIconView != null) { |
| 2766 | return mLeftIconView; |
| 2767 | } |
| 2768 | if (mContentParent == null) { |
| 2769 | installDecor(); |
| 2770 | } |
| 2771 | return (mLeftIconView = (ImageView)findViewById(com.android.internal.R.id.left_icon)); |
| 2772 | } |
| 2773 | |
| 2774 | private ProgressBar getCircularProgressBar(boolean shouldInstallDecor) { |
| 2775 | if (mCircularProgressBar != null) { |
| 2776 | return mCircularProgressBar; |
| 2777 | } |
| 2778 | if (mContentParent == null && shouldInstallDecor) { |
| 2779 | installDecor(); |
| 2780 | } |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 2781 | mCircularProgressBar = (ProgressBar) findViewById(com.android.internal.R.id.progress_circular); |
| 2782 | if (mCircularProgressBar != null) { |
| 2783 | mCircularProgressBar.setVisibility(View.INVISIBLE); |
| 2784 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2785 | return mCircularProgressBar; |
| 2786 | } |
| 2787 | |
| 2788 | private ProgressBar getHorizontalProgressBar(boolean shouldInstallDecor) { |
| 2789 | if (mHorizontalProgressBar != null) { |
| 2790 | return mHorizontalProgressBar; |
| 2791 | } |
| 2792 | if (mContentParent == null && shouldInstallDecor) { |
| 2793 | installDecor(); |
| 2794 | } |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 2795 | mHorizontalProgressBar = (ProgressBar) findViewById(com.android.internal.R.id.progress_horizontal); |
| 2796 | if (mHorizontalProgressBar != null) { |
| 2797 | mHorizontalProgressBar.setVisibility(View.INVISIBLE); |
| 2798 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2799 | return mHorizontalProgressBar; |
| 2800 | } |
| 2801 | |
| 2802 | private ImageView getRightIconView() { |
| 2803 | if (mRightIconView != null) { |
| 2804 | return mRightIconView; |
| 2805 | } |
| 2806 | if (mContentParent == null) { |
| 2807 | installDecor(); |
| 2808 | } |
| 2809 | return (mRightIconView = (ImageView)findViewById(com.android.internal.R.id.right_icon)); |
| 2810 | } |
| 2811 | |
| 2812 | /** |
| 2813 | * Helper method for calling the {@link Callback#onPanelClosed(int, Menu)} |
| 2814 | * callback. This method will grab whatever extra state is needed for the |
| 2815 | * callback that isn't given in the parameters. If the panel is not open, |
| 2816 | * this will not perform the callback. |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2817 | * |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2818 | * @param featureId Feature ID of the panel that was closed. Must be given. |
| 2819 | * @param panel Panel that was closed. Optional but useful if there is no |
| 2820 | * menu given. |
| 2821 | * @param menu The menu that was closed. Optional, but give if you have. |
| 2822 | */ |
| 2823 | private void callOnPanelClosed(int featureId, PanelFeatureState panel, Menu menu) { |
| 2824 | final Callback cb = getCallback(); |
| 2825 | if (cb == null) |
| 2826 | return; |
| 2827 | |
| 2828 | // Try to get a menu |
| 2829 | if (menu == null) { |
| 2830 | // Need a panel to grab the menu, so try to get that |
| 2831 | if (panel == null) { |
| 2832 | if ((featureId >= 0) && (featureId < mPanels.length)) { |
| 2833 | panel = mPanels[featureId]; |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | if (panel != null) { |
| 2838 | // menu still may be null, which is okay--we tried our best |
| 2839 | menu = panel.menu; |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | // If the panel is not open, do not callback |
| 2844 | if ((panel != null) && (!panel.isOpen)) |
| 2845 | return; |
| 2846 | |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 2847 | if (!isDestroyed()) { |
| 2848 | cb.onPanelClosed(featureId, menu); |
| 2849 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | /** |
| 2853 | * Helper method for adding launch-search to most applications. Opens the |
| 2854 | * search window using default settings. |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2855 | * |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2856 | * @return true if search window opened |
| 2857 | */ |
| 2858 | private boolean launchDefaultSearch() { |
| 2859 | final Callback cb = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 2860 | if (cb == null || isDestroyed()) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2861 | return false; |
| 2862 | } else { |
Karl Rosaen | 323216b | 2009-07-20 14:00:29 -0700 | [diff] [blame] | 2863 | sendCloseSystemWindows("search"); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2864 | return cb.onSearchRequested(); |
| 2865 | } |
| 2866 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2867 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2868 | @Override |
| 2869 | public void setVolumeControlStream(int streamType) { |
| 2870 | mVolumeControlStreamType = streamType; |
| 2871 | } |
| 2872 | |
| 2873 | @Override |
| 2874 | public int getVolumeControlStream() { |
| 2875 | return mVolumeControlStreamType; |
| 2876 | } |
| 2877 | |
| 2878 | private static final class DrawableFeatureState { |
| 2879 | DrawableFeatureState(int _featureId) { |
| 2880 | featureId = _featureId; |
| 2881 | } |
| 2882 | |
| 2883 | final int featureId; |
| 2884 | |
| 2885 | int resid; |
| 2886 | |
| 2887 | Uri uri; |
| 2888 | |
| 2889 | Drawable local; |
| 2890 | |
| 2891 | Drawable child; |
| 2892 | |
| 2893 | Drawable def; |
| 2894 | |
| 2895 | Drawable cur; |
| 2896 | |
| 2897 | int alpha = 255; |
| 2898 | |
| 2899 | int curAlpha = 255; |
| 2900 | } |
| 2901 | |
| 2902 | private static final class PanelFeatureState { |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2903 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2904 | /** Feature ID for this panel. */ |
| 2905 | int featureId; |
| 2906 | |
| 2907 | // Information pulled from the style for this panel. |
| 2908 | |
| 2909 | int background; |
| 2910 | |
| 2911 | /** The background when the panel spans the entire available width. */ |
| 2912 | int fullBackground; |
| 2913 | |
| 2914 | int gravity; |
| 2915 | |
| 2916 | int x; |
| 2917 | |
| 2918 | int y; |
| 2919 | |
| 2920 | int windowAnimations; |
| 2921 | |
| 2922 | /** Dynamic state of the panel. */ |
| 2923 | DecorView decorView; |
| 2924 | |
| 2925 | /** The panel that was returned by onCreatePanelView(). */ |
| 2926 | View createdPanelView; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2927 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2928 | /** The panel that we are actually showing. */ |
| 2929 | View shownPanelView; |
| 2930 | |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2931 | /** Use {@link #setMenu} to set this. */ |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 2932 | MenuBuilder menu; |
| 2933 | |
| 2934 | IconMenuPresenter iconMenuPresenter; |
| 2935 | ListMenuPresenter expandedMenuPresenter; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2936 | |
| 2937 | /** |
| 2938 | * Whether the panel has been prepared (see |
| 2939 | * {@link PhoneWindow#preparePanel}). |
| 2940 | */ |
| 2941 | boolean isPrepared; |
| 2942 | |
| 2943 | /** |
| 2944 | * Whether an item's action has been performed. This happens in obvious |
| 2945 | * scenarios (user clicks on menu item), but can also happen with |
| 2946 | * chording menu+(shortcut key). |
| 2947 | */ |
| 2948 | boolean isHandled; |
| 2949 | |
| 2950 | boolean isOpen; |
| 2951 | |
| 2952 | /** |
| 2953 | * True if the menu is in expanded mode, false if the menu is in icon |
| 2954 | * mode |
| 2955 | */ |
| 2956 | boolean isInExpandedMode; |
| 2957 | |
| 2958 | public boolean qwertyMode; |
| 2959 | |
| 2960 | boolean refreshDecorView; |
| 2961 | |
Dianne Hackborn | b31e84bc | 2010-06-08 18:04:35 -0700 | [diff] [blame] | 2962 | boolean refreshMenuContent; |
| 2963 | |
Dianne Hackborn | d007144 | 2009-09-25 01:35:29 -0700 | [diff] [blame] | 2964 | boolean wasLastOpen; |
| 2965 | |
| 2966 | boolean wasLastExpanded; |
| 2967 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2968 | /** |
| 2969 | * Contains the state of the menu when told to freeze. |
| 2970 | */ |
| 2971 | Bundle frozenMenuState; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 2972 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 2973 | PanelFeatureState(int featureId) { |
| 2974 | this.featureId = featureId; |
| 2975 | |
| 2976 | refreshDecorView = false; |
| 2977 | } |
| 2978 | |
Adam Powell | 526b931 | 2011-04-22 15:42:05 -0700 | [diff] [blame] | 2979 | public boolean hasPanelItems() { |
| 2980 | if (shownPanelView == null) return false; |
| 2981 | |
| 2982 | if (isInExpandedMode) { |
| 2983 | return expandedMenuPresenter.getAdapter().getCount() > 0; |
| 2984 | } else { |
| 2985 | return ((ViewGroup) shownPanelView).getChildCount() > 0; |
| 2986 | } |
| 2987 | } |
| 2988 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 2989 | /** |
| 2990 | * Unregister and free attached MenuPresenters. They will be recreated as needed. |
| 2991 | */ |
| 2992 | public void clearMenuPresenters() { |
| 2993 | if (menu != null) { |
| 2994 | menu.removeMenuPresenter(iconMenuPresenter); |
| 2995 | menu.removeMenuPresenter(expandedMenuPresenter); |
| 2996 | } |
| 2997 | iconMenuPresenter = null; |
| 2998 | expandedMenuPresenter = null; |
| 2999 | } |
| 3000 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3001 | void setStyle(Context context) { |
| 3002 | TypedArray a = context.obtainStyledAttributes(com.android.internal.R.styleable.Theme); |
| 3003 | background = a.getResourceId( |
| 3004 | com.android.internal.R.styleable.Theme_panelBackground, 0); |
| 3005 | fullBackground = a.getResourceId( |
| 3006 | com.android.internal.R.styleable.Theme_panelFullBackground, 0); |
| 3007 | windowAnimations = a.getResourceId( |
| 3008 | com.android.internal.R.styleable.Theme_windowAnimationStyle, 0); |
| 3009 | a.recycle(); |
| 3010 | } |
| 3011 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 3012 | void setMenu(MenuBuilder menu) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3013 | this.menu = menu; |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 3014 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3015 | |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 3016 | MenuView getExpandedMenuView(MenuPresenter.Callback cb) { |
| 3017 | if (menu == null) return null; |
| 3018 | |
| 3019 | getIconMenuView(cb); // Need this initialized to know where our offset goes |
| 3020 | |
| 3021 | boolean init = false; |
| 3022 | if (expandedMenuPresenter == null) { |
| 3023 | expandedMenuPresenter = new ListMenuPresenter( |
| 3024 | com.android.internal.R.layout.list_menu_item_layout, |
| 3025 | com.android.internal.R.style.Theme_ExpandedMenu); |
| 3026 | expandedMenuPresenter.setCallback(cb); |
| 3027 | menu.addMenuPresenter(expandedMenuPresenter); |
| 3028 | init = true; |
| 3029 | } |
| 3030 | |
| 3031 | expandedMenuPresenter.setItemIndexOffset(iconMenuPresenter.getNumActualItemsShown()); |
| 3032 | MenuView result = expandedMenuPresenter.getMenuView(decorView); |
| 3033 | |
| 3034 | if (init && frozenMenuState != null) { |
| 3035 | expandedMenuPresenter.restoreHierarchyState(frozenMenuState); |
| 3036 | // Once we initialize the expanded menu we're done with the frozen state |
| 3037 | // since we will have also restored any icon menu state. |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3038 | frozenMenuState = null; |
| 3039 | } |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 3040 | |
| 3041 | return result; |
| 3042 | } |
| 3043 | |
| 3044 | MenuView getIconMenuView(MenuPresenter.Callback cb) { |
| 3045 | if (menu == null) return null; |
| 3046 | |
| 3047 | boolean init = false; |
| 3048 | if (iconMenuPresenter == null) { |
| 3049 | iconMenuPresenter = new IconMenuPresenter(); |
| 3050 | iconMenuPresenter.setCallback(cb); |
| 3051 | menu.addMenuPresenter(iconMenuPresenter); |
| 3052 | init = true; |
| 3053 | } |
| 3054 | |
| 3055 | MenuView result = iconMenuPresenter.getMenuView(decorView); |
| 3056 | |
| 3057 | if (init && frozenMenuState != null) { |
| 3058 | iconMenuPresenter.restoreHierarchyState(frozenMenuState); |
| 3059 | } |
| 3060 | |
| 3061 | return result; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | Parcelable onSaveInstanceState() { |
| 3065 | SavedState savedState = new SavedState(); |
| 3066 | savedState.featureId = featureId; |
| 3067 | savedState.isOpen = isOpen; |
| 3068 | savedState.isInExpandedMode = isInExpandedMode; |
| 3069 | |
| 3070 | if (menu != null) { |
| 3071 | savedState.menuState = new Bundle(); |
Adam Powell | 696cba5 | 2011-03-29 10:38:16 -0700 | [diff] [blame] | 3072 | if (iconMenuPresenter != null) { |
| 3073 | iconMenuPresenter.saveHierarchyState(savedState.menuState); |
| 3074 | } |
| 3075 | if (expandedMenuPresenter != null) { |
| 3076 | expandedMenuPresenter.saveHierarchyState(savedState.menuState); |
| 3077 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3078 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3079 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3080 | return savedState; |
| 3081 | } |
| 3082 | |
| 3083 | void onRestoreInstanceState(Parcelable state) { |
| 3084 | SavedState savedState = (SavedState) state; |
| 3085 | featureId = savedState.featureId; |
Dianne Hackborn | d007144 | 2009-09-25 01:35:29 -0700 | [diff] [blame] | 3086 | wasLastOpen = savedState.isOpen; |
| 3087 | wasLastExpanded = savedState.isInExpandedMode; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3088 | frozenMenuState = savedState.menuState; |
| 3089 | |
| 3090 | /* |
| 3091 | * A LocalActivityManager keeps the same instance of this class around. |
| 3092 | * The first time the menu is being shown after restoring, the |
| 3093 | * Activity.onCreateOptionsMenu should be called. But, if it is the |
| 3094 | * same instance then menu != null and we won't call that method. |
| 3095 | * So, clear this. Also clear any cached views. |
| 3096 | */ |
| 3097 | menu = null; |
| 3098 | createdPanelView = null; |
| 3099 | shownPanelView = null; |
| 3100 | decorView = null; |
| 3101 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3102 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3103 | private static class SavedState implements Parcelable { |
| 3104 | int featureId; |
| 3105 | boolean isOpen; |
| 3106 | boolean isInExpandedMode; |
| 3107 | Bundle menuState; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3108 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3109 | public int describeContents() { |
| 3110 | return 0; |
| 3111 | } |
| 3112 | |
| 3113 | public void writeToParcel(Parcel dest, int flags) { |
| 3114 | dest.writeInt(featureId); |
| 3115 | dest.writeInt(isOpen ? 1 : 0); |
| 3116 | dest.writeInt(isInExpandedMode ? 1 : 0); |
| 3117 | |
| 3118 | if (isOpen) { |
| 3119 | dest.writeBundle(menuState); |
| 3120 | } |
| 3121 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3122 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3123 | private static SavedState readFromParcel(Parcel source) { |
| 3124 | SavedState savedState = new SavedState(); |
| 3125 | savedState.featureId = source.readInt(); |
| 3126 | savedState.isOpen = source.readInt() == 1; |
| 3127 | savedState.isInExpandedMode = source.readInt() == 1; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3128 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3129 | if (savedState.isOpen) { |
| 3130 | savedState.menuState = source.readBundle(); |
| 3131 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3132 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3133 | return savedState; |
| 3134 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3135 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3136 | public static final Parcelable.Creator<SavedState> CREATOR |
| 3137 | = new Parcelable.Creator<SavedState>() { |
| 3138 | public SavedState createFromParcel(Parcel in) { |
| 3139 | return readFromParcel(in); |
| 3140 | } |
| 3141 | |
| 3142 | public SavedState[] newArray(int size) { |
| 3143 | return new SavedState[size]; |
| 3144 | } |
| 3145 | }; |
| 3146 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3147 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | /** |
| 3151 | * Simple implementation of MenuBuilder.Callback that: |
| 3152 | * <li> Opens a submenu when selected. |
| 3153 | * <li> Calls back to the callback's onMenuItemSelected when an item is |
| 3154 | * selected. |
| 3155 | */ |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 3156 | private final class DialogMenuCallback implements MenuBuilder.Callback { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3157 | private int mFeatureId; |
| 3158 | private MenuDialogHelper mSubMenuHelper; |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3159 | |
Adam Powell | 4267534 | 2010-07-09 18:02:59 -0700 | [diff] [blame] | 3160 | public DialogMenuCallback(int featureId) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3161 | mFeatureId = featureId; |
| 3162 | } |
| 3163 | |
| 3164 | public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 3165 | if (allMenusAreClosing) { |
| 3166 | Callback callback = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 3167 | if (callback != null && !isDestroyed()) { |
| 3168 | callback.onPanelClosed(mFeatureId, menu); |
| 3169 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3170 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3171 | if (menu == mContextMenu) { |
| 3172 | dismissContextMenu(); |
| 3173 | } |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3174 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3175 | // Dismiss the submenu, if it is showing |
| 3176 | if (mSubMenuHelper != null) { |
| 3177 | mSubMenuHelper.dismiss(); |
| 3178 | mSubMenuHelper = null; |
| 3179 | } |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | public void onCloseSubMenu(SubMenuBuilder menu) { |
| 3184 | Callback callback = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 3185 | if (callback != null && !isDestroyed()) { |
| 3186 | callback.onPanelClosed(mFeatureId, menu.getRootMenu()); |
| 3187 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
| 3191 | Callback callback = getCallback(); |
Dianne Hackborn | b66ad57 | 2011-03-01 17:10:30 -0800 | [diff] [blame] | 3192 | return (callback != null && !isDestroyed()) |
| 3193 | && callback.onMenuItemSelected(mFeatureId, item); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | public void onMenuModeChange(MenuBuilder menu) { |
| 3197 | } |
| 3198 | |
| 3199 | public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
| 3200 | // Set a simple callback for the submenu |
| 3201 | subMenu.setCallback(this); |
| 3202 | |
| 3203 | // The window manager will give us a valid window token |
| 3204 | mSubMenuHelper = new MenuDialogHelper(subMenu); |
| 3205 | mSubMenuHelper.show(null); |
The Android Open Source Project | bc8d29f | 2009-03-05 20:00:44 -0800 | [diff] [blame] | 3206 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3207 | return true; |
| 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | void sendCloseSystemWindows() { |
| 3212 | PhoneWindowManager.sendCloseSystemWindows(getContext(), null); |
| 3213 | } |
| 3214 | |
| 3215 | void sendCloseSystemWindows(String reason) { |
| 3216 | PhoneWindowManager.sendCloseSystemWindows(getContext(), reason); |
| 3217 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 3218 | } |