EditQuickResponseDialog.java revision 7985b43ab7310f76c170266da346f993d2f86051
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.email.activity.setup; 18 19import com.android.email.R; 20import com.android.emailcommon.provider.QuickResponse; 21import com.android.emailcommon.provider.EmailContent.QuickResponseColumns; 22import com.android.emailcommon.utility.EmailAsyncTask; 23 24import android.app.AlertDialog; 25import android.app.Dialog; 26import android.app.DialogFragment; 27import android.content.ContentValues; 28import android.content.Context; 29import android.content.DialogInterface; 30import android.net.Uri; 31import android.os.Bundle; 32import android.text.Editable; 33import android.text.TextWatcher; 34import android.view.LayoutInflater; 35import android.view.View; 36import android.widget.EditText; 37 38/** 39 * Dialog to edit the text of a given or new quick response 40 */ 41public class EditQuickResponseDialog extends DialogFragment { 42 private EditText mQuickResponseEditText; 43 private AlertDialog mDialog; 44 45 private static final String QUICK_RESPONSE_STRING = "quick_response_edited_string"; 46 private static final String QUICK_RESPONSE_CONTENT_URI = "quick_response_content_uri"; 47 private static final String QUICK_RESPONSE_CREATE = "quick_response_create"; 48 private static final String ACCOUNT_ID = "accountId"; 49 50 // Public no-args constructor needed for fragment re-instantiation 51 public EditQuickResponseDialog() {} 52 53 /** 54 * Creates a new dialog to edit an existing QuickResponse or create a new 55 * one. 56 * 57 * @param baseUri - The content Uri QuickResponse which the user is editing; 58 * @param accountId - The accountId for the account which holds this QuickResponse 59 * @param create - True if this is a new QuickResponse 60 */ 61 public static EditQuickResponseDialog newInstance(String text, 62 Uri baseUri, long accountId, boolean create) { 63 final EditQuickResponseDialog dialog = new EditQuickResponseDialog(); 64 65 Bundle args = new Bundle(4); 66 args.putString(QUICK_RESPONSE_STRING, text); 67 args.putLong(ACCOUNT_ID, accountId); 68 args.putParcelable(QUICK_RESPONSE_CONTENT_URI, baseUri); 69 args.putBoolean(QUICK_RESPONSE_CREATE, create); 70 71 dialog.setArguments(args); 72 return dialog; 73 } 74 75 @Override 76 public Dialog onCreateDialog(Bundle savedInstanceState) { 77 final long accountId = getArguments().getLong(ACCOUNT_ID); 78 final Uri uri = getArguments().getParcelable(QUICK_RESPONSE_CONTENT_URI); 79 final boolean create = getArguments().getBoolean(QUICK_RESPONSE_CREATE); 80 81 String quickResponseSavedString = null; 82 if (savedInstanceState != null) { 83 quickResponseSavedString = 84 savedInstanceState.getString(QUICK_RESPONSE_STRING); 85 } 86 if (quickResponseSavedString == null) { 87 quickResponseSavedString = getArguments().getString(QUICK_RESPONSE_STRING); 88 } 89 90 final View wrapper = LayoutInflater.from(getActivity()) 91 .inflate(R.layout.quick_response_edit_dialog, null); 92 mQuickResponseEditText = (EditText) wrapper.findViewById(R.id.quick_response_text); 93 94 if (quickResponseSavedString != null) { 95 mQuickResponseEditText.setText(quickResponseSavedString); 96 } 97 98 mQuickResponseEditText.setSelection(mQuickResponseEditText.length()); 99 mQuickResponseEditText.addTextChangedListener(new TextWatcher() { 100 @Override 101 public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 102 103 @Override 104 public void onTextChanged(CharSequence s, int start, int before, int count) {} 105 106 @Override 107 public void afterTextChanged(Editable s) { 108 mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(s.length() > 0); 109 } 110 }); 111 112 DialogInterface.OnClickListener saveClickListener = 113 new DialogInterface.OnClickListener() { 114 @Override 115 public void onClick(DialogInterface dialog, int which) { 116 final String text = mQuickResponseEditText.getText().toString(); 117 final ContentValues values = new ContentValues(2); 118 values.put(QuickResponseColumns.ACCOUNT_KEY, accountId); 119 values.put(QuickResponseColumns.TEXT, text); 120 121 if (create) { 122 getActivity().getContentResolver().insert(uri, values); 123 } else { 124 getActivity().getContentResolver().update(uri, values, null, null); 125 } 126 } 127 }; 128 DialogInterface.OnClickListener deleteClickListener = 129 new DialogInterface.OnClickListener() { 130 @Override 131 public void onClick(DialogInterface dialog, int which) { 132 getActivity().getContentResolver().delete(uri, null, null); 133 } 134 }; 135 136 final AlertDialog.Builder b = new AlertDialog.Builder(getActivity()); 137 b.setTitle(getResources().getString(R.string.edit_quick_response_dialog)) 138 .setView(wrapper) 139 .setNegativeButton(R.string.cancel_action, null) 140 .setPositiveButton(R.string.save_action, saveClickListener); 141 if (!create) { 142 b.setNeutralButton(R.string.delete, deleteClickListener); 143 } 144 mDialog = b.create(); 145 return mDialog; 146 } 147 148 @Override 149 public void onResume() { 150 super.onResume(); 151 if (mQuickResponseEditText.length() == 0) { 152 mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); 153 } 154 } 155 156 // Saves contents during orientation change 157 @Override 158 public void onSaveInstanceState(Bundle outState) { 159 super.onSaveInstanceState(outState); 160 outState.putString( 161 QUICK_RESPONSE_STRING, mQuickResponseEditText.getText().toString()); 162 } 163} 164