PremiumSmsAccess.java revision 2328946b581276088cd37cc316c15f38b0f9129d
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.DividerPreference; 32import com.android.settings.R; 33import com.android.settings.applications.AppStateBaseBridge.Callback; 34import com.android.settings.applications.AppStateSmsPremBridge.SmsState; 35import com.android.settings.core.instrumentation.MetricsFeatureProvider; 36import com.android.settings.notification.EmptyTextSettings; 37import com.android.settings.overlay.FeatureFactory; 38import com.android.settingslib.applications.ApplicationsState; 39import com.android.settingslib.applications.ApplicationsState.AppEntry; 40import com.android.settingslib.applications.ApplicationsState.Callbacks; 41import com.android.settingslib.applications.ApplicationsState.Session; 42 43import java.util.ArrayList; 44 45public class PremiumSmsAccess extends EmptyTextSettings implements Callback, Callbacks, OnPreferenceChangeListener { 46 47 private ApplicationsState mApplicationsState; 48 private AppStateSmsPremBridge mSmsBackend; 49 private Session mSession; 50 51 @Override 52 public void onCreate(Bundle icicle) { 53 super.onCreate(icicle); 54 mApplicationsState = ApplicationsState.getInstance((Application) 55 getContext().getApplicationContext()); 56 mSession = mApplicationsState.newSession(this); 57 mSmsBackend = new AppStateSmsPremBridge(getContext(), mApplicationsState, this); 58 } 59 60 @Override 61 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 62 super.onViewCreated(view, savedInstanceState); 63 setLoading(true, false); 64 } 65 66 @Override 67 public void onResume() { 68 super.onResume(); 69 mSession.resume(); 70 mSmsBackend.resume(); 71 } 72 73 @Override 74 public void onPause() { 75 mSmsBackend.pause(); 76 mSession.pause(); 77 super.onPause(); 78 } 79 80 @Override 81 public int getMetricsCategory() { 82 return MetricsProto.MetricsEvent.PREMIUM_SMS_ACCESS; 83 } 84 85 @Override 86 public boolean onPreferenceChange(Preference preference, Object newValue) { 87 PremiumSmsPreference pref = (PremiumSmsPreference) preference; 88 int smsState = Integer.parseInt((String) newValue); 89 logSpecialPermissionChange(smsState, pref.mAppEntry.info.packageName); 90 mSmsBackend.setSmsState(pref.mAppEntry.info.packageName, smsState); 91 return true; 92 } 93 94 @VisibleForTesting 95 void logSpecialPermissionChange(int smsState, String packageName) { 96 int category = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN; 97 switch (smsState) { 98 case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER: 99 category = MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_PREMIUM_SMS_ASK; 100 break; 101 case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW: 102 category = MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_PREMIUM_SMS_DENY; 103 break; 104 case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW: 105 category = MetricsProto.MetricsEvent. 106 APP_SPECIAL_PERMISSION_PREMIUM_SMS_ALWAYS_ALLOW; 107 break; 108 } 109 if (category != SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN) { 110 FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider().action( 111 getContext(), category, packageName); 112 } 113 } 114 115 private void updatePrefs(ArrayList<AppEntry> apps) { 116 if (apps == null) return; 117 setEmptyText(R.string.premium_sms_none); 118 setLoading(false, true); 119 final PreferenceScreen screen = getPreferenceManager().createPreferenceScreen( 120 getPrefContext()); 121 screen.setOrderingAsAdded(true); 122 123 for (int i = 0; i < apps.size(); i++) { 124 final PremiumSmsPreference smsPreference = 125 new PremiumSmsPreference(apps.get(i), getPrefContext()); 126 smsPreference.setOnPreferenceChangeListener(this); 127 screen.addPreference(smsPreference); 128 } 129 if (apps.size() != 0) { 130 DividerPreference summary = new DividerPreference(getPrefContext()); 131 summary.setSelectable(false); 132 summary.setSummary(R.string.premium_sms_warning); 133 summary.setDividerAllowedAbove(true); 134 screen.addPreference(summary); 135 } 136 137 setPreferenceScreen(screen); 138 } 139 140 private void update() { 141 updatePrefs(mSession.rebuild(AppStateSmsPremBridge.FILTER_APP_PREMIUM_SMS, 142 ApplicationsState.ALPHA_COMPARATOR)); 143 } 144 145 @Override 146 public void onExtraInfoUpdated() { 147 update(); 148 } 149 150 @Override 151 public void onRebuildComplete(ArrayList<AppEntry> apps) { 152 updatePrefs(apps); 153 } 154 155 @Override 156 public void onRunningStateChanged(boolean running) { 157 158 } 159 160 @Override 161 public void onPackageListChanged() { 162 163 } 164 165 @Override 166 public void onPackageIconChanged() { 167 168 } 169 170 @Override 171 public void onPackageSizeChanged(String packageName) { 172 173 } 174 175 @Override 176 public void onAllSizesComputed() { 177 178 } 179 180 @Override 181 public void onLauncherInfoChanged() { 182 183 } 184 185 @Override 186 public void onLoadEntriesCompleted() { 187 188 } 189 190 private class PremiumSmsPreference extends DropDownPreference { 191 private final AppEntry mAppEntry; 192 193 public PremiumSmsPreference(AppEntry appEntry, Context context) { 194 super(context); 195 mAppEntry = appEntry; 196 mAppEntry.ensureLabel(context); 197 setTitle(mAppEntry.label); 198 if (mAppEntry.icon != null) { 199 setIcon(mAppEntry.icon); 200 } 201 setEntries(R.array.security_settings_premium_sms_values); 202 setEntryValues(new CharSequence[] { 203 String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER), 204 String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW), 205 String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW), 206 }); 207 setValue(String.valueOf(getCurrentValue())); 208 setSummary("%s"); 209 } 210 211 private int getCurrentValue() { 212 return mAppEntry.extraInfo instanceof SmsState 213 ? ((SmsState) mAppEntry.extraInfo).smsState 214 : SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN; 215 } 216 217 @Override 218 public void onBindViewHolder(PreferenceViewHolder holder) { 219 if (getIcon() == null) { 220 holder.itemView.post(new Runnable() { 221 @Override 222 public void run() { 223 mApplicationsState.ensureIcon(mAppEntry); 224 setIcon(mAppEntry.icon); 225 } 226 }); 227 } 228 super.onBindViewHolder(holder); 229 } 230 } 231} 232