michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import org.mozilla.gecko.widget.GeckoPopupMenu; michael@0: michael@0: import android.view.Menu; michael@0: import android.view.MenuItem; michael@0: import android.view.View; michael@0: michael@0: class ActionModeCompat implements GeckoPopupMenu.OnMenuItemClickListener, michael@0: View.OnClickListener { michael@0: private final String LOGTAG = "GeckoActionModeCompat"; michael@0: michael@0: private Callback mCallback; michael@0: private ActionModeCompatView mView; michael@0: private Presenter mPresenter; michael@0: michael@0: /* A set of callbacks to be called during this ActionMode's lifecycle. These will control the michael@0: * creation, interaction with, and destruction of menuitems for the view */ michael@0: public static interface Callback { michael@0: /* Called when action mode is first created. Implementors should use this to inflate menu resources. */ michael@0: public boolean onCreateActionMode(ActionModeCompat mode, Menu menu); michael@0: michael@0: /* Called to refresh an action mode's action menu. Called whenever the mode is invalidated. Implementors michael@0: * should use this to enable/disable/show/hide menu items. */ michael@0: public boolean onPrepareActionMode(ActionModeCompat mode, Menu menu); michael@0: michael@0: /* Called to report a user click on an action button. */ michael@0: public boolean onActionItemClicked(ActionModeCompat mode, MenuItem item); michael@0: michael@0: /* Called when an action mode is about to be exited and destroyed. */ michael@0: public void onDestroyActionMode(ActionModeCompat mode); michael@0: } michael@0: michael@0: /* Presenters handle the actual showing/hiding of the action mode UI in the app. Its their responsibility michael@0: * to create an action mode, and assign it Callbacks and ActionModeCompatView's. */ michael@0: public static interface Presenter { michael@0: /* Called when an action mode should be shown */ michael@0: public void startActionModeCompat(final Callback callback); michael@0: michael@0: /* Called when whatever action mode is showing should be hidden */ michael@0: public void endActionModeCompat(); michael@0: } michael@0: michael@0: public ActionModeCompat(Presenter presenter, Callback callback, ActionModeCompatView view) { michael@0: mPresenter = presenter; michael@0: mCallback = callback; michael@0: michael@0: mView = view; michael@0: mView.initForMode(this); michael@0: } michael@0: michael@0: public void finish() { michael@0: // Clearing the menu will also clear the ActionItemBar michael@0: mView.getMenu().clear(); michael@0: if (mCallback != null) { michael@0: mCallback.onDestroyActionMode(this); michael@0: } michael@0: } michael@0: michael@0: public CharSequence getTitle() { michael@0: return mView.getTitle(); michael@0: } michael@0: michael@0: public void setTitle(CharSequence title) { michael@0: mView.setTitle(title); michael@0: } michael@0: michael@0: public void setTitle(int resId) { michael@0: mView.setTitle(resId); michael@0: } michael@0: michael@0: public Menu getMenu() { michael@0: return mView.getMenu(); michael@0: } michael@0: michael@0: public void invalidate() { michael@0: if (mCallback != null) { michael@0: mCallback.onPrepareActionMode(this, mView.getMenu()); michael@0: } michael@0: mView.invalidate(); michael@0: } michael@0: michael@0: /* GeckoPopupMenu.OnMenuItemClickListener */ michael@0: @Override michael@0: public boolean onMenuItemClick(MenuItem item) { michael@0: if (mCallback != null) { michael@0: return mCallback.onActionItemClicked(this, item); michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /* View.OnClickListener*/ michael@0: @Override michael@0: public void onClick(View v) { michael@0: mPresenter.endActionModeCompat(); michael@0: } michael@0: }