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 void onDestroy() {
82        mSmsBackend.release();
83        mSession.release();
84        super.onDestroy();
85    }
86
87    @Override
88    public int getMetricsCategory() {
89        return MetricsProto.MetricsEvent.PREMIUM_SMS_ACCESS;
90    }
91
92    @Override
93    public boolean onPreferenceChange(Preference preference, Object newValue) {
94        PremiumSmsPreference pref = (PremiumSmsPreference) preference;
95        int smsState = Integer.parseInt((String) newValue);
96        logSpecialPermissionChange(smsState, pref.mAppEntry.info.packageName);
97        mSmsBackend.setSmsState(pref.mAppEntry.info.packageName, smsState);
98        return true;
99    }
100
101    @VisibleForTesting
102    void logSpecialPermissionChange(int smsState, String packageName) {
103        int category = SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN;
104        switch (smsState) {
105            case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER:
106                category = MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_PREMIUM_SMS_ASK;
107                break;
108            case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW:
109                category = MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_PREMIUM_SMS_DENY;
110                break;
111            case SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW:
112                category = MetricsProto.MetricsEvent.
113                        APP_SPECIAL_PERMISSION_PREMIUM_SMS_ALWAYS_ALLOW;
114                break;
115        }
116        if (category != SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN) {
117            FeatureFactory.getFactory(getContext()).getMetricsFeatureProvider().action(
118                    getContext(), category, packageName);
119        }
120    }
121
122    private void updatePrefs(ArrayList<AppEntry> apps) {
123        if (apps == null) return;
124        setEmptyText(R.string.premium_sms_none);
125        setLoading(false, true);
126        final PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(
127                getPrefContext());
128        screen.setOrderingAsAdded(true);
129
130        for (int i = 0; i < apps.size(); i++) {
131            final PremiumSmsPreference smsPreference =
132                    new PremiumSmsPreference(apps.get(i), getPrefContext());
133            smsPreference.setOnPreferenceChangeListener(this);
134            screen.addPreference(smsPreference);
135        }
136        if (apps.size() != 0) {
137            DividerPreference summary = new DividerPreference(getPrefContext());
138            summary.setSelectable(false);
139            summary.setSummary(R.string.premium_sms_warning);
140            summary.setDividerAllowedAbove(true);
141            screen.addPreference(summary);
142        }
143
144        setPreferenceScreen(screen);
145    }
146
147    private void update() {
148        updatePrefs(mSession.rebuild(AppStateSmsPremBridge.FILTER_APP_PREMIUM_SMS,
149                ApplicationsState.ALPHA_COMPARATOR));
150    }
151
152    @Override
153    public void onExtraInfoUpdated() {
154        update();
155    }
156
157    @Override
158    public void onRebuildComplete(ArrayList<AppEntry> apps) {
159        updatePrefs(apps);
160    }
161
162    @Override
163    public void onRunningStateChanged(boolean running) {
164
165    }
166
167    @Override
168    public void onPackageListChanged() {
169
170    }
171
172    @Override
173    public void onPackageIconChanged() {
174
175    }
176
177    @Override
178    public void onPackageSizeChanged(String packageName) {
179
180    }
181
182    @Override
183    public void onAllSizesComputed() {
184
185    }
186
187    @Override
188    public void onLauncherInfoChanged() {
189
190    }
191
192    @Override
193    public void onLoadEntriesCompleted() {
194
195    }
196
197    private class PremiumSmsPreference extends DropDownPreference {
198        private final AppEntry mAppEntry;
199
200        public PremiumSmsPreference(AppEntry appEntry, Context context) {
201            super(context);
202            mAppEntry = appEntry;
203            mAppEntry.ensureLabel(context);
204            setTitle(mAppEntry.label);
205            if (mAppEntry.icon != null) {
206                setIcon(mAppEntry.icon);
207            }
208            setEntries(R.array.security_settings_premium_sms_values);
209            setEntryValues(new CharSequence[] {
210                    String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ASK_USER),
211                    String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_NEVER_ALLOW),
212                    String.valueOf(SmsUsageMonitor.PREMIUM_SMS_PERMISSION_ALWAYS_ALLOW),
213            });
214            setValue(String.valueOf(getCurrentValue()));
215            setSummary("%s");
216        }
217
218        private int getCurrentValue() {
219            return mAppEntry.extraInfo instanceof SmsState
220                    ? ((SmsState) mAppEntry.extraInfo).smsState
221                    : SmsUsageMonitor.PREMIUM_SMS_PERMISSION_UNKNOWN;
222        }
223
224        @Override
225        public void onBindViewHolder(PreferenceViewHolder holder) {
226            if (getIcon() == null) {
227                holder.itemView.post(new Runnable() {
228                    @Override
229                    public void run() {
230                        mApplicationsState.ensureIcon(mAppEntry);
231                        setIcon(mAppEntry.icon);
232                    }
233                });
234            }
235            super.onBindViewHolder(holder);
236        }
237    }
238}
239