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