blob: 7c8891b52f8dd34fcb7bbb1acbd2623e72561884 [file] [log] [blame]
Jorge Lugo5a3888f2011-06-01 10:09:26 -07001/*
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;
Tony Mantlerc6953b72013-08-14 11:05:19 -070020import com.android.mail.providers.UIProvider;
Jorge Lugo5a3888f2011-06-01 10:09:26 -070021
22import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.DialogFragment;
25import android.content.ContentValues;
Jorge Lugo5a3888f2011-06-01 10:09:26 -070026import android.content.DialogInterface;
Tony Mantler6d245342013-08-09 16:38:23 -070027import android.net.Uri;
Jorge Lugo5a3888f2011-06-01 10:09:26 -070028import android.os.Bundle;
Jorge Lugo7920f582011-07-13 20:58:01 -070029import android.text.Editable;
30import android.text.TextWatcher;
Marc Blankf4192872012-06-28 10:40:46 -070031import android.view.LayoutInflater;
32import android.view.View;
Jorge Lugo5a3888f2011-06-01 10:09:26 -070033import android.widget.EditText;
34
35/**
36 * Dialog to edit the text of a given or new quick response
37 */
Tony Mantler05273f82013-08-07 16:37:15 -070038public class EditQuickResponseDialog extends DialogFragment {
Jorge Lugo5a3888f2011-06-01 10:09:26 -070039 private EditText mQuickResponseEditText;
Jorge Lugo7920f582011-07-13 20:58:01 -070040 private AlertDialog mDialog;
Jorge Lugo5a3888f2011-06-01 10:09:26 -070041
Tony Mantler6d245342013-08-09 16:38:23 -070042 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";
Jorge Lugo5a3888f2011-06-01 10:09:26 -070045
Paul Westbrook7985b432013-08-13 16:43:34 -070046 // Public no-args constructor needed for fragment re-instantiation
47 public EditQuickResponseDialog() {}
48
Jorge Lugo5a3888f2011-06-01 10:09:26 -070049 /**
50 * Creates a new dialog to edit an existing QuickResponse or create a new
51 * one.
52 *
Tony Mantlerc6953b72013-08-14 11:05:19 -070053 * @param baseUri - The content Uri QuickResponse which the user is editing
54 * or the content Uri for creating a new QuickResponse
Tony Mantler6d245342013-08-09 16:38:23 -070055 * @param create - True if this is a new QuickResponse
Jorge Lugo5a3888f2011-06-01 10:09:26 -070056 */
Tony Mantler6d245342013-08-09 16:38:23 -070057 public static EditQuickResponseDialog newInstance(String text,
Tony Mantlerc6953b72013-08-14 11:05:19 -070058 Uri baseUri, boolean create) {
Jorge Lugo5a3888f2011-06-01 10:09:26 -070059 final EditQuickResponseDialog dialog = new EditQuickResponseDialog();
60
Tony Mantler6d245342013-08-09 16:38:23 -070061 Bundle args = new Bundle(4);
62 args.putString(QUICK_RESPONSE_STRING, text);
Tony Mantler6d245342013-08-09 16:38:23 -070063 args.putParcelable(QUICK_RESPONSE_CONTENT_URI, baseUri);
64 args.putBoolean(QUICK_RESPONSE_CREATE, create);
Jorge Lugo5a3888f2011-06-01 10:09:26 -070065
66 dialog.setArguments(args);
67 return dialog;
68 }
69
70 @Override
71 public Dialog onCreateDialog(Bundle savedInstanceState) {
Tony Mantler6d245342013-08-09 16:38:23 -070072 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())
Marc Blankf4192872012-06-28 10:40:46 -070085 .inflate(R.layout.quick_response_edit_dialog, null);
86 mQuickResponseEditText = (EditText) wrapper.findViewById(R.id.quick_response_text);
Tony Mantler6d245342013-08-09 16:38:23 -070087
88 if (quickResponseSavedString != null) {
89 mQuickResponseEditText.setText(quickResponseSavedString);
Jorge Lugo5a3888f2011-06-01 10:09:26 -070090 }
Tony Mantler6d245342013-08-09 16:38:23 -070091
Jorge Lugo5a3888f2011-06-01 10:09:26 -070092 mQuickResponseEditText.setSelection(mQuickResponseEditText.length());
Tony Mantler05273f82013-08-07 16:37:15 -070093 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) {
Tony Mantler05273f82013-08-07 16:37:15 -0700110 final String text = mQuickResponseEditText.getText().toString();
Tony Mantlerc6953b72013-08-14 11:05:19 -0700111 final ContentValues values = new ContentValues(1);
112 values.put(UIProvider.QuickResponseColumns.TEXT, text);
Tony Mantler6d245342013-08-09 16:38:23 -0700113
114 if (create) {
115 getActivity().getContentResolver().insert(uri, values);
116 } else {
117 getActivity().getContentResolver().update(uri, values, null, null);
Tony Mantler05273f82013-08-07 16:37:15 -0700118 }
Tony Mantler05273f82013-08-07 16:37:15 -0700119 }
120 };
121 DialogInterface.OnClickListener deleteClickListener =
122 new DialogInterface.OnClickListener() {
123 @Override
124 public void onClick(DialogInterface dialog, int which) {
Tony Mantler6d245342013-08-09 16:38:23 -0700125 getActivity().getContentResolver().delete(uri, null, null);
Tony Mantler05273f82013-08-07 16:37:15 -0700126 }
127 };
Jorge Lugo5a3888f2011-06-01 10:09:26 -0700128
Tony Mantler6d245342013-08-09 16:38:23 -0700129 final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
Jorge Lugo5a3888f2011-06-01 10:09:26 -0700130 b.setTitle(getResources().getString(R.string.edit_quick_response_dialog))
Marc Blankf4192872012-06-28 10:40:46 -0700131 .setView(wrapper)
Tony Mantler05273f82013-08-07 16:37:15 -0700132 .setNegativeButton(R.string.cancel_action, null)
Tony Mantler6d245342013-08-09 16:38:23 -0700133 .setPositiveButton(R.string.save_action, saveClickListener);
134 if (!create) {
135 b.setNeutralButton(R.string.delete, deleteClickListener);
136 }
Jorge Lugo7920f582011-07-13 20:58:01 -0700137 mDialog = b.create();
138 return mDialog;
Jorge Lugo5a3888f2011-06-01 10:09:26 -0700139 }
140
Jorge Lugo7920f582011-07-13 20:58:01 -0700141 @Override
142 public void onResume() {
143 super.onResume();
Jorge Lugo7920f582011-07-13 20:58:01 -0700144 if (mQuickResponseEditText.length() == 0) {
145 mDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
146 }
147 }
148
Jorge Lugo5a3888f2011-06-01 10:09:26 -0700149 // Saves contents during orientation change
150 @Override
151 public void onSaveInstanceState(Bundle outState) {
152 super.onSaveInstanceState(outState);
153 outState.putString(
Tony Mantler6d245342013-08-09 16:38:23 -0700154 QUICK_RESPONSE_STRING, mQuickResponseEditText.getText().toString());
Jorge Lugo5a3888f2011-06-01 10:09:26 -0700155 }
Jorge Lugo5a3888f2011-06-01 10:09:26 -0700156}