Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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.view; |
| 18 | |
| 19 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.StringRes; |
| 21 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 22 | /** |
Scott Main | 5e09593 | 2011-12-16 13:06:22 -0800 | [diff] [blame] | 23 | * Represents a contextual mode of the user interface. Action modes can be used to provide |
| 24 | * alternative interaction modes and replace parts of the normal UI until finished. |
| 25 | * Examples of good action modes include text selection and contextual actions. |
| 26 | * <div class="special reference"> |
| 27 | * <h3>Developer Guides</h3> |
| 28 | * <p>For information about how to provide contextual actions with {@code ActionMode}, |
Scott Main | ef0314b | 2012-02-17 14:37:58 -0800 | [diff] [blame] | 29 | * read the <a href="{@docRoot}guide/topics/ui/menus.html#context-menu">Menus</a> |
Scott Main | 5e09593 | 2011-12-16 13:06:22 -0800 | [diff] [blame] | 30 | * developer guide.</p> |
| 31 | * </div> |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 32 | */ |
| 33 | public abstract class ActionMode { |
Clara Bayarri | d6aeff1 | 2015-01-26 16:38:07 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * The action mode is treated as a Primary mode. This is the default. |
| 37 | * Use with {@link #setType}. |
| 38 | */ |
| 39 | public static final int TYPE_PRIMARY = 0; |
| 40 | /** |
| 41 | * The action mode is treated as a Floating Toolbar. |
| 42 | * Use with {@link #setType}. |
| 43 | */ |
| 44 | public static final int TYPE_FLOATING = 1; |
| 45 | |
Adam Powell | f178737 | 2011-07-14 22:37:06 -0700 | [diff] [blame] | 46 | private Object mTag; |
Adam Powell | 785c447 | 2012-05-02 21:25:39 -0700 | [diff] [blame] | 47 | private boolean mTitleOptionalHint; |
Clara Bayarri | d6aeff1 | 2015-01-26 16:38:07 +0000 | [diff] [blame] | 48 | private int mType = TYPE_PRIMARY; |
Adam Powell | f178737 | 2011-07-14 22:37:06 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Set a tag object associated with this ActionMode. |
| 52 | * |
| 53 | * <p>Like the tag available to views, this allows applications to associate arbitrary |
| 54 | * data with an ActionMode for later reference. |
| 55 | * |
| 56 | * @param tag Tag to associate with this ActionMode |
| 57 | * |
| 58 | * @see #getTag() |
| 59 | */ |
| 60 | public void setTag(Object tag) { |
| 61 | mTag = tag; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Retrieve the tag object associated with this ActionMode. |
| 66 | * |
| 67 | * <p>Like the tag available to views, this allows applications to associate arbitrary |
| 68 | * data with an ActionMode for later reference. |
| 69 | * |
| 70 | * @return Tag associated with this ActionMode |
| 71 | * |
| 72 | * @see #setTag(Object) |
| 73 | */ |
| 74 | public Object getTag() { |
| 75 | return mTag; |
| 76 | } |
| 77 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Set the title of the action mode. This method will have no visible effect if |
| 80 | * a custom view has been set. |
| 81 | * |
| 82 | * @param title Title string to set |
| 83 | * |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 84 | * @see #setTitle(int) |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 85 | * @see #setCustomView(View) |
| 86 | */ |
| 87 | public abstract void setTitle(CharSequence title); |
| 88 | |
| 89 | /** |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 90 | * Set the title of the action mode. This method will have no visible effect if |
| 91 | * a custom view has been set. |
| 92 | * |
| 93 | * @param resId Resource ID of a string to set as the title |
| 94 | * |
| 95 | * @see #setTitle(CharSequence) |
| 96 | * @see #setCustomView(View) |
| 97 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 98 | public abstract void setTitle(@StringRes int resId); |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 99 | |
| 100 | /** |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 101 | * Set the subtitle of the action mode. This method will have no visible effect if |
| 102 | * a custom view has been set. |
| 103 | * |
| 104 | * @param subtitle Subtitle string to set |
| 105 | * |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 106 | * @see #setSubtitle(int) |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 107 | * @see #setCustomView(View) |
| 108 | */ |
| 109 | public abstract void setSubtitle(CharSequence subtitle); |
| 110 | |
| 111 | /** |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 112 | * Set the subtitle of the action mode. This method will have no visible effect if |
| 113 | * a custom view has been set. |
| 114 | * |
| 115 | * @param resId Resource ID of a string to set as the subtitle |
| 116 | * |
| 117 | * @see #setSubtitle(CharSequence) |
| 118 | * @see #setCustomView(View) |
| 119 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 120 | public abstract void setSubtitle(@StringRes int resId); |
Adam Powell | c9ae2a2 | 2010-07-28 14:44:21 -0700 | [diff] [blame] | 121 | |
| 122 | /** |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 123 | * Set whether or not the title/subtitle display for this action mode |
| 124 | * is optional. |
| 125 | * |
| 126 | * <p>In many cases the supplied title for an action mode is merely |
| 127 | * meant to add context and is not strictly required for the action |
| 128 | * mode to be useful. If the title is optional, the system may choose |
| 129 | * to hide the title entirely rather than truncate it due to a lack |
| 130 | * of available space.</p> |
| 131 | * |
| 132 | * <p>Note that this is merely a hint; the underlying implementation |
| 133 | * may choose to ignore this setting under some circumstances.</p> |
| 134 | * |
| 135 | * @param titleOptional true if the title only presents optional information. |
| 136 | */ |
| 137 | public void setTitleOptionalHint(boolean titleOptional) { |
Adam Powell | 785c447 | 2012-05-02 21:25:39 -0700 | [diff] [blame] | 138 | mTitleOptionalHint = titleOptional; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return true if this action mode has been given a hint to consider the |
| 143 | * title/subtitle display to be optional. |
| 144 | * |
| 145 | * @see #setTitleOptionalHint(boolean) |
| 146 | * @see #isTitleOptional() |
| 147 | */ |
| 148 | public boolean getTitleOptionalHint() { |
| 149 | return mTitleOptionalHint; |
Adam Powell | b98a81f | 2012-02-24 11:09:07 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @return true if this action mode considers the title and subtitle fields |
| 154 | * as optional. Optional titles may not be displayed to the user. |
| 155 | */ |
| 156 | public boolean isTitleOptional() { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | /** |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 161 | * Set a custom view for this action mode. The custom view will take the place of |
| 162 | * the title and subtitle. Useful for things like search boxes. |
| 163 | * |
| 164 | * @param view Custom view to use in place of the title/subtitle. |
| 165 | * |
| 166 | * @see #setTitle(CharSequence) |
| 167 | * @see #setSubtitle(CharSequence) |
| 168 | */ |
| 169 | public abstract void setCustomView(View view); |
| 170 | |
| 171 | /** |
Clara Bayarri | d6aeff1 | 2015-01-26 16:38:07 +0000 | [diff] [blame] | 172 | * Set a type for this action mode. This will affect how the system renders the action mode if |
| 173 | * it has to. |
| 174 | * |
| 175 | * @param type One of {@link #TYPE_PRIMARY} or {@link #TYPE_FLOATING}. |
| 176 | */ |
| 177 | public void setType(int type) { |
| 178 | mType = type; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Returns the type for this action mode. |
| 183 | * |
| 184 | * @return One of {@link #TYPE_PRIMARY} or {@link #TYPE_FLOATING}. |
| 185 | */ |
| 186 | public int getType() { |
| 187 | return mType; |
| 188 | } |
| 189 | |
| 190 | /** |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 191 | * Invalidate the action mode and refresh menu content. The mode's |
| 192 | * {@link ActionMode.Callback} will have its |
| 193 | * {@link Callback#onPrepareActionMode(ActionMode, Menu)} method called. |
| 194 | * If it returns true the menu will be scanned for updated content and any relevant changes |
| 195 | * will be reflected to the user. |
| 196 | */ |
| 197 | public abstract void invalidate(); |
| 198 | |
| 199 | /** |
| 200 | * Finish and close this action mode. The action mode's {@link ActionMode.Callback} will |
| 201 | * have its {@link Callback#onDestroyActionMode(ActionMode)} method called. |
| 202 | */ |
| 203 | public abstract void finish(); |
| 204 | |
| 205 | /** |
| 206 | * Returns the menu of actions that this action mode presents. |
| 207 | * @return The action mode's menu. |
| 208 | */ |
| 209 | public abstract Menu getMenu(); |
| 210 | |
| 211 | /** |
| 212 | * Returns the current title of this action mode. |
| 213 | * @return Title text |
| 214 | */ |
| 215 | public abstract CharSequence getTitle(); |
| 216 | |
| 217 | /** |
| 218 | * Returns the current subtitle of this action mode. |
| 219 | * @return Subtitle text |
| 220 | */ |
| 221 | public abstract CharSequence getSubtitle(); |
| 222 | |
| 223 | /** |
| 224 | * Returns the current custom view for this action mode. |
| 225 | * @return The current custom view |
| 226 | */ |
| 227 | public abstract View getCustomView(); |
| 228 | |
| 229 | /** |
Adam Powell | 9168f0b | 2010-08-02 15:46:24 -0700 | [diff] [blame] | 230 | * Returns a {@link MenuInflater} with the ActionMode's context. |
| 231 | */ |
| 232 | public abstract MenuInflater getMenuInflater(); |
| 233 | |
| 234 | /** |
Adam Powell | f8419a0 | 2011-10-03 12:08:54 -0700 | [diff] [blame] | 235 | * Returns whether the UI presenting this action mode can take focus or not. |
| 236 | * This is used by internal components within the framework that would otherwise |
| 237 | * present an action mode UI that requires focus, such as an EditText as a custom view. |
| 238 | * |
| 239 | * @return true if the UI used to show this action mode can take focus |
| 240 | * @hide Internal use only |
| 241 | */ |
| 242 | public boolean isUiFocusable() { |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | /** |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 247 | * Callback interface for action modes. Supplied to |
| 248 | * {@link View#startActionMode(Callback)}, a Callback |
| 249 | * configures and handles events raised by a user's interaction with an action mode. |
| 250 | * |
| 251 | * <p>An action mode's lifecycle is as follows: |
| 252 | * <ul> |
| 253 | * <li>{@link Callback#onCreateActionMode(ActionMode, Menu)} once on initial |
| 254 | * creation</li> |
| 255 | * <li>{@link Callback#onPrepareActionMode(ActionMode, Menu)} after creation |
| 256 | * and any time the {@link ActionMode} is invalidated</li> |
| 257 | * <li>{@link Callback#onActionItemClicked(ActionMode, MenuItem)} any time a |
| 258 | * contextual action button is clicked</li> |
| 259 | * <li>{@link Callback#onDestroyActionMode(ActionMode)} when the action mode |
| 260 | * is closed</li> |
| 261 | * </ul> |
| 262 | */ |
| 263 | public interface Callback { |
| 264 | /** |
| 265 | * Called when action mode is first created. The menu supplied will be used to |
| 266 | * generate action buttons for the action mode. |
| 267 | * |
| 268 | * @param mode ActionMode being created |
| 269 | * @param menu Menu used to populate action buttons |
| 270 | * @return true if the action mode should be created, false if entering this |
| 271 | * mode should be aborted. |
| 272 | */ |
| 273 | public boolean onCreateActionMode(ActionMode mode, Menu menu); |
| 274 | |
| 275 | /** |
| 276 | * Called to refresh an action mode's action menu whenever it is invalidated. |
| 277 | * |
| 278 | * @param mode ActionMode being prepared |
| 279 | * @param menu Menu used to populate action buttons |
| 280 | * @return true if the menu or action mode was updated, false otherwise. |
| 281 | */ |
| 282 | public boolean onPrepareActionMode(ActionMode mode, Menu menu); |
| 283 | |
| 284 | /** |
| 285 | * Called to report a user click on an action button. |
| 286 | * |
| 287 | * @param mode The current ActionMode |
| 288 | * @param item The item that was clicked |
| 289 | * @return true if this callback handled the event, false if the standard MenuItem |
| 290 | * invocation should continue. |
| 291 | */ |
| 292 | public boolean onActionItemClicked(ActionMode mode, MenuItem item); |
| 293 | |
| 294 | /** |
| 295 | * Called when an action mode is about to be exited and destroyed. |
| 296 | * |
| 297 | * @param mode The current ActionMode being destroyed |
| 298 | */ |
| 299 | public void onDestroyActionMode(ActionMode mode); |
| 300 | } |
| 301 | } |