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