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