Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.widget.GeckoPopupMenu; |
michael@0 | 8 | |
michael@0 | 9 | import android.view.Menu; |
michael@0 | 10 | import android.view.MenuItem; |
michael@0 | 11 | import android.view.View; |
michael@0 | 12 | |
michael@0 | 13 | class ActionModeCompat implements GeckoPopupMenu.OnMenuItemClickListener, |
michael@0 | 14 | View.OnClickListener { |
michael@0 | 15 | private final String LOGTAG = "GeckoActionModeCompat"; |
michael@0 | 16 | |
michael@0 | 17 | private Callback mCallback; |
michael@0 | 18 | private ActionModeCompatView mView; |
michael@0 | 19 | private Presenter mPresenter; |
michael@0 | 20 | |
michael@0 | 21 | /* A set of callbacks to be called during this ActionMode's lifecycle. These will control the |
michael@0 | 22 | * creation, interaction with, and destruction of menuitems for the view */ |
michael@0 | 23 | public static interface Callback { |
michael@0 | 24 | /* Called when action mode is first created. Implementors should use this to inflate menu resources. */ |
michael@0 | 25 | public boolean onCreateActionMode(ActionModeCompat mode, Menu menu); |
michael@0 | 26 | |
michael@0 | 27 | /* Called to refresh an action mode's action menu. Called whenever the mode is invalidated. Implementors |
michael@0 | 28 | * should use this to enable/disable/show/hide menu items. */ |
michael@0 | 29 | public boolean onPrepareActionMode(ActionModeCompat mode, Menu menu); |
michael@0 | 30 | |
michael@0 | 31 | /* Called to report a user click on an action button. */ |
michael@0 | 32 | public boolean onActionItemClicked(ActionModeCompat mode, MenuItem item); |
michael@0 | 33 | |
michael@0 | 34 | /* Called when an action mode is about to be exited and destroyed. */ |
michael@0 | 35 | public void onDestroyActionMode(ActionModeCompat mode); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /* Presenters handle the actual showing/hiding of the action mode UI in the app. Its their responsibility |
michael@0 | 39 | * to create an action mode, and assign it Callbacks and ActionModeCompatView's. */ |
michael@0 | 40 | public static interface Presenter { |
michael@0 | 41 | /* Called when an action mode should be shown */ |
michael@0 | 42 | public void startActionModeCompat(final Callback callback); |
michael@0 | 43 | |
michael@0 | 44 | /* Called when whatever action mode is showing should be hidden */ |
michael@0 | 45 | public void endActionModeCompat(); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | public ActionModeCompat(Presenter presenter, Callback callback, ActionModeCompatView view) { |
michael@0 | 49 | mPresenter = presenter; |
michael@0 | 50 | mCallback = callback; |
michael@0 | 51 | |
michael@0 | 52 | mView = view; |
michael@0 | 53 | mView.initForMode(this); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | public void finish() { |
michael@0 | 57 | // Clearing the menu will also clear the ActionItemBar |
michael@0 | 58 | mView.getMenu().clear(); |
michael@0 | 59 | if (mCallback != null) { |
michael@0 | 60 | mCallback.onDestroyActionMode(this); |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | public CharSequence getTitle() { |
michael@0 | 65 | return mView.getTitle(); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | public void setTitle(CharSequence title) { |
michael@0 | 69 | mView.setTitle(title); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | public void setTitle(int resId) { |
michael@0 | 73 | mView.setTitle(resId); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | public Menu getMenu() { |
michael@0 | 77 | return mView.getMenu(); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | public void invalidate() { |
michael@0 | 81 | if (mCallback != null) { |
michael@0 | 82 | mCallback.onPrepareActionMode(this, mView.getMenu()); |
michael@0 | 83 | } |
michael@0 | 84 | mView.invalidate(); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | /* GeckoPopupMenu.OnMenuItemClickListener */ |
michael@0 | 88 | @Override |
michael@0 | 89 | public boolean onMenuItemClick(MenuItem item) { |
michael@0 | 90 | if (mCallback != null) { |
michael@0 | 91 | return mCallback.onActionItemClicked(this, item); |
michael@0 | 92 | } |
michael@0 | 93 | return false; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | /* View.OnClickListener*/ |
michael@0 | 97 | @Override |
michael@0 | 98 | public void onClick(View v) { |
michael@0 | 99 | mPresenter.endActionModeCompat(); |
michael@0 | 100 | } |
michael@0 | 101 | } |