1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.activity.setup;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.DialogFragment;
22import android.app.ProgressDialog;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.os.Bundle;
26
27import com.android.email.R;
28import com.android.mail.utils.LogUtils;
29
30/**
31 * Simple dialog that shows progress as we work through the settings checks.
32 * This is stateless except for its UI (e.g. current strings) and can be torn down or
33 * recreated at any time without affecting the account checking progress.
34 */
35public class CheckSettingsProgressDialogFragment extends DialogFragment {
36    public final static String TAG = "CheckProgressDialog";
37
38    // Extras for saved instance state
39    private final static String ARGS_PROGRESS_STRING = "CheckProgressDialog.Progress";
40    private final static String ARGS_MODE_INT = "CheckProgressDialog.Mode";
41
42    // UI
43    private String mProgressString;
44
45    public interface Callback {
46        void onCheckSettingsProgressDialogCancel();
47    }
48
49    // Public no-args constructor needed for fragment re-instantiation
50    public CheckSettingsProgressDialogFragment() {}
51
52    /**
53     * Create a dialog that reports progress
54     * @param checkMode check settings mode
55     */
56    public static CheckSettingsProgressDialogFragment newInstance(int checkMode) {
57        final CheckSettingsProgressDialogFragment f = new CheckSettingsProgressDialogFragment();
58        final Bundle b = new Bundle(1);
59        b.putInt(ARGS_MODE_INT, checkMode);
60        f.setArguments(b);
61        return f;
62    }
63
64    /**
65     * Update the progress of an existing dialog
66     * @param progress latest progress to be displayed
67     */
68    protected void updateProgress(int progress) {
69        mProgressString = AccountCheckSettingsFragment.getProgressString(getActivity(), progress);
70        final AlertDialog dialog = (AlertDialog) getDialog();
71        if (dialog != null && mProgressString != null) {
72            dialog.setMessage(mProgressString);
73        }
74    }
75
76    @Override
77    public Dialog onCreateDialog(Bundle savedInstanceState) {
78        final Context context = getActivity();
79        if (savedInstanceState != null) {
80            mProgressString = savedInstanceState.getString(ARGS_PROGRESS_STRING);
81        }
82        if (mProgressString == null) {
83            final int checkMode = getArguments().getInt(ARGS_MODE_INT);
84            final int progress = AccountCheckSettingsFragment.getProgressForMode(checkMode);
85            mProgressString = AccountCheckSettingsFragment.getProgressString(getActivity(),
86                    progress);
87        }
88
89        // Don't bail out if the user taps outside the progress window
90        setCancelable(false);
91
92        final ProgressDialog dialog = new ProgressDialog(context);
93        dialog.setIndeterminate(true);
94        dialog.setMessage(mProgressString);
95        dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
96                context.getString(android.R.string.cancel),
97                new DialogInterface.OnClickListener() {
98                    @Override
99                    public void onClick(DialogInterface dialog, int which) {
100                        dialog.cancel();
101                    }
102                });
103        return dialog;
104    }
105
106    /**
107     * Listen for cancellation, which can happen from places other than the
108     * negative button (e.g. touching outside the dialog), and stop the checker
109     */
110    @Override
111    public void onCancel(DialogInterface dialog) {
112        super.onCancel(dialog);
113        final Callback callback = (Callback) getActivity();
114        if (callback != null) {
115            callback.onCheckSettingsProgressDialogCancel();
116        } else {
117            LogUtils.d(LogUtils.TAG, "Null callback in CheckSettings dialog onCancel");
118        }
119    }
120
121    @Override
122    public void onSaveInstanceState(Bundle outState) {
123        super.onSaveInstanceState(outState);
124        outState.putString(ARGS_PROGRESS_STRING, mProgressString);
125    }
126}
127