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