blob: 992d8b72b174e9c2d8d7e4b8db6719bed0806498 [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;
Jeff Brown98365d72012-08-19 20:30:52 -0700102import android.view.CompatibilityInfoHolder;
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
196 private final Object mSync = new Object();
197
198 private File mDatabasesDir;
199 private File mPreferencesDir;
200 private File mFilesDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 private File mCacheDir;
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800202 private File mObbDir;
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800203 private File mExternalFilesDir;
204 private File mExternalCacheDir;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 private static final String[] EMPTY_FILE_LIST = {};
207
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800208 /**
209 * Override this class when the system service constructor needs a
210 * ContextImpl. Else, use StaticServiceFetcher below.
211 */
212 /*package*/ static class ServiceFetcher {
213 int mContextCacheIndex = -1;
214
215 /**
216 * Main entrypoint; only override if you don't need caching.
217 */
218 public Object getService(ContextImpl ctx) {
219 ArrayList<Object> cache = ctx.mServiceCache;
220 Object service;
221 synchronized (cache) {
222 if (cache.size() == 0) {
223 // Initialize the cache vector on first access.
224 // At this point sNextPerContextServiceCacheIndex
225 // is the number of potential services that are
226 // cached per-Context.
227 for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
228 cache.add(null);
229 }
230 } else {
231 service = cache.get(mContextCacheIndex);
232 if (service != null) {
233 return service;
234 }
235 }
236 service = createService(ctx);
237 cache.set(mContextCacheIndex, service);
238 return service;
239 }
240 }
241
242 /**
243 * Override this to create a new per-Context instance of the
244 * service. getService() will handle locking and caching.
245 */
246 public Object createService(ContextImpl ctx) {
247 throw new RuntimeException("Not implemented");
248 }
249 }
250
251 /**
252 * Override this class for services to be cached process-wide.
253 */
254 abstract static class StaticServiceFetcher extends ServiceFetcher {
255 private Object mCachedInstance;
256
257 @Override
258 public final Object getService(ContextImpl unused) {
259 synchronized (StaticServiceFetcher.this) {
260 Object service = mCachedInstance;
261 if (service != null) {
262 return service;
263 }
264 return mCachedInstance = createStaticService();
265 }
266 }
267
268 public abstract Object createStaticService();
269 }
270
271 private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
272 new HashMap<String, ServiceFetcher>();
273
274 private static int sNextPerContextServiceCacheIndex = 0;
275 private static void registerService(String serviceName, ServiceFetcher fetcher) {
276 if (!(fetcher instanceof StaticServiceFetcher)) {
277 fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
278 }
279 SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
280 }
281
282 // This one's defined separately and given a variable name so it
283 // can be re-used by getWallpaperManager(), avoiding a HashMap
284 // lookup.
285 private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() {
286 public Object createService(ContextImpl ctx) {
287 return new WallpaperManager(ctx.getOuterContext(),
288 ctx.mMainThread.getHandler());
289 }};
290
291 static {
292 registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
293 public Object getService(ContextImpl ctx) {
294 return AccessibilityManager.getInstance(ctx);
295 }});
296
297 registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
298 public Object createService(ContextImpl ctx) {
299 IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
300 IAccountManager service = IAccountManager.Stub.asInterface(b);
301 return new AccountManager(ctx, service);
302 }});
303
304 registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
305 public Object createService(ContextImpl ctx) {
306 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
307 }});
308
309 registerService(ALARM_SERVICE, new StaticServiceFetcher() {
310 public Object createStaticService() {
311 IBinder b = ServiceManager.getService(ALARM_SERVICE);
312 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
313 return new AlarmManager(service);
314 }});
315
316 registerService(AUDIO_SERVICE, new ServiceFetcher() {
317 public Object createService(ContextImpl ctx) {
318 return new AudioManager(ctx);
319 }});
320
Dianne Hackbornb58b8f82012-06-11 15:08:39 -0700321 registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() {
322 public Object createService(ContextImpl ctx) {
323 return new MediaRouter(ctx);
324 }});
325
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800326 registerService(BLUETOOTH_SERVICE, new ServiceFetcher() {
327 public Object createService(ContextImpl ctx) {
Matthew Xieddf7e472013-03-01 18:41:02 -0800328 return new BluetoothManager(ctx);
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800329 }});
330
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800331 registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
332 public Object createService(ContextImpl ctx) {
333 return new ClipboardManager(ctx.getOuterContext(),
334 ctx.mMainThread.getHandler());
335 }});
336
337 registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
338 public Object createStaticService() {
339 IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
340 return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
341 }});
342
343 registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
344 public Object createStaticService() {
345 IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
346 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
347 }});
348
349 registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
350 public Object createService(ContextImpl ctx) {
351 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
352 }});
353
354 registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
355 public Object createService(ContextImpl ctx) {
356 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
357 }});
358
Nick Pellyd2507462010-12-13 12:22:34 -0800359 registerService(NFC_SERVICE, new ServiceFetcher() {
360 public Object createService(ContextImpl ctx) {
361 return new NfcManager(ctx);
362 }});
363
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800364 registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
365 public Object createStaticService() {
366 return createDropBoxManager();
367 }});
368
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700369 registerService(INPUT_SERVICE, new StaticServiceFetcher() {
370 public Object createStaticService() {
371 return InputManager.getInstance();
Jeff Brownac143512012-04-05 18:57:33 -0700372 }});
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700373
Jeff Brownbd6e1502012-08-28 03:27:37 -0700374 registerService(DISPLAY_SERVICE, new ServiceFetcher() {
375 @Override
376 public Object createService(ContextImpl ctx) {
377 return new DisplayManager(ctx.getOuterContext());
378 }});
Jeff Brownfa25bf52012-07-23 19:26:30 -0700379
Jeff Brownf9e989d2013-04-04 23:04:03 -0700380 registerService(INPUT_METHOD_SERVICE, new StaticServiceFetcher() {
381 public Object createStaticService() {
382 return InputMethodManager.getInstance();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800383 }});
384
satok988323c2011-06-22 16:38:13 +0900385 registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
386 public Object createService(ContextImpl ctx) {
387 return TextServicesManager.getInstance();
388 }});
389
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800390 registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
391 public Object getService(ContextImpl ctx) {
392 // TODO: why isn't this caching it? It wasn't
393 // before, so I'm preserving the old behavior and
394 // using getService(), instead of createService()
395 // which would do the caching.
396 return new KeyguardManager();
397 }});
398
399 registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
400 public Object createService(ContextImpl ctx) {
401 return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
402 }});
403
Nick Pellye0fd6932012-07-11 10:26:13 -0700404 registerService(LOCATION_SERVICE, new ServiceFetcher() {
405 public Object createService(ContextImpl ctx) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800406 IBinder b = ServiceManager.getService(LOCATION_SERVICE);
Nick Pellye0fd6932012-07-11 10:26:13 -0700407 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800408 }});
409
Jeff Sharkey1a303952011-06-16 13:04:20 -0700410 registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
411 @Override
412 public Object createService(ContextImpl ctx) {
413 return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
414 ServiceManager.getService(NETWORK_POLICY_SERVICE)));
415 }
416 });
417
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800418 registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
419 public Object createService(ContextImpl ctx) {
420 final Context outerContext = ctx.getOuterContext();
421 return new NotificationManager(
422 new ContextThemeWrapper(outerContext,
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800423 Resources.selectSystemTheme(0,
424 outerContext.getApplicationInfo().targetSdkVersion,
425 com.android.internal.R.style.Theme_Dialog,
Adam Powell6e90a362011-08-14 16:48:32 -0700426 com.android.internal.R.style.Theme_Holo_Dialog,
427 com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800428 ctx.mMainThread.getHandler());
429 }});
430
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700431 registerService(NSD_SERVICE, new ServiceFetcher() {
432 @Override
433 public Object createService(ContextImpl ctx) {
434 IBinder b = ServiceManager.getService(NSD_SERVICE);
435 INsdManager service = INsdManager.Stub.asInterface(b);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700436 return new NsdManager(ctx.getOuterContext(), service);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700437 }});
438
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800439 // Note: this was previously cached in a static variable, but
440 // constructed using mMainThread.getHandler(), so converting
441 // it to be a regular Context-cached service...
442 registerService(POWER_SERVICE, new ServiceFetcher() {
443 public Object createService(ContextImpl ctx) {
444 IBinder b = ServiceManager.getService(POWER_SERVICE);
445 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown96307042012-07-27 15:51:34 -0700446 return new PowerManager(ctx.getOuterContext(),
447 service, ctx.mMainThread.getHandler());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800448 }});
449
450 registerService(SEARCH_SERVICE, new ServiceFetcher() {
451 public Object createService(ContextImpl ctx) {
452 return new SearchManager(ctx.getOuterContext(),
453 ctx.mMainThread.getHandler());
454 }});
455
456 registerService(SENSOR_SERVICE, new ServiceFetcher() {
457 public Object createService(ContextImpl ctx) {
Jaikumar Ganesh6d0c1d72013-03-27 17:41:33 -0700458 return new SystemSensorManager(ctx.getOuterContext(),
459 ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800460 }});
461
462 registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
463 public Object createService(ContextImpl ctx) {
464 return new StatusBarManager(ctx.getOuterContext());
465 }});
466
467 registerService(STORAGE_SERVICE, new ServiceFetcher() {
468 public Object createService(ContextImpl ctx) {
469 try {
Jeff Sharkeybe722152013-02-15 16:56:38 -0800470 return new StorageManager(
471 ctx.getContentResolver(), ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800472 } catch (RemoteException rex) {
473 Log.e(TAG, "Failed to create StorageManager", rex);
474 return null;
475 }
476 }});
477
478 registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
479 public Object createService(ContextImpl ctx) {
480 return new TelephonyManager(ctx.getOuterContext());
481 }});
482
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800483 registerService(UI_MODE_SERVICE, new ServiceFetcher() {
484 public Object createService(ContextImpl ctx) {
485 return new UiModeManager();
486 }});
487
Mike Lockwood3a68b832011-03-08 10:08:59 -0500488 registerService(USB_SERVICE, new ServiceFetcher() {
489 public Object createService(ContextImpl ctx) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500490 IBinder b = ServiceManager.getService(USB_SERVICE);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500491 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500492 }});
493
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400494 registerService(SERIAL_SERVICE, new ServiceFetcher() {
495 public Object createService(ContextImpl ctx) {
496 IBinder b = ServiceManager.getService(SERIAL_SERVICE);
497 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
498 }});
499
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800500 registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
501 public Object createService(ContextImpl ctx) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800502 return new SystemVibrator(ctx);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800503 }});
504
505 registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
506
507 registerService(WIFI_SERVICE, new ServiceFetcher() {
508 public Object createService(ContextImpl ctx) {
509 IBinder b = ServiceManager.getService(WIFI_SERVICE);
510 IWifiManager service = IWifiManager.Stub.asInterface(b);
Irfan Sheriff88759bb2012-07-02 15:58:28 -0700511 return new WifiManager(ctx.getOuterContext(), service);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800512 }});
513
repo sync55bc5f32011-06-24 14:23:07 -0700514 registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
515 public Object createService(ContextImpl ctx) {
516 IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
517 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
518 return new WifiP2pManager(service);
519 }});
520
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800521 registerService(WINDOW_SERVICE, new ServiceFetcher() {
Romain Guye50848b2013-06-07 10:57:25 -0700522 Display mDefaultDisplay;
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800523 public Object getService(ContextImpl ctx) {
Jeff Browna492c3a2012-08-23 19:48:44 -0700524 Display display = ctx.mDisplay;
525 if (display == null) {
Romain Guye50848b2013-06-07 10:57:25 -0700526 if (mDefaultDisplay == null) {
527 DisplayManager dm = (DisplayManager)ctx.getOuterContext().
528 getSystemService(Context.DISPLAY_SERVICE);
529 mDefaultDisplay = dm.getDisplay(Display.DEFAULT_DISPLAY);
530 }
531 display = mDefaultDisplay;
Jeff Browna492c3a2012-08-23 19:48:44 -0700532 }
533 return new WindowManagerImpl(display);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800534 }});
Amith Yamasani258848d2012-08-10 17:06:33 -0700535
536 registerService(USER_SERVICE, new ServiceFetcher() {
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800537 public Object createService(ContextImpl ctx) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700538 IBinder b = ServiceManager.getService(USER_SERVICE);
539 IUserManager service = IUserManager.Stub.asInterface(b);
540 return new UserManager(ctx, service);
541 }});
Dianne Hackborna06de0f2012-12-11 16:34:47 -0800542
543 registerService(APP_OPS_SERVICE, new ServiceFetcher() {
544 public Object createService(ContextImpl ctx) {
545 IBinder b = ServiceManager.getService(APP_OPS_SERVICE);
546 IAppOpsService service = IAppOpsService.Stub.asInterface(b);
547 return new AppOpsManager(ctx, service);
548 }});
Eino-Ville Talvalab2675542012-12-12 13:29:45 -0800549
550 registerService(CAMERA_SERVICE, new StaticServiceFetcher() {
551 public Object createStaticService() {
552 return new CameraManager();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700553 }
554 });
555
556 registerService(PRINT_SERVICE, new ServiceFetcher() {
557 public Object createService(ContextImpl ctx) {
558 IBinder iBinder = ServiceManager.getService(Context.PRINT_SERVICE);
559 IPrintManager service = IPrintManager.Stub.asInterface(iBinder);
560 return new PrintManager(ctx.getOuterContext(), service, UserHandle.myUserId(),
561 UserHandle.getAppId(Process.myUid()));
Eino-Ville Talvalab2675542012-12-12 13:29:45 -0800562 }});
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800563 }
564
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700565 static ContextImpl getImpl(Context context) {
566 Context nextContext;
567 while ((context instanceof ContextWrapper) &&
568 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
569 context = nextContext;
570 }
571 return (ContextImpl)context;
572 }
573
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800574 // The system service cache for the system services that are
575 // cached per-ContextImpl. Package-scoped to avoid accessor
576 // methods.
577 final ArrayList<Object> mServiceCache = new ArrayList<Object>();
578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 @Override
580 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700581 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 }
583
584 @Override
585 public Resources getResources() {
586 return mResources;
587 }
588
589 @Override
590 public PackageManager getPackageManager() {
591 if (mPackageManager != null) {
592 return mPackageManager;
593 }
594
595 IPackageManager pm = ActivityThread.getPackageManager();
596 if (pm != null) {
597 // Doesn't matter if we make more than one instance.
598 return (mPackageManager = new ApplicationPackageManager(this, pm));
599 }
600
601 return null;
602 }
603
604 @Override
605 public ContentResolver getContentResolver() {
606 return mContentResolver;
607 }
608
609 @Override
610 public Looper getMainLooper() {
611 return mMainThread.getLooper();
612 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 @Override
615 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700616 return (mPackageInfo != null) ?
617 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 @Override
621 public void setTheme(int resid) {
622 mThemeResource = resid;
623 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800626 public int getThemeResId() {
627 return mThemeResource;
628 }
629
630 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public Resources.Theme getTheme() {
632 if (mTheme == null) {
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800633 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
634 getOuterContext().getApplicationInfo().targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 mTheme = mResources.newTheme();
636 mTheme.applyStyle(mThemeResource, true);
637 }
638 return mTheme;
639 }
640
641 @Override
642 public ClassLoader getClassLoader() {
643 return mPackageInfo != null ?
644 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
645 }
646
647 @Override
648 public String getPackageName() {
649 if (mPackageInfo != null) {
650 return mPackageInfo.getPackageName();
651 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800652 // No mPackageInfo means this is a Context for the system itself,
653 // and this here is its name.
654 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 }
656
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800657 /** @hide */
658 @Override
659 public String getBasePackageName() {
660 return mBasePackageName != null ? mBasePackageName : getPackageName();
661 }
662
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700664 public ApplicationInfo getApplicationInfo() {
665 if (mPackageInfo != null) {
666 return mPackageInfo.getApplicationInfo();
667 }
668 throw new RuntimeException("Not supported in system context");
669 }
670
671 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 public String getPackageResourcePath() {
673 if (mPackageInfo != null) {
674 return mPackageInfo.getResDir();
675 }
676 throw new RuntimeException("Not supported in system context");
677 }
678
679 @Override
680 public String getPackageCodePath() {
681 if (mPackageInfo != null) {
682 return mPackageInfo.getAppDir();
683 }
684 throw new RuntimeException("Not supported in system context");
685 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200686
Joe Onorato23ecae32009-06-10 17:07:15 -0700687 public File getSharedPrefsFile(String name) {
688 return makeFilename(getPreferencesDir(), name + ".xml");
689 }
690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 @Override
692 public SharedPreferences getSharedPreferences(String name, int mode) {
693 SharedPreferencesImpl sp;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700694 synchronized (mSync) {
695 if (sSharedPrefs == null) {
696 sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>();
697 }
698
699 final String packageName = getPackageName();
700 ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
701 if (packagePrefs == null) {
702 packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
703 sSharedPrefs.put(packageName, packagePrefs);
704 }
705
706 sp = packagePrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700707 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800708 File prefsFile = getSharedPrefsFile(name);
709 sp = new SharedPreferencesImpl(prefsFile, mode);
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700710 packagePrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700711 return sp;
712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800714 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
715 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
716 // If somebody else (some other process) changed the prefs
717 // file behind our back, we reload it. This has been the
718 // historical (if undocumented) behavior.
719 sp.startReloadIfChangedUnexpectedly();
720 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700721 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723
724 private File getPreferencesDir() {
725 synchronized (mSync) {
726 if (mPreferencesDir == null) {
727 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
728 }
729 return mPreferencesDir;
730 }
731 }
732
733 @Override
734 public FileInputStream openFileInput(String name)
735 throws FileNotFoundException {
736 File f = makeFilename(getFilesDir(), name);
737 return new FileInputStream(f);
738 }
739
740 @Override
741 public FileOutputStream openFileOutput(String name, int mode)
742 throws FileNotFoundException {
743 final boolean append = (mode&MODE_APPEND) != 0;
744 File f = makeFilename(getFilesDir(), name);
745 try {
746 FileOutputStream fos = new FileOutputStream(f, append);
747 setFilePermissionsFromMode(f.getPath(), mode, 0);
748 return fos;
749 } catch (FileNotFoundException e) {
750 }
751
752 File parent = f.getParentFile();
753 parent.mkdir();
754 FileUtils.setPermissions(
755 parent.getPath(),
756 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
757 -1, -1);
758 FileOutputStream fos = new FileOutputStream(f, append);
759 setFilePermissionsFromMode(f.getPath(), mode, 0);
760 return fos;
761 }
762
763 @Override
764 public boolean deleteFile(String name) {
765 File f = makeFilename(getFilesDir(), name);
766 return f.delete();
767 }
768
769 @Override
770 public File getFilesDir() {
771 synchronized (mSync) {
772 if (mFilesDir == null) {
773 mFilesDir = new File(getDataDirFile(), "files");
774 }
775 if (!mFilesDir.exists()) {
776 if(!mFilesDir.mkdirs()) {
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200777 Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 return null;
779 }
780 FileUtils.setPermissions(
781 mFilesDir.getPath(),
782 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
783 -1, -1);
784 }
785 return mFilesDir;
786 }
787 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 @Override
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800790 public File getExternalFilesDir(String type) {
791 synchronized (mSync) {
792 if (mExternalFilesDir == null) {
793 mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory(
794 getPackageName());
795 }
796 if (!mExternalFilesDir.exists()) {
797 try {
798 (new File(Environment.getExternalStorageAndroidDataDir(),
799 ".nomedia")).createNewFile();
800 } catch (IOException e) {
801 }
802 if (!mExternalFilesDir.mkdirs()) {
803 Log.w(TAG, "Unable to create external files directory");
804 return null;
805 }
806 }
807 if (type == null) {
808 return mExternalFilesDir;
809 }
810 File dir = new File(mExternalFilesDir, type);
811 if (!dir.exists()) {
812 if (!dir.mkdirs()) {
813 Log.w(TAG, "Unable to create external media directory " + dir);
814 return null;
815 }
816 }
817 return dir;
818 }
819 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200820
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800821 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800822 public File getObbDir() {
823 synchronized (mSync) {
824 if (mObbDir == null) {
825 mObbDir = Environment.getExternalStorageAppObbDirectory(
826 getPackageName());
827 }
828 return mObbDir;
829 }
830 }
831
832 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 public File getCacheDir() {
834 synchronized (mSync) {
835 if (mCacheDir == null) {
836 mCacheDir = new File(getDataDirFile(), "cache");
837 }
838 if (!mCacheDir.exists()) {
839 if(!mCacheDir.mkdirs()) {
Amith Yamasani92d57052012-08-23 10:07:52 -0700840 Log.w(TAG, "Unable to create cache directory " + mCacheDir.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 return null;
842 }
843 FileUtils.setPermissions(
844 mCacheDir.getPath(),
845 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
846 -1, -1);
847 }
848 }
849 return mCacheDir;
850 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200851
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800852 @Override
853 public File getExternalCacheDir() {
854 synchronized (mSync) {
855 if (mExternalCacheDir == null) {
856 mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory(
857 getPackageName());
858 }
859 if (!mExternalCacheDir.exists()) {
860 try {
861 (new File(Environment.getExternalStorageAndroidDataDir(),
862 ".nomedia")).createNewFile();
863 } catch (IOException e) {
864 }
865 if (!mExternalCacheDir.mkdirs()) {
866 Log.w(TAG, "Unable to create external cache directory");
867 return null;
868 }
869 }
870 return mExternalCacheDir;
871 }
872 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 @Override
875 public File getFileStreamPath(String name) {
876 return makeFilename(getFilesDir(), name);
877 }
878
879 @Override
880 public String[] fileList() {
881 final String[] list = getFilesDir().list();
882 return (list != null) ? list : EMPTY_FILE_LIST;
883 }
884
885 @Override
886 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700887 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889
890 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700891 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
892 DatabaseErrorHandler errorHandler) {
893 File f = validateFilePath(name, true);
Jeff Brown47847f32012-03-22 19:13:11 -0700894 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
895 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
896 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
897 }
898 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700899 setFilePermissionsFromMode(f.getPath(), mode, 0);
900 return db;
901 }
902
903 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 public boolean deleteDatabase(String name) {
905 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800906 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800907 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 } catch (Exception e) {
909 }
910 return false;
911 }
912
913 @Override
914 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800915 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
917
918 @Override
919 public String[] databaseList() {
920 final String[] list = getDatabasesDir().list();
921 return (list != null) ? list : EMPTY_FILE_LIST;
922 }
923
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 private File getDatabasesDir() {
926 synchronized (mSync) {
927 if (mDatabasesDir == null) {
928 mDatabasesDir = new File(getDataDirFile(), "databases");
929 }
930 if (mDatabasesDir.getPath().equals("databases")) {
931 mDatabasesDir = new File("/data/system");
932 }
933 return mDatabasesDir;
934 }
935 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 @Override
938 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700939 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941
942 @Override
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700943 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700944 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 }
946
947 @Override
948 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700949 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 }
951
952 @Override
953 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700954 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 }
956
957 @Override
958 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700959 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 }
961
962 @Override
963 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700964 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
966
967 @Override
968 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700969 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971
972 @Override
973 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700974 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700975 startActivity(intent, null);
976 }
977
Amith Yamasani82644082012-08-03 13:09:11 -0700978 /** @hide */
979 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700980 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700981 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700982 }
983
Dianne Hackborna4972e92012-03-14 10:38:05 -0700984 @Override
985 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700986 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
988 throw new AndroidRuntimeException(
989 "Calling startActivity() from outside of an Activity "
990 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
991 + " Is this really what you want?");
992 }
993 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700994 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700995 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997
Amith Yamasani258848d2012-08-10 17:06:33 -0700998 /** @hide */
999 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001000 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -07001001 try {
1002 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001003 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -07001004 intent.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001005 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options,
1006 user.getIdentifier());
Amith Yamasani258848d2012-08-10 17:06:33 -07001007 } catch (RemoteException re) {
1008 }
1009 }
1010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001012 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001013 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -07001014 startActivities(intents, null);
1015 }
1016
Amith Yamasaniea7e9152012-09-24 16:11:18 -07001017 /** @hide */
1018 @Override
1019 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
1020 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1021 throw new AndroidRuntimeException(
1022 "Calling startActivities() from outside of an Activity "
1023 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
1024 + " Is this really what you want?");
1025 }
1026 mMainThread.getInstrumentation().execStartActivitiesAsUser(
1027 getOuterContext(), mMainThread.getApplicationThread(), null,
1028 (Activity)null, intents, options, userHandle.getIdentifier());
1029 }
1030
Dianne Hackborna4972e92012-03-14 10:38:05 -07001031 @Override
1032 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001033 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001034 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
1035 throw new AndroidRuntimeException(
1036 "Calling startActivities() from outside of an Activity "
1037 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
1038 + " Is this really what you want?");
1039 }
1040 mMainThread.getInstrumentation().execStartActivities(
1041 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001042 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08001043 }
1044
1045 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001046 public void startIntentSender(IntentSender intent,
1047 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
1048 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -07001049 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
1050 }
1051
1052 @Override
1053 public void startIntentSender(IntentSender intent, Intent fillInIntent,
1054 int flagsMask, int flagsValues, int extraFlags, Bundle options)
1055 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001056 try {
1057 String resolvedType = null;
1058 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001059 fillInIntent.migrateExtraStreamToClipData();
1060 fillInIntent.prepareToLeaveProcess();
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001061 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
1062 }
1063 int result = ActivityManagerNative.getDefault()
1064 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
1065 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001066 0, flagsMask, flagsValues, options);
1067 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001068 throw new IntentSender.SendIntentException();
1069 }
1070 Instrumentation.checkStartActivityResult(result, null);
1071 } catch (RemoteException e) {
1072 }
1073 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001074
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001075 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001077 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1079 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001080 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 ActivityManagerNative.getDefault().broadcastIntent(
1082 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001083 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001084 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 } catch (RemoteException e) {
1086 }
1087 }
1088
Amith Yamasani67cf7d32012-02-16 14:31:23 -08001089 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001091 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1093 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001094 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 ActivityManagerNative.getDefault().broadcastIntent(
1096 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001097 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE,
1098 false, false, getUserId());
1099 } catch (RemoteException e) {
1100 }
1101 }
1102
1103 @Override
1104 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
1105 warnIfCallingFromSystemProcess();
1106 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1107 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001108 intent.prepareToLeaveProcess();
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001109 ActivityManagerNative.getDefault().broadcastIntent(
1110 mMainThread.getApplicationThread(), intent, resolvedType, null,
1111 Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001112 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 } catch (RemoteException e) {
1114 }
1115 }
1116
1117 @Override
1118 public void sendOrderedBroadcast(Intent intent,
1119 String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001120 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1122 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001123 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 ActivityManagerNative.getDefault().broadcastIntent(
1125 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001126 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, true, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001127 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 } catch (RemoteException e) {
1129 }
1130 }
1131
1132 @Override
1133 public void sendOrderedBroadcast(Intent intent,
1134 String receiverPermission, BroadcastReceiver resultReceiver,
1135 Handler scheduler, int initialCode, String initialData,
1136 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001137 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
1138 resultReceiver, scheduler, initialCode, initialData, initialExtras);
1139 }
1140
1141 @Override
1142 public void sendOrderedBroadcast(Intent intent,
1143 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1144 Handler scheduler, int initialCode, String initialData,
1145 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001146 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 IIntentReceiver rd = null;
1148 if (resultReceiver != null) {
1149 if (mPackageInfo != null) {
1150 if (scheduler == null) {
1151 scheduler = mMainThread.getHandler();
1152 }
1153 rd = mPackageInfo.getReceiverDispatcher(
1154 resultReceiver, getOuterContext(), scheduler,
1155 mMainThread.getInstrumentation(), false);
1156 } else {
1157 if (scheduler == null) {
1158 scheduler = mMainThread.getHandler();
1159 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001160 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1162 }
1163 }
1164 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1165 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001166 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 ActivityManagerNative.getDefault().broadcastIntent(
1168 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001169 initialCode, initialData, initialExtras, receiverPermission, appOp,
1170 true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 } catch (RemoteException e) {
1172 }
1173 }
1174
1175 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001176 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001177 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1178 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001179 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001180 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001181 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
1182 AppOpsManager.OP_NONE, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001183 } catch (RemoteException e) {
1184 }
1185 }
1186
1187 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001188 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1189 String receiverPermission) {
1190 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1191 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001192 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001193 ActivityManagerNative.getDefault().broadcastIntent(
1194 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001195 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, false, false,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001196 user.getIdentifier());
1197 } catch (RemoteException e) {
1198 }
1199 }
1200
1201 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001202 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001203 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001204 int initialCode, String initialData, Bundle initialExtras) {
1205 IIntentReceiver rd = null;
1206 if (resultReceiver != null) {
1207 if (mPackageInfo != null) {
1208 if (scheduler == null) {
1209 scheduler = mMainThread.getHandler();
1210 }
1211 rd = mPackageInfo.getReceiverDispatcher(
1212 resultReceiver, getOuterContext(), scheduler,
1213 mMainThread.getInstrumentation(), false);
1214 } else {
1215 if (scheduler == null) {
1216 scheduler = mMainThread.getHandler();
1217 }
1218 rd = new LoadedApk.ReceiverDispatcher(
1219 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1220 }
1221 }
1222 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1223 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001224 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001225 ActivityManagerNative.getDefault().broadcastIntent(
1226 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001227 initialCode, initialData, initialExtras, receiverPermission,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001228 AppOpsManager.OP_NONE, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001229 } catch (RemoteException e) {
1230 }
1231 }
1232
1233 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001235 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1237 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001238 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 ActivityManagerNative.getDefault().broadcastIntent(
1240 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001241 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001242 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 } catch (RemoteException e) {
1244 }
1245 }
1246
1247 @Override
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001248 public void sendStickyOrderedBroadcast(Intent intent,
1249 BroadcastReceiver resultReceiver,
1250 Handler scheduler, int initialCode, String initialData,
1251 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001252 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001253 IIntentReceiver rd = null;
1254 if (resultReceiver != null) {
1255 if (mPackageInfo != null) {
1256 if (scheduler == null) {
1257 scheduler = mMainThread.getHandler();
1258 }
1259 rd = mPackageInfo.getReceiverDispatcher(
1260 resultReceiver, getOuterContext(), scheduler,
1261 mMainThread.getInstrumentation(), false);
1262 } else {
1263 if (scheduler == null) {
1264 scheduler = mMainThread.getHandler();
1265 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001266 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001267 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1268 }
1269 }
1270 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1271 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001272 intent.prepareToLeaveProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001273 ActivityManagerNative.getDefault().broadcastIntent(
1274 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1275 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001276 AppOpsManager.OP_NONE, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001277 } catch (RemoteException e) {
1278 }
1279 }
1280
1281 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 public void removeStickyBroadcast(Intent intent) {
1283 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1284 if (resolvedType != null) {
1285 intent = new Intent(intent);
1286 intent.setDataAndType(intent.getData(), resolvedType);
1287 }
1288 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001289 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001291 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 } catch (RemoteException e) {
1293 }
1294 }
1295
1296 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001297 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1298 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1299 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001300 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001301 ActivityManagerNative.getDefault().broadcastIntent(
1302 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001303 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001304 } catch (RemoteException e) {
1305 }
1306 }
1307
1308 @Override
1309 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1310 UserHandle user, BroadcastReceiver resultReceiver,
1311 Handler scheduler, int initialCode, String initialData,
1312 Bundle initialExtras) {
1313 IIntentReceiver rd = null;
1314 if (resultReceiver != null) {
1315 if (mPackageInfo != null) {
1316 if (scheduler == null) {
1317 scheduler = mMainThread.getHandler();
1318 }
1319 rd = mPackageInfo.getReceiverDispatcher(
1320 resultReceiver, getOuterContext(), scheduler,
1321 mMainThread.getInstrumentation(), false);
1322 } else {
1323 if (scheduler == null) {
1324 scheduler = mMainThread.getHandler();
1325 }
1326 rd = new LoadedApk.ReceiverDispatcher(
1327 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1328 }
1329 }
1330 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1331 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001332 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001333 ActivityManagerNative.getDefault().broadcastIntent(
1334 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1335 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001336 AppOpsManager.OP_NONE, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001337 } catch (RemoteException e) {
1338 }
1339 }
1340
1341 @Override
1342 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1343 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1344 if (resolvedType != null) {
1345 intent = new Intent(intent);
1346 intent.setDataAndType(intent.getData(), resolvedType);
1347 }
1348 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001349 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001350 ActivityManagerNative.getDefault().unbroadcastIntent(
1351 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1352 } catch (RemoteException e) {
1353 }
1354 }
1355
1356 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1358 return registerReceiver(receiver, filter, null, null);
1359 }
1360
1361 @Override
1362 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1363 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001364 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001365 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
1367
Dianne Hackborn20e80982012-08-31 19:00:44 -07001368 @Override
1369 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1370 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1371 return registerReceiverInternal(receiver, user.getIdentifier(),
1372 filter, broadcastPermission, scheduler, getOuterContext());
1373 }
1374
1375 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 IntentFilter filter, String broadcastPermission,
1377 Handler scheduler, Context context) {
1378 IIntentReceiver rd = null;
1379 if (receiver != null) {
1380 if (mPackageInfo != null && context != null) {
1381 if (scheduler == null) {
1382 scheduler = mMainThread.getHandler();
1383 }
1384 rd = mPackageInfo.getReceiverDispatcher(
1385 receiver, context, scheduler,
1386 mMainThread.getInstrumentation(), true);
1387 } else {
1388 if (scheduler == null) {
1389 scheduler = mMainThread.getHandler();
1390 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001391 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001392 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394 }
1395 try {
1396 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001397 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001398 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 } catch (RemoteException e) {
1400 return null;
1401 }
1402 }
1403
1404 @Override
1405 public void unregisterReceiver(BroadcastReceiver receiver) {
1406 if (mPackageInfo != null) {
1407 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1408 getOuterContext(), receiver);
1409 try {
1410 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1411 } catch (RemoteException e) {
1412 }
1413 } else {
1414 throw new RuntimeException("Not supported in system context");
1415 }
1416 }
1417
1418 @Override
1419 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001420 warnIfCallingFromSystemProcess();
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001421 return startServiceAsUser(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001422 }
1423
1424 @Override
1425 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001426 warnIfCallingFromSystemProcess();
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001427 return stopServiceAsUser(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001428 }
1429
1430 @Override
1431 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001433 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 ComponentName cn = ActivityManagerNative.getDefault().startService(
1435 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001436 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001437 if (cn != null) {
1438 if (cn.getPackageName().equals("!")) {
1439 throw new SecurityException(
1440 "Not allowed to start service " + service
1441 + " without permission " + cn.getClassName());
1442 } else if (cn.getPackageName().equals("!!")) {
1443 throw new SecurityException(
1444 "Unable to start service " + service
1445 + ": " + cn.getClassName());
1446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 }
1448 return cn;
1449 } catch (RemoteException e) {
1450 return null;
1451 }
1452 }
1453
1454 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001455 public boolean stopServiceAsUser(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001457 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 int res = ActivityManagerNative.getDefault().stopService(
1459 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001460 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 if (res < 0) {
1462 throw new SecurityException(
1463 "Not allowed to stop service " + service);
1464 }
1465 return res != 0;
1466 } catch (RemoteException e) {
1467 return false;
1468 }
1469 }
1470
1471 @Override
1472 public boolean bindService(Intent service, ServiceConnection conn,
1473 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001474 warnIfCallingFromSystemProcess();
Amith Yamasani27b89e62013-01-16 12:30:11 -08001475 return bindServiceAsUser(service, conn, flags, Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001476 }
1477
1478 /** @hide */
1479 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001480 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1481 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001483 if (conn == null) {
1484 throw new IllegalArgumentException("connection is null");
1485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 if (mPackageInfo != null) {
1487 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1488 mMainThread.getHandler(), flags);
1489 } else {
1490 throw new RuntimeException("Not supported in system context");
1491 }
1492 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001493 IBinder token = getActivityToken();
1494 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1495 && mPackageInfo.getApplicationInfo().targetSdkVersion
1496 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1497 flags |= BIND_WAIVE_PRIORITY;
1498 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001499 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 int res = ActivityManagerNative.getDefault().bindService(
1501 mMainThread.getApplicationThread(), getActivityToken(),
1502 service, service.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasani27b89e62013-01-16 12:30:11 -08001503 sd, flags, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 if (res < 0) {
1505 throw new SecurityException(
1506 "Not allowed to bind to service " + service);
1507 }
1508 return res != 0;
1509 } catch (RemoteException e) {
1510 return false;
1511 }
1512 }
1513
1514 @Override
1515 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001516 if (conn == null) {
1517 throw new IllegalArgumentException("connection is null");
1518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 if (mPackageInfo != null) {
1520 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1521 getOuterContext(), conn);
1522 try {
1523 ActivityManagerNative.getDefault().unbindService(sd);
1524 } catch (RemoteException e) {
1525 }
1526 } else {
1527 throw new RuntimeException("Not supported in system context");
1528 }
1529 }
1530
1531 @Override
1532 public boolean startInstrumentation(ComponentName className,
1533 String profileFile, Bundle arguments) {
1534 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001535 if (arguments != null) {
1536 arguments.setAllowFds(false);
1537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 return ActivityManagerNative.getDefault().startInstrumentation(
Svetoslav Ganov80943d82013-01-02 10:25:37 -08001539 className, profileFile, 0, arguments, null, null, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 } catch (RemoteException e) {
1541 // System has crashed, nothing we can do.
1542 }
1543 return false;
1544 }
1545
1546 @Override
1547 public Object getSystemService(String name) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001548 ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
1549 return fetcher == null ? null : fetcher.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
1551
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001552 private WallpaperManager getWallpaperManager() {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001553 return (WallpaperManager) WALLPAPER_FETCHER.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 }
1555
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001556 /* package */ static DropBoxManager createDropBoxManager() {
1557 IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
1558 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001559 if (service == null) {
1560 // Don't return a DropBoxManager that will NPE upon use.
1561 // This also avoids caching a broken DropBoxManager in
1562 // getDropBoxManager during early boot, before the
1563 // DROPBOX_SERVICE is registered.
1564 return null;
1565 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001566 return new DropBoxManager(service);
1567 }
1568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 @Override
1570 public int checkPermission(String permission, int pid, int uid) {
1571 if (permission == null) {
1572 throw new IllegalArgumentException("permission is null");
1573 }
1574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 try {
1576 return ActivityManagerNative.getDefault().checkPermission(
1577 permission, pid, uid);
1578 } catch (RemoteException e) {
1579 return PackageManager.PERMISSION_DENIED;
1580 }
1581 }
1582
1583 @Override
1584 public int checkCallingPermission(String permission) {
1585 if (permission == null) {
1586 throw new IllegalArgumentException("permission is null");
1587 }
1588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 int pid = Binder.getCallingPid();
1590 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001591 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
1593 return PackageManager.PERMISSION_DENIED;
1594 }
1595
1596 @Override
1597 public int checkCallingOrSelfPermission(String permission) {
1598 if (permission == null) {
1599 throw new IllegalArgumentException("permission is null");
1600 }
1601
1602 return checkPermission(permission, Binder.getCallingPid(),
1603 Binder.getCallingUid());
1604 }
1605
1606 private void enforce(
1607 String permission, int resultOfCheck,
1608 boolean selfToo, int uid, String message) {
1609 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1610 throw new SecurityException(
1611 (message != null ? (message + ": ") : "") +
1612 (selfToo
1613 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001614 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 permission +
1616 ".");
1617 }
1618 }
1619
1620 public void enforcePermission(
1621 String permission, int pid, int uid, String message) {
1622 enforce(permission,
1623 checkPermission(permission, pid, uid),
1624 false,
1625 uid,
1626 message);
1627 }
1628
1629 public void enforceCallingPermission(String permission, String message) {
1630 enforce(permission,
1631 checkCallingPermission(permission),
1632 false,
1633 Binder.getCallingUid(),
1634 message);
1635 }
1636
1637 public void enforceCallingOrSelfPermission(
1638 String permission, String message) {
1639 enforce(permission,
1640 checkCallingOrSelfPermission(permission),
1641 true,
1642 Binder.getCallingUid(),
1643 message);
1644 }
1645
1646 @Override
1647 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1648 try {
1649 ActivityManagerNative.getDefault().grantUriPermission(
1650 mMainThread.getApplicationThread(), toPackage, uri,
1651 modeFlags);
1652 } catch (RemoteException e) {
1653 }
1654 }
1655
1656 @Override
1657 public void revokeUriPermission(Uri uri, int modeFlags) {
1658 try {
1659 ActivityManagerNative.getDefault().revokeUriPermission(
1660 mMainThread.getApplicationThread(), uri,
1661 modeFlags);
1662 } catch (RemoteException e) {
1663 }
1664 }
1665
1666 @Override
1667 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 try {
1669 return ActivityManagerNative.getDefault().checkUriPermission(
1670 uri, pid, uid, modeFlags);
1671 } catch (RemoteException e) {
1672 return PackageManager.PERMISSION_DENIED;
1673 }
1674 }
1675
1676 @Override
1677 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 int pid = Binder.getCallingPid();
1679 if (pid != Process.myPid()) {
1680 return checkUriPermission(uri, pid,
1681 Binder.getCallingUid(), modeFlags);
1682 }
1683 return PackageManager.PERMISSION_DENIED;
1684 }
1685
1686 @Override
1687 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1688 return checkUriPermission(uri, Binder.getCallingPid(),
1689 Binder.getCallingUid(), modeFlags);
1690 }
1691
1692 @Override
1693 public int checkUriPermission(Uri uri, String readPermission,
1694 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001695 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1697 + readPermission + " writePermission=" + writePermission
1698 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1699 }
1700 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1701 if (readPermission == null
1702 || checkPermission(readPermission, pid, uid)
1703 == PackageManager.PERMISSION_GRANTED) {
1704 return PackageManager.PERMISSION_GRANTED;
1705 }
1706 }
1707 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1708 if (writePermission == null
1709 || checkPermission(writePermission, pid, uid)
1710 == PackageManager.PERMISSION_GRANTED) {
1711 return PackageManager.PERMISSION_GRANTED;
1712 }
1713 }
1714 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1715 : PackageManager.PERMISSION_DENIED;
1716 }
1717
1718 private String uriModeFlagToString(int uriModeFlags) {
1719 switch (uriModeFlags) {
1720 case Intent.FLAG_GRANT_READ_URI_PERMISSION |
1721 Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1722 return "read and write";
1723 case Intent.FLAG_GRANT_READ_URI_PERMISSION:
1724 return "read";
1725 case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1726 return "write";
1727 }
1728 throw new IllegalArgumentException(
1729 "Unknown permission mode flags: " + uriModeFlags);
1730 }
1731
1732 private void enforceForUri(
1733 int modeFlags, int resultOfCheck, boolean selfToo,
1734 int uid, Uri uri, String message) {
1735 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1736 throw new SecurityException(
1737 (message != null ? (message + ": ") : "") +
1738 (selfToo
1739 ? "Neither user " + uid + " nor current process has "
1740 : "User " + uid + " does not have ") +
1741 uriModeFlagToString(modeFlags) +
1742 " permission on " +
1743 uri +
1744 ".");
1745 }
1746 }
1747
1748 public void enforceUriPermission(
1749 Uri uri, int pid, int uid, int modeFlags, String message) {
1750 enforceForUri(
1751 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1752 false, uid, uri, message);
1753 }
1754
1755 public void enforceCallingUriPermission(
1756 Uri uri, int modeFlags, String message) {
1757 enforceForUri(
1758 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001759 false,
1760 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 }
1762
1763 public void enforceCallingOrSelfUriPermission(
1764 Uri uri, int modeFlags, String message) {
1765 enforceForUri(
1766 modeFlags,
1767 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1768 Binder.getCallingUid(), uri, message);
1769 }
1770
1771 public void enforceUriPermission(
1772 Uri uri, String readPermission, String writePermission,
1773 int pid, int uid, int modeFlags, String message) {
1774 enforceForUri(modeFlags,
1775 checkUriPermission(
1776 uri, readPermission, writePermission, pid, uid,
1777 modeFlags),
1778 false,
1779 uid,
1780 uri,
1781 message);
1782 }
1783
Amith Yamasanicd757062012-10-19 18:23:52 -07001784 private void warnIfCallingFromSystemProcess() {
1785 if (Process.myUid() == Process.SYSTEM_UID) {
1786 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001787 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001788 }
1789 }
1790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 @Override
1792 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001793 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001794 return createPackageContextAsUser(packageName, flags,
1795 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001796 }
1797
1798 @Override
1799 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1800 throws NameNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001802 final ContextImpl context = new ContextImpl(mMainThread.getSystemContext());
Adam Powell6f2a3d22012-10-05 22:32:04 -07001803 context.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
1804 context.init(mPackageInfo, null, mMainThread, mResources, mBasePackageName, user);
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001805 return context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 }
1807
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001808 LoadedApk pi =
Amith Yamasani98edc952012-09-25 14:09:27 -07001809 mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags,
1810 user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 if (pi != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001812 ContextImpl c = new ContextImpl();
Romain Guy870e09f2009-07-06 16:35:25 -07001813 c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001814 c.init(pi, null, mMainThread, mResources, mBasePackageName, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 if (c.mResources != null) {
1816 return c;
1817 }
1818 }
1819
1820 // Should be a better exception.
1821 throw new PackageManager.NameNotFoundException(
1822 "Application package " + packageName + " not found");
1823 }
1824
Romain Guy870e09f2009-07-06 16:35:25 -07001825 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001826 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001827 if (overrideConfiguration == null) {
1828 throw new IllegalArgumentException("overrideConfiguration must not be null");
1829 }
1830
Dianne Hackborn756220b2012-08-14 16:45:30 -07001831 ContextImpl c = new ContextImpl();
1832 c.init(mPackageInfo, null, mMainThread);
1833 c.mResources = mMainThread.getTopLevelResources(
Jeff Browna492c3a2012-08-23 19:48:44 -07001834 mPackageInfo.getResDir(),
1835 getDisplayId(), overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001836 mResources.getCompatibilityInfo());
1837 return c;
1838 }
1839
1840 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001841 public Context createDisplayContext(Display display) {
1842 if (display == null) {
1843 throw new IllegalArgumentException("display must not be null");
1844 }
1845
1846 int displayId = display.getDisplayId();
1847 CompatibilityInfo ci = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
1848 CompatibilityInfoHolder cih = getCompatibilityInfo(displayId);
1849 if (cih != null) {
1850 ci = cih.get();
1851 }
1852
1853 ContextImpl context = new ContextImpl();
1854 context.init(mPackageInfo, null, mMainThread);
1855 context.mDisplay = display;
1856 context.mResources = mMainThread.getTopLevelResources(
1857 mPackageInfo.getResDir(), displayId, null, ci);
1858 return context;
1859 }
1860
1861 private int getDisplayId() {
1862 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1863 }
1864
1865 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001866 public boolean isRestricted() {
1867 return mRestricted;
1868 }
1869
Jeff Brown98365d72012-08-19 20:30:52 -07001870 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001871 public CompatibilityInfoHolder getCompatibilityInfo(int displayId) {
1872 return displayId == Display.DEFAULT_DISPLAY ? mPackageInfo.mCompatibilityInfo : null;
Jeff Brown98365d72012-08-19 20:30:52 -07001873 }
1874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 private File getDataDirFile() {
1876 if (mPackageInfo != null) {
1877 return mPackageInfo.getDataDirFile();
1878 }
1879 throw new RuntimeException("Not supported in system context");
1880 }
1881
1882 @Override
1883 public File getDir(String name, int mode) {
1884 name = "app_" + name;
1885 File file = makeFilename(getDataDirFile(), name);
1886 if (!file.exists()) {
1887 file.mkdir();
1888 setFilePermissionsFromMode(file.getPath(), mode,
1889 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1890 }
1891 return file;
1892 }
1893
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001894 /** {@hide} */
1895 public int getUserId() {
1896 return mUser.getIdentifier();
1897 }
1898
Dianne Hackborn21556372010-02-04 16:34:40 -08001899 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001900 final ContextImpl context = new ContextImpl();
1901 context.init(Resources.getSystem(), mainThread, Process.myUserHandle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001902 return context;
1903 }
1904
Dianne Hackborn21556372010-02-04 16:34:40 -08001905 ContextImpl() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 mOuterContext = this;
1907 }
1908
1909 /**
1910 * Create a new ApplicationContext from an existing one. The new one
1911 * works and operates the same as the one it is copying.
1912 *
1913 * @param context Existing application context.
1914 */
Dianne Hackborn21556372010-02-04 16:34:40 -08001915 public ContextImpl(ContextImpl context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 mPackageInfo = context.mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001917 mBasePackageName = context.mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 mResources = context.mResources;
1919 mMainThread = context.mMainThread;
1920 mContentResolver = context.mContentResolver;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001921 mUser = context.mUser;
Jeff Browna492c3a2012-08-23 19:48:44 -07001922 mDisplay = context.mDisplay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 mOuterContext = this;
1924 }
1925
Jeff Sharkey6d515712012-09-20 16:06:08 -07001926 final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread) {
1927 init(packageInfo, activityToken, mainThread, null, null, Process.myUserHandle());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001928 }
1929
Jeff Sharkey6d515712012-09-20 16:06:08 -07001930 final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread,
1931 Resources container, String basePackageName, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 mPackageInfo = packageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001933 mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 mResources = mPackageInfo.getResources(mainThread);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001935
Dianne Hackborn559a7872010-04-07 18:19:41 -07001936 if (mResources != null && container != null
1937 && container.getCompatibilityInfo().applicationScale !=
1938 mResources.getCompatibilityInfo().applicationScale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001939 if (DEBUG) {
1940 Log.d(TAG, "loaded context has different scaling. Using container's" +
1941 " compatiblity info:" + container.getDisplayMetrics());
1942 }
1943 mResources = mainThread.getTopLevelResources(
Jeff Browna492c3a2012-08-23 19:48:44 -07001944 mPackageInfo.getResDir(), Display.DEFAULT_DISPLAY,
1945 null, container.getCompatibilityInfo());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 mMainThread = mainThread;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001948 mActivityToken = activityToken;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001949 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
1950 mUser = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952
Jeff Sharkey6d515712012-09-20 16:06:08 -07001953 final void init(Resources resources, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 mPackageInfo = null;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001955 mBasePackageName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 mResources = resources;
1957 mMainThread = mainThread;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001958 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
1959 mUser = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 }
1961
1962 final void scheduleFinalCleanup(String who, String what) {
1963 mMainThread.scheduleContextCleanup(this, who, what);
1964 }
1965
1966 final void performFinalCleanup(String who, String what) {
1967 //Log.i(TAG, "Cleanup up context: " + this);
1968 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1969 }
1970
1971 final Context getReceiverRestrictedContext() {
1972 if (mReceiverRestrictedContext != null) {
1973 return mReceiverRestrictedContext;
1974 }
1975 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1976 }
1977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 final void setOuterContext(Context context) {
1979 mOuterContext = context;
1980 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 final Context getOuterContext() {
1983 return mOuterContext;
1984 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 final IBinder getActivityToken() {
1987 return mActivityToken;
1988 }
1989
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08001990 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 int extraPermissions) {
1992 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
1993 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
1994 |extraPermissions;
1995 if ((mode&MODE_WORLD_READABLE) != 0) {
1996 perms |= FileUtils.S_IROTH;
1997 }
1998 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
1999 perms |= FileUtils.S_IWOTH;
2000 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002001 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2003 + ", perms=0x" + Integer.toHexString(perms));
2004 }
2005 FileUtils.setPermissions(name, perms, -1, -1);
2006 }
2007
Oscar Montemayora8529f62009-11-18 10:14:20 -08002008 private File validateFilePath(String name, boolean createDirectory) {
2009 File dir;
2010 File f;
2011
2012 if (name.charAt(0) == File.separatorChar) {
2013 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
2014 dir = new File(dirPath);
2015 name = name.substring(name.lastIndexOf(File.separatorChar));
2016 f = new File(dir, name);
2017 } else {
2018 dir = getDatabasesDir();
2019 f = makeFilename(dir, name);
2020 }
2021
2022 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
2023 FileUtils.setPermissions(dir.getPath(),
2024 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
2025 -1, -1);
2026 }
2027
2028 return f;
2029 }
2030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 private File makeFilename(File base, String name) {
2032 if (name.indexOf(File.separatorChar) < 0) {
2033 return new File(base, name);
2034 }
2035 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002036 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
2038
2039 // ----------------------------------------------------------------------
2040 // ----------------------------------------------------------------------
2041 // ----------------------------------------------------------------------
2042
2043 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002044 private final ActivityThread mMainThread;
2045 private final UserHandle mUser;
2046
2047 public ApplicationContentResolver(
2048 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002050 mMainThread = Preconditions.checkNotNull(mainThread);
2051 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053
2054 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002055 protected IContentProvider acquireProvider(Context context, String auth) {
2056 return mMainThread.acquireProvider(context, auth, mUser.getIdentifier(), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 }
2058
2059 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002060 protected IContentProvider acquireExistingProvider(Context context, String auth) {
2061 return mMainThread.acquireExistingProvider(context, auth, mUser.getIdentifier(), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002062 }
2063
2064 @Override
2065 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002066 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002068
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002069 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002070 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
2071 return mMainThread.acquireProvider(c, auth, mUser.getIdentifier(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002072 }
2073
2074 @Override
2075 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002076 return mMainThread.releaseProvider(icp, false);
2077 }
2078
2079 @Override
2080 public void unstableProviderDied(IContentProvider icp) {
2081 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084}