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