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 { onStarted(Preference preference, boolean reading)15 public void onStarted(Preference preference, boolean reading); onFinished(Preference preference, boolean reading)16 public void onFinished(Preference preference, boolean reading); onError(Preference preference, int error)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 ArrayList<String> mBusyList=new ArrayList<String> (); 34 35 @Override onCreateDialog(int id)36 protected Dialog onCreateDialog(int id) { 37 if (id == BUSY_READING_DIALOG || id == BUSY_SAVING_DIALOG) { 38 ProgressDialog dialog = new ProgressDialog(this); 39 dialog.setTitle(getText(R.string.updating_title)); 40 dialog.setIndeterminate(true); 41 42 switch(id) { 43 case BUSY_READING_DIALOG: 44 dialog.setCancelable(true); 45 dialog.setOnCancelListener(this); 46 dialog.setMessage(getText(R.string.reading_settings)); 47 return dialog; 48 case BUSY_SAVING_DIALOG: 49 dialog.setCancelable(false); 50 dialog.setMessage(getText(R.string.updating_settings)); 51 return dialog; 52 } 53 return null; 54 } 55 56 if (id == RESPONSE_ERROR || id == RADIO_OFF_ERROR || id == EXCEPTION_ERROR) { 57 AlertDialog.Builder b = new AlertDialog.Builder(this); 58 59 int msgId; 60 int titleId = R.string.error_updating_title; 61 62 switch (id) { 63 case RESPONSE_ERROR: 64 msgId = R.string.response_error; 65 // Set Button 2, tells the activity that the error is 66 // recoverable on dialog exit. 67 b.setNegativeButton(R.string.close_dialog, this); 68 break; 69 case RADIO_OFF_ERROR: 70 msgId = R.string.radio_off_error; 71 // Set Button 3 72 b.setNeutralButton(R.string.close_dialog, this); 73 break; 74 case EXCEPTION_ERROR: 75 default: 76 msgId = R.string.exception_error; 77 // Set Button 3, tells the activity that the error is 78 // not recoverable on dialog exit. 79 b.setNeutralButton(R.string.close_dialog, this); 80 break; 81 } 82 83 b.setTitle(getText(titleId)); 84 b.setMessage(getText(msgId)); 85 b.setCancelable(false); 86 AlertDialog dialog = b.create(); 87 88 // make the dialog more obvious by blurring the background. 89 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); 90 91 return dialog; 92 } 93 return null; 94 } 95 onClick(DialogInterface dialog, int which)96 public void onClick(DialogInterface dialog, int which) { 97 dialog.dismiss(); 98 } 99 onStarted(Preference preference, boolean reading)100 public void onStarted(Preference preference, boolean reading) { 101 if (DBG) dumpState(); 102 if (DBG) Log.d(LOG_TAG, "onStarted, preference=" + preference.getKey() 103 + ", reading=" + reading); 104 mBusyList.add(preference.getKey()); 105 106 if (reading) { 107 showDialog(BUSY_READING_DIALOG); 108 } else { 109 showDialog(BUSY_SAVING_DIALOG); 110 } 111 } 112 onFinished(Preference preference, boolean reading)113 public void onFinished(Preference preference, boolean reading) { 114 if (DBG) dumpState(); 115 if (DBG) Log.d(LOG_TAG, "onFinished, preference=" + preference.getKey() 116 + ", reading=" + reading); 117 mBusyList.remove(preference.getKey()); 118 119 if (mBusyList.isEmpty()) { 120 if (reading) { 121 dismissDialog(BUSY_READING_DIALOG); 122 } else { 123 dismissDialog(BUSY_SAVING_DIALOG); 124 } 125 } 126 } 127 onError(Preference preference, int error)128 public void onError(Preference preference, int error) { 129 if (DBG) dumpState(); 130 if (DBG) Log.d(LOG_TAG, "onError, preference=" + preference.getKey() + ", error=" + error); 131 showDialog(error); 132 } 133 onCancel(DialogInterface dialog)134 public void onCancel(DialogInterface dialog) { 135 if (DBG) dumpState(); 136 finish(); 137 } 138 dumpState()139 void dumpState() { 140 Log.d(LOG_TAG, "dumpState begin"); 141 for (String key : mBusyList) { 142 Log.d(LOG_TAG, "mBusyList: key=" + key); 143 } 144 Log.d(LOG_TAG, "dumpState end"); 145 } 146 } 147