blob: 71804089f9a3f3514ab812783ddcdaa52bc3234d [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
svetoslavganov75986cf2009-05-14 22:28:01 -070019import com.android.internal.policy.PolicyManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070020import com.android.internal.util.Preconditions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
Matthew Xieddf7e472013-03-01 18:41:02 -080022import android.bluetooth.BluetoothManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.BroadcastReceiver;
24import android.content.ComponentName;
25import android.content.ContentResolver;
26import android.content.Context;
27import android.content.ContextWrapper;
28import android.content.IContentProvider;
29import android.content.Intent;
30import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070031import android.content.IIntentReceiver;
32import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.ReceiverCallNotAllowedException;
34import android.content.ServiceConnection;
35import android.content.SharedPreferences;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070039import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070041import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070042import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070044import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.database.sqlite.SQLiteDatabase;
46import android.database.sqlite.SQLiteDatabase.CursorFactory;
47import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.graphics.drawable.Drawable;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040049import android.hardware.ISerialManager;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040050import android.hardware.SerialManager;
Jeff Brown25157e42012-04-16 12:13:05 -070051import android.hardware.SystemSensorManager;
Eino-Ville Talvalab2675542012-12-12 13:29:45 -080052import android.hardware.photography.CameraManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070053import android.hardware.display.DisplayManager;
Jeff Brown9df6e7a2012-04-05 11:49:26 -070054import android.hardware.input.InputManager;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080055import android.hardware.usb.IUsbManager;
56import android.hardware.usb.UsbManager;
Bai Taoa58a8752010-07-13 15:32:16 +080057import android.location.CountryDetector;
58import android.location.ICountryDetector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.location.ILocationManager;
60import android.location.LocationManager;
61import android.media.AudioManager;
Dianne Hackbornb58b8f82012-06-11 15:08:39 -070062import android.media.MediaRouter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.net.ConnectivityManager;
64import android.net.IConnectivityManager;
Jeff Sharkey1a303952011-06-16 13:04:20 -070065import android.net.INetworkPolicyManager;
66import android.net.NetworkPolicyManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.net.Uri;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070068import android.net.nsd.INsdManager;
69import android.net.nsd.NsdManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.net.wifi.IWifiManager;
71import android.net.wifi.WifiManager;
repo sync55bc5f32011-06-24 14:23:07 -070072import android.net.wifi.p2p.IWifiP2pManager;
73import android.net.wifi.p2p.WifiP2pManager;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080074import android.nfc.NfcManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
76import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070077import android.os.Debug;
Dan Egnorf18a01c2009-11-12 11:32:50 -080078import android.os.DropBoxManager;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080079import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.FileUtils;
81import android.os.Handler;
82import android.os.IBinder;
83import android.os.IPowerManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070084import android.os.IUserManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070085import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.PowerManager;
87import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070088import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070090import android.os.UserHandle;
Jeff Brownc2346132012-04-13 01:55:38 -070091import android.os.SystemVibrator;
Amith Yamasani258848d2012-08-10 17:06:33 -070092import android.os.UserManager;
San Mehatb1043402010-02-05 08:26:50 -080093import android.os.storage.StorageManager;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070094import android.print.IPrintManager;
95import android.print.PrintManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.telephony.TelephonyManager;
Dianne Hackborn9f531192010-08-04 17:48:03 -070097import android.content.ClipboardManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070099import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -0700101import android.util.Slog;
Craig Mautner48d0d182013-06-11 07:53:06 -0700102import android.view.DisplayAdjustments;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.ContextThemeWrapper;
Jeff Brown98365d72012-08-19 20:30:52 -0700104import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.WindowManagerImpl;
svetoslavganov75986cf2009-05-14 22:28:01 -0700106import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import android.view.inputmethod.InputMethodManager;
satok988323c2011-06-22 16:38:13 +0900108import android.view.textservice.TextServicesManager;
Fred Quintana60307342009-03-24 22:48:12 -0700109import android.accounts.AccountManager;
110import android.accounts.IAccountManager;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800111import android.app.admin.DevicePolicyManager;
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800112
113import com.android.internal.app.IAppOpsService;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800114import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import java.io.File;
117import java.io.FileInputStream;
118import java.io.FileNotFoundException;
119import java.io.FileOutputStream;
120import java.io.IOException;
121import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import java.util.ArrayList;
123import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125class ReceiverRestrictedContext extends ContextWrapper {
126 ReceiverRestrictedContext(Context base) {
127 super(base);
128 }
129
130 @Override
131 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
132 return registerReceiver(receiver, filter, null, null);
133 }
134
135 @Override
136 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
137 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700138 if (receiver == null) {
139 // Allow retrieving current sticky broadcast; this is safe since we
140 // aren't actually registering a receiver.
141 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
142 } else {
143 throw new ReceiverCallNotAllowedException(
144 "BroadcastReceiver components are not allowed to register to receive intents");
145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
147
148 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700149 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
150 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700151 if (receiver == null) {
152 // Allow retrieving current sticky broadcast; this is safe since we
153 // aren't actually registering a receiver.
154 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
155 } else {
156 throw new ReceiverCallNotAllowedException(
157 "BroadcastReceiver components are not allowed to register to receive intents");
158 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700159 }
160
161 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
163 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700164 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 }
166}
167
168/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800169 * Common implementation of Context API, which provides the base
170 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800172class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800173 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700174 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700176 /**
177 * Map from package name, to preference name, to cached preferences.
178 */
179 private static ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>> sSharedPrefs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700181 /*package*/ LoadedApk mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700182 private String mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 private Resources mResources;
184 /*package*/ ActivityThread mMainThread;
185 private Context mOuterContext;
186 private IBinder mActivityToken = null;
187 private ApplicationContentResolver mContentResolver;
188 private int mThemeResource = 0;
189 private Resources.Theme mTheme = null;
190 private PackageManager mPackageManager;
Jeff Browna492c3a2012-08-23 19:48:44 -0700191 private Display mDisplay; // may be null if default display
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 private Context mReceiverRestrictedContext = null;
Romain Guy870e09f2009-07-06 16:35:25 -0700193 private boolean mRestricted;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700194 private UserHandle mUser;
Craig Mautner88c05892013-06-28 09:47:45 -0700195 private ResourcesManager mResourcesManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
197 private final Object mSync = new Object();
198
199 private File mDatabasesDir;
200 private File mPreferencesDir;
201 private File mFilesDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 private File mCacheDir;
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800203 private File mObbDir;
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800204 private File mExternalFilesDir;
205 private File mExternalCacheDir;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 private static final String[] EMPTY_FILE_LIST = {};
208
Craig Mautner48d0d182013-06-11 07:53:06 -0700209 final private DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments();
210
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800211 /**
212 * Override this class when the system service constructor needs a
213 * ContextImpl. Else, use StaticServiceFetcher below.
214 */
215 /*package*/ static class ServiceFetcher {
216 int mContextCacheIndex = -1;
217
218 /**
219 * Main entrypoint; only override if you don't need caching.
220 */
221 public Object getService(ContextImpl ctx) {
222 ArrayList<Object> cache = ctx.mServiceCache;
223 Object service;
224 synchronized (cache) {
225 if (cache.size() == 0) {
226 // Initialize the cache vector on first access.
227 // At this point sNextPerContextServiceCacheIndex
228 // is the number of potential services that are
229 // cached per-Context.
230 for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
231 cache.add(null);
232 }
233 } else {
234 service = cache.get(mContextCacheIndex);
235 if (service != null) {
236 return service;
237 }
238 }
239 service = createService(ctx);
240 cache.set(mContextCacheIndex, service);
241 return service;
242 }
243 }
244
245 /**
246 * Override this to create a new per-Context instance of the
247 * service. getService() will handle locking and caching.
248 */
249 public Object createService(ContextImpl ctx) {
250 throw new RuntimeException("Not implemented");
251 }
252 }
253
254 /**
255 * Override this class for services to be cached process-wide.
256 */
257 abstract static class StaticServiceFetcher extends ServiceFetcher {
258 private Object mCachedInstance;
259
260 @Override
261 public final Object getService(ContextImpl unused) {
262 synchronized (StaticServiceFetcher.this) {
263 Object service = mCachedInstance;
264 if (service != null) {
265 return service;
266 }
267 return mCachedInstance = createStaticService();
268 }
269 }
270
271 public abstract Object createStaticService();
272 }
273
274 private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
275 new HashMap<String, ServiceFetcher>();
276
277 private static int sNextPerContextServiceCacheIndex = 0;
278 private static void registerService(String serviceName, ServiceFetcher fetcher) {
279 if (!(fetcher instanceof StaticServiceFetcher)) {
280 fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
281 }
282 SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
283 }
284
285 // This one's defined separately and given a variable name so it
286 // can be re-used by getWallpaperManager(), avoiding a HashMap
287 // lookup.
288 private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() {
289 public Object createService(ContextImpl ctx) {
290 return new WallpaperManager(ctx.getOuterContext(),
291 ctx.mMainThread.getHandler());
292 }};
293
294 static {
295 registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
296 public Object getService(ContextImpl ctx) {
297 return AccessibilityManager.getInstance(ctx);
298 }});
299
300 registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
301 public Object createService(ContextImpl ctx) {
302 IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
303 IAccountManager service = IAccountManager.Stub.asInterface(b);
304 return new AccountManager(ctx, service);
305 }});
306
307 registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
308 public Object createService(ContextImpl ctx) {
309 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
310 }});
311
Christopher Tatee0a22b32013-07-11 14:43:13 -0700312 registerService(ALARM_SERVICE, new ServiceFetcher() {
313 public Object createService(ContextImpl ctx) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800314 IBinder b = ServiceManager.getService(ALARM_SERVICE);
315 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
Christopher Tatee0a22b32013-07-11 14:43:13 -0700316 return new AlarmManager(service, ctx);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800317 }});
318
319 registerService(AUDIO_SERVICE, new ServiceFetcher() {
320 public Object createService(ContextImpl ctx) {
321 return new AudioManager(ctx);
322 }});
323
Dianne Hackbornb58b8f82012-06-11 15:08:39 -0700324 registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() {
325 public Object createService(ContextImpl ctx) {
326 return new MediaRouter(ctx);
327 }});
328
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800329 registerService(BLUETOOTH_SERVICE, new ServiceFetcher() {
330 public Object createService(ContextImpl ctx) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800331 return new BluetoothManager(ctx);
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800332 }});
333
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800334 registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
335 public Object createService(ContextImpl ctx) {
336 return new ClipboardManager(ctx.getOuterContext(),
337 ctx.mMainThread.getHandler());
338 }});
339
340 registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
341 public Object createStaticService() {
342 IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
343 return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
344 }});
345
346 registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
347 public Object createStaticService() {
348 IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
349 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
350 }});
351
352 registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
353 public Object createService(ContextImpl ctx) {
354 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
355 }});
356
357 registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
358 public Object createService(ContextImpl ctx) {
359 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
360 }});
361
Nick Pellyd2507462010-12-13 12:22:34 -0800362 registerService(NFC_SERVICE, new ServiceFetcher() {
363 public Object createService(ContextImpl ctx) {
364 return new NfcManager(ctx);
365 }});
366
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800367 registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
368 public Object createStaticService() {
369 return createDropBoxManager();
370 }});
371
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700372 registerService(INPUT_SERVICE, new StaticServiceFetcher() {
373 public Object createStaticService() {
374 return InputManager.getInstance();
Jeff Brownac143512012-04-05 18:57:33 -0700375 }});
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700376
Jeff Brownbd6e1502012-08-28 03:27:37 -0700377 registerService(DISPLAY_SERVICE, new ServiceFetcher() {
378 @Override
379 public Object createService(ContextImpl ctx) {
380 return new DisplayManager(ctx.getOuterContext());
381 }});
Jeff Brownfa25bf52012-07-23 19:26:30 -0700382
Jeff Brownf9e989d2013-04-04 23:04:03 -0700383 registerService(INPUT_METHOD_SERVICE, new StaticServiceFetcher() {
384 public Object createStaticService() {
385 return InputMethodManager.getInstance();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800386 }});
387
satok988323c2011-06-22 16:38:13 +0900388 registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
389 public Object createService(ContextImpl ctx) {
390 return TextServicesManager.getInstance();
391 }});
392
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800393 registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
394 public Object getService(ContextImpl ctx) {
395 // TODO: why isn't this caching it? It wasn't
396 // before, so I'm preserving the old behavior and
397 // using getService(), instead of createService()
398 // which would do the caching.
399 return new KeyguardManager();
400 }});
401
402 registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
403 public Object createService(ContextImpl ctx) {
404 return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
405 }});
406
Nick Pellye0fd6932012-07-11 10:26:13 -0700407 registerService(LOCATION_SERVICE, new ServiceFetcher() {
408 public Object createService(ContextImpl ctx) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800409 IBinder b = ServiceManager.getService(LOCATION_SERVICE);
Nick Pellye0fd6932012-07-11 10:26:13 -0700410 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800411 }});
412
Jeff Sharkey1a303952011-06-16 13:04:20 -0700413 registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
414 @Override
415 public Object createService(ContextImpl ctx) {
416 return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
417 ServiceManager.getService(NETWORK_POLICY_SERVICE)));
418 }
419 });
420
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800421 registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
422 public Object createService(ContextImpl ctx) {
423 final Context outerContext = ctx.getOuterContext();
424 return new NotificationManager(
425 new ContextThemeWrapper(outerContext,
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800426 Resources.selectSystemTheme(0,
427 outerContext.getApplicationInfo().targetSdkVersion,
428 com.android.internal.R.style.Theme_Dialog,
Adam Powell6e90a362011-08-14 16:48:32 -0700429 com.android.internal.R.style.Theme_Holo_Dialog,
430 com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800431 ctx.mMainThread.getHandler());
432 }});
433
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700434 registerService(NSD_SERVICE, new ServiceFetcher() {
435 @Override
436 public Object createService(ContextImpl ctx) {
437 IBinder b = ServiceManager.getService(NSD_SERVICE);
438 INsdManager service = INsdManager.Stub.asInterface(b);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700439 return new NsdManager(ctx.getOuterContext(), service);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700440 }});
441
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800442 // Note: this was previously cached in a static variable, but
443 // constructed using mMainThread.getHandler(), so converting
444 // it to be a regular Context-cached service...
445 registerService(POWER_SERVICE, new ServiceFetcher() {
446 public Object createService(ContextImpl ctx) {
447 IBinder b = ServiceManager.getService(POWER_SERVICE);
448 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown96307042012-07-27 15:51:34 -0700449 return new PowerManager(ctx.getOuterContext(),
450 service, ctx.mMainThread.getHandler());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800451 }});
452
453 registerService(SEARCH_SERVICE, new ServiceFetcher() {
454 public Object createService(ContextImpl ctx) {
455 return new SearchManager(ctx.getOuterContext(),
456 ctx.mMainThread.getHandler());
457 }});
458
459 registerService(SENSOR_SERVICE, new ServiceFetcher() {
460 public Object createService(ContextImpl ctx) {
Jaikumar Ganesh6d0c1d72013-03-27 17:41:33 -0700461 return new SystemSensorManager(ctx.getOuterContext(),
462 ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800463 }});
464
465 registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
466 public Object createService(ContextImpl ctx) {
467 return new StatusBarManager(ctx.getOuterContext());
468 }});
469
470 registerService(STORAGE_SERVICE, new ServiceFetcher() {
471 public Object createService(ContextImpl ctx) {
472 try {
Jeff Sharkeybe722152013-02-15 16:56:38 -0800473 return new StorageManager(
474 ctx.getContentResolver(), ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800475 } catch (RemoteException rex) {
476 Log.e(TAG, "Failed to create StorageManager", rex);
477 return null;
478 }
479 }});
480
481 registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
482 public Object createService(ContextImpl ctx) {
483 return new TelephonyManager(ctx.getOuterContext());
484 }});
485
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800486 registerService(UI_MODE_SERVICE, new ServiceFetcher() {
487 public Object createService(ContextImpl ctx) {
488 return new UiModeManager();
489 }});
490
Mike Lockwood3a68b832011-03-08 10:08:59 -0500491 registerService(USB_SERVICE, new ServiceFetcher() {
492 public Object createService(ContextImpl ctx) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500493 IBinder b = ServiceManager.getService(USB_SERVICE);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500494 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500495 }});
496
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400497 registerService(SERIAL_SERVICE, new ServiceFetcher() {
498 public Object createService(ContextImpl ctx) {
499 IBinder b = ServiceManager.getService(SERIAL_SERVICE);
500 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
501 }});
502
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800503 registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
504 public Object createService(ContextImpl ctx) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800505 return new SystemVibrator(ctx);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800506 }});
507
508 registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
509
510 registerService(WIFI_SERVICE, new ServiceFetcher() {
511 public Object createService(ContextImpl ctx) {
512 IBinder b = ServiceManager.getService(WIFI_SERVICE);
513 IWifiManager service = IWifiManager.Stub.asInterface(b);
Irfan Sheriff88759bb2012-07-02 15:58:28 -0700514 return new WifiManager(ctx.getOuterContext(), service);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800515 }});
516
repo sync55bc5f32011-06-24 14:23:07 -0700517 registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
518 public Object createService(ContextImpl ctx) {
519 IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
520 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
521 return new WifiP2pManager(service);
522 }});
523
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800524 registerService(WINDOW_SERVICE, new ServiceFetcher() {
Romain Guye50848b2013-06-07 10:57:25 -0700525 Display mDefaultDisplay;
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800526 public Object getService(ContextImpl ctx) {
Jeff Browna492c3a2012-08-23 19:48:44 -0700527 Display display = ctx.mDisplay;
528 if (display == null) {
Romain Guye50848b2013-06-07 10:57:25 -0700529 if (mDefaultDisplay == null) {
530 DisplayManager dm = (DisplayManager)ctx.getOuterContext().
531 getSystemService(Context.DISPLAY_SERVICE);
532 mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
533 }
534 display = mDefaultDisplay;
Jeff Browna492c3a2012-08-23 19:48:44 -0700535 }
536 return new WindowManagerImpl(display);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800537 }});
Amith Yamasani258848d2012-08-10 17:06:33 -0700538
539 registerService(USER_SERVICE, new ServiceFetcher() {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800540 public Object createService(ContextImpl ctx) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700541 IBinder b = ServiceManager.getService(USER_SERVICE);
542 IUserManager service = IUserManager.Stub.asInterface(b);
543 return new UserManager(ctx, service);
544 }});
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800545
546 registerService(APP_OPS_SERVICE, new ServiceFetcher() {
547 public Object createService(ContextImpl ctx) {
548 IBinder b = ServiceManager.getService(APP_OPS_SERVICE);
549 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
550 return new AppOpsManager(ctx, service);
551 }});
Eino-Ville Talvalab2675542012-12-12 13:29:45 -0800552
Igor Murashkine363fbb2013-06-25 20:26:06 +0000553 registerService(CAMERA_SERVICE, new ServiceFetcher() {
554 public Object createService(ContextImpl ctx) {
555 return new CameraManager(ctx);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700556 }
557 });
558
559 registerService(PRINT_SERVICE, new ServiceFetcher() {
560 public Object createService(ContextImpl ctx) {
561 IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
562 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
563 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
564 UserHandle.getAppId(Process.myUid()));
Eino-Ville Talvalab2675542012-12-12 13:29:45 -0800565 }});
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800566 }
567
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700568 static ContextImpl getImpl(Context context) {
569 Context nextContext;
570 while ((context instanceof ContextWrapper) &&
571 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
572 context = nextContext;
573 }
574 return (ContextImpl)context;
575 }
576
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800577 // The system service cache for the system services that are
578 // cached per-ContextImpl. Package-scoped to avoid accessor
579 // methods.
580 final ArrayList<Object> mServiceCache = new ArrayList<Object>();
581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 @Override
583 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700584 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586
587 @Override
588 public Resources getResources() {
589 return mResources;
590 }
591
592 @Override
593 public PackageManager getPackageManager() {
594 if (mPackageManager != null) {
595 return mPackageManager;
596 }
597
598 IPackageManager pm = ActivityThread.getPackageManager();
599 if (pm != null) {
600 // Doesn't matter if we make more than one instance.
601 return (mPackageManager = new ApplicationPackageManager(this, pm));
602 }
603
604 return null;
605 }
606
607 @Override
608 public ContentResolver getContentResolver() {
609 return mContentResolver;
610 }
611
612 @Override
613 public Looper getMainLooper() {
614 return mMainThread.getLooper();
615 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 @Override
618 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700619 return (mPackageInfo != null) ?
620 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 @Override
624 public void setTheme(int resid) {
625 mThemeResource = resid;
626 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800629 public int getThemeResId() {
630 return mThemeResource;
631 }
632
633 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 public Resources.Theme getTheme() {
635 if (mTheme == null) {
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800636 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
637 getOuterContext().getApplicationInfo().targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 mTheme = mResources.newTheme();
639 mTheme.applyStyle(mThemeResource, true);
640 }
641 return mTheme;
642 }
643
644 @Override
645 public ClassLoader getClassLoader() {
646 return mPackageInfo != null ?
647 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
648 }
649
650 @Override
651 public String getPackageName() {
652 if (mPackageInfo != null) {
653 return mPackageInfo.getPackageName();
654 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800655 // No mPackageInfo means this is a Context for the system itself,
656 // and this here is its name.
657 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 }
659
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800660 /** @hide */
661 @Override
662 public String getBasePackageName() {
663 return mBasePackageName != null ? mBasePackageName : getPackageName();
664 }
665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700667 public ApplicationInfo getApplicationInfo() {
668 if (mPackageInfo != null) {
669 return mPackageInfo.getApplicationInfo();
670 }
671 throw new RuntimeException("Not supported in system context");
672 }
673
674 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 public String getPackageResourcePath() {
676 if (mPackageInfo != null) {
677 return mPackageInfo.getResDir();
678 }
679 throw new RuntimeException("Not supported in system context");
680 }
681
682 @Override
683 public String getPackageCodePath() {
684 if (mPackageInfo != null) {
685 return mPackageInfo.getAppDir();
686 }
687 throw new RuntimeException("Not supported in system context");
688 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200689
Joe Onorato23ecae32009-06-10 17:07:15 -0700690 public File getSharedPrefsFile(String name) {
691 return makeFilename(getPreferencesDir(), name + ".xml");
692 }
693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 @Override
695 public SharedPreferences getSharedPreferences(String name, int mode) {
696 SharedPreferencesImpl sp;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700697 synchronized (mSync) {
698 if (sSharedPrefs == null) {
699 sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>();
700 }
701
702 final String packageName = getPackageName();
703 ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
704 if (packagePrefs == null) {
705 packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
706 sSharedPrefs.put(packageName, packagePrefs);
707 }
708
709 sp = packagePrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700710 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800711 File prefsFile = getSharedPrefsFile(name);
712 sp = new SharedPreferencesImpl(prefsFile, mode);
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700713 packagePrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700714 return sp;
715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800717 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
718 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
719 // If somebody else (some other process) changed the prefs
720 // file behind our back, we reload it. This has been the
721 // historical (if undocumented) behavior.
722 sp.startReloadIfChangedUnexpectedly();
723 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700724 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726
727 private File getPreferencesDir() {
728 synchronized (mSync) {
729 if (mPreferencesDir == null) {
730 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
731 }
732 return mPreferencesDir;
733 }
734 }
735
736 @Override
737 public FileInputStream openFileInput(String name)
738 throws FileNotFoundException {
739 File f = makeFilename(getFilesDir(), name);
740 return new FileInputStream(f);
741 }
742
743 @Override
744 public FileOutputStream openFileOutput(String name, int mode)
745 throws FileNotFoundException {
746 final boolean append = (mode&MODE_APPEND) != 0;
747 File f = makeFilename(getFilesDir(), name);
748 try {
749 FileOutputStream fos = new FileOutputStream(f, append);
750 setFilePermissionsFromMode(f.getPath(), mode, 0);
751 return fos;
752 } catch (FileNotFoundException e) {
753 }
754
755 File parent = f.getParentFile();
756 parent.mkdir();
757 FileUtils.setPermissions(
758 parent.getPath(),
759 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
760 -1, -1);
761 FileOutputStream fos = new FileOutputStream(f, append);
762 setFilePermissionsFromMode(f.getPath(), mode, 0);
763 return fos;
764 }
765
766 @Override
767 public boolean deleteFile(String name) {
768 File f = makeFilename(getFilesDir(), name);
769 return f.delete();
770 }
771
772 @Override
773 public File getFilesDir() {
774 synchronized (mSync) {
775 if (mFilesDir == null) {
776 mFilesDir = new File(getDataDirFile(), "files");
777 }
778 if (!mFilesDir.exists()) {
779 if(!mFilesDir.mkdirs()) {
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200780 Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 return null;
782 }
783 FileUtils.setPermissions(
784 mFilesDir.getPath(),
785 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
786 -1, -1);
787 }
788 return mFilesDir;
789 }
790 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 @Override
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800793 public File getExternalFilesDir(String type) {
794 synchronized (mSync) {
795 if (mExternalFilesDir == null) {
796 mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory(
797 getPackageName());
798 }
799 if (!mExternalFilesDir.exists()) {
800 try {
801 (new File(Environment.getExternalStorageAndroidDataDir(),
802 ".nomedia")).createNewFile();
803 } catch (IOException e) {
804 }
805 if (!mExternalFilesDir.mkdirs()) {
806 Log.w(TAG, "Unable to create external files directory");
807 return null;
808 }
809 }
810 if (type == null) {
811 return mExternalFilesDir;
812 }
813 File dir = new File(mExternalFilesDir, type);
814 if (!dir.exists()) {
815 if (!dir.mkdirs()) {
816 Log.w(TAG, "Unable to create external media directory " + dir);
817 return null;
818 }
819 }
820 return dir;
821 }
822 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200823
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800824 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800825 public File getObbDir() {
826 synchronized (mSync) {
827 if (mObbDir == null) {
828 mObbDir = Environment.getExternalStorageAppObbDirectory(
829 getPackageName());
830 }
831 return mObbDir;
832 }
833 }
834
835 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 public File getCacheDir() {
837 synchronized (mSync) {
838 if (mCacheDir == null) {
839 mCacheDir = new File(getDataDirFile(), "cache");
840 }
841 if (!mCacheDir.exists()) {
842 if(!mCacheDir.mkdirs()) {
Amith Yamasani92d57052012-08-23 10:07:52 -0700843 Log.w(TAG, "Unable to create cache directory " + mCacheDir.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 return null;
845 }
846 FileUtils.setPermissions(
847 mCacheDir.getPath(),
848 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
849 -1, -1);
850 }
851 }
852 return mCacheDir;
853 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200854
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800855 @Override
856 public File getExternalCacheDir() {
857 synchronized (mSync) {
858 if (mExternalCacheDir == null) {
859 mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory(
860 getPackageName());
861 }
862 if (!mExternalCacheDir.exists()) {
863 try {
864 (new File(Environment.getExternalStorageAndroidDataDir(),
865 ".nomedia")).createNewFile();
866 } catch (IOException e) {
867 }
868 if (!mExternalCacheDir.mkdirs()) {
869 Log.w(TAG, "Unable to create external cache directory");
870 return null;
871 }
872 }
873 return mExternalCacheDir;
874 }
875 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 @Override
878 public File getFileStreamPath(String name) {
879 return makeFilename(getFilesDir(), name);
880 }
881
882 @Override
883 public String[] fileList() {
884 final String[] list = getFilesDir().list();
885 return (list != null) ? list : EMPTY_FILE_LIST;
886 }
887
888 @Override
889 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700890 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
892
893 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700894 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
895 DatabaseErrorHandler errorHandler) {
896 File f = validateFilePath(name, true);
Jeff Brown47847f32012-03-22 19:13:11 -0700897 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
898 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
899 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
900 }
901 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700902 setFilePermissionsFromMode(f.getPath(), mode, 0);
903 return db;
904 }
905
906 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 public boolean deleteDatabase(String name) {
908 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800909 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800910 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 } catch (Exception e) {
912 }
913 return false;
914 }
915
916 @Override
917 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800918 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920
921 @Override
922 public String[] databaseList() {
923 final String[] list = getDatabasesDir().list();
924 return (list != null) ? list : EMPTY_FILE_LIST;
925 }
926
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 private File getDatabasesDir() {
929 synchronized (mSync) {
930 if (mDatabasesDir == null) {
931 mDatabasesDir = new File(getDataDirFile(), "databases");
932 }
933 if (mDatabasesDir.getPath().equals("databases")) {
934 mDatabasesDir = new File("/data/system");
935 }
936 return mDatabasesDir;
937 }
938 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 @Override
941 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700942 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 }
944
945 @Override
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700946 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700947 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949
950 @Override
951 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700952 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
954
955 @Override
956 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700957 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
959
960 @Override
961 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700962 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964
965 @Override
966 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700967 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
969
970 @Override
971 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700972 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
974
975 @Override
976 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700977 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700978 startActivity(intent, null);
979 }
980
Amith Yamasani82644082012-08-03 13:09:11 -0700981 /** @hide */
982 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700983 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700984 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700985 }
986
Dianne Hackborna4972e92012-03-14 10:38:05 -0700987 @Override
988 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700989 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
991 throw new AndroidRuntimeException(
992 "Calling startActivity() from outside of an Activity "
993 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
994 + " Is this really what you want?");
995 }
996 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700997 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700998 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
1000
Amith Yamasani258848d2012-08-10 17:06:33 -07001001 /** @hide */
1002 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001003 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001004 try {
1005 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001006 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -07001007 intent.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001008 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options,
1009 user.getIdentifier());
Amith Yamasani258848d2012-08-10 17:06:33 -07001010 } catch (RemoteException re) {
1011 }
1012 }
1013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001015 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001016 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -07001017 startActivities(intents, null);
1018 }
1019
Amith Yamasaniea7e9152012-09-24 16:11:18 -07001020 /** @hide */
1021 @Override
1022 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
1023 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1024 throw new AndroidRuntimeException(
1025 "Calling startActivities() from outside of an Activity "
1026 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
1027 + " Is this really what you want?");
1028 }
1029 mMainThread.getInstrumentation().execStartActivitiesAsUser(
1030 getOuterContext(), mMainThread.getApplicationThread(), null,
1031 (Activity)null, intents, options, userHandle.getIdentifier());
1032 }
1033
Dianne Hackborna4972e92012-03-14 10:38:05 -07001034 @Override
1035 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001036 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001037 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1038 throw new AndroidRuntimeException(
1039 "Calling startActivities() from outside of an Activity "
1040 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
1041 + " Is this really what you want?");
1042 }
1043 mMainThread.getInstrumentation().execStartActivities(
1044 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001045 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001046 }
1047
1048 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001049 public void startIntentSender(IntentSender intent,
1050 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
1051 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001052 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
1053 }
1054
1055 @Override
1056 public void startIntentSender(IntentSender intent, Intent fillInIntent,
1057 int flagsMask, int flagsValues, int extraFlags, Bundle options)
1058 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001059 try {
1060 String resolvedType = null;
1061 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001062 fillInIntent.migrateExtraStreamToClipData();
1063 fillInIntent.prepareToLeaveProcess();
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001064 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
1065 }
1066 int result = ActivityManagerNative.getDefault()
1067 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
1068 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001069 0, flagsMask, flagsValues, options);
1070 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001071 throw new IntentSender.SendIntentException();
1072 }
1073 Instrumentation.checkStartActivityResult(result, null);
1074 } catch (RemoteException e) {
1075 }
1076 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001077
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001078 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001080 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1082 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001083 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 ActivityManagerNative.getDefault().broadcastIntent(
1085 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001086 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001087 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 } catch (RemoteException e) {
1089 }
1090 }
1091
Amith Yamasani67cf7d32012-02-16 14:31:23 -08001092 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001094 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1096 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001097 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 ActivityManagerNative.getDefault().broadcastIntent(
1099 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001100 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE,
1101 false, false, getUserId());
1102 } catch (RemoteException e) {
1103 }
1104 }
1105
1106 @Override
1107 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
1108 warnIfCallingFromSystemProcess();
1109 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1110 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001111 intent.prepareToLeaveProcess();
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001112 ActivityManagerNative.getDefault().broadcastIntent(
1113 mMainThread.getApplicationThread(), intent, resolvedType, null,
1114 Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001115 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 } catch (RemoteException e) {
1117 }
1118 }
1119
1120 @Override
1121 public void sendOrderedBroadcast(Intent intent,
1122 String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001123 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1125 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001126 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 ActivityManagerNative.getDefault().broadcastIntent(
1128 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001129 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, true, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001130 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 } catch (RemoteException e) {
1132 }
1133 }
1134
1135 @Override
1136 public void sendOrderedBroadcast(Intent intent,
1137 String receiverPermission, BroadcastReceiver resultReceiver,
1138 Handler scheduler, int initialCode, String initialData,
1139 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001140 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1141 resultReceiver, scheduler, initialCode, initialData, initialExtras);
1142 }
1143
1144 @Override
1145 public void sendOrderedBroadcast(Intent intent,
1146 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1147 Handler scheduler, int initialCode, String initialData,
1148 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001149 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 IIntentReceiver rd = null;
1151 if (resultReceiver != null) {
1152 if (mPackageInfo != null) {
1153 if (scheduler == null) {
1154 scheduler = mMainThread.getHandler();
1155 }
1156 rd = mPackageInfo.getReceiverDispatcher(
1157 resultReceiver, getOuterContext(), scheduler,
1158 mMainThread.getInstrumentation(), false);
1159 } else {
1160 if (scheduler == null) {
1161 scheduler = mMainThread.getHandler();
1162 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001163 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1165 }
1166 }
1167 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1168 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001169 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 ActivityManagerNative.getDefault().broadcastIntent(
1171 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001172 initialCode, initialData, initialExtras, receiverPermission, appOp,
1173 true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 } catch (RemoteException e) {
1175 }
1176 }
1177
1178 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001179 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001180 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1181 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001182 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001183 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001184 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
1185 AppOpsManager.OP_NONE, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001186 } catch (RemoteException e) {
1187 }
1188 }
1189
1190 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001191 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1192 String receiverPermission) {
1193 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1194 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001195 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001196 ActivityManagerNative.getDefault().broadcastIntent(
1197 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001198 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, false, false,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001199 user.getIdentifier());
1200 } catch (RemoteException e) {
1201 }
1202 }
1203
1204 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001205 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001206 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001207 int initialCode, String initialData, Bundle initialExtras) {
1208 IIntentReceiver rd = null;
1209 if (resultReceiver != null) {
1210 if (mPackageInfo != null) {
1211 if (scheduler == null) {
1212 scheduler = mMainThread.getHandler();
1213 }
1214 rd = mPackageInfo.getReceiverDispatcher(
1215 resultReceiver, getOuterContext(), scheduler,
1216 mMainThread.getInstrumentation(), false);
1217 } else {
1218 if (scheduler == null) {
1219 scheduler = mMainThread.getHandler();
1220 }
1221 rd = new LoadedApk.ReceiverDispatcher(
1222 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1223 }
1224 }
1225 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1226 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001227 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001228 ActivityManagerNative.getDefault().broadcastIntent(
1229 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001230 initialCode, initialData, initialExtras, receiverPermission,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001231 AppOpsManager.OP_NONE, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001232 } catch (RemoteException e) {
1233 }
1234 }
1235
1236 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001238 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1240 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001241 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 ActivityManagerNative.getDefault().broadcastIntent(
1243 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001244 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001245 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 } catch (RemoteException e) {
1247 }
1248 }
1249
1250 @Override
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001251 public void sendStickyOrderedBroadcast(Intent intent,
1252 BroadcastReceiver resultReceiver,
1253 Handler scheduler, int initialCode, String initialData,
1254 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001255 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001256 IIntentReceiver rd = null;
1257 if (resultReceiver != null) {
1258 if (mPackageInfo != null) {
1259 if (scheduler == null) {
1260 scheduler = mMainThread.getHandler();
1261 }
1262 rd = mPackageInfo.getReceiverDispatcher(
1263 resultReceiver, getOuterContext(), scheduler,
1264 mMainThread.getInstrumentation(), false);
1265 } else {
1266 if (scheduler == null) {
1267 scheduler = mMainThread.getHandler();
1268 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001269 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001270 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1271 }
1272 }
1273 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1274 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001275 intent.prepareToLeaveProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001276 ActivityManagerNative.getDefault().broadcastIntent(
1277 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1278 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001279 AppOpsManager.OP_NONE, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001280 } catch (RemoteException e) {
1281 }
1282 }
1283
1284 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 public void removeStickyBroadcast(Intent intent) {
1286 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1287 if (resolvedType != null) {
1288 intent = new Intent(intent);
1289 intent.setDataAndType(intent.getData(), resolvedType);
1290 }
1291 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001292 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001294 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 } catch (RemoteException e) {
1296 }
1297 }
1298
1299 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001300 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1301 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1302 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001303 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001304 ActivityManagerNative.getDefault().broadcastIntent(
1305 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001306 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001307 } catch (RemoteException e) {
1308 }
1309 }
1310
1311 @Override
1312 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1313 UserHandle user, BroadcastReceiver resultReceiver,
1314 Handler scheduler, int initialCode, String initialData,
1315 Bundle initialExtras) {
1316 IIntentReceiver rd = null;
1317 if (resultReceiver != null) {
1318 if (mPackageInfo != null) {
1319 if (scheduler == null) {
1320 scheduler = mMainThread.getHandler();
1321 }
1322 rd = mPackageInfo.getReceiverDispatcher(
1323 resultReceiver, getOuterContext(), scheduler,
1324 mMainThread.getInstrumentation(), false);
1325 } else {
1326 if (scheduler == null) {
1327 scheduler = mMainThread.getHandler();
1328 }
1329 rd = new LoadedApk.ReceiverDispatcher(
1330 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1331 }
1332 }
1333 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1334 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001335 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001336 ActivityManagerNative.getDefault().broadcastIntent(
1337 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1338 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001339 AppOpsManager.OP_NONE, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001340 } catch (RemoteException e) {
1341 }
1342 }
1343
1344 @Override
1345 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1346 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1347 if (resolvedType != null) {
1348 intent = new Intent(intent);
1349 intent.setDataAndType(intent.getData(), resolvedType);
1350 }
1351 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001352 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001353 ActivityManagerNative.getDefault().unbroadcastIntent(
1354 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1355 } catch (RemoteException e) {
1356 }
1357 }
1358
1359 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1361 return registerReceiver(receiver, filter, null, null);
1362 }
1363
1364 @Override
1365 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1366 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001367 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001368 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 }
1370
Dianne Hackborn20e80982012-08-31 19:00:44 -07001371 @Override
1372 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1373 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1374 return registerReceiverInternal(receiver, user.getIdentifier(),
1375 filter, broadcastPermission, scheduler, getOuterContext());
1376 }
1377
1378 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 IntentFilter filter, String broadcastPermission,
1380 Handler scheduler, Context context) {
1381 IIntentReceiver rd = null;
1382 if (receiver != null) {
1383 if (mPackageInfo != null && context != null) {
1384 if (scheduler == null) {
1385 scheduler = mMainThread.getHandler();
1386 }
1387 rd = mPackageInfo.getReceiverDispatcher(
1388 receiver, context, scheduler,
1389 mMainThread.getInstrumentation(), true);
1390 } else {
1391 if (scheduler == null) {
1392 scheduler = mMainThread.getHandler();
1393 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001394 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001395 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 }
1397 }
1398 try {
1399 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001400 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001401 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 } catch (RemoteException e) {
1403 return null;
1404 }
1405 }
1406
1407 @Override
1408 public void unregisterReceiver(BroadcastReceiver receiver) {
1409 if (mPackageInfo != null) {
1410 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1411 getOuterContext(), receiver);
1412 try {
1413 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1414 } catch (RemoteException e) {
1415 }
1416 } else {
1417 throw new RuntimeException("Not supported in system context");
1418 }
1419 }
1420
1421 @Override
1422 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001423 warnIfCallingFromSystemProcess();
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001424 return startServiceAsUser(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001425 }
1426
1427 @Override
1428 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001429 warnIfCallingFromSystemProcess();
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001430 return stopServiceAsUser(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001431 }
1432
1433 @Override
1434 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001436 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 ComponentName cn = ActivityManagerNative.getDefault().startService(
1438 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001439 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001440 if (cn != null) {
1441 if (cn.getPackageName().equals("!")) {
1442 throw new SecurityException(
1443 "Not allowed to start service " + service
1444 + " without permission " + cn.getClassName());
1445 } else if (cn.getPackageName().equals("!!")) {
1446 throw new SecurityException(
1447 "Unable to start service " + service
1448 + ": " + cn.getClassName());
1449 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451 return cn;
1452 } catch (RemoteException e) {
1453 return null;
1454 }
1455 }
1456
1457 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001458 public boolean stopServiceAsUser(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001460 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 int res = ActivityManagerNative.getDefault().stopService(
1462 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001463 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 if (res < 0) {
1465 throw new SecurityException(
1466 "Not allowed to stop service " + service);
1467 }
1468 return res != 0;
1469 } catch (RemoteException e) {
1470 return false;
1471 }
1472 }
1473
1474 @Override
1475 public boolean bindService(Intent service, ServiceConnection conn,
1476 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001477 warnIfCallingFromSystemProcess();
Amith Yamasani27b89e62013-01-16 12:30:11 -08001478 return bindServiceAsUser(service, conn, flags, Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001479 }
1480
1481 /** @hide */
1482 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001483 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1484 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001486 if (conn == null) {
1487 throw new IllegalArgumentException("connection is null");
1488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 if (mPackageInfo != null) {
1490 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1491 mMainThread.getHandler(), flags);
1492 } else {
1493 throw new RuntimeException("Not supported in system context");
1494 }
1495 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001496 IBinder token = getActivityToken();
1497 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1498 && mPackageInfo.getApplicationInfo().targetSdkVersion
1499 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1500 flags |= BIND_WAIVE_PRIORITY;
1501 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001502 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 int res = ActivityManagerNative.getDefault().bindService(
1504 mMainThread.getApplicationThread(), getActivityToken(),
1505 service, service.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasani27b89e62013-01-16 12:30:11 -08001506 sd, flags, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 if (res < 0) {
1508 throw new SecurityException(
1509 "Not allowed to bind to service " + service);
1510 }
1511 return res != 0;
1512 } catch (RemoteException e) {
1513 return false;
1514 }
1515 }
1516
1517 @Override
1518 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001519 if (conn == null) {
1520 throw new IllegalArgumentException("connection is null");
1521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 if (mPackageInfo != null) {
1523 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1524 getOuterContext(), conn);
1525 try {
1526 ActivityManagerNative.getDefault().unbindService(sd);
1527 } catch (RemoteException e) {
1528 }
1529 } else {
1530 throw new RuntimeException("Not supported in system context");
1531 }
1532 }
1533
1534 @Override
1535 public boolean startInstrumentation(ComponentName className,
1536 String profileFile, Bundle arguments) {
1537 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001538 if (arguments != null) {
1539 arguments.setAllowFds(false);
1540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 return ActivityManagerNative.getDefault().startInstrumentation(
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001542 className, profileFile, 0, arguments, null, null, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 } catch (RemoteException e) {
1544 // System has crashed, nothing we can do.
1545 }
1546 return false;
1547 }
1548
1549 @Override
1550 public Object getSystemService(String name) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001551 ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
1552 return fetcher == null ? null : fetcher.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001555 private WallpaperManager getWallpaperManager() {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001556 return (WallpaperManager) WALLPAPER_FETCHER.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
1558
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001559 /* package */ static DropBoxManager createDropBoxManager() {
1560 IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
1561 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001562 if (service == null) {
1563 // Don't return a DropBoxManager that will NPE upon use.
1564 // This also avoids caching a broken DropBoxManager in
1565 // getDropBoxManager during early boot, before the
1566 // DROPBOX_SERVICE is registered.
1567 return null;
1568 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001569 return new DropBoxManager(service);
1570 }
1571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 @Override
1573 public int checkPermission(String permission, int pid, int uid) {
1574 if (permission == null) {
1575 throw new IllegalArgumentException("permission is null");
1576 }
1577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 try {
1579 return ActivityManagerNative.getDefault().checkPermission(
1580 permission, pid, uid);
1581 } catch (RemoteException e) {
1582 return PackageManager.PERMISSION_DENIED;
1583 }
1584 }
1585
1586 @Override
1587 public int checkCallingPermission(String permission) {
1588 if (permission == null) {
1589 throw new IllegalArgumentException("permission is null");
1590 }
1591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 int pid = Binder.getCallingPid();
1593 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001594 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
1596 return PackageManager.PERMISSION_DENIED;
1597 }
1598
1599 @Override
1600 public int checkCallingOrSelfPermission(String permission) {
1601 if (permission == null) {
1602 throw new IllegalArgumentException("permission is null");
1603 }
1604
1605 return checkPermission(permission, Binder.getCallingPid(),
1606 Binder.getCallingUid());
1607 }
1608
1609 private void enforce(
1610 String permission, int resultOfCheck,
1611 boolean selfToo, int uid, String message) {
1612 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1613 throw new SecurityException(
1614 (message != null ? (message + ": ") : "") +
1615 (selfToo
1616 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001617 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 permission +
1619 ".");
1620 }
1621 }
1622
1623 public void enforcePermission(
1624 String permission, int pid, int uid, String message) {
1625 enforce(permission,
1626 checkPermission(permission, pid, uid),
1627 false,
1628 uid,
1629 message);
1630 }
1631
1632 public void enforceCallingPermission(String permission, String message) {
1633 enforce(permission,
1634 checkCallingPermission(permission),
1635 false,
1636 Binder.getCallingUid(),
1637 message);
1638 }
1639
1640 public void enforceCallingOrSelfPermission(
1641 String permission, String message) {
1642 enforce(permission,
1643 checkCallingOrSelfPermission(permission),
1644 true,
1645 Binder.getCallingUid(),
1646 message);
1647 }
1648
1649 @Override
1650 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1651 try {
1652 ActivityManagerNative.getDefault().grantUriPermission(
1653 mMainThread.getApplicationThread(), toPackage, uri,
1654 modeFlags);
1655 } catch (RemoteException e) {
1656 }
1657 }
1658
1659 @Override
1660 public void revokeUriPermission(Uri uri, int modeFlags) {
1661 try {
1662 ActivityManagerNative.getDefault().revokeUriPermission(
1663 mMainThread.getApplicationThread(), uri,
1664 modeFlags);
1665 } catch (RemoteException e) {
1666 }
1667 }
1668
1669 @Override
1670 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 try {
1672 return ActivityManagerNative.getDefault().checkUriPermission(
1673 uri, pid, uid, modeFlags);
1674 } catch (RemoteException e) {
1675 return PackageManager.PERMISSION_DENIED;
1676 }
1677 }
1678
1679 @Override
1680 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 int pid = Binder.getCallingPid();
1682 if (pid != Process.myPid()) {
1683 return checkUriPermission(uri, pid,
1684 Binder.getCallingUid(), modeFlags);
1685 }
1686 return PackageManager.PERMISSION_DENIED;
1687 }
1688
1689 @Override
1690 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1691 return checkUriPermission(uri, Binder.getCallingPid(),
1692 Binder.getCallingUid(), modeFlags);
1693 }
1694
1695 @Override
1696 public int checkUriPermission(Uri uri, String readPermission,
1697 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001698 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1700 + readPermission + " writePermission=" + writePermission
1701 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1702 }
1703 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1704 if (readPermission == null
1705 || checkPermission(readPermission, pid, uid)
1706 == PackageManager.PERMISSION_GRANTED) {
1707 return PackageManager.PERMISSION_GRANTED;
1708 }
1709 }
1710 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1711 if (writePermission == null
1712 || checkPermission(writePermission, pid, uid)
1713 == PackageManager.PERMISSION_GRANTED) {
1714 return PackageManager.PERMISSION_GRANTED;
1715 }
1716 }
1717 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1718 : PackageManager.PERMISSION_DENIED;
1719 }
1720
1721 private String uriModeFlagToString(int uriModeFlags) {
1722 switch (uriModeFlags) {
1723 case Intent.FLAG_GRANT_READ_URI_PERMISSION |
1724 Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1725 return "read and write";
1726 case Intent.FLAG_GRANT_READ_URI_PERMISSION:
1727 return "read";
1728 case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1729 return "write";
1730 }
1731 throw new IllegalArgumentException(
1732 "Unknown permission mode flags: " + uriModeFlags);
1733 }
1734
1735 private void enforceForUri(
1736 int modeFlags, int resultOfCheck, boolean selfToo,
1737 int uid, Uri uri, String message) {
1738 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1739 throw new SecurityException(
1740 (message != null ? (message + ": ") : "") +
1741 (selfToo
1742 ? "Neither user " + uid + " nor current process has "
1743 : "User " + uid + " does not have ") +
1744 uriModeFlagToString(modeFlags) +
1745 " permission on " +
1746 uri +
1747 ".");
1748 }
1749 }
1750
1751 public void enforceUriPermission(
1752 Uri uri, int pid, int uid, int modeFlags, String message) {
1753 enforceForUri(
1754 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1755 false, uid, uri, message);
1756 }
1757
1758 public void enforceCallingUriPermission(
1759 Uri uri, int modeFlags, String message) {
1760 enforceForUri(
1761 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001762 false,
1763 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 }
1765
1766 public void enforceCallingOrSelfUriPermission(
1767 Uri uri, int modeFlags, String message) {
1768 enforceForUri(
1769 modeFlags,
1770 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1771 Binder.getCallingUid(), uri, message);
1772 }
1773
1774 public void enforceUriPermission(
1775 Uri uri, String readPermission, String writePermission,
1776 int pid, int uid, int modeFlags, String message) {
1777 enforceForUri(modeFlags,
1778 checkUriPermission(
1779 uri, readPermission, writePermission, pid, uid,
1780 modeFlags),
1781 false,
1782 uid,
1783 uri,
1784 message);
1785 }
1786
Amith Yamasanicd757062012-10-19 18:23:52 -07001787 private void warnIfCallingFromSystemProcess() {
1788 if (Process.myUid() == Process.SYSTEM_UID) {
1789 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001790 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001791 }
1792 }
1793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 @Override
1795 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001796 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001797 return createPackageContextAsUser(packageName, flags,
1798 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001799 }
1800
1801 @Override
1802 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1803 throws NameNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001805 final ContextImpl context = new ContextImpl(mMainThread.getSystemContext());
Adam Powell6f2a3d22012-10-05 22:32:04 -07001806 context.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
1807 context.init(mPackageInfo, null, mMainThread, mResources, mBasePackageName, user);
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001808 return context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
1810
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001811 LoadedApk pi =
Amith Yamasani98edc952012-09-25 14:09:27 -07001812 mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags,
1813 user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 if (pi != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001815 ContextImpl c = new ContextImpl();
Romain Guy870e09f2009-07-06 16:35:25 -07001816 c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001817 c.init(pi, null, mMainThread, mResources, mBasePackageName, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 if (c.mResources != null) {
1819 return c;
1820 }
1821 }
1822
1823 // Should be a better exception.
1824 throw new PackageManager.NameNotFoundException(
1825 "Application package " + packageName + " not found");
1826 }
1827
Romain Guy870e09f2009-07-06 16:35:25 -07001828 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001829 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001830 if (overrideConfiguration == null) {
1831 throw new IllegalArgumentException("overrideConfiguration must not be null");
1832 }
1833
Dianne Hackborn756220b2012-08-14 16:45:30 -07001834 ContextImpl c = new ContextImpl();
1835 c.init(mPackageInfo, null, mMainThread);
Craig Mautner88c05892013-06-28 09:47:45 -07001836 c.mResources = mResourcesManager.getTopLevelResources(mPackageInfo.getResDir(),
1837 getDisplayId(), overrideConfiguration, mResources.getCompatibilityInfo(),
1838 mActivityToken);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001839 return c;
1840 }
1841
1842 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001843 public Context createDisplayContext(Display display) {
1844 if (display == null) {
1845 throw new IllegalArgumentException("display must not be null");
1846 }
1847
1848 int displayId = display.getDisplayId();
Jeff Browna492c3a2012-08-23 19:48:44 -07001849
1850 ContextImpl context = new ContextImpl();
1851 context.init(mPackageInfo, null, mMainThread);
1852 context.mDisplay = display;
Craig Mautner48d0d182013-06-11 07:53:06 -07001853 DisplayAdjustments daj = getDisplayAdjustments(displayId);
Craig Mautner88c05892013-06-28 09:47:45 -07001854 context.mResources = mResourcesManager.getTopLevelResources(mPackageInfo.getResDir(),
1855 displayId, null, daj.getCompatibilityInfo(), null);
Jeff Browna492c3a2012-08-23 19:48:44 -07001856 return context;
1857 }
1858
1859 private int getDisplayId() {
1860 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1861 }
1862
1863 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001864 public boolean isRestricted() {
1865 return mRestricted;
1866 }
1867
Jeff Brown98365d72012-08-19 20:30:52 -07001868 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001869 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1870 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -07001871 }
1872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 private File getDataDirFile() {
1874 if (mPackageInfo != null) {
1875 return mPackageInfo.getDataDirFile();
1876 }
1877 throw new RuntimeException("Not supported in system context");
1878 }
1879
1880 @Override
1881 public File getDir(String name, int mode) {
1882 name = "app_" + name;
1883 File file = makeFilename(getDataDirFile(), name);
1884 if (!file.exists()) {
1885 file.mkdir();
1886 setFilePermissionsFromMode(file.getPath(), mode,
1887 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1888 }
1889 return file;
1890 }
1891
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001892 /** {@hide} */
1893 public int getUserId() {
1894 return mUser.getIdentifier();
1895 }
1896
Dianne Hackborn21556372010-02-04 16:34:40 -08001897 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001898 final ContextImpl context = new ContextImpl();
1899 context.init(Resources.getSystem(), mainThread, Process.myUserHandle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 return context;
1901 }
1902
Dianne Hackborn21556372010-02-04 16:34:40 -08001903 ContextImpl() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 mOuterContext = this;
1905 }
1906
1907 /**
1908 * Create a new ApplicationContext from an existing one. The new one
1909 * works and operates the same as the one it is copying.
1910 *
1911 * @param context Existing application context.
1912 */
Dianne Hackborn21556372010-02-04 16:34:40 -08001913 public ContextImpl(ContextImpl context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 mPackageInfo = context.mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001915 mBasePackageName = context.mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 mResources = context.mResources;
1917 mMainThread = context.mMainThread;
1918 mContentResolver = context.mContentResolver;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001919 mUser = context.mUser;
Jeff Browna492c3a2012-08-23 19:48:44 -07001920 mDisplay = context.mDisplay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 mOuterContext = this;
Craig Mautner48d0d182013-06-11 07:53:06 -07001922 mDisplayAdjustments.setCompatibilityInfo(mPackageInfo.getCompatibilityInfo());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924
Jeff Sharkey6d515712012-09-20 16:06:08 -07001925 final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread) {
1926 init(packageInfo, activityToken, mainThread, null, null, Process.myUserHandle());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001927 }
1928
Jeff Sharkey6d515712012-09-20 16:06:08 -07001929 final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread,
1930 Resources container, String basePackageName, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 mPackageInfo = packageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001932 mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 mResources = mPackageInfo.getResources(mainThread);
Craig Mautner88c05892013-06-28 09:47:45 -07001934 mResourcesManager = ResourcesManager.getInstance();
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001935
Craig Mautner48d0d182013-06-11 07:53:06 -07001936 CompatibilityInfo compatInfo =
1937 container == null ? null : container.getCompatibilityInfo();
1938 if (mResources != null &&
1939 ((compatInfo != null && compatInfo.applicationScale !=
1940 mResources.getCompatibilityInfo().applicationScale)
1941 || activityToken != null)) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001942 if (DEBUG) {
1943 Log.d(TAG, "loaded context has different scaling. Using container's" +
1944 " compatiblity info:" + container.getDisplayMetrics());
1945 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001946 if (compatInfo == null) {
1947 compatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
1948 }
1949 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
1950 mDisplayAdjustments.setActivityToken(activityToken);
Craig Mautner88c05892013-06-28 09:47:45 -07001951 mResources = mResourcesManager.getTopLevelResources(mPackageInfo.getResDir(),
Craig Mautner48d0d182013-06-11 07:53:06 -07001952 Display.DEFAULT_DISPLAY, null, compatInfo, activityToken);
1953 } else {
1954 mDisplayAdjustments.setCompatibilityInfo(packageInfo.getCompatibilityInfo());
1955 mDisplayAdjustments.setActivityToken(activityToken);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 mMainThread = mainThread;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001958 mActivityToken = activityToken;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001959 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
1960 mUser = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962
Jeff Sharkey6d515712012-09-20 16:06:08 -07001963 final void init(Resources resources, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 mPackageInfo = null;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001965 mBasePackageName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 mResources = resources;
1967 mMainThread = mainThread;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001968 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
1969 mUser = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
1971
1972 final void scheduleFinalCleanup(String who, String what) {
1973 mMainThread.scheduleContextCleanup(this, who, what);
1974 }
1975
1976 final void performFinalCleanup(String who, String what) {
1977 //Log.i(TAG, "Cleanup up context: " + this);
1978 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1979 }
1980
1981 final Context getReceiverRestrictedContext() {
1982 if (mReceiverRestrictedContext != null) {
1983 return mReceiverRestrictedContext;
1984 }
1985 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1986 }
1987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 final void setOuterContext(Context context) {
1989 mOuterContext = context;
1990 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 final Context getOuterContext() {
1993 return mOuterContext;
1994 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 final IBinder getActivityToken() {
1997 return mActivityToken;
1998 }
1999
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002000 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 int extraPermissions) {
2002 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2003 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2004 |extraPermissions;
2005 if ((mode&MODE_WORLD_READABLE) != 0) {
2006 perms |= FileUtils.S_IROTH;
2007 }
2008 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2009 perms |= FileUtils.S_IWOTH;
2010 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002011 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2013 + ", perms=0x" + Integer.toHexString(perms));
2014 }
2015 FileUtils.setPermissions(name, perms, -1, -1);
2016 }
2017
Oscar Montemayora8529f62009-11-18 10:14:20 -08002018 private File validateFilePath(String name, boolean createDirectory) {
2019 File dir;
2020 File f;
2021
2022 if (name.charAt(0) == File.separatorChar) {
2023 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
2024 dir = new File(dirPath);
2025 name = name.substring(name.lastIndexOf(File.separatorChar));
2026 f = new File(dir, name);
2027 } else {
2028 dir = getDatabasesDir();
2029 f = makeFilename(dir, name);
2030 }
2031
2032 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
2033 FileUtils.setPermissions(dir.getPath(),
2034 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
2035 -1, -1);
2036 }
2037
2038 return f;
2039 }
2040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 private File makeFilename(File base, String name) {
2042 if (name.indexOf(File.separatorChar) < 0) {
2043 return new File(base, name);
2044 }
2045 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002046 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002047 }
2048
2049 // ----------------------------------------------------------------------
2050 // ----------------------------------------------------------------------
2051 // ----------------------------------------------------------------------
2052
2053 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002054 private final ActivityThread mMainThread;
2055 private final UserHandle mUser;
2056
2057 public ApplicationContentResolver(
2058 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002060 mMainThread = Preconditions.checkNotNull(mainThread);
2061 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
2063
2064 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002065 protected IContentProvider acquireProvider(Context context, String auth) {
2066 return mMainThread.acquireProvider(context, auth, mUser.getIdentifier(), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
2068
2069 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002070 protected IContentProvider acquireExistingProvider(Context context, String auth) {
2071 return mMainThread.acquireExistingProvider(context, auth, mUser.getIdentifier(), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002072 }
2073
2074 @Override
2075 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002076 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002078
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002079 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002080 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
2081 return mMainThread.acquireProvider(c, auth, mUser.getIdentifier(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002082 }
2083
2084 @Override
2085 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002086 return mMainThread.releaseProvider(icp, false);
2087 }
2088
2089 @Override
2090 public void unstableProviderDied(IContentProvider icp) {
2091 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094}