diff options
Diffstat (limited to 'core/java/android/app/AlertDialog.java')
-rw-r--r-- | core/java/android/app/AlertDialog.java | 598 |
1 files changed, 598 insertions, 0 deletions
diff --git a/core/java/android/app/AlertDialog.java b/core/java/android/app/AlertDialog.java new file mode 100644 index 0000000..cc80ba4 --- /dev/null +++ b/core/java/android/app/AlertDialog.java @@ -0,0 +1,598 @@ +/* + * Copyright (C) 2007 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.content.DialogInterface; +import android.database.Cursor; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.os.Message; +import android.view.KeyEvent; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ListAdapter; +import android.widget.ListView; + +import com.android.internal.app.AlertController; + +/** + * A subclass of Dialog that can display one, two or three buttons. If you only want to + * display a String in this dialog box, use the setMessage() method. If you + * want to display a more complex view, look up the FrameLayout called "body" + * and add your view to it: + * + * <pre> + * FrameLayout fl = (FrameLayout) findViewById(R.id.body); + * fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT)); + * </pre> + */ +public class AlertDialog extends Dialog implements DialogInterface { + private AlertController mAlert; + + protected AlertDialog(Context context) { + this(context, com.android.internal.R.style.Theme_Dialog_Alert); + } + + protected AlertDialog(Context context, int theme) { + super(context, theme); + mAlert = new AlertController(context, this, getWindow()); + } + + protected AlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { + super(context, com.android.internal.R.style.Theme_Dialog_Alert); + setCancelable(cancelable); + setOnCancelListener(cancelListener); + mAlert = new AlertController(context, this, getWindow()); + } + + @Override + public void setTitle(CharSequence title) { + super.setTitle(title); + mAlert.setTitle(title); + } + + /** + * @see Builder#setCustomTitle(View) + */ + public void setCustomTitle(View customTitleView) { + mAlert.setCustomTitle(customTitleView); + } + + public void setMessage(CharSequence message) { + mAlert.setMessage(message); + } + + /** + * Set the view to display in that dialog. + */ + public void setView(View view) { + mAlert.setView(view); + } + + public void setButton(CharSequence text, Message msg) { + mAlert.setButton(text, msg); + } + + public void setButton2(CharSequence text, Message msg) { + mAlert.setButton2(text, msg); + } + + public void setButton3(CharSequence text, Message msg) { + mAlert.setButton3(text, msg); + } + + /** + * Set a listener to be invoked when button 1 of the dialog is pressed. + * @param text The text to display in button 1. + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public void setButton(CharSequence text, final OnClickListener listener) { + mAlert.setButton(text, listener); + } + + /** + * Set a listener to be invoked when button 2 of the dialog is pressed. + * @param text The text to display in button 2. + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public void setButton2(CharSequence text, final OnClickListener listener) { + mAlert.setButton2(text, listener); + } + + /** + * Set a listener to be invoked when button 3 of the dialog is pressed. + * @param text The text to display in button 3. + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public void setButton3(CharSequence text, final OnClickListener listener) { + mAlert.setButton3(text, listener); + } + + /** + * Set resId to 0 if you don't want an icon. + * @param resId the resourceId of the drawable to use as the icon or 0 + * if you don't want an icon. + */ + public void setIcon(int resId) { + mAlert.setIcon(resId); + } + + public void setIcon(Drawable icon) { + mAlert.setIcon(icon); + } + + public void setInverseBackgroundForced(boolean forceInverseBackground) { + mAlert.setInverseBackgroundForced(forceInverseBackground); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mAlert.installContent(); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (mAlert.onKeyDown(keyCode, event)) return true; + return super.onKeyDown(keyCode, event); + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + if (mAlert.onKeyUp(keyCode, event)) return true; + return super.onKeyUp(keyCode, event); + } + + public static class Builder { + private final AlertController.AlertParams P; + + /** + * Constructor using a context for this builder and the {@link AlertDialog} it creates. + */ + public Builder(Context context) { + P = new AlertController.AlertParams(context); + } + + /** + * Set the title using the given resource id. + */ + public Builder setTitle(int titleId) { + P.mTitle = P.mContext.getText(titleId); + return this; + } + + /** + * Set the title displayed in the {@link Dialog}. + */ + public Builder setTitle(CharSequence title) { + P.mTitle = title; + return this; + } + + /** + * Set the title using the custom view {@code customTitleView}. The + * methods {@link #setTitle(int)} and {@link #setIcon(int)} should be + * sufficient for most titles, but this is provided if the title needs + * more customization. Using this will replace the title and icon set + * via the other methods. + * + * @param customTitleView The custom view to use as the title. + */ + public Builder setCustomTitle(View customTitleView) { + P.mCustomTitleView = customTitleView; + return this; + } + + /** + * Set the message to display using the given resource id. + */ + public Builder setMessage(int messageId) { + P.mMessage = P.mContext.getText(messageId); + return this; + } + + /** + * Set the message to display. + */ + public Builder setMessage(CharSequence message) { + P.mMessage = message; + return this; + } + + /** + * Set the resource id of the {@link Drawable} to be used in the title. + */ + public Builder setIcon(int iconId) { + P.mIconId = iconId; + return this; + } + + /** + * Set the {@link Drawable} to be used in the title. + */ + public Builder setIcon(Drawable icon) { + P.mIcon = icon; + return this; + } + + /** + * Set a listener to be invoked when the positive button of the dialog is pressed. + * @param textId The resource id of the text to display in the positive button + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public Builder setPositiveButton(int textId, final OnClickListener listener) { + P.mPositiveButtonText = P.mContext.getText(textId); + P.mPositiveButtonListener = listener; + return this; + } + + /** + * Set a listener to be invoked when the positive button of the dialog is pressed. + * @param text The text to display in the positive button + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public Builder setPositiveButton(CharSequence text, final OnClickListener listener) { + P.mPositiveButtonText = text; + P.mPositiveButtonListener = listener; + return this; + } + + /** + * Set a listener to be invoked when the negative button of the dialog is pressed. + * @param textId The resource id of the text to display in the negative button + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public Builder setNegativeButton(int textId, final OnClickListener listener) { + P.mNegativeButtonText = P.mContext.getText(textId); + P.mNegativeButtonListener = listener; + return this; + } + + /** + * Set a listener to be invoked when the negative button of the dialog is pressed. + * @param text The text to display in the negative button + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public Builder setNegativeButton(CharSequence text, final OnClickListener listener) { + P.mNegativeButtonText = text; + P.mNegativeButtonListener = listener; + return this; + } + + /** + * Set a listener to be invoked when the neutral button of the dialog is pressed. + * @param textId The resource id of the text to display in the neutral button + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public Builder setNeutralButton(int textId, final OnClickListener listener) { + P.mNeutralButtonText = P.mContext.getText(textId); + P.mNeutralButtonListener = listener; + return this; + } + + /** + * Set a listener to be invoked when the neutral button of the dialog is pressed. + * @param text The text to display in the neutral button + * @param listener The {@link DialogInterface.OnClickListener} to use. + */ + public Builder setNeutralButton(CharSequence text, final OnClickListener listener) { + P.mNeutralButtonText = text; + P.mNeutralButtonListener = listener; + return this; + } + + /** + * Sets whether the dialog is cancelable or not default is true. + */ + public Builder setCancelable(boolean cancelable) { + P.mCancelable = cancelable; + return this; + } + + /** + * Sets the callback that will be called if the dialog is canceled. + * @see #setCancelable(boolean) + */ + public Builder setOnCancelListener(OnCancelListener onCancelListener) { + P.mOnCancelListener = onCancelListener; + return this; + } + + /** + * Sets the callback that will be called if a key is dispatched to the dialog. + */ + public Builder setOnKeyListener(OnKeyListener onKeyListener) { + P.mOnKeyListener = onKeyListener; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, you will be notified of the + * selected item via the supplied listener. This should be an array type i.e. R.array.foo + */ + public Builder setItems(int itemsId, final OnClickListener listener) { + P.mItems = P.mContext.getResources().getTextArray(itemsId); + P.mOnClickListener = listener; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, you will be notified of the + * selected item via the supplied listener. + */ + public Builder setItems(CharSequence[] items, final OnClickListener listener) { + P.mItems = items; + P.mOnClickListener = listener; + return this; + } + + /** + * Set a list of items, which are supplied by the given {@link ListAdapter}, to be + * displayed in the dialog as the content, you will be notified of the + * selected item via the supplied listener. + * + * @param adapter The {@link ListAdapter} to supply the list of items + * @param listener The listener that will be called when an item is clicked. + */ + public Builder setAdapter(final ListAdapter adapter, final OnClickListener listener) { + P.mAdapter = adapter; + P.mOnClickListener = listener; + return this; + } + + /** + * Set a list of items, which are supplied by the given {@link Cursor}, to be + * displayed in the dialog as the content, you will be notified of the + * selected item via the supplied listener. + * + * @param cursor The {@link Cursor} to supply the list of items + * @param listener The listener that will be called when an item is clicked. + * @param labelColumn The column name on the cursor containing the string to display + * in the label. + */ + public Builder setCursor(final Cursor cursor, final OnClickListener listener, + String labelColumn) { + P.mCursor = cursor; + P.mLabelColumn = labelColumn; + P.mOnClickListener = listener; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, + * you will be notified of the selected item via the supplied listener. + * This should be an array type, e.g. R.array.foo. The list will have + * a check mark displayed to the right of the text for each checked + * item. Clicking on an item in the list will not dismiss the dialog. + * Clicking on a button will dismiss the dialog. + * + * @param itemsId the resource id of an array i.e. R.array.foo + * @param checkedItems specifies which items are checked. It should be null in which case no + * items are checked. If non null it must be exactly the same length as the array of + * items. + * @param listener notified when an item on the list is clicked. The dialog will not be + * dismissed when an item is clicked. It will only be dismissed if clicked on a + * button, if no buttons are supplied it's up to the user to dismiss the dialog. + */ + public Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems, + final OnMultiChoiceClickListener listener) { + P.mItems = P.mContext.getResources().getTextArray(itemsId); + P.mOnCheckboxClickListener = listener; + P.mCheckedItems = checkedItems; + P.mIsMultiChoice = true; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, + * you will be notified of the selected item via the supplied listener. + * The list will have a check mark displayed to the right of the text + * for each checked item. Clicking on an item in the list will not + * dismiss the dialog. Clicking on a button will dismiss the dialog. + * + * @param items the text of the items to be displayed in the list. + * @param checkedItems specifies which items are checked. It should be null in which case no + * items are checked. If non null it must be exactly the same length as the array of + * items. + * @param listener notified when an item on the list is clicked. The dialog will not be + * dismissed when an item is clicked. It will only be dismissed if clicked on a + * button, if no buttons are supplied it's up to the user to dismiss the dialog. + */ + public Builder setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, + final OnMultiChoiceClickListener listener) { + P.mItems = items; + P.mOnCheckboxClickListener = listener; + P.mCheckedItems = checkedItems; + P.mIsMultiChoice = true; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, + * you will be notified of the selected item via the supplied listener. + * The list will have a check mark displayed to the right of the text + * for each checked item. Clicking on an item in the list will not + * dismiss the dialog. Clicking on a button will dismiss the dialog. + * + * @param cursor the cursor used to provide the items. + * @param isCheckedColumn specifies the column name on the cursor to use to determine + * whether a checkbox is checked or not. It must return an integer value where 1 + * means checked and 0 means unchecked. + * @param labelColumn The column name on the cursor containing the string to display in the + * label. + * @param listener notified when an item on the list is clicked. The dialog will not be + * dismissed when an item is clicked. It will only be dismissed if clicked on a + * button, if no buttons are supplied it's up to the user to dismiss the dialog. + */ + public Builder setMultiChoiceItems(Cursor cursor, String isCheckedColumn, String labelColumn, + final OnMultiChoiceClickListener listener) { + P.mCursor = cursor; + P.mOnCheckboxClickListener = listener; + P.mIsCheckedColumn = isCheckedColumn; + P.mLabelColumn = labelColumn; + P.mIsMultiChoice = true; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, you will be notified of + * the selected item via the supplied listener. This should be an array type i.e. + * R.array.foo The list will have a check mark displayed to the right of the text for the + * checked item. Clicking on an item in the list will not dismiss the dialog. Clicking on a + * button will dismiss the dialog. + * + * @param itemsId the resource id of an array i.e. R.array.foo + * @param checkedItem specifies which item is checked. If -1 no items are checked. + * @param listener notified when an item on the list is clicked. The dialog will not be + * dismissed when an item is clicked. It will only be dismissed if clicked on a + * button, if no buttons are supplied it's up to the user to dismiss the dialog. + */ + public Builder setSingleChoiceItems(int itemsId, int checkedItem, + final OnClickListener listener) { + P.mItems = P.mContext.getResources().getTextArray(itemsId); + P.mOnClickListener = listener; + P.mCheckedItem = checkedItem; + P.mIsSingleChoice = true; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, you will be notified of + * the selected item via the supplied listener. The list will have a check mark displayed to + * the right of the text for the checked item. Clicking on an item in the list will not + * dismiss the dialog. Clicking on a button will dismiss the dialog. + * + * @param cursor the cursor to retrieve the items from. + * @param checkedItem specifies which item is checked. If -1 no items are checked. + * @param labelColumn The column name on the cursor containing the string to display in the + * label. + * @param listener notified when an item on the list is clicked. The dialog will not be + * dismissed when an item is clicked. It will only be dismissed if clicked on a + * button, if no buttons are supplied it's up to the user to dismiss the dialog. + */ + public Builder setSingleChoiceItems(Cursor cursor, int checkedItem, String labelColumn, + final OnClickListener listener) { + P.mCursor = cursor; + P.mOnClickListener = listener; + P.mCheckedItem = checkedItem; + P.mLabelColumn = labelColumn; + P.mIsSingleChoice = true; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, you will be notified of + * the selected item via the supplied listener. The list will have a check mark displayed to + * the right of the text for the checked item. Clicking on an item in the list will not + * dismiss the dialog. Clicking on a button will dismiss the dialog. + * + * @param items the items to be displayed. + * @param checkedItem specifies which item is checked. If -1 no items are checked. + * @param listener notified when an item on the list is clicked. The dialog will not be + * dismissed when an item is clicked. It will only be dismissed if clicked on a + * button, if no buttons are supplied it's up to the user to dismiss the dialog. + */ + public Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener) { + P.mItems = items; + P.mOnClickListener = listener; + P.mCheckedItem = checkedItem; + P.mIsSingleChoice = true; + return this; + } + + /** + * Set a list of items to be displayed in the dialog as the content, you will be notified of + * the selected item via the supplied listener. The list will have a check mark displayed to + * the right of the text for the checked item. Clicking on an item in the list will not + * dismiss the dialog. Clicking on a button will dismiss the dialog. + * + * @param adapter The {@link ListAdapter} to supply the list of items + * @param checkedItem specifies which item is checked. If -1 no items are checked. + * @param listener notified when an item on the list is clicked. The dialog will not be + * dismissed when an item is clicked. It will only be dismissed if clicked on a + * button, if no buttons are supplied it's up to the user to dismiss the dialog. + */ + public Builder setSingleChoiceItems(ListAdapter adapter, int checkedItem, final OnClickListener listener) { + P.mAdapter = adapter; + P.mOnClickListener = listener; + P.mCheckedItem = checkedItem; + P.mIsSingleChoice = true; + return this; + } + + /** + * Sets a listener to be invoked when an item in the list is selected. + * + * @param listener The listener to be invoked. + * @see AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener) + */ + public Builder setOnItemSelectedListener(final AdapterView.OnItemSelectedListener listener) { + P.mOnItemSelectedListener = listener; + return this; + } + + /** + * Set a custom view to be the contents of the Dialog. If the supplied view is an instance + * of a {@link ListView} the light background will be used. + */ + public Builder setView(View view) { + P.mView = view; + return this; + } + + /** + * Sets the Dialog to use the inverse background, regardless of what the + * contents is. + * + * @param useInverseBackground Whether to use the inverse background + * @return This Builder object to allow for chaining of sets. + */ + public Builder setInverseBackgroundForced(boolean useInverseBackground) { + P.mForceInverseBackground = useInverseBackground; + return this; + } + + /** + * Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not + * {@link Dialog#show()} the dialog. This allows the user to do any extra processing + * before displaying the dialog. Use {@link #show()} if you don't have any other processing + * to do and want this to be created and displayed. + */ + public AlertDialog create() { + final AlertDialog dialog = new AlertDialog(P.mContext); + P.apply(dialog.mAlert); + dialog.setCancelable(P.mCancelable); + dialog.setOnCancelListener(P.mOnCancelListener); + if (P.mOnKeyListener != null) { + dialog.setOnKeyListener(P.mOnKeyListener); + } + return dialog; + } + + /** + * Creates a {@link AlertDialog} with the arguments supplied to this builder and + * {@link Dialog#show()}'s the dialog. + */ + public AlertDialog show() { + AlertDialog dialog = create(); + dialog.show(); + return dialog; + } + } + +} |