StandaloneActionMode.java revision 785c447b2bc625209706fd128ce61781c3a4183b
15d27977f9da482627ceb19317a2cd70467aff046Adam Powell/* 25d27977f9da482627ceb19317a2cd70467aff046Adam Powell * Copyright (C) 2010 The Android Open Source Project 35d27977f9da482627ceb19317a2cd70467aff046Adam Powell * 45d27977f9da482627ceb19317a2cd70467aff046Adam Powell * Licensed under the Apache License, Version 2.0 (the "License"); 55d27977f9da482627ceb19317a2cd70467aff046Adam Powell * you may not use this file except in compliance with the License. 65d27977f9da482627ceb19317a2cd70467aff046Adam Powell * You may obtain a copy of the License at 75d27977f9da482627ceb19317a2cd70467aff046Adam Powell * 85d27977f9da482627ceb19317a2cd70467aff046Adam Powell * http://www.apache.org/licenses/LICENSE-2.0 95d27977f9da482627ceb19317a2cd70467aff046Adam Powell * 105d27977f9da482627ceb19317a2cd70467aff046Adam Powell * Unless required by applicable law or agreed to in writing, software 115d27977f9da482627ceb19317a2cd70467aff046Adam Powell * distributed under the License is distributed on an "AS IS" BASIS, 125d27977f9da482627ceb19317a2cd70467aff046Adam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135d27977f9da482627ceb19317a2cd70467aff046Adam Powell * See the License for the specific language governing permissions and 145d27977f9da482627ceb19317a2cd70467aff046Adam Powell * limitations under the License. 155d27977f9da482627ceb19317a2cd70467aff046Adam Powell */ 165d27977f9da482627ceb19317a2cd70467aff046Adam Powellpackage com.android.internal.view; 175d27977f9da482627ceb19317a2cd70467aff046Adam Powell 185d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport com.android.internal.view.menu.MenuBuilder; 195d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport com.android.internal.view.menu.MenuPopupHelper; 205d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport com.android.internal.view.menu.SubMenuBuilder; 215d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport com.android.internal.widget.ActionBarContextView; 225d27977f9da482627ceb19317a2cd70467aff046Adam Powell 235d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.content.Context; 245d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.view.ActionMode; 255d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.view.Menu; 269168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powellimport android.view.MenuInflater; 275d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.view.MenuItem; 285d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport android.view.View; 2986ed436b83d6b71ff00d1c3db910f2952018489eAdam Powellimport android.view.accessibility.AccessibilityEvent; 305d27977f9da482627ceb19317a2cd70467aff046Adam Powell 315d27977f9da482627ceb19317a2cd70467aff046Adam Powellimport java.lang.ref.WeakReference; 325d27977f9da482627ceb19317a2cd70467aff046Adam Powell 335d27977f9da482627ceb19317a2cd70467aff046Adam Powellpublic class StandaloneActionMode extends ActionMode implements MenuBuilder.Callback { 345d27977f9da482627ceb19317a2cd70467aff046Adam Powell private Context mContext; 355d27977f9da482627ceb19317a2cd70467aff046Adam Powell private ActionBarContextView mContextView; 365d27977f9da482627ceb19317a2cd70467aff046Adam Powell private ActionMode.Callback mCallback; 375d27977f9da482627ceb19317a2cd70467aff046Adam Powell private WeakReference<View> mCustomView; 385d27977f9da482627ceb19317a2cd70467aff046Adam Powell private boolean mFinished; 39f8419a0299680ed580975b0fcb758990b4367db8Adam Powell private boolean mFocusable; 405d27977f9da482627ceb19317a2cd70467aff046Adam Powell 415d27977f9da482627ceb19317a2cd70467aff046Adam Powell private MenuBuilder mMenu; 425d27977f9da482627ceb19317a2cd70467aff046Adam Powell 435d27977f9da482627ceb19317a2cd70467aff046Adam Powell public StandaloneActionMode(Context context, ActionBarContextView view, 44f8419a0299680ed580975b0fcb758990b4367db8Adam Powell ActionMode.Callback callback, boolean isFocusable) { 455d27977f9da482627ceb19317a2cd70467aff046Adam Powell mContext = context; 465d27977f9da482627ceb19317a2cd70467aff046Adam Powell mContextView = view; 475d27977f9da482627ceb19317a2cd70467aff046Adam Powell mCallback = callback; 485d27977f9da482627ceb19317a2cd70467aff046Adam Powell 494d9861e7ec8488634d316b20981464de2ab7b6feAdam Powell mMenu = new MenuBuilder(context).setDefaultShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 505d27977f9da482627ceb19317a2cd70467aff046Adam Powell mMenu.setCallback(this); 51f8419a0299680ed580975b0fcb758990b4367db8Adam Powell mFocusable = isFocusable; 525d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 535d27977f9da482627ceb19317a2cd70467aff046Adam Powell 545d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 555d27977f9da482627ceb19317a2cd70467aff046Adam Powell public void setTitle(CharSequence title) { 565d27977f9da482627ceb19317a2cd70467aff046Adam Powell mContextView.setTitle(title); 575d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 585d27977f9da482627ceb19317a2cd70467aff046Adam Powell 595d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 605d27977f9da482627ceb19317a2cd70467aff046Adam Powell public void setSubtitle(CharSequence subtitle) { 615d27977f9da482627ceb19317a2cd70467aff046Adam Powell mContextView.setSubtitle(subtitle); 625d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 635d27977f9da482627ceb19317a2cd70467aff046Adam Powell 645d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 65c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell public void setTitle(int resId) { 66c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell setTitle(mContext.getString(resId)); 67c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell } 68c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell 69c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell @Override 70c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell public void setSubtitle(int resId) { 71c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell setSubtitle(mContext.getString(resId)); 72c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell } 73c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell 74c9ae2a24dc1fa274ca0916c91a2e9a2764ba4bb3Adam Powell @Override 75b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell public void setTitleOptionalHint(boolean titleOptional) { 76785c447b2bc625209706fd128ce61781c3a4183bAdam Powell super.setTitleOptionalHint(titleOptional); 77b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell mContextView.setTitleOptional(titleOptional); 78b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell } 79b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell 80b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell @Override 81b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell public boolean isTitleOptional() { 82b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell return mContextView.isTitleOptional(); 83b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell } 84b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell 85b98a81f86ab87f1d718f329f03256111fdabd8d1Adam Powell @Override 865d27977f9da482627ceb19317a2cd70467aff046Adam Powell public void setCustomView(View view) { 875d27977f9da482627ceb19317a2cd70467aff046Adam Powell mContextView.setCustomView(view); 885d27977f9da482627ceb19317a2cd70467aff046Adam Powell mCustomView = view != null ? new WeakReference<View>(view) : null; 895d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 905d27977f9da482627ceb19317a2cd70467aff046Adam Powell 915d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 925d27977f9da482627ceb19317a2cd70467aff046Adam Powell public void invalidate() { 935d27977f9da482627ceb19317a2cd70467aff046Adam Powell mCallback.onPrepareActionMode(this, mMenu); 945d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 955d27977f9da482627ceb19317a2cd70467aff046Adam Powell 965d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 975d27977f9da482627ceb19317a2cd70467aff046Adam Powell public void finish() { 985d27977f9da482627ceb19317a2cd70467aff046Adam Powell if (mFinished) { 995d27977f9da482627ceb19317a2cd70467aff046Adam Powell return; 1005d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1015d27977f9da482627ceb19317a2cd70467aff046Adam Powell mFinished = true; 1025d27977f9da482627ceb19317a2cd70467aff046Adam Powell 10386ed436b83d6b71ff00d1c3db910f2952018489eAdam Powell mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); 1045d27977f9da482627ceb19317a2cd70467aff046Adam Powell mCallback.onDestroyActionMode(this); 1055d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1065d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1075d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 1085d27977f9da482627ceb19317a2cd70467aff046Adam Powell public Menu getMenu() { 1095d27977f9da482627ceb19317a2cd70467aff046Adam Powell return mMenu; 1105d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1115d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1125d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 1135d27977f9da482627ceb19317a2cd70467aff046Adam Powell public CharSequence getTitle() { 1145d27977f9da482627ceb19317a2cd70467aff046Adam Powell return mContextView.getTitle(); 1155d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1165d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1175d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 1185d27977f9da482627ceb19317a2cd70467aff046Adam Powell public CharSequence getSubtitle() { 1195d27977f9da482627ceb19317a2cd70467aff046Adam Powell return mContextView.getSubtitle(); 1205d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1215d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1225d27977f9da482627ceb19317a2cd70467aff046Adam Powell @Override 1235d27977f9da482627ceb19317a2cd70467aff046Adam Powell public View getCustomView() { 1245d27977f9da482627ceb19317a2cd70467aff046Adam Powell return mCustomView != null ? mCustomView.get() : null; 1255d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1265d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1279168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powell @Override 1289168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powell public MenuInflater getMenuInflater() { 1294d9861e7ec8488634d316b20981464de2ab7b6feAdam Powell return new MenuInflater(mContext); 1309168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powell } 1319168f0b170c6a99371ae46e7d3f5d66c8c4c930dAdam Powell 1325d27977f9da482627ceb19317a2cd70467aff046Adam Powell public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { 1335d27977f9da482627ceb19317a2cd70467aff046Adam Powell return mCallback.onActionItemClicked(this, item); 1345d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1355d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1365d27977f9da482627ceb19317a2cd70467aff046Adam Powell public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 1375d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1385d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1395d27977f9da482627ceb19317a2cd70467aff046Adam Powell public boolean onSubMenuSelected(SubMenuBuilder subMenu) { 1405d27977f9da482627ceb19317a2cd70467aff046Adam Powell if (!subMenu.hasVisibleItems()) { 1415d27977f9da482627ceb19317a2cd70467aff046Adam Powell return true; 1425d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1435d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1445d27977f9da482627ceb19317a2cd70467aff046Adam Powell new MenuPopupHelper(mContext, subMenu).show(); 1455d27977f9da482627ceb19317a2cd70467aff046Adam Powell return true; 1465d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1475d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1485d27977f9da482627ceb19317a2cd70467aff046Adam Powell public void onCloseSubMenu(SubMenuBuilder menu) { 1495d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 1505d27977f9da482627ceb19317a2cd70467aff046Adam Powell 1515d27977f9da482627ceb19317a2cd70467aff046Adam Powell public void onMenuModeChange(MenuBuilder menu) { 152f6148c53f93978af678cc0559a4417b608a33ae1Adam Powell invalidate(); 153696cba573e651b0e4f18a4718627c8ccecb3bda0Adam Powell mContextView.showOverflowMenu(); 1545d27977f9da482627ceb19317a2cd70467aff046Adam Powell } 155f8419a0299680ed580975b0fcb758990b4367db8Adam Powell 156f8419a0299680ed580975b0fcb758990b4367db8Adam Powell public boolean isUiFocusable() { 157f8419a0299680ed580975b0fcb758990b4367db8Adam Powell return mFocusable; 158f8419a0299680ed580975b0fcb758990b4367db8Adam Powell } 1595d27977f9da482627ceb19317a2cd70467aff046Adam Powell} 160