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 package com.android.settings.applications;
15 
16 import android.content.Context;
17 import android.telephony.SmsManager;
18 
19 import com.android.settingslib.applications.ApplicationsState;
20 import com.android.settingslib.applications.ApplicationsState.AppEntry;
21 import com.android.settingslib.applications.ApplicationsState.AppFilter;
22 
23 import java.util.ArrayList;
24 
25 /**
26  * Connects the info provided by ApplicationsState and premium sms permission state.
27  */
28 public class AppStateSmsPremBridge extends AppStateBaseBridge {
29 
30     private final Context mContext;
31     private final SmsManager mSmsManager;
32 
AppStateSmsPremBridge(Context context, ApplicationsState appState, Callback callback)33     public AppStateSmsPremBridge(Context context, ApplicationsState appState, Callback callback) {
34         super(appState, callback);
35         mContext = context;
36         mSmsManager = SmsManager.getDefault();
37     }
38 
39     @Override
loadAllExtraInfo()40     protected void loadAllExtraInfo() {
41         ArrayList<AppEntry> apps = mAppSession.getAllApps();
42         final int N = apps.size();
43         for (int i = 0; i < N; i++) {
44             AppEntry app = apps.get(i);
45             updateExtraInfo(app, app.info.packageName, app.info.uid);
46         }
47     }
48 
49     @Override
updateExtraInfo(AppEntry app, String pkg, int uid)50     protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
51         app.extraInfo = getState(pkg);
52     }
53 
getState(String pkg)54     public SmsState getState(String pkg) {
55         final SmsState state = new SmsState();
56         state.smsState = getSmsState(pkg);
57         return state;
58     }
59 
getSmsState(String pkg)60     private int getSmsState(String pkg) {
61         return mSmsManager.getPremiumSmsConsent(pkg);
62     }
63 
setSmsState(String pkg, int state)64     public void setSmsState(String pkg, int state) {
65         mSmsManager.setPremiumSmsConsent(pkg, state);
66     }
67 
68     public static class SmsState {
69         public int smsState;
70 
isGranted()71         public boolean isGranted() {
72             return smsState == SmsManager.PREMIUM_SMS_CONSENT_ALWAYS_ALLOW;
73         }
74     }
75 
76     public static final AppFilter FILTER_APP_PREMIUM_SMS = new AppFilter() {
77         @Override
78         public void init() {
79         }
80 
81         @Override
82         public boolean filterApp(AppEntry info) {
83             return info.extraInfo instanceof SmsState && ((SmsState) info.extraInfo).smsState
84                     != SmsManager.PREMIUM_SMS_CONSENT_UNKNOWN;
85         }
86     };
87 }
88