Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2013 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.ui;
     19 
     20 import android.app.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.DialogFragment;
     23 import android.content.DialogInterface;
     24 import android.os.Bundle;
     25 
     26 import com.android.mail.R;
     27 import com.android.mail.providers.Folder;
     28 import com.android.mail.providers.UIProvider;
     29 
     30 import java.lang.ref.WeakReference;
     31 
     32 /**
     33  * A confirmation dialog for emptying folders of all contents.
     34  * Currently used to empty trash or spam.
     35  */
     36 public class EmptyFolderDialogFragment extends DialogFragment {
     37 
     38     public interface EmptyFolderDialogFragmentListener {
     39 
     40         /**
     41          * Called when the folder is emptied by the DialogFragment.
     42          */
     43         void onFolderEmptied();
     44     }
     45 
     46     public static final String FRAGMENT_TAG = "EmptyFolderDialogFragment";
     47 
     48     private static final String ARG_NUM_CONVERSATIONS = "numConversations";
     49     private static final String ARG_FOLDER_TYPE = "folderType";
     50 
     51     private WeakReference<EmptyFolderDialogFragmentListener> mListener = null;
     52 
     53     private int mNumConversations;
     54     private int mFolderType;
     55 
     56     // Public no-args constructor needed for fragment re-instantiation
     57     public EmptyFolderDialogFragment() {}
     58 
     59     /**
     60      * Creates a new instance of {@link EmptyFolderDialogFragment}.
     61      * @param numConversations The number of conversations to display in the dialog.
     62      * @param folderType The type of dialog to show. The current available options are
     63      *                   {@link com.android.mail.providers.UIProvider.FolderType#TRASH} and
     64      *                   {@link com.android.mail.providers.UIProvider.FolderType#SPAM}.
     65      * @return The newly created {@link EmptyFolderDialogFragment}.
     66      */
     67     public static EmptyFolderDialogFragment newInstance(
     68             final int numConversations, final int folderType) {
     69         final EmptyFolderDialogFragment fragment =
     70                 new EmptyFolderDialogFragment();
     71 
     72         final Bundle args = new Bundle(2);
     73         args.putInt(ARG_NUM_CONVERSATIONS, numConversations);
     74         args.putInt(ARG_FOLDER_TYPE, folderType);
     75         fragment.setArguments(args);
     76 
     77         return fragment;
     78     }
     79 
     80     @Override
     81     public Dialog onCreateDialog(final Bundle savedInstanceState) {
     82         mNumConversations = getArguments().getInt(ARG_NUM_CONVERSATIONS);
     83         mFolderType = getArguments().getInt(ARG_FOLDER_TYPE);
     84 
     85         final String dialogMessage = getResources().getQuantityString(
     86                 R.plurals.empty_folder_dialog_message, mNumConversations, mNumConversations);
     87 
     88         // Checks if we're in the spam folder, otherwise just uses trash as the default.
     89         final int dialogTitleId = Folder.isType(mFolderType, UIProvider.FolderType.SPAM) ?
     90                 R.string.empty_spam_dialog_title : R.string.empty_trash_dialog_title;
     91 
     92         return new AlertDialog.Builder(getActivity()).setTitle(dialogTitleId)
     93                 .setMessage(dialogMessage)
     94                 .setNegativeButton(R.string.cancel, null)
     95                 .setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
     96                     @Override
     97                     public void onClick(final DialogInterface dialog, final int whichButton) {
     98                         if (mListener != null) {
     99                             final EmptyFolderDialogFragmentListener listener =
    100                                     mListener.get();
    101                             if (listener != null) {
    102                                 listener.onFolderEmptied();
    103                             }
    104                         }
    105                     }
    106                 })
    107                 .create();
    108     }
    109 
    110     public void setListener(final EmptyFolderDialogFragmentListener listener) {
    111         mListener = new WeakReference<EmptyFolderDialogFragmentListener>(listener);
    112     }
    113 }
    114