Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.server; |
| 18 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.app.AppOpsManager; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.content.ComponentName; |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame^] | 23 | import android.content.ContentProvider; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 24 | import android.content.ContentValues; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.ServiceConnection; |
| 28 | import android.content.pm.PackageManager; |
| 29 | import android.net.Uri; |
| 30 | import android.os.Binder; |
Shri Borde | 7237972 | 2014-09-02 09:48:49 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.IBinder; |
| 34 | import android.os.Message; |
| 35 | import android.os.RemoteException; |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 36 | import android.os.SystemClock; |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame^] | 37 | import android.os.UserHandle; |
| 38 | import android.provider.Telephony; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 39 | import android.telephony.TelephonyManager; |
| 40 | import android.util.Slog; |
| 41 | |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 42 | import com.android.internal.telephony.IMms; |
| 43 | |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame^] | 44 | import java.util.List; |
| 45 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 46 | /** |
| 47 | * This class is a proxy for MmsService APIs. We need this because MmsService runs |
| 48 | * in phone process and may crash anytime. This manages a connection to the actual |
| 49 | * MmsService and bridges the public SMS/MMS APIs with MmsService implementation. |
| 50 | */ |
| 51 | public class MmsServiceBroker extends SystemService { |
| 52 | private static final String TAG = "MmsServiceBroker"; |
| 53 | |
| 54 | private static final ComponentName MMS_SERVICE_COMPONENT = |
| 55 | new ComponentName("com.android.mms.service", "com.android.mms.service.MmsService"); |
| 56 | |
| 57 | private static final int MSG_TRY_CONNECTING = 1; |
| 58 | |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 59 | private static final Uri FAKE_SMS_SENT_URI = Uri.parse("content://sms/sent/0"); |
| 60 | private static final Uri FAKE_MMS_SENT_URI = Uri.parse("content://mms/sent/0"); |
| 61 | private static final Uri FAKE_SMS_DRAFT_URI = Uri.parse("content://sms/draft/0"); |
| 62 | private static final Uri FAKE_MMS_DRAFT_URI = Uri.parse("content://mms/draft/0"); |
| 63 | |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 64 | private static final long SERVICE_CONNECTION_WAIT_TIME_MS = 4 * 1000L; // 4 seconds |
| 65 | private static final long RETRY_DELAY_ON_DISCONNECTION_MS = 3 * 1000L; // 3 seconds |
| 66 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 67 | private Context mContext; |
| 68 | // The actual MMS service instance to invoke |
| 69 | private volatile IMms mService; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 70 | |
| 71 | // Cached system service instances |
| 72 | private volatile AppOpsManager mAppOpsManager = null; |
| 73 | private volatile PackageManager mPackageManager = null; |
| 74 | private volatile TelephonyManager mTelephonyManager = null; |
| 75 | |
| 76 | private final Handler mConnectionHandler = new Handler() { |
| 77 | @Override |
| 78 | public void handleMessage(Message msg) { |
| 79 | switch (msg.what) { |
| 80 | case MSG_TRY_CONNECTING: |
| 81 | tryConnecting(); |
| 82 | break; |
| 83 | default: |
| 84 | Slog.e(TAG, "Unknown message"); |
| 85 | } |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | private ServiceConnection mConnection = new ServiceConnection() { |
| 90 | @Override |
| 91 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 92 | Slog.i(TAG, "MmsService connected"); |
| 93 | synchronized (MmsServiceBroker.this) { |
| 94 | mService = IMms.Stub.asInterface(service); |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 95 | MmsServiceBroker.this.notifyAll(); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void onServiceDisconnected(ComponentName name) { |
| 101 | Slog.i(TAG, "MmsService unexpectedly disconnected"); |
| 102 | synchronized (MmsServiceBroker.this) { |
| 103 | mService = null; |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 104 | MmsServiceBroker.this.notifyAll(); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 105 | } |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 106 | // Retry connecting, but not too eager (with a delay) |
| 107 | // since it may come back by itself. |
| 108 | mConnectionHandler.sendMessageDelayed( |
| 109 | mConnectionHandler.obtainMessage(MSG_TRY_CONNECTING), |
| 110 | RETRY_DELAY_ON_DISCONNECTION_MS); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | |
| 114 | public MmsServiceBroker(Context context) { |
| 115 | super(context); |
| 116 | mContext = context; |
| 117 | mService = null; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onStart() { |
| 122 | publishBinderService("imms", new BinderService()); |
| 123 | } |
| 124 | |
| 125 | public void systemRunning() { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 126 | Slog.i(TAG, "Delay connecting to MmsService until an API is called"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private void tryConnecting() { |
| 130 | Slog.i(TAG, "Connecting to MmsService"); |
| 131 | synchronized (this) { |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 132 | if (mService != null) { |
| 133 | Slog.d(TAG, "Already connected"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | final Intent intent = new Intent(); |
| 137 | intent.setComponent(MMS_SERVICE_COMPONENT); |
| 138 | try { |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 139 | if (!mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)) { |
| 140 | Slog.e(TAG, "Failed to bind to MmsService"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 141 | } |
| 142 | } catch (SecurityException e) { |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 143 | Slog.e(TAG, "Forbidden to bind to MmsService", e); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | private void ensureService() { |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 149 | synchronized (this) { |
| 150 | if (mService == null) { |
| 151 | // Service is not connected. Try blocking connecting. |
| 152 | Slog.w(TAG, "MmsService not connected. Try connecting..."); |
| 153 | mConnectionHandler.sendMessage( |
| 154 | mConnectionHandler.obtainMessage(MSG_TRY_CONNECTING)); |
| 155 | final long shouldEnd = |
| 156 | SystemClock.elapsedRealtime() + SERVICE_CONNECTION_WAIT_TIME_MS; |
| 157 | long waitTime = SERVICE_CONNECTION_WAIT_TIME_MS; |
| 158 | while (waitTime > 0) { |
| 159 | try { |
| 160 | // TODO: consider using Java concurrent construct instead of raw object wait |
| 161 | this.wait(waitTime); |
| 162 | } catch (InterruptedException e) { |
| 163 | Slog.w(TAG, "Connection wait interrupted", e); |
| 164 | } |
| 165 | if (mService != null) { |
| 166 | // Success |
| 167 | return; |
| 168 | } |
| 169 | // Calculate remaining waiting time to make sure we wait the full timeout period |
| 170 | waitTime = shouldEnd - SystemClock.elapsedRealtime(); |
| 171 | } |
| 172 | // Timed out. Something's really wrong. |
| 173 | Slog.e(TAG, "Can not connect to MmsService (timed out)"); |
| 174 | throw new RuntimeException("Timed out in connecting to MmsService"); |
| 175 | } |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Making sure when we obtain the mService instance it is always valid. |
| 181 | * Throws {@link RuntimeException} when it is empty. |
| 182 | */ |
| 183 | private IMms getServiceGuarded() { |
| 184 | ensureService(); |
| 185 | return mService; |
| 186 | } |
| 187 | |
| 188 | private AppOpsManager getAppOpsManager() { |
| 189 | if (mAppOpsManager == null) { |
| 190 | mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
| 191 | } |
| 192 | return mAppOpsManager; |
| 193 | } |
| 194 | |
| 195 | private PackageManager getPackageManager() { |
| 196 | if (mPackageManager == null) { |
| 197 | mPackageManager = mContext.getPackageManager(); |
| 198 | } |
| 199 | return mPackageManager; |
| 200 | } |
| 201 | |
| 202 | private TelephonyManager getTelephonyManager() { |
| 203 | if (mTelephonyManager == null) { |
| 204 | mTelephonyManager = (TelephonyManager) mContext.getSystemService( |
| 205 | Context.TELEPHONY_SERVICE); |
| 206 | } |
| 207 | return mTelephonyManager; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Throws a security exception unless the caller has carrier privilege. |
| 212 | */ |
| 213 | private void enforceCarrierPrivilege() { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 214 | final String[] packages = getPackageManager().getPackagesForUid(Binder.getCallingUid()); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 215 | for (String pkg : packages) { |
| 216 | if (getTelephonyManager().checkCarrierPrivilegesForPackage(pkg) == |
| 217 | TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) { |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | throw new SecurityException("No carrier privilege"); |
| 222 | } |
| 223 | |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 224 | private String getCallingPackageName() { |
| 225 | final String[] packages = getPackageManager().getPackagesForUid(Binder.getCallingUid()); |
| 226 | if (packages != null && packages.length > 0) { |
| 227 | return packages[0]; |
| 228 | } |
| 229 | return "unknown"; |
| 230 | } |
| 231 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 232 | // Service API calls implementation, proxied to the real MmsService in "com.android.mms.service" |
| 233 | private final class BinderService extends IMms.Stub { |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame^] | 234 | private static final String PHONE_PACKAGE_NAME = "com.android.phone"; |
| 235 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 236 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 237 | public void sendMessage(int subId, String callingPkg, Uri contentUri, |
Ye Wen | 8179c2a | 2014-09-04 15:36:11 -0700 | [diff] [blame] | 238 | String locationUrl, Bundle configOverrides, PendingIntent sentIntent) |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 239 | throws RemoteException { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 240 | Slog.d(TAG, "sendMessage() by " + callingPkg); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 241 | mContext.enforceCallingPermission(Manifest.permission.SEND_SMS, "Send MMS message"); |
| 242 | if (getAppOpsManager().noteOp(AppOpsManager.OP_SEND_SMS, Binder.getCallingUid(), |
| 243 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 244 | return; |
| 245 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame^] | 246 | contentUri = adjustUriForUserAndGrantPermission(contentUri, |
| 247 | Telephony.Mms.Intents.MMS_SEND_ACTION, |
| 248 | Intent.FLAG_GRANT_READ_URI_PERMISSION); |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 249 | getServiceGuarded().sendMessage(subId, callingPkg, contentUri, locationUrl, |
| 250 | configOverrides, sentIntent); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 254 | public void downloadMessage(int subId, String callingPkg, String locationUrl, |
Ye Wen | 8179c2a | 2014-09-04 15:36:11 -0700 | [diff] [blame] | 255 | Uri contentUri, Bundle configOverrides, |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 256 | PendingIntent downloadedIntent) throws RemoteException { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 257 | Slog.d(TAG, "downloadMessage() by " + callingPkg); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 258 | mContext.enforceCallingPermission(Manifest.permission.RECEIVE_MMS, |
| 259 | "Download MMS message"); |
| 260 | if (getAppOpsManager().noteOp(AppOpsManager.OP_RECEIVE_MMS, Binder.getCallingUid(), |
| 261 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 262 | return; |
| 263 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame^] | 264 | contentUri = adjustUriForUserAndGrantPermission(contentUri, |
| 265 | Telephony.Mms.Intents.MMS_DOWNLOAD_ACTION, |
| 266 | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 267 | |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 268 | getServiceGuarded().downloadMessage(subId, callingPkg, locationUrl, contentUri, |
| 269 | configOverrides, downloadedIntent); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
Cheuksan Wang | ee04543 | 2014-09-08 19:54:58 -0700 | [diff] [blame] | 273 | public void updateMmsSendStatus(int messageRef, byte[] pdu, int status) |
| 274 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 275 | enforceCarrierPrivilege(); |
Cheuksan Wang | ee04543 | 2014-09-08 19:54:58 -0700 | [diff] [blame] | 276 | getServiceGuarded().updateMmsSendStatus(messageRef, pdu, status); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | @Override |
Cheuksan Wang | ee04543 | 2014-09-08 19:54:58 -0700 | [diff] [blame] | 280 | public void updateMmsDownloadStatus(int messageRef, int status) throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 281 | enforceCarrierPrivilege(); |
Cheuksan Wang | ee04543 | 2014-09-08 19:54:58 -0700 | [diff] [blame] | 282 | getServiceGuarded().updateMmsDownloadStatus(messageRef, status); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 286 | public Bundle getCarrierConfigValues(int subId) throws RemoteException { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 287 | Slog.d(TAG, "getCarrierConfigValues() by " + getCallingPackageName()); |
Shri Borde | 7237972 | 2014-09-02 09:48:49 -0700 | [diff] [blame] | 288 | return getServiceGuarded().getCarrierConfigValues(subId); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public Uri importTextMessage(String callingPkg, String address, int type, String text, |
| 293 | long timestampMillis, boolean seen, boolean read) throws RemoteException { |
| 294 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Import SMS message"); |
| 295 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 296 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 297 | // Silently fail AppOps failure due to not being the default SMS app |
| 298 | // while writing the TelephonyProvider |
| 299 | return FAKE_SMS_SENT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 300 | } |
| 301 | return getServiceGuarded().importTextMessage( |
| 302 | callingPkg, address, type, text, timestampMillis, seen, read); |
| 303 | } |
| 304 | |
| 305 | @Override |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 306 | public Uri importMultimediaMessage(String callingPkg, Uri contentUri, |
| 307 | String messageId, long timestampSecs, boolean seen, boolean read) |
| 308 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 309 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Import MMS message"); |
| 310 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 311 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 312 | // Silently fail AppOps failure due to not being the default SMS app |
| 313 | // while writing the TelephonyProvider |
| 314 | return FAKE_MMS_SENT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 315 | } |
| 316 | return getServiceGuarded().importMultimediaMessage( |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 317 | callingPkg, contentUri, messageId, timestampSecs, seen, read); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public boolean deleteStoredMessage(String callingPkg, Uri messageUri) |
| 322 | throws RemoteException { |
| 323 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, |
| 324 | "Delete SMS/MMS message"); |
| 325 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 326 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 327 | return false; |
| 328 | } |
| 329 | return getServiceGuarded().deleteStoredMessage(callingPkg, messageUri); |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public boolean deleteStoredConversation(String callingPkg, long conversationId) |
| 334 | throws RemoteException { |
| 335 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Delete conversation"); |
| 336 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 337 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 338 | return false; |
| 339 | } |
| 340 | return getServiceGuarded().deleteStoredConversation(callingPkg, conversationId); |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public boolean updateStoredMessageStatus(String callingPkg, Uri messageUri, |
| 345 | ContentValues statusValues) throws RemoteException { |
| 346 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, |
| 347 | "Update SMS/MMS message"); |
| 348 | return getServiceGuarded() |
| 349 | .updateStoredMessageStatus(callingPkg, messageUri, statusValues); |
| 350 | } |
| 351 | |
| 352 | @Override |
Ye Wen | a3dbd10 | 2014-07-29 10:42:25 -0700 | [diff] [blame] | 353 | public boolean archiveStoredConversation(String callingPkg, long conversationId, |
| 354 | boolean archived) throws RemoteException { |
| 355 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, |
| 356 | "Update SMS/MMS message"); |
| 357 | return getServiceGuarded() |
| 358 | .archiveStoredConversation(callingPkg, conversationId, archived); |
| 359 | } |
| 360 | |
| 361 | @Override |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 362 | public Uri addTextMessageDraft(String callingPkg, String address, String text) |
| 363 | throws RemoteException { |
| 364 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Add SMS draft"); |
| 365 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 366 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 367 | // Silently fail AppOps failure due to not being the default SMS app |
| 368 | // while writing the TelephonyProvider |
| 369 | return FAKE_SMS_DRAFT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 370 | } |
| 371 | return getServiceGuarded().addTextMessageDraft(callingPkg, address, text); |
| 372 | } |
| 373 | |
| 374 | @Override |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 375 | public Uri addMultimediaMessageDraft(String callingPkg, Uri contentUri) |
| 376 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 377 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Add MMS draft"); |
| 378 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 379 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 380 | // Silently fail AppOps failure due to not being the default SMS app |
| 381 | // while writing the TelephonyProvider |
| 382 | return FAKE_MMS_DRAFT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 383 | } |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 384 | return getServiceGuarded().addMultimediaMessageDraft(callingPkg, contentUri); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 388 | public void sendStoredMessage(int subId, String callingPkg, Uri messageUri, |
Ye Wen | 8179c2a | 2014-09-04 15:36:11 -0700 | [diff] [blame] | 389 | Bundle configOverrides, PendingIntent sentIntent) throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 390 | mContext.enforceCallingPermission(Manifest.permission.SEND_SMS, |
| 391 | "Send stored MMS message"); |
| 392 | if (getAppOpsManager().noteOp(AppOpsManager.OP_SEND_SMS, Binder.getCallingUid(), |
| 393 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 394 | return; |
| 395 | } |
Ye Wen | 63c00c4 | 2014-08-01 13:38:58 -0700 | [diff] [blame] | 396 | getServiceGuarded().sendStoredMessage(subId, callingPkg, messageUri, configOverrides, |
| 397 | sentIntent); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public void setAutoPersisting(String callingPkg, boolean enabled) throws RemoteException { |
| 402 | mContext.enforceCallingPermission(Manifest.permission.WRITE_SMS, "Set auto persist"); |
| 403 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 404 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 405 | return; |
| 406 | } |
| 407 | getServiceGuarded().setAutoPersisting(callingPkg, enabled); |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public boolean getAutoPersisting() throws RemoteException { |
| 412 | return getServiceGuarded().getAutoPersisting(); |
| 413 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame^] | 414 | |
| 415 | /** |
| 416 | * Modifies the Uri to contain the caller's userId, if necessary. |
| 417 | * Grants the phone package on primary user permission to access the contentUri, |
| 418 | * even if the caller is not in the primary user. |
| 419 | * |
| 420 | * @param contentUri The Uri to adjust |
| 421 | * @param action The intent action used to find the associated carrier app |
| 422 | * @param permission The permission to add |
| 423 | * @return The adjusted Uri containing the calling userId. |
| 424 | */ |
| 425 | private Uri adjustUriForUserAndGrantPermission(Uri contentUri, String action, |
| 426 | int permission) { |
| 427 | final int callingUserId = UserHandle.getCallingUserId(); |
| 428 | if (callingUserId != UserHandle.USER_OWNER) { |
| 429 | contentUri = ContentProvider.maybeAddUserId(contentUri, callingUserId); |
| 430 | } |
| 431 | long token = Binder.clearCallingIdentity(); |
| 432 | try { |
| 433 | mContext.grantUriPermission(PHONE_PACKAGE_NAME, contentUri, permission); |
| 434 | |
| 435 | // Grant permission for the carrier app. |
| 436 | Intent intent = new Intent(action); |
| 437 | TelephonyManager telephonyManager = |
| 438 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 439 | List<String> carrierPackages = telephonyManager.getCarrierPackageNamesForIntent( |
| 440 | intent); |
| 441 | if (carrierPackages != null && carrierPackages.size() == 1) { |
| 442 | mContext.grantUriPermission(carrierPackages.get(0), contentUri, permission); |
| 443 | } |
| 444 | } finally { |
| 445 | Binder.restoreCallingIdentity(token); |
| 446 | } |
| 447 | return contentUri; |
| 448 | } |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 449 | } |
| 450 | } |