AlertController.java revision 19eaf14cb3cca613f984a32be2fab2534427fb29
119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes/*
219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * Copyright (C) 2015 The Android Open Source Project
319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes *
419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * Licensed under the Apache License, Version 2.0 (the "License");
519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * you may not use this file except in compliance with the License.
619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * You may obtain a copy of the License at
719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes *
819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes *      http://www.apache.org/licenses/LICENSE-2.0
919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes *
1019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * Unless required by applicable law or agreed to in writing, software
1119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * distributed under the License is distributed on an "AS IS" BASIS,
1219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * See the License for the specific language governing permissions and
1419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes * limitations under the License.
1519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes */
1619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
1719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banespackage android.support.v7.app;
1819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
1919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.content.Context;
2019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.content.DialogInterface;
2119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.content.res.TypedArray;
2219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.database.Cursor;
2319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.graphics.drawable.Drawable;
2419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.os.Handler;
2519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.os.Message;
2619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.support.v7.appcompat.R;
2719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.support.v7.internal.widget.TintTypedArray;
2819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.text.TextUtils;
2919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.util.TypedValue;
3019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.view.Gravity;
3119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.view.KeyEvent;
3219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.view.LayoutInflater;
3319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.view.View;
3419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.view.ViewGroup;
3519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.view.ViewGroup.LayoutParams;
3619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.view.Window;
3719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.view.WindowManager;
3819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.AdapterView;
3919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.AdapterView.OnItemClickListener;
4019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.ArrayAdapter;
4119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.Button;
4219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.CheckedTextView;
4319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.CursorAdapter;
4419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.FrameLayout;
4519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.ImageView;
4619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.LinearLayout;
4719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.ListAdapter;
4819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.ListView;
4919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.ScrollView;
5019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.SimpleCursorAdapter;
5119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport android.widget.TextView;
5219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
5319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport java.lang.ref.WeakReference;
5419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
5519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesimport static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
5619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
5719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banesclass AlertController {
5819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private final Context mContext;
5919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private final AppCompatDialog mDialog;
6019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private final Window mWindow;
6119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
6219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private CharSequence mTitle;
6319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private CharSequence mMessage;
6419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private ListView mListView;
6519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private View mView;
6619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
6719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mViewLayoutResId;
6819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
6919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mViewSpacingLeft;
7019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mViewSpacingTop;
7119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mViewSpacingRight;
7219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mViewSpacingBottom;
7319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private boolean mViewSpacingSpecified = false;
7419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
7519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private Button mButtonPositive;
7619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private CharSequence mButtonPositiveText;
7719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private Message mButtonPositiveMessage;
7819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
7919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private Button mButtonNegative;
8019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private CharSequence mButtonNegativeText;
8119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private Message mButtonNegativeMessage;
8219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
8319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private Button mButtonNeutral;
8419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private CharSequence mButtonNeutralText;
8519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private Message mButtonNeutralMessage;
8619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
8719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private ScrollView mScrollView;
8819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
8919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mIconId = 0;
9019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private Drawable mIcon;
9119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
9219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private ImageView mIconView;
9319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private TextView mTitleView;
9419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private TextView mMessageView;
9519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private View mCustomTitleView;
9619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
9719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private ListAdapter mAdapter;
9819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
9919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mCheckedItem = -1;
10019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
10119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mAlertDialogLayout;
10219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mButtonPanelSideLayout;
10319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mListLayout;
10419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mMultiChoiceItemLayout;
10519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mSingleChoiceItemLayout;
10619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mListItemLayout;
10719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
10819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int mButtonPanelLayoutHint = AlertDialog.LAYOUT_HINT_NONE;
10919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
11019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private Handler mHandler;
11119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
11219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private final View.OnClickListener mButtonHandler = new View.OnClickListener() {
11319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        @Override
11419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public void onClick(View v) {
11519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            final Message m;
11619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (v == mButtonPositive && mButtonPositiveMessage != null) {
11719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                m = Message.obtain(mButtonPositiveMessage);
11819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else if (v == mButtonNegative && mButtonNegativeMessage != null) {
11919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                m = Message.obtain(mButtonNegativeMessage);
12019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else if (v == mButtonNeutral && mButtonNeutralMessage != null) {
12119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                m = Message.obtain(mButtonNeutralMessage);
12219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else {
12319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                m = null;
12419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
12519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
12619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (m != null) {
12719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                m.sendToTarget();
12819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
12919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
13019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            // Post a message so we dismiss after the above handlers are executed
13119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialog)
13219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    .sendToTarget();
13319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
13419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    };
13519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
13619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private static final class ButtonHandler extends Handler {
13719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        // Button clicks have Message.what as the BUTTON{1,2,3} constant
13819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        private static final int MSG_DISMISS_DIALOG = 1;
13919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
14019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        private WeakReference<DialogInterface> mDialog;
14119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
14219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public ButtonHandler(DialogInterface dialog) {
14319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mDialog = new WeakReference<>(dialog);
14419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
14519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
14619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        @Override
14719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public void handleMessage(Message msg) {
14819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            switch (msg.what) {
14919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
15019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                case DialogInterface.BUTTON_POSITIVE:
15119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                case DialogInterface.BUTTON_NEGATIVE:
15219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                case DialogInterface.BUTTON_NEUTRAL:
15319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    ((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);
15419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    break;
15519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
15619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                case MSG_DISMISS_DIALOG:
15719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    ((DialogInterface) msg.obj).dismiss();
15819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
15919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
16019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
16119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
16219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private static boolean shouldCenterSingleButton(Context context) {
16319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        TypedValue outValue = new TypedValue();
16419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        context.getTheme().resolveAttribute(R.attr.alertDialogCenterButtons, outValue, true);
16519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return outValue.data != 0;
16619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
16719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
16819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public AlertController(Context context, AppCompatDialog di, Window window) {
16919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mContext = context;
17019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mDialog = di;
17119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mWindow = window;
17219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mHandler = new ButtonHandler(di);
17319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
17419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        TypedArray a = context.obtainStyledAttributes(null, R.styleable.AlertDialog,
17519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                R.attr.alertDialogStyle, 0);
17619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
17719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mAlertDialogLayout = a.getResourceId(R.styleable.AlertDialog_android_layout, 0);
17819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mButtonPanelSideLayout = a.getResourceId(R.styleable.AlertDialog_buttonPanelSideLayout, 0);
17919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
18019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mListLayout = a.getResourceId(R.styleable.AlertDialog_listLayout, 0);
18119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mMultiChoiceItemLayout = a.getResourceId(R.styleable.AlertDialog_multiChoiceItemLayout, 0);
18219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mSingleChoiceItemLayout = a
18319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                .getResourceId(R.styleable.AlertDialog_singleChoiceItemLayout, 0);
18419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mListItemLayout = a.getResourceId(R.styleable.AlertDialog_listItemLayout, 0);
18519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
18619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        a.recycle();
18719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
18819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
18919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    static boolean canTextInput(View v) {
19019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (v.onCheckIsTextEditor()) {
19119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            return true;
19219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
19319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
19419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (!(v instanceof ViewGroup)) {
19519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            return false;
19619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
19719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
19819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        ViewGroup vg = (ViewGroup) v;
19919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        int i = vg.getChildCount();
20019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        while (i > 0) {
20119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            i--;
20219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            v = vg.getChildAt(i);
20319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (canTextInput(v)) {
20419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                return true;
20519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
20619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
20719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
20819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return false;
20919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
21019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
21119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void installContent() {
21219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        /* We use a custom title so never request a window title */
21319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mDialog.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
21419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
21519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final int contentView = selectContentView();
21619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mDialog.setContentView(contentView);
21719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        setupView();
21819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
21919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
22019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private int selectContentView() {
22119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mButtonPanelSideLayout == 0) {
22219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            return mAlertDialogLayout;
22319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
22419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mButtonPanelLayoutHint == AlertDialog.LAYOUT_HINT_SIDE) {
22519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            return mButtonPanelSideLayout;
22619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
22719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return mAlertDialogLayout;
22819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
22919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
23019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setTitle(CharSequence title) {
23119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mTitle = title;
23219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mTitleView != null) {
23319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mTitleView.setText(title);
23419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
23519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
23619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
23719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
23819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @see AlertDialog.Builder#setCustomTitle(View)
23919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
24019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setCustomTitle(View customTitleView) {
24119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mCustomTitleView = customTitleView;
24219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
24319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
24419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setMessage(CharSequence message) {
24519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mMessage = message;
24619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mMessageView != null) {
24719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mMessageView.setText(message);
24819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
24919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
25019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
25119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
25219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * Set the view resource to display in the dialog.
25319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
25419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setView(int layoutResId) {
25519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mView = null;
25619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewLayoutResId = layoutResId;
25719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewSpacingSpecified = false;
25819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
25919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
26019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
26119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * Set the view to display in the dialog.
26219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
26319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setView(View view) {
26419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mView = view;
26519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewLayoutResId = 0;
26619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewSpacingSpecified = false;
26719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
26819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
26919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
27019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * Set the view to display in the dialog along with the spacing around that view
27119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
27219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight,
27319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            int viewSpacingBottom) {
27419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mView = view;
27519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewLayoutResId = 0;
27619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewSpacingSpecified = true;
27719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewSpacingLeft = viewSpacingLeft;
27819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewSpacingTop = viewSpacingTop;
27919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewSpacingRight = viewSpacingRight;
28019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mViewSpacingBottom = viewSpacingBottom;
28119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
28219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
28319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
28419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * Sets a hint for the best button panel layout.
28519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
28619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setButtonPanelLayoutHint(int layoutHint) {
28719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mButtonPanelLayoutHint = layoutHint;
28819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
28919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
29019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
29119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * Sets a click listener or a message to be sent when the button is clicked.
29219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * You only need to pass one of {@code listener} or {@code msg}.
29319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     *
29419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @param whichButton Which button, can be one of
29519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     *                    {@link DialogInterface#BUTTON_POSITIVE},
29619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     *                    {@link DialogInterface#BUTTON_NEGATIVE}, or
29719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     *                    {@link DialogInterface#BUTTON_NEUTRAL}
29819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @param text        The text to display in positive button.
29919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @param listener    The {@link DialogInterface.OnClickListener} to use.
30019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @param msg         The {@link Message} to be sent when clicked.
30119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
30219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setButton(int whichButton, CharSequence text,
30319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            DialogInterface.OnClickListener listener, Message msg) {
30419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
30519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (msg == null && listener != null) {
30619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            msg = mHandler.obtainMessage(whichButton, listener);
30719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
30819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
30919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        switch (whichButton) {
31019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
31119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            case DialogInterface.BUTTON_POSITIVE:
31219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mButtonPositiveText = text;
31319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mButtonPositiveMessage = msg;
31419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                break;
31519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
31619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            case DialogInterface.BUTTON_NEGATIVE:
31719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mButtonNegativeText = text;
31819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mButtonNegativeMessage = msg;
31919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                break;
32019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
32119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            case DialogInterface.BUTTON_NEUTRAL:
32219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mButtonNeutralText = text;
32319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mButtonNeutralMessage = msg;
32419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                break;
32519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
32619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            default:
32719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                throw new IllegalArgumentException("Button does not exist");
32819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
32919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
33019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
33119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
33219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * Specifies the icon to display next to the alert title.
33319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     *
33419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @param resId the resource identifier of the drawable to use as the icon,
33519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     *              or 0 for no icon
33619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
33719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setIcon(int resId) {
33819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mIcon = null;
33919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mIconId = resId;
34019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
34119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mIconView != null) {
34219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (resId != 0) {
34319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mIconView.setImageResource(mIconId);
34419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else {
34519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mIconView.setVisibility(View.GONE);
34619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
34719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
34819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
34919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
35019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
35119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * Specifies the icon to display next to the alert title.
35219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     *
35319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @param icon the drawable to use as the icon or null for no icon
35419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
35519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public void setIcon(Drawable icon) {
35619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mIcon = icon;
35719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mIconId = 0;
35819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
35919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mIconView != null) {
36019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (icon != null) {
36119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mIconView.setImageDrawable(icon);
36219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else {
36319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mIconView.setVisibility(View.GONE);
36419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
36519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
36619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
36719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
36819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    /**
36919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @param attrId the attributeId of the theme-specific drawable
37019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     *               to resolve the resourceId for.
37119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     * @return resId the resourceId of the theme-specific drawable
37219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes     */
37319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public int getIconAttributeResId(int attrId) {
37419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        TypedValue out = new TypedValue();
37519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mContext.getTheme().resolveAttribute(attrId, out, true);
37619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return out.resourceId;
37719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
37819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
37919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public ListView getListView() {
38019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return mListView;
38119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
38219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
38319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public Button getButton(int whichButton) {
38419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        switch (whichButton) {
38519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            case DialogInterface.BUTTON_POSITIVE:
38619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                return mButtonPositive;
38719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            case DialogInterface.BUTTON_NEGATIVE:
38819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                return mButtonNegative;
38919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            case DialogInterface.BUTTON_NEUTRAL:
39019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                return mButtonNeutral;
39119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            default:
39219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                return null;
39319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
39419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
39519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
39619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    @SuppressWarnings({"UnusedDeclaration"})
39719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public boolean onKeyDown(int keyCode, KeyEvent event) {
39819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return mScrollView != null && mScrollView.executeKeyEvent(event);
39919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
40019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
40119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    @SuppressWarnings({"UnusedDeclaration"})
40219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public boolean onKeyUp(int keyCode, KeyEvent event) {
40319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return mScrollView != null && mScrollView.executeKeyEvent(event);
40419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
40519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
40619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private void setupView() {
40719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final ViewGroup contentPanel = (ViewGroup) mWindow.findViewById(R.id.contentPanel);
40819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        setupContent(contentPanel);
40919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final boolean hasButtons = setupButtons();
41019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
41119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final ViewGroup topPanel = (ViewGroup) mWindow.findViewById(R.id.topPanel);
41219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final TintTypedArray a = TintTypedArray.obtainStyledAttributes(mContext,
41319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                null, R.styleable.AlertDialog, R.attr.alertDialogStyle, 0);
41419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final boolean hasTitle = setupTitle(topPanel);
41519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
41619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final View buttonPanel = mWindow.findViewById(R.id.buttonPanel);
41719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (!hasButtons) {
41819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            buttonPanel.setVisibility(View.GONE);
41919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            final View spacer = mWindow.findViewById(R.id.textSpacerNoButtons);
42019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (spacer != null) {
42119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                spacer.setVisibility(View.VISIBLE);
42219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
42319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
42419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
42519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final FrameLayout customPanel = (FrameLayout) mWindow.findViewById(R.id.customPanel);
42619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final View customView;
42719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mView != null) {
42819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            customView = mView;
42919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        } else if (mViewLayoutResId != 0) {
43019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            final LayoutInflater inflater = LayoutInflater.from(mContext);
43119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            customView = inflater.inflate(mViewLayoutResId, customPanel, false);
43219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        } else {
43319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            customView = null;
43419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
43519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
43619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final boolean hasCustomView = customView != null;
43719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (!hasCustomView || !canTextInput(customView)) {
43819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mWindow.setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
43919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
44019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
44119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
44219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (hasCustomView) {
44319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            final FrameLayout custom = (FrameLayout) mWindow.findViewById(R.id.custom);
44419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            custom.addView(customView, new LayoutParams(MATCH_PARENT, MATCH_PARENT));
44519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
44619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mViewSpacingSpecified) {
44719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                custom.setPadding(
44819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight, mViewSpacingBottom);
44919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
45019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
45119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mListView != null) {
45219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                ((LinearLayout.LayoutParams) customPanel.getLayoutParams()).weight = 0;
45319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
45419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        } else {
45519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            customPanel.setVisibility(View.GONE);
45619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
45719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
45819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        final ListView listView = mListView;
45919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (listView != null && mAdapter != null) {
46019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            listView.setAdapter(mAdapter);
46119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            final int checkedItem = mCheckedItem;
46219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (checkedItem > -1) {
46319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                listView.setItemChecked(checkedItem, true);
46419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                listView.setSelection(checkedItem);
46519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
46619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
46719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
46819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        a.recycle();
46919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
47019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
47119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private boolean setupTitle(ViewGroup topPanel) {
47219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        boolean hasTitle = true;
47319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
47419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mCustomTitleView != null) {
47519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            // Add the custom title view directly to the topPanel layout
47619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            LayoutParams lp = new LayoutParams(
47719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
47819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
47919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            topPanel.addView(mCustomTitleView, 0, lp);
48019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
48119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            // Hide the title template
48219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            View titleTemplate = mWindow.findViewById(R.id.title_template);
48319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            titleTemplate.setVisibility(View.GONE);
48419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        } else {
48519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mIconView = (ImageView) mWindow.findViewById(android.R.id.icon);
48619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
48719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            final boolean hasTextTitle = !TextUtils.isEmpty(mTitle);
48819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (hasTextTitle) {
48919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                // Display the title if a title is supplied, else hide it.
49019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mTitleView = (TextView) mWindow.findViewById(R.id.alertTitle);
49119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mTitleView.setText(mTitle);
49219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
49319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                // Do this last so that if the user has supplied any icons we
49419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                // use them instead of the default ones. If the user has
49519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                // specified 0 then make it disappear.
49619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                if (mIconId != 0) {
49719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    mIconView.setImageResource(mIconId);
49819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                } else if (mIcon != null) {
49919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    mIconView.setImageDrawable(mIcon);
50019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                } else {
50119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    // Apply the padding from the icon to ensure the title is
50219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    // aligned correctly.
50319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    mTitleView.setPadding(mIconView.getPaddingLeft(),
50419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mIconView.getPaddingTop(),
50519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mIconView.getPaddingRight(),
50619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mIconView.getPaddingBottom());
50719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    mIconView.setVisibility(View.GONE);
50819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                }
50919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else {
51019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                // Hide the title template
51119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                final View titleTemplate = mWindow.findViewById(R.id.title_template);
51219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                titleTemplate.setVisibility(View.GONE);
51319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mIconView.setVisibility(View.GONE);
51419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                topPanel.setVisibility(View.GONE);
51519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                hasTitle = false;
51619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
51719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
51819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return hasTitle;
51919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
52019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
52119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private void setupContent(ViewGroup contentPanel) {
52219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mScrollView = (ScrollView) mWindow.findViewById(R.id.scrollView);
52319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mScrollView.setFocusable(false);
52419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
52519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        // Special case for users that only want to display a String
52619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mMessageView = (TextView) mWindow.findViewById(android.R.id.message);
52719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mMessageView == null) {
52819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            return;
52919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
53019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
53119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (mMessage != null) {
53219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mMessageView.setText(mMessage);
53319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        } else {
53419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mMessageView.setVisibility(View.GONE);
53519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mScrollView.removeView(mMessageView);
53619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
53719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mListView != null) {
53819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                final ViewGroup scrollParent = (ViewGroup) mScrollView.getParent();
53919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                final int childIndex = scrollParent.indexOfChild(mScrollView);
54019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                scrollParent.removeViewAt(childIndex);
54119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                scrollParent.addView(mListView, childIndex,
54219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        new LayoutParams(MATCH_PARENT, MATCH_PARENT));
54319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else {
54419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                contentPanel.setVisibility(View.GONE);
54519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
54619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
54719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
54819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
54919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private boolean setupButtons() {
55019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        int BIT_BUTTON_POSITIVE = 1;
55119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        int BIT_BUTTON_NEGATIVE = 2;
55219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        int BIT_BUTTON_NEUTRAL = 4;
55319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        int whichButtons = 0;
55419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mButtonPositive = (Button) mWindow.findViewById(android.R.id.button1);
55519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mButtonPositive.setOnClickListener(mButtonHandler);
55619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
55719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (TextUtils.isEmpty(mButtonPositiveText)) {
55819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonPositive.setVisibility(View.GONE);
55919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        } else {
56019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonPositive.setText(mButtonPositiveText);
56119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonPositive.setVisibility(View.VISIBLE);
56219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            whichButtons = whichButtons | BIT_BUTTON_POSITIVE;
56319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
56419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
56519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mButtonNegative = (Button) mWindow.findViewById(android.R.id.button2);
56619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mButtonNegative.setOnClickListener(mButtonHandler);
56719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
56819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (TextUtils.isEmpty(mButtonNegativeText)) {
56919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonNegative.setVisibility(View.GONE);
57019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        } else {
57119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonNegative.setText(mButtonNegativeText);
57219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonNegative.setVisibility(View.VISIBLE);
57319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
57419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            whichButtons = whichButtons | BIT_BUTTON_NEGATIVE;
57519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
57619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
57719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mButtonNeutral = (Button) mWindow.findViewById(android.R.id.button3);
57819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        mButtonNeutral.setOnClickListener(mButtonHandler);
57919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
58019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (TextUtils.isEmpty(mButtonNeutralText)) {
58119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonNeutral.setVisibility(View.GONE);
58219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        } else {
58319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonNeutral.setText(mButtonNeutralText);
58419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mButtonNeutral.setVisibility(View.VISIBLE);
58519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
58619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            whichButtons = whichButtons | BIT_BUTTON_NEUTRAL;
58719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
58819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
58919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        if (shouldCenterSingleButton(mContext)) {
59019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            /*
59119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             * If we only have 1 button it should be centered on the layout and
59219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             * expand to fill 50% of the available space.
59319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             */
59419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (whichButtons == BIT_BUTTON_POSITIVE) {
59519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                centerButton(mButtonPositive);
59619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else if (whichButtons == BIT_BUTTON_NEGATIVE) {
59719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                centerButton(mButtonNegative);
59819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else if (whichButtons == BIT_BUTTON_NEUTRAL) {
59919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                centerButton(mButtonNeutral);
60019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
60119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
60219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
60319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        return whichButtons != 0;
60419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
60519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
60619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private void centerButton(Button button) {
60719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) button.getLayoutParams();
60819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        params.gravity = Gravity.CENTER_HORIZONTAL;
60919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        params.weight = 0.5f;
61019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        button.setLayoutParams(params);
61119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
61219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
61319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    public static class AlertParams {
61419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public final Context mContext;
61519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public final LayoutInflater mInflater;
61619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
61719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public int mIconId = 0;
61819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public Drawable mIcon;
61919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public int mIconAttrId = 0;
62019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public CharSequence mTitle;
62119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public View mCustomTitleView;
62219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public CharSequence mMessage;
62319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public CharSequence mPositiveButtonText;
62419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public DialogInterface.OnClickListener mPositiveButtonListener;
62519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public CharSequence mNegativeButtonText;
62619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public DialogInterface.OnClickListener mNegativeButtonListener;
62719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public CharSequence mNeutralButtonText;
62819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public DialogInterface.OnClickListener mNeutralButtonListener;
62919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public boolean mCancelable;
63019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public DialogInterface.OnCancelListener mOnCancelListener;
63119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public DialogInterface.OnDismissListener mOnDismissListener;
63219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public DialogInterface.OnKeyListener mOnKeyListener;
63319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public CharSequence[] mItems;
63419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public ListAdapter mAdapter;
63519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public DialogInterface.OnClickListener mOnClickListener;
63619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public int mViewLayoutResId;
63719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public View mView;
63819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public int mViewSpacingLeft;
63919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public int mViewSpacingTop;
64019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public int mViewSpacingRight;
64119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public int mViewSpacingBottom;
64219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public boolean mViewSpacingSpecified = false;
64319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public boolean[] mCheckedItems;
64419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public boolean mIsMultiChoice;
64519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public boolean mIsSingleChoice;
64619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public int mCheckedItem = -1;
64719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public DialogInterface.OnMultiChoiceClickListener mOnCheckboxClickListener;
64819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public Cursor mCursor;
64919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public String mLabelColumn;
65019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public String mIsCheckedColumn;
65119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public boolean mForceInverseBackground;
65219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public AdapterView.OnItemSelectedListener mOnItemSelectedListener;
65319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public OnPrepareListViewListener mOnPrepareListViewListener;
65419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public boolean mRecycleOnMeasure = true;
65519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
65619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        /**
65719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes         * Interface definition for a callback to be invoked before the ListView
65819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes         * will be bound to an adapter.
65919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes         */
66019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public interface OnPrepareListViewListener {
66119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
66219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            /**
66319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             * Called before the ListView is bound to an adapter.
66419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             *
66519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             * @param listView The ListView that will be shown in the dialog.
66619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             */
66719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            void onPrepareListView(ListView listView);
66819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
66919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
67019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public AlertParams(Context context) {
67119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mContext = context;
67219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mCancelable = true;
67319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
67419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
67519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
67619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public void apply(AlertController dialog) {
67719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mCustomTitleView != null) {
67819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                dialog.setCustomTitle(mCustomTitleView);
67919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else {
68019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                if (mTitle != null) {
68119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    dialog.setTitle(mTitle);
68219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                }
68319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                if (mIcon != null) {
68419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    dialog.setIcon(mIcon);
68519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                }
68619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                if (mIconId != 0) {
68719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    dialog.setIcon(mIconId);
68819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                }
68919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                if (mIconAttrId != 0) {
69019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    dialog.setIcon(dialog.getIconAttributeResId(mIconAttrId));
69119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                }
69219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
69319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mMessage != null) {
69419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                dialog.setMessage(mMessage);
69519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
69619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mPositiveButtonText != null) {
69719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,
69819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        mPositiveButtonListener, null);
69919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
70019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mNegativeButtonText != null) {
70119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText,
70219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        mNegativeButtonListener, null);
70319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
70419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mNeutralButtonText != null) {
70519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText,
70619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        mNeutralButtonListener, null);
70719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
70819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            // For a list, the client can either supply an array of items or an
70919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            // adapter or a cursor
71019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if ((mItems != null) || (mCursor != null) || (mAdapter != null)) {
71119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                createListView(dialog);
71219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
71319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mView != null) {
71419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                if (mViewSpacingSpecified) {
71519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    dialog.setView(mView, mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight,
71619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mViewSpacingBottom);
71719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                } else {
71819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    dialog.setView(mView);
71919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                }
72019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else if (mViewLayoutResId != 0) {
72119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                dialog.setView(mViewLayoutResId);
72219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
72319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
72419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            /*
72519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            dialog.setCancelable(mCancelable);
72619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            dialog.setOnCancelListener(mOnCancelListener);
72719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mOnKeyListener != null) {
72819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                dialog.setOnKeyListener(mOnKeyListener);
72919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
73019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            */
73119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
73219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
73319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        private void createListView(final AlertController dialog) {
73419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            final ListView listView = (ListView) mInflater.inflate(dialog.mListLayout, null);
73519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            ListAdapter adapter;
73619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
73719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mIsMultiChoice) {
73819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                if (mCursor == null) {
73919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    adapter = new ArrayAdapter<CharSequence>(
74019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mContext, dialog.mMultiChoiceItemLayout, android.R.id.text1, mItems) {
74119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        @Override
74219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        public View getView(int position, View convertView, ViewGroup parent) {
74319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            View view = super.getView(position, convertView, parent);
74419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            if (mCheckedItems != null) {
74519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                                boolean isItemChecked = mCheckedItems[position];
74619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                                if (isItemChecked) {
74719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                                    listView.setItemChecked(position, true);
74819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                                }
74919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            }
75019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            return view;
75119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        }
75219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    };
75319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                } else {
75419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    adapter = new CursorAdapter(mContext, mCursor, false) {
75519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        private final int mLabelIndex;
75619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        private final int mIsCheckedIndex;
75719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
75819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        {
75919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            final Cursor cursor = getCursor();
76019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mLabelIndex = cursor.getColumnIndexOrThrow(mLabelColumn);
76119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mIsCheckedIndex = cursor.getColumnIndexOrThrow(mIsCheckedColumn);
76219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        }
76319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
76419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        @Override
76519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        public void bindView(View view, Context context, Cursor cursor) {
76619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            CheckedTextView text = (CheckedTextView) view
76719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                                    .findViewById(android.R.id.text1);
76819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            text.setText(cursor.getString(mLabelIndex));
76919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            listView.setItemChecked(cursor.getPosition(),
77019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                                    cursor.getInt(mIsCheckedIndex) == 1);
77119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        }
77219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
77319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        @Override
77419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        public View newView(Context context, Cursor cursor, ViewGroup parent) {
77519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            return mInflater.inflate(dialog.mMultiChoiceItemLayout,
77619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                                    parent, false);
77719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        }
77819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
77919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    };
78019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                }
78119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else {
78219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                int layout = mIsSingleChoice
78319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        ? dialog.mSingleChoiceItemLayout : dialog.mListItemLayout;
78419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                if (mCursor == null) {
78519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    adapter = (mAdapter != null) ? mAdapter
78619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            : new CheckedItemAdapter(mContext, layout, android.R.id.text1, mItems);
78719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                } else {
78819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    adapter = new SimpleCursorAdapter(mContext, layout,
78919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mCursor, new String[]{mLabelColumn}, new int[]{android.R.id.text1});
79019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                }
79119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
79219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
79319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mOnPrepareListViewListener != null) {
79419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                mOnPrepareListViewListener.onPrepareListView(listView);
79519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
79619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
79719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            /* Don't directly set the adapter on the ListView as we might
79819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             * want to add a footer to the ListView later.
79919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes             */
80019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            dialog.mAdapter = adapter;
80119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            dialog.mCheckedItem = mCheckedItem;
80219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
80319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mOnClickListener != null) {
80419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                listView.setOnItemClickListener(new OnItemClickListener() {
80519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    @Override
80619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
80719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        mOnClickListener.onClick(dialog.mDialog, position);
80819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        if (!mIsSingleChoice) {
80919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            dialog.mDialog.dismiss();
81019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        }
81119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    }
81219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                });
81319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else if (mOnCheckboxClickListener != null) {
81419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                listView.setOnItemClickListener(new OnItemClickListener() {
81519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    @Override
81619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
81719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        if (mCheckedItems != null) {
81819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                            mCheckedItems[position] = listView.isItemChecked(position);
81919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        }
82019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                        mOnCheckboxClickListener.onClick(
82119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                                dialog.mDialog, position, listView.isItemChecked(position));
82219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                    }
82319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                });
82419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
82519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
82619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            // Attach a given OnItemSelectedListener to the ListView
82719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mOnItemSelectedListener != null) {
82819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                listView.setOnItemSelectedListener(mOnItemSelectedListener);
82919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
83019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
83119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            if (mIsSingleChoice) {
83219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
83319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            } else if (mIsMultiChoice) {
83419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
83519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            }
83619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            dialog.mListView = listView;
83719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
83819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
83919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
84019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    private static class CheckedItemAdapter extends ArrayAdapter<CharSequence> {
84119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public CheckedItemAdapter(Context context, int resource, int textViewResourceId,
84219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes                CharSequence[] objects) {
84319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            super(context, resource, textViewResourceId, objects);
84419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
84519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
84619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        @Override
84719eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public boolean hasStableIds() {
84819eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            return true;
84919eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
85019eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes
85119eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        @Override
85219eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        public long getItemId(int position) {
85319eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes            return position;
85419eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes        }
85519eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes    }
85619eaf14cb3cca613f984a32be2fab2534427fb29Chris Banes}
857