blob: 4c35a8c6f0d395ad1eb9968d9356412daedbd0ab [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.BroadcastReceiver;
22import android.content.ComponentName;
23import android.content.ContentResolver;
24import android.content.Context;
25import android.content.ContextWrapper;
26import android.content.IContentProvider;
27import android.content.Intent;
28import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070029import android.content.IIntentReceiver;
30import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.ReceiverCallNotAllowedException;
32import android.content.ServiceConnection;
33import android.content.SharedPreferences;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.PackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070038import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070040import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.database.sqlite.SQLiteDatabase;
42import android.database.sqlite.SQLiteDatabase.CursorFactory;
43import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.graphics.drawable.Drawable;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040045import android.hardware.ISerialManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.hardware.SensorManager;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040047import android.hardware.SerialManager;
Jeff Brown25157e42012-04-16 12:13:05 -070048import android.hardware.SystemSensorManager;
Jeff Brownac143512012-04-05 18:57:33 -070049import android.hardware.input.IInputManager;
Jeff Brown9df6e7a2012-04-05 11:49:26 -070050import android.hardware.input.InputManager;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080051import android.hardware.usb.IUsbManager;
52import android.hardware.usb.UsbManager;
Bai Taoa58a8752010-07-13 15:32:16 +080053import android.location.CountryDetector;
54import android.location.ICountryDetector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.location.ILocationManager;
56import android.location.LocationManager;
57import android.media.AudioManager;
58import android.net.ConnectivityManager;
59import android.net.IConnectivityManager;
Jeff Sharkey1a303952011-06-16 13:04:20 -070060import android.net.INetworkPolicyManager;
61import android.net.NetworkPolicyManager;
Irfan Sheriffc9b68512010-04-08 14:12:33 -070062import android.net.ThrottleManager;
63import android.net.IThrottleManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.net.Uri;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070065import android.net.nsd.INsdManager;
66import android.net.nsd.NsdManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.net.wifi.IWifiManager;
68import android.net.wifi.WifiManager;
repo sync55bc5f32011-06-24 14:23:07 -070069import android.net.wifi.p2p.IWifiP2pManager;
70import android.net.wifi.p2p.WifiP2pManager;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080071import android.nfc.NfcManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Binder;
73import android.os.Bundle;
Dan Egnorf18a01c2009-11-12 11:32:50 -080074import android.os.DropBoxManager;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080075import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.FileUtils;
77import android.os.Handler;
78import android.os.IBinder;
79import android.os.IPowerManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070080import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.PowerManager;
82import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070083import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.ServiceManager;
Amith Yamasani742a6712011-05-04 14:49:28 -070085import android.os.UserId;
Jeff Brownc2346132012-04-13 01:55:38 -070086import android.os.SystemVibrator;
San Mehatb1043402010-02-05 08:26:50 -080087import android.os.storage.StorageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.telephony.TelephonyManager;
Dianne Hackborn9f531192010-08-04 17:48:03 -070089import android.content.ClipboardManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.util.AndroidRuntimeException;
91import android.util.Log;
92import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.view.WindowManagerImpl;
svetoslavganov75986cf2009-05-14 22:28:01 -070094import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.inputmethod.InputMethodManager;
satok988323c2011-06-22 16:38:13 +090096import android.view.textservice.TextServicesManager;
Fred Quintana60307342009-03-24 22:48:12 -070097import android.accounts.AccountManager;
98import android.accounts.IAccountManager;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080099import android.app.admin.DevicePolicyManager;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800100import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.io.File;
103import java.io.FileInputStream;
104import java.io.FileNotFoundException;
105import java.io.FileOutputStream;
106import java.io.IOException;
107import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import java.util.ArrayList;
109import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111class ReceiverRestrictedContext extends ContextWrapper {
112 ReceiverRestrictedContext(Context base) {
113 super(base);
114 }
115
116 @Override
117 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
118 return registerReceiver(receiver, filter, null, null);
119 }
120
121 @Override
122 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
123 String broadcastPermission, Handler scheduler) {
124 throw new ReceiverCallNotAllowedException(
125 "IntentReceiver components are not allowed to register to receive intents");
126 //ex.fillInStackTrace();
127 //Log.e("IntentReceiver", ex.getMessage(), ex);
128 //return mContext.registerReceiver(receiver, filter, broadcastPermission,
129 // scheduler);
130 }
131
132 @Override
133 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
134 throw new ReceiverCallNotAllowedException(
135 "IntentReceiver components are not allowed to bind to services");
136 //ex.fillInStackTrace();
137 //Log.e("IntentReceiver", ex.getMessage(), ex);
138 //return mContext.bindService(service, interfaceName, conn, flags);
139 }
140}
141
142/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800143 * Common implementation of Context API, which provides the base
144 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800146class ContextImpl extends Context {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 private final static String TAG = "ApplicationContext";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700148 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700150 private static final HashMap<String, SharedPreferencesImpl> sSharedPrefs =
151 new HashMap<String, SharedPreferencesImpl>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700153 /*package*/ LoadedApk mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700154 private String mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 private Resources mResources;
156 /*package*/ ActivityThread mMainThread;
157 private Context mOuterContext;
158 private IBinder mActivityToken = null;
159 private ApplicationContentResolver mContentResolver;
160 private int mThemeResource = 0;
161 private Resources.Theme mTheme = null;
162 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 private Context mReceiverRestrictedContext = null;
Romain Guy870e09f2009-07-06 16:35:25 -0700164 private boolean mRestricted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 private final Object mSync = new Object();
167
168 private File mDatabasesDir;
169 private File mPreferencesDir;
170 private File mFilesDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 private File mCacheDir;
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800172 private File mObbDir;
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800173 private File mExternalFilesDir;
174 private File mExternalCacheDir;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private static final String[] EMPTY_FILE_LIST = {};
177
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800178 /**
179 * Override this class when the system service constructor needs a
180 * ContextImpl. Else, use StaticServiceFetcher below.
181 */
182 /*package*/ static class ServiceFetcher {
183 int mContextCacheIndex = -1;
184
185 /**
186 * Main entrypoint; only override if you don't need caching.
187 */
188 public Object getService(ContextImpl ctx) {
189 ArrayList<Object> cache = ctx.mServiceCache;
190 Object service;
191 synchronized (cache) {
192 if (cache.size() == 0) {
193 // Initialize the cache vector on first access.
194 // At this point sNextPerContextServiceCacheIndex
195 // is the number of potential services that are
196 // cached per-Context.
197 for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
198 cache.add(null);
199 }
200 } else {
201 service = cache.get(mContextCacheIndex);
202 if (service != null) {
203 return service;
204 }
205 }
206 service = createService(ctx);
207 cache.set(mContextCacheIndex, service);
208 return service;
209 }
210 }
211
212 /**
213 * Override this to create a new per-Context instance of the
214 * service. getService() will handle locking and caching.
215 */
216 public Object createService(ContextImpl ctx) {
217 throw new RuntimeException("Not implemented");
218 }
219 }
220
221 /**
222 * Override this class for services to be cached process-wide.
223 */
224 abstract static class StaticServiceFetcher extends ServiceFetcher {
225 private Object mCachedInstance;
226
227 @Override
228 public final Object getService(ContextImpl unused) {
229 synchronized (StaticServiceFetcher.this) {
230 Object service = mCachedInstance;
231 if (service != null) {
232 return service;
233 }
234 return mCachedInstance = createStaticService();
235 }
236 }
237
238 public abstract Object createStaticService();
239 }
240
241 private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
242 new HashMap<String, ServiceFetcher>();
243
244 private static int sNextPerContextServiceCacheIndex = 0;
245 private static void registerService(String serviceName, ServiceFetcher fetcher) {
246 if (!(fetcher instanceof StaticServiceFetcher)) {
247 fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
248 }
249 SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
250 }
251
252 // This one's defined separately and given a variable name so it
253 // can be re-used by getWallpaperManager(), avoiding a HashMap
254 // lookup.
255 private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() {
256 public Object createService(ContextImpl ctx) {
257 return new WallpaperManager(ctx.getOuterContext(),
258 ctx.mMainThread.getHandler());
259 }};
260
261 static {
262 registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
263 public Object getService(ContextImpl ctx) {
264 return AccessibilityManager.getInstance(ctx);
265 }});
266
267 registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
268 public Object createService(ContextImpl ctx) {
269 IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
270 IAccountManager service = IAccountManager.Stub.asInterface(b);
271 return new AccountManager(ctx, service);
272 }});
273
274 registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
275 public Object createService(ContextImpl ctx) {
276 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
277 }});
278
279 registerService(ALARM_SERVICE, new StaticServiceFetcher() {
280 public Object createStaticService() {
281 IBinder b = ServiceManager.getService(ALARM_SERVICE);
282 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
283 return new AlarmManager(service);
284 }});
285
286 registerService(AUDIO_SERVICE, new ServiceFetcher() {
287 public Object createService(ContextImpl ctx) {
288 return new AudioManager(ctx);
289 }});
290
291 registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
292 public Object createService(ContextImpl ctx) {
293 return new ClipboardManager(ctx.getOuterContext(),
294 ctx.mMainThread.getHandler());
295 }});
296
297 registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
298 public Object createStaticService() {
299 IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
300 return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
301 }});
302
303 registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
304 public Object createStaticService() {
305 IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
306 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
307 }});
308
309 registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
310 public Object createService(ContextImpl ctx) {
311 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
312 }});
313
314 registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
315 public Object createService(ContextImpl ctx) {
316 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
317 }});
318
Nick Pellyd2507462010-12-13 12:22:34 -0800319 registerService(NFC_SERVICE, new ServiceFetcher() {
320 public Object createService(ContextImpl ctx) {
321 return new NfcManager(ctx);
322 }});
323
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800324 registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
325 public Object createStaticService() {
326 return createDropBoxManager();
327 }});
328
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700329 registerService(INPUT_SERVICE, new StaticServiceFetcher() {
330 public Object createStaticService() {
331 return InputManager.getInstance();
Jeff Brownac143512012-04-05 18:57:33 -0700332 }});
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700333
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800334 registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() {
335 public Object createService(ContextImpl ctx) {
336 return InputMethodManager.getInstance(ctx);
337 }});
338
satok988323c2011-06-22 16:38:13 +0900339 registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
340 public Object createService(ContextImpl ctx) {
341 return TextServicesManager.getInstance();
342 }});
343
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800344 registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
345 public Object getService(ContextImpl ctx) {
346 // TODO: why isn't this caching it? It wasn't
347 // before, so I'm preserving the old behavior and
348 // using getService(), instead of createService()
349 // which would do the caching.
350 return new KeyguardManager();
351 }});
352
353 registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
354 public Object createService(ContextImpl ctx) {
355 return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
356 }});
357
358 registerService(LOCATION_SERVICE, new StaticServiceFetcher() {
359 public Object createStaticService() {
360 IBinder b = ServiceManager.getService(LOCATION_SERVICE);
361 return new LocationManager(ILocationManager.Stub.asInterface(b));
362 }});
363
Jeff Sharkey1a303952011-06-16 13:04:20 -0700364 registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
365 @Override
366 public Object createService(ContextImpl ctx) {
367 return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
368 ServiceManager.getService(NETWORK_POLICY_SERVICE)));
369 }
370 });
371
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800372 registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
373 public Object createService(ContextImpl ctx) {
374 final Context outerContext = ctx.getOuterContext();
375 return new NotificationManager(
376 new ContextThemeWrapper(outerContext,
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800377 Resources.selectSystemTheme(0,
378 outerContext.getApplicationInfo().targetSdkVersion,
379 com.android.internal.R.style.Theme_Dialog,
Adam Powell6e90a362011-08-14 16:48:32 -0700380 com.android.internal.R.style.Theme_Holo_Dialog,
381 com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800382 ctx.mMainThread.getHandler());
383 }});
384
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700385 registerService(NSD_SERVICE, new ServiceFetcher() {
386 @Override
387 public Object createService(ContextImpl ctx) {
388 IBinder b = ServiceManager.getService(NSD_SERVICE);
389 INsdManager service = INsdManager.Stub.asInterface(b);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700390 return new NsdManager(ctx.getOuterContext(), service);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700391 }});
392
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800393 // Note: this was previously cached in a static variable, but
394 // constructed using mMainThread.getHandler(), so converting
395 // it to be a regular Context-cached service...
396 registerService(POWER_SERVICE, new ServiceFetcher() {
397 public Object createService(ContextImpl ctx) {
398 IBinder b = ServiceManager.getService(POWER_SERVICE);
399 IPowerManager service = IPowerManager.Stub.asInterface(b);
400 return new PowerManager(service, ctx.mMainThread.getHandler());
401 }});
402
403 registerService(SEARCH_SERVICE, new ServiceFetcher() {
404 public Object createService(ContextImpl ctx) {
405 return new SearchManager(ctx.getOuterContext(),
406 ctx.mMainThread.getHandler());
407 }});
408
409 registerService(SENSOR_SERVICE, new ServiceFetcher() {
410 public Object createService(ContextImpl ctx) {
Jeff Brown25157e42012-04-16 12:13:05 -0700411 return new SystemSensorManager(ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800412 }});
413
414 registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
415 public Object createService(ContextImpl ctx) {
416 return new StatusBarManager(ctx.getOuterContext());
417 }});
418
419 registerService(STORAGE_SERVICE, new ServiceFetcher() {
420 public Object createService(ContextImpl ctx) {
421 try {
422 return new StorageManager(ctx.mMainThread.getHandler().getLooper());
423 } catch (RemoteException rex) {
424 Log.e(TAG, "Failed to create StorageManager", rex);
425 return null;
426 }
427 }});
428
429 registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
430 public Object createService(ContextImpl ctx) {
431 return new TelephonyManager(ctx.getOuterContext());
432 }});
433
434 registerService(THROTTLE_SERVICE, new StaticServiceFetcher() {
435 public Object createStaticService() {
436 IBinder b = ServiceManager.getService(THROTTLE_SERVICE);
437 return new ThrottleManager(IThrottleManager.Stub.asInterface(b));
438 }});
439
440 registerService(UI_MODE_SERVICE, new ServiceFetcher() {
441 public Object createService(ContextImpl ctx) {
442 return new UiModeManager();
443 }});
444
Mike Lockwood3a68b832011-03-08 10:08:59 -0500445 registerService(USB_SERVICE, new ServiceFetcher() {
446 public Object createService(ContextImpl ctx) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500447 IBinder b = ServiceManager.getService(USB_SERVICE);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500448 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500449 }});
450
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400451 registerService(SERIAL_SERVICE, new ServiceFetcher() {
452 public Object createService(ContextImpl ctx) {
453 IBinder b = ServiceManager.getService(SERIAL_SERVICE);
454 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
455 }});
456
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800457 registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
458 public Object createService(ContextImpl ctx) {
Jeff Brownc2346132012-04-13 01:55:38 -0700459 return new SystemVibrator();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800460 }});
461
462 registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
463
464 registerService(WIFI_SERVICE, new ServiceFetcher() {
465 public Object createService(ContextImpl ctx) {
466 IBinder b = ServiceManager.getService(WIFI_SERVICE);
467 IWifiManager service = IWifiManager.Stub.asInterface(b);
468 return new WifiManager(service, ctx.mMainThread.getHandler());
469 }});
470
repo sync55bc5f32011-06-24 14:23:07 -0700471 registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
472 public Object createService(ContextImpl ctx) {
473 IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
474 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
475 return new WifiP2pManager(service);
476 }});
477
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800478 registerService(WINDOW_SERVICE, new ServiceFetcher() {
479 public Object getService(ContextImpl ctx) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700480 return WindowManagerImpl.getDefault(ctx.mPackageInfo.mCompatibilityInfo);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800481 }});
482 }
483
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700484 static ContextImpl getImpl(Context context) {
485 Context nextContext;
486 while ((context instanceof ContextWrapper) &&
487 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
488 context = nextContext;
489 }
490 return (ContextImpl)context;
491 }
492
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800493 // The system service cache for the system services that are
494 // cached per-ContextImpl. Package-scoped to avoid accessor
495 // methods.
496 final ArrayList<Object> mServiceCache = new ArrayList<Object>();
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 @Override
499 public AssetManager getAssets() {
500 return mResources.getAssets();
501 }
502
503 @Override
504 public Resources getResources() {
505 return mResources;
506 }
507
508 @Override
509 public PackageManager getPackageManager() {
510 if (mPackageManager != null) {
511 return mPackageManager;
512 }
513
514 IPackageManager pm = ActivityThread.getPackageManager();
515 if (pm != null) {
516 // Doesn't matter if we make more than one instance.
517 return (mPackageManager = new ApplicationPackageManager(this, pm));
518 }
519
520 return null;
521 }
522
523 @Override
524 public ContentResolver getContentResolver() {
525 return mContentResolver;
526 }
527
528 @Override
529 public Looper getMainLooper() {
530 return mMainThread.getLooper();
531 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 @Override
534 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700535 return (mPackageInfo != null) ?
536 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 @Override
540 public void setTheme(int resid) {
541 mThemeResource = resid;
542 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800545 public int getThemeResId() {
546 return mThemeResource;
547 }
548
549 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 public Resources.Theme getTheme() {
551 if (mTheme == null) {
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800552 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
553 getOuterContext().getApplicationInfo().targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 mTheme = mResources.newTheme();
555 mTheme.applyStyle(mThemeResource, true);
556 }
557 return mTheme;
558 }
559
560 @Override
561 public ClassLoader getClassLoader() {
562 return mPackageInfo != null ?
563 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
564 }
565
566 @Override
567 public String getPackageName() {
568 if (mPackageInfo != null) {
569 return mPackageInfo.getPackageName();
570 }
571 throw new RuntimeException("Not supported in system context");
572 }
573
574 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700575 public ApplicationInfo getApplicationInfo() {
576 if (mPackageInfo != null) {
577 return mPackageInfo.getApplicationInfo();
578 }
579 throw new RuntimeException("Not supported in system context");
580 }
581
582 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 public String getPackageResourcePath() {
584 if (mPackageInfo != null) {
585 return mPackageInfo.getResDir();
586 }
587 throw new RuntimeException("Not supported in system context");
588 }
589
590 @Override
591 public String getPackageCodePath() {
592 if (mPackageInfo != null) {
593 return mPackageInfo.getAppDir();
594 }
595 throw new RuntimeException("Not supported in system context");
596 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200597
Joe Onorato23ecae32009-06-10 17:07:15 -0700598 public File getSharedPrefsFile(String name) {
599 return makeFilename(getPreferencesDir(), name + ".xml");
600 }
601
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 @Override
603 public SharedPreferences getSharedPreferences(String name, int mode) {
604 SharedPreferencesImpl sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 synchronized (sSharedPrefs) {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700606 sp = sSharedPrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700607 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800608 File prefsFile = getSharedPrefsFile(name);
609 sp = new SharedPreferencesImpl(prefsFile, mode);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700610 sSharedPrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700611 return sp;
612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800614 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
615 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
616 // If somebody else (some other process) changed the prefs
617 // file behind our back, we reload it. This has been the
618 // historical (if undocumented) behavior.
619 sp.startReloadIfChangedUnexpectedly();
620 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700621 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 }
623
624 private File getPreferencesDir() {
625 synchronized (mSync) {
626 if (mPreferencesDir == null) {
627 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
628 }
629 return mPreferencesDir;
630 }
631 }
632
633 @Override
634 public FileInputStream openFileInput(String name)
635 throws FileNotFoundException {
636 File f = makeFilename(getFilesDir(), name);
637 return new FileInputStream(f);
638 }
639
640 @Override
641 public FileOutputStream openFileOutput(String name, int mode)
642 throws FileNotFoundException {
643 final boolean append = (mode&MODE_APPEND) != 0;
644 File f = makeFilename(getFilesDir(), name);
645 try {
646 FileOutputStream fos = new FileOutputStream(f, append);
647 setFilePermissionsFromMode(f.getPath(), mode, 0);
648 return fos;
649 } catch (FileNotFoundException e) {
650 }
651
652 File parent = f.getParentFile();
653 parent.mkdir();
654 FileUtils.setPermissions(
655 parent.getPath(),
656 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
657 -1, -1);
658 FileOutputStream fos = new FileOutputStream(f, append);
659 setFilePermissionsFromMode(f.getPath(), mode, 0);
660 return fos;
661 }
662
663 @Override
664 public boolean deleteFile(String name) {
665 File f = makeFilename(getFilesDir(), name);
666 return f.delete();
667 }
668
669 @Override
670 public File getFilesDir() {
671 synchronized (mSync) {
672 if (mFilesDir == null) {
673 mFilesDir = new File(getDataDirFile(), "files");
674 }
675 if (!mFilesDir.exists()) {
676 if(!mFilesDir.mkdirs()) {
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200677 Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 return null;
679 }
680 FileUtils.setPermissions(
681 mFilesDir.getPath(),
682 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
683 -1, -1);
684 }
685 return mFilesDir;
686 }
687 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800690 public File getExternalFilesDir(String type) {
691 synchronized (mSync) {
692 if (mExternalFilesDir == null) {
693 mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory(
694 getPackageName());
695 }
696 if (!mExternalFilesDir.exists()) {
697 try {
698 (new File(Environment.getExternalStorageAndroidDataDir(),
699 ".nomedia")).createNewFile();
700 } catch (IOException e) {
701 }
702 if (!mExternalFilesDir.mkdirs()) {
703 Log.w(TAG, "Unable to create external files directory");
704 return null;
705 }
706 }
707 if (type == null) {
708 return mExternalFilesDir;
709 }
710 File dir = new File(mExternalFilesDir, type);
711 if (!dir.exists()) {
712 if (!dir.mkdirs()) {
713 Log.w(TAG, "Unable to create external media directory " + dir);
714 return null;
715 }
716 }
717 return dir;
718 }
719 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200720
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800721 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800722 public File getObbDir() {
723 synchronized (mSync) {
724 if (mObbDir == null) {
725 mObbDir = Environment.getExternalStorageAppObbDirectory(
726 getPackageName());
727 }
728 return mObbDir;
729 }
730 }
731
732 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 public File getCacheDir() {
734 synchronized (mSync) {
735 if (mCacheDir == null) {
736 mCacheDir = new File(getDataDirFile(), "cache");
737 }
738 if (!mCacheDir.exists()) {
739 if(!mCacheDir.mkdirs()) {
740 Log.w(TAG, "Unable to create cache directory");
741 return null;
742 }
743 FileUtils.setPermissions(
744 mCacheDir.getPath(),
745 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
746 -1, -1);
747 }
748 }
749 return mCacheDir;
750 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200751
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800752 @Override
753 public File getExternalCacheDir() {
754 synchronized (mSync) {
755 if (mExternalCacheDir == null) {
756 mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory(
757 getPackageName());
758 }
759 if (!mExternalCacheDir.exists()) {
760 try {
761 (new File(Environment.getExternalStorageAndroidDataDir(),
762 ".nomedia")).createNewFile();
763 } catch (IOException e) {
764 }
765 if (!mExternalCacheDir.mkdirs()) {
766 Log.w(TAG, "Unable to create external cache directory");
767 return null;
768 }
769 }
770 return mExternalCacheDir;
771 }
772 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 @Override
775 public File getFileStreamPath(String name) {
776 return makeFilename(getFilesDir(), name);
777 }
778
779 @Override
780 public String[] fileList() {
781 final String[] list = getFilesDir().list();
782 return (list != null) ? list : EMPTY_FILE_LIST;
783 }
784
785 @Override
786 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700787 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800788 }
789
790 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700791 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
792 DatabaseErrorHandler errorHandler) {
793 File f = validateFilePath(name, true);
Jeff Brown47847f32012-03-22 19:13:11 -0700794 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
795 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
796 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
797 }
798 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700799 setFilePermissionsFromMode(f.getPath(), mode, 0);
800 return db;
801 }
802
803 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 public boolean deleteDatabase(String name) {
805 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800806 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800807 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 } catch (Exception e) {
809 }
810 return false;
811 }
812
813 @Override
814 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800815 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817
818 @Override
819 public String[] databaseList() {
820 final String[] list = getDatabasesDir().list();
821 return (list != null) ? list : EMPTY_FILE_LIST;
822 }
823
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 private File getDatabasesDir() {
826 synchronized (mSync) {
827 if (mDatabasesDir == null) {
828 mDatabasesDir = new File(getDataDirFile(), "databases");
829 }
830 if (mDatabasesDir.getPath().equals("databases")) {
831 mDatabasesDir = new File("/data/system");
832 }
833 return mDatabasesDir;
834 }
835 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 @Override
838 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700839 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 }
841
842 @Override
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700843 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700844 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
846
847 @Override
848 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700849 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
851
852 @Override
853 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700854 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 }
856
857 @Override
858 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700859 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 }
861
862 @Override
863 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700864 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
866
867 @Override
868 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700869 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871
872 @Override
873 public void startActivity(Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700874 startActivity(intent, null);
875 }
876
877 @Override
878 public void startActivity(Intent intent, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
880 throw new AndroidRuntimeException(
881 "Calling startActivity() from outside of an Activity "
882 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
883 + " Is this really what you want?");
884 }
885 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700886 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700887 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889
890 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800891 public void startActivities(Intent[] intents) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700892 startActivities(intents, null);
893 }
894
895 @Override
896 public void startActivities(Intent[] intents, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800897 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
898 throw new AndroidRuntimeException(
899 "Calling startActivities() from outside of an Activity "
900 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
901 + " Is this really what you want?");
902 }
903 mMainThread.getInstrumentation().execStartActivities(
904 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700905 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800906 }
907
908 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700909 public void startIntentSender(IntentSender intent,
910 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
911 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700912 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
913 }
914
915 @Override
916 public void startIntentSender(IntentSender intent, Intent fillInIntent,
917 int flagsMask, int flagsValues, int extraFlags, Bundle options)
918 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700919 try {
920 String resolvedType = null;
921 if (fillInIntent != null) {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400922 fillInIntent.setAllowFds(false);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700923 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
924 }
925 int result = ActivityManagerNative.getDefault()
926 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
927 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700928 0, flagsMask, flagsValues, options);
929 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700930 throw new IntentSender.SendIntentException();
931 }
932 Instrumentation.checkStartActivityResult(result, null);
933 } catch (RemoteException e) {
934 }
935 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200936
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700937 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 public void sendBroadcast(Intent intent) {
939 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
940 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400941 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 ActivityManagerNative.getDefault().broadcastIntent(
943 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700944 Activity.RESULT_OK, null, null, null, false, false,
945 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 } catch (RemoteException e) {
947 }
948 }
949
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800950 /** @hide */
951 @Override
952 public void sendBroadcast(Intent intent, int userId) {
953 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
954 try {
955 intent.setAllowFds(false);
956 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
957 intent, resolvedType, null, Activity.RESULT_OK, null, null, null, false, false,
958 userId);
959 } catch (RemoteException e) {
960 }
961 }
962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 @Override
964 public void sendBroadcast(Intent intent, String receiverPermission) {
965 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
966 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400967 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 ActivityManagerNative.getDefault().broadcastIntent(
969 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700970 Activity.RESULT_OK, null, null, receiverPermission, false, false,
971 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 } catch (RemoteException e) {
973 }
974 }
975
976 @Override
977 public void sendOrderedBroadcast(Intent intent,
978 String receiverPermission) {
979 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
980 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400981 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 ActivityManagerNative.getDefault().broadcastIntent(
983 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700984 Activity.RESULT_OK, null, null, receiverPermission, true, false,
985 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 } catch (RemoteException e) {
987 }
988 }
989
990 @Override
991 public void sendOrderedBroadcast(Intent intent,
992 String receiverPermission, BroadcastReceiver resultReceiver,
993 Handler scheduler, int initialCode, String initialData,
994 Bundle initialExtras) {
995 IIntentReceiver rd = null;
996 if (resultReceiver != null) {
997 if (mPackageInfo != null) {
998 if (scheduler == null) {
999 scheduler = mMainThread.getHandler();
1000 }
1001 rd = mPackageInfo.getReceiverDispatcher(
1002 resultReceiver, getOuterContext(), scheduler,
1003 mMainThread.getInstrumentation(), false);
1004 } else {
1005 if (scheduler == null) {
1006 scheduler = mMainThread.getHandler();
1007 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001008 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1010 }
1011 }
1012 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1013 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001014 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 ActivityManagerNative.getDefault().broadcastIntent(
1016 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1017 initialCode, initialData, initialExtras, receiverPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -07001018 true, false, Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 } catch (RemoteException e) {
1020 }
1021 }
1022
1023 @Override
1024 public void sendStickyBroadcast(Intent intent) {
1025 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1026 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001027 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 ActivityManagerNative.getDefault().broadcastIntent(
1029 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001030 Activity.RESULT_OK, null, null, null, false, true,
1031 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 } catch (RemoteException e) {
1033 }
1034 }
1035
1036 @Override
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001037 public void sendStickyOrderedBroadcast(Intent intent,
1038 BroadcastReceiver resultReceiver,
1039 Handler scheduler, int initialCode, String initialData,
1040 Bundle initialExtras) {
1041 IIntentReceiver rd = null;
1042 if (resultReceiver != null) {
1043 if (mPackageInfo != null) {
1044 if (scheduler == null) {
1045 scheduler = mMainThread.getHandler();
1046 }
1047 rd = mPackageInfo.getReceiverDispatcher(
1048 resultReceiver, getOuterContext(), scheduler,
1049 mMainThread.getInstrumentation(), false);
1050 } else {
1051 if (scheduler == null) {
1052 scheduler = mMainThread.getHandler();
1053 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001054 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001055 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1056 }
1057 }
1058 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1059 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001060 intent.setAllowFds(false);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001061 ActivityManagerNative.getDefault().broadcastIntent(
1062 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1063 initialCode, initialData, initialExtras, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001064 true, true, Binder.getOrigCallingUser());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001065 } catch (RemoteException e) {
1066 }
1067 }
1068
1069 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 public void removeStickyBroadcast(Intent intent) {
1071 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1072 if (resolvedType != null) {
1073 intent = new Intent(intent);
1074 intent.setDataAndType(intent.getData(), resolvedType);
1075 }
1076 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001077 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 ActivityManagerNative.getDefault().unbroadcastIntent(
Amith Yamasani742a6712011-05-04 14:49:28 -07001079 mMainThread.getApplicationThread(), intent, Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 } catch (RemoteException e) {
1081 }
1082 }
1083
1084 @Override
1085 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1086 return registerReceiver(receiver, filter, null, null);
1087 }
1088
1089 @Override
1090 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1091 String broadcastPermission, Handler scheduler) {
1092 return registerReceiverInternal(receiver, filter, broadcastPermission,
1093 scheduler, getOuterContext());
1094 }
1095
1096 private Intent registerReceiverInternal(BroadcastReceiver receiver,
1097 IntentFilter filter, String broadcastPermission,
1098 Handler scheduler, Context context) {
1099 IIntentReceiver rd = null;
1100 if (receiver != null) {
1101 if (mPackageInfo != null && context != null) {
1102 if (scheduler == null) {
1103 scheduler = mMainThread.getHandler();
1104 }
1105 rd = mPackageInfo.getReceiverDispatcher(
1106 receiver, context, scheduler,
1107 mMainThread.getInstrumentation(), true);
1108 } else {
1109 if (scheduler == null) {
1110 scheduler = mMainThread.getHandler();
1111 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001112 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001113 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115 }
1116 try {
1117 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001118 mMainThread.getApplicationThread(), mBasePackageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 rd, filter, broadcastPermission);
1120 } catch (RemoteException e) {
1121 return null;
1122 }
1123 }
1124
1125 @Override
1126 public void unregisterReceiver(BroadcastReceiver receiver) {
1127 if (mPackageInfo != null) {
1128 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1129 getOuterContext(), receiver);
1130 try {
1131 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1132 } catch (RemoteException e) {
1133 }
1134 } else {
1135 throw new RuntimeException("Not supported in system context");
1136 }
1137 }
1138
1139 @Override
1140 public ComponentName startService(Intent service) {
1141 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001142 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 ComponentName cn = ActivityManagerNative.getDefault().startService(
1144 mMainThread.getApplicationThread(), service,
1145 service.resolveTypeIfNeeded(getContentResolver()));
1146 if (cn != null && cn.getPackageName().equals("!")) {
1147 throw new SecurityException(
1148 "Not allowed to start service " + service
1149 + " without permission " + cn.getClassName());
1150 }
1151 return cn;
1152 } catch (RemoteException e) {
1153 return null;
1154 }
1155 }
1156
1157 @Override
1158 public boolean stopService(Intent service) {
1159 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001160 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 int res = ActivityManagerNative.getDefault().stopService(
1162 mMainThread.getApplicationThread(), service,
1163 service.resolveTypeIfNeeded(getContentResolver()));
1164 if (res < 0) {
1165 throw new SecurityException(
1166 "Not allowed to stop service " + service);
1167 }
1168 return res != 0;
1169 } catch (RemoteException e) {
1170 return false;
1171 }
1172 }
1173
1174 @Override
1175 public boolean bindService(Intent service, ServiceConnection conn,
1176 int flags) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001177 return bindService(service, conn, flags, UserId.getUserId(Process.myUid()));
1178 }
1179
1180 /** @hide */
1181 @Override
1182 public boolean bindService(Intent service, ServiceConnection conn, int flags, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 IServiceConnection sd;
1184 if (mPackageInfo != null) {
1185 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1186 mMainThread.getHandler(), flags);
1187 } else {
1188 throw new RuntimeException("Not supported in system context");
1189 }
1190 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001191 IBinder token = getActivityToken();
1192 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1193 && mPackageInfo.getApplicationInfo().targetSdkVersion
1194 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1195 flags |= BIND_WAIVE_PRIORITY;
1196 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001197 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 int res = ActivityManagerNative.getDefault().bindService(
1199 mMainThread.getApplicationThread(), getActivityToken(),
1200 service, service.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001201 sd, flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 if (res < 0) {
1203 throw new SecurityException(
1204 "Not allowed to bind to service " + service);
1205 }
1206 return res != 0;
1207 } catch (RemoteException e) {
1208 return false;
1209 }
1210 }
1211
1212 @Override
1213 public void unbindService(ServiceConnection conn) {
1214 if (mPackageInfo != null) {
1215 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1216 getOuterContext(), conn);
1217 try {
1218 ActivityManagerNative.getDefault().unbindService(sd);
1219 } catch (RemoteException e) {
1220 }
1221 } else {
1222 throw new RuntimeException("Not supported in system context");
1223 }
1224 }
1225
1226 @Override
1227 public boolean startInstrumentation(ComponentName className,
1228 String profileFile, Bundle arguments) {
1229 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001230 if (arguments != null) {
1231 arguments.setAllowFds(false);
1232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 return ActivityManagerNative.getDefault().startInstrumentation(
1234 className, profileFile, 0, arguments, null);
1235 } catch (RemoteException e) {
1236 // System has crashed, nothing we can do.
1237 }
1238 return false;
1239 }
1240
1241 @Override
1242 public Object getSystemService(String name) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001243 ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
1244 return fetcher == null ? null : fetcher.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
1246
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001247 private WallpaperManager getWallpaperManager() {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001248 return (WallpaperManager) WALLPAPER_FETCHER.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 }
1250
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001251 /* package */ static DropBoxManager createDropBoxManager() {
1252 IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
1253 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001254 if (service == null) {
1255 // Don't return a DropBoxManager that will NPE upon use.
1256 // This also avoids caching a broken DropBoxManager in
1257 // getDropBoxManager during early boot, before the
1258 // DROPBOX_SERVICE is registered.
1259 return null;
1260 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001261 return new DropBoxManager(service);
1262 }
1263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 @Override
1265 public int checkPermission(String permission, int pid, int uid) {
1266 if (permission == null) {
1267 throw new IllegalArgumentException("permission is null");
1268 }
1269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 try {
1271 return ActivityManagerNative.getDefault().checkPermission(
1272 permission, pid, uid);
1273 } catch (RemoteException e) {
1274 return PackageManager.PERMISSION_DENIED;
1275 }
1276 }
1277
1278 @Override
1279 public int checkCallingPermission(String permission) {
1280 if (permission == null) {
1281 throw new IllegalArgumentException("permission is null");
1282 }
1283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 int pid = Binder.getCallingPid();
1285 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001286 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 }
1288 return PackageManager.PERMISSION_DENIED;
1289 }
1290
1291 @Override
1292 public int checkCallingOrSelfPermission(String permission) {
1293 if (permission == null) {
1294 throw new IllegalArgumentException("permission is null");
1295 }
1296
1297 return checkPermission(permission, Binder.getCallingPid(),
1298 Binder.getCallingUid());
1299 }
1300
1301 private void enforce(
1302 String permission, int resultOfCheck,
1303 boolean selfToo, int uid, String message) {
1304 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1305 throw new SecurityException(
1306 (message != null ? (message + ": ") : "") +
1307 (selfToo
1308 ? "Neither user " + uid + " nor current process has "
1309 : "User " + uid + " does not have ") +
1310 permission +
1311 ".");
1312 }
1313 }
1314
1315 public void enforcePermission(
1316 String permission, int pid, int uid, String message) {
1317 enforce(permission,
1318 checkPermission(permission, pid, uid),
1319 false,
1320 uid,
1321 message);
1322 }
1323
1324 public void enforceCallingPermission(String permission, String message) {
1325 enforce(permission,
1326 checkCallingPermission(permission),
1327 false,
1328 Binder.getCallingUid(),
1329 message);
1330 }
1331
1332 public void enforceCallingOrSelfPermission(
1333 String permission, String message) {
1334 enforce(permission,
1335 checkCallingOrSelfPermission(permission),
1336 true,
1337 Binder.getCallingUid(),
1338 message);
1339 }
1340
1341 @Override
1342 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1343 try {
1344 ActivityManagerNative.getDefault().grantUriPermission(
1345 mMainThread.getApplicationThread(), toPackage, uri,
1346 modeFlags);
1347 } catch (RemoteException e) {
1348 }
1349 }
1350
1351 @Override
1352 public void revokeUriPermission(Uri uri, int modeFlags) {
1353 try {
1354 ActivityManagerNative.getDefault().revokeUriPermission(
1355 mMainThread.getApplicationThread(), uri,
1356 modeFlags);
1357 } catch (RemoteException e) {
1358 }
1359 }
1360
1361 @Override
1362 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 try {
1364 return ActivityManagerNative.getDefault().checkUriPermission(
1365 uri, pid, uid, modeFlags);
1366 } catch (RemoteException e) {
1367 return PackageManager.PERMISSION_DENIED;
1368 }
1369 }
1370
1371 @Override
1372 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 int pid = Binder.getCallingPid();
1374 if (pid != Process.myPid()) {
1375 return checkUriPermission(uri, pid,
1376 Binder.getCallingUid(), modeFlags);
1377 }
1378 return PackageManager.PERMISSION_DENIED;
1379 }
1380
1381 @Override
1382 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1383 return checkUriPermission(uri, Binder.getCallingPid(),
1384 Binder.getCallingUid(), modeFlags);
1385 }
1386
1387 @Override
1388 public int checkUriPermission(Uri uri, String readPermission,
1389 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001390 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1392 + readPermission + " writePermission=" + writePermission
1393 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1394 }
1395 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1396 if (readPermission == null
1397 || checkPermission(readPermission, pid, uid)
1398 == PackageManager.PERMISSION_GRANTED) {
1399 return PackageManager.PERMISSION_GRANTED;
1400 }
1401 }
1402 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1403 if (writePermission == null
1404 || checkPermission(writePermission, pid, uid)
1405 == PackageManager.PERMISSION_GRANTED) {
1406 return PackageManager.PERMISSION_GRANTED;
1407 }
1408 }
1409 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1410 : PackageManager.PERMISSION_DENIED;
1411 }
1412
1413 private String uriModeFlagToString(int uriModeFlags) {
1414 switch (uriModeFlags) {
1415 case Intent.FLAG_GRANT_READ_URI_PERMISSION |
1416 Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1417 return "read and write";
1418 case Intent.FLAG_GRANT_READ_URI_PERMISSION:
1419 return "read";
1420 case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1421 return "write";
1422 }
1423 throw new IllegalArgumentException(
1424 "Unknown permission mode flags: " + uriModeFlags);
1425 }
1426
1427 private void enforceForUri(
1428 int modeFlags, int resultOfCheck, boolean selfToo,
1429 int uid, Uri uri, String message) {
1430 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1431 throw new SecurityException(
1432 (message != null ? (message + ": ") : "") +
1433 (selfToo
1434 ? "Neither user " + uid + " nor current process has "
1435 : "User " + uid + " does not have ") +
1436 uriModeFlagToString(modeFlags) +
1437 " permission on " +
1438 uri +
1439 ".");
1440 }
1441 }
1442
1443 public void enforceUriPermission(
1444 Uri uri, int pid, int uid, int modeFlags, String message) {
1445 enforceForUri(
1446 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1447 false, uid, uri, message);
1448 }
1449
1450 public void enforceCallingUriPermission(
1451 Uri uri, int modeFlags, String message) {
1452 enforceForUri(
1453 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001454 false,
1455 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 }
1457
1458 public void enforceCallingOrSelfUriPermission(
1459 Uri uri, int modeFlags, String message) {
1460 enforceForUri(
1461 modeFlags,
1462 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1463 Binder.getCallingUid(), uri, message);
1464 }
1465
1466 public void enforceUriPermission(
1467 Uri uri, String readPermission, String writePermission,
1468 int pid, int uid, int modeFlags, String message) {
1469 enforceForUri(modeFlags,
1470 checkUriPermission(
1471 uri, readPermission, writePermission, pid, uid,
1472 modeFlags),
1473 false,
1474 uid,
1475 uri,
1476 message);
1477 }
1478
1479 @Override
1480 public Context createPackageContext(String packageName, int flags)
1481 throws PackageManager.NameNotFoundException {
1482 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001483 final ContextImpl context = new ContextImpl(mMainThread.getSystemContext());
1484 context.mBasePackageName = mBasePackageName;
1485 return context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 }
1487
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001488 LoadedApk pi =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001489 mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 if (pi != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001491 ContextImpl c = new ContextImpl();
Romain Guy870e09f2009-07-06 16:35:25 -07001492 c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001493 c.init(pi, null, mMainThread, mResources, mBasePackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 if (c.mResources != null) {
1495 return c;
1496 }
1497 }
1498
1499 // Should be a better exception.
1500 throw new PackageManager.NameNotFoundException(
1501 "Application package " + packageName + " not found");
1502 }
1503
Romain Guy870e09f2009-07-06 16:35:25 -07001504 @Override
1505 public boolean isRestricted() {
1506 return mRestricted;
1507 }
1508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 private File getDataDirFile() {
1510 if (mPackageInfo != null) {
1511 return mPackageInfo.getDataDirFile();
1512 }
1513 throw new RuntimeException("Not supported in system context");
1514 }
1515
1516 @Override
1517 public File getDir(String name, int mode) {
1518 name = "app_" + name;
1519 File file = makeFilename(getDataDirFile(), name);
1520 if (!file.exists()) {
1521 file.mkdir();
1522 setFilePermissionsFromMode(file.getPath(), mode,
1523 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1524 }
1525 return file;
1526 }
1527
Dianne Hackborn21556372010-02-04 16:34:40 -08001528 static ContextImpl createSystemContext(ActivityThread mainThread) {
1529 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 context.init(Resources.getSystem(), mainThread);
1531 return context;
1532 }
1533
Dianne Hackborn21556372010-02-04 16:34:40 -08001534 ContextImpl() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 mOuterContext = this;
1536 }
1537
1538 /**
1539 * Create a new ApplicationContext from an existing one. The new one
1540 * works and operates the same as the one it is copying.
1541 *
1542 * @param context Existing application context.
1543 */
Dianne Hackborn21556372010-02-04 16:34:40 -08001544 public ContextImpl(ContextImpl context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 mPackageInfo = context.mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001546 mBasePackageName = context.mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 mResources = context.mResources;
1548 mMainThread = context.mMainThread;
1549 mContentResolver = context.mContentResolver;
1550 mOuterContext = this;
1551 }
1552
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001553 final void init(LoadedApk packageInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 IBinder activityToken, ActivityThread mainThread) {
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001555 init(packageInfo, activityToken, mainThread, null, null);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001556 }
1557
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001558 final void init(LoadedApk packageInfo,
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001559 IBinder activityToken, ActivityThread mainThread,
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001560 Resources container, String basePackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 mPackageInfo = packageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001562 mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 mResources = mPackageInfo.getResources(mainThread);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001564
Dianne Hackborn559a7872010-04-07 18:19:41 -07001565 if (mResources != null && container != null
1566 && container.getCompatibilityInfo().applicationScale !=
1567 mResources.getCompatibilityInfo().applicationScale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001568 if (DEBUG) {
1569 Log.d(TAG, "loaded context has different scaling. Using container's" +
1570 " compatiblity info:" + container.getDisplayMetrics());
1571 }
1572 mResources = mainThread.getTopLevelResources(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001573 mPackageInfo.getResDir(), container.getCompatibilityInfo());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 mMainThread = mainThread;
1576 mContentResolver = new ApplicationContentResolver(this, mainThread);
1577
1578 setActivityToken(activityToken);
1579 }
1580
1581 final void init(Resources resources, ActivityThread mainThread) {
1582 mPackageInfo = null;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001583 mBasePackageName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 mResources = resources;
1585 mMainThread = mainThread;
1586 mContentResolver = new ApplicationContentResolver(this, mainThread);
1587 }
1588
1589 final void scheduleFinalCleanup(String who, String what) {
1590 mMainThread.scheduleContextCleanup(this, who, what);
1591 }
1592
1593 final void performFinalCleanup(String who, String what) {
1594 //Log.i(TAG, "Cleanup up context: " + this);
1595 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1596 }
1597
1598 final Context getReceiverRestrictedContext() {
1599 if (mReceiverRestrictedContext != null) {
1600 return mReceiverRestrictedContext;
1601 }
1602 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1603 }
1604
1605 final void setActivityToken(IBinder token) {
1606 mActivityToken = token;
1607 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 final void setOuterContext(Context context) {
1610 mOuterContext = context;
1611 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 final Context getOuterContext() {
1614 return mOuterContext;
1615 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 final IBinder getActivityToken() {
1618 return mActivityToken;
1619 }
1620
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08001621 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 int extraPermissions) {
1623 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
1624 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
1625 |extraPermissions;
1626 if ((mode&MODE_WORLD_READABLE) != 0) {
1627 perms |= FileUtils.S_IROTH;
1628 }
1629 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
1630 perms |= FileUtils.S_IWOTH;
1631 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001632 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
1634 + ", perms=0x" + Integer.toHexString(perms));
1635 }
1636 FileUtils.setPermissions(name, perms, -1, -1);
1637 }
1638
Oscar Montemayora8529f62009-11-18 10:14:20 -08001639 private File validateFilePath(String name, boolean createDirectory) {
1640 File dir;
1641 File f;
1642
1643 if (name.charAt(0) == File.separatorChar) {
1644 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
1645 dir = new File(dirPath);
1646 name = name.substring(name.lastIndexOf(File.separatorChar));
1647 f = new File(dir, name);
1648 } else {
1649 dir = getDatabasesDir();
1650 f = makeFilename(dir, name);
1651 }
1652
1653 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
1654 FileUtils.setPermissions(dir.getPath(),
1655 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1656 -1, -1);
1657 }
1658
1659 return f;
1660 }
1661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 private File makeFilename(File base, String name) {
1663 if (name.indexOf(File.separatorChar) < 0) {
1664 return new File(base, name);
1665 }
1666 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08001667 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
1669
1670 // ----------------------------------------------------------------------
1671 // ----------------------------------------------------------------------
1672 // ----------------------------------------------------------------------
1673
1674 private static final class ApplicationContentResolver extends ContentResolver {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001675 public ApplicationContentResolver(Context context, ActivityThread mainThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 super(context);
1677 mMainThread = mainThread;
1678 }
1679
1680 @Override
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001681 protected IContentProvider acquireProvider(Context context, String name) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001682 return mMainThread.acquireProvider(context, name, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
1684
1685 @Override
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001686 protected IContentProvider acquireExistingProvider(Context context, String name) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001687 return mMainThread.acquireExistingProvider(context, name, true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001688 }
1689
1690 @Override
1691 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001692 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001694
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001695 @Override
1696 protected IContentProvider acquireUnstableProvider(Context c, String name) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001697 return mMainThread.acquireProvider(c, name, false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001698 }
1699
1700 @Override
1701 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001702 return mMainThread.releaseProvider(icp, false);
1703 }
1704
1705 @Override
1706 public void unstableProviderDied(IContentProvider icp) {
1707 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001708 }
1709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 private final ActivityThread mMainThread;
1711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712}