PremiumSmsAccess.java revision afa12a41d5b8a71c9239d0a4c9746434264e608c
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15package com.android.settings.applications; 16 17import android.annotation.Nullable; 18import android.app.Application; 19import android.content.Context; 20import android.os.Bundle; 21import android.support.v7.preference.DropDownPreference; 22import android.support.v7.preference.Preference; 23import android.support.v7.preference.Preference.OnPreferenceChangeListener; 24import android.support.v7.preference.PreferenceScreen; 25import android.support.v7.preference.PreferenceViewHolder; 26import android.view.View; 27 28import com.android.internal.annotations.VisibleForTesting; 29import com.android.internal.logging.nano.MetricsProto; 30import com.android.internal.telephony.SmsUsageMonitor; 31import com.android.settings.R; 32import com.android.settings.applications.AppStateBaseBridge.Callback; 33import com.android.settings.applications.AppStateSmsPremBridge.SmsState; 34import com.android.settings.notification.EmptyTextSettings; 35import com.android.settings.overlay.FeatureFactory; 36import com.android.settingslib.applications.ApplicationsState; 37import com.android.settingslib.applications.ApplicationsState.AppEntry; 38import com.android.settingslib.applications.ApplicationsState.Callbacks; 39import com.android.settingslib.applications.ApplicationsState.Session; 40import com.android.settingslib.widget.FooterPreference; 41 42import java.util.ArrayList; 43 44public class PremiumSmsAccess extends EmptyTextSettings implements Callback, Callbacks, OnPreferenceChangeListener { 45 46 private ApplicationsState mApplicationsState; 47 private AppStateSmsPremBridge mSmsBackend; 48 private Session mSession; 49 50 @Override 51 public void onCreate(Bundle icicle) { 52 super.onCreate(icicle); 53 mApplicationsState = ApplicationsState.getInstance((Application) 54 getContext().getApplicationContext()); 55 mSession = mApplicationsState.newSession(this, getLifecycle()); 56 mSmsBackend = new AppStateSmsPremBridge(getContext(), mApplicationsState, this); 57 } 58 59 @Override 60 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 61 super.onViewCreated(view, savedInstanceState); 62 setLoading(true, false); 63 } 64 65 @Override 66 public void onResume() { 67 super.onResume(); 68 mSmsBackend.resume(); 69 } 70 71 @Override 72 public void onPause() { 73 mSmsBackend.pause(); 74 super.onPause(); 75 } 76 77 @Override 78 public void onDestroy() { 79 mSmsBackend.release(); 80 super.onDestroy(); 81 } 82 83 @Override 84 protected int getPreferenceScreenResId() { 85 return R.xml.premium_sms_settings; 86 } 87 88 @Override 89 public int getMetricsCategory() { 90 return MetricsProto.MetricsEvent.PREMIUM_SMS_ACCESS; 91 } 92 93 @Override 94 public boolean onPreferenceChange(Preference preference, Object newValue) { 95 PremiumSmsPreference pref = (PremiumSmsPreference) preference; 96 int smsState = Integer.parseInt((String) newValue); 97 logSpecialPermissionChange(smsState, pref.mAppEntry.info.packageName); 98 mSmsBackend.setSmsState(pref.mAppEntry.info.packageName, smsState); 99 return true; 100 } 101 102 @VisibleForTesting 103 void logSpecialPermissionChange(int smsState, String packageName) { 104 int category = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN; 105 switch (smsState) { 106 case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER: 107 category = MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_PREMIUM_SMS_ASK; 108 break; 109 case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW: 110 category = MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_PREMIUM_SMS_DENY; 111 break; 112 case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW: 113 category = MetricsProto.MetricsEvent. 114 APP_SPECIAL_PERMISSION_PREMIUM_SMS_ALWAYS_ALLOW; 115 break; 116 } 117 if (category != SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN) { 118 FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider().action( 119 getContext(), category, packageName); 120 } 121 } 122 123 private void updatePrefs(ArrayList<AppEntry> apps) { 124 if (apps == null) return; 125 setEmptyText(R.string.premium_sms_none); 126 setLoading(false, true); 127 final PreferenceScreen screen; 128 if (usePreferenceScreenTitle()) { 129 screen = getPreferenceScreen(); 130 screen.removeAll(); 131 } else { 132 screen = getPreferenceManager().createPreferenceScreen(getPrefContext()); 133 } 134 135 screen.setOrderingAsAdded(true); 136 137 for (int i = 0; i < apps.size(); i++) { 138 final PremiumSmsPreference smsPreference = 139 new PremiumSmsPreference(apps.get(i), getPrefContext()); 140 smsPreference.setOnPreferenceChangeListener(this); 141 screen.addPreference(smsPreference); 142 } 143 if (apps.size() != 0) { 144 FooterPreference footer = new FooterPreference(getPrefContext()); 145 footer.setTitle(R.string.premium_sms_warning); 146 screen.addPreference(footer); 147 } 148 149 if (!usePreferenceScreenTitle()) { 150 setPreferenceScreen(screen); 151 } 152 } 153 154 private void update() { 155 updatePrefs(mSession.rebuild(AppStateSmsPremBridge.FILTER_APP_PREMIUM_SMS, 156 ApplicationsState.ALPHA_COMPARATOR)); 157 } 158 159 @Override 160 public void onExtraInfoUpdated() { 161 update(); 162 } 163 164 @Override 165 public void onRebuildComplete(ArrayList<AppEntry> apps) { 166 updatePrefs(apps); 167 } 168 169 @Override 170 public void onRunningStateChanged(boolean running) { 171 172 } 173 174 @Override 175 public void onPackageListChanged() { 176 177 } 178 179 @Override 180 public void onPackageIconChanged() { 181 182 } 183 184 @Override 185 public void onPackageSizeChanged(String packageName) { 186 187 } 188 189 @Override 190 public void onAllSizesComputed() { 191 192 } 193 194 @Override 195 public void onLauncherInfoChanged() { 196 197 } 198 199 @Override 200 public void onLoadEntriesCompleted() { 201 202 } 203 204 private class PremiumSmsPreference extends DropDownPreference { 205 private final AppEntry mAppEntry; 206 207 public PremiumSmsPreference(AppEntry appEntry, Context context) { 208 super(context); 209 mAppEntry = appEntry; 210 mAppEntry.ensureLabel(context); 211 setTitle(mAppEntry.label); 212 if (mAppEntry.icon != null) { 213 setIcon(mAppEntry.icon); 214 } 215 setEntries(R.array.security_settings_premium_sms_values); 216 setEntryValues(new CharSequence[] { 217 String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER), 218 String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW), 219 String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW), 220 }); 221 setValue(String.valueOf(getCurrentValue())); 222 setSummary("%s"); 223 } 224 225 private int getCurrentValue() { 226 return mAppEntry.extraInfo instanceof SmsState 227 ? ((SmsState) mAppEntry.extraInfo).smsState 228 : SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN; 229 } 230 231 @Override 232 public void onBindViewHolder(PreferenceViewHolder holder) { 233 if (getIcon() == null) { 234 holder.itemView.post(new Runnable() { 235 @Override 236 public void run() { 237 mApplicationsState.ensureIcon(mAppEntry); 238 setIcon(mAppEntry.icon); 239 } 240 }); 241 } 242 super.onBindViewHolder(holder); 243 } 244 } 245} 246