Home | History | Annotate | Download | only in browse
      1 /*******************************************************************************
      2  *      Copyright (C) 2012 Google Inc.
      3  *      Licensed to The Android Open Source Project.
      4  *
      5  *      Licensed under the Apache License, Version 2.0 (the "License");
      6  *      you may not use this file except in compliance with the License.
      7  *      You may obtain a copy of the License at
      8  *
      9  *           http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *      Unless required by applicable law or agreed to in writing, software
     12  *      distributed under the License is distributed on an "AS IS" BASIS,
     13  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *      See the License for the specific language governing permissions and
     15  *      limitations under the License.
     16  *******************************************************************************/
     17 
     18 package com.android.mail.browse;
     19 
     20 import android.app.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.DialogFragment;
     23 import android.app.FragmentManager;
     24 import android.content.DialogInterface;
     25 import android.os.Bundle;
     26 
     27 import com.android.mail.R;
     28 import com.android.mail.ui.ControllableActivity;
     29 import com.android.mail.ui.ConversationUpdater;
     30 
     31 /**
     32  * A dialog which is also a fragment. This dialog shows a message, two buttons (ok/cancel), and runs
     33  * a listener only for the positive action (ok). Since this is a fragment, it is created
     34  * automatically over orientation changes. To make a listener that works with this dialog, create a
     35  * listener with {@link ConversationUpdater#makeDialogListener(int, boolean)}, which correctly
     36  * handles activity life-cycle events.
     37  */
     38 public class ConfirmDialogFragment extends DialogFragment {
     39     /** Tag for saving the message shown in the dialog. */
     40     private static final String MESSAGE_KEY = "message";
     41     /** Tag for the dialog in the fragment manager. */
     42     private static final String DIALOG_TAG = "confirm-dialog";
     43 
     44     /**
     45      * Since the fragment can be easily destroyed, get the listener from the central activity
     46      * immediately after the positive button ("OK" in English) is clicked.  We cannot get the
     47      * listener in {@link #onActivityCreated(Bundle)} because the controller has not had a chance
     48      * to create the listener yet.
     49      */
     50     private final AlertDialog.OnClickListener POSITIVE_ACTION = new AlertDialog.OnClickListener() {
     51         @Override
     52         public void onClick(DialogInterface arg0, int arg1) {
     53             // Casting to ControllableActivity will crash if it fails. This is the expected
     54             // behavior, since the cast should always succeed. Cast failures only occur during
     55             // development.
     56             final AlertDialog.OnClickListener listener =
     57                     ((ControllableActivity) getActivity()).getConversationUpdater().getListener();
     58             if (listener != null) {
     59                 listener.onClick(arg0, arg1);
     60             }
     61         }
     62     };
     63 
     64     /**
     65      * Needs a public empty constructor for instantiation on orientation changes.
     66      */
     67     public ConfirmDialogFragment() {}
     68 
     69     /**
     70      * Create a new {@link ConfirmDialogFragment}.
     71      * @param message
     72      * @return new {@link ConfirmDialogFragment} object.
     73      */
     74     public static ConfirmDialogFragment newInstance(CharSequence message) {
     75         final ConfirmDialogFragment f = new ConfirmDialogFragment();
     76         final Bundle args = new Bundle();
     77         args.putCharSequence(MESSAGE_KEY, message);
     78         f.setArguments(args);
     79         return f;
     80     }
     81 
     82     @Override
     83     public Dialog onCreateDialog(Bundle savedState) {
     84         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     85         final CharSequence message = (getArguments()).getCharSequence(MESSAGE_KEY);
     86         builder.setMessage(message)
     87                .setPositiveButton(R.string.ok, POSITIVE_ACTION)
     88                .setNegativeButton(R.string.cancel, null);
     89         return builder.create();
     90     }
     91 
     92     /**
     93      * Display this dialog with the provided fragment manager
     94      * @param manager
     95      */
     96     public final void displayDialog (FragmentManager manager) {
     97         show(manager, DIALOG_TAG);
     98     }
     99 }
    100