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 | |
Malcolm Chen | e427558 | 2019-12-12 14:36:04 -0800 | [diff] [blame] | 19 | import static android.telephony.SubscriptionManager.INVALID_SIM_SLOT_INDEX; |
| 20 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 21 | import android.Manifest; |
| 22 | import android.app.AppOpsManager; |
| 23 | import android.app.PendingIntent; |
| 24 | import android.content.ComponentName; |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 25 | import android.content.ContentProvider; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 26 | import android.content.ContentValues; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.ServiceConnection; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.net.Uri; |
| 32 | import android.os.Binder; |
Shri Borde | 7237972 | 2014-09-02 09:48:49 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.IBinder; |
| 36 | import android.os.Message; |
| 37 | import android.os.RemoteException; |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 38 | import android.os.SystemClock; |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 39 | import android.os.UserHandle; |
Cheuksan Wang | 5cec920 | 2014-12-16 13:40:36 -0800 | [diff] [blame] | 40 | import android.service.carrier.CarrierMessagingService; |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 41 | import android.telephony.SmsManager; |
Malcolm Chen | e427558 | 2019-12-12 14:36:04 -0800 | [diff] [blame] | 42 | import android.telephony.SubscriptionInfo; |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 43 | import android.telephony.SubscriptionManager; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 44 | import android.telephony.TelephonyManager; |
| 45 | import android.util.Slog; |
| 46 | |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 47 | import com.android.internal.telephony.IMms; |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 48 | import com.android.server.uri.UriGrantsManagerInternal; |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 49 | |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 50 | import java.util.List; |
| 51 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 52 | /** |
| 53 | * This class is a proxy for MmsService APIs. We need this because MmsService runs |
| 54 | * in phone process and may crash anytime. This manages a connection to the actual |
| 55 | * MmsService and bridges the public SMS/MMS APIs with MmsService implementation. |
| 56 | */ |
| 57 | public class MmsServiceBroker extends SystemService { |
| 58 | private static final String TAG = "MmsServiceBroker"; |
| 59 | |
| 60 | private static final ComponentName MMS_SERVICE_COMPONENT = |
| 61 | new ComponentName("com.android.mms.service", "com.android.mms.service.MmsService"); |
| 62 | |
| 63 | private static final int MSG_TRY_CONNECTING = 1; |
| 64 | |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 65 | private static final Uri FAKE_SMS_SENT_URI = Uri.parse("content://sms/sent/0"); |
| 66 | private static final Uri FAKE_MMS_SENT_URI = Uri.parse("content://mms/sent/0"); |
| 67 | private static final Uri FAKE_SMS_DRAFT_URI = Uri.parse("content://sms/draft/0"); |
| 68 | private static final Uri FAKE_MMS_DRAFT_URI = Uri.parse("content://mms/draft/0"); |
| 69 | |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 70 | private static final long SERVICE_CONNECTION_WAIT_TIME_MS = 4 * 1000L; // 4 seconds |
| 71 | private static final long RETRY_DELAY_ON_DISCONNECTION_MS = 3 * 1000L; // 3 seconds |
| 72 | |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 73 | private Context mContext; |
| 74 | // The actual MMS service instance to invoke |
| 75 | private volatile IMms mService; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 76 | |
| 77 | // Cached system service instances |
| 78 | private volatile AppOpsManager mAppOpsManager = null; |
| 79 | private volatile PackageManager mPackageManager = null; |
| 80 | private volatile TelephonyManager mTelephonyManager = null; |
| 81 | |
| 82 | private final Handler mConnectionHandler = new Handler() { |
| 83 | @Override |
| 84 | public void handleMessage(Message msg) { |
| 85 | switch (msg.what) { |
| 86 | case MSG_TRY_CONNECTING: |
| 87 | tryConnecting(); |
| 88 | break; |
| 89 | default: |
| 90 | Slog.e(TAG, "Unknown message"); |
| 91 | } |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | private ServiceConnection mConnection = new ServiceConnection() { |
| 96 | @Override |
| 97 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 98 | Slog.i(TAG, "MmsService connected"); |
| 99 | synchronized (MmsServiceBroker.this) { |
Jeff Sharkey | 0a17db1 | 2016-11-04 11:23:46 -0600 | [diff] [blame] | 100 | mService = IMms.Stub.asInterface(Binder.allowBlocking(service)); |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 101 | MmsServiceBroker.this.notifyAll(); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onServiceDisconnected(ComponentName name) { |
| 107 | Slog.i(TAG, "MmsService unexpectedly disconnected"); |
| 108 | synchronized (MmsServiceBroker.this) { |
| 109 | mService = null; |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 110 | MmsServiceBroker.this.notifyAll(); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 111 | } |
Ye Wen | 724dbbd7 | 2014-10-07 15:33:51 -0700 | [diff] [blame] | 112 | // Retry connecting, but not too eager (with a delay) |
| 113 | // since it may come back by itself. |
| 114 | mConnectionHandler.sendMessageDelayed( |
| 115 | mConnectionHandler.obtainMessage(MSG_TRY_CONNECTING), |
| 116 | RETRY_DELAY_ON_DISCONNECTION_MS); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 117 | } |
| 118 | }; |
| 119 | |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 120 | // Instance of IMms for returning failure to service API caller, |
| 121 | // used when MmsService cannot be connected. |
| 122 | private final IMms mServiceStubForFailure = new IMms() { |
| 123 | |
| 124 | @Override |
| 125 | public IBinder asBinder() { |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void sendMessage(int subId, String callingPkg, Uri contentUri, String locationUrl, |
Tom Taylor | 03079ec | 2020-01-24 10:21:50 -0800 | [diff] [blame] | 131 | Bundle configOverrides, PendingIntent sentIntent, long messageId) |
| 132 | throws RemoteException { |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 133 | returnPendingIntentWithError(sentIntent); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void downloadMessage(int subId, String callingPkg, String locationUrl, |
Tom Taylor | 03079ec | 2020-01-24 10:21:50 -0800 | [diff] [blame] | 138 | Uri contentUri, Bundle configOverrides, PendingIntent downloadedIntent, |
| 139 | long messageId) |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 140 | throws RemoteException { |
| 141 | returnPendingIntentWithError(downloadedIntent); |
| 142 | } |
| 143 | |
| 144 | @Override |
Ye Wen | 61c8d23 | 2015-04-01 11:27:14 -0700 | [diff] [blame] | 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, |
Tom Taylor | 03079ec | 2020-01-24 10:21:50 -0800 | [diff] [blame] | 334 | String locationUrl, Bundle configOverrides, PendingIntent sentIntent, |
| 335 | long messageId) |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 336 | throws RemoteException { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 337 | Slog.d(TAG, "sendMessage() by " + callingPkg); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 338 | mContext.enforceCallingPermission(Manifest.permission.SEND_SMS, "Send MMS message"); |
| 339 | if (getAppOpsManager().noteOp(AppOpsManager.OP_SEND_SMS, Binder.getCallingUid(), |
| 340 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Sahin Caliskan | 7c766b2 | 2018-10-30 10:20:08 -0700 | [diff] [blame] | 341 | Slog.e(TAG, callingPkg + " is not allowed to call sendMessage()"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 342 | return; |
| 343 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 344 | contentUri = adjustUriForUserAndGrantPermission(contentUri, |
Cheuksan Wang | 5cec920 | 2014-12-16 13:40:36 -0800 | [diff] [blame] | 345 | CarrierMessagingService.SERVICE_INTERFACE, |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 346 | Intent.FLAG_GRANT_READ_URI_PERMISSION, |
| 347 | subId); |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 348 | getServiceGuarded().sendMessage(subId, callingPkg, contentUri, locationUrl, |
Tom Taylor | 03079ec | 2020-01-24 10:21:50 -0800 | [diff] [blame] | 349 | configOverrides, sentIntent, messageId); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 353 | public void downloadMessage(int subId, String callingPkg, String locationUrl, |
Ye Wen | 8179c2a | 2014-09-04 15:36:11 -0700 | [diff] [blame] | 354 | Uri contentUri, Bundle configOverrides, |
Tom Taylor | 03079ec | 2020-01-24 10:21:50 -0800 | [diff] [blame] | 355 | PendingIntent downloadedIntent, long messageId) throws RemoteException { |
Ye Wen | bdc3a46 | 2014-11-11 11:17:28 -0800 | [diff] [blame] | 356 | Slog.d(TAG, "downloadMessage() by " + callingPkg); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 357 | mContext.enforceCallingPermission(Manifest.permission.RECEIVE_MMS, |
| 358 | "Download MMS message"); |
| 359 | if (getAppOpsManager().noteOp(AppOpsManager.OP_RECEIVE_MMS, Binder.getCallingUid(), |
| 360 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Sahin Caliskan | 7c766b2 | 2018-10-30 10:20:08 -0700 | [diff] [blame] | 361 | Slog.e(TAG, callingPkg + " is not allowed to call downloadMessage()"); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 362 | return; |
| 363 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 364 | contentUri = adjustUriForUserAndGrantPermission(contentUri, |
Cheuksan Wang | 5cec920 | 2014-12-16 13:40:36 -0800 | [diff] [blame] | 365 | CarrierMessagingService.SERVICE_INTERFACE, |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 366 | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION, |
| 367 | subId); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 368 | |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 369 | getServiceGuarded().downloadMessage(subId, callingPkg, locationUrl, contentUri, |
Tom Taylor | 03079ec | 2020-01-24 10:21:50 -0800 | [diff] [blame] | 370 | configOverrides, downloadedIntent, messageId); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 374 | public Uri importTextMessage(String callingPkg, String address, int type, String text, |
| 375 | long timestampMillis, boolean seen, boolean read) throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 376 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 377 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 378 | // Silently fail AppOps failure due to not being the default SMS app |
| 379 | // while writing the TelephonyProvider |
| 380 | return FAKE_SMS_SENT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 381 | } |
| 382 | return getServiceGuarded().importTextMessage( |
| 383 | callingPkg, address, type, text, timestampMillis, seen, read); |
| 384 | } |
| 385 | |
| 386 | @Override |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 387 | public Uri importMultimediaMessage(String callingPkg, Uri contentUri, |
| 388 | String messageId, long timestampSecs, boolean seen, boolean read) |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 389 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 390 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 391 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 392 | // Silently fail AppOps failure due to not being the default SMS app |
| 393 | // while writing the TelephonyProvider |
| 394 | return FAKE_MMS_SENT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 395 | } |
| 396 | return getServiceGuarded().importMultimediaMessage( |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 397 | callingPkg, contentUri, messageId, timestampSecs, seen, read); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | @Override |
| 401 | public boolean deleteStoredMessage(String callingPkg, Uri messageUri) |
| 402 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 403 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 404 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 405 | return false; |
| 406 | } |
| 407 | return getServiceGuarded().deleteStoredMessage(callingPkg, messageUri); |
| 408 | } |
| 409 | |
| 410 | @Override |
| 411 | public boolean deleteStoredConversation(String callingPkg, long conversationId) |
| 412 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 413 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 414 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 415 | return false; |
| 416 | } |
| 417 | return getServiceGuarded().deleteStoredConversation(callingPkg, conversationId); |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public boolean updateStoredMessageStatus(String callingPkg, Uri messageUri, |
| 422 | ContentValues statusValues) throws RemoteException { |
Svetoslav | 6c58957 | 2015-04-16 16:19:24 -0700 | [diff] [blame] | 423 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 424 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 425 | return false; |
| 426 | } |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 427 | return getServiceGuarded() |
| 428 | .updateStoredMessageStatus(callingPkg, messageUri, statusValues); |
| 429 | } |
| 430 | |
| 431 | @Override |
Ye Wen | a3dbd10 | 2014-07-29 10:42:25 -0700 | [diff] [blame] | 432 | public boolean archiveStoredConversation(String callingPkg, long conversationId, |
| 433 | boolean archived) throws RemoteException { |
Svetoslav | 6c58957 | 2015-04-16 16:19:24 -0700 | [diff] [blame] | 434 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 435 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 436 | return false; |
| 437 | } |
Ye Wen | a3dbd10 | 2014-07-29 10:42:25 -0700 | [diff] [blame] | 438 | return getServiceGuarded() |
| 439 | .archiveStoredConversation(callingPkg, conversationId, archived); |
| 440 | } |
| 441 | |
| 442 | @Override |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 443 | public Uri addTextMessageDraft(String callingPkg, String address, String text) |
| 444 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 445 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 446 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 447 | // Silently fail AppOps failure due to not being the default SMS app |
| 448 | // while writing the TelephonyProvider |
| 449 | return FAKE_SMS_DRAFT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 450 | } |
| 451 | return getServiceGuarded().addTextMessageDraft(callingPkg, address, text); |
| 452 | } |
| 453 | |
| 454 | @Override |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 455 | public Uri addMultimediaMessageDraft(String callingPkg, Uri contentUri) |
| 456 | throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 457 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 458 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
Ye Wen | fa58ac0 | 2014-07-31 17:15:30 -0700 | [diff] [blame] | 459 | // Silently fail AppOps failure due to not being the default SMS app |
| 460 | // while writing the TelephonyProvider |
| 461 | return FAKE_MMS_DRAFT_URI; |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 462 | } |
Julian Odell | 31ef14d | 2014-08-25 17:53:52 -0700 | [diff] [blame] | 463 | return getServiceGuarded().addMultimediaMessageDraft(callingPkg, contentUri); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | @Override |
Wink Saville | 63f03dd | 2014-10-23 10:44:45 -0700 | [diff] [blame] | 467 | public void sendStoredMessage(int subId, String callingPkg, Uri messageUri, |
Ye Wen | 8179c2a | 2014-09-04 15:36:11 -0700 | [diff] [blame] | 468 | Bundle configOverrides, PendingIntent sentIntent) throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 469 | if (getAppOpsManager().noteOp(AppOpsManager.OP_SEND_SMS, Binder.getCallingUid(), |
| 470 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 471 | return; |
| 472 | } |
Ye Wen | 63c00c4 | 2014-08-01 13:38:58 -0700 | [diff] [blame] | 473 | getServiceGuarded().sendStoredMessage(subId, callingPkg, messageUri, configOverrides, |
| 474 | sentIntent); |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @Override |
| 478 | public void setAutoPersisting(String callingPkg, boolean enabled) throws RemoteException { |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 479 | if (getAppOpsManager().noteOp(AppOpsManager.OP_WRITE_SMS, Binder.getCallingUid(), |
| 480 | callingPkg) != AppOpsManager.MODE_ALLOWED) { |
| 481 | return; |
| 482 | } |
| 483 | getServiceGuarded().setAutoPersisting(callingPkg, enabled); |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public boolean getAutoPersisting() throws RemoteException { |
| 488 | return getServiceGuarded().getAutoPersisting(); |
| 489 | } |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 490 | |
| 491 | /** |
| 492 | * Modifies the Uri to contain the caller's userId, if necessary. |
| 493 | * Grants the phone package on primary user permission to access the contentUri, |
| 494 | * even if the caller is not in the primary user. |
| 495 | * |
| 496 | * @param contentUri The Uri to adjust |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 497 | * @param action The intent action used to find the associated carrier app |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 498 | * @param permission The permission to add |
| 499 | * @return The adjusted Uri containing the calling userId. |
| 500 | */ |
| 501 | private Uri adjustUriForUserAndGrantPermission(Uri contentUri, String action, |
Leland Miller | 02c8524 | 2019-07-08 15:31:02 -0700 | [diff] [blame] | 502 | int permission, int subId) { |
Jeff Sharkey | 7ff418d | 2016-11-30 14:29:59 -0700 | [diff] [blame] | 503 | final Intent grantIntent = new Intent(); |
| 504 | grantIntent.setData(contentUri); |
| 505 | grantIntent.setFlags(permission); |
| 506 | |
| 507 | final int callingUid = Binder.getCallingUid(); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 508 | final int callingUserId = UserHandle.getCallingUserId(); |
Xiaohui Chen | 7c69636 | 2015-09-16 09:56:14 -0700 | [diff] [blame] | 509 | if (callingUserId != UserHandle.USER_SYSTEM) { |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 510 | contentUri = ContentProvider.maybeAddUserId(contentUri, callingUserId); |
| 511 | } |
Jeff Sharkey | 7ff418d | 2016-11-30 14:29:59 -0700 | [diff] [blame] | 512 | |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 513 | long token = Binder.clearCallingIdentity(); |
| 514 | try { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 515 | LocalServices.getService(UriGrantsManagerInternal.class) |
Jeff Sharkey | 7ff418d | 2016-11-30 14:29:59 -0700 | [diff] [blame] | 516 | .grantUriPermissionFromIntent(callingUid, PHONE_PACKAGE_NAME, |
| 517 | grantIntent, UserHandle.USER_SYSTEM); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 518 | |
| 519 | // Grant permission for the carrier app. |
| 520 | Intent intent = new Intent(action); |
Malcolm Chen | e427558 | 2019-12-12 14:36:04 -0800 | [diff] [blame] | 521 | TelephonyManager telephonyManager = (TelephonyManager) |
| 522 | mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 523 | List<String> carrierPackages = telephonyManager |
| 524 | .getCarrierPackageNamesForIntentAndPhone( |
| 525 | intent, getPhoneIdFromSubId(subId)); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 526 | if (carrierPackages != null && carrierPackages.size() == 1) { |
Wale Ogunwale | 6d50dcc | 2018-07-21 23:00:40 -0700 | [diff] [blame] | 527 | LocalServices.getService(UriGrantsManagerInternal.class) |
Jeff Sharkey | 7ff418d | 2016-11-30 14:29:59 -0700 | [diff] [blame] | 528 | .grantUriPermissionFromIntent(callingUid, carrierPackages.get(0), |
| 529 | grantIntent, UserHandle.USER_SYSTEM); |
Tom Taylor | 86201db | 2014-11-24 09:36:43 -0800 | [diff] [blame] | 530 | } |
| 531 | } finally { |
| 532 | Binder.restoreCallingIdentity(token); |
| 533 | } |
| 534 | return contentUri; |
| 535 | } |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 536 | } |
Malcolm Chen | e427558 | 2019-12-12 14:36:04 -0800 | [diff] [blame] | 537 | |
| 538 | private int getPhoneIdFromSubId(int subId) { |
| 539 | SubscriptionManager subManager = (SubscriptionManager) |
| 540 | mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); |
| 541 | if (subManager == null) return INVALID_SIM_SLOT_INDEX; |
| 542 | SubscriptionInfo info = subManager.getActiveSubscriptionInfo(subId); |
| 543 | if (info == null) return INVALID_SIM_SLOT_INDEX; |
| 544 | return info.getSimSlotIndex(); |
| 545 | } |
Ye Wen | d97e1fd | 2014-07-24 12:56:45 -0700 | [diff] [blame] | 546 | } |