blob: 3e1e35886d8ed3285f83a0c15d13cf86d811c036 [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
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -080022import android.bluetooth.BluetoothAdapter;
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.hardware.SensorManager;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040051import android.hardware.SerialManager;
Jeff Brown25157e42012-04-16 12:13:05 -070052import android.hardware.SystemSensorManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070053import android.hardware.display.DisplayManager;
Jeff Brownac143512012-04-05 18:57:33 -070054import android.hardware.input.IInputManager;
Jeff Brown9df6e7a2012-04-05 11:49:26 -070055import android.hardware.input.InputManager;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080056import android.hardware.usb.IUsbManager;
57import android.hardware.usb.UsbManager;
Bai Taoa58a8752010-07-13 15:32:16 +080058import android.location.CountryDetector;
59import android.location.ICountryDetector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.location.ILocationManager;
61import android.location.LocationManager;
62import android.media.AudioManager;
Dianne Hackbornb58b8f82012-06-11 15:08:39 -070063import android.media.MediaRouter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.net.ConnectivityManager;
65import android.net.IConnectivityManager;
Jeff Sharkey1a303952011-06-16 13:04:20 -070066import android.net.INetworkPolicyManager;
67import android.net.NetworkPolicyManager;
Irfan Sheriffc9b68512010-04-08 14:12:33 -070068import android.net.ThrottleManager;
69import android.net.IThrottleManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.net.Uri;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070071import android.net.nsd.INsdManager;
72import android.net.nsd.NsdManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.net.wifi.IWifiManager;
74import android.net.wifi.WifiManager;
repo sync55bc5f32011-06-24 14:23:07 -070075import android.net.wifi.p2p.IWifiP2pManager;
76import android.net.wifi.p2p.WifiP2pManager;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080077import android.nfc.NfcManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Binder;
79import android.os.Bundle;
Dan Egnorf18a01c2009-11-12 11:32:50 -080080import android.os.DropBoxManager;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080081import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.FileUtils;
83import android.os.Handler;
84import android.os.IBinder;
85import android.os.IPowerManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070086import android.os.IUserManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.os.PowerManager;
89import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070090import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070092import android.os.UserHandle;
Jeff Brownc2346132012-04-13 01:55:38 -070093import android.os.SystemVibrator;
Amith Yamasani258848d2012-08-10 17:06:33 -070094import android.os.UserManager;
San Mehatb1043402010-02-05 08:26:50 -080095import android.os.storage.StorageManager;
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;
99import android.util.Log;
Jeff Brown98365d72012-08-19 20:30:52 -0700100import android.view.CompatibilityInfoHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.ContextThemeWrapper;
Jeff Brown98365d72012-08-19 20:30:52 -0700102import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import android.view.WindowManagerImpl;
svetoslavganov75986cf2009-05-14 22:28:01 -0700104import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.inputmethod.InputMethodManager;
satok988323c2011-06-22 16:38:13 +0900106import android.view.textservice.TextServicesManager;
Fred Quintana60307342009-03-24 22:48:12 -0700107import android.accounts.AccountManager;
108import android.accounts.IAccountManager;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800109import android.app.admin.DevicePolicyManager;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800110import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -0700111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.io.File;
113import java.io.FileInputStream;
114import java.io.FileNotFoundException;
115import java.io.FileOutputStream;
116import java.io.IOException;
117import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import java.util.ArrayList;
119import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121class ReceiverRestrictedContext extends ContextWrapper {
122 ReceiverRestrictedContext(Context base) {
123 super(base);
124 }
125
126 @Override
127 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
128 return registerReceiver(receiver, filter, null, null);
129 }
130
131 @Override
132 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
133 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700134 if (receiver == null) {
135 // Allow retrieving current sticky broadcast; this is safe since we
136 // aren't actually registering a receiver.
137 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
138 } else {
139 throw new ReceiverCallNotAllowedException(
140 "BroadcastReceiver components are not allowed to register to receive intents");
141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 }
143
144 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700145 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
146 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700147 if (receiver == null) {
148 // Allow retrieving current sticky broadcast; this is safe since we
149 // aren't actually registering a receiver.
150 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
151 } else {
152 throw new ReceiverCallNotAllowedException(
153 "BroadcastReceiver components are not allowed to register to receive intents");
154 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700155 }
156
157 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
159 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700160 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 }
162}
163
164/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800165 * Common implementation of Context API, which provides the base
166 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800168class ContextImpl extends Context {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private final static String TAG = "ApplicationContext";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700170 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700172 private static final HashMap<String, SharedPreferencesImpl> sSharedPrefs =
173 new HashMap<String, SharedPreferencesImpl>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700175 /*package*/ LoadedApk mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700176 private String mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 private Resources mResources;
178 /*package*/ ActivityThread mMainThread;
179 private Context mOuterContext;
180 private IBinder mActivityToken = null;
181 private ApplicationContentResolver mContentResolver;
182 private int mThemeResource = 0;
183 private Resources.Theme mTheme = null;
184 private PackageManager mPackageManager;
Jeff Browna492c3a2012-08-23 19:48:44 -0700185 private Display mDisplay; // may be null if default display
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 private Context mReceiverRestrictedContext = null;
Romain Guy870e09f2009-07-06 16:35:25 -0700187 private boolean mRestricted;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700188 private UserHandle mUser;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
190 private final Object mSync = new Object();
191
192 private File mDatabasesDir;
193 private File mPreferencesDir;
194 private File mFilesDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 private File mCacheDir;
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800196 private File mObbDir;
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800197 private File mExternalFilesDir;
198 private File mExternalCacheDir;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 private static final String[] EMPTY_FILE_LIST = {};
201
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800202 /**
203 * Override this class when the system service constructor needs a
204 * ContextImpl. Else, use StaticServiceFetcher below.
205 */
206 /*package*/ static class ServiceFetcher {
207 int mContextCacheIndex = -1;
208
209 /**
210 * Main entrypoint; only override if you don't need caching.
211 */
212 public Object getService(ContextImpl ctx) {
213 ArrayList<Object> cache = ctx.mServiceCache;
214 Object service;
215 synchronized (cache) {
216 if (cache.size() == 0) {
217 // Initialize the cache vector on first access.
218 // At this point sNextPerContextServiceCacheIndex
219 // is the number of potential services that are
220 // cached per-Context.
221 for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
222 cache.add(null);
223 }
224 } else {
225 service = cache.get(mContextCacheIndex);
226 if (service != null) {
227 return service;
228 }
229 }
230 service = createService(ctx);
231 cache.set(mContextCacheIndex, service);
232 return service;
233 }
234 }
235
236 /**
237 * Override this to create a new per-Context instance of the
238 * service. getService() will handle locking and caching.
239 */
240 public Object createService(ContextImpl ctx) {
241 throw new RuntimeException("Not implemented");
242 }
243 }
244
245 /**
246 * Override this class for services to be cached process-wide.
247 */
248 abstract static class StaticServiceFetcher extends ServiceFetcher {
249 private Object mCachedInstance;
250
251 @Override
252 public final Object getService(ContextImpl unused) {
253 synchronized (StaticServiceFetcher.this) {
254 Object service = mCachedInstance;
255 if (service != null) {
256 return service;
257 }
258 return mCachedInstance = createStaticService();
259 }
260 }
261
262 public abstract Object createStaticService();
263 }
264
265 private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
266 new HashMap<String, ServiceFetcher>();
267
268 private static int sNextPerContextServiceCacheIndex = 0;
269 private static void registerService(String serviceName, ServiceFetcher fetcher) {
270 if (!(fetcher instanceof StaticServiceFetcher)) {
271 fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
272 }
273 SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
274 }
275
276 // This one's defined separately and given a variable name so it
277 // can be re-used by getWallpaperManager(), avoiding a HashMap
278 // lookup.
279 private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() {
280 public Object createService(ContextImpl ctx) {
281 return new WallpaperManager(ctx.getOuterContext(),
282 ctx.mMainThread.getHandler());
283 }};
284
285 static {
286 registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
287 public Object getService(ContextImpl ctx) {
288 return AccessibilityManager.getInstance(ctx);
289 }});
290
291 registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
292 public Object createService(ContextImpl ctx) {
293 IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
294 IAccountManager service = IAccountManager.Stub.asInterface(b);
295 return new AccountManager(ctx, service);
296 }});
297
298 registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
299 public Object createService(ContextImpl ctx) {
300 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
301 }});
302
303 registerService(ALARM_SERVICE, new StaticServiceFetcher() {
304 public Object createStaticService() {
305 IBinder b = ServiceManager.getService(ALARM_SERVICE);
306 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
307 return new AlarmManager(service);
308 }});
309
310 registerService(AUDIO_SERVICE, new ServiceFetcher() {
311 public Object createService(ContextImpl ctx) {
312 return new AudioManager(ctx);
313 }});
314
Dianne Hackbornb58b8f82012-06-11 15:08:39 -0700315 registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() {
316 public Object createService(ContextImpl ctx) {
317 return new MediaRouter(ctx);
318 }});
319
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800320 registerService(BLUETOOTH_SERVICE, new ServiceFetcher() {
321 public Object createService(ContextImpl ctx) {
322 return BluetoothAdapter.getDefaultAdapter();
323 }});
324
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800325 registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
326 public Object createService(ContextImpl ctx) {
327 return new ClipboardManager(ctx.getOuterContext(),
328 ctx.mMainThread.getHandler());
329 }});
330
331 registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
332 public Object createStaticService() {
333 IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
334 return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
335 }});
336
337 registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
338 public Object createStaticService() {
339 IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
340 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
341 }});
342
343 registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
344 public Object createService(ContextImpl ctx) {
345 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
346 }});
347
348 registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
349 public Object createService(ContextImpl ctx) {
350 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
351 }});
352
Nick Pellyd2507462010-12-13 12:22:34 -0800353 registerService(NFC_SERVICE, new ServiceFetcher() {
354 public Object createService(ContextImpl ctx) {
355 return new NfcManager(ctx);
356 }});
357
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800358 registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
359 public Object createStaticService() {
360 return createDropBoxManager();
361 }});
362
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700363 registerService(INPUT_SERVICE, new StaticServiceFetcher() {
364 public Object createStaticService() {
365 return InputManager.getInstance();
Jeff Brownac143512012-04-05 18:57:33 -0700366 }});
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700367
Jeff Brownbd6e1502012-08-28 03:27:37 -0700368 registerService(DISPLAY_SERVICE, new ServiceFetcher() {
369 @Override
370 public Object createService(ContextImpl ctx) {
371 return new DisplayManager(ctx.getOuterContext());
372 }});
Jeff Brownfa25bf52012-07-23 19:26:30 -0700373
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800374 registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() {
375 public Object createService(ContextImpl ctx) {
376 return InputMethodManager.getInstance(ctx);
377 }});
378
satok988323c2011-06-22 16:38:13 +0900379 registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
380 public Object createService(ContextImpl ctx) {
381 return TextServicesManager.getInstance();
382 }});
383
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800384 registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
385 public Object getService(ContextImpl ctx) {
386 // TODO: why isn't this caching it? It wasn't
387 // before, so I'm preserving the old behavior and
388 // using getService(), instead of createService()
389 // which would do the caching.
390 return new KeyguardManager();
391 }});
392
393 registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
394 public Object createService(ContextImpl ctx) {
395 return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
396 }});
397
Nick Pellye0fd6932012-07-11 10:26:13 -0700398 registerService(LOCATION_SERVICE, new ServiceFetcher() {
399 public Object createService(ContextImpl ctx) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800400 IBinder b = ServiceManager.getService(LOCATION_SERVICE);
Nick Pellye0fd6932012-07-11 10:26:13 -0700401 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800402 }});
403
Jeff Sharkey1a303952011-06-16 13:04:20 -0700404 registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
405 @Override
406 public Object createService(ContextImpl ctx) {
407 return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
408 ServiceManager.getService(NETWORK_POLICY_SERVICE)));
409 }
410 });
411
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800412 registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
413 public Object createService(ContextImpl ctx) {
414 final Context outerContext = ctx.getOuterContext();
415 return new NotificationManager(
416 new ContextThemeWrapper(outerContext,
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800417 Resources.selectSystemTheme(0,
418 outerContext.getApplicationInfo().targetSdkVersion,
419 com.android.internal.R.style.Theme_Dialog,
Adam Powell6e90a362011-08-14 16:48:32 -0700420 com.android.internal.R.style.Theme_Holo_Dialog,
421 com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800422 ctx.mMainThread.getHandler());
423 }});
424
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700425 registerService(NSD_SERVICE, new ServiceFetcher() {
426 @Override
427 public Object createService(ContextImpl ctx) {
428 IBinder b = ServiceManager.getService(NSD_SERVICE);
429 INsdManager service = INsdManager.Stub.asInterface(b);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700430 return new NsdManager(ctx.getOuterContext(), service);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700431 }});
432
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800433 // Note: this was previously cached in a static variable, but
434 // constructed using mMainThread.getHandler(), so converting
435 // it to be a regular Context-cached service...
436 registerService(POWER_SERVICE, new ServiceFetcher() {
437 public Object createService(ContextImpl ctx) {
438 IBinder b = ServiceManager.getService(POWER_SERVICE);
439 IPowerManager service = IPowerManager.Stub.asInterface(b);
Jeff Brown96307042012-07-27 15:51:34 -0700440 return new PowerManager(ctx.getOuterContext(),
441 service, ctx.mMainThread.getHandler());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800442 }});
443
444 registerService(SEARCH_SERVICE, new ServiceFetcher() {
445 public Object createService(ContextImpl ctx) {
446 return new SearchManager(ctx.getOuterContext(),
447 ctx.mMainThread.getHandler());
448 }});
449
450 registerService(SENSOR_SERVICE, new ServiceFetcher() {
451 public Object createService(ContextImpl ctx) {
Jeff Brown25157e42012-04-16 12:13:05 -0700452 return new SystemSensorManager(ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800453 }});
454
455 registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
456 public Object createService(ContextImpl ctx) {
457 return new StatusBarManager(ctx.getOuterContext());
458 }});
459
460 registerService(STORAGE_SERVICE, new ServiceFetcher() {
461 public Object createService(ContextImpl ctx) {
462 try {
463 return new StorageManager(ctx.mMainThread.getHandler().getLooper());
464 } catch (RemoteException rex) {
465 Log.e(TAG, "Failed to create StorageManager", rex);
466 return null;
467 }
468 }});
469
470 registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
471 public Object createService(ContextImpl ctx) {
472 return new TelephonyManager(ctx.getOuterContext());
473 }});
474
475 registerService(THROTTLE_SERVICE, new StaticServiceFetcher() {
476 public Object createStaticService() {
477 IBinder b = ServiceManager.getService(THROTTLE_SERVICE);
478 return new ThrottleManager(IThrottleManager.Stub.asInterface(b));
479 }});
480
481 registerService(UI_MODE_SERVICE, new ServiceFetcher() {
482 public Object createService(ContextImpl ctx) {
483 return new UiModeManager();
484 }});
485
Mike Lockwood3a68b832011-03-08 10:08:59 -0500486 registerService(USB_SERVICE, new ServiceFetcher() {
487 public Object createService(ContextImpl ctx) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500488 IBinder b = ServiceManager.getService(USB_SERVICE);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500489 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500490 }});
491
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400492 registerService(SERIAL_SERVICE, new ServiceFetcher() {
493 public Object createService(ContextImpl ctx) {
494 IBinder b = ServiceManager.getService(SERIAL_SERVICE);
495 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
496 }});
497
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800498 registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
499 public Object createService(ContextImpl ctx) {
Jeff Brownc2346132012-04-13 01:55:38 -0700500 return new SystemVibrator();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800501 }});
502
503 registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
504
505 registerService(WIFI_SERVICE, new ServiceFetcher() {
506 public Object createService(ContextImpl ctx) {
507 IBinder b = ServiceManager.getService(WIFI_SERVICE);
508 IWifiManager service = IWifiManager.Stub.asInterface(b);
Irfan Sheriff88759bb2012-07-02 15:58:28 -0700509 return new WifiManager(ctx.getOuterContext(), service);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800510 }});
511
repo sync55bc5f32011-06-24 14:23:07 -0700512 registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
513 public Object createService(ContextImpl ctx) {
514 IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
515 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
516 return new WifiP2pManager(service);
517 }});
518
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800519 registerService(WINDOW_SERVICE, new ServiceFetcher() {
520 public Object getService(ContextImpl ctx) {
Jeff Browna492c3a2012-08-23 19:48:44 -0700521 Display display = ctx.mDisplay;
522 if (display == null) {
523 DisplayManager dm = (DisplayManager)ctx.getOuterContext().getSystemService(
524 Context.DISPLAY_SERVICE);
525 display = dm.getDisplay(Display.DEFAULT_DISPLAY);
526 }
527 return new WindowManagerImpl(display);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800528 }});
Amith Yamasani258848d2012-08-10 17:06:33 -0700529
530 registerService(USER_SERVICE, new ServiceFetcher() {
531 public Object getService(ContextImpl ctx) {
532 IBinder b = ServiceManager.getService(USER_SERVICE);
533 IUserManager service = IUserManager.Stub.asInterface(b);
534 return new UserManager(ctx, service);
535 }});
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800536 }
537
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700538 static ContextImpl getImpl(Context context) {
539 Context nextContext;
540 while ((context instanceof ContextWrapper) &&
541 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
542 context = nextContext;
543 }
544 return (ContextImpl)context;
545 }
546
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800547 // The system service cache for the system services that are
548 // cached per-ContextImpl. Package-scoped to avoid accessor
549 // methods.
550 final ArrayList<Object> mServiceCache = new ArrayList<Object>();
551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 @Override
553 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700554 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 }
556
557 @Override
558 public Resources getResources() {
559 return mResources;
560 }
561
562 @Override
563 public PackageManager getPackageManager() {
564 if (mPackageManager != null) {
565 return mPackageManager;
566 }
567
568 IPackageManager pm = ActivityThread.getPackageManager();
569 if (pm != null) {
570 // Doesn't matter if we make more than one instance.
571 return (mPackageManager = new ApplicationPackageManager(this, pm));
572 }
573
574 return null;
575 }
576
577 @Override
578 public ContentResolver getContentResolver() {
579 return mContentResolver;
580 }
581
582 @Override
583 public Looper getMainLooper() {
584 return mMainThread.getLooper();
585 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 @Override
588 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700589 return (mPackageInfo != null) ?
590 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 @Override
594 public void setTheme(int resid) {
595 mThemeResource = resid;
596 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800599 public int getThemeResId() {
600 return mThemeResource;
601 }
602
603 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 public Resources.Theme getTheme() {
605 if (mTheme == null) {
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800606 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
607 getOuterContext().getApplicationInfo().targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 mTheme = mResources.newTheme();
609 mTheme.applyStyle(mThemeResource, true);
610 }
611 return mTheme;
612 }
613
614 @Override
615 public ClassLoader getClassLoader() {
616 return mPackageInfo != null ?
617 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
618 }
619
620 @Override
621 public String getPackageName() {
622 if (mPackageInfo != null) {
623 return mPackageInfo.getPackageName();
624 }
625 throw new RuntimeException("Not supported in system context");
626 }
627
628 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700629 public ApplicationInfo getApplicationInfo() {
630 if (mPackageInfo != null) {
631 return mPackageInfo.getApplicationInfo();
632 }
633 throw new RuntimeException("Not supported in system context");
634 }
635
636 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 public String getPackageResourcePath() {
638 if (mPackageInfo != null) {
639 return mPackageInfo.getResDir();
640 }
641 throw new RuntimeException("Not supported in system context");
642 }
643
644 @Override
645 public String getPackageCodePath() {
646 if (mPackageInfo != null) {
647 return mPackageInfo.getAppDir();
648 }
649 throw new RuntimeException("Not supported in system context");
650 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200651
Joe Onorato23ecae32009-06-10 17:07:15 -0700652 public File getSharedPrefsFile(String name) {
653 return makeFilename(getPreferencesDir(), name + ".xml");
654 }
655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 @Override
657 public SharedPreferences getSharedPreferences(String name, int mode) {
658 SharedPreferencesImpl sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 synchronized (sSharedPrefs) {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700660 sp = sSharedPrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700661 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800662 File prefsFile = getSharedPrefsFile(name);
663 sp = new SharedPreferencesImpl(prefsFile, mode);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700664 sSharedPrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700665 return sp;
666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800668 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
669 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
670 // If somebody else (some other process) changed the prefs
671 // file behind our back, we reload it. This has been the
672 // historical (if undocumented) behavior.
673 sp.startReloadIfChangedUnexpectedly();
674 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700675 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 }
677
678 private File getPreferencesDir() {
679 synchronized (mSync) {
680 if (mPreferencesDir == null) {
681 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
682 }
683 return mPreferencesDir;
684 }
685 }
686
687 @Override
688 public FileInputStream openFileInput(String name)
689 throws FileNotFoundException {
690 File f = makeFilename(getFilesDir(), name);
691 return new FileInputStream(f);
692 }
693
694 @Override
695 public FileOutputStream openFileOutput(String name, int mode)
696 throws FileNotFoundException {
697 final boolean append = (mode&MODE_APPEND) != 0;
698 File f = makeFilename(getFilesDir(), name);
699 try {
700 FileOutputStream fos = new FileOutputStream(f, append);
701 setFilePermissionsFromMode(f.getPath(), mode, 0);
702 return fos;
703 } catch (FileNotFoundException e) {
704 }
705
706 File parent = f.getParentFile();
707 parent.mkdir();
708 FileUtils.setPermissions(
709 parent.getPath(),
710 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
711 -1, -1);
712 FileOutputStream fos = new FileOutputStream(f, append);
713 setFilePermissionsFromMode(f.getPath(), mode, 0);
714 return fos;
715 }
716
717 @Override
718 public boolean deleteFile(String name) {
719 File f = makeFilename(getFilesDir(), name);
720 return f.delete();
721 }
722
723 @Override
724 public File getFilesDir() {
725 synchronized (mSync) {
726 if (mFilesDir == null) {
727 mFilesDir = new File(getDataDirFile(), "files");
728 }
729 if (!mFilesDir.exists()) {
730 if(!mFilesDir.mkdirs()) {
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200731 Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 return null;
733 }
734 FileUtils.setPermissions(
735 mFilesDir.getPath(),
736 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
737 -1, -1);
738 }
739 return mFilesDir;
740 }
741 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 @Override
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800744 public File getExternalFilesDir(String type) {
745 synchronized (mSync) {
746 if (mExternalFilesDir == null) {
747 mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory(
748 getPackageName());
749 }
750 if (!mExternalFilesDir.exists()) {
751 try {
752 (new File(Environment.getExternalStorageAndroidDataDir(),
753 ".nomedia")).createNewFile();
754 } catch (IOException e) {
755 }
756 if (!mExternalFilesDir.mkdirs()) {
757 Log.w(TAG, "Unable to create external files directory");
758 return null;
759 }
760 }
761 if (type == null) {
762 return mExternalFilesDir;
763 }
764 File dir = new File(mExternalFilesDir, type);
765 if (!dir.exists()) {
766 if (!dir.mkdirs()) {
767 Log.w(TAG, "Unable to create external media directory " + dir);
768 return null;
769 }
770 }
771 return dir;
772 }
773 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200774
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800775 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800776 public File getObbDir() {
777 synchronized (mSync) {
778 if (mObbDir == null) {
779 mObbDir = Environment.getExternalStorageAppObbDirectory(
780 getPackageName());
781 }
782 return mObbDir;
783 }
784 }
785
786 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 public File getCacheDir() {
788 synchronized (mSync) {
789 if (mCacheDir == null) {
790 mCacheDir = new File(getDataDirFile(), "cache");
791 }
792 if (!mCacheDir.exists()) {
793 if(!mCacheDir.mkdirs()) {
Amith Yamasani92d57052012-08-23 10:07:52 -0700794 Log.w(TAG, "Unable to create cache directory " + mCacheDir.getAbsolutePath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 return null;
796 }
797 FileUtils.setPermissions(
798 mCacheDir.getPath(),
799 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
800 -1, -1);
801 }
802 }
803 return mCacheDir;
804 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200805
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800806 @Override
807 public File getExternalCacheDir() {
808 synchronized (mSync) {
809 if (mExternalCacheDir == null) {
810 mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory(
811 getPackageName());
812 }
813 if (!mExternalCacheDir.exists()) {
814 try {
815 (new File(Environment.getExternalStorageAndroidDataDir(),
816 ".nomedia")).createNewFile();
817 } catch (IOException e) {
818 }
819 if (!mExternalCacheDir.mkdirs()) {
820 Log.w(TAG, "Unable to create external cache directory");
821 return null;
822 }
823 }
824 return mExternalCacheDir;
825 }
826 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 @Override
829 public File getFileStreamPath(String name) {
830 return makeFilename(getFilesDir(), name);
831 }
832
833 @Override
834 public String[] fileList() {
835 final String[] list = getFilesDir().list();
836 return (list != null) ? list : EMPTY_FILE_LIST;
837 }
838
839 @Override
840 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700841 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843
844 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700845 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
846 DatabaseErrorHandler errorHandler) {
847 File f = validateFilePath(name, true);
Jeff Brown47847f32012-03-22 19:13:11 -0700848 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
849 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
850 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
851 }
852 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700853 setFilePermissionsFromMode(f.getPath(), mode, 0);
854 return db;
855 }
856
857 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 public boolean deleteDatabase(String name) {
859 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800860 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800861 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 } catch (Exception e) {
863 }
864 return false;
865 }
866
867 @Override
868 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800869 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871
872 @Override
873 public String[] databaseList() {
874 final String[] list = getDatabasesDir().list();
875 return (list != null) ? list : EMPTY_FILE_LIST;
876 }
877
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 private File getDatabasesDir() {
880 synchronized (mSync) {
881 if (mDatabasesDir == null) {
882 mDatabasesDir = new File(getDataDirFile(), "databases");
883 }
884 if (mDatabasesDir.getPath().equals("databases")) {
885 mDatabasesDir = new File("/data/system");
886 }
887 return mDatabasesDir;
888 }
889 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 @Override
892 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700893 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895
896 @Override
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700897 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700898 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
900
901 @Override
902 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700903 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
905
906 @Override
907 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700908 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910
911 @Override
912 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700913 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 }
915
916 @Override
917 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700918 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920
921 @Override
922 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700923 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 }
925
926 @Override
927 public void startActivity(Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700928 startActivity(intent, null);
929 }
930
Amith Yamasani82644082012-08-03 13:09:11 -0700931 /** @hide */
932 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700933 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700934 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700935 }
936
Dianne Hackborna4972e92012-03-14 10:38:05 -0700937 @Override
938 public void startActivity(Intent intent, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
940 throw new AndroidRuntimeException(
941 "Calling startActivity() from outside of an Activity "
942 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
943 + " Is this really what you want?");
944 }
945 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700946 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700947 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949
Amith Yamasani258848d2012-08-10 17:06:33 -0700950 /** @hide */
951 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700952 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700953 try {
954 ActivityManagerNative.getDefault().startActivityAsUser(
955 mMainThread.getApplicationThread(), intent,
956 intent.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700957 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options,
958 user.getIdentifier());
Amith Yamasani258848d2012-08-10 17:06:33 -0700959 } catch (RemoteException re) {
960 }
961 }
962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800964 public void startActivities(Intent[] intents) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700965 startActivities(intents, null);
966 }
967
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700968 /** @hide */
969 @Override
970 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
971 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
972 throw new AndroidRuntimeException(
973 "Calling startActivities() from outside of an Activity "
974 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
975 + " Is this really what you want?");
976 }
977 mMainThread.getInstrumentation().execStartActivitiesAsUser(
978 getOuterContext(), mMainThread.getApplicationThread(), null,
979 (Activity)null, intents, options, userHandle.getIdentifier());
980 }
981
Dianne Hackborna4972e92012-03-14 10:38:05 -0700982 @Override
983 public void startActivities(Intent[] intents, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800984 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
985 throw new AndroidRuntimeException(
986 "Calling startActivities() from outside of an Activity "
987 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
988 + " Is this really what you want?");
989 }
990 mMainThread.getInstrumentation().execStartActivities(
991 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700992 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800993 }
994
995 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700996 public void startIntentSender(IntentSender intent,
997 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
998 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700999 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
1000 }
1001
1002 @Override
1003 public void startIntentSender(IntentSender intent, Intent fillInIntent,
1004 int flagsMask, int flagsValues, int extraFlags, Bundle options)
1005 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001006 try {
1007 String resolvedType = null;
1008 if (fillInIntent != null) {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001009 fillInIntent.setAllowFds(false);
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001010 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
1011 }
1012 int result = ActivityManagerNative.getDefault()
1013 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
1014 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -07001015 0, flagsMask, flagsValues, options);
1016 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001017 throw new IntentSender.SendIntentException();
1018 }
1019 Instrumentation.checkStartActivityResult(result, null);
1020 } catch (RemoteException e) {
1021 }
1022 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001023
Dianne Hackbornfa82f222009-09-17 15:14:12 -07001024 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 public void sendBroadcast(Intent intent) {
1026 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1027 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001028 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 ActivityManagerNative.getDefault().broadcastIntent(
1030 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001031 Activity.RESULT_OK, null, null, null, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001032 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 } catch (RemoteException e) {
1034 }
1035 }
1036
Amith Yamasani67cf7d32012-02-16 14:31:23 -08001037 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 public void sendBroadcast(Intent intent, String receiverPermission) {
1039 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1040 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001041 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 ActivityManagerNative.getDefault().broadcastIntent(
1043 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001044 Activity.RESULT_OK, null, null, receiverPermission, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001045 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 } catch (RemoteException e) {
1047 }
1048 }
1049
1050 @Override
1051 public void sendOrderedBroadcast(Intent intent,
1052 String receiverPermission) {
1053 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1054 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001055 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 ActivityManagerNative.getDefault().broadcastIntent(
1057 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001058 Activity.RESULT_OK, null, null, receiverPermission, true, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001059 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 } catch (RemoteException e) {
1061 }
1062 }
1063
1064 @Override
1065 public void sendOrderedBroadcast(Intent intent,
1066 String receiverPermission, BroadcastReceiver resultReceiver,
1067 Handler scheduler, int initialCode, String initialData,
1068 Bundle initialExtras) {
1069 IIntentReceiver rd = null;
1070 if (resultReceiver != null) {
1071 if (mPackageInfo != null) {
1072 if (scheduler == null) {
1073 scheduler = mMainThread.getHandler();
1074 }
1075 rd = mPackageInfo.getReceiverDispatcher(
1076 resultReceiver, getOuterContext(), scheduler,
1077 mMainThread.getInstrumentation(), false);
1078 } else {
1079 if (scheduler == null) {
1080 scheduler = mMainThread.getHandler();
1081 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001082 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1084 }
1085 }
1086 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1087 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001088 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 ActivityManagerNative.getDefault().broadcastIntent(
1090 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1091 initialCode, initialData, initialExtras, receiverPermission,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001092 true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 } catch (RemoteException e) {
1094 }
1095 }
1096
1097 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001098 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001099 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1100 try {
1101 intent.setAllowFds(false);
1102 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
1103 intent, resolvedType, null, Activity.RESULT_OK, null, null, null, false, false,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001104 user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001105 } catch (RemoteException e) {
1106 }
1107 }
1108
1109 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001110 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1111 String receiverPermission) {
1112 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1113 try {
1114 intent.setAllowFds(false);
1115 ActivityManagerNative.getDefault().broadcastIntent(
1116 mMainThread.getApplicationThread(), intent, resolvedType, null,
1117 Activity.RESULT_OK, null, null, receiverPermission, false, false,
1118 user.getIdentifier());
1119 } catch (RemoteException e) {
1120 }
1121 }
1122
1123 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001124 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001125 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001126 int initialCode, String initialData, Bundle initialExtras) {
1127 IIntentReceiver rd = null;
1128 if (resultReceiver != null) {
1129 if (mPackageInfo != null) {
1130 if (scheduler == null) {
1131 scheduler = mMainThread.getHandler();
1132 }
1133 rd = mPackageInfo.getReceiverDispatcher(
1134 resultReceiver, getOuterContext(), scheduler,
1135 mMainThread.getInstrumentation(), false);
1136 } else {
1137 if (scheduler == null) {
1138 scheduler = mMainThread.getHandler();
1139 }
1140 rd = new LoadedApk.ReceiverDispatcher(
1141 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1142 }
1143 }
1144 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1145 try {
1146 intent.setAllowFds(false);
1147 ActivityManagerNative.getDefault().broadcastIntent(
1148 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001149 initialCode, initialData, initialExtras, receiverPermission,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001150 true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001151 } catch (RemoteException e) {
1152 }
1153 }
1154
1155 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 public void sendStickyBroadcast(Intent intent) {
1157 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1158 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001159 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 ActivityManagerNative.getDefault().broadcastIntent(
1161 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001162 Activity.RESULT_OK, null, null, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001163 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 } catch (RemoteException e) {
1165 }
1166 }
1167
1168 @Override
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001169 public void sendStickyOrderedBroadcast(Intent intent,
1170 BroadcastReceiver resultReceiver,
1171 Handler scheduler, int initialCode, String initialData,
1172 Bundle initialExtras) {
1173 IIntentReceiver rd = null;
1174 if (resultReceiver != null) {
1175 if (mPackageInfo != null) {
1176 if (scheduler == null) {
1177 scheduler = mMainThread.getHandler();
1178 }
1179 rd = mPackageInfo.getReceiverDispatcher(
1180 resultReceiver, getOuterContext(), scheduler,
1181 mMainThread.getInstrumentation(), false);
1182 } else {
1183 if (scheduler == null) {
1184 scheduler = mMainThread.getHandler();
1185 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001186 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001187 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1188 }
1189 }
1190 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1191 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001192 intent.setAllowFds(false);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001193 ActivityManagerNative.getDefault().broadcastIntent(
1194 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1195 initialCode, initialData, initialExtras, null,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001196 true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001197 } catch (RemoteException e) {
1198 }
1199 }
1200
1201 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 public void removeStickyBroadcast(Intent intent) {
1203 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1204 if (resolvedType != null) {
1205 intent = new Intent(intent);
1206 intent.setDataAndType(intent.getData(), resolvedType);
1207 }
1208 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001209 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001211 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 } catch (RemoteException e) {
1213 }
1214 }
1215
1216 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001217 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1218 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1219 try {
1220 intent.setAllowFds(false);
1221 ActivityManagerNative.getDefault().broadcastIntent(
1222 mMainThread.getApplicationThread(), intent, resolvedType, null,
1223 Activity.RESULT_OK, null, null, null, false, true, user.getIdentifier());
1224 } catch (RemoteException e) {
1225 }
1226 }
1227
1228 @Override
1229 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1230 UserHandle user, BroadcastReceiver resultReceiver,
1231 Handler scheduler, int initialCode, String initialData,
1232 Bundle initialExtras) {
1233 IIntentReceiver rd = null;
1234 if (resultReceiver != null) {
1235 if (mPackageInfo != null) {
1236 if (scheduler == null) {
1237 scheduler = mMainThread.getHandler();
1238 }
1239 rd = mPackageInfo.getReceiverDispatcher(
1240 resultReceiver, getOuterContext(), scheduler,
1241 mMainThread.getInstrumentation(), false);
1242 } else {
1243 if (scheduler == null) {
1244 scheduler = mMainThread.getHandler();
1245 }
1246 rd = new LoadedApk.ReceiverDispatcher(
1247 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1248 }
1249 }
1250 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1251 try {
1252 intent.setAllowFds(false);
1253 ActivityManagerNative.getDefault().broadcastIntent(
1254 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1255 initialCode, initialData, initialExtras, null,
1256 true, true, user.getIdentifier());
1257 } catch (RemoteException e) {
1258 }
1259 }
1260
1261 @Override
1262 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1263 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1264 if (resolvedType != null) {
1265 intent = new Intent(intent);
1266 intent.setDataAndType(intent.getData(), resolvedType);
1267 }
1268 try {
1269 intent.setAllowFds(false);
1270 ActivityManagerNative.getDefault().unbroadcastIntent(
1271 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1272 } catch (RemoteException e) {
1273 }
1274 }
1275
1276 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1278 return registerReceiver(receiver, filter, null, null);
1279 }
1280
1281 @Override
1282 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1283 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001284 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001285 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287
Dianne Hackborn20e80982012-08-31 19:00:44 -07001288 @Override
1289 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1290 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1291 return registerReceiverInternal(receiver, user.getIdentifier(),
1292 filter, broadcastPermission, scheduler, getOuterContext());
1293 }
1294
1295 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 IntentFilter filter, String broadcastPermission,
1297 Handler scheduler, Context context) {
1298 IIntentReceiver rd = null;
1299 if (receiver != null) {
1300 if (mPackageInfo != null && context != null) {
1301 if (scheduler == null) {
1302 scheduler = mMainThread.getHandler();
1303 }
1304 rd = mPackageInfo.getReceiverDispatcher(
1305 receiver, context, scheduler,
1306 mMainThread.getInstrumentation(), true);
1307 } else {
1308 if (scheduler == null) {
1309 scheduler = mMainThread.getHandler();
1310 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001311 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001312 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
1314 }
1315 try {
1316 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001317 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001318 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 } catch (RemoteException e) {
1320 return null;
1321 }
1322 }
1323
1324 @Override
1325 public void unregisterReceiver(BroadcastReceiver receiver) {
1326 if (mPackageInfo != null) {
1327 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1328 getOuterContext(), receiver);
1329 try {
1330 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1331 } catch (RemoteException e) {
1332 }
1333 } else {
1334 throw new RuntimeException("Not supported in system context");
1335 }
1336 }
1337
1338 @Override
1339 public ComponentName startService(Intent service) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001340 return startServiceAsUser(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001341 }
1342
1343 @Override
1344 public boolean stopService(Intent service) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001345 return stopServiceAsUser(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001346 }
1347
1348 @Override
1349 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001351 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 ComponentName cn = ActivityManagerNative.getDefault().startService(
1353 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001354 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001355 if (cn != null) {
1356 if (cn.getPackageName().equals("!")) {
1357 throw new SecurityException(
1358 "Not allowed to start service " + service
1359 + " without permission " + cn.getClassName());
1360 } else if (cn.getPackageName().equals("!!")) {
1361 throw new SecurityException(
1362 "Unable to start service " + service
1363 + ": " + cn.getClassName());
1364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 }
1366 return cn;
1367 } catch (RemoteException e) {
1368 return null;
1369 }
1370 }
1371
1372 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001373 public boolean stopServiceAsUser(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001375 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 int res = ActivityManagerNative.getDefault().stopService(
1377 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001378 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 if (res < 0) {
1380 throw new SecurityException(
1381 "Not allowed to stop service " + service);
1382 }
1383 return res != 0;
1384 } catch (RemoteException e) {
1385 return false;
1386 }
1387 }
1388
1389 @Override
1390 public boolean bindService(Intent service, ServiceConnection conn,
1391 int flags) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001392 return bindService(service, conn, flags, UserHandle.getUserId(Process.myUid()));
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001393 }
1394
1395 /** @hide */
1396 @Override
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001397 public boolean bindService(Intent service, ServiceConnection conn, int flags, int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001399 if (conn == null) {
1400 throw new IllegalArgumentException("connection is null");
1401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 if (mPackageInfo != null) {
1403 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1404 mMainThread.getHandler(), flags);
1405 } else {
1406 throw new RuntimeException("Not supported in system context");
1407 }
1408 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001409 IBinder token = getActivityToken();
1410 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1411 && mPackageInfo.getApplicationInfo().targetSdkVersion
1412 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1413 flags |= BIND_WAIVE_PRIORITY;
1414 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001415 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 int res = ActivityManagerNative.getDefault().bindService(
1417 mMainThread.getApplicationThread(), getActivityToken(),
1418 service, service.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001419 sd, flags, userHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 if (res < 0) {
1421 throw new SecurityException(
1422 "Not allowed to bind to service " + service);
1423 }
1424 return res != 0;
1425 } catch (RemoteException e) {
1426 return false;
1427 }
1428 }
1429
1430 @Override
1431 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001432 if (conn == null) {
1433 throw new IllegalArgumentException("connection is null");
1434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 if (mPackageInfo != null) {
1436 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1437 getOuterContext(), conn);
1438 try {
1439 ActivityManagerNative.getDefault().unbindService(sd);
1440 } catch (RemoteException e) {
1441 }
1442 } else {
1443 throw new RuntimeException("Not supported in system context");
1444 }
1445 }
1446
1447 @Override
1448 public boolean startInstrumentation(ComponentName className,
1449 String profileFile, Bundle arguments) {
1450 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001451 if (arguments != null) {
1452 arguments.setAllowFds(false);
1453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 return ActivityManagerNative.getDefault().startInstrumentation(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001455 className, profileFile, 0, arguments, null, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 } catch (RemoteException e) {
1457 // System has crashed, nothing we can do.
1458 }
1459 return false;
1460 }
1461
1462 @Override
1463 public Object getSystemService(String name) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001464 ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
1465 return fetcher == null ? null : fetcher.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
1467
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001468 private WallpaperManager getWallpaperManager() {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001469 return (WallpaperManager) WALLPAPER_FETCHER.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 }
1471
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001472 /* package */ static DropBoxManager createDropBoxManager() {
1473 IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
1474 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001475 if (service == null) {
1476 // Don't return a DropBoxManager that will NPE upon use.
1477 // This also avoids caching a broken DropBoxManager in
1478 // getDropBoxManager during early boot, before the
1479 // DROPBOX_SERVICE is registered.
1480 return null;
1481 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001482 return new DropBoxManager(service);
1483 }
1484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 @Override
1486 public int checkPermission(String permission, int pid, int uid) {
1487 if (permission == null) {
1488 throw new IllegalArgumentException("permission is null");
1489 }
1490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 try {
1492 return ActivityManagerNative.getDefault().checkPermission(
1493 permission, pid, uid);
1494 } catch (RemoteException e) {
1495 return PackageManager.PERMISSION_DENIED;
1496 }
1497 }
1498
1499 @Override
1500 public int checkCallingPermission(String permission) {
1501 if (permission == null) {
1502 throw new IllegalArgumentException("permission is null");
1503 }
1504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 int pid = Binder.getCallingPid();
1506 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001507 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509 return PackageManager.PERMISSION_DENIED;
1510 }
1511
1512 @Override
1513 public int checkCallingOrSelfPermission(String permission) {
1514 if (permission == null) {
1515 throw new IllegalArgumentException("permission is null");
1516 }
1517
1518 return checkPermission(permission, Binder.getCallingPid(),
1519 Binder.getCallingUid());
1520 }
1521
1522 private void enforce(
1523 String permission, int resultOfCheck,
1524 boolean selfToo, int uid, String message) {
1525 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1526 throw new SecurityException(
1527 (message != null ? (message + ": ") : "") +
1528 (selfToo
1529 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001530 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 permission +
1532 ".");
1533 }
1534 }
1535
1536 public void enforcePermission(
1537 String permission, int pid, int uid, String message) {
1538 enforce(permission,
1539 checkPermission(permission, pid, uid),
1540 false,
1541 uid,
1542 message);
1543 }
1544
1545 public void enforceCallingPermission(String permission, String message) {
1546 enforce(permission,
1547 checkCallingPermission(permission),
1548 false,
1549 Binder.getCallingUid(),
1550 message);
1551 }
1552
1553 public void enforceCallingOrSelfPermission(
1554 String permission, String message) {
1555 enforce(permission,
1556 checkCallingOrSelfPermission(permission),
1557 true,
1558 Binder.getCallingUid(),
1559 message);
1560 }
1561
1562 @Override
1563 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1564 try {
1565 ActivityManagerNative.getDefault().grantUriPermission(
1566 mMainThread.getApplicationThread(), toPackage, uri,
1567 modeFlags);
1568 } catch (RemoteException e) {
1569 }
1570 }
1571
1572 @Override
1573 public void revokeUriPermission(Uri uri, int modeFlags) {
1574 try {
1575 ActivityManagerNative.getDefault().revokeUriPermission(
1576 mMainThread.getApplicationThread(), uri,
1577 modeFlags);
1578 } catch (RemoteException e) {
1579 }
1580 }
1581
1582 @Override
1583 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 try {
1585 return ActivityManagerNative.getDefault().checkUriPermission(
1586 uri, pid, uid, modeFlags);
1587 } catch (RemoteException e) {
1588 return PackageManager.PERMISSION_DENIED;
1589 }
1590 }
1591
1592 @Override
1593 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 int pid = Binder.getCallingPid();
1595 if (pid != Process.myPid()) {
1596 return checkUriPermission(uri, pid,
1597 Binder.getCallingUid(), modeFlags);
1598 }
1599 return PackageManager.PERMISSION_DENIED;
1600 }
1601
1602 @Override
1603 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1604 return checkUriPermission(uri, Binder.getCallingPid(),
1605 Binder.getCallingUid(), modeFlags);
1606 }
1607
1608 @Override
1609 public int checkUriPermission(Uri uri, String readPermission,
1610 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001611 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1613 + readPermission + " writePermission=" + writePermission
1614 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1615 }
1616 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1617 if (readPermission == null
1618 || checkPermission(readPermission, pid, uid)
1619 == PackageManager.PERMISSION_GRANTED) {
1620 return PackageManager.PERMISSION_GRANTED;
1621 }
1622 }
1623 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1624 if (writePermission == null
1625 || checkPermission(writePermission, pid, uid)
1626 == PackageManager.PERMISSION_GRANTED) {
1627 return PackageManager.PERMISSION_GRANTED;
1628 }
1629 }
1630 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1631 : PackageManager.PERMISSION_DENIED;
1632 }
1633
1634 private String uriModeFlagToString(int uriModeFlags) {
1635 switch (uriModeFlags) {
1636 case Intent.FLAG_GRANT_READ_URI_PERMISSION |
1637 Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1638 return "read and write";
1639 case Intent.FLAG_GRANT_READ_URI_PERMISSION:
1640 return "read";
1641 case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1642 return "write";
1643 }
1644 throw new IllegalArgumentException(
1645 "Unknown permission mode flags: " + uriModeFlags);
1646 }
1647
1648 private void enforceForUri(
1649 int modeFlags, int resultOfCheck, boolean selfToo,
1650 int uid, Uri uri, String message) {
1651 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1652 throw new SecurityException(
1653 (message != null ? (message + ": ") : "") +
1654 (selfToo
1655 ? "Neither user " + uid + " nor current process has "
1656 : "User " + uid + " does not have ") +
1657 uriModeFlagToString(modeFlags) +
1658 " permission on " +
1659 uri +
1660 ".");
1661 }
1662 }
1663
1664 public void enforceUriPermission(
1665 Uri uri, int pid, int uid, int modeFlags, String message) {
1666 enforceForUri(
1667 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1668 false, uid, uri, message);
1669 }
1670
1671 public void enforceCallingUriPermission(
1672 Uri uri, int modeFlags, String message) {
1673 enforceForUri(
1674 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001675 false,
1676 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
1679 public void enforceCallingOrSelfUriPermission(
1680 Uri uri, int modeFlags, String message) {
1681 enforceForUri(
1682 modeFlags,
1683 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1684 Binder.getCallingUid(), uri, message);
1685 }
1686
1687 public void enforceUriPermission(
1688 Uri uri, String readPermission, String writePermission,
1689 int pid, int uid, int modeFlags, String message) {
1690 enforceForUri(modeFlags,
1691 checkUriPermission(
1692 uri, readPermission, writePermission, pid, uid,
1693 modeFlags),
1694 false,
1695 uid,
1696 uri,
1697 message);
1698 }
1699
1700 @Override
1701 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001702 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001703 return createPackageContextAsUser(packageName, flags,
1704 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001705 }
1706
1707 @Override
1708 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1709 throws NameNotFoundException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001711 final ContextImpl context = new ContextImpl(mMainThread.getSystemContext());
Adam Powell6f2a3d22012-10-05 22:32:04 -07001712 context.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
1713 context.init(mPackageInfo, null, mMainThread, mResources, mBasePackageName, user);
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001714 return context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 }
1716
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001717 LoadedApk pi =
Amith Yamasani98edc952012-09-25 14:09:27 -07001718 mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags,
1719 user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 if (pi != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001721 ContextImpl c = new ContextImpl();
Romain Guy870e09f2009-07-06 16:35:25 -07001722 c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001723 c.init(pi, null, mMainThread, mResources, mBasePackageName, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 if (c.mResources != null) {
1725 return c;
1726 }
1727 }
1728
1729 // Should be a better exception.
1730 throw new PackageManager.NameNotFoundException(
1731 "Application package " + packageName + " not found");
1732 }
1733
Romain Guy870e09f2009-07-06 16:35:25 -07001734 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001735 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001736 if (overrideConfiguration == null) {
1737 throw new IllegalArgumentException("overrideConfiguration must not be null");
1738 }
1739
Dianne Hackborn756220b2012-08-14 16:45:30 -07001740 ContextImpl c = new ContextImpl();
1741 c.init(mPackageInfo, null, mMainThread);
1742 c.mResources = mMainThread.getTopLevelResources(
Jeff Browna492c3a2012-08-23 19:48:44 -07001743 mPackageInfo.getResDir(),
1744 getDisplayId(), overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001745 mResources.getCompatibilityInfo());
1746 return c;
1747 }
1748
1749 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001750 public Context createDisplayContext(Display display) {
1751 if (display == null) {
1752 throw new IllegalArgumentException("display must not be null");
1753 }
1754
1755 int displayId = display.getDisplayId();
1756 CompatibilityInfo ci = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
1757 CompatibilityInfoHolder cih = getCompatibilityInfo(displayId);
1758 if (cih != null) {
1759 ci = cih.get();
1760 }
1761
1762 ContextImpl context = new ContextImpl();
1763 context.init(mPackageInfo, null, mMainThread);
1764 context.mDisplay = display;
1765 context.mResources = mMainThread.getTopLevelResources(
1766 mPackageInfo.getResDir(), displayId, null, ci);
1767 return context;
1768 }
1769
1770 private int getDisplayId() {
1771 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1772 }
1773
1774 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001775 public boolean isRestricted() {
1776 return mRestricted;
1777 }
1778
Jeff Brown98365d72012-08-19 20:30:52 -07001779 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001780 public CompatibilityInfoHolder getCompatibilityInfo(int displayId) {
1781 return displayId == Display.DEFAULT_DISPLAY ? mPackageInfo.mCompatibilityInfo : null;
Jeff Brown98365d72012-08-19 20:30:52 -07001782 }
1783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 private File getDataDirFile() {
1785 if (mPackageInfo != null) {
1786 return mPackageInfo.getDataDirFile();
1787 }
1788 throw new RuntimeException("Not supported in system context");
1789 }
1790
1791 @Override
1792 public File getDir(String name, int mode) {
1793 name = "app_" + name;
1794 File file = makeFilename(getDataDirFile(), name);
1795 if (!file.exists()) {
1796 file.mkdir();
1797 setFilePermissionsFromMode(file.getPath(), mode,
1798 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1799 }
1800 return file;
1801 }
1802
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001803 /** {@hide} */
1804 public int getUserId() {
1805 return mUser.getIdentifier();
1806 }
1807
Dianne Hackborn21556372010-02-04 16:34:40 -08001808 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001809 final ContextImpl context = new ContextImpl();
1810 context.init(Resources.getSystem(), mainThread, Process.myUserHandle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 return context;
1812 }
1813
Dianne Hackborn21556372010-02-04 16:34:40 -08001814 ContextImpl() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 mOuterContext = this;
1816 }
1817
1818 /**
1819 * Create a new ApplicationContext from an existing one. The new one
1820 * works and operates the same as the one it is copying.
1821 *
1822 * @param context Existing application context.
1823 */
Dianne Hackborn21556372010-02-04 16:34:40 -08001824 public ContextImpl(ContextImpl context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 mPackageInfo = context.mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001826 mBasePackageName = context.mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 mResources = context.mResources;
1828 mMainThread = context.mMainThread;
1829 mContentResolver = context.mContentResolver;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001830 mUser = context.mUser;
Jeff Browna492c3a2012-08-23 19:48:44 -07001831 mDisplay = context.mDisplay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 mOuterContext = this;
1833 }
1834
Jeff Sharkey6d515712012-09-20 16:06:08 -07001835 final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread) {
1836 init(packageInfo, activityToken, mainThread, null, null, Process.myUserHandle());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001837 }
1838
Jeff Sharkey6d515712012-09-20 16:06:08 -07001839 final void init(LoadedApk packageInfo, IBinder activityToken, ActivityThread mainThread,
1840 Resources container, String basePackageName, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 mPackageInfo = packageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001842 mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 mResources = mPackageInfo.getResources(mainThread);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001844
Dianne Hackborn559a7872010-04-07 18:19:41 -07001845 if (mResources != null && container != null
1846 && container.getCompatibilityInfo().applicationScale !=
1847 mResources.getCompatibilityInfo().applicationScale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001848 if (DEBUG) {
1849 Log.d(TAG, "loaded context has different scaling. Using container's" +
1850 " compatiblity info:" + container.getDisplayMetrics());
1851 }
1852 mResources = mainThread.getTopLevelResources(
Jeff Browna492c3a2012-08-23 19:48:44 -07001853 mPackageInfo.getResDir(), Display.DEFAULT_DISPLAY,
1854 null, container.getCompatibilityInfo());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 mMainThread = mainThread;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001857 mActivityToken = activityToken;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001858 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
1859 mUser = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
1861
Jeff Sharkey6d515712012-09-20 16:06:08 -07001862 final void init(Resources resources, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 mPackageInfo = null;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001864 mBasePackageName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 mResources = resources;
1866 mMainThread = mainThread;
Jeff Sharkey6d515712012-09-20 16:06:08 -07001867 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
1868 mUser = user;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 }
1870
1871 final void scheduleFinalCleanup(String who, String what) {
1872 mMainThread.scheduleContextCleanup(this, who, what);
1873 }
1874
1875 final void performFinalCleanup(String who, String what) {
1876 //Log.i(TAG, "Cleanup up context: " + this);
1877 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1878 }
1879
1880 final Context getReceiverRestrictedContext() {
1881 if (mReceiverRestrictedContext != null) {
1882 return mReceiverRestrictedContext;
1883 }
1884 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1885 }
1886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 final void setOuterContext(Context context) {
1888 mOuterContext = context;
1889 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 final Context getOuterContext() {
1892 return mOuterContext;
1893 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 final IBinder getActivityToken() {
1896 return mActivityToken;
1897 }
1898
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08001899 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 int extraPermissions) {
1901 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
1902 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
1903 |extraPermissions;
1904 if ((mode&MODE_WORLD_READABLE) != 0) {
1905 perms |= FileUtils.S_IROTH;
1906 }
1907 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
1908 perms |= FileUtils.S_IWOTH;
1909 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001910 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
1912 + ", perms=0x" + Integer.toHexString(perms));
1913 }
1914 FileUtils.setPermissions(name, perms, -1, -1);
1915 }
1916
Oscar Montemayora8529f62009-11-18 10:14:20 -08001917 private File validateFilePath(String name, boolean createDirectory) {
1918 File dir;
1919 File f;
1920
1921 if (name.charAt(0) == File.separatorChar) {
1922 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
1923 dir = new File(dirPath);
1924 name = name.substring(name.lastIndexOf(File.separatorChar));
1925 f = new File(dir, name);
1926 } else {
1927 dir = getDatabasesDir();
1928 f = makeFilename(dir, name);
1929 }
1930
1931 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
1932 FileUtils.setPermissions(dir.getPath(),
1933 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1934 -1, -1);
1935 }
1936
1937 return f;
1938 }
1939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 private File makeFilename(File base, String name) {
1941 if (name.indexOf(File.separatorChar) < 0) {
1942 return new File(base, name);
1943 }
1944 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08001945 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 }
1947
1948 // ----------------------------------------------------------------------
1949 // ----------------------------------------------------------------------
1950 // ----------------------------------------------------------------------
1951
1952 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001953 private final ActivityThread mMainThread;
1954 private final UserHandle mUser;
1955
1956 public ApplicationContentResolver(
1957 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07001959 mMainThread = Preconditions.checkNotNull(mainThread);
1960 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962
1963 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001964 protected IContentProvider acquireProvider(Context context, String auth) {
1965 return mMainThread.acquireProvider(context, auth, mUser.getIdentifier(), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
1967
1968 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001969 protected IContentProvider acquireExistingProvider(Context context, String auth) {
1970 return mMainThread.acquireExistingProvider(context, auth, mUser.getIdentifier(), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001971 }
1972
1973 @Override
1974 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001975 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001977
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001978 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001979 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
1980 return mMainThread.acquireProvider(c, auth, mUser.getIdentifier(), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001981 }
1982
1983 @Override
1984 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001985 return mMainThread.releaseProvider(icp, false);
1986 }
1987
1988 @Override
1989 public void unstableProviderDied(IContentProvider icp) {
1990 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993}