From ac695c608ba620e2362f57126d7be453cf5b7e1b Mon Sep 17 00:00:00 2001 From: Adam Powell Date: Tue, 20 Jul 2010 18:19:27 -0700 Subject: [PATCH] Refactored contextual modes out of action bar. Change-Id: I1fc1c9383e5ee90f135b92a5afa8eadbf1c13d20 --- api/current.xml | 428 +++++++++--------- core/java/android/app/ActionBar.java | 149 ------ core/java/android/app/Activity.java | 17 +- core/java/android/app/ContextualMode.java | 155 +++++++ core/java/android/content/Context.java | 23 + .../android/internal/app/ActionBarImpl.java | 17 +- .../internal/widget/ActionBarContextView.java | 4 +- 7 files changed, 419 insertions(+), 374 deletions(-) create mode 100644 core/java/android/app/ContextualMode.java diff --git a/api/current.xml b/api/current.xml index 76b1c9aa073a5..e66e52934f938 100644 --- a/api/current.xml +++ b/api/current.xml @@ -19481,17 +19481,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/core/java/android/app/ActionBar.java b/core/java/android/app/ActionBar.java index 3cd2b9e86f966..e1124a1874d69 100644 --- a/core/java/android/app/ActionBar.java +++ b/core/java/android/app/ActionBar.java @@ -17,8 +17,6 @@ package android.app; import android.graphics.drawable.Drawable; -import android.view.Menu; -import android.view.MenuItem; import android.view.View; import android.widget.SpinnerAdapter; @@ -220,20 +218,6 @@ public abstract class ActionBar { */ public abstract int getDisplayOptions(); - /** - * Start a context mode controlled by callback. - * The {@link ContextModeCallback} will receive lifecycle events for the duration - * of the context mode. - * - * @param callback Callback handler that will manage this context mode. - */ - public abstract void startContextMode(ContextModeCallback callback); - - /** - * Finish the current context mode. - */ - public abstract void finishContextMode(); - /** * Set the action bar into tabbed navigation mode. * @@ -311,139 +295,6 @@ public abstract class ActionBar { */ public abstract void selectTabAt(int position); - /** - * Represents a contextual mode of the Action Bar. Context modes can be used for - * modal interactions with activity content and replace the normal Action Bar until finished. - * Examples of good contextual modes include selection modes, search, content editing, etc. - */ - public static abstract class ContextMode { - /** - * Set the title of the context mode. This method will have no visible effect if - * a custom view has been set. - * - * @param title Title string to set - * - * @see #setCustomView(View) - */ - public abstract void setTitle(CharSequence title); - - /** - * Set the subtitle of the context mode. This method will have no visible effect if - * a custom view has been set. - * - * @param subtitle Subtitle string to set - * - * @see #setCustomView(View) - */ - public abstract void setSubtitle(CharSequence subtitle); - - /** - * Set a custom view for this context mode. The custom view will take the place of - * the title and subtitle. Useful for things like search boxes. - * - * @param view Custom view to use in place of the title/subtitle. - * - * @see #setTitle(CharSequence) - * @see #setSubtitle(CharSequence) - */ - public abstract void setCustomView(View view); - - /** - * Invalidate the context mode and refresh menu content. The context mode's - * {@link ContextModeCallback} will have its - * {@link ContextModeCallback#onPrepareContextMode(ContextMode, Menu)} method called. - * If it returns true the menu will be scanned for updated content and any relevant changes - * will be reflected to the user. - */ - public abstract void invalidate(); - - /** - * Finish and close this context mode. The context mode's {@link ContextModeCallback} will - * have its {@link ContextModeCallback#onDestroyContextMode(ContextMode)} method called. - */ - public abstract void finish(); - - /** - * Returns the menu of actions that this context mode presents. - * @return The context mode's menu. - */ - public abstract Menu getMenu(); - - /** - * Returns the current title of this context mode. - * @return Title text - */ - public abstract CharSequence getTitle(); - - /** - * Returns the current subtitle of this context mode. - * @return Subtitle text - */ - public abstract CharSequence getSubtitle(); - - /** - * Returns the current custom view for this context mode. - * @return The current custom view - */ - public abstract View getCustomView(); - } - - /** - * Callback interface for ActionBar context modes. Supplied to - * {@link ActionBar#startContextMode(ContextModeCallback)}, a ContextModeCallback - * configures and handles events raised by a user's interaction with a context mode. - * - *

A context mode's lifecycle is as follows: - *

- */ - public interface ContextModeCallback { - /** - * Called when a context mode is first created. The menu supplied will be used to generate - * action buttons for the context mode. - * - * @param mode ContextMode being created - * @param menu Menu used to populate contextual action buttons - * @return true if the context mode should be created, false if entering this context mode - * should be aborted. - */ - public boolean onCreateContextMode(ContextMode mode, Menu menu); - - /** - * Called to refresh a context mode's action menu whenever it is invalidated. - * - * @param mode ContextMode being prepared - * @param menu Menu used to populate contextual action buttons - * @return true if the menu or context mode was updated, false otherwise. - */ - public boolean onPrepareContextMode(ContextMode mode, Menu menu); - - /** - * Called to report a user click on a contextual action button. - * - * @param mode The current ContextMode - * @param item The item that was clicked - * @return true if this callback handled the event, false if the standard MenuItem - * invocation should continue. - */ - public boolean onContextItemClicked(ContextMode mode, MenuItem item); - - /** - * Called when a context mode is about to be exited and destroyed. - * - * @param mode The current ContextMode being destroyed - */ - public void onDestroyContextMode(ContextMode mode); - } - /** * Callback interface for ActionBar navigation events. */ diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java index 1cdd423e5e2e4..32982085846b8 100644 --- a/core/java/android/app/Activity.java +++ b/core/java/android/app/Activity.java @@ -662,7 +662,7 @@ public class Activity extends ContextThemeWrapper /*package*/ boolean mWindowAdded = false; /*package*/ boolean mVisibleFromServer = false; /*package*/ boolean mVisibleFromClient = true; - /*package*/ ActionBar mActionBar = null; + /*package*/ ActionBarImpl mActionBar = null; private CharSequence mTitle; private int mTitleColor = 0; @@ -4253,4 +4253,19 @@ public class Activity extends ContextThemeWrapper } } } + + @Override + public ContextualMode startContextualMode(ContextualMode.Callback callback) { + if (mActionBar == null) { + return null; + } + return mActionBar.startContextualMode(callback); + } + + @Override + public void finishContextualMode() { + if (mActionBar != null) { + mActionBar.finishContextualMode(); + } + } } diff --git a/core/java/android/app/ContextualMode.java b/core/java/android/app/ContextualMode.java new file mode 100644 index 0000000000000..a635bc892c1cd --- /dev/null +++ b/core/java/android/app/ContextualMode.java @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.app; + +import android.content.Context; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; + +/** + * Represents a contextual mode of the user interface. Contextual modes can be used for + * modal interactions with content and replace parts of the normal UI until finished. + * Examples of good contextual modes include selection modes, search, content editing, etc. + */ +public abstract class ContextualMode { + /** + * Set the title of the contextual mode. This method will have no visible effect if + * a custom view has been set. + * + * @param title Title string to set + * + * @see #setCustomView(View) + */ + public abstract void setTitle(CharSequence title); + + /** + * Set the subtitle of the contextual mode. This method will have no visible effect if + * a custom view has been set. + * + * @param subtitle Subtitle string to set + * + * @see #setCustomView(View) + */ + public abstract void setSubtitle(CharSequence subtitle); + + /** + * Set a custom view for this contextual mode. The custom view will take the place of + * the title and subtitle. Useful for things like search boxes. + * + * @param view Custom view to use in place of the title/subtitle. + * + * @see #setTitle(CharSequence) + * @see #setSubtitle(CharSequence) + */ + public abstract void setCustomView(View view); + + /** + * Invalidate the contextual mode and refresh menu content. The contextual mode's + * {@link ContextualMode.Callback} will have its + * {@link Callback#onPrepareContextMode(ContextualMode, Menu)} method called. + * If it returns true the menu will be scanned for updated content and any relevant changes + * will be reflected to the user. + */ + public abstract void invalidate(); + + /** + * Finish and close this context mode. The context mode's {@link ContextualMode.Callback} will + * have its {@link Callback#onDestroyContextMode(ContextualMode)} method called. + */ + public abstract void finish(); + + /** + * Returns the menu of actions that this contextual mode presents. + * @return The contextual mode's menu. + */ + public abstract Menu getMenu(); + + /** + * Returns the current title of this contextual mode. + * @return Title text + */ + public abstract CharSequence getTitle(); + + /** + * Returns the current subtitle of this contextual mode. + * @return Subtitle text + */ + public abstract CharSequence getSubtitle(); + + /** + * Returns the current custom view for this contextual mode. + * @return The current custom view + */ + public abstract View getCustomView(); + + /** + * Callback interface for contextual modes. Supplied to + * {@link Context#startContextMode(Callback)}, a ContextModeCallback + * configures and handles events raised by a user's interaction with a context mode. + * + *

A context mode's lifecycle is as follows: + *

+ */ + public interface Callback { + /** + * Called when a context mode is first created. The menu supplied will be used to generate + * action buttons for the context mode. + * + * @param mode ContextMode being created + * @param menu Menu used to populate contextual action buttons + * @return true if the context mode should be created, false if entering this context mode + * should be aborted. + */ + public boolean onCreateContextMode(ContextualMode mode, Menu menu); + + /** + * Called to refresh a context mode's action menu whenever it is invalidated. + * + * @param mode ContextMode being prepared + * @param menu Menu used to populate contextual action buttons + * @return true if the menu or context mode was updated, false otherwise. + */ + public boolean onPrepareContextMode(ContextualMode mode, Menu menu); + + /** + * Called to report a user click on a contextual action button. + * + * @param mode The current ContextMode + * @param item The item that was clicked + * @return true if this callback handled the event, false if the standard MenuItem + * invocation should continue. + */ + public boolean onContextItemClicked(ContextualMode mode, MenuItem item); + + /** + * Called when a context mode is about to be exited and destroyed. + * + * @param mode The current ContextMode being destroyed + */ + public void onDestroyContextMode(ContextualMode mode); + } +} \ No newline at end of file diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index b7e4b173a5e36..aceeed682bd2f 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -16,6 +16,7 @@ package android.content; +import android.app.ContextualMode; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.AssetManager; @@ -26,6 +27,7 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; +import android.media.MediaScannerConnection.OnScanCompletedListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; @@ -1981,4 +1983,25 @@ public abstract class Context { public boolean isRestricted() { return false; } + + /** + * Start a contextual mode controlled by callback. + * The {@link ContextualMode.Callback} will receive lifecycle events for the duration + * of the contextual mode. There can only be one contextual mode active at a time. + * Starting a new contextual mode while one is already active will finish the old + * contextual mode. + * + * @param callback Callback handler that will manage this context mode. + * @return The new contextual mode started by this call, or null + * if the mode was not started. + */ + public ContextualMode startContextualMode(ContextualMode.Callback callback) { + return null; + } + + /** + * Finish the current contextual mode if present. + */ + public void finishContextualMode() { + } } diff --git a/core/java/com/android/internal/app/ActionBarImpl.java b/core/java/com/android/internal/app/ActionBarImpl.java index 63dbdb4fdbde8..ec6d2be44c443 100644 --- a/core/java/com/android/internal/app/ActionBarImpl.java +++ b/core/java/com/android/internal/app/ActionBarImpl.java @@ -25,6 +25,7 @@ import com.android.internal.widget.ActionBarView; import android.app.ActionBar; import android.app.Activity; +import android.app.ContextualMode; import android.app.Fragment; import android.app.FragmentTransaction; import android.graphics.drawable.Drawable; @@ -196,9 +197,8 @@ public class ActionBarImpl extends ActionBar { return mActionView.getDisplayOptions(); } - @Override - public void startContextMode(ContextModeCallback callback) { - finishContextMode(); + public ContextualMode startContextualMode(ContextualMode.Callback callback) { + finishContextualMode(); // Don't wait for the close context mode animation to finish. if (mClosingContext) { @@ -217,11 +217,12 @@ public class ActionBarImpl extends ActionBar { mLowerContextView.setVisibility(View.VISIBLE); } mContextMode = mode; + return mode; } + return null; } - @Override - public void finishContextMode() { + public void finishContextualMode() { if (mContextMode != null) { mContextMode.finish(); } @@ -337,12 +338,12 @@ public class ActionBarImpl extends ActionBar { /** * @hide */ - public class ContextMode extends ActionBar.ContextMode implements MenuBuilder.Callback { - private ContextModeCallback mCallback; + public class ContextMode extends ContextualMode implements MenuBuilder.Callback { + private ContextualMode.Callback mCallback; private MenuBuilder mMenu; private WeakReference mCustomView; - public ContextMode(ContextModeCallback callback) { + public ContextMode(ContextualMode.Callback callback) { mCallback = callback; mMenu = new MenuBuilder(mActionView.getContext()); mMenu.setCallback(this); diff --git a/core/java/com/android/internal/widget/ActionBarContextView.java b/core/java/com/android/internal/widget/ActionBarContextView.java index dcd05466caf41..08405a3d75d80 100644 --- a/core/java/com/android/internal/widget/ActionBarContextView.java +++ b/core/java/com/android/internal/widget/ActionBarContextView.java @@ -19,7 +19,7 @@ import com.android.internal.R; import com.android.internal.view.menu.ActionMenuView; import com.android.internal.view.menu.MenuBuilder; -import android.app.ActionBar; +import android.app.ContextualMode; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; @@ -135,7 +135,7 @@ public class ActionBarContextView extends ViewGroup { } } - public void initForMode(final ActionBar.ContextMode mode) { + public void initForMode(final ContextualMode mode) { if (mCloseButton == null) { mCloseButton = new ImageButton(getContext()); mCloseButton.setImageDrawable(mCloseDrawable);