Adam Powell | 5ee36c4 | 2011-06-02 12:59:43 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
| 16 | |
| 17 | package android.inputmethodservice; |
| 18 | |
| 19 | import com.android.internal.view.menu.MenuBuilder; |
| 20 | import com.android.internal.view.menu.MenuPopupHelper; |
| 21 | |
| 22 | import android.content.Context; |
| 23 | import android.util.AttributeSet; |
| 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.widget.Button; |
| 30 | import android.widget.LinearLayout; |
| 31 | |
| 32 | /** |
| 33 | * ExtractEditLayout provides an ActionMode presentation for the |
| 34 | * limited screen real estate in extract mode. |
| 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | public class ExtractEditLayout extends LinearLayout { |
| 39 | ExtractActionMode mActionMode; |
| 40 | Button mExtractActionButton; |
| 41 | Button mEditButton; |
| 42 | |
| 43 | public ExtractEditLayout(Context context) { |
| 44 | super(context); |
| 45 | } |
| 46 | |
| 47 | public ExtractEditLayout(Context context, AttributeSet attrs) { |
| 48 | super(context, attrs); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public ActionMode startActionModeForChild(View sourceView, ActionMode.Callback cb) { |
| 53 | final ExtractActionMode mode = new ExtractActionMode(cb); |
| 54 | if (mode.dispatchOnCreate()) { |
| 55 | mode.invalidate(); |
| 56 | mExtractActionButton.setVisibility(INVISIBLE); |
| 57 | mEditButton.setVisibility(VISIBLE); |
| 58 | mActionMode = mode; |
| 59 | return mode; |
| 60 | } |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onFinishInflate() { |
| 66 | super.onFinishInflate(); |
| 67 | mExtractActionButton = (Button) findViewById(com.android.internal.R.id.inputExtractAction); |
| 68 | mEditButton = (Button) findViewById(com.android.internal.R.id.inputExtractEditButton); |
| 69 | mEditButton.setOnClickListener(new OnClickListener() { |
| 70 | public void onClick(View clicked) { |
| 71 | if (mActionMode != null) { |
| 72 | new MenuPopupHelper(getContext(), mActionMode.mMenu, clicked).show(); |
| 73 | } |
| 74 | } |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | private class ExtractActionMode extends ActionMode implements MenuBuilder.Callback { |
| 79 | private ActionMode.Callback mCallback; |
| 80 | MenuBuilder mMenu; |
| 81 | |
| 82 | public ExtractActionMode(Callback cb) { |
| 83 | mMenu = new MenuBuilder(getContext()); |
| 84 | mMenu.setCallback(this); |
| 85 | mCallback = cb; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void setTitle(CharSequence title) { |
| 90 | // Title will not be shown. |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void setTitle(int resId) { |
| 95 | // Title will nor be shown. |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void setSubtitle(CharSequence subtitle) { |
| 100 | // Subtitle will not be shown. |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void setSubtitle(int resId) { |
| 105 | // Subtitle will not be shown. |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void setCustomView(View view) { |
| 110 | // Custom view is not supported here. |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void invalidate() { |
| 115 | mMenu.stopDispatchingItemsChanged(); |
| 116 | try { |
| 117 | mCallback.onPrepareActionMode(this, mMenu); |
| 118 | } finally { |
| 119 | mMenu.startDispatchingItemsChanged(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public boolean dispatchOnCreate() { |
| 124 | mMenu.stopDispatchingItemsChanged(); |
| 125 | try { |
| 126 | return mCallback.onCreateActionMode(this, mMenu); |
| 127 | } finally { |
| 128 | mMenu.startDispatchingItemsChanged(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void finish() { |
| 134 | if (mActionMode != this) { |
| 135 | // Not the active action mode - no-op |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | mCallback.onDestroyActionMode(this); |
| 140 | mCallback = null; |
| 141 | |
| 142 | mExtractActionButton.setVisibility(VISIBLE); |
| 143 | mEditButton.setVisibility(INVISIBLE); |
| 144 | |
| 145 | mActionMode = null; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Menu getMenu() { |
| 150 | return mMenu; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public CharSequence getTitle() { |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public CharSequence getSubtitle() { |
| 160 | return null; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public View getCustomView() { |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public MenuInflater getMenuInflater() { |
| 170 | return new MenuInflater(getContext()); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) { |
| 175 | return mCallback.onActionItemClicked(this, item); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void onMenuModeChange(MenuBuilder menu) { |
| 180 | } |
| 181 | |
| 182 | } |
| 183 | } |