1 /*
2  * Copyright (C) 2012 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.android.calendar;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.preference.EditTextPreference;
22 import android.preference.Preference;
23 import android.preference.Preference.OnPreferenceChangeListener;
24 import android.preference.PreferenceFragment;
25 import android.preference.PreferenceScreen;
26 import android.util.Log;
27 
28 import java.util.Arrays;
29 
30 /**
31  * Fragment to facilitate editing of quick responses when emailing guests
32  *
33  */
34 public class QuickResponseSettings extends PreferenceFragment implements OnPreferenceChangeListener {
35     private static final String TAG = "QuickResponseSettings";
36 
37     EditTextPreference[] mEditTextPrefs;
38     String[] mResponses;
39 
40     @Override
onCreate(Bundle savedInstanceState)41     public void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43 
44         PreferenceScreen ps = getPreferenceManager().createPreferenceScreen(getActivity());
45         ps.setTitle(R.string.quick_response_settings_title);
46 
47         mResponses = Utils.getQuickResponses(getActivity());
48 
49         if (mResponses != null) {
50             mEditTextPrefs = new EditTextPreference[mResponses.length];
51 
52             Arrays.sort(mResponses);
53             int i = 0;
54             for (String response : mResponses) {
55                 EditTextPreference et = new EditTextPreference(getActivity());
56                 et.setDialogTitle(R.string.quick_response_settings_edit_title);
57                 et.setTitle(response); // Display Text
58                 et.setText(response); // Value to edit
59                 et.setOnPreferenceChangeListener(this);
60                 mEditTextPrefs[i++] = et;
61                 ps.addPreference(et);
62             }
63         } else {
64             Log.wtf(TAG, "No responses found");
65         }
66         setPreferenceScreen(ps);
67     }
68 
69     @Override
onAttach(Activity activity)70     public void onAttach(Activity activity) {
71         super.onAttach(activity);
72         ((CalendarSettingsActivity) activity).hideMenuButtons();
73     }
74 
75     @Override
onResume()76     public void onResume() {
77         super.onResume();
78         CalendarSettingsActivity activity = (CalendarSettingsActivity) getActivity();
79         if (!activity.isMultiPane()) {
80             activity.setTitle(R.string.quick_response_settings_title);
81         }
82     }
83 
84     // Implements OnPreferenceChangeListener
85     @Override
onPreferenceChange(Preference preference, Object newValue)86     public boolean onPreferenceChange(Preference preference, Object newValue) {
87         for (int i = 0; i < mEditTextPrefs.length; i++) {
88             if (mEditTextPrefs[i].compareTo(preference) == 0) {
89                 if (!mResponses[i].equals(newValue)) {
90                     mResponses[i] = (String) newValue;
91                     mEditTextPrefs[i].setTitle(mResponses[i]);
92                     mEditTextPrefs[i].setText(mResponses[i]);
93                     Utils.setSharedPreference(getActivity(), Utils.KEY_QUICK_RESPONSES, mResponses);
94                 }
95                 return true;
96             }
97         }
98         return false;
99     }
100 }
101