MmsServiceBroker.java revision 31ef14d4f00b90e13a9755ecfa6cbe8aa7466da7
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server; 18 19import com.android.internal.telephony.IMms; 20 21import android.Manifest; 22import android.app.AppOpsManager; 23import android.app.PendingIntent; 24import android.content.ComponentName; 25import android.content.ContentValues; 26import android.content.Context; 27import android.content.Intent; 28import android.content.ServiceConnection; 29import android.content.pm.PackageManager; 30import android.net.Uri; 31import android.os.Binder; 32import android.os.Bundle; 33import android.os.Handler; 34import android.os.IBinder; 35import android.os.Message; 36import android.os.RemoteException; 37import android.os.ServiceManager; 38import android.telephony.TelephonyManager; 39import android.util.Slog; 40 41/** 42 * This class is a proxy for MmsService APIs. We need this because MmsService runs 43 * in phone process and may crash anytime. This manages a connection to the actual 44 * MmsService and bridges the public SMS/MMS APIs with MmsService implementation. 45 */ 46public class MmsServiceBroker extends SystemService { 47 private static final String TAG = "MmsServiceBroker"; 48 49 private static final ComponentName MMS_SERVICE_COMPONENT = 50 new ComponentName("com.android.mms.service", "com.android.mms.service.MmsService"); 51 52 private static final int MSG_TRY_CONNECTING = 1; 53 54 private static final Uri FAKE_SMS_SENT_URI = Uri.parse("content://sms/sent/0"); 55 private static final Uri FAKE_MMS_SENT_URI = Uri.parse("content://mms/sent/0"); 56 private static final Uri FAKE_SMS_DRAFT_URI = Uri.parse("content://sms/draft/0"); 57 private static final Uri FAKE_MMS_DRAFT_URI = Uri.parse("content://mms/draft/0"); 58 59 private Context mContext; 60 // The actual MMS service instance to invoke 61 private volatile IMms mService; 62 private boolean mIsConnecting; 63 64 // Cached system service instances 65 private volatile AppOpsManager mAppOpsManager = null; 66 private volatile PackageManager mPackageManager = null; 67 private volatile TelephonyManager mTelephonyManager = null; 68 69 private final Handler mConnectionHandler = new Handler() { 70 @Override 71 public void handleMessage(Message msg) { 72 switch (msg.what) { 73 case MSG_TRY_CONNECTING: 74 tryConnecting(); 75 break; 76 default: 77 Slog.e(TAG, "Unknown message"); 78 } 79 } 80 }; 81 82 private ServiceConnection mConnection = new ServiceConnection() { 83 @Override 84 public void onServiceConnected(ComponentName name, IBinder service) { 85 Slog.i(TAG, "MmsService connected"); 86 synchronized (MmsServiceBroker.this) { 87 mService = IMms.Stub.asInterface(service); 88 mIsConnecting = false; 89 } 90 } 91 92 @Override 93 public void onServiceDisconnected(ComponentName name) { 94 Slog.i(TAG, "MmsService unexpectedly disconnected"); 95 synchronized (MmsServiceBroker.this) { 96 mService = null; 97 mIsConnecting = false; 98 } 99 } 100 }; 101 102 public MmsServiceBroker(Context context) { 103 super(context); 104 mContext = context; 105 mService = null; 106 mIsConnecting = false; 107 } 108 109 @Override 110 public void onStart() { 111 publishBinderService("imms", new BinderService()); 112 } 113 114 public void systemRunning() { 115 tryConnecting(); 116 } 117 118 private void tryConnecting() { 119 Slog.i(TAG, "Connecting to MmsService"); 120 synchronized (this) { 121 if (mIsConnecting) { 122 Slog.d(TAG, "Already connecting"); 123 return; 124 } 125 final Intent intent = new Intent(); 126 intent.setComponent(MMS_SERVICE_COMPONENT); 127 try { 128 if (mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)) { 129 mIsConnecting = true; 130 } else { 131 Slog.e(TAG, "Failed to connect to MmsService"); 132 } 133 } catch (SecurityException e) { 134 Slog.e(TAG, "Forbidden to connect to MmsService", e); 135 } 136 } 137 } 138 139 private void ensureService() { 140 if (mService == null) { 141 // Service instance lost, kicking off the connection again 142 mConnectionHandler.sendMessage(mConnectionHandler.obtainMessage(MSG_TRY_CONNECTING)); 143 throw new RuntimeException("MMS service is not connected"); 144 } 145 } 146 147 /** 148 * Making sure when we obtain the mService instance it is always valid. 149 * Throws {@link RuntimeException} when it is empty. 150 */ 151 private IMms getServiceGuarded() { 152 ensureService(); 153 return mService; 154 } 155 156 private AppOpsManager getAppOpsManager() { 157 if (mAppOpsManager == null) { 158 mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); 159 } 160 return mAppOpsManager; 161 } 162 163 private PackageManager getPackageManager() { 164 if (mPackageManager == null) { 165 mPackageManager = mContext.getPackageManager(); 166 } 167 return mPackageManager; 168 } 169 170 private TelephonyManager getTelephonyManager() { 171 if (mTelephonyManager == null) { 172 mTelephonyManager = (TelephonyManager) mContext.getSystemService( 173 Context.TELEPHONY_SERVICE); 174 } 175 return mTelephonyManager; 176 } 177 178 /* 179 * Throws a security exception unless the caller has carrier privilege. 180 */ 181 private void enforceCarrierPrivilege() { 182 String[] packages = getPackageManager().getPackagesForUid(Binder.getCallingUid()); 183 for (String pkg : packages) { 184 if (getTelephonyManager().checkCarrierPrivilegesForPackage(pkg) == 185 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) { 186 return; 187 } 188 } 189 throw new SecurityException("No carrier privilege"); 190 } 191 192 // Service API calls implementation, proxied to the real MmsService in "com.android.mms.service" 193 private final class BinderService extends IMms.Stub { 194 @Override 195 public void sendMessage(long subId, String callingPkg, Uri contentUri, 196 String locationUrl, ContentValues configOverrides, PendingIntent sentIntent) 197 throws RemoteException { 198 mContext.enforceCallingPermission(Manifest.permission.SEND_SMS, "Send MMS message"); 199 if (getAppOpsManager().noteOp(AppOpsManager.OP_SEND_SMS, Binder.getCallingUid(), 200 callingPkg) != AppOpsManager.MODE_ALLOWED) { 201 return; 202 } 203 getServiceGuarded().sendMessage(subId, callingPkg, contentUri, locationUrl, 204 configOverrides, sentIntent); 205 } 206 207 @Override 208 public void downloadMessage(long subId, String callingPkg, String locationUrl, 209 Uri contentUri, ContentValues configOverrides, 210 PendingIntent downloadedIntent) throws RemoteException { 211 mContext.enforceCallingPermission(Manifest.permission.RECEIVE_MMS, 212 "Download MMS message"); 213 if (getAppOpsManager().noteOp(AppOpsManager.OP_RECEIVE_MMS, Binder.getCallingUid(), 214 callingPkg) != AppOpsManager.MODE_ALLOWED) { 215 return; 216 } 217 getServiceGuarded().downloadMessage(subId, callingPkg, locationUrl, contentUri, 218 configOverrides, downloadedIntent); 219 } 220 221 @Override 222 public void updateMmsSendStatus(int messageRef, boolean success) throws RemoteException { 223 enforceCarrierPrivilege(); 224 getServiceGuarded().updateMmsSendStatus(messageRef, success); 225 } 226 227 @Override 228 public void updateMmsDownloadStatus(int messageRef, byte[] pdu) throws RemoteException { 229 enforceCarrierPrivilege(); 230 getServiceGuarded().updateMmsDownloadStatus(messageRef, pdu); 231 } 232 233 @Override 234 public Bundle getCarrierConfigValues(long subId) throws RemoteException { 235 return getServiceGuarded().getCarrierConfigValues(subId); 236 } 237 238 @Override 239 public Uri importTextMessage(String callingPkg, String address, int type, String text, 240 long timestampMillis, boolean seen, boolean read) throws RemoteException { 241 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Import SMS message"); 242 if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), 243 callingPkg) != AppOpsManager.MODE_ALLOWED) { 244 // Silently fail AppOps failure due to not being the default SMS app 245 // while writing the TelephonyProvider 246 return FAKE_SMS_SENT_URI; 247 } 248 return getServiceGuarded().importTextMessage( 249 callingPkg, address, type, text, timestampMillis, seen, read); 250 } 251 252 @Override 253 public Uri importMultimediaMessage(String callingPkg, Uri contentUri, 254 String messageId, long timestampSecs, boolean seen, boolean read) 255 throws RemoteException { 256 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Import MMS message"); 257 if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), 258 callingPkg) != AppOpsManager.MODE_ALLOWED) { 259 // Silently fail AppOps failure due to not being the default SMS app 260 // while writing the TelephonyProvider 261 return FAKE_MMS_SENT_URI; 262 } 263 return getServiceGuarded().importMultimediaMessage( 264 callingPkg, contentUri, messageId, timestampSecs, seen, read); 265 } 266 267 @Override 268 public boolean deleteStoredMessage(String callingPkg, Uri messageUri) 269 throws RemoteException { 270 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, 271 "Delete SMS/MMS message"); 272 if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), 273 callingPkg) != AppOpsManager.MODE_ALLOWED) { 274 return false; 275 } 276 return getServiceGuarded().deleteStoredMessage(callingPkg, messageUri); 277 } 278 279 @Override 280 public boolean deleteStoredConversation(String callingPkg, long conversationId) 281 throws RemoteException { 282 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Delete conversation"); 283 if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), 284 callingPkg) != AppOpsManager.MODE_ALLOWED) { 285 return false; 286 } 287 return getServiceGuarded().deleteStoredConversation(callingPkg, conversationId); 288 } 289 290 @Override 291 public boolean updateStoredMessageStatus(String callingPkg, Uri messageUri, 292 ContentValues statusValues) throws RemoteException { 293 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, 294 "Update SMS/MMS message"); 295 return getServiceGuarded() 296 .updateStoredMessageStatus(callingPkg, messageUri, statusValues); 297 } 298 299 @Override 300 public boolean archiveStoredConversation(String callingPkg, long conversationId, 301 boolean archived) throws RemoteException { 302 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, 303 "Update SMS/MMS message"); 304 return getServiceGuarded() 305 .archiveStoredConversation(callingPkg, conversationId, archived); 306 } 307 308 @Override 309 public Uri addTextMessageDraft(String callingPkg, String address, String text) 310 throws RemoteException { 311 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Add SMS draft"); 312 if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), 313 callingPkg) != AppOpsManager.MODE_ALLOWED) { 314 // Silently fail AppOps failure due to not being the default SMS app 315 // while writing the TelephonyProvider 316 return FAKE_SMS_DRAFT_URI; 317 } 318 return getServiceGuarded().addTextMessageDraft(callingPkg, address, text); 319 } 320 321 @Override 322 public Uri addMultimediaMessageDraft(String callingPkg, Uri contentUri) 323 throws RemoteException { 324 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Add MMS draft"); 325 if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), 326 callingPkg) != AppOpsManager.MODE_ALLOWED) { 327 // Silently fail AppOps failure due to not being the default SMS app 328 // while writing the TelephonyProvider 329 return FAKE_MMS_DRAFT_URI; 330 } 331 return getServiceGuarded().addMultimediaMessageDraft(callingPkg, contentUri); 332 } 333 334 @Override 335 public void sendStoredMessage(long subId, String callingPkg, Uri messageUri, 336 ContentValues configOverrides, PendingIntent sentIntent) throws RemoteException { 337 mContext.enforceCallingPermission(Manifest.permission.SEND_SMS, 338 "Send stored MMS message"); 339 if (getAppOpsManager().noteOp(AppOpsManager.OP_SEND_SMS, Binder.getCallingUid(), 340 callingPkg) != AppOpsManager.MODE_ALLOWED) { 341 return; 342 } 343 getServiceGuarded().sendStoredMessage(subId, callingPkg, messageUri, configOverrides, 344 sentIntent); 345 } 346 347 @Override 348 public void setAutoPersisting(String callingPkg, boolean enabled) throws RemoteException { 349 mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Set auto persist"); 350 if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), 351 callingPkg) != AppOpsManager.MODE_ALLOWED) { 352 return; 353 } 354 getServiceGuarded().setAutoPersisting(callingPkg, enabled); 355 } 356 357 @Override 358 public boolean getAutoPersisting() throws RemoteException { 359 return getServiceGuarded().getAutoPersisting(); 360 } 361 } 362} 363