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; |
Cheuksan Wang | 5cec920 | 2014-12-16 13:40:36 -0800 | [diff] [blame] | 38 | import android.service.carrier.CarrierMessagingService; |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 39 | import android.telephony.SmsManager; |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 40 | import android.telephony.SubscriptionManager; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 41 | import android.telephony.TelephonyManager; |
| 42 | import android.util.Slog; |
| 43 | |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 44 | import com.android.internal.telephony.IMms; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 45 | import com.android.server.uri.UriGrantsManagerInternal; |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 46 | |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 47 | import java.util.List; |
| 48 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 49 | /** |
| 50 | * This class is a proxy for MmsService APIs. We need this because MmsService runs |
| 51 | * in phone process and may crash anytime. This manages a connection to the actual |
| 52 | * MmsService and bridges the public SMS/MMS APIs with MmsService implementation. |
| 53 | */ |
| 54 | public class MmsServiceBroker extends SystemService { |
| 55 | private static final String TAG = "MmsServiceBroker"; |
| 56 | |
| 57 | private static final ComponentName MMS_SERVICE_COMPONENT = |
| 58 | new ComponentName("com.android.mms.service", "com.android.mms.service.MmsService"); |
| 59 | |
| 60 | private static final int MSG_TRY_CONNECTING = 1; |
| 61 | |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 62 | private static final Uri FAKE_SMS_SENT_URI = Uri.parse("content://sms/sent/0"); |
| 63 | private static final Uri FAKE_MMS_SENT_URI = Uri.parse("content://mms/sent/0"); |
| 64 | private static final Uri FAKE_SMS_DRAFT_URI = Uri.parse("content://sms/draft/0"); |
| 65 | private static final Uri FAKE_MMS_DRAFT_URI = Uri.parse("content://mms/draft/0"); |
| 66 | |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 67 | private static final long SERVICE_CONNECTION_WAIT_TIME_MS = 4 * 1000L; // 4 seconds |
| 68 | private static final long RETRY_DELAY_ON_DISCONNECTION_MS = 3 * 1000L; // 3 seconds |
| 69 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 70 | private Context mContext; |
| 71 | // The actual MMS service instance to invoke |
| 72 | private volatile IMms mService; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 73 | |
| 74 | // Cached system service instances |
| 75 | private volatile AppOpsManager mAppOpsManager = null; |
| 76 | private volatile PackageManager mPackageManager = null; |
| 77 | private volatile TelephonyManager mTelephonyManager = null; |
| 78 | |
| 79 | private final Handler mConnectionHandler = new Handler() { |
| 80 | @Override |
| 81 | public void handleMessage(Message msg) { |
| 82 | switch (msg.what) { |
| 83 | case MSG_TRY_CONNECTING: |
| 84 | tryConnecting(); |
| 85 | break; |
| 86 | default: |
| 87 | Slog.e(TAG, "Unknown message"); |
| 88 | } |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | private ServiceConnection mConnection = new ServiceConnection() { |
| 93 | @Override |
| 94 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 95 | Slog.i(TAG, "MmsService connected"); |
| 96 | synchronized (MmsServiceBroker.this) { |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 97 | mService = IMms.Stub.asInterface(Binder.allowBlocking(service)); |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 98 | MmsServiceBroker.this.notifyAll(); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void onServiceDisconnected(ComponentName name) { |
| 104 | Slog.i(TAG, "MmsService unexpectedly disconnected"); |
| 105 | synchronized (MmsServiceBroker.this) { |
| 106 | mService = null; |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 107 | MmsServiceBroker.this.notifyAll(); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 108 | } |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 109 | // Retry connecting, but not too eager (with a delay) |
| 110 | // since it may come back by itself. |
| 111 | mConnectionHandler.sendMessageDelayed( |
| 112 | mConnectionHandler.obtainMessage(MSG_TRY_CONNECTING), |
| 113 | RETRY_DELAY_ON_DISCONNECTION_MS); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 114 | } |
| 115 | }; |
| 116 | |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 117 | // Instance of IMms for returning failure to service API caller, |
| 118 | // used when MmsService cannot be connected. |
| 119 | private final IMms mServiceStubForFailure = new IMms() { |
| 120 | |
| 121 | @Override |
| 122 | public IBinder asBinder() { |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void sendMessage(int subId, String callingPkg, Uri contentUri, String locationUrl, |
| 128 | Bundle configOverrides, PendingIntent sentIntent) throws RemoteException { |
| 129 | returnPendingIntentWithError(sentIntent); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void downloadMessage(int subId, String callingPkg, String locationUrl, |
| 134 | Uri contentUri, Bundle configOverrides, PendingIntent downloadedIntent) |
| 135 | throws RemoteException { |
| 136 | returnPendingIntentWithError(downloadedIntent); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public Bundle getCarrierConfigValues(int subId) throws RemoteException { |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Uri importTextMessage(String callingPkg, String address, int type, String text, |
| 146 | long timestampMillis, boolean seen, boolean read) throws RemoteException { |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Uri importMultimediaMessage(String callingPkg, Uri contentUri, String messageId, |
| 152 | long timestampSecs, boolean seen, boolean read) throws RemoteException { |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public boolean deleteStoredMessage(String callingPkg, Uri messageUri) |
| 158 | throws RemoteException { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public boolean deleteStoredConversation(String callingPkg, long conversationId) |
| 164 | throws RemoteException { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean updateStoredMessageStatus(String callingPkg, Uri messageUri, |
| 170 | ContentValues statusValues) throws RemoteException { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public boolean archiveStoredConversation(String callingPkg, long conversationId, |
| 176 | boolean archived) throws RemoteException { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Uri addTextMessageDraft(String callingPkg, String address, String text) |
| 182 | throws RemoteException { |
| 183 | return null; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Uri addMultimediaMessageDraft(String callingPkg, Uri contentUri) |
| 188 | throws RemoteException { |
| 189 | return null; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void sendStoredMessage(int subId, String callingPkg, Uri messageUri, |
| 194 | Bundle configOverrides, PendingIntent sentIntent) throws RemoteException { |
| 195 | returnPendingIntentWithError(sentIntent); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void setAutoPersisting(String callingPkg, boolean enabled) throws RemoteException { |
| 200 | // Do nothing |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public boolean getAutoPersisting() throws RemoteException { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | private void returnPendingIntentWithError(PendingIntent pendingIntent) { |
| 209 | try { |
| 210 | pendingIntent.send(mContext, SmsManager.MMS_ERROR_UNSPECIFIED, null); |
| 211 | } catch (PendingIntent.CanceledException e) { |
| 212 | Slog.e(TAG, "Failed to return pending intent result", e); |
| 213 | } |
| 214 | } |
| 215 | }; |
| 216 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 217 | public MmsServiceBroker(Context context) { |
| 218 | super(context); |
| 219 | mContext = context; |
| 220 | mService = null; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void onStart() { |
| 225 | publishBinderService("imms", new BinderService()); |
| 226 | } |
| 227 | |
| 228 | public void systemRunning() { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 229 | Slog.i(TAG, "Delay connecting to MmsService until an API is called"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | private void tryConnecting() { |
| 233 | Slog.i(TAG, "Connecting to MmsService"); |
| 234 | synchronized (this) { |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 235 | if (mService != null) { |
| 236 | Slog.d(TAG, "Already connected"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | final Intent intent = new Intent(); |
| 240 | intent.setComponent(MMS_SERVICE_COMPONENT); |
| 241 | try { |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 242 | if (!mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE)) { |
| 243 | Slog.e(TAG, "Failed to bind to MmsService"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 244 | } |
| 245 | } catch (SecurityException e) { |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 246 | Slog.e(TAG, "Forbidden to bind to MmsService", e); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 251 | private IMms getOrConnectService() { |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 252 | synchronized (this) { |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 253 | if (mService != null) { |
| 254 | return mService; |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 255 | } |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 256 | // Service is not connected. Try blocking connecting. |
| 257 | Slog.w(TAG, "MmsService not connected. Try connecting..."); |
| 258 | mConnectionHandler.sendMessage( |
| 259 | mConnectionHandler.obtainMessage(MSG_TRY_CONNECTING)); |
| 260 | final long shouldEnd = |
| 261 | SystemClock.elapsedRealtime() + SERVICE_CONNECTION_WAIT_TIME_MS; |
| 262 | long waitTime = SERVICE_CONNECTION_WAIT_TIME_MS; |
| 263 | while (waitTime > 0) { |
| 264 | try { |
| 265 | // TODO: consider using Java concurrent construct instead of raw object wait |
| 266 | this.wait(waitTime); |
| 267 | } catch (InterruptedException e) { |
| 268 | Slog.w(TAG, "Connection wait interrupted", e); |
| 269 | } |
| 270 | if (mService != null) { |
| 271 | // Success |
| 272 | return mService; |
| 273 | } |
| 274 | // Calculate remaining waiting time to make sure we wait the full timeout period |
| 275 | waitTime = shouldEnd - SystemClock.elapsedRealtime(); |
| 276 | } |
| 277 | // Timed out. Something's really wrong. |
| 278 | Slog.e(TAG, "Can not connect to MmsService (timed out)"); |
| 279 | return null; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 284 | * Make sure to return a non-empty service instance. Return the connected MmsService |
| 285 | * instance, if not connected, try connecting. If fail to connect, return a fake service |
| 286 | * instance which returns failure to service caller. |
| 287 | * |
| 288 | * @return a non-empty service instance, real or fake |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 289 | */ |
| 290 | private IMms getServiceGuarded() { |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 291 | final IMms service = getOrConnectService(); |
| 292 | if (service != null) { |
| 293 | return service; |
| 294 | } |
| 295 | return mServiceStubForFailure; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | private AppOpsManager getAppOpsManager() { |
| 299 | if (mAppOpsManager == null) { |
| 300 | mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
| 301 | } |
| 302 | return mAppOpsManager; |
| 303 | } |
| 304 | |
| 305 | private PackageManager getPackageManager() { |
| 306 | if (mPackageManager == null) { |
| 307 | mPackageManager = mContext.getPackageManager(); |
| 308 | } |
| 309 | return mPackageManager; |
| 310 | } |
| 311 | |
| 312 | private TelephonyManager getTelephonyManager() { |
| 313 | if (mTelephonyManager == null) { |
| 314 | mTelephonyManager = (TelephonyManager) mContext.getSystemService( |
| 315 | Context.TELEPHONY_SERVICE); |
| 316 | } |
| 317 | return mTelephonyManager; |
| 318 | } |
| 319 | |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 320 | private String getCallingPackageName() { |
| 321 | final String[] packages = getPackageManager().getPackagesForUid(Binder.getCallingUid()); |
| 322 | if (packages != null && packages.length > 0) { |
| 323 | return packages[0]; |
| 324 | } |
| 325 | return "unknown"; |
| 326 | } |
| 327 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 328 | // Service API calls implementation, proxied to the real MmsService in "com.android.mms.service" |
| 329 | private final class BinderService extends IMms.Stub { |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 330 | private static final String PHONE_PACKAGE_NAME = "com.android.phone"; |
| 331 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 332 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 333 | public void sendMessage(int subId, String callingPkg, Uri contentUri, |
Ye Wen | 8179c2a | 2014-09-04 15:36:11 -0700 | [diff] [blame] | 334 | String locationUrl, Bundle configOverrides, PendingIntent sentIntent) |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 335 | throws RemoteException { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 336 | Slog.d(TAG, "sendMessage() by " + callingPkg); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 337 | mContext.enforceCallingPermission(Manifest.permission.SEND_SMS, "Send MMS message"); |
| 338 | if (getAppOpsManager().noteOp(AppOpsManager.OP_SEND_SMS, Binder.getCallingUid(), |
| 339 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Sahin Caliskan | 7c766b2 | 2018-10-30 10:20:08 -0700 | [diff] [blame] | 340 | Slog.e(TAG, callingPkg + " is not allowed to call sendMessage()"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 341 | return; |
| 342 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 343 | contentUri = adjustUriForUserAndGrantPermission(contentUri, |
Cheuksan Wang | 5cec920 | 2014-12-16 13:40:36 -0800 | [diff] [blame] | 344 | CarrierMessagingService.SERVICE_INTERFACE, |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 345 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 346 | subId); |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 347 | getServiceGuarded().sendMessage(subId, callingPkg, contentUri, locationUrl, |
| 348 | configOverrides, sentIntent); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 352 | public void downloadMessage(int subId, String callingPkg, String locationUrl, |
Ye Wen | 8179c2a | 2014-09-04 15:36:11 -0700 | [diff] [blame] | 353 | Uri contentUri, Bundle configOverrides, |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 354 | PendingIntent downloadedIntent) throws RemoteException { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 355 | Slog.d(TAG, "downloadMessage() by " + callingPkg); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 356 | mContext.enforceCallingPermission(Manifest.permission.RECEIVE_MMS, |
| 357 | "Download MMS message"); |
| 358 | if (getAppOpsManager().noteOp(AppOpsManager.OP_RECEIVE_MMS, Binder.getCallingUid(), |
| 359 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Sahin Caliskan | 7c766b2 | 2018-10-30 10:20:08 -0700 | [diff] [blame] | 360 | Slog.e(TAG, callingPkg + " is not allowed to call downloadMessage()"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 361 | return; |
| 362 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 363 | contentUri = adjustUriForUserAndGrantPermission(contentUri, |
Cheuksan Wang | 5cec920 | 2014-12-16 13:40:36 -0800 | [diff] [blame] | 364 | CarrierMessagingService.SERVICE_INTERFACE, |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 365 | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION, |
| 366 | subId); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 367 | |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 368 | getServiceGuarded().downloadMessage(subId, callingPkg, locationUrl, contentUri, |
| 369 | configOverrides, downloadedIntent); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 373 | public Bundle getCarrierConfigValues(int subId) throws RemoteException { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 374 | Slog.d(TAG, "getCarrierConfigValues() by " + getCallingPackageName()); |
Shri Borde | 7237972 | 2014-09-02 09:48:49 -0700 | [diff] [blame] | 375 | return getServiceGuarded().getCarrierConfigValues(subId); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public Uri importTextMessage(String callingPkg, String address, int type, String text, |
| 380 | long timestampMillis, boolean seen, boolean read) throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 381 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 382 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 383 | // Silently fail AppOps failure due to not being the default SMS app |
| 384 | // while writing the TelephonyProvider |
| 385 | return FAKE_SMS_SENT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 386 | } |
| 387 | return getServiceGuarded().importTextMessage( |
| 388 | callingPkg, address, type, text, timestampMillis, seen, read); |
| 389 | } |
| 390 | |
| 391 | @Override |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 392 | public Uri importMultimediaMessage(String callingPkg, Uri contentUri, |
| 393 | String messageId, long timestampSecs, boolean seen, boolean read) |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 394 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 395 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 396 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 397 | // Silently fail AppOps failure due to not being the default SMS app |
| 398 | // while writing the TelephonyProvider |
| 399 | return FAKE_MMS_SENT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 400 | } |
| 401 | return getServiceGuarded().importMultimediaMessage( |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 402 | callingPkg, contentUri, messageId, timestampSecs, seen, read); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public boolean deleteStoredMessage(String callingPkg, Uri messageUri) |
| 407 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 408 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 409 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 410 | return false; |
| 411 | } |
| 412 | return getServiceGuarded().deleteStoredMessage(callingPkg, messageUri); |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public boolean deleteStoredConversation(String callingPkg, long conversationId) |
| 417 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 418 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 419 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 420 | return false; |
| 421 | } |
| 422 | return getServiceGuarded().deleteStoredConversation(callingPkg, conversationId); |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public boolean updateStoredMessageStatus(String callingPkg, Uri messageUri, |
| 427 | ContentValues statusValues) throws RemoteException { |
Svetoslav | 6c58957 | 2015-04-16 16:19:24 -0700 | [diff] [blame] | 428 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 429 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 430 | return false; |
| 431 | } |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 432 | return getServiceGuarded() |
| 433 | .updateStoredMessageStatus(callingPkg, messageUri, statusValues); |
| 434 | } |
| 435 | |
| 436 | @Override |
Ye Wen | a3dbd10 | 2014-07-29 10:42:25 -0700 | [diff] [blame] | 437 | public boolean archiveStoredConversation(String callingPkg, long conversationId, |
| 438 | boolean archived) throws RemoteException { |
Svetoslav | 6c58957 | 2015-04-16 16:19:24 -0700 | [diff] [blame] | 439 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 440 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 441 | return false; |
| 442 | } |
Ye Wen | a3dbd10 | 2014-07-29 10:42:25 -0700 | [diff] [blame] | 443 | return getServiceGuarded() |
| 444 | .archiveStoredConversation(callingPkg, conversationId, archived); |
| 445 | } |
| 446 | |
| 447 | @Override |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 448 | public Uri addTextMessageDraft(String callingPkg, String address, String text) |
| 449 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 450 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 451 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 452 | // Silently fail AppOps failure due to not being the default SMS app |
| 453 | // while writing the TelephonyProvider |
| 454 | return FAKE_SMS_DRAFT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 455 | } |
| 456 | return getServiceGuarded().addTextMessageDraft(callingPkg, address, text); |
| 457 | } |
| 458 | |
| 459 | @Override |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 460 | public Uri addMultimediaMessageDraft(String callingPkg, Uri contentUri) |
| 461 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 462 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 463 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 464 | // Silently fail AppOps failure due to not being the default SMS app |
| 465 | // while writing the TelephonyProvider |
| 466 | return FAKE_MMS_DRAFT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 467 | } |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 468 | return getServiceGuarded().addMultimediaMessageDraft(callingPkg, contentUri); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 472 | public void sendStoredMessage(int subId, String callingPkg, Uri messageUri, |
Ye Wen | 8179c2a | 2014-09-04 15:36:11 -0700 | [diff] [blame] | 473 | Bundle configOverrides, PendingIntent sentIntent) throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 474 | if (getAppOpsManager().noteOp(AppOpsManager.OP_SEND_SMS, Binder.getCallingUid(), |
| 475 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 476 | return; |
| 477 | } |
Ye Wen | 63c00c4 | 2014-08-01 13:38:58 -0700 | [diff] [blame] | 478 | getServiceGuarded().sendStoredMessage(subId, callingPkg, messageUri, configOverrides, |
| 479 | sentIntent); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public void setAutoPersisting(String callingPkg, boolean enabled) throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 484 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 485 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 486 | return; |
| 487 | } |
| 488 | getServiceGuarded().setAutoPersisting(callingPkg, enabled); |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public boolean getAutoPersisting() throws RemoteException { |
| 493 | return getServiceGuarded().getAutoPersisting(); |
| 494 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 495 | |
| 496 | /** |
| 497 | * Modifies the Uri to contain the caller's userId, if necessary. |
| 498 | * Grants the phone package on primary user permission to access the contentUri, |
| 499 | * even if the caller is not in the primary user. |
| 500 | * |
| 501 | * @param contentUri The Uri to adjust |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 502 | * @param action The intent action used to find the associated carrier app |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 503 | * @param permission The permission to add |
| 504 | * @return The adjusted Uri containing the calling userId. |
| 505 | */ |
| 506 | private Uri adjustUriForUserAndGrantPermission(Uri contentUri, String action, |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 507 | int permission, int subId) { |
Jeff Sharkey | 7ff418d | 2016-11-30 14:29:59 -0700 | [diff] [blame] | 508 | final Intent grantIntent = new Intent(); |
| 509 | grantIntent.setData(contentUri); |
| 510 | grantIntent.setFlags(permission); |
| 511 | |
| 512 | final int callingUid = Binder.getCallingUid(); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 513 | final int callingUserId = UserHandle.getCallingUserId(); |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 514 | if (callingUserId != UserHandle.USER_SYSTEM) { |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 515 | contentUri = ContentProvider.maybeAddUserId(contentUri, callingUserId); |
| 516 | } |
Jeff Sharkey | 7ff418d | 2016-11-30 14:29:59 -0700 | [diff] [blame] | 517 | |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 518 | long token = Binder.clearCallingIdentity(); |
| 519 | try { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 520 | LocalServices.getService(UriGrantsManagerInternal.class) |
Jeff Sharkey | 7ff418d | 2016-11-30 14:29:59 -0700 | [diff] [blame] | 521 | .grantUriPermissionFromIntent(callingUid, PHONE_PACKAGE_NAME, |
| 522 | grantIntent, UserHandle.USER_SYSTEM); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 523 | |
| 524 | // Grant permission for the carrier app. |
| 525 | Intent intent = new Intent(action); |
| 526 | TelephonyManager telephonyManager = |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 527 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 528 | List<String> carrierPackages = |
| 529 | telephonyManager.getCarrierPackageNamesForIntentAndPhone( |
| 530 | intent, SubscriptionManager.getPhoneId(subId)); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 531 | if (carrierPackages != null && carrierPackages.size() == 1) { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 532 | LocalServices.getService(UriGrantsManagerInternal.class) |
Jeff Sharkey | 7ff418d | 2016-11-30 14:29:59 -0700 | [diff] [blame] | 533 | .grantUriPermissionFromIntent(callingUid, carrierPackages.get(0), |
| 534 | grantIntent, UserHandle.USER_SYSTEM); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 535 | } |
| 536 | } finally { |
| 537 | Binder.restoreCallingIdentity(token); |
| 538 | } |
| 539 | return contentUri; |
| 540 | } |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 541 | } |
| 542 | } |