The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 android.app; |
| 18 | |
Vinit Deshapnde | 011e1b3 | 2014-05-07 21:09:11 -0700 | [diff] [blame] | 19 | import android.net.wifi.IWifiScanner; |
| 20 | import android.net.wifi.WifiScanner; |
Dianne Hackborn | ff32f35 | 2013-07-18 17:31:13 -0700 | [diff] [blame] | 21 | import android.os.Build; |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 22 | |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 23 | import android.service.persistentdata.IPersistentDataBlockService; |
| 24 | import android.service.persistentdata.PersistentDataBlockManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 25 | import com.android.internal.policy.PolicyManager; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 26 | import com.android.internal.util.Preconditions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 28 | import android.bluetooth.BluetoothManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
| 30 | import android.content.ComponentName; |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 31 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.ContentResolver; |
| 33 | import android.content.Context; |
| 34 | import android.content.ContextWrapper; |
| 35 | import android.content.IContentProvider; |
| 36 | import android.content.Intent; |
| 37 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 38 | import android.content.IIntentReceiver; |
| 39 | import android.content.IntentSender; |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 40 | import android.content.IRestrictionsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.ReceiverCallNotAllowedException; |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 42 | import android.content.RestrictionsManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.content.ServiceConnection; |
| 44 | import android.content.SharedPreferences; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 46 | import android.content.pm.ILauncherApps; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.content.pm.IPackageManager; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 48 | import android.content.pm.LauncherApps; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 50 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.content.res.AssetManager; |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 52 | import android.content.res.CompatibilityInfo; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 53 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.content.res.Resources; |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 55 | import android.database.DatabaseErrorHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.database.sqlite.SQLiteDatabase; |
| 57 | import android.database.sqlite.SQLiteDatabase.CursorFactory; |
| 58 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.graphics.drawable.Drawable; |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 60 | import android.hardware.ConsumerIrManager; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 61 | import android.hardware.ISerialManager; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 62 | import android.hardware.SerialManager; |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 63 | import android.hardware.SystemSensorManager; |
Jinsuk Kim | 7f01f8e | 2014-05-29 17:10:39 +0900 | [diff] [blame] | 64 | import android.hardware.hdmi.HdmiControlManager; |
Jinsuk Kim | 7f01f8e | 2014-05-29 17:10:39 +0900 | [diff] [blame] | 65 | import android.hardware.hdmi.IHdmiControlService; |
Eino-Ville Talvala | 2f1a2e4 | 2013-07-25 17:12:05 -0700 | [diff] [blame] | 66 | import android.hardware.camera2.CameraManager; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 67 | import android.hardware.display.DisplayManager; |
Jeff Brown | 9df6e7a | 2012-04-05 11:49:26 -0700 | [diff] [blame] | 68 | import android.hardware.input.InputManager; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 69 | import android.hardware.usb.IUsbManager; |
| 70 | import android.hardware.usb.UsbManager; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 71 | import android.location.CountryDetector; |
| 72 | import android.location.ICountryDetector; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.location.ILocationManager; |
| 74 | import android.location.LocationManager; |
| 75 | import android.media.AudioManager; |
Dianne Hackborn | b58b8f8 | 2012-06-11 15:08:39 -0700 | [diff] [blame] | 76 | import android.media.MediaRouter; |
RoboErik | 42ea7ee | 2014-05-16 16:27:35 -0700 | [diff] [blame] | 77 | import android.media.session.MediaSessionManager; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 78 | import android.media.tv.ITvInputManager; |
| 79 | import android.media.tv.TvInputManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.net.ConnectivityManager; |
| 81 | import android.net.IConnectivityManager; |
Lorenzo Colitti | f9ff2c9 | 2014-05-21 16:32:11 -0700 | [diff] [blame] | 82 | import android.net.EthernetManager; |
| 83 | import android.net.IEthernetManager; |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 84 | import android.net.INetworkPolicyManager; |
| 85 | import android.net.NetworkPolicyManager; |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 86 | import android.net.NetworkScoreManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.net.Uri; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 88 | import android.net.nsd.INsdManager; |
| 89 | import android.net.nsd.NsdManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.net.wifi.IWifiManager; |
| 91 | import android.net.wifi.WifiManager; |
Roger Chang | 7fee723 | 2014-05-15 14:46:49 -0700 | [diff] [blame] | 92 | import android.net.wifi.passpoint.IWifiPasspointManager; |
| 93 | import android.net.wifi.passpoint.WifiPasspointManager; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 94 | import android.net.wifi.p2p.IWifiP2pManager; |
| 95 | import android.net.wifi.p2p.WifiP2pManager; |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 96 | import android.nfc.NfcManager; |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 97 | import android.os.BatteryManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.os.Binder; |
| 99 | import android.os.Bundle; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 100 | import android.os.Debug; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 101 | import android.os.DropBoxManager; |
Oscar Montemayor | 539d3c4 | 2010-01-29 15:27:00 -0800 | [diff] [blame] | 102 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.os.FileUtils; |
| 104 | import android.os.Handler; |
| 105 | import android.os.IBinder; |
| 106 | import android.os.IPowerManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 107 | import android.os.IUserManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 108 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | import android.os.PowerManager; |
| 110 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 111 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.os.ServiceManager; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 113 | import android.os.UserHandle; |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 114 | import android.os.SystemVibrator; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 115 | import android.os.UserManager; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 116 | import android.os.storage.IMountService; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 117 | import android.os.storage.StorageManager; |
Santos Cordon | f90186d | 2014-07-01 13:35:35 -0700 | [diff] [blame] | 118 | import android.phone.PhoneManager; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 119 | import android.print.IPrintManager; |
| 120 | import android.print.PrintManager; |
Jim Miller | a759614 | 2014-06-06 15:00:49 -0700 | [diff] [blame] | 121 | import android.service.fingerprint.IFingerprintService; |
Jim Miller | 08fa40c | 2014-04-29 18:18:47 -0700 | [diff] [blame] | 122 | import android.service.fingerprint.FingerprintManager; |
Yorke Lee | b4ce143 | 2014-06-09 13:53:23 -0700 | [diff] [blame] | 123 | import android.telecomm.TelecommManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | import android.telephony.TelephonyManager; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 125 | import android.content.ClipboardManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | import android.util.AndroidRuntimeException; |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 127 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | import android.util.Log; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 129 | import android.util.Slog; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 130 | import android.view.DisplayAdjustments; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | import android.view.ContextThemeWrapper; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 132 | import android.view.Display; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | import android.view.WindowManagerImpl; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 134 | import android.view.accessibility.AccessibilityManager; |
Alan Viverette | 69ce69b | 2013-08-29 12:23:48 -0700 | [diff] [blame] | 135 | import android.view.accessibility.CaptioningManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | import android.view.inputmethod.InputMethodManager; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 137 | import android.view.textservice.TextServicesManager; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 138 | import android.accounts.AccountManager; |
| 139 | import android.accounts.IAccountManager; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 140 | import android.app.admin.DevicePolicyManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 141 | import android.app.job.IJobScheduler; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 142 | import android.app.trust.TrustManager; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 143 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 144 | import com.android.internal.annotations.GuardedBy; |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 145 | import com.android.internal.app.IAppOpsService; |
Christopher Tate | 8f64f80 | 2014-05-19 13:46:29 -0700 | [diff] [blame] | 146 | import com.android.internal.appwidget.IAppWidgetService.Stub; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 147 | import com.android.internal.os.IDropBoxManagerService; |
Yorke Lee | b4ce143 | 2014-06-09 13:53:23 -0700 | [diff] [blame] | 148 | import com.android.internal.telecomm.ITelecommService; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | import java.io.File; |
| 151 | import java.io.FileInputStream; |
| 152 | import java.io.FileNotFoundException; |
| 153 | import java.io.FileOutputStream; |
| 154 | import java.io.IOException; |
| 155 | import java.io.InputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | import java.util.ArrayList; |
| 157 | import java.util.HashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | class ReceiverRestrictedContext extends ContextWrapper { |
| 160 | ReceiverRestrictedContext(Context base) { |
| 161 | super(base); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 166 | return registerReceiver(receiver, filter, null, null); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 171 | String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 172 | if (receiver == null) { |
| 173 | // Allow retrieving current sticky broadcast; this is safe since we |
| 174 | // aren't actually registering a receiver. |
| 175 | return super.registerReceiver(null, filter, broadcastPermission, scheduler); |
| 176 | } else { |
| 177 | throw new ReceiverCallNotAllowedException( |
| 178 | "BroadcastReceiver components are not allowed to register to receive intents"); |
| 179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 183 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 184 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 185 | if (receiver == null) { |
| 186 | // Allow retrieving current sticky broadcast; this is safe since we |
| 187 | // aren't actually registering a receiver. |
| 188 | return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler); |
| 189 | } else { |
| 190 | throw new ReceiverCallNotAllowedException( |
| 191 | "BroadcastReceiver components are not allowed to register to receive intents"); |
| 192 | } |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 197 | throw new ReceiverCallNotAllowedException( |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 198 | "BroadcastReceiver components are not allowed to bind to services"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 203 | * Common implementation of Context API, which provides the base |
| 204 | * context object for Activity and other application components. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | */ |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 206 | class ContextImpl extends Context { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 207 | private final static String TAG = "ContextImpl"; |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 208 | private final static boolean DEBUG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Map from package name, to preference name, to cached preferences. |
| 212 | */ |
| 213 | private static ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>> sSharedPrefs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 215 | final ActivityThread mMainThread; |
| 216 | final LoadedApk mPackageInfo; |
| 217 | |
| 218 | private final IBinder mActivityToken; |
| 219 | |
| 220 | private final UserHandle mUser; |
| 221 | |
| 222 | private final ApplicationContentResolver mContentResolver; |
| 223 | |
| 224 | private final String mBasePackageName; |
| 225 | private final String mOpPackageName; |
| 226 | |
| 227 | private final ResourcesManager mResourcesManager; |
| 228 | private final Resources mResources; |
| 229 | private final Display mDisplay; // may be null if default display |
| 230 | private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments(); |
| 231 | private final Configuration mOverrideConfiguration; |
| 232 | |
| 233 | private final boolean mRestricted; |
| 234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | private Context mOuterContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | private int mThemeResource = 0; |
| 237 | private Resources.Theme mTheme = null; |
| 238 | private PackageManager mPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | private Context mReceiverRestrictedContext = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | |
| 241 | private final Object mSync = new Object(); |
| 242 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 243 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | private File mDatabasesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 245 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | private File mPreferencesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 247 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | private File mFilesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 249 | @GuardedBy("mSync") |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 250 | private File mNoBackupFilesDir; |
| 251 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | private File mCacheDir; |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame^] | 253 | @GuardedBy("mSync") |
| 254 | private File mCodeCacheDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 255 | |
| 256 | @GuardedBy("mSync") |
| 257 | private File[] mExternalObbDirs; |
| 258 | @GuardedBy("mSync") |
| 259 | private File[] mExternalFilesDirs; |
| 260 | @GuardedBy("mSync") |
| 261 | private File[] mExternalCacheDirs; |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 262 | @GuardedBy("mSync") |
| 263 | private File[] mExternalMediaDirs; |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | private static final String[] EMPTY_FILE_LIST = {}; |
| 266 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 267 | /** |
| 268 | * Override this class when the system service constructor needs a |
| 269 | * ContextImpl. Else, use StaticServiceFetcher below. |
| 270 | */ |
| 271 | /*package*/ static class ServiceFetcher { |
| 272 | int mContextCacheIndex = -1; |
| 273 | |
| 274 | /** |
| 275 | * Main entrypoint; only override if you don't need caching. |
| 276 | */ |
| 277 | public Object getService(ContextImpl ctx) { |
| 278 | ArrayList<Object> cache = ctx.mServiceCache; |
| 279 | Object service; |
| 280 | synchronized (cache) { |
| 281 | if (cache.size() == 0) { |
| 282 | // Initialize the cache vector on first access. |
| 283 | // At this point sNextPerContextServiceCacheIndex |
| 284 | // is the number of potential services that are |
| 285 | // cached per-Context. |
| 286 | for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) { |
| 287 | cache.add(null); |
| 288 | } |
| 289 | } else { |
| 290 | service = cache.get(mContextCacheIndex); |
| 291 | if (service != null) { |
| 292 | return service; |
| 293 | } |
| 294 | } |
| 295 | service = createService(ctx); |
| 296 | cache.set(mContextCacheIndex, service); |
| 297 | return service; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Override this to create a new per-Context instance of the |
| 303 | * service. getService() will handle locking and caching. |
| 304 | */ |
| 305 | public Object createService(ContextImpl ctx) { |
| 306 | throw new RuntimeException("Not implemented"); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Override this class for services to be cached process-wide. |
| 312 | */ |
| 313 | abstract static class StaticServiceFetcher extends ServiceFetcher { |
| 314 | private Object mCachedInstance; |
| 315 | |
| 316 | @Override |
| 317 | public final Object getService(ContextImpl unused) { |
| 318 | synchronized (StaticServiceFetcher.this) { |
| 319 | Object service = mCachedInstance; |
| 320 | if (service != null) { |
| 321 | return service; |
| 322 | } |
| 323 | return mCachedInstance = createStaticService(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | public abstract Object createStaticService(); |
| 328 | } |
| 329 | |
| 330 | private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP = |
| 331 | new HashMap<String, ServiceFetcher>(); |
| 332 | |
| 333 | private static int sNextPerContextServiceCacheIndex = 0; |
| 334 | private static void registerService(String serviceName, ServiceFetcher fetcher) { |
| 335 | if (!(fetcher instanceof StaticServiceFetcher)) { |
| 336 | fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++; |
| 337 | } |
| 338 | SYSTEM_SERVICE_MAP.put(serviceName, fetcher); |
| 339 | } |
| 340 | |
| 341 | // This one's defined separately and given a variable name so it |
| 342 | // can be re-used by getWallpaperManager(), avoiding a HashMap |
| 343 | // lookup. |
| 344 | private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() { |
| 345 | public Object createService(ContextImpl ctx) { |
| 346 | return new WallpaperManager(ctx.getOuterContext(), |
| 347 | ctx.mMainThread.getHandler()); |
| 348 | }}; |
| 349 | |
| 350 | static { |
| 351 | registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() { |
| 352 | public Object getService(ContextImpl ctx) { |
| 353 | return AccessibilityManager.getInstance(ctx); |
| 354 | }}); |
| 355 | |
Alan Viverette | 69ce69b | 2013-08-29 12:23:48 -0700 | [diff] [blame] | 356 | registerService(CAPTIONING_SERVICE, new ServiceFetcher() { |
| 357 | public Object getService(ContextImpl ctx) { |
| 358 | return new CaptioningManager(ctx); |
| 359 | }}); |
| 360 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 361 | registerService(ACCOUNT_SERVICE, new ServiceFetcher() { |
| 362 | public Object createService(ContextImpl ctx) { |
| 363 | IBinder b = ServiceManager.getService(ACCOUNT_SERVICE); |
| 364 | IAccountManager service = IAccountManager.Stub.asInterface(b); |
| 365 | return new AccountManager(ctx, service); |
| 366 | }}); |
| 367 | |
| 368 | registerService(ACTIVITY_SERVICE, new ServiceFetcher() { |
| 369 | public Object createService(ContextImpl ctx) { |
| 370 | return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); |
| 371 | }}); |
| 372 | |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 373 | registerService(ALARM_SERVICE, new ServiceFetcher() { |
| 374 | public Object createService(ContextImpl ctx) { |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 375 | IBinder b = ServiceManager.getService(ALARM_SERVICE); |
| 376 | IAlarmManager service = IAlarmManager.Stub.asInterface(b); |
Christopher Tate | e0a22b3 | 2013-07-11 14:43:13 -0700 | [diff] [blame] | 377 | return new AlarmManager(service, ctx); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 378 | }}); |
| 379 | |
| 380 | registerService(AUDIO_SERVICE, new ServiceFetcher() { |
| 381 | public Object createService(ContextImpl ctx) { |
| 382 | return new AudioManager(ctx); |
| 383 | }}); |
| 384 | |
Dianne Hackborn | b58b8f8 | 2012-06-11 15:08:39 -0700 | [diff] [blame] | 385 | registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() { |
| 386 | public Object createService(ContextImpl ctx) { |
| 387 | return new MediaRouter(ctx); |
| 388 | }}); |
| 389 | |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 390 | registerService(BLUETOOTH_SERVICE, new ServiceFetcher() { |
| 391 | public Object createService(ContextImpl ctx) { |
Matthew Xie | ddf7e47 | 2013-03-01 18:41:02 -0800 | [diff] [blame] | 392 | return new BluetoothManager(ctx); |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 393 | }}); |
| 394 | |
Jinsuk Kim | 7f01f8e | 2014-05-29 17:10:39 +0900 | [diff] [blame] | 395 | registerService(HDMI_CONTROL_SERVICE, new StaticServiceFetcher() { |
| 396 | public Object createStaticService() { |
| 397 | IBinder b = ServiceManager.getService(HDMI_CONTROL_SERVICE); |
| 398 | return new HdmiControlManager(IHdmiControlService.Stub.asInterface(b)); |
| 399 | }}); |
Jinsuk Kim | fbcd503 | 2014-03-21 16:25:13 +0900 | [diff] [blame] | 400 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 401 | registerService(CLIPBOARD_SERVICE, new ServiceFetcher() { |
| 402 | public Object createService(ContextImpl ctx) { |
| 403 | return new ClipboardManager(ctx.getOuterContext(), |
| 404 | ctx.mMainThread.getHandler()); |
| 405 | }}); |
| 406 | |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 407 | registerService(CONNECTIVITY_SERVICE, new ServiceFetcher() { |
| 408 | public Object createService(ContextImpl ctx) { |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 409 | IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE); |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 410 | return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b), |
| 411 | ctx.getPackageName()); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 412 | }}); |
| 413 | |
| 414 | registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() { |
| 415 | public Object createStaticService() { |
| 416 | IBinder b = ServiceManager.getService(COUNTRY_DETECTOR); |
| 417 | return new CountryDetector(ICountryDetector.Stub.asInterface(b)); |
| 418 | }}); |
| 419 | |
| 420 | registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() { |
| 421 | public Object createService(ContextImpl ctx) { |
| 422 | return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler()); |
| 423 | }}); |
| 424 | |
| 425 | registerService(DOWNLOAD_SERVICE, new ServiceFetcher() { |
| 426 | public Object createService(ContextImpl ctx) { |
| 427 | return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName()); |
| 428 | }}); |
| 429 | |
Todd Poynor | e35872d | 2013-12-10 11:57:21 -0800 | [diff] [blame] | 430 | registerService(BATTERY_SERVICE, new ServiceFetcher() { |
| 431 | public Object createService(ContextImpl ctx) { |
| 432 | return new BatteryManager(); |
| 433 | }}); |
| 434 | |
Nick Pelly | d250746 | 2010-12-13 12:22:34 -0800 | [diff] [blame] | 435 | registerService(NFC_SERVICE, new ServiceFetcher() { |
| 436 | public Object createService(ContextImpl ctx) { |
| 437 | return new NfcManager(ctx); |
| 438 | }}); |
| 439 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 440 | registerService(DROPBOX_SERVICE, new StaticServiceFetcher() { |
| 441 | public Object createStaticService() { |
| 442 | return createDropBoxManager(); |
| 443 | }}); |
| 444 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 445 | registerService(INPUT_SERVICE, new StaticServiceFetcher() { |
| 446 | public Object createStaticService() { |
| 447 | return InputManager.getInstance(); |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 448 | }}); |
Jeff Brown | 9df6e7a | 2012-04-05 11:49:26 -0700 | [diff] [blame] | 449 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 450 | registerService(DISPLAY_SERVICE, new ServiceFetcher() { |
| 451 | @Override |
| 452 | public Object createService(ContextImpl ctx) { |
| 453 | return new DisplayManager(ctx.getOuterContext()); |
| 454 | }}); |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 455 | |
Jeff Brown | f9e989d | 2013-04-04 23:04:03 -0700 | [diff] [blame] | 456 | registerService(INPUT_METHOD_SERVICE, new StaticServiceFetcher() { |
| 457 | public Object createStaticService() { |
| 458 | return InputMethodManager.getInstance(); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 459 | }}); |
| 460 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 461 | registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() { |
| 462 | public Object createService(ContextImpl ctx) { |
| 463 | return TextServicesManager.getInstance(); |
| 464 | }}); |
| 465 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 466 | registerService(KEYGUARD_SERVICE, new ServiceFetcher() { |
| 467 | public Object getService(ContextImpl ctx) { |
| 468 | // TODO: why isn't this caching it? It wasn't |
| 469 | // before, so I'm preserving the old behavior and |
| 470 | // using getService(), instead of createService() |
| 471 | // which would do the caching. |
| 472 | return new KeyguardManager(); |
| 473 | }}); |
| 474 | |
| 475 | registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() { |
| 476 | public Object createService(ContextImpl ctx) { |
| 477 | return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext()); |
| 478 | }}); |
| 479 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 480 | registerService(LOCATION_SERVICE, new ServiceFetcher() { |
| 481 | public Object createService(ContextImpl ctx) { |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 482 | IBinder b = ServiceManager.getService(LOCATION_SERVICE); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 483 | return new LocationManager(ctx, ILocationManager.Stub.asInterface(b)); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 484 | }}); |
| 485 | |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 486 | registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() { |
| 487 | @Override |
| 488 | public Object createService(ContextImpl ctx) { |
| 489 | return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface( |
| 490 | ServiceManager.getService(NETWORK_POLICY_SERVICE))); |
| 491 | } |
| 492 | }); |
| 493 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 494 | registerService(NOTIFICATION_SERVICE, new ServiceFetcher() { |
| 495 | public Object createService(ContextImpl ctx) { |
| 496 | final Context outerContext = ctx.getOuterContext(); |
| 497 | return new NotificationManager( |
| 498 | new ContextThemeWrapper(outerContext, |
Alan Viverette | 5effd7e | 2014-05-05 12:25:33 -0700 | [diff] [blame] | 499 | Resources.selectSystemTheme(0, |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 500 | outerContext.getApplicationInfo().targetSdkVersion, |
Alan Viverette | 5effd7e | 2014-05-05 12:25:33 -0700 | [diff] [blame] | 501 | com.android.internal.R.style.Theme_Dialog, |
| 502 | com.android.internal.R.style.Theme_Holo_Dialog, |
| 503 | com.android.internal.R.style.Theme_DeviceDefault_Dialog, |
| 504 | com.android.internal.R.style.Theme_DeviceDefault_Light_Dialog)), |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 505 | ctx.mMainThread.getHandler()); |
| 506 | }}); |
| 507 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 508 | registerService(NSD_SERVICE, new ServiceFetcher() { |
| 509 | @Override |
| 510 | public Object createService(ContextImpl ctx) { |
| 511 | IBinder b = ServiceManager.getService(NSD_SERVICE); |
| 512 | INsdManager service = INsdManager.Stub.asInterface(b); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 513 | return new NsdManager(ctx.getOuterContext(), service); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 514 | }}); |
| 515 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 516 | // Note: this was previously cached in a static variable, but |
| 517 | // constructed using mMainThread.getHandler(), so converting |
| 518 | // it to be a regular Context-cached service... |
| 519 | registerService(POWER_SERVICE, new ServiceFetcher() { |
| 520 | public Object createService(ContextImpl ctx) { |
| 521 | IBinder b = ServiceManager.getService(POWER_SERVICE); |
| 522 | IPowerManager service = IPowerManager.Stub.asInterface(b); |
Jeff Brown | 2c43c33 | 2014-06-12 22:38:59 -0700 | [diff] [blame] | 523 | if (service == null) { |
| 524 | Log.wtf(TAG, "Failed to get power manager service."); |
| 525 | } |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 526 | return new PowerManager(ctx.getOuterContext(), |
| 527 | service, ctx.mMainThread.getHandler()); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 528 | }}); |
| 529 | |
| 530 | registerService(SEARCH_SERVICE, new ServiceFetcher() { |
| 531 | public Object createService(ContextImpl ctx) { |
| 532 | return new SearchManager(ctx.getOuterContext(), |
| 533 | ctx.mMainThread.getHandler()); |
| 534 | }}); |
| 535 | |
| 536 | registerService(SENSOR_SERVICE, new ServiceFetcher() { |
| 537 | public Object createService(ContextImpl ctx) { |
Jaikumar Ganesh | 6d0c1d7 | 2013-03-27 17:41:33 -0700 | [diff] [blame] | 538 | return new SystemSensorManager(ctx.getOuterContext(), |
| 539 | ctx.mMainThread.getHandler().getLooper()); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 540 | }}); |
| 541 | |
| 542 | registerService(STATUS_BAR_SERVICE, new ServiceFetcher() { |
| 543 | public Object createService(ContextImpl ctx) { |
| 544 | return new StatusBarManager(ctx.getOuterContext()); |
| 545 | }}); |
| 546 | |
| 547 | registerService(STORAGE_SERVICE, new ServiceFetcher() { |
| 548 | public Object createService(ContextImpl ctx) { |
| 549 | try { |
Jeff Sharkey | be72215 | 2013-02-15 16:56:38 -0800 | [diff] [blame] | 550 | return new StorageManager( |
| 551 | ctx.getContentResolver(), ctx.mMainThread.getHandler().getLooper()); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 552 | } catch (RemoteException rex) { |
| 553 | Log.e(TAG, "Failed to create StorageManager", rex); |
| 554 | return null; |
| 555 | } |
| 556 | }}); |
| 557 | |
| 558 | registerService(TELEPHONY_SERVICE, new ServiceFetcher() { |
| 559 | public Object createService(ContextImpl ctx) { |
| 560 | return new TelephonyManager(ctx.getOuterContext()); |
| 561 | }}); |
| 562 | |
Yorke Lee | b4ce143 | 2014-06-09 13:53:23 -0700 | [diff] [blame] | 563 | registerService(TELECOMM_SERVICE, new ServiceFetcher() { |
| 564 | public Object createService(ContextImpl ctx) { |
Ihab Awad | 807fe0a | 2014-07-09 12:30:52 -0700 | [diff] [blame] | 565 | return new TelecommManager(ctx.getOuterContext()); |
Yorke Lee | b4ce143 | 2014-06-09 13:53:23 -0700 | [diff] [blame] | 566 | }}); |
| 567 | |
Santos Cordon | f90186d | 2014-07-01 13:35:35 -0700 | [diff] [blame] | 568 | registerService(PHONE_SERVICE, new ServiceFetcher() { |
| 569 | public Object createService(ContextImpl ctx) { |
Santos Cordon | f3e83e4 | 2014-07-10 10:35:07 -0700 | [diff] [blame] | 570 | return new PhoneManager(ctx.getOuterContext()); |
Santos Cordon | f90186d | 2014-07-01 13:35:35 -0700 | [diff] [blame] | 571 | }}); |
| 572 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 573 | registerService(UI_MODE_SERVICE, new ServiceFetcher() { |
| 574 | public Object createService(ContextImpl ctx) { |
| 575 | return new UiModeManager(); |
| 576 | }}); |
| 577 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 578 | registerService(USB_SERVICE, new ServiceFetcher() { |
| 579 | public Object createService(ContextImpl ctx) { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 580 | IBinder b = ServiceManager.getService(USB_SERVICE); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 581 | return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 582 | }}); |
| 583 | |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 584 | registerService(SERIAL_SERVICE, new ServiceFetcher() { |
| 585 | public Object createService(ContextImpl ctx) { |
| 586 | IBinder b = ServiceManager.getService(SERIAL_SERVICE); |
| 587 | return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); |
| 588 | }}); |
| 589 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 590 | registerService(VIBRATOR_SERVICE, new ServiceFetcher() { |
| 591 | public Object createService(ContextImpl ctx) { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 592 | return new SystemVibrator(ctx); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 593 | }}); |
| 594 | |
| 595 | registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER); |
| 596 | |
| 597 | registerService(WIFI_SERVICE, new ServiceFetcher() { |
| 598 | public Object createService(ContextImpl ctx) { |
| 599 | IBinder b = ServiceManager.getService(WIFI_SERVICE); |
| 600 | IWifiManager service = IWifiManager.Stub.asInterface(b); |
Irfan Sheriff | 88759bb | 2012-07-02 15:58:28 -0700 | [diff] [blame] | 601 | return new WifiManager(ctx.getOuterContext(), service); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 602 | }}); |
| 603 | |
Yuhao Zheng | a486447 | 2014-04-10 11:45:42 -0700 | [diff] [blame] | 604 | registerService(WIFI_PASSPOINT_SERVICE, new ServiceFetcher() { |
Yuhao Zheng | 10bf635 | 2014-03-25 15:00:45 -0700 | [diff] [blame] | 605 | public Object createService(ContextImpl ctx) { |
Yuhao Zheng | a486447 | 2014-04-10 11:45:42 -0700 | [diff] [blame] | 606 | IBinder b = ServiceManager.getService(WIFI_PASSPOINT_SERVICE); |
Roger Chang | 7fee723 | 2014-05-15 14:46:49 -0700 | [diff] [blame] | 607 | IWifiPasspointManager service = IWifiPasspointManager.Stub.asInterface(b); |
| 608 | return new WifiPasspointManager(ctx.getOuterContext(), service); |
Yuhao Zheng | 10bf635 | 2014-03-25 15:00:45 -0700 | [diff] [blame] | 609 | }}); |
| 610 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 611 | registerService(WIFI_P2P_SERVICE, new ServiceFetcher() { |
| 612 | public Object createService(ContextImpl ctx) { |
| 613 | IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE); |
| 614 | IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b); |
| 615 | return new WifiP2pManager(service); |
| 616 | }}); |
| 617 | |
Vinit Deshapnde | 011e1b3 | 2014-05-07 21:09:11 -0700 | [diff] [blame] | 618 | registerService(WIFI_SCANNING_SERVICE, new ServiceFetcher() { |
| 619 | public Object createService(ContextImpl ctx) { |
| 620 | IBinder b = ServiceManager.getService(WIFI_SCANNING_SERVICE); |
| 621 | IWifiScanner service = IWifiScanner.Stub.asInterface(b); |
| 622 | return new WifiScanner(ctx.getOuterContext(), service); |
| 623 | }}); |
| 624 | |
Lorenzo Colitti | f9ff2c9 | 2014-05-21 16:32:11 -0700 | [diff] [blame] | 625 | registerService(ETHERNET_SERVICE, new ServiceFetcher() { |
| 626 | public Object createService(ContextImpl ctx) { |
| 627 | IBinder b = ServiceManager.getService(ETHERNET_SERVICE); |
| 628 | IEthernetManager service = IEthernetManager.Stub.asInterface(b); |
| 629 | return new EthernetManager(ctx.getOuterContext(), service); |
| 630 | }}); |
| 631 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 632 | registerService(WINDOW_SERVICE, new ServiceFetcher() { |
Romain Guy | e50848b | 2013-06-07 10:57:25 -0700 | [diff] [blame] | 633 | Display mDefaultDisplay; |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 634 | public Object getService(ContextImpl ctx) { |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 635 | Display display = ctx.mDisplay; |
| 636 | if (display == null) { |
Romain Guy | e50848b | 2013-06-07 10:57:25 -0700 | [diff] [blame] | 637 | if (mDefaultDisplay == null) { |
| 638 | DisplayManager dm = (DisplayManager)ctx.getOuterContext(). |
| 639 | getSystemService(Context.DISPLAY_SERVICE); |
| 640 | mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY); |
| 641 | } |
| 642 | display = mDefaultDisplay; |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 643 | } |
| 644 | return new WindowManagerImpl(display); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 645 | }}); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 646 | |
| 647 | registerService(USER_SERVICE, new ServiceFetcher() { |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 648 | public Object createService(ContextImpl ctx) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 649 | IBinder b = ServiceManager.getService(USER_SERVICE); |
| 650 | IUserManager service = IUserManager.Stub.asInterface(b); |
| 651 | return new UserManager(ctx, service); |
| 652 | }}); |
Dianne Hackborn | a06de0f | 2012-12-11 16:34:47 -0800 | [diff] [blame] | 653 | |
| 654 | registerService(APP_OPS_SERVICE, new ServiceFetcher() { |
| 655 | public Object createService(ContextImpl ctx) { |
| 656 | IBinder b = ServiceManager.getService(APP_OPS_SERVICE); |
| 657 | IAppOpsService service = IAppOpsService.Stub.asInterface(b); |
| 658 | return new AppOpsManager(ctx, service); |
| 659 | }}); |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 660 | |
Igor Murashkin | e363fbb | 2013-06-25 20:26:06 +0000 | [diff] [blame] | 661 | registerService(CAMERA_SERVICE, new ServiceFetcher() { |
| 662 | public Object createService(ContextImpl ctx) { |
| 663 | return new CameraManager(ctx); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 664 | } |
| 665 | }); |
| 666 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 667 | registerService(LAUNCHER_APPS_SERVICE, new ServiceFetcher() { |
| 668 | public Object createService(ContextImpl ctx) { |
| 669 | IBinder b = ServiceManager.getService(LAUNCHER_APPS_SERVICE); |
| 670 | ILauncherApps service = ILauncherApps.Stub.asInterface(b); |
| 671 | return new LauncherApps(ctx, service); |
| 672 | } |
| 673 | }); |
| 674 | |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 675 | registerService(RESTRICTIONS_SERVICE, new ServiceFetcher() { |
| 676 | public Object createService(ContextImpl ctx) { |
| 677 | IBinder b = ServiceManager.getService(RESTRICTIONS_SERVICE); |
| 678 | IRestrictionsManager service = IRestrictionsManager.Stub.asInterface(b); |
| 679 | return new RestrictionsManager(ctx, service); |
| 680 | } |
| 681 | }); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 682 | registerService(PRINT_SERVICE, new ServiceFetcher() { |
| 683 | public Object createService(ContextImpl ctx) { |
| 684 | IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE); |
| 685 | IPrintManager service = IPrintManager.Stub.asInterface(iBinder); |
| 686 | return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(), |
| 687 | UserHandle.getAppId(Process.myUid())); |
Eino-Ville Talvala | b267554 | 2012-12-12 13:29:45 -0800 | [diff] [blame] | 688 | }}); |
Erik Gilling | 51e95df | 2013-06-26 11:06:51 -0700 | [diff] [blame] | 689 | |
| 690 | registerService(CONSUMER_IR_SERVICE, new ServiceFetcher() { |
| 691 | public Object createService(ContextImpl ctx) { |
| 692 | return new ConsumerIrManager(ctx); |
| 693 | }}); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 694 | |
| 695 | registerService(MEDIA_SESSION_SERVICE, new ServiceFetcher() { |
| 696 | public Object createService(ContextImpl ctx) { |
RoboErik | 42ea7ee | 2014-05-16 16:27:35 -0700 | [diff] [blame] | 697 | return new MediaSessionManager(ctx); |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 698 | } |
| 699 | }); |
Jim Miller | a759614 | 2014-06-06 15:00:49 -0700 | [diff] [blame] | 700 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 701 | registerService(TRUST_SERVICE, new ServiceFetcher() { |
| 702 | public Object createService(ContextImpl ctx) { |
| 703 | IBinder b = ServiceManager.getService(TRUST_SERVICE); |
| 704 | return new TrustManager(b); |
| 705 | } |
| 706 | }); |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 707 | |
Jim Miller | a759614 | 2014-06-06 15:00:49 -0700 | [diff] [blame] | 708 | registerService(FINGERPRINT_SERVICE, new ServiceFetcher() { |
| 709 | public Object createService(ContextImpl ctx) { |
Jim Miller | 06e658f | 2014-06-17 15:59:40 -0700 | [diff] [blame] | 710 | IBinder binder = ServiceManager.getService(FINGERPRINT_SERVICE); |
| 711 | IFingerprintService service = IFingerprintService.Stub.asInterface(binder); |
Jim Miller | a759614 | 2014-06-06 15:00:49 -0700 | [diff] [blame] | 712 | return new FingerprintManager(ctx.getOuterContext(), service); |
| 713 | } |
| 714 | }); |
| 715 | |
Jae Seo | 3957091 | 2014-02-20 18:23:25 -0800 | [diff] [blame] | 716 | registerService(TV_INPUT_SERVICE, new ServiceFetcher() { |
| 717 | public Object createService(ContextImpl ctx) { |
| 718 | IBinder iBinder = ServiceManager.getService(TV_INPUT_SERVICE); |
| 719 | ITvInputManager service = ITvInputManager.Stub.asInterface(iBinder); |
| 720 | return new TvInputManager(service, UserHandle.myUserId()); |
| 721 | }}); |
Jeff Davidson | b51e0a6 | 2014-04-09 12:38:15 -0700 | [diff] [blame] | 722 | |
| 723 | registerService(NETWORK_SCORE_SERVICE, new ServiceFetcher() { |
| 724 | public Object createService(ContextImpl ctx) { |
| 725 | return new NetworkScoreManager(ctx); |
| 726 | } |
| 727 | }); |
Dianne Hackborn | e22b3b1 | 2014-05-07 18:06:44 -0700 | [diff] [blame] | 728 | |
| 729 | registerService(USAGE_STATS_SERVICE, new ServiceFetcher() { |
| 730 | public Object createService(ContextImpl ctx) { |
| 731 | return new UsageStatsManager(ctx.getOuterContext()); |
| 732 | }}); |
Christopher Tate | 8f64f80 | 2014-05-19 13:46:29 -0700 | [diff] [blame] | 733 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 734 | registerService(JOB_SCHEDULER_SERVICE, new ServiceFetcher() { |
Christopher Tate | 8f64f80 | 2014-05-19 13:46:29 -0700 | [diff] [blame] | 735 | public Object createService(ContextImpl ctx) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 736 | IBinder b = ServiceManager.getService(JOB_SCHEDULER_SERVICE); |
| 737 | return new JobSchedulerImpl(IJobScheduler.Stub.asInterface(b)); |
Andres Morales | 68d4acd | 2014-07-01 19:40:41 -0700 | [diff] [blame] | 738 | }}); |
| 739 | |
| 740 | registerService(PERSISTENT_DATA_BLOCK_SERVICE, new ServiceFetcher() { |
| 741 | public Object createService(ContextImpl ctx) { |
| 742 | IBinder b = ServiceManager.getService(PERSISTENT_DATA_BLOCK_SERVICE); |
| 743 | return new PersistentDataBlockManager( |
| 744 | IPersistentDataBlockService.Stub.asInterface(b)); |
| 745 | }}); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 748 | static ContextImpl getImpl(Context context) { |
| 749 | Context nextContext; |
| 750 | while ((context instanceof ContextWrapper) && |
| 751 | (nextContext=((ContextWrapper)context).getBaseContext()) != null) { |
| 752 | context = nextContext; |
| 753 | } |
| 754 | return (ContextImpl)context; |
| 755 | } |
| 756 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 757 | // The system service cache for the system services that are |
| 758 | // cached per-ContextImpl. Package-scoped to avoid accessor |
| 759 | // methods. |
| 760 | final ArrayList<Object> mServiceCache = new ArrayList<Object>(); |
| 761 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 762 | @Override |
| 763 | public AssetManager getAssets() { |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 764 | return getResources().getAssets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | @Override |
| 768 | public Resources getResources() { |
| 769 | return mResources; |
| 770 | } |
| 771 | |
| 772 | @Override |
| 773 | public PackageManager getPackageManager() { |
| 774 | if (mPackageManager != null) { |
| 775 | return mPackageManager; |
| 776 | } |
| 777 | |
| 778 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 779 | if (pm != null) { |
| 780 | // Doesn't matter if we make more than one instance. |
| 781 | return (mPackageManager = new ApplicationPackageManager(this, pm)); |
| 782 | } |
| 783 | |
| 784 | return null; |
| 785 | } |
| 786 | |
| 787 | @Override |
| 788 | public ContentResolver getContentResolver() { |
| 789 | return mContentResolver; |
| 790 | } |
| 791 | |
| 792 | @Override |
| 793 | public Looper getMainLooper() { |
| 794 | return mMainThread.getLooper(); |
| 795 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 796 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | @Override |
| 798 | public Context getApplicationContext() { |
Christopher Tate | eb9e9ec | 2010-03-23 17:14:36 -0700 | [diff] [blame] | 799 | return (mPackageInfo != null) ? |
| 800 | mPackageInfo.getApplication() : mMainThread.getApplication(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | @Override |
| 804 | public void setTheme(int resid) { |
| 805 | mThemeResource = resid; |
| 806 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 807 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | @Override |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 809 | public int getThemeResId() { |
| 810 | return mThemeResource; |
| 811 | } |
| 812 | |
| 813 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | public Resources.Theme getTheme() { |
| 815 | if (mTheme == null) { |
Alan Viverette | 5effd7e | 2014-05-05 12:25:33 -0700 | [diff] [blame] | 816 | mThemeResource = Resources.selectDefaultTheme(mThemeResource, |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 817 | getOuterContext().getApplicationInfo().targetSdkVersion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | mTheme = mResources.newTheme(); |
| 819 | mTheme.applyStyle(mThemeResource, true); |
| 820 | } |
| 821 | return mTheme; |
| 822 | } |
| 823 | |
| 824 | @Override |
| 825 | public ClassLoader getClassLoader() { |
| 826 | return mPackageInfo != null ? |
| 827 | mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader(); |
| 828 | } |
| 829 | |
| 830 | @Override |
| 831 | public String getPackageName() { |
| 832 | if (mPackageInfo != null) { |
| 833 | return mPackageInfo.getPackageName(); |
| 834 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 835 | // No mPackageInfo means this is a Context for the system itself, |
| 836 | // and this here is its name. |
| 837 | return "android"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | } |
| 839 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 840 | /** @hide */ |
| 841 | @Override |
| 842 | public String getBasePackageName() { |
| 843 | return mBasePackageName != null ? mBasePackageName : getPackageName(); |
| 844 | } |
| 845 | |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 846 | /** @hide */ |
| 847 | @Override |
| 848 | public String getOpPackageName() { |
| 849 | return mOpPackageName != null ? mOpPackageName : getBasePackageName(); |
| 850 | } |
| 851 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | @Override |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 853 | public ApplicationInfo getApplicationInfo() { |
| 854 | if (mPackageInfo != null) { |
| 855 | return mPackageInfo.getApplicationInfo(); |
| 856 | } |
| 857 | throw new RuntimeException("Not supported in system context"); |
| 858 | } |
| 859 | |
| 860 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 861 | public String getPackageResourcePath() { |
| 862 | if (mPackageInfo != null) { |
| 863 | return mPackageInfo.getResDir(); |
| 864 | } |
| 865 | throw new RuntimeException("Not supported in system context"); |
| 866 | } |
| 867 | |
| 868 | @Override |
| 869 | public String getPackageCodePath() { |
| 870 | if (mPackageInfo != null) { |
| 871 | return mPackageInfo.getAppDir(); |
| 872 | } |
| 873 | throw new RuntimeException("Not supported in system context"); |
| 874 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 875 | |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 876 | public File getSharedPrefsFile(String name) { |
| 877 | return makeFilename(getPreferencesDir(), name + ".xml"); |
| 878 | } |
| 879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | @Override |
| 881 | public SharedPreferences getSharedPreferences(String name, int mode) { |
| 882 | SharedPreferencesImpl sp; |
Dianne Hackborn | f691359 | 2013-09-05 13:21:24 -0700 | [diff] [blame] | 883 | synchronized (ContextImpl.class) { |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 884 | if (sSharedPrefs == null) { |
| 885 | sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>(); |
| 886 | } |
| 887 | |
| 888 | final String packageName = getPackageName(); |
| 889 | ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName); |
| 890 | if (packagePrefs == null) { |
| 891 | packagePrefs = new ArrayMap<String, SharedPreferencesImpl>(); |
| 892 | sSharedPrefs.put(packageName, packagePrefs); |
| 893 | } |
| 894 | |
Dianne Hackborn | ff32f35 | 2013-07-18 17:31:13 -0700 | [diff] [blame] | 895 | // At least one application in the world actually passes in a null |
| 896 | // name. This happened to work because when we generated the file name |
| 897 | // we would stringify it to "null.xml". Nice. |
| 898 | if (mPackageInfo.getApplicationInfo().targetSdkVersion < |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 899 | Build.VERSION_CODES.KITKAT) { |
Dianne Hackborn | ff32f35 | 2013-07-18 17:31:13 -0700 | [diff] [blame] | 900 | if (name == null) { |
| 901 | name = "null"; |
| 902 | } |
| 903 | } |
| 904 | |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 905 | sp = packagePrefs.get(name); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 906 | if (sp == null) { |
Brad Fitzpatrick | 4cd50b8 | 2010-12-01 17:31:45 -0800 | [diff] [blame] | 907 | File prefsFile = getSharedPrefsFile(name); |
| 908 | sp = new SharedPreferencesImpl(prefsFile, mode); |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 909 | packagePrefs.put(name, sp); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 910 | return sp; |
| 911 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 912 | } |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 913 | if ((mode & Context.MODE_MULTI_PROCESS) != 0 || |
| 914 | getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { |
| 915 | // If somebody else (some other process) changed the prefs |
| 916 | // file behind our back, we reload it. This has been the |
| 917 | // historical (if undocumented) behavior. |
| 918 | sp.startReloadIfChangedUnexpectedly(); |
| 919 | } |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 920 | return sp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | private File getPreferencesDir() { |
| 924 | synchronized (mSync) { |
| 925 | if (mPreferencesDir == null) { |
| 926 | mPreferencesDir = new File(getDataDirFile(), "shared_prefs"); |
| 927 | } |
| 928 | return mPreferencesDir; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | @Override |
| 933 | public FileInputStream openFileInput(String name) |
| 934 | throws FileNotFoundException { |
| 935 | File f = makeFilename(getFilesDir(), name); |
| 936 | return new FileInputStream(f); |
| 937 | } |
| 938 | |
| 939 | @Override |
| 940 | public FileOutputStream openFileOutput(String name, int mode) |
| 941 | throws FileNotFoundException { |
| 942 | final boolean append = (mode&MODE_APPEND) != 0; |
| 943 | File f = makeFilename(getFilesDir(), name); |
| 944 | try { |
| 945 | FileOutputStream fos = new FileOutputStream(f, append); |
| 946 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 947 | return fos; |
| 948 | } catch (FileNotFoundException e) { |
| 949 | } |
| 950 | |
| 951 | File parent = f.getParentFile(); |
| 952 | parent.mkdir(); |
| 953 | FileUtils.setPermissions( |
| 954 | parent.getPath(), |
| 955 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 956 | -1, -1); |
| 957 | FileOutputStream fos = new FileOutputStream(f, append); |
| 958 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 959 | return fos; |
| 960 | } |
| 961 | |
| 962 | @Override |
| 963 | public boolean deleteFile(String name) { |
| 964 | File f = makeFilename(getFilesDir(), name); |
| 965 | return f.delete(); |
| 966 | } |
| 967 | |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 968 | // Common-path handling of app data dir creation |
| 969 | private static File createFilesDirLocked(File file) { |
| 970 | if (!file.exists()) { |
| 971 | if (!file.mkdirs()) { |
| 972 | if (file.exists()) { |
| 973 | // spurious failure; probably racing with another process for this app |
| 974 | return file; |
| 975 | } |
| 976 | Log.w(TAG, "Unable to create files subdir " + file.getPath()); |
| 977 | return null; |
| 978 | } |
| 979 | FileUtils.setPermissions( |
| 980 | file.getPath(), |
| 981 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 982 | -1, -1); |
| 983 | } |
| 984 | return file; |
| 985 | } |
| 986 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | @Override |
| 988 | public File getFilesDir() { |
| 989 | synchronized (mSync) { |
| 990 | if (mFilesDir == null) { |
| 991 | mFilesDir = new File(getDataDirFile(), "files"); |
| 992 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 993 | return createFilesDirLocked(mFilesDir); |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | @Override |
| 998 | public File getNoBackupFilesDir() { |
| 999 | synchronized (mSync) { |
| 1000 | if (mNoBackupFilesDir == null) { |
| 1001 | mNoBackupFilesDir = new File(getDataDirFile(), "no_backup"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 1003 | return createFilesDirLocked(mNoBackupFilesDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | } |
| 1005 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1006 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | @Override |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1008 | public File getExternalFilesDir(String type) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1009 | // Operates on primary external storage |
| 1010 | return getExternalFilesDirs(type)[0]; |
| 1011 | } |
| 1012 | |
| 1013 | @Override |
| 1014 | public File[] getExternalFilesDirs(String type) { |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1015 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1016 | if (mExternalFilesDirs == null) { |
| 1017 | mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName()); |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1018 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1019 | |
| 1020 | // Splice in requested type, if any |
| 1021 | File[] dirs = mExternalFilesDirs; |
| 1022 | if (type != null) { |
| 1023 | dirs = Environment.buildPaths(dirs, type); |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1024 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1025 | |
| 1026 | // Create dirs if needed |
| 1027 | return ensureDirsExistOrFilter(dirs); |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1028 | } |
| 1029 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1030 | |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1031 | @Override |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 1032 | public File getObbDir() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1033 | // Operates on primary external storage |
| 1034 | return getObbDirs()[0]; |
| 1035 | } |
| 1036 | |
| 1037 | @Override |
| 1038 | public File[] getObbDirs() { |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 1039 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1040 | if (mExternalObbDirs == null) { |
| 1041 | mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName()); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 1042 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 1043 | |
| 1044 | // Create dirs if needed |
| 1045 | return ensureDirsExistOrFilter(mExternalObbDirs); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | public File getCacheDir() { |
| 1051 | synchronized (mSync) { |
| 1052 | if (mCacheDir == null) { |
| 1053 | mCacheDir = new File(getDataDirFile(), "cache"); |
| 1054 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 1055 | return createFilesDirLocked(mCacheDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1058 | |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1059 | @Override |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame^] | 1060 | public File getCodeCacheDir() { |
| 1061 | synchronized (mSync) { |
| 1062 | if (mCodeCacheDir == null) { |
| 1063 | mCodeCacheDir = new File(getDataDirFile(), "code_cache"); |
| 1064 | } |
| 1065 | return createFilesDirLocked(mCodeCacheDir); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | @Override |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1070 | public File getExternalCacheDir() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1071 | // Operates on primary external storage |
| 1072 | return getExternalCacheDirs()[0]; |
| 1073 | } |
| 1074 | |
| 1075 | @Override |
| 1076 | public File[] getExternalCacheDirs() { |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1077 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1078 | if (mExternalCacheDirs == null) { |
| 1079 | mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName()); |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1080 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1081 | |
| 1082 | // Create dirs if needed |
| 1083 | return ensureDirsExistOrFilter(mExternalCacheDirs); |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1084 | } |
| 1085 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1086 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1087 | @Override |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 1088 | public File[] getExternalMediaDirs() { |
| 1089 | synchronized (mSync) { |
| 1090 | if (mExternalMediaDirs == null) { |
| 1091 | mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName()); |
| 1092 | } |
| 1093 | |
| 1094 | // Create dirs if needed |
| 1095 | return ensureDirsExistOrFilter(mExternalMediaDirs); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | public File getFileStreamPath(String name) { |
| 1101 | return makeFilename(getFilesDir(), name); |
| 1102 | } |
| 1103 | |
| 1104 | @Override |
| 1105 | public String[] fileList() { |
| 1106 | final String[] list = getFilesDir().list(); |
| 1107 | return (list != null) ? list : EMPTY_FILE_LIST; |
| 1108 | } |
| 1109 | |
| 1110 | @Override |
| 1111 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 1112 | return openOrCreateDatabase(name, mode, factory, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | @Override |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 1116 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, |
| 1117 | DatabaseErrorHandler errorHandler) { |
| 1118 | File f = validateFilePath(name, true); |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 1119 | int flags = SQLiteDatabase.CREATE_IF_NECESSARY; |
| 1120 | if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { |
| 1121 | flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; |
| 1122 | } |
| 1123 | SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 1124 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 1125 | return db; |
| 1126 | } |
| 1127 | |
| 1128 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | public boolean deleteDatabase(String name) { |
| 1130 | try { |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 1131 | File f = validateFilePath(name, false); |
Jeff Brown | 79087e4 | 2012-03-01 19:52:44 -0800 | [diff] [blame] | 1132 | return SQLiteDatabase.deleteDatabase(f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | } catch (Exception e) { |
| 1134 | } |
| 1135 | return false; |
| 1136 | } |
| 1137 | |
| 1138 | @Override |
| 1139 | public File getDatabasePath(String name) { |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 1140 | return validateFilePath(name, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | @Override |
| 1144 | public String[] databaseList() { |
| 1145 | final String[] list = getDatabasesDir().list(); |
| 1146 | return (list != null) ? list : EMPTY_FILE_LIST; |
| 1147 | } |
| 1148 | |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1149 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1150 | private File getDatabasesDir() { |
| 1151 | synchronized (mSync) { |
| 1152 | if (mDatabasesDir == null) { |
| 1153 | mDatabasesDir = new File(getDataDirFile(), "databases"); |
| 1154 | } |
| 1155 | if (mDatabasesDir.getPath().equals("databases")) { |
| 1156 | mDatabasesDir = new File("/data/system"); |
| 1157 | } |
| 1158 | return mDatabasesDir; |
| 1159 | } |
| 1160 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1162 | @Override |
| 1163 | public Drawable getWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1164 | return getWallpaperManager().getDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
| 1167 | @Override |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1168 | public Drawable peekWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1169 | return getWallpaperManager().peekDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | @Override |
| 1173 | public int getWallpaperDesiredMinimumWidth() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1174 | return getWallpaperManager().getDesiredMinimumWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | @Override |
| 1178 | public int getWallpaperDesiredMinimumHeight() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1179 | return getWallpaperManager().getDesiredMinimumHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | @Override |
| 1183 | public void setWallpaper(Bitmap bitmap) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1184 | getWallpaperManager().setBitmap(bitmap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | @Override |
| 1188 | public void setWallpaper(InputStream data) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1189 | getWallpaperManager().setStream(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | @Override |
| 1193 | public void clearWallpaper() throws IOException { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1194 | getWallpaperManager().clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | @Override |
| 1198 | public void startActivity(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1199 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1200 | startActivity(intent, null); |
| 1201 | } |
| 1202 | |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 1203 | /** @hide */ |
| 1204 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1205 | public void startActivityAsUser(Intent intent, UserHandle user) { |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 1206 | startActivityAsUser(intent, null, user); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1209 | @Override |
| 1210 | public void startActivity(Intent intent, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1211 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 1213 | throw new AndroidRuntimeException( |
| 1214 | "Calling startActivity() from outside of an Activity " |
| 1215 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag." |
| 1216 | + " Is this really what you want?"); |
| 1217 | } |
| 1218 | mMainThread.getInstrumentation().execStartActivity( |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 1219 | getOuterContext(), mMainThread.getApplicationThread(), null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1220 | (Activity)null, intent, -1, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1223 | /** @hide */ |
| 1224 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1225 | public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1226 | try { |
| 1227 | ActivityManagerNative.getDefault().startActivityAsUser( |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 1228 | mMainThread.getApplicationThread(), getBasePackageName(), intent, |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1229 | intent.resolveTypeIfNeeded(getContentResolver()), |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1230 | null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options, |
| 1231 | user.getIdentifier()); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1232 | } catch (RemoteException re) { |
| 1233 | } |
| 1234 | } |
| 1235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | @Override |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 1237 | public void startActivities(Intent[] intents) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1238 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1239 | startActivities(intents, null); |
| 1240 | } |
| 1241 | |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 1242 | /** @hide */ |
| 1243 | @Override |
| 1244 | public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) { |
| 1245 | if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 1246 | throw new AndroidRuntimeException( |
| 1247 | "Calling startActivities() from outside of an Activity " |
| 1248 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent." |
| 1249 | + " Is this really what you want?"); |
| 1250 | } |
| 1251 | mMainThread.getInstrumentation().execStartActivitiesAsUser( |
| 1252 | getOuterContext(), mMainThread.getApplicationThread(), null, |
| 1253 | (Activity)null, intents, options, userHandle.getIdentifier()); |
| 1254 | } |
| 1255 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1256 | @Override |
| 1257 | public void startActivities(Intent[] intents, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1258 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 1259 | if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 1260 | throw new AndroidRuntimeException( |
| 1261 | "Calling startActivities() from outside of an Activity " |
| 1262 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent." |
| 1263 | + " Is this really what you want?"); |
| 1264 | } |
| 1265 | mMainThread.getInstrumentation().execStartActivities( |
| 1266 | getOuterContext(), mMainThread.getApplicationThread(), null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1267 | (Activity)null, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | @Override |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1271 | public void startIntentSender(IntentSender intent, |
| 1272 | Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
| 1273 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1274 | startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null); |
| 1275 | } |
| 1276 | |
| 1277 | @Override |
| 1278 | public void startIntentSender(IntentSender intent, Intent fillInIntent, |
| 1279 | int flagsMask, int flagsValues, int extraFlags, Bundle options) |
| 1280 | throws IntentSender.SendIntentException { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1281 | try { |
| 1282 | String resolvedType = null; |
| 1283 | if (fillInIntent != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1284 | fillInIntent.migrateExtraStreamToClipData(); |
| 1285 | fillInIntent.prepareToLeaveProcess(); |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1286 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 1287 | } |
| 1288 | int result = ActivityManagerNative.getDefault() |
| 1289 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
| 1290 | fillInIntent, resolvedType, null, null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 1291 | 0, flagsMask, flagsValues, options); |
| 1292 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1293 | throw new IntentSender.SendIntentException(); |
| 1294 | } |
| 1295 | Instrumentation.checkStartActivityResult(result, null); |
| 1296 | } catch (RemoteException e) { |
| 1297 | } |
| 1298 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1299 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 1300 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | public void sendBroadcast(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1302 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1304 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1305 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1307 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1308 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, false, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1309 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | } catch (RemoteException e) { |
| 1311 | } |
| 1312 | } |
| 1313 | |
Amith Yamasani | 67cf7d3 | 2012-02-16 14:31:23 -0800 | [diff] [blame] | 1314 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | public void sendBroadcast(Intent intent, String receiverPermission) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1316 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1317 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1318 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1319 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1320 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1321 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1322 | Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, |
| 1323 | false, false, getUserId()); |
| 1324 | } catch (RemoteException e) { |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | @Override |
| 1329 | public void sendBroadcast(Intent intent, String receiverPermission, int appOp) { |
| 1330 | warnIfCallingFromSystemProcess(); |
| 1331 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1332 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1333 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1334 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1335 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 1336 | Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1337 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | } catch (RemoteException e) { |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | @Override |
| 1343 | public void sendOrderedBroadcast(Intent intent, |
| 1344 | String receiverPermission) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1345 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1347 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1348 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1350 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1351 | Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, true, false, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1352 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | } catch (RemoteException e) { |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | @Override |
| 1358 | public void sendOrderedBroadcast(Intent intent, |
| 1359 | String receiverPermission, BroadcastReceiver resultReceiver, |
| 1360 | Handler scheduler, int initialCode, String initialData, |
| 1361 | Bundle initialExtras) { |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1362 | sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE, |
| 1363 | resultReceiver, scheduler, initialCode, initialData, initialExtras); |
| 1364 | } |
| 1365 | |
| 1366 | @Override |
| 1367 | public void sendOrderedBroadcast(Intent intent, |
| 1368 | String receiverPermission, int appOp, BroadcastReceiver resultReceiver, |
| 1369 | Handler scheduler, int initialCode, String initialData, |
| 1370 | Bundle initialExtras) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1371 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | IIntentReceiver rd = null; |
| 1373 | if (resultReceiver != null) { |
| 1374 | if (mPackageInfo != null) { |
| 1375 | if (scheduler == null) { |
| 1376 | scheduler = mMainThread.getHandler(); |
| 1377 | } |
| 1378 | rd = mPackageInfo.getReceiverDispatcher( |
| 1379 | resultReceiver, getOuterContext(), scheduler, |
| 1380 | mMainThread.getInstrumentation(), false); |
| 1381 | } else { |
| 1382 | if (scheduler == null) { |
| 1383 | scheduler = mMainThread.getHandler(); |
| 1384 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1385 | rd = new LoadedApk.ReceiverDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1386 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1387 | } |
| 1388 | } |
| 1389 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1390 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1391 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1393 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1394 | initialCode, initialData, initialExtras, receiverPermission, appOp, |
| 1395 | true, false, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | } catch (RemoteException e) { |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1401 | public void sendBroadcastAsUser(Intent intent, UserHandle user) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1402 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1403 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1404 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1405 | ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(), |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1406 | intent, resolvedType, null, Activity.RESULT_OK, null, null, null, |
| 1407 | AppOpsManager.OP_NONE, false, false, user.getIdentifier()); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1408 | } catch (RemoteException e) { |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1413 | public void sendBroadcastAsUser(Intent intent, UserHandle user, |
| 1414 | String receiverPermission) { |
| 1415 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1416 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1417 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1418 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1419 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1420 | Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, false, false, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1421 | user.getIdentifier()); |
| 1422 | } catch (RemoteException e) { |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1427 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1428 | String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1429 | int initialCode, String initialData, Bundle initialExtras) { |
Amith Yamasani | 3cf7572 | 2014-05-16 12:37:29 -0700 | [diff] [blame] | 1430 | sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE, |
| 1431 | resultReceiver, scheduler, initialCode, initialData, initialExtras); |
| 1432 | } |
| 1433 | |
| 1434 | @Override |
| 1435 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
| 1436 | String receiverPermission, int appOp, BroadcastReceiver resultReceiver, |
| 1437 | Handler scheduler, |
| 1438 | int initialCode, String initialData, Bundle initialExtras) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1439 | IIntentReceiver rd = null; |
| 1440 | if (resultReceiver != null) { |
| 1441 | if (mPackageInfo != null) { |
| 1442 | if (scheduler == null) { |
| 1443 | scheduler = mMainThread.getHandler(); |
| 1444 | } |
| 1445 | rd = mPackageInfo.getReceiverDispatcher( |
| 1446 | resultReceiver, getOuterContext(), scheduler, |
| 1447 | mMainThread.getInstrumentation(), false); |
| 1448 | } else { |
| 1449 | if (scheduler == null) { |
| 1450 | scheduler = mMainThread.getHandler(); |
| 1451 | } |
| 1452 | rd = new LoadedApk.ReceiverDispatcher( |
| 1453 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1454 | } |
| 1455 | } |
| 1456 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1457 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1458 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1459 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1460 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1461 | initialCode, initialData, initialExtras, receiverPermission, |
Amith Yamasani | 0261b9b | 2014-05-20 16:57:26 -0700 | [diff] [blame] | 1462 | appOp, true, false, user.getIdentifier()); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1463 | } catch (RemoteException e) { |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | public void sendStickyBroadcast(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1469 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1471 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1472 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1474 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1475 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1476 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | } catch (RemoteException e) { |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | @Override |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1482 | public void sendStickyOrderedBroadcast(Intent intent, |
| 1483 | BroadcastReceiver resultReceiver, |
| 1484 | Handler scheduler, int initialCode, String initialData, |
| 1485 | Bundle initialExtras) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1486 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1487 | IIntentReceiver rd = null; |
| 1488 | if (resultReceiver != null) { |
| 1489 | if (mPackageInfo != null) { |
| 1490 | if (scheduler == null) { |
| 1491 | scheduler = mMainThread.getHandler(); |
| 1492 | } |
| 1493 | rd = mPackageInfo.getReceiverDispatcher( |
| 1494 | resultReceiver, getOuterContext(), scheduler, |
| 1495 | mMainThread.getInstrumentation(), false); |
| 1496 | } else { |
| 1497 | if (scheduler == null) { |
| 1498 | scheduler = mMainThread.getHandler(); |
| 1499 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1500 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1501 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1502 | } |
| 1503 | } |
| 1504 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1505 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1506 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1507 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1508 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1509 | initialCode, initialData, initialExtras, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1510 | AppOpsManager.OP_NONE, true, true, getUserId()); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1511 | } catch (RemoteException e) { |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | public void removeStickyBroadcast(Intent intent) { |
| 1517 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1518 | if (resolvedType != null) { |
| 1519 | intent = new Intent(intent); |
| 1520 | intent.setDataAndType(intent.getData(), resolvedType); |
| 1521 | } |
| 1522 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1523 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | ActivityManagerNative.getDefault().unbroadcastIntent( |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1525 | mMainThread.getApplicationThread(), intent, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | } catch (RemoteException e) { |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1531 | public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { |
| 1532 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1533 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1534 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1535 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1536 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1537 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1538 | } catch (RemoteException e) { |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | @Override |
| 1543 | public void sendStickyOrderedBroadcastAsUser(Intent intent, |
| 1544 | UserHandle user, BroadcastReceiver resultReceiver, |
| 1545 | Handler scheduler, int initialCode, String initialData, |
| 1546 | Bundle initialExtras) { |
| 1547 | IIntentReceiver rd = null; |
| 1548 | if (resultReceiver != null) { |
| 1549 | if (mPackageInfo != null) { |
| 1550 | if (scheduler == null) { |
| 1551 | scheduler = mMainThread.getHandler(); |
| 1552 | } |
| 1553 | rd = mPackageInfo.getReceiverDispatcher( |
| 1554 | resultReceiver, getOuterContext(), scheduler, |
| 1555 | mMainThread.getInstrumentation(), false); |
| 1556 | } else { |
| 1557 | if (scheduler == null) { |
| 1558 | scheduler = mMainThread.getHandler(); |
| 1559 | } |
| 1560 | rd = new LoadedApk.ReceiverDispatcher( |
| 1561 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1562 | } |
| 1563 | } |
| 1564 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1565 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1566 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1567 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1568 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1569 | initialCode, initialData, initialExtras, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1570 | AppOpsManager.OP_NONE, true, true, user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1571 | } catch (RemoteException e) { |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | @Override |
| 1576 | public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { |
| 1577 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1578 | if (resolvedType != null) { |
| 1579 | intent = new Intent(intent); |
| 1580 | intent.setDataAndType(intent.getData(), resolvedType); |
| 1581 | } |
| 1582 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1583 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1584 | ActivityManagerNative.getDefault().unbroadcastIntent( |
| 1585 | mMainThread.getApplicationThread(), intent, user.getIdentifier()); |
| 1586 | } catch (RemoteException e) { |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 1592 | return registerReceiver(receiver, filter, null, null); |
| 1593 | } |
| 1594 | |
| 1595 | @Override |
| 1596 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 1597 | String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1598 | return registerReceiverInternal(receiver, getUserId(), |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1599 | filter, broadcastPermission, scheduler, getOuterContext()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | } |
| 1601 | |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1602 | @Override |
| 1603 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 1604 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 1605 | return registerReceiverInternal(receiver, user.getIdentifier(), |
| 1606 | filter, broadcastPermission, scheduler, getOuterContext()); |
| 1607 | } |
| 1608 | |
| 1609 | private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | IntentFilter filter, String broadcastPermission, |
| 1611 | Handler scheduler, Context context) { |
| 1612 | IIntentReceiver rd = null; |
| 1613 | if (receiver != null) { |
| 1614 | if (mPackageInfo != null && context != null) { |
| 1615 | if (scheduler == null) { |
| 1616 | scheduler = mMainThread.getHandler(); |
| 1617 | } |
| 1618 | rd = mPackageInfo.getReceiverDispatcher( |
| 1619 | receiver, context, scheduler, |
| 1620 | mMainThread.getInstrumentation(), true); |
| 1621 | } else { |
| 1622 | if (scheduler == null) { |
| 1623 | scheduler = mMainThread.getHandler(); |
| 1624 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1625 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 1626 | receiver, context, scheduler, null, true).getIIntentReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | try { |
| 1630 | return ActivityManagerNative.getDefault().registerReceiver( |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1631 | mMainThread.getApplicationThread(), mBasePackageName, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1632 | rd, filter, broadcastPermission, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1633 | } catch (RemoteException e) { |
| 1634 | return null; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | @Override |
| 1639 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 1640 | if (mPackageInfo != null) { |
| 1641 | IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher( |
| 1642 | getOuterContext(), receiver); |
| 1643 | try { |
| 1644 | ActivityManagerNative.getDefault().unregisterReceiver(rd); |
| 1645 | } catch (RemoteException e) { |
| 1646 | } |
| 1647 | } else { |
| 1648 | throw new RuntimeException("Not supported in system context"); |
| 1649 | } |
| 1650 | } |
| 1651 | |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1652 | private void validateServiceIntent(Intent service) { |
| 1653 | if (service.getComponent() == null && service.getPackage() == null) { |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1654 | if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.L) { |
| 1655 | IllegalArgumentException ex = new IllegalArgumentException( |
| 1656 | "Service Intent must be explicit: " + service); |
| 1657 | throw ex; |
| 1658 | } else { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1659 | Log.w(TAG, "Implicit intents with startService are not safe: " + service |
| 1660 | + " " + Debug.getCallers(2, 3)); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | @Override |
| 1666 | public ComponentName startService(Intent service) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1667 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1668 | return startServiceCommon(service, mUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | @Override |
| 1672 | public boolean stopService(Intent service) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1673 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1674 | return stopServiceCommon(service, mUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | @Override |
| 1678 | public ComponentName startServiceAsUser(Intent service, UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1679 | return startServiceCommon(service, user); |
| 1680 | } |
| 1681 | |
| 1682 | private ComponentName startServiceCommon(Intent service, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1683 | try { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1684 | validateServiceIntent(service); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1685 | service.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | ComponentName cn = ActivityManagerNative.getDefault().startService( |
| 1687 | mMainThread.getApplicationThread(), service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1688 | service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier()); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1689 | if (cn != null) { |
| 1690 | if (cn.getPackageName().equals("!")) { |
| 1691 | throw new SecurityException( |
| 1692 | "Not allowed to start service " + service |
| 1693 | + " without permission " + cn.getClassName()); |
| 1694 | } else if (cn.getPackageName().equals("!!")) { |
| 1695 | throw new SecurityException( |
| 1696 | "Unable to start service " + service |
| 1697 | + ": " + cn.getClassName()); |
| 1698 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | } |
| 1700 | return cn; |
| 1701 | } catch (RemoteException e) { |
| 1702 | return null; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | @Override |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1707 | public boolean stopServiceAsUser(Intent service, UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1708 | return stopServiceCommon(service, user); |
| 1709 | } |
| 1710 | |
| 1711 | private boolean stopServiceCommon(Intent service, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1712 | try { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1713 | validateServiceIntent(service); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1714 | service.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1715 | int res = ActivityManagerNative.getDefault().stopService( |
| 1716 | mMainThread.getApplicationThread(), service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1717 | service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1718 | if (res < 0) { |
| 1719 | throw new SecurityException( |
| 1720 | "Not allowed to stop service " + service); |
| 1721 | } |
| 1722 | return res != 0; |
| 1723 | } catch (RemoteException e) { |
| 1724 | return false; |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | @Override |
| 1729 | public boolean bindService(Intent service, ServiceConnection conn, |
| 1730 | int flags) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1731 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1732 | return bindServiceCommon(service, conn, flags, Process.myUserHandle()); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | /** @hide */ |
| 1736 | @Override |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1737 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 1738 | UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1739 | return bindServiceCommon(service, conn, flags, user); |
| 1740 | } |
| 1741 | |
| 1742 | private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, |
| 1743 | UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1744 | IServiceConnection sd; |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1745 | if (conn == null) { |
| 1746 | throw new IllegalArgumentException("connection is null"); |
| 1747 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1748 | if (mPackageInfo != null) { |
| 1749 | sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), |
| 1750 | mMainThread.getHandler(), flags); |
| 1751 | } else { |
| 1752 | throw new RuntimeException("Not supported in system context"); |
| 1753 | } |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1754 | validateServiceIntent(service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1756 | IBinder token = getActivityToken(); |
| 1757 | if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null |
| 1758 | && mPackageInfo.getApplicationInfo().targetSdkVersion |
| 1759 | < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 1760 | flags |= BIND_WAIVE_PRIORITY; |
| 1761 | } |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1762 | service.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1763 | int res = ActivityManagerNative.getDefault().bindService( |
| 1764 | mMainThread.getApplicationThread(), getActivityToken(), |
| 1765 | service, service.resolveTypeIfNeeded(getContentResolver()), |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1766 | sd, flags, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1767 | if (res < 0) { |
| 1768 | throw new SecurityException( |
| 1769 | "Not allowed to bind to service " + service); |
| 1770 | } |
| 1771 | return res != 0; |
| 1772 | } catch (RemoteException e) { |
| 1773 | return false; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | @Override |
| 1778 | public void unbindService(ServiceConnection conn) { |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1779 | if (conn == null) { |
| 1780 | throw new IllegalArgumentException("connection is null"); |
| 1781 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1782 | if (mPackageInfo != null) { |
| 1783 | IServiceConnection sd = mPackageInfo.forgetServiceDispatcher( |
| 1784 | getOuterContext(), conn); |
| 1785 | try { |
| 1786 | ActivityManagerNative.getDefault().unbindService(sd); |
| 1787 | } catch (RemoteException e) { |
| 1788 | } |
| 1789 | } else { |
| 1790 | throw new RuntimeException("Not supported in system context"); |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | @Override |
| 1795 | public boolean startInstrumentation(ComponentName className, |
| 1796 | String profileFile, Bundle arguments) { |
| 1797 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1798 | if (arguments != null) { |
| 1799 | arguments.setAllowFds(false); |
| 1800 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | return ActivityManagerNative.getDefault().startInstrumentation( |
Narayan Kamath | 8dcfefd | 2014-05-15 18:12:59 +0100 | [diff] [blame] | 1802 | className, profileFile, 0, arguments, null, null, getUserId(), |
| 1803 | null /* ABI override */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | } catch (RemoteException e) { |
| 1805 | // System has crashed, nothing we can do. |
| 1806 | } |
| 1807 | return false; |
| 1808 | } |
| 1809 | |
| 1810 | @Override |
| 1811 | public Object getSystemService(String name) { |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 1812 | ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name); |
| 1813 | return fetcher == null ? null : fetcher.getService(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1814 | } |
| 1815 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1816 | private WallpaperManager getWallpaperManager() { |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 1817 | return (WallpaperManager) WALLPAPER_FETCHER.getService(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1818 | } |
| 1819 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1820 | /* package */ static DropBoxManager createDropBoxManager() { |
| 1821 | IBinder b = ServiceManager.getService(DROPBOX_SERVICE); |
| 1822 | IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b); |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1823 | if (service == null) { |
| 1824 | // Don't return a DropBoxManager that will NPE upon use. |
| 1825 | // This also avoids caching a broken DropBoxManager in |
| 1826 | // getDropBoxManager during early boot, before the |
| 1827 | // DROPBOX_SERVICE is registered. |
| 1828 | return null; |
| 1829 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1830 | return new DropBoxManager(service); |
| 1831 | } |
| 1832 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1833 | @Override |
| 1834 | public int checkPermission(String permission, int pid, int uid) { |
| 1835 | if (permission == null) { |
| 1836 | throw new IllegalArgumentException("permission is null"); |
| 1837 | } |
| 1838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | try { |
| 1840 | return ActivityManagerNative.getDefault().checkPermission( |
| 1841 | permission, pid, uid); |
| 1842 | } catch (RemoteException e) { |
| 1843 | return PackageManager.PERMISSION_DENIED; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | @Override |
| 1848 | public int checkCallingPermission(String permission) { |
| 1849 | if (permission == null) { |
| 1850 | throw new IllegalArgumentException("permission is null"); |
| 1851 | } |
| 1852 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1853 | int pid = Binder.getCallingPid(); |
| 1854 | if (pid != Process.myPid()) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1855 | return checkPermission(permission, pid, Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1856 | } |
| 1857 | return PackageManager.PERMISSION_DENIED; |
| 1858 | } |
| 1859 | |
| 1860 | @Override |
| 1861 | public int checkCallingOrSelfPermission(String permission) { |
| 1862 | if (permission == null) { |
| 1863 | throw new IllegalArgumentException("permission is null"); |
| 1864 | } |
| 1865 | |
| 1866 | return checkPermission(permission, Binder.getCallingPid(), |
| 1867 | Binder.getCallingUid()); |
| 1868 | } |
| 1869 | |
| 1870 | private void enforce( |
| 1871 | String permission, int resultOfCheck, |
| 1872 | boolean selfToo, int uid, String message) { |
| 1873 | if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { |
| 1874 | throw new SecurityException( |
| 1875 | (message != null ? (message + ": ") : "") + |
| 1876 | (selfToo |
| 1877 | ? "Neither user " + uid + " nor current process has " |
Christopher Tate | 4dc7a68 | 2012-09-11 12:15:49 -0700 | [diff] [blame] | 1878 | : "uid " + uid + " does not have ") + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1879 | permission + |
| 1880 | "."); |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | public void enforcePermission( |
| 1885 | String permission, int pid, int uid, String message) { |
| 1886 | enforce(permission, |
| 1887 | checkPermission(permission, pid, uid), |
| 1888 | false, |
| 1889 | uid, |
| 1890 | message); |
| 1891 | } |
| 1892 | |
| 1893 | public void enforceCallingPermission(String permission, String message) { |
| 1894 | enforce(permission, |
| 1895 | checkCallingPermission(permission), |
| 1896 | false, |
| 1897 | Binder.getCallingUid(), |
| 1898 | message); |
| 1899 | } |
| 1900 | |
| 1901 | public void enforceCallingOrSelfPermission( |
| 1902 | String permission, String message) { |
| 1903 | enforce(permission, |
| 1904 | checkCallingOrSelfPermission(permission), |
| 1905 | true, |
| 1906 | Binder.getCallingUid(), |
| 1907 | message); |
| 1908 | } |
| 1909 | |
| 1910 | @Override |
| 1911 | public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { |
| 1912 | try { |
| 1913 | ActivityManagerNative.getDefault().grantUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1914 | mMainThread.getApplicationThread(), toPackage, |
| 1915 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1916 | } catch (RemoteException e) { |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | @Override |
| 1921 | public void revokeUriPermission(Uri uri, int modeFlags) { |
| 1922 | try { |
| 1923 | ActivityManagerNative.getDefault().revokeUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1924 | mMainThread.getApplicationThread(), |
| 1925 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1926 | } catch (RemoteException e) { |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | @Override |
| 1931 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | try { |
| 1933 | return ActivityManagerNative.getDefault().checkUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1934 | ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags, |
| 1935 | resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1936 | } catch (RemoteException e) { |
| 1937 | return PackageManager.PERMISSION_DENIED; |
| 1938 | } |
| 1939 | } |
| 1940 | |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1941 | private int resolveUserId(Uri uri) { |
| 1942 | return ContentProvider.getUserIdFromUri(uri, getUserId()); |
| 1943 | } |
| 1944 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1945 | @Override |
| 1946 | public int checkCallingUriPermission(Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1947 | int pid = Binder.getCallingPid(); |
| 1948 | if (pid != Process.myPid()) { |
| 1949 | return checkUriPermission(uri, pid, |
| 1950 | Binder.getCallingUid(), modeFlags); |
| 1951 | } |
| 1952 | return PackageManager.PERMISSION_DENIED; |
| 1953 | } |
| 1954 | |
| 1955 | @Override |
| 1956 | public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { |
| 1957 | return checkUriPermission(uri, Binder.getCallingPid(), |
| 1958 | Binder.getCallingUid(), modeFlags); |
| 1959 | } |
| 1960 | |
| 1961 | @Override |
| 1962 | public int checkUriPermission(Uri uri, String readPermission, |
| 1963 | String writePermission, int pid, int uid, int modeFlags) { |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 1964 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission=" |
| 1966 | + readPermission + " writePermission=" + writePermission |
| 1967 | + " pid=" + pid + " uid=" + uid + " mode" + modeFlags); |
| 1968 | } |
| 1969 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 1970 | if (readPermission == null |
| 1971 | || checkPermission(readPermission, pid, uid) |
| 1972 | == PackageManager.PERMISSION_GRANTED) { |
| 1973 | return PackageManager.PERMISSION_GRANTED; |
| 1974 | } |
| 1975 | } |
| 1976 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 1977 | if (writePermission == null |
| 1978 | || checkPermission(writePermission, pid, uid) |
| 1979 | == PackageManager.PERMISSION_GRANTED) { |
| 1980 | return PackageManager.PERMISSION_GRANTED; |
| 1981 | } |
| 1982 | } |
| 1983 | return uri != null ? checkUriPermission(uri, pid, uid, modeFlags) |
| 1984 | : PackageManager.PERMISSION_DENIED; |
| 1985 | } |
| 1986 | |
| 1987 | private String uriModeFlagToString(int uriModeFlags) { |
Jeff Sharkey | 846318a | 2014-04-04 12:12:41 -0700 | [diff] [blame] | 1988 | StringBuilder builder = new StringBuilder(); |
| 1989 | if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 1990 | builder.append("read and "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1991 | } |
Jeff Sharkey | 846318a | 2014-04-04 12:12:41 -0700 | [diff] [blame] | 1992 | if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 1993 | builder.append("write and "); |
| 1994 | } |
| 1995 | if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) { |
| 1996 | builder.append("persistable and "); |
| 1997 | } |
| 1998 | if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) { |
| 1999 | builder.append("prefix and "); |
| 2000 | } |
| 2001 | |
| 2002 | if (builder.length() > 5) { |
| 2003 | builder.setLength(builder.length() - 5); |
| 2004 | return builder.toString(); |
| 2005 | } else { |
| 2006 | throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags); |
| 2007 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | private void enforceForUri( |
| 2011 | int modeFlags, int resultOfCheck, boolean selfToo, |
| 2012 | int uid, Uri uri, String message) { |
| 2013 | if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { |
| 2014 | throw new SecurityException( |
| 2015 | (message != null ? (message + ": ") : "") + |
| 2016 | (selfToo |
| 2017 | ? "Neither user " + uid + " nor current process has " |
| 2018 | : "User " + uid + " does not have ") + |
| 2019 | uriModeFlagToString(modeFlags) + |
| 2020 | " permission on " + |
| 2021 | uri + |
| 2022 | "."); |
| 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | public void enforceUriPermission( |
| 2027 | Uri uri, int pid, int uid, int modeFlags, String message) { |
| 2028 | enforceForUri( |
| 2029 | modeFlags, checkUriPermission(uri, pid, uid, modeFlags), |
| 2030 | false, uid, uri, message); |
| 2031 | } |
| 2032 | |
| 2033 | public void enforceCallingUriPermission( |
| 2034 | Uri uri, int modeFlags, String message) { |
| 2035 | enforceForUri( |
| 2036 | modeFlags, checkCallingUriPermission(uri, modeFlags), |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 2037 | false, |
| 2038 | Binder.getCallingUid(), uri, message); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | public void enforceCallingOrSelfUriPermission( |
| 2042 | Uri uri, int modeFlags, String message) { |
| 2043 | enforceForUri( |
| 2044 | modeFlags, |
| 2045 | checkCallingOrSelfUriPermission(uri, modeFlags), true, |
| 2046 | Binder.getCallingUid(), uri, message); |
| 2047 | } |
| 2048 | |
| 2049 | public void enforceUriPermission( |
| 2050 | Uri uri, String readPermission, String writePermission, |
| 2051 | int pid, int uid, int modeFlags, String message) { |
| 2052 | enforceForUri(modeFlags, |
| 2053 | checkUriPermission( |
| 2054 | uri, readPermission, writePermission, pid, uid, |
| 2055 | modeFlags), |
| 2056 | false, |
| 2057 | uid, |
| 2058 | uri, |
| 2059 | message); |
| 2060 | } |
| 2061 | |
Tom O'Neill | 365632e | 2013-09-09 09:34:58 -0700 | [diff] [blame] | 2062 | /** |
| 2063 | * Logs a warning if the system process directly called a method such as |
| 2064 | * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}. |
| 2065 | * The "AsUser" variants allow us to properly enforce the user's restrictions. |
| 2066 | */ |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 2067 | private void warnIfCallingFromSystemProcess() { |
| 2068 | if (Process.myUid() == Process.SYSTEM_UID) { |
| 2069 | Slog.w(TAG, "Calling a method in the system process without a qualified user: " |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 2070 | + Debug.getCallers(5)); |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 2071 | } |
| 2072 | } |
| 2073 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2074 | @Override |
| 2075 | public Context createPackageContext(String packageName, int flags) |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2076 | throws NameNotFoundException { |
Amith Yamasani | 64442c1 | 2012-10-07 08:17:46 -0700 | [diff] [blame] | 2077 | return createPackageContextAsUser(packageName, flags, |
| 2078 | mUser != null ? mUser : Process.myUserHandle()); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | @Override |
| 2082 | public Context createPackageContextAsUser(String packageName, int flags, UserHandle user) |
| 2083 | throws NameNotFoundException { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2084 | final boolean restricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2085 | if (packageName.equals("system") || packageName.equals("android")) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2086 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
| 2087 | user, restricted, mDisplay, mOverrideConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | } |
| 2089 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2090 | LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), |
| 2091 | flags, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2092 | if (pi != null) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2093 | ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken, |
| 2094 | user, restricted, mDisplay, mOverrideConfiguration); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2095 | if (c.mResources != null) { |
| 2096 | return c; |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | // Should be a better exception. |
| 2101 | throw new PackageManager.NameNotFoundException( |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2102 | "Application package " + packageName + " not found"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2103 | } |
| 2104 | |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 2105 | @Override |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 2106 | public Context createConfigurationContext(Configuration overrideConfiguration) { |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 2107 | if (overrideConfiguration == null) { |
| 2108 | throw new IllegalArgumentException("overrideConfiguration must not be null"); |
| 2109 | } |
| 2110 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2111 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
| 2112 | mUser, mRestricted, mDisplay, overrideConfiguration); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | @Override |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 2116 | public Context createDisplayContext(Display display) { |
| 2117 | if (display == null) { |
| 2118 | throw new IllegalArgumentException("display must not be null"); |
| 2119 | } |
| 2120 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2121 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
| 2122 | mUser, mRestricted, display, mOverrideConfiguration); |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | private int getDisplayId() { |
| 2126 | return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY; |
| 2127 | } |
| 2128 | |
| 2129 | @Override |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 2130 | public boolean isRestricted() { |
| 2131 | return mRestricted; |
| 2132 | } |
| 2133 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 2134 | @Override |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 2135 | public DisplayAdjustments getDisplayAdjustments(int displayId) { |
| 2136 | return mDisplayAdjustments; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2139 | private File getDataDirFile() { |
| 2140 | if (mPackageInfo != null) { |
| 2141 | return mPackageInfo.getDataDirFile(); |
| 2142 | } |
| 2143 | throw new RuntimeException("Not supported in system context"); |
| 2144 | } |
| 2145 | |
| 2146 | @Override |
| 2147 | public File getDir(String name, int mode) { |
| 2148 | name = "app_" + name; |
| 2149 | File file = makeFilename(getDataDirFile(), name); |
| 2150 | if (!file.exists()) { |
| 2151 | file.mkdir(); |
| 2152 | setFilePermissionsFromMode(file.getPath(), mode, |
| 2153 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH); |
| 2154 | } |
| 2155 | return file; |
| 2156 | } |
| 2157 | |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 2158 | /** {@hide} */ |
| 2159 | public int getUserId() { |
| 2160 | return mUser.getIdentifier(); |
| 2161 | } |
| 2162 | |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 2163 | static ContextImpl createSystemContext(ActivityThread mainThread) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2164 | LoadedApk packageInfo = new LoadedApk(mainThread); |
| 2165 | ContextImpl context = new ContextImpl(null, mainThread, |
| 2166 | packageInfo, null, null, false, null, null); |
| 2167 | context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(), |
| 2168 | context.mResourcesManager.getDisplayMetricsLocked(Display.DEFAULT_DISPLAY)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2169 | return context; |
| 2170 | } |
| 2171 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2172 | static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) { |
| 2173 | if (packageInfo == null) throw new IllegalArgumentException("packageInfo"); |
| 2174 | return new ContextImpl(null, mainThread, |
| 2175 | packageInfo, null, null, false, null, null); |
| 2176 | } |
| 2177 | |
| 2178 | static ContextImpl createActivityContext(ActivityThread mainThread, |
| 2179 | LoadedApk packageInfo, IBinder activityToken) { |
| 2180 | if (packageInfo == null) throw new IllegalArgumentException("packageInfo"); |
| 2181 | if (activityToken == null) throw new IllegalArgumentException("activityInfo"); |
| 2182 | return new ContextImpl(null, mainThread, |
| 2183 | packageInfo, activityToken, null, false, null, null); |
| 2184 | } |
| 2185 | |
| 2186 | private ContextImpl(ContextImpl container, ActivityThread mainThread, |
| 2187 | LoadedApk packageInfo, IBinder activityToken, UserHandle user, boolean restricted, |
| 2188 | Display display, Configuration overrideConfiguration) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2189 | mOuterContext = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2190 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2191 | mMainThread = mainThread; |
| 2192 | mActivityToken = activityToken; |
| 2193 | mRestricted = restricted; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2194 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2195 | if (user == null) { |
| 2196 | user = Process.myUserHandle(); |
| 2197 | } |
| 2198 | mUser = user; |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 2199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2200 | mPackageInfo = packageInfo; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2201 | mContentResolver = new ApplicationContentResolver(this, mainThread, user); |
| 2202 | mResourcesManager = ResourcesManager.getInstance(); |
| 2203 | mDisplay = display; |
| 2204 | mOverrideConfiguration = overrideConfiguration; |
| 2205 | |
| 2206 | final int displayId = getDisplayId(); |
| 2207 | CompatibilityInfo compatInfo = null; |
| 2208 | if (container != null) { |
| 2209 | compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo(); |
| 2210 | } |
| 2211 | if (compatInfo == null && displayId == Display.DEFAULT_DISPLAY) { |
| 2212 | compatInfo = packageInfo.getCompatibilityInfo(); |
| 2213 | } |
| 2214 | mDisplayAdjustments.setCompatibilityInfo(compatInfo); |
| 2215 | mDisplayAdjustments.setActivityToken(activityToken); |
| 2216 | |
| 2217 | Resources resources = packageInfo.getResources(mainThread); |
| 2218 | if (resources != null) { |
| 2219 | if (activityToken != null |
| 2220 | || displayId != Display.DEFAULT_DISPLAY |
| 2221 | || overrideConfiguration != null |
| 2222 | || (compatInfo != null && compatInfo.applicationScale |
| 2223 | != resources.getCompatibilityInfo().applicationScale)) { |
Jeff Sharkey | 8a4c972 | 2014-06-16 13:48:42 -0700 | [diff] [blame] | 2224 | resources = mResourcesManager.getTopLevelResources(packageInfo.getResDir(), |
| 2225 | packageInfo.getSplitResDirs(), packageInfo.getOverlayDirs(), |
| 2226 | packageInfo.getApplicationInfo().sharedLibraryFiles, displayId, |
| 2227 | overrideConfiguration, compatInfo, activityToken); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | mResources = resources; |
| 2231 | |
| 2232 | if (container != null) { |
| 2233 | mBasePackageName = container.mBasePackageName; |
| 2234 | mOpPackageName = container.mOpPackageName; |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 2235 | } else { |
| 2236 | mBasePackageName = packageInfo.mPackageName; |
| 2237 | ApplicationInfo ainfo = packageInfo.getApplicationInfo(); |
| 2238 | if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) { |
| 2239 | // Special case: system components allow themselves to be loaded in to other |
| 2240 | // processes. For purposes of app ops, we must then consider the context as |
| 2241 | // belonging to the package of this process, not the system itself, otherwise |
| 2242 | // the package+uid verifications in app ops will fail. |
| 2243 | mOpPackageName = ActivityThread.currentPackageName(); |
| 2244 | } else { |
| 2245 | mOpPackageName = mBasePackageName; |
| 2246 | } |
| 2247 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2248 | } |
| 2249 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2250 | void installSystemApplicationInfo(ApplicationInfo info) { |
| 2251 | mPackageInfo.installSystemApplicationInfo(info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2252 | } |
| 2253 | |
| 2254 | final void scheduleFinalCleanup(String who, String what) { |
| 2255 | mMainThread.scheduleContextCleanup(this, who, what); |
| 2256 | } |
| 2257 | |
| 2258 | final void performFinalCleanup(String who, String what) { |
| 2259 | //Log.i(TAG, "Cleanup up context: " + this); |
| 2260 | mPackageInfo.removeContextRegistrations(getOuterContext(), who, what); |
| 2261 | } |
| 2262 | |
| 2263 | final Context getReceiverRestrictedContext() { |
| 2264 | if (mReceiverRestrictedContext != null) { |
| 2265 | return mReceiverRestrictedContext; |
| 2266 | } |
| 2267 | return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext()); |
| 2268 | } |
| 2269 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2270 | final void setOuterContext(Context context) { |
| 2271 | mOuterContext = context; |
| 2272 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2274 | final Context getOuterContext() { |
| 2275 | return mOuterContext; |
| 2276 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2278 | final IBinder getActivityToken() { |
| 2279 | return mActivityToken; |
| 2280 | } |
| 2281 | |
Brad Fitzpatrick | d3da440 | 2010-11-10 08:27:11 -0800 | [diff] [blame] | 2282 | static void setFilePermissionsFromMode(String name, int mode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2283 | int extraPermissions) { |
| 2284 | int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR |
| 2285 | |FileUtils.S_IRGRP|FileUtils.S_IWGRP |
| 2286 | |extraPermissions; |
| 2287 | if ((mode&MODE_WORLD_READABLE) != 0) { |
| 2288 | perms |= FileUtils.S_IROTH; |
| 2289 | } |
| 2290 | if ((mode&MODE_WORLD_WRITEABLE) != 0) { |
| 2291 | perms |= FileUtils.S_IWOTH; |
| 2292 | } |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 2293 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2294 | Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode) |
| 2295 | + ", perms=0x" + Integer.toHexString(perms)); |
| 2296 | } |
| 2297 | FileUtils.setPermissions(name, perms, -1, -1); |
| 2298 | } |
| 2299 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2300 | private File validateFilePath(String name, boolean createDirectory) { |
| 2301 | File dir; |
| 2302 | File f; |
| 2303 | |
| 2304 | if (name.charAt(0) == File.separatorChar) { |
| 2305 | String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar)); |
| 2306 | dir = new File(dirPath); |
| 2307 | name = name.substring(name.lastIndexOf(File.separatorChar)); |
| 2308 | f = new File(dir, name); |
| 2309 | } else { |
| 2310 | dir = getDatabasesDir(); |
| 2311 | f = makeFilename(dir, name); |
| 2312 | } |
| 2313 | |
| 2314 | if (createDirectory && !dir.isDirectory() && dir.mkdir()) { |
| 2315 | FileUtils.setPermissions(dir.getPath(), |
| 2316 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 2317 | -1, -1); |
| 2318 | } |
| 2319 | |
| 2320 | return f; |
| 2321 | } |
| 2322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2323 | private File makeFilename(File base, String name) { |
| 2324 | if (name.indexOf(File.separatorChar) < 0) { |
| 2325 | return new File(base, name); |
| 2326 | } |
| 2327 | throw new IllegalArgumentException( |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2328 | "File " + name + " contains a path separator"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2329 | } |
| 2330 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2331 | /** |
| 2332 | * Ensure that given directories exist, trying to create them if missing. If |
| 2333 | * unable to create, they are filtered by replacing with {@code null}. |
| 2334 | */ |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2335 | private File[] ensureDirsExistOrFilter(File[] dirs) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2336 | File[] result = new File[dirs.length]; |
| 2337 | for (int i = 0; i < dirs.length; i++) { |
| 2338 | File dir = dirs[i]; |
| 2339 | if (!dir.exists()) { |
| 2340 | if (!dir.mkdirs()) { |
Christopher Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 2341 | // recheck existence in case of cross-process race |
| 2342 | if (!dir.exists()) { |
| 2343 | // Failing to mkdir() may be okay, since we might not have |
| 2344 | // enough permissions; ask vold to create on our behalf. |
| 2345 | final IMountService mount = IMountService.Stub.asInterface( |
| 2346 | ServiceManager.getService("mount")); |
| 2347 | int res = -1; |
| 2348 | try { |
| 2349 | res = mount.mkdirs(getPackageName(), dir.getAbsolutePath()); |
| 2350 | } catch (RemoteException e) { |
| 2351 | } |
| 2352 | if (res != 0) { |
| 2353 | Log.w(TAG, "Failed to ensure directory: " + dir); |
| 2354 | dir = null; |
| 2355 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2356 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2357 | } |
| 2358 | } |
| 2359 | result[i] = dir; |
| 2360 | } |
| 2361 | return result; |
| 2362 | } |
| 2363 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2364 | // ---------------------------------------------------------------------- |
| 2365 | // ---------------------------------------------------------------------- |
| 2366 | // ---------------------------------------------------------------------- |
| 2367 | |
| 2368 | private static final class ApplicationContentResolver extends ContentResolver { |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2369 | private final ActivityThread mMainThread; |
| 2370 | private final UserHandle mUser; |
| 2371 | |
| 2372 | public ApplicationContentResolver( |
| 2373 | Context context, ActivityThread mainThread, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2374 | super(context); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2375 | mMainThread = Preconditions.checkNotNull(mainThread); |
| 2376 | mUser = Preconditions.checkNotNull(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2380 | protected IContentProvider acquireProvider(Context context, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2381 | return mMainThread.acquireProvider(context, |
| 2382 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2383 | resolveUserIdFromAuthority(auth), true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2387 | protected IContentProvider acquireExistingProvider(Context context, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2388 | return mMainThread.acquireExistingProvider(context, |
| 2389 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2390 | resolveUserIdFromAuthority(auth), true); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | @Override |
| 2394 | public boolean releaseProvider(IContentProvider provider) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 2395 | return mMainThread.releaseProvider(provider, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2396 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2397 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2398 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2399 | protected IContentProvider acquireUnstableProvider(Context c, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2400 | return mMainThread.acquireProvider(c, |
| 2401 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2402 | resolveUserIdFromAuthority(auth), false); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | @Override |
| 2406 | public boolean releaseUnstableProvider(IContentProvider icp) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 2407 | return mMainThread.releaseProvider(icp, false); |
| 2408 | } |
| 2409 | |
| 2410 | @Override |
| 2411 | public void unstableProviderDied(IContentProvider icp) { |
| 2412 | mMainThread.handleUnstableProviderDied(icp.asBinder(), true); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2413 | } |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 2414 | |
| 2415 | @Override |
| 2416 | public void appNotRespondingViaProvider(IContentProvider icp) { |
| 2417 | mMainThread.appNotRespondingViaProvider(icp.asBinder()); |
| 2418 | } |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2419 | |
| 2420 | /** @hide */ |
| 2421 | protected int resolveUserIdFromAuthority(String auth) { |
| 2422 | return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier()); |
| 2423 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2424 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2425 | } |