1 /* 2 * Copyright (C) 2011 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 17 package com.example.android.voicemail.common.ui; 18 19 import android.app.Activity; 20 import android.app.AlertDialog; 21 import android.app.Dialog; 22 import android.os.Bundle; 23 24 /** 25 * Uses an {@link Activity} to show Dialogs. 26 * <p> 27 * Instantiate this class inside your Activity. 28 * 29 * <pre class="code"> 30 * private final DialogHelperImpl mDialogHelper = new DialogHelperImpl(this); 31 * </pre> 32 * <p> 33 * Override your Activity's onCreateDialog(int, Bundle) method, as follows: 34 * 35 * <pre class="code"> 36 * @Override 37 * protected Dialog onCreateDialog(int id, Bundle bundle) { 38 * return mDialogHelper.handleOnCreateDialog(id, bundle); 39 * } 40 * </pre> 41 * <p> 42 * Now you can pass mDialogHelper around as a {@link DialogHelper} interface, and code that wants to 43 * show a Dialog can call a method like this: 44 * 45 * <pre class="code"> 46 * mDialogHelper.showErrorMessageDialog("An exception occurred!", e); 47 * </pre> 48 * <p> 49 * If you want more flexibility, and want to mix this implementation with your own dialogs, then you 50 * should do something like this in your Activity: 51 * 52 * <pre class="code"> 53 * @Override 54 * protected Dialog onCreateDialog(int id, Bundle bundle) { 55 * switch (id) { 56 * case ID_MY_OTHER_DIALOG: 57 * return new AlertDialog.Builder(this) 58 * .setTitle("something") 59 * .create(); 60 * default: 61 * return mDialogHelper.handleOnCreateDialog(id, bundle); 62 * } 63 * } 64 * </pre> 65 * 66 * Just be careful that you don't pick any IDs that conflict with those used by this class (which 67 * are documented in the public static final fields). 68 */ 69 public class DialogHelperImpl implements DialogHelper { 70 public static final int DIALOG_ID_EXCEPTION = 88953588; 71 72 private static final String KEY_EXCEPTION = "exception"; 73 private static final String KEY_TITLE = "title"; 74 75 private final Activity mActivity; 76 DialogHelperImpl(Activity activity)77 public DialogHelperImpl(Activity activity) { 78 mActivity = activity; 79 } 80 81 @Override showErrorMessageDialog(int titleId, Exception exception)82 public void showErrorMessageDialog(int titleId, Exception exception) { 83 showErrorMessageDialog(mActivity.getString(titleId), exception); 84 } 85 86 @Override showErrorMessageDialog(String title, Exception exception)87 public void showErrorMessageDialog(String title, Exception exception) { 88 Bundle bundle = new Bundle(); 89 bundle.putString(KEY_TITLE, title); 90 bundle.putSerializable(KEY_EXCEPTION, exception); 91 mActivity.showDialog(DIALOG_ID_EXCEPTION, bundle); 92 } 93 94 /** 95 * You should call this method from your Activity's onCreateDialog(int, Bundle) method. 96 */ handleOnCreateDialog(int id, Bundle args)97 public Dialog handleOnCreateDialog(int id, Bundle args) { 98 if (id == DIALOG_ID_EXCEPTION) { 99 Exception exception = (Exception) args.getSerializable(KEY_EXCEPTION); 100 String title = args.getString(KEY_TITLE); 101 return new AlertDialog.Builder(mActivity) 102 .setTitle(title) 103 .setMessage(convertExceptionToErrorMessage(exception)) 104 .setCancelable(true) 105 .create(); 106 } 107 return null; 108 } 109 convertExceptionToErrorMessage(Exception exception)110 private String convertExceptionToErrorMessage(Exception exception) { 111 StringBuilder sb = new StringBuilder().append(exception.getClass().getSimpleName()); 112 if (exception.getMessage() != null) { 113 sb.append("\n"); 114 sb.append(exception.getMessage()); 115 } 116 return sb.toString(); 117 } 118 } 119