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.mail.providers.UIProvider;
21 
22 import android.app.AlertDialog;
23 import android.app.Dialog;
24 import android.app.DialogFragment;
25 import android.content.ContentValues;
26 import android.content.DialogInterface;
27 import android.net.Uri;
28 import android.os.Bundle;
29 import android.text.Editable;
30 import android.text.TextWatcher;
31 import android.view.LayoutInflater;
32 import android.view.View;
33 import android.widget.EditText;
34 
35 /**
36  * Dialog to edit the text of a given or new quick response
37  */
38 public class EditQuickResponseDialog extends DialogFragment {
39     private EditText mQuickResponseEditText;
40     private AlertDialog mDialog;
41 
42     private static final String QUICK_RESPONSE_STRING = "quick_response_edited_string";
43     private static final String QUICK_RESPONSE_CONTENT_URI = "quick_response_content_uri";
44     private static final String QUICK_RESPONSE_CREATE = "quick_response_create";
45 
46     // Public no-args constructor needed for fragment re-instantiation
EditQuickResponseDialog()47     public EditQuickResponseDialog() {}
48 
49     /**
50      * Creates a new dialog to edit an existing QuickResponse or create a new
51      * one.
52      *
53      * @param baseUri - The content Uri QuickResponse which the user is editing
54      *                or the content Uri for creating a new QuickResponse
55      * @param create - True if this is a new QuickResponse
56      */
newInstance(String text, Uri baseUri, boolean create)57     public static EditQuickResponseDialog newInstance(String text,
58             Uri baseUri, boolean create) {
59         final EditQuickResponseDialog dialog = new EditQuickResponseDialog();
60 
61         Bundle args = new Bundle(4);
62         args.putString(QUICK_RESPONSE_STRING, text);
63         args.putParcelable(QUICK_RESPONSE_CONTENT_URI, baseUri);
64         args.putBoolean(QUICK_RESPONSE_CREATE, create);
65 
66         dialog.setArguments(args);
67         return dialog;
68     }
69 
70     @Override
onCreateDialog(Bundle savedInstanceState)71     public Dialog onCreateDialog(Bundle savedInstanceState) {
72         final Uri uri = getArguments().getParcelable(QUICK_RESPONSE_CONTENT_URI);
73         final boolean create = getArguments().getBoolean(QUICK_RESPONSE_CREATE);
74 
75         String quickResponseSavedString = null;
76         if (savedInstanceState != null) {
77             quickResponseSavedString =
78                     savedInstanceState.getString(QUICK_RESPONSE_STRING);
79         }
80         if (quickResponseSavedString == null) {
81             quickResponseSavedString = getArguments().getString(QUICK_RESPONSE_STRING);
82         }
83 
84         final View wrapper = LayoutInflater.from(getActivity())
85                 .inflate(R.layout.quick_response_edit_dialog, null);
86         mQuickResponseEditText = (EditText) wrapper.findViewById(R.id.quick_response_text);
87 
88         if (quickResponseSavedString != null) {
89             mQuickResponseEditText.setText(quickResponseSavedString);
90         }
91 
92         mQuickResponseEditText.setSelection(mQuickResponseEditText.length());
93         mQuickResponseEditText.addTextChangedListener(new TextWatcher() {
94             @Override
95             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
96 
97             @Override
98             public void onTextChanged(CharSequence s, int start, int before, int count) {}
99 
100             @Override
101             public void afterTextChanged(Editable s) {
102                 mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(s.length() > 0);
103             }
104         });
105 
106         DialogInterface.OnClickListener saveClickListener =
107                 new DialogInterface.OnClickListener() {
108                     @Override
109                     public void onClick(DialogInterface dialog, int which) {
110                         final String text = mQuickResponseEditText.getText().toString();
111                         final ContentValues values = new ContentValues(1);
112                         values.put(UIProvider.QuickResponseColumns.TEXT, text);
113 
114                         if (create) {
115                             getActivity().getContentResolver().insert(uri, values);
116                         } else {
117                             getActivity().getContentResolver().update(uri, values, null, null);
118                         }
119                     }
120                 };
121         DialogInterface.OnClickListener deleteClickListener =
122                 new DialogInterface.OnClickListener() {
123                     @Override
124                     public void onClick(DialogInterface dialog, int which) {
125                         getActivity().getContentResolver().delete(uri, null, null);
126                     }
127                 };
128 
129         final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
130         b.setTitle(getResources().getString(R.string.edit_quick_response_dialog))
131                 .setView(wrapper)
132                 .setNegativeButton(android.R.string.cancel, null)
133                 .setPositiveButton(R.string.save_action, saveClickListener);
134         if (!create) {
135             b.setNeutralButton(R.string.delete, deleteClickListener);
136         }
137         mDialog = b.create();
138         return mDialog;
139     }
140 
141     @Override
onResume()142     public void onResume() {
143         super.onResume();
144         if (mQuickResponseEditText.length() == 0) {
145             mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
146         }
147     }
148 
149     // Saves contents during orientation change
150     @Override
onSaveInstanceState(Bundle outState)151     public void onSaveInstanceState(Bundle outState) {
152         super.onSaveInstanceState(outState);
153         outState.putString(
154                 QUICK_RESPONSE_STRING, mQuickResponseEditText.getText().toString());
155     }
156 }
157