1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/ActionModeCompat.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,101 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko; 1.9 + 1.10 +import org.mozilla.gecko.widget.GeckoPopupMenu; 1.11 + 1.12 +import android.view.Menu; 1.13 +import android.view.MenuItem; 1.14 +import android.view.View; 1.15 + 1.16 +class ActionModeCompat implements GeckoPopupMenu.OnMenuItemClickListener, 1.17 + View.OnClickListener { 1.18 + private final String LOGTAG = "GeckoActionModeCompat"; 1.19 + 1.20 + private Callback mCallback; 1.21 + private ActionModeCompatView mView; 1.22 + private Presenter mPresenter; 1.23 + 1.24 + /* A set of callbacks to be called during this ActionMode's lifecycle. These will control the 1.25 + * creation, interaction with, and destruction of menuitems for the view */ 1.26 + public static interface Callback { 1.27 + /* Called when action mode is first created. Implementors should use this to inflate menu resources. */ 1.28 + public boolean onCreateActionMode(ActionModeCompat mode, Menu menu); 1.29 + 1.30 + /* Called to refresh an action mode's action menu. Called whenever the mode is invalidated. Implementors 1.31 + * should use this to enable/disable/show/hide menu items. */ 1.32 + public boolean onPrepareActionMode(ActionModeCompat mode, Menu menu); 1.33 + 1.34 + /* Called to report a user click on an action button. */ 1.35 + public boolean onActionItemClicked(ActionModeCompat mode, MenuItem item); 1.36 + 1.37 + /* Called when an action mode is about to be exited and destroyed. */ 1.38 + public void onDestroyActionMode(ActionModeCompat mode); 1.39 + } 1.40 + 1.41 + /* Presenters handle the actual showing/hiding of the action mode UI in the app. Its their responsibility 1.42 + * to create an action mode, and assign it Callbacks and ActionModeCompatView's. */ 1.43 + public static interface Presenter { 1.44 + /* Called when an action mode should be shown */ 1.45 + public void startActionModeCompat(final Callback callback); 1.46 + 1.47 + /* Called when whatever action mode is showing should be hidden */ 1.48 + public void endActionModeCompat(); 1.49 + } 1.50 + 1.51 + public ActionModeCompat(Presenter presenter, Callback callback, ActionModeCompatView view) { 1.52 + mPresenter = presenter; 1.53 + mCallback = callback; 1.54 + 1.55 + mView = view; 1.56 + mView.initForMode(this); 1.57 + } 1.58 + 1.59 + public void finish() { 1.60 + // Clearing the menu will also clear the ActionItemBar 1.61 + mView.getMenu().clear(); 1.62 + if (mCallback != null) { 1.63 + mCallback.onDestroyActionMode(this); 1.64 + } 1.65 + } 1.66 + 1.67 + public CharSequence getTitle() { 1.68 + return mView.getTitle(); 1.69 + } 1.70 + 1.71 + public void setTitle(CharSequence title) { 1.72 + mView.setTitle(title); 1.73 + } 1.74 + 1.75 + public void setTitle(int resId) { 1.76 + mView.setTitle(resId); 1.77 + } 1.78 + 1.79 + public Menu getMenu() { 1.80 + return mView.getMenu(); 1.81 + } 1.82 + 1.83 + public void invalidate() { 1.84 + if (mCallback != null) { 1.85 + mCallback.onPrepareActionMode(this, mView.getMenu()); 1.86 + } 1.87 + mView.invalidate(); 1.88 + } 1.89 + 1.90 + /* GeckoPopupMenu.OnMenuItemClickListener */ 1.91 + @Override 1.92 + public boolean onMenuItemClick(MenuItem item) { 1.93 + if (mCallback != null) { 1.94 + return mCallback.onActionItemClicked(this, item); 1.95 + } 1.96 + return false; 1.97 + } 1.98 + 1.99 + /* View.OnClickListener*/ 1.100 + @Override 1.101 + public void onClick(View v) { 1.102 + mPresenter.endActionModeCompat(); 1.103 + } 1.104 +}