1 /*
2  * Copyright (C) 2015 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.messaging.ui.appsettings;
18 
19 import android.app.FragmentTransaction;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
23 import android.media.Ringtone;
24 import android.media.RingtoneManager;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.preference.Preference;
28 import android.preference.PreferenceFragment;
29 import android.preference.PreferenceScreen;
30 import android.preference.RingtonePreference;
31 import android.preference.TwoStatePreference;
32 import android.provider.Settings;
33 import android.support.v4.app.NavUtils;
34 import android.text.TextUtils;
35 import android.view.Menu;
36 import android.view.MenuItem;
37 
38 import com.android.messaging.R;
39 import com.android.messaging.ui.BugleActionBarActivity;
40 import com.android.messaging.ui.LicenseActivity;
41 import com.android.messaging.ui.UIIntents;
42 import com.android.messaging.util.BuglePrefs;
43 import com.android.messaging.util.DebugUtils;
44 import com.android.messaging.util.OsUtil;
45 import com.android.messaging.util.PhoneUtils;
46 
47 public class ApplicationSettingsActivity extends BugleActionBarActivity {
48     @Override
onCreate(final Bundle savedInstanceState)49     protected void onCreate(final Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51 
52         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
53         final boolean topLevel = getIntent().getBooleanExtra(
54                 UIIntents.UI_INTENT_EXTRA_TOP_LEVEL_SETTINGS, false);
55         if (topLevel) {
56             getSupportActionBar().setTitle(getString(R.string.settings_activity_title));
57         }
58 
59         FragmentTransaction ft = getFragmentManager().beginTransaction();
60         ft.replace(android.R.id.content, new ApplicationSettingsFragment());
61         ft.commit();
62     }
63 
64     @Override
onCreateOptionsMenu(Menu menu)65     public boolean onCreateOptionsMenu(Menu menu) {
66         if (super.onCreateOptionsMenu(menu)) {
67             return true;
68         }
69         getMenuInflater().inflate(R.menu.settings_menu, menu);
70         return true;
71     }
72 
73     @Override
onOptionsItemSelected(final MenuItem item)74     public boolean onOptionsItemSelected(final MenuItem item) {
75         switch (item.getItemId()) {
76         case android.R.id.home:
77             NavUtils.navigateUpFromSameTask(this);
78             return true;
79         case R.id.action_license:
80             final Intent intent = new Intent(this, LicenseActivity.class);
81             startActivity(intent);
82             return true;
83         }
84         return super.onOptionsItemSelected(item);
85     }
86 
87     public static class ApplicationSettingsFragment extends PreferenceFragment implements
88             OnSharedPreferenceChangeListener {
89 
90         private String mNotificationsEnabledPreferenceKey;
91         private TwoStatePreference mNotificationsEnabledPreference;
92         private String mRingtonePreferenceKey;
93         private RingtonePreference mRingtonePreference;
94         private Preference mVibratePreference;
95         private String mSmsDisabledPrefKey;
96         private Preference mSmsDisabledPreference;
97         private String mSmsEnabledPrefKey;
98         private Preference mSmsEnabledPreference;
99         private boolean mIsSmsPreferenceClicked;
100 
ApplicationSettingsFragment()101         public ApplicationSettingsFragment() {
102             // Required empty constructor
103         }
104 
105         @Override
onCreate(final Bundle savedInstanceState)106         public void onCreate(final Bundle savedInstanceState) {
107             super.onCreate(savedInstanceState);
108 
109             getPreferenceManager().setSharedPreferencesName(BuglePrefs.SHARED_PREFERENCES_NAME);
110             addPreferencesFromResource(R.xml.preferences_application);
111 
112             mNotificationsEnabledPreferenceKey =
113                     getString(R.string.notifications_enabled_pref_key);
114             mNotificationsEnabledPreference = (TwoStatePreference) findPreference(
115                     mNotificationsEnabledPreferenceKey);
116             mRingtonePreferenceKey = getString(R.string.notification_sound_pref_key);
117             mRingtonePreference = (RingtonePreference) findPreference(mRingtonePreferenceKey);
118             mVibratePreference = findPreference(
119                     getString(R.string.notification_vibration_pref_key));
120             mSmsDisabledPrefKey = getString(R.string.sms_disabled_pref_key);
121             mSmsDisabledPreference = findPreference(mSmsDisabledPrefKey);
122             mSmsEnabledPrefKey = getString(R.string.sms_enabled_pref_key);
123             mSmsEnabledPreference = findPreference(mSmsEnabledPrefKey);
124             mIsSmsPreferenceClicked = false;
125 
126             final SharedPreferences prefs = getPreferenceScreen().getSharedPreferences();
127             updateSoundSummary(prefs);
128 
129             if (!DebugUtils.isDebugEnabled()) {
130                 final Preference debugCategory = findPreference(getString(
131                         R.string.debug_pref_key));
132                 getPreferenceScreen().removePreference(debugCategory);
133             }
134 
135             final PreferenceScreen advancedScreen = (PreferenceScreen) findPreference(
136                     getString(R.string.advanced_pref_key));
137             final boolean topLevel = getActivity().getIntent().getBooleanExtra(
138                     UIIntents.UI_INTENT_EXTRA_TOP_LEVEL_SETTINGS, false);
139             if (topLevel) {
140                 advancedScreen.setIntent(UIIntents.get()
141                         .getAdvancedSettingsIntent(getPreferenceScreen().getContext()));
142             } else {
143                 // Hide the Advanced settings screen if this is not top-level; these are shown at
144                 // the parent SettingsActivity.
145                 getPreferenceScreen().removePreference(advancedScreen);
146             }
147         }
148 
149         @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)150         public boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen,
151                 Preference preference) {
152             if (preference.getKey() ==  mSmsDisabledPrefKey ||
153                     preference.getKey() == mSmsEnabledPrefKey) {
154                 mIsSmsPreferenceClicked = true;
155             }
156             return super.onPreferenceTreeClick(preferenceScreen, preference);
157         }
158 
updateSoundSummary(final SharedPreferences sharedPreferences)159         private void updateSoundSummary(final SharedPreferences sharedPreferences) {
160             // The silent ringtone just returns an empty string
161             String ringtoneName = mRingtonePreference.getContext().getString(
162                     R.string.silent_ringtone);
163 
164             String ringtoneString = sharedPreferences.getString(mRingtonePreferenceKey, null);
165 
166             // Bootstrap the default setting in the preferences so that we have a valid selection
167             // in the dialog the first time that the user opens it.
168             if (ringtoneString == null) {
169                 ringtoneString = Settings.System.DEFAULT_NOTIFICATION_URI.toString();
170                 final SharedPreferences.Editor editor = sharedPreferences.edit();
171                 editor.putString(mRingtonePreferenceKey, ringtoneString);
172                 editor.apply();
173             }
174 
175             if (!TextUtils.isEmpty(ringtoneString)) {
176                 final Uri ringtoneUri = Uri.parse(ringtoneString);
177                 final Ringtone tone = RingtoneManager.getRingtone(mRingtonePreference.getContext(),
178                         ringtoneUri);
179 
180                 if (tone != null) {
181                     ringtoneName = tone.getTitle(mRingtonePreference.getContext());
182                 }
183             }
184 
185             mRingtonePreference.setSummary(ringtoneName);
186         }
187 
updateSmsEnabledPreferences()188         private void updateSmsEnabledPreferences() {
189             if (!OsUtil.isAtLeastKLP()) {
190                 getPreferenceScreen().removePreference(mSmsDisabledPreference);
191                 getPreferenceScreen().removePreference(mSmsEnabledPreference);
192             } else {
193                 final String defaultSmsAppLabel = getString(R.string.default_sms_app,
194                         PhoneUtils.getDefault().getDefaultSmsAppLabel());
195                 boolean isSmsEnabledBeforeState;
196                 boolean isSmsEnabledCurrentState;
197                 if (PhoneUtils.getDefault().isDefaultSmsApp()) {
198                     if (getPreferenceScreen().findPreference(mSmsEnabledPrefKey) == null) {
199                         getPreferenceScreen().addPreference(mSmsEnabledPreference);
200                         isSmsEnabledBeforeState = false;
201                     } else {
202                         isSmsEnabledBeforeState = true;
203                     }
204                     isSmsEnabledCurrentState = true;
205                     getPreferenceScreen().removePreference(mSmsDisabledPreference);
206                     mSmsEnabledPreference.setSummary(defaultSmsAppLabel);
207                 } else {
208                     if (getPreferenceScreen().findPreference(mSmsDisabledPrefKey) == null) {
209                         getPreferenceScreen().addPreference(mSmsDisabledPreference);
210                         isSmsEnabledBeforeState = true;
211                     } else {
212                         isSmsEnabledBeforeState = false;
213                     }
214                     isSmsEnabledCurrentState = false;
215                     getPreferenceScreen().removePreference(mSmsEnabledPreference);
216                     mSmsDisabledPreference.setSummary(defaultSmsAppLabel);
217                 }
218                 updateNotificationsPreferences();
219             }
220             mIsSmsPreferenceClicked = false;
221         }
222 
updateNotificationsPreferences()223         private void updateNotificationsPreferences() {
224             final boolean canNotify = !OsUtil.isAtLeastKLP()
225                     || PhoneUtils.getDefault().isDefaultSmsApp();
226             mNotificationsEnabledPreference.setEnabled(canNotify);
227         }
228 
229         @Override
onStart()230         public void onStart() {
231             super.onStart();
232             // We do this on start rather than on resume because the sound picker is in a
233             // separate activity.
234             getPreferenceScreen().getSharedPreferences()
235                     .registerOnSharedPreferenceChangeListener(this);
236         }
237 
238         @Override
onResume()239         public void onResume() {
240             super.onResume();
241             updateSmsEnabledPreferences();
242             updateNotificationsPreferences();
243         }
244 
245         @Override
onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key)246         public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences,
247                 final String key) {
248             if (key.equals(mNotificationsEnabledPreferenceKey)) {
249                 updateNotificationsPreferences();
250             } else if (key.equals(mRingtonePreferenceKey)) {
251                 updateSoundSummary(sharedPreferences);
252             }
253         }
254 
255         @Override
onStop()256         public void onStop() {
257             super.onStop();
258             getPreferenceScreen().getSharedPreferences()
259                     .unregisterOnSharedPreferenceChangeListener(this);
260         }
261     }
262 }
263