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