• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.email.activity.setup;
18 
19 import com.android.email.R;
20 import com.android.emailcommon.provider.QuickResponse;
21 import com.android.emailcommon.provider.EmailContent.QuickResponseColumns;
22 import com.android.emailcommon.utility.EmailAsyncTask;
23 
24 import android.app.AlertDialog;
25 import android.app.Dialog;
26 import android.app.DialogFragment;
27 import android.content.ContentValues;
28 import android.content.Context;
29 import android.content.DialogInterface;
30 import android.os.Bundle;
31 import android.text.Editable;
32 import android.text.TextWatcher;
33 import android.view.WindowManager;
34 import android.widget.EditText;
35 
36 /**
37  * Dialog to edit the text of a given or new quick response
38  */
39 public class EditQuickResponseDialog extends DialogFragment
40         implements DialogInterface.OnClickListener, TextWatcher {
41     private EditText mQuickResponseEditText;
42     private QuickResponse mQuickResponse;
43     private AlertDialog mDialog;
44 
45     private static final String QUICK_RESPONSE_EDITED_STRING = "quick_response_edited_string";
46     private static final String QUICK_RESPONSE = "quick_response";
47 
48     /**
49      * Creates a new dialog to edit an existing QuickResponse or create a new
50      * one.
51      *
52      * @param quickResponse - The QuickResponse fwhich the user is editing;
53      *        null if user is creating a new QuickResponse.
54      * @param accountId - The accountId for the account which holds this QuickResponse
55      */
newInstance( QuickResponse quickResponse, long accountId)56     public static EditQuickResponseDialog newInstance(
57             QuickResponse quickResponse, long accountId) {
58         final EditQuickResponseDialog dialog = new EditQuickResponseDialog();
59 
60         Bundle args = new Bundle();
61         args.putLong("accountId", accountId);
62         if (quickResponse != null) {
63             args.putParcelable(QUICK_RESPONSE, quickResponse);
64         }
65 
66         dialog.setArguments(args);
67         return dialog;
68     }
69 
70     @Override
onCreateDialog(Bundle savedInstanceState)71     public Dialog onCreateDialog(Bundle savedInstanceState) {
72         final Context context = getActivity();
73         mQuickResponse = (QuickResponse) getArguments().getParcelable(QUICK_RESPONSE);
74 
75         mQuickResponseEditText = new EditText(context);
76         if (savedInstanceState != null) {
77             String quickResponseSavedString =
78                     savedInstanceState.getString(QUICK_RESPONSE_EDITED_STRING);
79             if (quickResponseSavedString != null) {
80                 mQuickResponseEditText.setText(quickResponseSavedString);
81             }
82         } else if (mQuickResponse != null) {
83             mQuickResponseEditText.setText(mQuickResponse.toString());
84         }
85         mQuickResponseEditText.setSelection(mQuickResponseEditText.length());
86         mQuickResponseEditText.addTextChangedListener(this);
87 
88         final AlertDialog.Builder b = new AlertDialog.Builder(context);
89         b.setTitle(getResources().getString(R.string.edit_quick_response_dialog))
90                 .setView(mQuickResponseEditText)
91                 .setNegativeButton(R.string.cancel_action, this)
92                 .setPositiveButton(R.string.save_action, this);
93         mDialog = b.create();
94         return mDialog;
95     }
96 
97     @Override
onResume()98     public void onResume() {
99         super.onResume();
100         mDialog.getWindow()
101                 .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
102         if (mQuickResponseEditText.length() == 0) {
103             mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
104         }
105     }
106 
107     // implements TextWatcher
108     @Override
afterTextChanged(Editable s)109     public void afterTextChanged(Editable s) {
110             mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(s.length() > 0);
111     }
112 
113     // implements TextWatcher
114     @Override
beforeTextChanged(CharSequence s, int start, int count, int after)115     public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
116 
117     // implements TextWatcher
118     @Override
onTextChanged(CharSequence s, int start, int before, int count)119     public void onTextChanged(CharSequence s, int start, int before, int count) {}
120 
121     // Saves contents during orientation change
122     @Override
onSaveInstanceState(Bundle outState)123     public void onSaveInstanceState(Bundle outState) {
124         super.onSaveInstanceState(outState);
125         outState.putString(
126                 QUICK_RESPONSE_EDITED_STRING, mQuickResponseEditText.getText().toString());
127     }
128 
129     /**
130      * Implements DialogInterface.OnClickListener
131      */
132     @Override
onClick(DialogInterface dialog, int which)133     public void onClick(DialogInterface dialog, int which) {
134         switch (which) {
135             case DialogInterface.BUTTON_NEGATIVE:
136                 dialog.cancel();
137                 break;
138             case DialogInterface.BUTTON_POSITIVE:
139                 final long accountId = getArguments().getLong("accountId");
140                 final String text = mQuickResponseEditText.getText().toString();
141                 final Context context = getActivity();
142                 if (mQuickResponse == null) {
143                     mQuickResponse = new QuickResponse(accountId, text);
144                 }
145 
146                 // Insert the new QuickResponse into the database. Content watchers used to
147                 // update the ListView of QuickResponses upon insertion.
148                 EmailAsyncTask.runAsyncParallel(new Runnable() {
149                     @Override
150                     public void run() {
151                         if (!mQuickResponse.isSaved()) {
152                             mQuickResponse.save(context);
153                         } else {
154                             ContentValues values = new ContentValues();
155                             values.put(QuickResponseColumns.TEXT, text);
156                             mQuickResponse.update(context, values);
157                         }
158                     }
159 
160                 });
161                 break;
162         }
163     }
164 }
165