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