1/*
2 * Copyright (C) 2012 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.mms.ui;
18
19import android.app.Activity;
20import android.app.ProgressDialog;
21import android.os.AsyncTask;
22import android.os.Handler;
23
24
25/**
26 * This AsyncDialog class is used to execute a runnable in a background thread and once that
27 * finishes, execute a runnable on the UI thread. If the background runnable task takes longer
28 * than half a second, a progress modal dialog is displayed.
29 *
30 */
31public class AsyncDialog {
32    private ProgressDialog mProgressDialog;
33    private final Activity mActivity;
34    private final Handler mHandler;
35
36    public AsyncDialog(Activity activity) {
37        mActivity = activity;
38        mHandler = new Handler();
39    }
40
41    /**
42     * Asynchronously executes a task while blocking the UI with a progress spinner.
43     *
44     * Must be invoked by the UI thread.  No exceptions!
45     *
46     * @param backgroundTask the work to be done in the background wrapped in a Runnable
47     * @param postExecuteTask an optional runnable to run on the UI thread when the background
48     * runnable is finished
49     * @param dialogStringId the id of the string to be shown in the dialog
50     */
51    public void runAsync(final Runnable backgroundTask,
52            final Runnable postExecuteTask, final int dialogStringId) {
53        new ModalDialogAsyncTask(dialogStringId, postExecuteTask)
54            .execute(new Runnable[] {backgroundTask});
55    }
56
57    // Shows the activity's progress spinner. Should be canceled if exiting the activity.
58    private Runnable mShowProgressDialogRunnable = new Runnable() {
59        @Override
60        public void run() {
61            if (mProgressDialog != null) {
62                mProgressDialog.show();
63            }
64        }
65    };
66
67    public void clearPendingProgressDialog() {
68        // remove any callback to display a progress spinner
69        mHandler.removeCallbacks(mShowProgressDialogRunnable);
70        // clear the dialog so any pending dialog.dismiss() call can be avoided
71        mProgressDialog = null;
72    }
73
74    /**
75     * Asynchronously performs tasks specified by Runnables.
76     * Displays a progress spinner while the tasks are running.  The progress spinner
77     * will only show if tasks have not finished after a certain amount of time.
78     *
79     * This AsyncTask must be instantiated and invoked on the UI thread.
80     *
81     * TODO: Need to implement a way for the background thread to pass a result to
82     * the onPostExecute thread. AsyncTask already provides this functionality.
83     */
84    private class ModalDialogAsyncTask extends AsyncTask<Runnable, Void, Void> {
85        final Runnable mPostExecuteTask;
86
87        /**
88         * Creates the Task with the specified string id to be shown in the dialog
89         */
90        public ModalDialogAsyncTask(int dialogStringId,
91                final Runnable postExecuteTask) {
92            mPostExecuteTask = postExecuteTask;
93            // lazy initialization of progress dialog for loading attachments
94            if (mProgressDialog == null) {
95                mProgressDialog = createProgressDialog();
96            }
97            mProgressDialog.setMessage(mActivity.getText(dialogStringId));
98        }
99
100        /**
101         * Initializes the progress dialog with its intended settings.
102         */
103        private ProgressDialog createProgressDialog() {
104            ProgressDialog dialog = new ProgressDialog(mActivity);
105            dialog.setIndeterminate(true);
106            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
107            dialog.setCanceledOnTouchOutside(false);
108            dialog.setCancelable(false);
109            return dialog;
110        }
111
112        /**
113         * Activates a progress spinner on the UI.  This assumes the UI has invoked this Task.
114         */
115        @Override
116        protected void onPreExecute() {
117            // activate spinner after half a second
118            mHandler.postDelayed(mShowProgressDialogRunnable, 500);
119        }
120
121        /**
122         * Perform the specified Runnable tasks on a background thread
123         */
124        @Override
125        protected Void doInBackground(Runnable... params) {
126            if (params != null) {
127                try {
128                    for (int i = 0; i < params.length; i++) {
129                        params[i].run();
130                    }
131
132                    // Test code. Uncomment this block to test the progress dialog popping up.
133//                    try {
134//                        Thread.sleep(2000);
135//                    } catch (Exception e) {
136//                    }
137                } finally {
138                    // Cancel pending display of the progress bar if the background task has
139                    // finished before the progress bar has popped up.
140                    mHandler.removeCallbacks(mShowProgressDialogRunnable);
141                }
142            }
143            return null;
144        }
145
146        /**
147         * Deactivates the progress spinner on the UI. This assumes the UI has invoked this Task.
148         */
149        @Override
150        protected void onPostExecute(Void result) {
151            if (mActivity.isFinishing()) {
152                return;
153            }
154            if (mProgressDialog != null && mProgressDialog.isShowing()) {
155                mProgressDialog.dismiss();
156            }
157            if (mPostExecuteTask != null) {
158                mPostExecuteTask.run();
159            }
160        }
161    }
162}