1 /* 2 * Copyright (C) 2016 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.settings.applications.specialaccess.premiumsms; 18 19 import android.annotation.Nullable; 20 import android.app.Application; 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.telephony.SmsManager; 25 import android.view.View; 26 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.DropDownPreference; 29 import androidx.preference.Preference; 30 import androidx.preference.Preference.OnPreferenceChangeListener; 31 import androidx.preference.PreferenceScreen; 32 import androidx.preference.PreferenceViewHolder; 33 34 import com.android.settings.R; 35 import com.android.settings.applications.AppStateBaseBridge.Callback; 36 import com.android.settings.applications.AppStateSmsPremBridge; 37 import com.android.settings.applications.AppStateSmsPremBridge.SmsState; 38 import com.android.settings.overlay.FeatureFactory; 39 import com.android.settings.search.BaseSearchIndexProvider; 40 import com.android.settings.widget.EmptyTextSettings; 41 import com.android.settingslib.applications.ApplicationsState; 42 import com.android.settingslib.applications.ApplicationsState.AppEntry; 43 import com.android.settingslib.applications.ApplicationsState.Callbacks; 44 import com.android.settingslib.applications.ApplicationsState.Session; 45 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 46 import com.android.settingslib.search.SearchIndexable; 47 import com.android.settingslib.widget.FooterPreference; 48 49 import java.util.ArrayList; 50 51 @SearchIndexable 52 public class PremiumSmsAccess extends EmptyTextSettings 53 implements Callback, Callbacks, OnPreferenceChangeListener { 54 55 private ApplicationsState mApplicationsState; 56 private AppStateSmsPremBridge mSmsBackend; 57 private Session mSession; 58 59 @Override onCreate(Bundle icicle)60 public void onCreate(Bundle icicle) { 61 super.onCreate(icicle); 62 mApplicationsState = ApplicationsState.getInstance((Application) 63 getContext().getApplicationContext()); 64 mSession = mApplicationsState.newSession(this, getSettingsLifecycle()); 65 mSmsBackend = new AppStateSmsPremBridge(getContext(), mApplicationsState, this); 66 } 67 68 @Override onViewCreated(View view, @Nullable Bundle savedInstanceState)69 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 70 super.onViewCreated(view, savedInstanceState); 71 setLoading(true, false); 72 } 73 74 @Override onResume()75 public void onResume() { 76 super.onResume(); 77 mSmsBackend.resume(); 78 } 79 80 @Override onPause()81 public void onPause() { 82 mSmsBackend.pause(); 83 super.onPause(); 84 } 85 86 @Override onDestroy()87 public void onDestroy() { 88 mSmsBackend.release(); 89 super.onDestroy(); 90 } 91 92 @Override getPreferenceScreenResId()93 protected int getPreferenceScreenResId() { 94 return R.xml.premium_sms_settings; 95 } 96 97 @Override getMetricsCategory()98 public int getMetricsCategory() { 99 return SettingsEnums.PREMIUM_SMS_ACCESS; 100 } 101 102 @Override onPreferenceChange(Preference preference, Object newValue)103 public boolean onPreferenceChange(Preference preference, Object newValue) { 104 PremiumSmsPreference pref = (PremiumSmsPreference) preference; 105 int smsState = Integer.parseInt((String) newValue); 106 logSpecialPermissionChange(smsState, pref.mAppEntry.info.packageName); 107 mSmsBackend.setSmsState(pref.mAppEntry.info.packageName, smsState); 108 return true; 109 } 110 111 @VisibleForTesting logSpecialPermissionChange(int smsState, String packageName)112 void logSpecialPermissionChange(int smsState, String packageName) { 113 int category = SmsManager.PREMIUM_SMS_CONSENT_UNKNOWN; 114 switch (smsState) { 115 case SmsManager.PREMIUM_SMS_CONSENT_ASK_USER: 116 category = SettingsEnums.APP_SPECIAL_PERMISSION_PREMIUM_SMS_ASK; 117 break; 118 case SmsManager.PREMIUM_SMS_CONSENT_NEVER_ALLOW: 119 category = SettingsEnums.APP_SPECIAL_PERMISSION_PREMIUM_SMS_DENY; 120 break; 121 case SmsManager.PREMIUM_SMS_CONSENT_ALWAYS_ALLOW: 122 category = SettingsEnums. 123 APP_SPECIAL_PERMISSION_PREMIUM_SMS_ALWAYS_ALLOW; 124 break; 125 } 126 if (category != SmsManager.PREMIUM_SMS_CONSENT_UNKNOWN) { 127 // TODO(117860032): Category is wrong. It should be defined in SettingsEnums. 128 final MetricsFeatureProvider metricsFeatureProvider = 129 FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider(); 130 metricsFeatureProvider.action( 131 metricsFeatureProvider.getAttribution(getActivity()), 132 category, 133 getMetricsCategory(), 134 packageName, 135 smsState); 136 } 137 } 138 updatePrefs(ArrayList<AppEntry> apps)139 private void updatePrefs(ArrayList<AppEntry> apps) { 140 if (apps == null) return; 141 setEmptyText(R.string.premium_sms_none); 142 setLoading(false, true); 143 final PreferenceScreen screen = getPreferenceScreen(); 144 screen.removeAll(); 145 screen.setOrderingAsAdded(true); 146 147 for (int i = 0; i < apps.size(); i++) { 148 final PremiumSmsPreference smsPreference = 149 new PremiumSmsPreference(apps.get(i), getPrefContext()); 150 smsPreference.setOnPreferenceChangeListener(this); 151 screen.addPreference(smsPreference); 152 } 153 if (apps.size() != 0) { 154 FooterPreference footer = new FooterPreference(getPrefContext()); 155 footer.setTitle(R.string.premium_sms_warning); 156 screen.addPreference(footer); 157 } 158 } 159 update()160 private void update() { 161 updatePrefs(mSession.rebuild(AppStateSmsPremBridge.FILTER_APP_PREMIUM_SMS, 162 ApplicationsState.ALPHA_COMPARATOR)); 163 } 164 165 @Override onExtraInfoUpdated()166 public void onExtraInfoUpdated() { 167 update(); 168 } 169 170 @Override onRebuildComplete(ArrayList<AppEntry> apps)171 public void onRebuildComplete(ArrayList<AppEntry> apps) { 172 updatePrefs(apps); 173 } 174 175 @Override onRunningStateChanged(boolean running)176 public void onRunningStateChanged(boolean running) { 177 178 } 179 180 @Override onPackageListChanged()181 public void onPackageListChanged() { 182 183 } 184 185 @Override onPackageIconChanged()186 public void onPackageIconChanged() { 187 188 } 189 190 @Override onPackageSizeChanged(String packageName)191 public void onPackageSizeChanged(String packageName) { 192 193 } 194 195 @Override onAllSizesComputed()196 public void onAllSizesComputed() { 197 198 } 199 200 @Override onLauncherInfoChanged()201 public void onLauncherInfoChanged() { 202 203 } 204 205 @Override onLoadEntriesCompleted()206 public void onLoadEntriesCompleted() { 207 208 } 209 210 private class PremiumSmsPreference extends DropDownPreference { 211 private final AppEntry mAppEntry; 212 PremiumSmsPreference(AppEntry appEntry, Context context)213 public PremiumSmsPreference(AppEntry appEntry, Context context) { 214 super(context); 215 mAppEntry = appEntry; 216 mAppEntry.ensureLabel(context); 217 setTitle(mAppEntry.label); 218 if (mAppEntry.icon != null) { 219 setIcon(mAppEntry.icon); 220 } 221 setEntries(R.array.security_settings_premium_sms_values); 222 setEntryValues(new CharSequence[]{ 223 String.valueOf(SmsManager.PREMIUM_SMS_CONSENT_ASK_USER), 224 String.valueOf(SmsManager.PREMIUM_SMS_CONSENT_NEVER_ALLOW), 225 String.valueOf(SmsManager.PREMIUM_SMS_CONSENT_ALWAYS_ALLOW), 226 }); 227 setValue(String.valueOf(getCurrentValue())); 228 setSummary("%s"); 229 } 230 getCurrentValue()231 private int getCurrentValue() { 232 return mAppEntry.extraInfo instanceof SmsState 233 ? ((SmsState) mAppEntry.extraInfo).smsState 234 : SmsManager.PREMIUM_SMS_CONSENT_UNKNOWN; 235 } 236 237 @Override onBindViewHolder(PreferenceViewHolder holder)238 public void onBindViewHolder(PreferenceViewHolder holder) { 239 if (getIcon() == null) { 240 holder.itemView.post(new Runnable() { 241 @Override 242 public void run() { 243 mApplicationsState.ensureIcon(mAppEntry); 244 setIcon(mAppEntry.icon); 245 } 246 }); 247 } 248 super.onBindViewHolder(holder); 249 } 250 } 251 252 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 253 new BaseSearchIndexProvider(R.xml.premium_sms_settings); 254 } 255