| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.android.internal.view; |
| 17 |
|
| 18 |
import com.android.internal.view.menu.MenuBuilder; |
| 19 |
import com.android.internal.view.menu.MenuPopupHelper; |
| 20 |
import com.android.internal.view.menu.SubMenuBuilder; |
| 21 |
import com.android.internal.widget.ActionBarContextView; |
| 22 |
|
| 23 |
import android.content.Context; |
| 24 |
import android.view.ActionMode; |
| 25 |
import android.view.Menu; |
| 26 |
import android.view.MenuInflater; |
| 27 |
import android.view.MenuItem; |
| 28 |
import android.view.View; |
| 29 |
import android.view.accessibility.AccessibilityEvent; |
| 30 |
|
| 31 |
import java.lang.ref.WeakReference; |
| 32 |
|
| 33 |
public class StandaloneActionMode extends ActionMode implements MenuBuilder.Callback { |
| 34 |
private Context mContext; |
| 35 |
private ActionBarContextView mContextView; |
| 36 |
private ActionMode.Callback mCallback; |
| 37 |
private WeakReference<View> mCustomView; |
| 38 |
private boolean mFinished; |
| 39 |
private boolean mFocusable; |
| 40 |
|
| 41 |
private MenuBuilder mMenu; |
| 42 |
|
| 43 |
public StandaloneActionMode(Context context, ActionBarContextView view, |
| 44 |
ActionMode.Callback callback, boolean isFocusable) { |
| 45 |
mContext = context; |
| 46 |
mContextView = view; |
| 47 |
mCallback = callback; |
| 48 |
|
| 49 |
mMenu = new MenuBuilder(view.getContext()).setDefaultShowAsAction( |
| 50 |
MenuItem.SHOW_AS_ACTION_IF_ROOM); |
| 51 |
mMenu.setCallback(this); |
| 52 |
mFocusable = isFocusable; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void setTitle(CharSequence title) { |
| 57 |
mContextView.setTitle(title); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void setSubtitle(CharSequence subtitle) { |
| 62 |
mContextView.setSubtitle(subtitle); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void setTitle(int resId) { |
| 67 |
setTitle(resId != 0 ? mContext.getString(resId) : null); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void setSubtitle(int resId) { |
| 72 |
setSubtitle(resId != 0 ? mContext.getString(resId) : null); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public void setTitleOptionalHint(boolean titleOptional) { |
| 77 |
super.setTitleOptionalHint(titleOptional); |
| 78 |
mContextView.setTitleOptional(titleOptional); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public boolean isTitleOptional() { |
| 83 |
return mContextView.isTitleOptional(); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public void setCustomView(View view) { |
| 88 |
mContextView.setCustomView(view); |
| 89 |
mCustomView = view != null ? new WeakReference<View>(view) : null; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void invalidate() { |
| 94 |
mCallback.onPrepareActionMode(this, mMenu); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void finish() { |
| 99 |
if (mFinished) { |
| 100 |
return; |
| 101 |
} |
| 102 |
mFinished = true; |
| 103 |
|
| 104 |
mContextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
| 105 |
mCallback.onDestroyActionMode(this); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public Menu getMenu() { |
| 110 |
return mMenu; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public CharSequence getTitle() { |
| 115 |
return mContextView.getTitle(); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public CharSequence getSubtitle() { |
| 120 |
return mContextView.getSubtitle(); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public View getCustomView() { |
| 125 |
return mCustomView != null ? mCustomView.get() : null; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public MenuInflater getMenuInflater() { |
| 130 |
return new MenuInflater(mContextView.getContext()); |
| 131 |
} |
| 132 |
|
| 133 |
public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
| 134 |
return mCallback.onActionItemClicked(this, item); |
| 135 |
} |
| 136 |
|
| 137 |
public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) { |
| 138 |
} |
| 139 |
|
| 140 |
public boolean onSubMenuSelected(SubMenuBuilder subMenu) { |
| 141 |
if (!subMenu.hasVisibleItems()) { |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
new MenuPopupHelper(mContextView.getContext(), subMenu).show(); |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
public void onCloseSubMenu(SubMenuBuilder menu) { |
| 150 |
} |
| 151 |
|
| 152 |
public void onMenuModeChange(MenuBuilder menu) { |
| 153 |
invalidate(); |
| 154 |
mContextView.showOverflowMenu(); |
| 155 |
} |
| 156 |
|
| 157 |
public boolean isUiFocusable() { |
| 158 |
return mFocusable; |
| 159 |
} |
| 160 |
} |