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.ActivityNotFoundException;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
24 import android.content.pm.PackageManager;
25 import android.os.Bundle;
26 import android.preference.Preference;
27 import android.preference.Preference.OnPreferenceClickListener;
28 import android.preference.PreferenceCategory;
29 import android.preference.PreferenceFragment;
30 import android.preference.PreferenceScreen;
31 import android.text.TextUtils;
32 import android.view.MenuItem;
33 
34 import androidx.appcompat.mms.MmsManager;
35 import androidx.core.app.NavUtils;
36 
37 import com.android.messaging.Factory;
38 import com.android.messaging.R;
39 import com.android.messaging.datamodel.ParticipantRefresh;
40 import com.android.messaging.datamodel.data.ParticipantData;
41 import com.android.messaging.sms.ApnDatabase;
42 import com.android.messaging.sms.MmsConfig;
43 import com.android.messaging.sms.MmsUtils;
44 import com.android.messaging.ui.BugleActionBarActivity;
45 import com.android.messaging.ui.UIIntents;
46 import com.android.messaging.util.Assert;
47 import com.android.messaging.util.BuglePrefs;
48 import com.android.messaging.util.LogUtil;
49 import com.android.messaging.util.PhoneUtils;
50 
51 public class PerSubscriptionSettingsActivity extends BugleActionBarActivity {
52     @Override
onCreate(final Bundle savedInstanceState)53     protected void onCreate(final Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55 
56         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
57         final String title = getIntent().getStringExtra(
58                 UIIntents.UI_INTENT_EXTRA_PER_SUBSCRIPTION_SETTING_TITLE);
59         if (!TextUtils.isEmpty(title)) {
60             getSupportActionBar().setTitle(title);
61         } else {
62             // This will fall back to the default title, i.e. "Messaging settings," so No-op.
63         }
64 
65         final FragmentTransaction ft = getFragmentManager().beginTransaction();
66         final PerSubscriptionSettingsFragment fragment = new PerSubscriptionSettingsFragment();
67         ft.replace(android.R.id.content, fragment);
68         ft.commit();
69     }
70 
71     @Override
onOptionsItemSelected(final MenuItem item)72     public boolean onOptionsItemSelected(final MenuItem item) {
73         switch (item.getItemId()) {
74         case android.R.id.home:
75             NavUtils.navigateUpFromSameTask(this);
76             return true;
77         }
78         return super.onOptionsItemSelected(item);
79     }
80 
81     public static class PerSubscriptionSettingsFragment extends PreferenceFragment
82             implements OnSharedPreferenceChangeListener {
83         private PhoneNumberPreference mPhoneNumberPreference;
84         private Preference mGroupMmsPreference;
85         private String mGroupMmsPrefKey;
86         private String mPhoneNumberKey;
87         private int mSubId;
88 
PerSubscriptionSettingsFragment()89         public PerSubscriptionSettingsFragment() {
90             // Required empty constructor
91         }
92 
93         @Override
onCreate(final Bundle savedInstanceState)94         public void onCreate(final Bundle savedInstanceState) {
95             super.onCreate(savedInstanceState);
96 
97             // Get sub id from launch intent
98             final Intent intent = getActivity().getIntent();
99             Assert.notNull(intent);
100             mSubId = (intent != null) ? intent.getIntExtra(UIIntents.UI_INTENT_EXTRA_SUB_ID,
101                     ParticipantData.DEFAULT_SELF_SUB_ID) : ParticipantData.DEFAULT_SELF_SUB_ID;
102 
103             final BuglePrefs subPrefs = Factory.get().getSubscriptionPrefs(mSubId);
104             getPreferenceManager().setSharedPreferencesName(subPrefs.getSharedPreferencesName());
105             addPreferencesFromResource(R.xml.preferences_per_subscription);
106 
107             mPhoneNumberKey = getString(R.string.mms_phone_number_pref_key);
108             mPhoneNumberPreference = (PhoneNumberPreference) findPreference(mPhoneNumberKey);
109             final PreferenceCategory advancedCategory = (PreferenceCategory)
110                     findPreference(getString(R.string.advanced_category_pref_key));
111             final PreferenceCategory mmsCategory = (PreferenceCategory)
112                     findPreference(getString(R.string.mms_messaging_category_pref_key));
113 
114             mPhoneNumberPreference.setDefaultPhoneNumber(
115                     PhoneUtils.get(mSubId).getCanonicalForSelf(false/*allowOverride*/), mSubId);
116 
117             mGroupMmsPrefKey = getString(R.string.group_mms_pref_key);
118             mGroupMmsPreference = findPreference(mGroupMmsPrefKey);
119             if (!MmsConfig.get(mSubId).getGroupMmsEnabled()) {
120                 // Always show group messaging setting even if the SIM has no number
121                 // If broadcast sms is selected, the SIM number is not needed
122                 // If group mms is selected, the phone number dialog will popup when message
123                 // is being sent, making sure we will have a self number for group mms.
124                 mmsCategory.removePreference(mGroupMmsPreference);
125             } else {
126                 mGroupMmsPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
127                     @Override
128                     public boolean onPreferenceClick(Preference pref) {
129                         GroupMmsSettingDialog.showDialog(getActivity(), mSubId);
130                         return true;
131                     }
132                 });
133                 updateGroupMmsPrefSummary();
134             }
135 
136             if (!MmsConfig.get(mSubId).getSMSDeliveryReportsEnabled()) {
137                 final Preference deliveryReportsPref = findPreference(
138                         getString(R.string.delivery_reports_pref_key));
139                 advancedCategory.removePreference(deliveryReportsPref);
140             }
141             final Preference wirelessAlertPref = findPreference(getString(
142                     R.string.wireless_alerts_key));
143             if (!isCellBroadcastAppLinkEnabled()) {
144                 advancedCategory.removePreference(wirelessAlertPref);
145             } else {
146                 wirelessAlertPref.setOnPreferenceClickListener(
147                         new Preference.OnPreferenceClickListener() {
148                             @Override
149                             public boolean onPreferenceClick(final Preference preference) {
150                                 try {
151                                     startActivity(UIIntents.get().getWirelessAlertsIntent());
152                                 } catch (final ActivityNotFoundException e) {
153                                     // Handle so we shouldn't crash if the wireless alerts
154                                     // implementation is broken.
155                                     LogUtil.e(LogUtil.BUGLE_TAG,
156                                             "Failed to launch wireless alerts activity", e);
157                                 }
158                                 return true;
159                             }
160                         });
161             }
162 
163             // Access Point Names (APNs)
164             final PreferenceScreen apnsScreen =
165                     (PreferenceScreen) findPreference(getString(R.string.sms_apns_key));
166 
167             if (!MmsManager.shouldUseLegacyMms()
168                     || (MmsUtils.useSystemApnTable() && !ApnDatabase.doesDatabaseExist())) {
169                 // 1) Remove the ability to edit the local APN prefs if it doesn't use legacy APIs.
170                 // 2) Don't remove the ability to edit the local APN prefs if this device lets us
171                 // access the system APN, but we can't find the MCC/MNC in the APN table and we
172                 // created the local APN table in case the MCC/MNC was in there. In other words,
173                 // if the local APN table exists, let the user edit it.
174                 advancedCategory.removePreference((Preference) apnsScreen);
175             } else {
176                 apnsScreen.setIntent(UIIntents.get()
177                         .getApnSettingsIntent(getPreferenceScreen().getContext(), mSubId));
178             }
179 
180             // We want to disable preferences if we are not the default app, but we do all of the
181             // above first so that the user sees the correct information on the screen
182             if (!PhoneUtils.getDefault().isDefaultSmsApp()) {
183                 mGroupMmsPreference.setEnabled(false);
184                 final Preference autoRetrieveMmsPreference =
185                         findPreference(getString(R.string.auto_retrieve_mms_pref_key));
186                 autoRetrieveMmsPreference.setEnabled(false);
187                 final Preference deliveryReportsPreference =
188                         findPreference(getString(R.string.delivery_reports_pref_key));
189                 if (deliveryReportsPreference != null) {
190                     deliveryReportsPreference.setEnabled(false);
191                 }
192             }
193 
194             if (advancedCategory.getPreferenceCount() == 0) {
195                 getPreferenceScreen().removePreference(advancedCategory);
196             }
197         }
198 
isCellBroadcastAppLinkEnabled()199         private boolean isCellBroadcastAppLinkEnabled() {
200             if (!MmsConfig.get(mSubId).getShowCellBroadcast()) {
201                 return false;
202             }
203             try {
204                 final PackageManager pm = getActivity().getPackageManager();
205                 return pm.getApplicationEnabledSetting(UIIntents.CMAS_COMPONENT)
206                         != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
207             } catch (final IllegalArgumentException ignored) {
208                 // CMAS app not installed.
209             }
210             return false;
211         }
212 
updateGroupMmsPrefSummary()213         private void updateGroupMmsPrefSummary() {
214             final boolean groupMmsEnabled = getPreferenceScreen().getSharedPreferences().getBoolean(
215                     mGroupMmsPrefKey, getResources().getBoolean(R.bool.group_mms_pref_default));
216             mGroupMmsPreference.setSummary(groupMmsEnabled ?
217                     R.string.enable_group_mms : R.string.disable_group_mms);
218         }
219 
220         @Override
onResume()221         public void onResume() {
222             super.onResume();
223             getPreferenceScreen().getSharedPreferences()
224                     .registerOnSharedPreferenceChangeListener(this);
225         }
226 
227         @Override
onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key)228         public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences,
229                 final String key) {
230             if (key.equals(mGroupMmsPrefKey)) {
231                 updateGroupMmsPrefSummary();
232             } else if (key.equals(mPhoneNumberKey)) {
233                 // Save the changed phone number in preferences specific to the sub id
234                 final String newPhoneNumber = mPhoneNumberPreference.getText();
235                 final BuglePrefs subPrefs = BuglePrefs.getSubscriptionPrefs(mSubId);
236                 if (TextUtils.isEmpty(newPhoneNumber)) {
237                     subPrefs.remove(mPhoneNumberKey);
238                 } else {
239                     subPrefs.putString(getString(R.string.mms_phone_number_pref_key),
240                             newPhoneNumber);
241                 }
242                 // Update the self participants so the new phone number will be reflected
243                 // everywhere in the UI.
244                 ParticipantRefresh.refreshSelfParticipants();
245             }
246         }
247 
248         @Override
onPause()249         public void onPause() {
250             super.onPause();
251             getPreferenceScreen().getSharedPreferences()
252                     .unregisterOnSharedPreferenceChangeListener(this);
253         }
254     }
255 }
256