Home | History | Annotate | Download | only in phone
      1 package com.android.phone;
      2 
      3 import android.app.AlertDialog;
      4 import android.app.Dialog;
      5 import android.app.ProgressDialog;
      6 import android.content.DialogInterface;
      7 import android.preference.Preference;
      8 import android.preference.PreferenceActivity;
      9 import android.util.Log;
     10 import android.view.WindowManager;
     11 
     12 import java.util.ArrayList;
     13 
     14 interface  TimeConsumingPreferenceListener {
     15     public void onStarted(Preference preference, boolean reading);
     16     public void onFinished(Preference preference, boolean reading);
     17     public void onError(Preference preference, int error);
     18 }
     19 
     20 public class TimeConsumingPreferenceActivity extends PreferenceActivity
     21                         implements TimeConsumingPreferenceListener, DialogInterface.OnClickListener,
     22                         DialogInterface.OnCancelListener {
     23     private static final String LOG_TAG = "TimeConsumingPreferenceActivity";
     24     private final boolean DBG = (PhoneApp.DBG_LEVEL >= 2);
     25 
     26     private static final int BUSY_READING_DIALOG = 100;
     27     private static final int BUSY_SAVING_DIALOG = 200;
     28 
     29     static final int EXCEPTION_ERROR = 300;
     30     static final int RESPONSE_ERROR = 400;
     31     static final int RADIO_OFF_ERROR = 500;
     32 
     33     private final ArrayList<String> mBusyList=new ArrayList<String> ();
     34 
     35     protected boolean mIsForeground = false;
     36 
     37     @Override
     38     protected Dialog onCreateDialog(int id) {
     39         if (id == BUSY_READING_DIALOG || id == BUSY_SAVING_DIALOG) {
     40             ProgressDialog dialog = new ProgressDialog(this);
     41             dialog.setTitle(getText(R.string.updating_title));
     42             dialog.setIndeterminate(true);
     43 
     44             switch(id) {
     45                 case BUSY_READING_DIALOG:
     46                     dialog.setCancelable(true);
     47                     dialog.setOnCancelListener(this);
     48                     dialog.setMessage(getText(R.string.reading_settings));
     49                     return dialog;
     50                 case BUSY_SAVING_DIALOG:
     51                     dialog.setCancelable(false);
     52                     dialog.setMessage(getText(R.string.updating_settings));
     53                     return dialog;
     54             }
     55             return null;
     56         }
     57 
     58         if (id == RESPONSE_ERROR || id == RADIO_OFF_ERROR || id == EXCEPTION_ERROR) {
     59             AlertDialog.Builder b = new AlertDialog.Builder(this);
     60 
     61             int msgId;
     62             int titleId = R.string.error_updating_title;
     63 
     64             switch (id) {
     65                 case RESPONSE_ERROR:
     66                     msgId = R.string.response_error;
     67                     // Set Button 2, tells the activity that the error is
     68                     // recoverable on dialog exit.
     69                     b.setNegativeButton(R.string.close_dialog, this);
     70                     break;
     71                 case RADIO_OFF_ERROR:
     72                     msgId = R.string.radio_off_error;
     73                     // Set Button 3
     74                     b.setNeutralButton(R.string.close_dialog, this);
     75                     break;
     76                 case EXCEPTION_ERROR:
     77                 default:
     78                     msgId = R.string.exception_error;
     79                     // Set Button 3, tells the activity that the error is
     80                     // not recoverable on dialog exit.
     81                     b.setNeutralButton(R.string.close_dialog, this);
     82                     break;
     83             }
     84 
     85             b.setTitle(getText(titleId));
     86             b.setMessage(getText(msgId));
     87             b.setCancelable(false);
     88             AlertDialog dialog = b.create();
     89 
     90             // make the dialog more obvious by blurring the background.
     91             dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
     92 
     93             return dialog;
     94         }
     95         return null;
     96     }
     97 
     98     @Override
     99     public void onResume() {
    100         super.onResume();
    101         mIsForeground = true;
    102     }
    103 
    104     @Override
    105     public void onPause() {
    106         super.onPause();
    107         mIsForeground = false;
    108     }
    109 
    110     public void onClick(DialogInterface dialog, int which) {
    111         dialog.dismiss();
    112     }
    113 
    114     public void onStarted(Preference preference, boolean reading) {
    115         if (DBG) dumpState();
    116         if (DBG) Log.d(LOG_TAG, "onStarted, preference=" + preference.getKey()
    117                 + ", reading=" + reading);
    118         mBusyList.add(preference.getKey());
    119 
    120         if (mIsForeground) {
    121               if (reading) {
    122                   showDialog(BUSY_READING_DIALOG);
    123               } else {
    124                   showDialog(BUSY_SAVING_DIALOG);
    125               }
    126         }
    127 
    128     }
    129 
    130     public void onFinished(Preference preference, boolean reading) {
    131         if (DBG) dumpState();
    132         if (DBG) Log.d(LOG_TAG, "onFinished, preference=" + preference.getKey()
    133                 + ", reading=" + reading);
    134         mBusyList.remove(preference.getKey());
    135 
    136         if (mBusyList.isEmpty() && mIsForeground) {
    137             if (reading) {
    138                 dismissDialogSafely(BUSY_READING_DIALOG);
    139             } else {
    140                 dismissDialogSafely(BUSY_SAVING_DIALOG);
    141             }
    142         }
    143     }
    144 
    145     public void onError(Preference preference, int error) {
    146         if (DBG) dumpState();
    147         if (DBG) Log.d(LOG_TAG, "onError, preference=" + preference.getKey() + ", error=" + error);
    148 
    149         if (mIsForeground) {
    150             showDialog(error);
    151         }
    152     }
    153 
    154     public void onCancel(DialogInterface dialog) {
    155         if (DBG) dumpState();
    156         finish();
    157     }
    158 
    159     private void dismissDialogSafely(int id) {
    160         try {
    161             dismissDialog(id);
    162         } catch (IllegalArgumentException e) {
    163             // This is expected in the case where we were in the background
    164             // at the time we would normally have shown the dialog, so we didn't
    165             // show it.
    166         }
    167     }
    168 
    169     void dumpState() {
    170         Log.d(LOG_TAG, "dumpState begin");
    171         for (String key : mBusyList) {
    172             Log.d(LOG_TAG, "mBusyList: key=" + key);
    173         }
    174         Log.d(LOG_TAG, "dumpState end");
    175     }
    176 }
    177