1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package android.support.v7.view; 17 18import android.content.Context; 19import android.support.v4.view.MenuItemCompat; 20import android.support.v7.view.menu.MenuBuilder; 21import android.support.v7.view.menu.MenuPopupHelper; 22import android.support.v7.view.menu.SubMenuBuilder; 23import android.support.v7.widget.ActionBarContextView; 24import android.view.Menu; 25import android.view.MenuInflater; 26import android.view.MenuItem; 27import android.view.View; 28import android.view.accessibility.AccessibilityEvent; 29 30import java.lang.ref.WeakReference; 31 32/** 33 * @hide 34 */ 35public class StandaloneActionMode extends ActionMode implements MenuBuilder.Callback { 36 private Context mContext; 37 private ActionBarContextView mContextView; 38 private ActionMode.Callback mCallback; 39 private WeakReference<View> mCustomView; 40 private boolean mFinished; 41 private boolean mFocusable; 42 43 private MenuBuilder mMenu; 44 45 public StandaloneActionMode(Context context, ActionBarContextView view, 46 ActionMode.Callback callback, boolean isFocusable) { 47 mContext = context; 48 mContextView = view; 49 mCallback = callback; 50 51 mMenu = new MenuBuilder(view.getContext()).setDefaultShowAsAction( 52 MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); 53 mMenu.setCallback(this); 54 mFocusable = isFocusable; 55 } 56 57 @Override 58 public void setTitle(CharSequence title) { 59 mContextView.setTitle(title); 60 } 61 62 @Override 63 public void setSubtitle(CharSequence subtitle) { 64 mContextView.setSubtitle(subtitle); 65 } 66 67 @Override 68 public void setTitle(int resId) { 69 setTitle(mContext.getString(resId)); 70 } 71 72 @Override 73 public void setSubtitle(int resId) { 74 setSubtitle(mContext.getString(resId)); 75 } 76 77 @Override 78 public void setTitleOptionalHint(boolean titleOptional) { 79 super.setTitleOptionalHint(titleOptional); 80 mContextView.setTitleOptional(titleOptional); 81 } 82 83 @Override 84 public boolean isTitleOptional() { 85 return mContextView.isTitleOptional(); 86 } 87 88 @Override 89 public void setCustomView(View view) { 90 mContextView.setCustomView(view); 91 mCustomView = view != null ? new WeakReference<View>(view) : null; 92 } 93 94 @Override 95 public void invalidate() { 96 mCallback.onPrepareActionMode(this, mMenu); 97 } 98 99 @Override 100 public void finish() { 101 if (mFinished) { 102 return; 103 } 104 mFinished = true; 105 106 mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); 107 mCallback.onDestroyActionMode(this); 108 } 109 110 @Override 111 public Menu getMenu() { 112 return mMenu; 113 } 114 115 @Override 116 public CharSequence getTitle() { 117 return mContextView.getTitle(); 118 } 119 120 @Override 121 public CharSequence getSubtitle() { 122 return mContextView.getSubtitle(); 123 } 124 125 @Override 126 public View getCustomView() { 127 return mCustomView != null ? mCustomView.get() : null; 128 } 129 130 @Override 131 public MenuInflater getMenuInflater() { 132 return new SupportMenuInflater(mContextView.getContext()); 133 } 134 135 public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { 136 return mCallback.onActionItemClicked(this, item); 137 } 138 139 public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { 140 } 141 142 public boolean onSubMenuSelected(SubMenuBuilder subMenu) { 143 if (!subMenu.hasVisibleItems()) { 144 return true; 145 } 146 147 new MenuPopupHelper(mContextView.getContext(), subMenu).show(); 148 return true; 149 } 150 151 public void onCloseSubMenu(SubMenuBuilder menu) { 152 } 153 154 public void onMenuModeChange(MenuBuilder menu) { 155 invalidate(); 156 mContextView.showOverflowMenu(); 157 } 158 159 public boolean isUiFocusable() { 160 return mFocusable; 161 } 162} 163