blob: eeee57d12af99269ce90911fee0f79bf9bf0d996 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.app;
18
Dianne Hackbornff32f352013-07-18 17:31:13 -070019import android.os.Build;
svetoslavganov75986cf2009-05-14 22:28:01 -070020import com.android.internal.policy.PolicyManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070021import com.android.internal.util.Preconditions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
Matthew Xieddf7e472013-03-01 18:41:02 -080023import android.bluetooth.BluetoothManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.BroadcastReceiver;
25import android.content.ComponentName;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.ContextWrapper;
29import android.content.IContentProvider;
30import android.content.Intent;
31import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070032import android.content.IIntentReceiver;
33import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.ReceiverCallNotAllowedException;
35import android.content.ServiceConnection;
36import android.content.SharedPreferences;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070040import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070042import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070043import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070045import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.database.sqlite.SQLiteDatabase;
47import android.database.sqlite.SQLiteDatabase.CursorFactory;
48import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.graphics.drawable.Drawable;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040050import android.hardware.ISerialManager;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040051import android.hardware.SerialManager;
Jeff Brown25157e42012-04-16 12:13:05 -070052import android.hardware.SystemSensorManager;
Eino-Ville Talvalab2675542012-12-12 13:29:45 -080053import android.hardware.photography.CameraManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070054import android.hardware.display.DisplayManager;
Jeff Brown9df6e7a2012-04-05 11:49:26 -070055import android.hardware.input.InputManager;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080056import android.hardware.usb.IUsbManager;
57import android.hardware.usb.UsbManager;
Bai Taoa58a8752010-07-13 15:32:16 +080058import android.location.CountryDetector;
59import android.location.ICountryDetector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.location.ILocationManager;
61import android.location.LocationManager;
62import android.media.AudioManager;
Dianne Hackbornb58b8f82012-06-11 15:08:39 -070063import android.media.MediaRouter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.net.ConnectivityManager;
65import android.net.IConnectivityManager;
Jeff Sharkey1a303952011-06-16 13:04:20 -070066import android.net.INetworkPolicyManager;
67import android.net.NetworkPolicyManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.net.Uri;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070069import android.net.nsd.INsdManager;
70import android.net.nsd.NsdManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.net.wifi.IWifiManager;
72import android.net.wifi.WifiManager;
repo sync55bc5f32011-06-24 14:23:07 -070073import android.net.wifi.p2p.IWifiP2pManager;
74import android.net.wifi.p2p.WifiP2pManager;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080075import android.nfc.NfcManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.Binder;
77import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070078import android.os.Debug;
Dan Egnorf18a01c2009-11-12 11:32:50 -080079import android.os.DropBoxManager;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080080import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.FileUtils;
82import android.os.Handler;
83import android.os.IBinder;
84import android.os.IPowerManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070085import android.os.IUserManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070086import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.PowerManager;
88import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070089import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070091import android.os.UserHandle;
Jeff Brownc2346132012-04-13 01:55:38 -070092import android.os.SystemVibrator;
Amith Yamasani258848d2012-08-10 17:06:33 -070093import android.os.UserManager;
San Mehatb1043402010-02-05 08:26:50 -080094import android.os.storage.StorageManager;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070095import android.print.IPrintManager;
96import android.print.PrintManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.telephony.TelephonyManager;
Dianne Hackborn9f531192010-08-04 17:48:03 -070098import android.content.ClipboardManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700100import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -0700102import android.util.Slog;
Craig Mautner48d0d182013-06-11 07:53:06 -0700103import android.view.DisplayAdjustments;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.ContextThemeWrapper;
Jeff Brown98365d72012-08-19 20:30:52 -0700105import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.view.WindowManagerImpl;
svetoslavganov75986cf2009-05-14 22:28:01 -0700107import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.inputmethod.InputMethodManager;
satok988323c2011-06-22 16:38:13 +0900109import android.view.textservice.TextServicesManager;
Fred Quintana60307342009-03-24 22:48:12 -0700110import android.accounts.AccountManager;
111import android.accounts.IAccountManager;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800112import android.app.admin.DevicePolicyManager;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800113
114import com.android.internal.app.IAppOpsService;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800115import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.io.File;
118import java.io.FileInputStream;
119import java.io.FileNotFoundException;
120import java.io.FileOutputStream;
121import java.io.IOException;
122import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import java.util.ArrayList;
124import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126class ReceiverRestrictedContext extends ContextWrapper {
127 ReceiverRestrictedContext(Context base) {
128 super(base);
129 }
130
131 @Override
132 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
133 return registerReceiver(receiver, filter, null, null);
134 }
135
136 @Override
137 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
138 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700139 if (receiver == null) {
140 // Allow retrieving current sticky broadcast; this is safe since we
141 // aren't actually registering a receiver.
142 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
143 } else {
144 throw new ReceiverCallNotAllowedException(
145 "BroadcastReceiver components are not allowed to register to receive intents");
146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 }
148
149 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700150 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
151 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700152 if (receiver == null) {
153 // Allow retrieving current sticky broadcast; this is safe since we
154 // aren't actually registering a receiver.
155 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
156 } else {
157 throw new ReceiverCallNotAllowedException(
158 "BroadcastReceiver components are not allowed to register to receive intents");
159 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700160 }
161
162 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
164 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700165 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
167}
168
169/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800170 * Common implementation of Context API, which provides the base
171 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800173class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800174 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700175 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700177 /**
178 * Map from package name, to preference name, to cached preferences.
179 */
180 private static ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>> sSharedPrefs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700182 /*package*/ LoadedApk mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700183 private String mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 private Resources mResources;
185 /*package*/ ActivityThread mMainThread;
186 private Context mOuterContext;
187 private IBinder mActivityToken = null;
188 private ApplicationContentResolver mContentResolver;
189 private int mThemeResource = 0;
190 private Resources.Theme mTheme = null;
191 private PackageManager mPackageManager;
Jeff Browna492c3a2012-08-23 19:48:44 -0700192 private Display mDisplay; // may be null if default display
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 private Context mReceiverRestrictedContext = null;
Romain Guy870e09f2009-07-06 16:35:25 -0700194 private boolean mRestricted;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700195 private UserHandle mUser;
Craig Mautner88c05892013-06-28 09:47:45 -0700196 private ResourcesManager mResourcesManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
198 private final Object mSync = new Object();
199
200 private File mDatabasesDir;
201 private File mPreferencesDir;
202 private File mFilesDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 private File mCacheDir;
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800204 private File mObbDir;
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800205 private File mExternalFilesDir;
206 private File mExternalCacheDir;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 private static final String[] EMPTY_FILE_LIST = {};
209
Craig Mautner48d0d182013-06-11 07:53:06 -0700210 final private DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments();
211
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800212 /**
213 * Override this class when the system service constructor needs a
214 * ContextImpl. Else, use StaticServiceFetcher below.
215 */
216 /*package*/ static class ServiceFetcher {
217 int mContextCacheIndex = -1;
218
219 /**
220 * Main entrypoint; only override if you don't need caching.
221 */
222 public Object getService(ContextImpl ctx) {
223 ArrayList<Object> cache = ctx.mServiceCache;
224 Object service;
225 synchronized (cache) {
226 if (cache.size() == 0) {
227 // Initialize the cache vector on first access.
228 // At this point sNextPerContextServiceCacheIndex
229 // is the number of potential services that are
230 // cached per-Context.
231 for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
232 cache.add(null);
233 }
234 } else {
235 service = cache.get(mContextCacheIndex);
236 if (service != null) {
237 return service;
238 }
239 }
240 service = createService(ctx);
241 cache.set(mContextCacheIndex, service);
242 return service;
243 }
244 }
245
246 /**
247 * Override this to create a new per-Context instance of the
248 * service. getService() will handle locking and caching.
249 */
250 public Object createService(ContextImpl ctx) {
251 throw new RuntimeException("Not implemented");
252 }
253 }
254
255 /**
256 * Override this class for services to be cached process-wide.
257 */
258 abstract static class StaticServiceFetcher extends ServiceFetcher {
259 private Object mCachedInstance;
260
261 @Override
262 public final Object getService(ContextImpl unused) {
263 synchronized (StaticServiceFetcher.this) {
264 Object service = mCachedInstance;
265 if (service != null) {
266 return service;
267 }
268 return mCachedInstance = createStaticService();
269 }
270 }
271
272 public abstract Object createStaticService();
273 }
274
275 private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
276 new HashMap<String, ServiceFetcher>();
277
278 private static int sNextPerContextServiceCacheIndex = 0;
279 private static void registerService(String serviceName, ServiceFetcher fetcher) {
280 if (!(fetcher instanceof StaticServiceFetcher)) {
281 fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
282 }
283 SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
284 }
285
286 // This one's defined separately and given a variable name so it
287 // can be re-used by getWallpaperManager(), avoiding a HashMap
288 // lookup.
289 private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() {
290 public Object createService(ContextImpl ctx) {
291 return new WallpaperManager(ctx.getOuterContext(),
292 ctx.mMainThread.getHandler());
293 }};
294
295 static {
296 registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
297 public Object getService(ContextImpl ctx) {
298 return AccessibilityManager.getInstance(ctx);
299 }});
300
301 registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
302 public Object createService(ContextImpl ctx) {
303 IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
304 IAccountManager service = IAccountManager.Stub.asInterface(b);
305 return new AccountManager(ctx, service);
306 }});
307
308 registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
309 public Object createService(ContextImpl ctx) {
310 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
311 }});
312
Christopher Tatee0a22b32013-07-11 14:43:13 -0700313 registerService(ALARM_SERVICE, new ServiceFetcher() {
314 public Object createService(ContextImpl ctx) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800315 IBinder b = ServiceManager.getService(ALARM_SERVICE);
316 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
Christopher Tatee0a22b32013-07-11 14:43:13 -0700317 return new AlarmManager(service, ctx);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800318 }});
319
320 registerService(AUDIO_SERVICE, new ServiceFetcher() {
321 public Object createService(ContextImpl ctx) {
322 return new AudioManager(ctx);
323 }});
324
Dianne Hackbornb58b8f82012-06-11 15:08:39 -0700325 registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() {
326 public Object createService(ContextImpl ctx) {
327 return new MediaRouter(ctx);
328 }});
329
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800330 registerService(BLUETOOTH_SERVICE, new ServiceFetcher() {
331 public Object createService(ContextImpl ctx) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800332 return new BluetoothManager(ctx);
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800333 }});
334
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800335 registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
336 public Object createService(ContextImpl ctx) {
337 return new ClipboardManager(ctx.getOuterContext(),
338 ctx.mMainThread.getHandler());
339 }});
340
341 registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
342 public Object createStaticService() {
343 IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
344 return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
345 }});
346
347 registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
348 public Object createStaticService() {
349 IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
350 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
351 }});
352
353 registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
354 public Object createService(ContextImpl ctx) {
355 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
356 }});
357
358 registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
359 public Object createService(ContextImpl ctx) {
360 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
361 }});
362
Nick Pellyd2507462010-12-13 12:22:34 -0800363 registerService(NFC_SERVICE, new ServiceFetcher() {
364 public Object createService(ContextImpl ctx) {
365 return new NfcManager(ctx);
366 }});
367
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800368 registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
369 public Object createStaticService() {
370 return createDropBoxManager();
371 }});
372
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700373 registerService(INPUT_SERVICE, new StaticServiceFetcher() {
374 public Object createStaticService() {
375 return InputManager.getInstance();
Jeff Brownac143512012-04-05 18:57:33 -0700376 }});
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700377
Jeff Brownbd6e1502012-08-28 03:27:37 -0700378 registerService(DISPLAY_SERVICE, new ServiceFetcher() {
379 @Override
380 public Object createService(ContextImpl ctx) {
381 return new DisplayManager(ctx.getOuterContext());
382 }});
Jeff Brownfa25bf52012-07-23 19:26:30 -0700383
Jeff Brownf9e989d2013-04-04 23:04:03 -0700384 registerService(INPUT_METHOD_SERVICE, new StaticServiceFetcher() {
385 public Object createStaticService() {
386 return InputMethodManager.getInstance();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800387 }});
388
satok988323c2011-06-22 16:38:13 +0900389 registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
390 public Object createService(ContextImpl ctx) {
391 return TextServicesManager.getInstance();
392 }});
393
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800394 registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
395 public Object getService(ContextImpl ctx) {
396 // TODO: why isn't this caching it? It wasn't
397 // before, so I'm preserving the old behavior and
398 // using getService(), instead of createService()
399 // which would do the caching.
400 return new KeyguardManager();
401 }});
402
403 registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
404 public Object createService(ContextImpl ctx) {
405 return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
406 }});
407
Nick Pellye0fd6932012-07-11 10:26:13 -0700408 registerService(LOCATION_SERVICE, new ServiceFetcher() {
409 public Object createService(ContextImpl ctx) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800410 IBinder b = ServiceManager.getService(LOCATION_SERVICE);
Nick Pellye0fd6932012-07-11 10:26:13 -0700411 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800412 }});
413
Jeff Sharkey1a303952011-06-16 13:04:20 -0700414 registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
415 @Override
416 public Object createService(ContextImpl ctx) {
417 return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
418 ServiceManager.getService(NETWORK_POLICY_SERVICE)));
419 }
420 });
421
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800422 registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
423 public Object createService(ContextImpl ctx) {
424 final Context outerContext = ctx.getOuterContext();
425 return new NotificationManager(
426 new ContextThemeWrapper(outerContext,
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800427 Resources.selectSystemTheme(0,
428 outerContext.getApplicationInfo().targetSdkVersion,
429 com.android.internal.R.style.Theme_Dialog,
Adam Powell6e90a362011-08-14 16:48:32 -0700430 com.android.internal.R.style.Theme_Holo_Dialog,
431 com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800432 ctx.mMainThread.getHandler());
433 }});
434
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700435 registerService(NSD_SERVICE, new ServiceFetcher() {
436 @Override
437 public Object createService(ContextImpl ctx) {
438 IBinder b = ServiceManager.getService(NSD_SERVICE);
439 INsdManager service = INsdManager.Stub.asInterface(b);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700440 return new NsdManager(ctx.getOuterContext(), service);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700441 }});
442
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800443 // Note: this was previously cached in a static variable, but
444 // constructed using mMainThread.getHandler(), so converting
445 // it to be a regular Context-cached service...
446 registerService(POWER_SERVICE, new ServiceFetcher() {
447 public Object createService(ContextImpl ctx) {
448 IBinder b = ServiceManager.getService(POWER_SERVICE);
449 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown96307042012-07-27 15:51:34 -0700450 return new PowerManager(ctx.getOuterContext(),
451 service, ctx.mMainThread.getHandler());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800452 }});
453
454 registerService(SEARCH_SERVICE, new ServiceFetcher() {
455 public Object createService(ContextImpl ctx) {
456 return new SearchManager(ctx.getOuterContext(),
457 ctx.mMainThread.getHandler());
458 }});
459
460 registerService(SENSOR_SERVICE, new ServiceFetcher() {
461 public Object createService(ContextImpl ctx) {
Jaikumar Ganesh6d0c1d782013-03-27 17:41:33 -0700462 return new SystemSensorManager(ctx.getOuterContext(),
463 ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800464 }});
465
466 registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
467 public Object createService(ContextImpl ctx) {
468 return new StatusBarManager(ctx.getOuterContext());
469 }});
470
471 registerService(STORAGE_SERVICE, new ServiceFetcher() {
472 public Object createService(ContextImpl ctx) {
473 try {
Jeff Sharkeybe722152013-02-15 16:56:38 -0800474 return new StorageManager(
475 ctx.getContentResolver(), ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800476 } catch (RemoteException rex) {
477 Log.e(TAG, "Failed to create StorageManager", rex);
478 return null;
479 }
480 }});
481
482 registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
483 public Object createService(ContextImpl ctx) {
484 return new TelephonyManager(ctx.getOuterContext());
485 }});
486
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800487 registerService(UI_MODE_SERVICE, new ServiceFetcher() {
488 public Object createService(ContextImpl ctx) {
489 return new UiModeManager();
490 }});
491
Mike Lockwood3a68b832011-03-08 10:08:59 -0500492 registerService(USB_SERVICE, new ServiceFetcher() {
493 public Object createService(ContextImpl ctx) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500494 IBinder b = ServiceManager.getService(USB_SERVICE);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500495 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500496 }});
497
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400498 registerService(SERIAL_SERVICE, new ServiceFetcher() {
499 public Object createService(ContextImpl ctx) {
500 IBinder b = ServiceManager.getService(SERIAL_SERVICE);
501 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
502 }});
503
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800504 registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
505 public Object createService(ContextImpl ctx) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800506 return new SystemVibrator(ctx);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800507 }});
508
509 registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
510
511 registerService(WIFI_SERVICE, new ServiceFetcher() {
512 public Object createService(ContextImpl ctx) {
513 IBinder b = ServiceManager.getService(WIFI_SERVICE);
514 IWifiManager service = IWifiManager.Stub.asInterface(b);
Irfan Sheriff88759bb2012-07-02 15:58:28 -0700515 return new WifiManager(ctx.getOuterContext(), service);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800516 }});
517
repo sync55bc5f32011-06-24 14:23:07 -0700518 registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
519 public Object createService(ContextImpl ctx) {
520 IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
521 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
522 return new WifiP2pManager(service);
523 }});
524
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800525 registerService(WINDOW_SERVICE, new ServiceFetcher() {
Romain Guye50848b2013-06-07 10:57:25 -0700526 Display mDefaultDisplay;
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800527 public Object getService(ContextImpl ctx) {
Jeff Browna492c3a2012-08-23 19:48:44 -0700528 Display display = ctx.mDisplay;
529 if (display == null) {
Romain Guye50848b2013-06-07 10:57:25 -0700530 if (mDefaultDisplay == null) {
531 DisplayManager dm = (DisplayManager)ctx.getOuterContext().
532 getSystemService(Context.DISPLAY_SERVICE);
533 mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
534 }
535 display = mDefaultDisplay;
Jeff Browna492c3a2012-08-23 19:48:44 -0700536 }
537 return new WindowManagerImpl(display);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800538 }});
Amith Yamasani258848d2012-08-10 17:06:33 -0700539
540 registerService(USER_SERVICE, new ServiceFetcher() {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800541 public Object createService(ContextImpl ctx) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700542 IBinder b = ServiceManager.getService(USER_SERVICE);
543 IUserManager service = IUserManager.Stub.asInterface(b);
544 return new UserManager(ctx, service);
545 }});
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800546
547 registerService(APP_OPS_SERVICE, new ServiceFetcher() {
548 public Object createService(ContextImpl ctx) {
549 IBinder b = ServiceManager.getService(APP_OPS_SERVICE);
550 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
551 return new AppOpsManager(ctx, service);
552 }});
Eino-Ville Talvalab2675542012-12-12 13:29:45 -0800553
Igor Murashkine363fbb2013-06-25 20:26:06 +0000554 registerService(CAMERA_SERVICE, new ServiceFetcher() {
555 public Object createService(ContextImpl ctx) {
556 return new CameraManager(ctx);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700557 }
558 });
559
560 registerService(PRINT_SERVICE, new ServiceFetcher() {
561 public Object createService(ContextImpl ctx) {
562 IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
563 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
564 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
565 UserHandle.getAppId(Process.myUid()));
Eino-Ville Talvalab2675542012-12-12 13:29:45 -0800566 }});
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800567 }
568
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700569 static ContextImpl getImpl(Context context) {
570 Context nextContext;
571 while ((context instanceof ContextWrapper) &&
572 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
573 context = nextContext;
574 }
575 return (ContextImpl)context;
576 }
577
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800578 // The system service cache for the system services that are
579 // cached per-ContextImpl. Package-scoped to avoid accessor
580 // methods.
581 final ArrayList<Object> mServiceCache = new ArrayList<Object>();
582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 @Override
584 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700585 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
587
588 @Override
589 public Resources getResources() {
590 return mResources;
591 }
592
593 @Override
594 public PackageManager getPackageManager() {
595 if (mPackageManager != null) {
596 return mPackageManager;
597 }
598
599 IPackageManager pm = ActivityThread.getPackageManager();
600 if (pm != null) {
601 // Doesn't matter if we make more than one instance.
602 return (mPackageManager = new ApplicationPackageManager(this, pm));
603 }
604
605 return null;
606 }
607
608 @Override
609 public ContentResolver getContentResolver() {
610 return mContentResolver;
611 }
612
613 @Override
614 public Looper getMainLooper() {
615 return mMainThread.getLooper();
616 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 @Override
619 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700620 return (mPackageInfo != null) ?
621 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 @Override
625 public void setTheme(int resid) {
626 mThemeResource = resid;
627 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800630 public int getThemeResId() {
631 return mThemeResource;
632 }
633
634 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 public Resources.Theme getTheme() {
636 if (mTheme == null) {
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800637 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
638 getOuterContext().getApplicationInfo().targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 mTheme = mResources.newTheme();
640 mTheme.applyStyle(mThemeResource, true);
641 }
642 return mTheme;
643 }
644
645 @Override
646 public ClassLoader getClassLoader() {
647 return mPackageInfo != null ?
648 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
649 }
650
651 @Override
652 public String getPackageName() {
653 if (mPackageInfo != null) {
654 return mPackageInfo.getPackageName();
655 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800656 // No mPackageInfo means this is a Context for the system itself,
657 // and this here is its name.
658 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800661 /** @hide */
662 @Override
663 public String getBasePackageName() {
664 return mBasePackageName != null ? mBasePackageName : getPackageName();
665 }
666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700668 public ApplicationInfo getApplicationInfo() {
669 if (mPackageInfo != null) {
670 return mPackageInfo.getApplicationInfo();
671 }
672 throw new RuntimeException("Not supported in system context");
673 }
674
675 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 public String getPackageResourcePath() {
677 if (mPackageInfo != null) {
678 return mPackageInfo.getResDir();
679 }
680 throw new RuntimeException("Not supported in system context");
681 }
682
683 @Override
684 public String getPackageCodePath() {
685 if (mPackageInfo != null) {
686 return mPackageInfo.getAppDir();
687 }
688 throw new RuntimeException("Not supported in system context");
689 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200690
Joe Onorato23ecae32009-06-10 17:07:15 -0700691 public File getSharedPrefsFile(String name) {
692 return makeFilename(getPreferencesDir(), name + ".xml");
693 }
694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 @Override
696 public SharedPreferences getSharedPreferences(String name, int mode) {
697 SharedPreferencesImpl sp;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700698 synchronized (mSync) {
699 if (sSharedPrefs == null) {
700 sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>();
701 }
702
703 final String packageName = getPackageName();
704 ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
705 if (packagePrefs == null) {
706 packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
707 sSharedPrefs.put(packageName, packagePrefs);
708 }
709
Dianne Hackbornff32f352013-07-18 17:31:13 -0700710 // At least one application in the world actually passes in a null
711 // name. This happened to work because when we generated the file name
712 // we would stringify it to "null.xml". Nice.
713 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
714 Build.VERSION_CODES.KEY_LIME_PIE) {
715 if (name == null) {
716 name = "null";
717 }
718 }
719
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700720 sp = packagePrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700721 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800722 File prefsFile = getSharedPrefsFile(name);
723 sp = new SharedPreferencesImpl(prefsFile, mode);
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700724 packagePrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700725 return sp;
726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800728 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
729 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
730 // If somebody else (some other process) changed the prefs
731 // file behind our back, we reload it. This has been the
732 // historical (if undocumented) behavior.
733 sp.startReloadIfChangedUnexpectedly();
734 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700735 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 }
737
738 private File getPreferencesDir() {
739 synchronized (mSync) {
740 if (mPreferencesDir == null) {
741 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
742 }
743 return mPreferencesDir;
744 }
745 }
746
747 @Override
748 public FileInputStream openFileInput(String name)
749 throws FileNotFoundException {
750 File f = makeFilename(getFilesDir(), name);
751 return new FileInputStream(f);
752 }
753
754 @Override
755 public FileOutputStream openFileOutput(String name, int mode)
756 throws FileNotFoundException {
757 final boolean append = (mode&MODE_APPEND) != 0;
758 File f = makeFilename(getFilesDir(), name);
759 try {
760 FileOutputStream fos = new FileOutputStream(f, append);
761 setFilePermissionsFromMode(f.getPath(), mode, 0);
762 return fos;
763 } catch (FileNotFoundException e) {
764 }
765
766 File parent = f.getParentFile();
767 parent.mkdir();
768 FileUtils.setPermissions(
769 parent.getPath(),
770 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
771 -1, -1);
772 FileOutputStream fos = new FileOutputStream(f, append);
773 setFilePermissionsFromMode(f.getPath(), mode, 0);
774 return fos;
775 }
776
777 @Override
778 public boolean deleteFile(String name) {
779 File f = makeFilename(getFilesDir(), name);
780 return f.delete();
781 }
782
783 @Override
784 public File getFilesDir() {
785 synchronized (mSync) {
786 if (mFilesDir == null) {
787 mFilesDir = new File(getDataDirFile(), "files");
788 }
789 if (!mFilesDir.exists()) {
790 if(!mFilesDir.mkdirs()) {
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200791 Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 return null;
793 }
794 FileUtils.setPermissions(
795 mFilesDir.getPath(),
796 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
797 -1, -1);
798 }
799 return mFilesDir;
800 }
801 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800804 public File getExternalFilesDir(String type) {
805 synchronized (mSync) {
806 if (mExternalFilesDir == null) {
807 mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory(
808 getPackageName());
809 }
810 if (!mExternalFilesDir.exists()) {
811 try {
812 (new File(Environment.getExternalStorageAndroidDataDir(),
813 ".nomedia")).createNewFile();
814 } catch (IOException e) {
815 }
816 if (!mExternalFilesDir.mkdirs()) {
817 Log.w(TAG, "Unable to create external files directory");
818 return null;
819 }
820 }
821 if (type == null) {
822 return mExternalFilesDir;
823 }
824 File dir = new File(mExternalFilesDir, type);
825 if (!dir.exists()) {
826 if (!dir.mkdirs()) {
827 Log.w(TAG, "Unable to create external media directory " + dir);
828 return null;
829 }
830 }
831 return dir;
832 }
833 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200834
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800835 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800836 public File getObbDir() {
837 synchronized (mSync) {
838 if (mObbDir == null) {
839 mObbDir = Environment.getExternalStorageAppObbDirectory(
840 getPackageName());
841 }
842 return mObbDir;
843 }
844 }
845
846 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 public File getCacheDir() {
848 synchronized (mSync) {
849 if (mCacheDir == null) {
850 mCacheDir = new File(getDataDirFile(), "cache");
851 }
852 if (!mCacheDir.exists()) {
853 if(!mCacheDir.mkdirs()) {
Amith Yamasani92d57052012-08-23 10:07:52 -0700854 Log.w(TAG, "Unable to create cache directory " + mCacheDir.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 return null;
856 }
857 FileUtils.setPermissions(
858 mCacheDir.getPath(),
859 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
860 -1, -1);
861 }
862 }
863 return mCacheDir;
864 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200865
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800866 @Override
867 public File getExternalCacheDir() {
868 synchronized (mSync) {
869 if (mExternalCacheDir == null) {
870 mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory(
871 getPackageName());
872 }
873 if (!mExternalCacheDir.exists()) {
874 try {
875 (new File(Environment.getExternalStorageAndroidDataDir(),
876 ".nomedia")).createNewFile();
877 } catch (IOException e) {
878 }
879 if (!mExternalCacheDir.mkdirs()) {
880 Log.w(TAG, "Unable to create external cache directory");
881 return null;
882 }
883 }
884 return mExternalCacheDir;
885 }
886 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 @Override
889 public File getFileStreamPath(String name) {
890 return makeFilename(getFilesDir(), name);
891 }
892
893 @Override
894 public String[] fileList() {
895 final String[] list = getFilesDir().list();
896 return (list != null) ? list : EMPTY_FILE_LIST;
897 }
898
899 @Override
900 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700901 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 }
903
904 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700905 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
906 DatabaseErrorHandler errorHandler) {
907 File f = validateFilePath(name, true);
Jeff Brown47847f32012-03-22 19:13:11 -0700908 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
909 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
910 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
911 }
912 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700913 setFilePermissionsFromMode(f.getPath(), mode, 0);
914 return db;
915 }
916
917 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 public boolean deleteDatabase(String name) {
919 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800920 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800921 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 } catch (Exception e) {
923 }
924 return false;
925 }
926
927 @Override
928 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800929 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 }
931
932 @Override
933 public String[] databaseList() {
934 final String[] list = getDatabasesDir().list();
935 return (list != null) ? list : EMPTY_FILE_LIST;
936 }
937
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 private File getDatabasesDir() {
940 synchronized (mSync) {
941 if (mDatabasesDir == null) {
942 mDatabasesDir = new File(getDataDirFile(), "databases");
943 }
944 if (mDatabasesDir.getPath().equals("databases")) {
945 mDatabasesDir = new File("/data/system");
946 }
947 return mDatabasesDir;
948 }
949 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 @Override
952 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700953 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
955
956 @Override
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700957 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700958 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960
961 @Override
962 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700963 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
966 @Override
967 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700968 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 }
970
971 @Override
972 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700973 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
975
976 @Override
977 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700978 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980
981 @Override
982 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700983 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 }
985
986 @Override
987 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700988 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700989 startActivity(intent, null);
990 }
991
Amith Yamasani82644082012-08-03 13:09:11 -0700992 /** @hide */
993 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700994 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700995 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700996 }
997
Dianne Hackborna4972e92012-03-14 10:38:05 -0700998 @Override
999 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001000 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1002 throw new AndroidRuntimeException(
1003 "Calling startActivity() from outside of an Activity "
1004 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
1005 + " Is this really what you want?");
1006 }
1007 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -07001008 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001009 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 }
1011
Amith Yamasani258848d2012-08-10 17:06:33 -07001012 /** @hide */
1013 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001014 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001015 try {
1016 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001017 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -07001018 intent.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001019 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options,
1020 user.getIdentifier());
Amith Yamasani258848d2012-08-10 17:06:33 -07001021 } catch (RemoteException re) {
1022 }
1023 }
1024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001026 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001027 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -07001028 startActivities(intents, null);
1029 }
1030
Amith Yamasaniea7e9152012-09-24 16:11:18 -07001031 /** @hide */
1032 @Override
1033 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
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().execStartActivitiesAsUser(
1041 getOuterContext(), mMainThread.getApplicationThread(), null,
1042 (Activity)null, intents, options, userHandle.getIdentifier());
1043 }
1044
Dianne Hackborna4972e92012-03-14 10:38:05 -07001045 @Override
1046 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001047 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001048 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1049 throw new AndroidRuntimeException(
1050 "Calling startActivities() from outside of an Activity "
1051 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
1052 + " Is this really what you want?");
1053 }
1054 mMainThread.getInstrumentation().execStartActivities(
1055 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001056 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001057 }
1058
1059 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001060 public void startIntentSender(IntentSender intent,
1061 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
1062 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001063 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
1064 }
1065
1066 @Override
1067 public void startIntentSender(IntentSender intent, Intent fillInIntent,
1068 int flagsMask, int flagsValues, int extraFlags, Bundle options)
1069 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001070 try {
1071 String resolvedType = null;
1072 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001073 fillInIntent.migrateExtraStreamToClipData();
1074 fillInIntent.prepareToLeaveProcess();
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001075 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
1076 }
1077 int result = ActivityManagerNative.getDefault()
1078 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
1079 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001080 0, flagsMask, flagsValues, options);
1081 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001082 throw new IntentSender.SendIntentException();
1083 }
1084 Instrumentation.checkStartActivityResult(result, null);
1085 } catch (RemoteException e) {
1086 }
1087 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001088
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001089 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001091 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1093 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001094 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 ActivityManagerNative.getDefault().broadcastIntent(
1096 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001097 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001098 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 } catch (RemoteException e) {
1100 }
1101 }
1102
Amith Yamasani67cf7d32012-02-16 14:31:23 -08001103 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001105 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1107 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001108 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 ActivityManagerNative.getDefault().broadcastIntent(
1110 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001111 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE,
1112 false, false, getUserId());
1113 } catch (RemoteException e) {
1114 }
1115 }
1116
1117 @Override
1118 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
1119 warnIfCallingFromSystemProcess();
1120 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1121 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001122 intent.prepareToLeaveProcess();
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001123 ActivityManagerNative.getDefault().broadcastIntent(
1124 mMainThread.getApplicationThread(), intent, resolvedType, null,
1125 Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001126 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 } catch (RemoteException e) {
1128 }
1129 }
1130
1131 @Override
1132 public void sendOrderedBroadcast(Intent intent,
1133 String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001134 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1136 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001137 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 ActivityManagerNative.getDefault().broadcastIntent(
1139 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001140 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, true, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001141 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 } catch (RemoteException e) {
1143 }
1144 }
1145
1146 @Override
1147 public void sendOrderedBroadcast(Intent intent,
1148 String receiverPermission, BroadcastReceiver resultReceiver,
1149 Handler scheduler, int initialCode, String initialData,
1150 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001151 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1152 resultReceiver, scheduler, initialCode, initialData, initialExtras);
1153 }
1154
1155 @Override
1156 public void sendOrderedBroadcast(Intent intent,
1157 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1158 Handler scheduler, int initialCode, String initialData,
1159 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001160 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 IIntentReceiver rd = null;
1162 if (resultReceiver != null) {
1163 if (mPackageInfo != null) {
1164 if (scheduler == null) {
1165 scheduler = mMainThread.getHandler();
1166 }
1167 rd = mPackageInfo.getReceiverDispatcher(
1168 resultReceiver, getOuterContext(), scheduler,
1169 mMainThread.getInstrumentation(), false);
1170 } else {
1171 if (scheduler == null) {
1172 scheduler = mMainThread.getHandler();
1173 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001174 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1176 }
1177 }
1178 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1179 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001180 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 ActivityManagerNative.getDefault().broadcastIntent(
1182 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001183 initialCode, initialData, initialExtras, receiverPermission, appOp,
1184 true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 } catch (RemoteException e) {
1186 }
1187 }
1188
1189 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001190 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001191 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1192 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001193 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001194 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001195 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
1196 AppOpsManager.OP_NONE, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001197 } catch (RemoteException e) {
1198 }
1199 }
1200
1201 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001202 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1203 String receiverPermission) {
1204 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1205 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001206 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001207 ActivityManagerNative.getDefault().broadcastIntent(
1208 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001209 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, false, false,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001210 user.getIdentifier());
1211 } catch (RemoteException e) {
1212 }
1213 }
1214
1215 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001216 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001217 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001218 int initialCode, String initialData, Bundle initialExtras) {
1219 IIntentReceiver rd = null;
1220 if (resultReceiver != null) {
1221 if (mPackageInfo != null) {
1222 if (scheduler == null) {
1223 scheduler = mMainThread.getHandler();
1224 }
1225 rd = mPackageInfo.getReceiverDispatcher(
1226 resultReceiver, getOuterContext(), scheduler,
1227 mMainThread.getInstrumentation(), false);
1228 } else {
1229 if (scheduler == null) {
1230 scheduler = mMainThread.getHandler();
1231 }
1232 rd = new LoadedApk.ReceiverDispatcher(
1233 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1234 }
1235 }
1236 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1237 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001238 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001239 ActivityManagerNative.getDefault().broadcastIntent(
1240 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001241 initialCode, initialData, initialExtras, receiverPermission,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001242 AppOpsManager.OP_NONE, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001243 } catch (RemoteException e) {
1244 }
1245 }
1246
1247 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001249 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1251 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001252 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 ActivityManagerNative.getDefault().broadcastIntent(
1254 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001255 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001256 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 } catch (RemoteException e) {
1258 }
1259 }
1260
1261 @Override
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001262 public void sendStickyOrderedBroadcast(Intent intent,
1263 BroadcastReceiver resultReceiver,
1264 Handler scheduler, int initialCode, String initialData,
1265 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001266 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001267 IIntentReceiver rd = null;
1268 if (resultReceiver != null) {
1269 if (mPackageInfo != null) {
1270 if (scheduler == null) {
1271 scheduler = mMainThread.getHandler();
1272 }
1273 rd = mPackageInfo.getReceiverDispatcher(
1274 resultReceiver, getOuterContext(), scheduler,
1275 mMainThread.getInstrumentation(), false);
1276 } else {
1277 if (scheduler == null) {
1278 scheduler = mMainThread.getHandler();
1279 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001280 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001281 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1282 }
1283 }
1284 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1285 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001286 intent.prepareToLeaveProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001287 ActivityManagerNative.getDefault().broadcastIntent(
1288 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1289 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001290 AppOpsManager.OP_NONE, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001291 } catch (RemoteException e) {
1292 }
1293 }
1294
1295 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 public void removeStickyBroadcast(Intent intent) {
1297 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1298 if (resolvedType != null) {
1299 intent = new Intent(intent);
1300 intent.setDataAndType(intent.getData(), resolvedType);
1301 }
1302 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001303 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001305 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 } catch (RemoteException e) {
1307 }
1308 }
1309
1310 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001311 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1312 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1313 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001314 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001315 ActivityManagerNative.getDefault().broadcastIntent(
1316 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001317 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001318 } catch (RemoteException e) {
1319 }
1320 }
1321
1322 @Override
1323 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1324 UserHandle user, BroadcastReceiver resultReceiver,
1325 Handler scheduler, int initialCode, String initialData,
1326 Bundle initialExtras) {
1327 IIntentReceiver rd = null;
1328 if (resultReceiver != null) {
1329 if (mPackageInfo != null) {
1330 if (scheduler == null) {
1331 scheduler = mMainThread.getHandler();
1332 }
1333 rd = mPackageInfo.getReceiverDispatcher(
1334 resultReceiver, getOuterContext(), scheduler,
1335 mMainThread.getInstrumentation(), false);
1336 } else {
1337 if (scheduler == null) {
1338 scheduler = mMainThread.getHandler();
1339 }
1340 rd = new LoadedApk.ReceiverDispatcher(
1341 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1342 }
1343 }
1344 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1345 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001346 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001347 ActivityManagerNative.getDefault().broadcastIntent(
1348 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1349 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001350 AppOpsManager.OP_NONE, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001351 } catch (RemoteException e) {
1352 }
1353 }
1354
1355 @Override
1356 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1357 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1358 if (resolvedType != null) {
1359 intent = new Intent(intent);
1360 intent.setDataAndType(intent.getData(), resolvedType);
1361 }
1362 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001363 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001364 ActivityManagerNative.getDefault().unbroadcastIntent(
1365 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1366 } catch (RemoteException e) {
1367 }
1368 }
1369
1370 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1372 return registerReceiver(receiver, filter, null, null);
1373 }
1374
1375 @Override
1376 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1377 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001378 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001379 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381
Dianne Hackborn20e80982012-08-31 19:00:44 -07001382 @Override
1383 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1384 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1385 return registerReceiverInternal(receiver, user.getIdentifier(),
1386 filter, broadcastPermission, scheduler, getOuterContext());
1387 }
1388
1389 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 IntentFilter filter, String broadcastPermission,
1391 Handler scheduler, Context context) {
1392 IIntentReceiver rd = null;
1393 if (receiver != null) {
1394 if (mPackageInfo != null && context != null) {
1395 if (scheduler == null) {
1396 scheduler = mMainThread.getHandler();
1397 }
1398 rd = mPackageInfo.getReceiverDispatcher(
1399 receiver, context, scheduler,
1400 mMainThread.getInstrumentation(), true);
1401 } else {
1402 if (scheduler == null) {
1403 scheduler = mMainThread.getHandler();
1404 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001405 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001406 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
1408 }
1409 try {
1410 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001411 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001412 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 } catch (RemoteException e) {
1414 return null;
1415 }
1416 }
1417
1418 @Override
1419 public void unregisterReceiver(BroadcastReceiver receiver) {
1420 if (mPackageInfo != null) {
1421 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1422 getOuterContext(), receiver);
1423 try {
1424 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1425 } catch (RemoteException e) {
1426 }
1427 } else {
1428 throw new RuntimeException("Not supported in system context");
1429 }
1430 }
1431
1432 @Override
1433 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001434 warnIfCallingFromSystemProcess();
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001435 return startServiceAsUser(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001436 }
1437
1438 @Override
1439 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001440 warnIfCallingFromSystemProcess();
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001441 return stopServiceAsUser(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001442 }
1443
1444 @Override
1445 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001447 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 ComponentName cn = ActivityManagerNative.getDefault().startService(
1449 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001450 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001451 if (cn != null) {
1452 if (cn.getPackageName().equals("!")) {
1453 throw new SecurityException(
1454 "Not allowed to start service " + service
1455 + " without permission " + cn.getClassName());
1456 } else if (cn.getPackageName().equals("!!")) {
1457 throw new SecurityException(
1458 "Unable to start service " + service
1459 + ": " + cn.getClassName());
1460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
1462 return cn;
1463 } catch (RemoteException e) {
1464 return null;
1465 }
1466 }
1467
1468 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001469 public boolean stopServiceAsUser(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001471 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 int res = ActivityManagerNative.getDefault().stopService(
1473 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001474 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 if (res < 0) {
1476 throw new SecurityException(
1477 "Not allowed to stop service " + service);
1478 }
1479 return res != 0;
1480 } catch (RemoteException e) {
1481 return false;
1482 }
1483 }
1484
1485 @Override
1486 public boolean bindService(Intent service, ServiceConnection conn,
1487 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001488 warnIfCallingFromSystemProcess();
Amith Yamasani27b89e62013-01-16 12:30:11 -08001489 return bindServiceAsUser(service, conn, flags, Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001490 }
1491
1492 /** @hide */
1493 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001494 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1495 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001497 if (conn == null) {
1498 throw new IllegalArgumentException("connection is null");
1499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 if (mPackageInfo != null) {
1501 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1502 mMainThread.getHandler(), flags);
1503 } else {
1504 throw new RuntimeException("Not supported in system context");
1505 }
1506 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001507 IBinder token = getActivityToken();
1508 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1509 && mPackageInfo.getApplicationInfo().targetSdkVersion
1510 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1511 flags |= BIND_WAIVE_PRIORITY;
1512 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001513 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 int res = ActivityManagerNative.getDefault().bindService(
1515 mMainThread.getApplicationThread(), getActivityToken(),
1516 service, service.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasani27b89e62013-01-16 12:30:11 -08001517 sd, flags, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 if (res < 0) {
1519 throw new SecurityException(
1520 "Not allowed to bind to service " + service);
1521 }
1522 return res != 0;
1523 } catch (RemoteException e) {
1524 return false;
1525 }
1526 }
1527
1528 @Override
1529 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001530 if (conn == null) {
1531 throw new IllegalArgumentException("connection is null");
1532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 if (mPackageInfo != null) {
1534 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1535 getOuterContext(), conn);
1536 try {
1537 ActivityManagerNative.getDefault().unbindService(sd);
1538 } catch (RemoteException e) {
1539 }
1540 } else {
1541 throw new RuntimeException("Not supported in system context");
1542 }
1543 }
1544
1545 @Override
1546 public boolean startInstrumentation(ComponentName className,
1547 String profileFile, Bundle arguments) {
1548 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001549 if (arguments != null) {
1550 arguments.setAllowFds(false);
1551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 return ActivityManagerNative.getDefault().startInstrumentation(
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001553 className, profileFile, 0, arguments, null, null, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 } catch (RemoteException e) {
1555 // System has crashed, nothing we can do.
1556 }
1557 return false;
1558 }
1559
1560 @Override
1561 public Object getSystemService(String name) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001562 ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
1563 return fetcher == null ? null : fetcher.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001566 private WallpaperManager getWallpaperManager() {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001567 return (WallpaperManager) WALLPAPER_FETCHER.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 }
1569
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001570 /* package */ static DropBoxManager createDropBoxManager() {
1571 IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
1572 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001573 if (service == null) {
1574 // Don't return a DropBoxManager that will NPE upon use.
1575 // This also avoids caching a broken DropBoxManager in
1576 // getDropBoxManager during early boot, before the
1577 // DROPBOX_SERVICE is registered.
1578 return null;
1579 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001580 return new DropBoxManager(service);
1581 }
1582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 @Override
1584 public int checkPermission(String permission, int pid, int uid) {
1585 if (permission == null) {
1586 throw new IllegalArgumentException("permission is null");
1587 }
1588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 try {
1590 return ActivityManagerNative.getDefault().checkPermission(
1591 permission, pid, uid);
1592 } catch (RemoteException e) {
1593 return PackageManager.PERMISSION_DENIED;
1594 }
1595 }
1596
1597 @Override
1598 public int checkCallingPermission(String permission) {
1599 if (permission == null) {
1600 throw new IllegalArgumentException("permission is null");
1601 }
1602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 int pid = Binder.getCallingPid();
1604 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001605 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
1607 return PackageManager.PERMISSION_DENIED;
1608 }
1609
1610 @Override
1611 public int checkCallingOrSelfPermission(String permission) {
1612 if (permission == null) {
1613 throw new IllegalArgumentException("permission is null");
1614 }
1615
1616 return checkPermission(permission, Binder.getCallingPid(),
1617 Binder.getCallingUid());
1618 }
1619
1620 private void enforce(
1621 String permission, int resultOfCheck,
1622 boolean selfToo, int uid, String message) {
1623 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1624 throw new SecurityException(
1625 (message != null ? (message + ": ") : "") +
1626 (selfToo
1627 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001628 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 permission +
1630 ".");
1631 }
1632 }
1633
1634 public void enforcePermission(
1635 String permission, int pid, int uid, String message) {
1636 enforce(permission,
1637 checkPermission(permission, pid, uid),
1638 false,
1639 uid,
1640 message);
1641 }
1642
1643 public void enforceCallingPermission(String permission, String message) {
1644 enforce(permission,
1645 checkCallingPermission(permission),
1646 false,
1647 Binder.getCallingUid(),
1648 message);
1649 }
1650
1651 public void enforceCallingOrSelfPermission(
1652 String permission, String message) {
1653 enforce(permission,
1654 checkCallingOrSelfPermission(permission),
1655 true,
1656 Binder.getCallingUid(),
1657 message);
1658 }
1659
1660 @Override
1661 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1662 try {
1663 ActivityManagerNative.getDefault().grantUriPermission(
1664 mMainThread.getApplicationThread(), toPackage, uri,
1665 modeFlags);
1666 } catch (RemoteException e) {
1667 }
1668 }
1669
1670 @Override
1671 public void revokeUriPermission(Uri uri, int modeFlags) {
1672 try {
1673 ActivityManagerNative.getDefault().revokeUriPermission(
1674 mMainThread.getApplicationThread(), uri,
1675 modeFlags);
1676 } catch (RemoteException e) {
1677 }
1678 }
1679
1680 @Override
1681 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 try {
1683 return ActivityManagerNative.getDefault().checkUriPermission(
1684 uri, pid, uid, modeFlags);
1685 } catch (RemoteException e) {
1686 return PackageManager.PERMISSION_DENIED;
1687 }
1688 }
1689
1690 @Override
1691 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 int pid = Binder.getCallingPid();
1693 if (pid != Process.myPid()) {
1694 return checkUriPermission(uri, pid,
1695 Binder.getCallingUid(), modeFlags);
1696 }
1697 return PackageManager.PERMISSION_DENIED;
1698 }
1699
1700 @Override
1701 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1702 return checkUriPermission(uri, Binder.getCallingPid(),
1703 Binder.getCallingUid(), modeFlags);
1704 }
1705
1706 @Override
1707 public int checkUriPermission(Uri uri, String readPermission,
1708 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001709 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1711 + readPermission + " writePermission=" + writePermission
1712 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1713 }
1714 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1715 if (readPermission == null
1716 || checkPermission(readPermission, pid, uid)
1717 == PackageManager.PERMISSION_GRANTED) {
1718 return PackageManager.PERMISSION_GRANTED;
1719 }
1720 }
1721 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1722 if (writePermission == null
1723 || checkPermission(writePermission, pid, uid)
1724 == PackageManager.PERMISSION_GRANTED) {
1725 return PackageManager.PERMISSION_GRANTED;
1726 }
1727 }
1728 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1729 : PackageManager.PERMISSION_DENIED;
1730 }
1731
1732 private String uriModeFlagToString(int uriModeFlags) {
1733 switch (uriModeFlags) {
1734 case Intent.FLAG_GRANT_READ_URI_PERMISSION |
1735 Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1736 return "read and write";
1737 case Intent.FLAG_GRANT_READ_URI_PERMISSION:
1738 return "read";
1739 case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1740 return "write";
1741 }
1742 throw new IllegalArgumentException(
1743 "Unknown permission mode flags: " + uriModeFlags);
1744 }
1745
1746 private void enforceForUri(
1747 int modeFlags, int resultOfCheck, boolean selfToo,
1748 int uid, Uri uri, String message) {
1749 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1750 throw new SecurityException(
1751 (message != null ? (message + ": ") : "") +
1752 (selfToo
1753 ? "Neither user " + uid + " nor current process has "
1754 : "User " + uid + " does not have ") +
1755 uriModeFlagToString(modeFlags) +
1756 " permission on " +
1757 uri +
1758 ".");
1759 }
1760 }
1761
1762 public void enforceUriPermission(
1763 Uri uri, int pid, int uid, int modeFlags, String message) {
1764 enforceForUri(
1765 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1766 false, uid, uri, message);
1767 }
1768
1769 public void enforceCallingUriPermission(
1770 Uri uri, int modeFlags, String message) {
1771 enforceForUri(
1772 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001773 false,
1774 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 }
1776
1777 public void enforceCallingOrSelfUriPermission(
1778 Uri uri, int modeFlags, String message) {
1779 enforceForUri(
1780 modeFlags,
1781 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1782 Binder.getCallingUid(), uri, message);
1783 }
1784
1785 public void enforceUriPermission(
1786 Uri uri, String readPermission, String writePermission,
1787 int pid, int uid, int modeFlags, String message) {
1788 enforceForUri(modeFlags,
1789 checkUriPermission(
1790 uri, readPermission, writePermission, pid, uid,
1791 modeFlags),
1792 false,
1793 uid,
1794 uri,
1795 message);
1796 }
1797
Amith Yamasanicd757062012-10-19 18:23:52 -07001798 private void warnIfCallingFromSystemProcess() {
1799 if (Process.myUid() == Process.SYSTEM_UID) {
1800 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001801 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001802 }
1803 }
1804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 @Override
1806 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001807 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001808 return createPackageContextAsUser(packageName, flags,
1809 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001810 }
1811
1812 @Override
1813 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1814 throws NameNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001816 final ContextImpl context = new ContextImpl(mMainThread.getSystemContext());
Adam Powell6f2a3d22012-10-05 22:32:04 -07001817 context.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
1818 context.init(mPackageInfo, null, mMainThread, mResources, mBasePackageName, user);
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001819 return context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 }
1821
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001822 LoadedApk pi =
Amith Yamasani98edc952012-09-25 14:09:27 -07001823 mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags,
1824 user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 if (pi != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001826 ContextImpl c = new ContextImpl();
Romain Guy870e09f2009-07-06 16:35:25 -07001827 c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001828 c.init(pi, null, mMainThread, mResources, mBasePackageName, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 if (c.mResources != null) {
1830 return c;
1831 }
1832 }
1833
1834 // Should be a better exception.
1835 throw new PackageManager.NameNotFoundException(
1836 "Application package " + packageName + " not found");
1837 }
1838
Romain Guy870e09f2009-07-06 16:35:25 -07001839 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001840 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001841 if (overrideConfiguration == null) {
1842 throw new IllegalArgumentException("overrideConfiguration must not be null");
1843 }
1844
Dianne Hackborn756220b2012-08-14 16:45:30 -07001845 ContextImpl c = new ContextImpl();
1846 c.init(mPackageInfo, null, mMainThread);
Craig Mautner88c05892013-06-28 09:47:45 -07001847 c.mResources = mResourcesManager.getTopLevelResources(mPackageInfo.getResDir(),
1848 getDisplayId(), overrideConfiguration, mResources.getCompatibilityInfo(),
1849 mActivityToken);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001850 return c;
1851 }
1852
1853 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001854 public Context createDisplayContext(Display display) {
1855 if (display == null) {
1856 throw new IllegalArgumentException("display must not be null");
1857 }
1858
1859 int displayId = display.getDisplayId();
Jeff Browna492c3a2012-08-23 19:48:44 -07001860
1861 ContextImpl context = new ContextImpl();
1862 context.init(mPackageInfo, null, mMainThread);
1863 context.mDisplay = display;
Craig Mautner48d0d182013-06-11 07:53:06 -07001864 DisplayAdjustments daj = getDisplayAdjustments(displayId);
Craig Mautner88c05892013-06-28 09:47:45 -07001865 context.mResources = mResourcesManager.getTopLevelResources(mPackageInfo.getResDir(),
1866 displayId, null, daj.getCompatibilityInfo(), null);
Jeff Browna492c3a2012-08-23 19:48:44 -07001867 return context;
1868 }
1869
1870 private int getDisplayId() {
1871 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1872 }
1873
1874 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001875 public boolean isRestricted() {
1876 return mRestricted;
1877 }
1878
Jeff Brown98365d72012-08-19 20:30:52 -07001879 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001880 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1881 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -07001882 }
1883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 private File getDataDirFile() {
1885 if (mPackageInfo != null) {
1886 return mPackageInfo.getDataDirFile();
1887 }
1888 throw new RuntimeException("Not supported in system context");
1889 }
1890
1891 @Override
1892 public File getDir(String name, int mode) {
1893 name = "app_" + name;
1894 File file = makeFilename(getDataDirFile(), name);
1895 if (!file.exists()) {
1896 file.mkdir();
1897 setFilePermissionsFromMode(file.getPath(), mode,
1898 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1899 }
1900 return file;
1901 }
1902
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001903 /** {@hide} */
1904 public int getUserId() {
1905 return mUser.getIdentifier();
1906 }
1907
Dianne Hackborn21556372010-02-04 16:34:40 -08001908 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001909 final ContextImpl context = new ContextImpl();
1910 context.init(Resources.getSystem(), mainThread, Process.myUserHandle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 return context;
1912 }
1913
Dianne Hackborn21556372010-02-04 16:34:40 -08001914 ContextImpl() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 mOuterContext = this;
1916 }
1917
1918 /**
1919 * Create a new ApplicationContext from an existing one. The new one
1920 * works and operates the same as the one it is copying.
1921 *
1922 * @param context Existing application context.
1923 */
Dianne Hackborn21556372010-02-04 16:34:40 -08001924 public ContextImpl(ContextImpl context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 mPackageInfo = context.mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001926 mBasePackageName = context.mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 mResources = context.mResources;
1928 mMainThread = context.mMainThread;
1929 mContentResolver = context.mContentResolver;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001930 mUser = context.mUser;
Jeff Browna492c3a2012-08-23 19:48:44 -07001931 mDisplay = context.mDisplay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 mOuterContext = this;
Craig Mautner48d0d182013-06-11 07:53:06 -07001933 mDisplayAdjustments.setCompatibilityInfo(mPackageInfo.getCompatibilityInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 }
1935
Jeff Sharkey6d515712012-09-20 16:06:08 -07001936 final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread) {
1937 init(packageInfo, activityToken, mainThread, null, null, Process.myUserHandle());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001938 }
1939
Jeff Sharkey6d515712012-09-20 16:06:08 -07001940 final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread,
1941 Resources container, String basePackageName, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 mPackageInfo = packageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001943 mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 mResources = mPackageInfo.getResources(mainThread);
Craig Mautner88c05892013-06-28 09:47:45 -07001945 mResourcesManager = ResourcesManager.getInstance();
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001946
Craig Mautner48d0d182013-06-11 07:53:06 -07001947 CompatibilityInfo compatInfo =
1948 container == null ? null : container.getCompatibilityInfo();
1949 if (mResources != null &&
1950 ((compatInfo != null && compatInfo.applicationScale !=
1951 mResources.getCompatibilityInfo().applicationScale)
1952 || activityToken != null)) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001953 if (DEBUG) {
1954 Log.d(TAG, "loaded context has different scaling. Using container's" +
1955 " compatiblity info:" + container.getDisplayMetrics());
1956 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001957 if (compatInfo == null) {
1958 compatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
1959 }
1960 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
1961 mDisplayAdjustments.setActivityToken(activityToken);
Craig Mautner88c05892013-06-28 09:47:45 -07001962 mResources = mResourcesManager.getTopLevelResources(mPackageInfo.getResDir(),
Craig Mautner48d0d182013-06-11 07:53:06 -07001963 Display.DEFAULT_DISPLAY, null, compatInfo, activityToken);
1964 } else {
1965 mDisplayAdjustments.setCompatibilityInfo(packageInfo.getCompatibilityInfo());
1966 mDisplayAdjustments.setActivityToken(activityToken);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 mMainThread = mainThread;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001969 mActivityToken = activityToken;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001970 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
1971 mUser = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
1973
Jeff Sharkey6d515712012-09-20 16:06:08 -07001974 final void init(Resources resources, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 mPackageInfo = null;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001976 mBasePackageName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 mResources = resources;
1978 mMainThread = mainThread;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001979 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
1980 mUser = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 }
1982
1983 final void scheduleFinalCleanup(String who, String what) {
1984 mMainThread.scheduleContextCleanup(this, who, what);
1985 }
1986
1987 final void performFinalCleanup(String who, String what) {
1988 //Log.i(TAG, "Cleanup up context: " + this);
1989 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1990 }
1991
1992 final Context getReceiverRestrictedContext() {
1993 if (mReceiverRestrictedContext != null) {
1994 return mReceiverRestrictedContext;
1995 }
1996 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1997 }
1998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 final void setOuterContext(Context context) {
2000 mOuterContext = context;
2001 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 final Context getOuterContext() {
2004 return mOuterContext;
2005 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 final IBinder getActivityToken() {
2008 return mActivityToken;
2009 }
2010
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002011 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 int extraPermissions) {
2013 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2014 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2015 |extraPermissions;
2016 if ((mode&MODE_WORLD_READABLE) != 0) {
2017 perms |= FileUtils.S_IROTH;
2018 }
2019 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2020 perms |= FileUtils.S_IWOTH;
2021 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002022 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2024 + ", perms=0x" + Integer.toHexString(perms));
2025 }
2026 FileUtils.setPermissions(name, perms, -1, -1);
2027 }
2028
Oscar Montemayora8529f62009-11-18 10:14:20 -08002029 private File validateFilePath(String name, boolean createDirectory) {
2030 File dir;
2031 File f;
2032
2033 if (name.charAt(0) == File.separatorChar) {
2034 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
2035 dir = new File(dirPath);
2036 name = name.substring(name.lastIndexOf(File.separatorChar));
2037 f = new File(dir, name);
2038 } else {
2039 dir = getDatabasesDir();
2040 f = makeFilename(dir, name);
2041 }
2042
2043 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
2044 FileUtils.setPermissions(dir.getPath(),
2045 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
2046 -1, -1);
2047 }
2048
2049 return f;
2050 }
2051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 private File makeFilename(File base, String name) {
2053 if (name.indexOf(File.separatorChar) < 0) {
2054 return new File(base, name);
2055 }
2056 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002057 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 }
2059
2060 // ----------------------------------------------------------------------
2061 // ----------------------------------------------------------------------
2062 // ----------------------------------------------------------------------
2063
2064 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002065 private final ActivityThread mMainThread;
2066 private final UserHandle mUser;
2067
2068 public ApplicationContentResolver(
2069 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002071 mMainThread = Preconditions.checkNotNull(mainThread);
2072 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 }
2074
2075 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002076 protected IContentProvider acquireProvider(Context context, String auth) {
2077 return mMainThread.acquireProvider(context, auth, mUser.getIdentifier(), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 }
2079
2080 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002081 protected IContentProvider acquireExistingProvider(Context context, String auth) {
2082 return mMainThread.acquireExistingProvider(context, auth, mUser.getIdentifier(), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002083 }
2084
2085 @Override
2086 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002087 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002089
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002090 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002091 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
2092 return mMainThread.acquireProvider(c, auth, mUser.getIdentifier(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002093 }
2094
2095 @Override
2096 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002097 return mMainThread.releaseProvider(icp, false);
2098 }
2099
2100 @Override
2101 public void unstableProviderDied(IContentProvider icp) {
2102 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105}