blob: 7043a73dc47ac8fe2f6fc8c5698a5c3ccc6bf5f3 [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;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080048import android.hardware.usb.IUsbManager;
49import android.hardware.usb.UsbManager;
Bai Taoa58a8752010-07-13 15:32:16 +080050import android.location.CountryDetector;
51import android.location.ICountryDetector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.location.ILocationManager;
53import android.location.LocationManager;
54import android.media.AudioManager;
55import android.net.ConnectivityManager;
56import android.net.IConnectivityManager;
Jeff Sharkey1a303952011-06-16 13:04:20 -070057import android.net.INetworkPolicyManager;
58import android.net.NetworkPolicyManager;
Irfan Sheriffc9b68512010-04-08 14:12:33 -070059import android.net.ThrottleManager;
60import android.net.IThrottleManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.net.Uri;
62import android.net.wifi.IWifiManager;
63import android.net.wifi.WifiManager;
repo sync55bc5f32011-06-24 14:23:07 -070064import android.net.wifi.p2p.IWifiP2pManager;
65import android.net.wifi.p2p.WifiP2pManager;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080066import android.nfc.NfcManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.os.Binder;
68import android.os.Bundle;
Dan Egnorf18a01c2009-11-12 11:32:50 -080069import android.os.DropBoxManager;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080070import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.FileUtils;
72import android.os.Handler;
73import android.os.IBinder;
74import android.os.IPowerManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070075import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.os.PowerManager;
77import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070078import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.ServiceManager;
Amith Yamasani742a6712011-05-04 14:49:28 -070080import android.os.UserId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.os.Vibrator;
San Mehatb1043402010-02-05 08:26:50 -080082import android.os.storage.StorageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.telephony.TelephonyManager;
Dianne Hackborn9f531192010-08-04 17:48:03 -070084import android.content.ClipboardManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.AndroidRuntimeException;
86import android.util.Log;
87import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.view.WindowManagerImpl;
svetoslavganov75986cf2009-05-14 22:28:01 -070089import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.inputmethod.InputMethodManager;
satok988323c2011-06-22 16:38:13 +090091import android.view.textservice.TextServicesManager;
Fred Quintana60307342009-03-24 22:48:12 -070092import android.accounts.AccountManager;
93import android.accounts.IAccountManager;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080094import android.app.admin.DevicePolicyManager;
Dan Egnorf18a01c2009-11-12 11:32:50 -080095import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -070096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.io.File;
98import java.io.FileInputStream;
99import java.io.FileNotFoundException;
100import java.io.FileOutputStream;
101import java.io.IOException;
102import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103import java.util.ArrayList;
104import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106class ReceiverRestrictedContext extends ContextWrapper {
107 ReceiverRestrictedContext(Context base) {
108 super(base);
109 }
110
111 @Override
112 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
113 return registerReceiver(receiver, filter, null, null);
114 }
115
116 @Override
117 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
118 String broadcastPermission, Handler scheduler) {
119 throw new ReceiverCallNotAllowedException(
120 "IntentReceiver components are not allowed to register to receive intents");
121 //ex.fillInStackTrace();
122 //Log.e("IntentReceiver", ex.getMessage(), ex);
123 //return mContext.registerReceiver(receiver, filter, broadcastPermission,
124 // scheduler);
125 }
126
127 @Override
128 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
129 throw new ReceiverCallNotAllowedException(
130 "IntentReceiver components are not allowed to bind to services");
131 //ex.fillInStackTrace();
132 //Log.e("IntentReceiver", ex.getMessage(), ex);
133 //return mContext.bindService(service, interfaceName, conn, flags);
134 }
135}
136
137/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800138 * Common implementation of Context API, which provides the base
139 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800141class ContextImpl extends Context {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 private final static String TAG = "ApplicationContext";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700143 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700145 private static final HashMap<String, SharedPreferencesImpl> sSharedPrefs =
146 new HashMap<String, SharedPreferencesImpl>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700148 /*package*/ LoadedApk mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700149 private String mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 private Resources mResources;
151 /*package*/ ActivityThread mMainThread;
152 private Context mOuterContext;
153 private IBinder mActivityToken = null;
154 private ApplicationContentResolver mContentResolver;
155 private int mThemeResource = 0;
156 private Resources.Theme mTheme = null;
157 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 private Context mReceiverRestrictedContext = null;
Romain Guy870e09f2009-07-06 16:35:25 -0700159 private boolean mRestricted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
161 private final Object mSync = new Object();
162
163 private File mDatabasesDir;
164 private File mPreferencesDir;
165 private File mFilesDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 private File mCacheDir;
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800167 private File mObbDir;
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800168 private File mExternalFilesDir;
169 private File mExternalCacheDir;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 private static final String[] EMPTY_FILE_LIST = {};
172
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800173 /**
174 * Override this class when the system service constructor needs a
175 * ContextImpl. Else, use StaticServiceFetcher below.
176 */
177 /*package*/ static class ServiceFetcher {
178 int mContextCacheIndex = -1;
179
180 /**
181 * Main entrypoint; only override if you don't need caching.
182 */
183 public Object getService(ContextImpl ctx) {
184 ArrayList<Object> cache = ctx.mServiceCache;
185 Object service;
186 synchronized (cache) {
187 if (cache.size() == 0) {
188 // Initialize the cache vector on first access.
189 // At this point sNextPerContextServiceCacheIndex
190 // is the number of potential services that are
191 // cached per-Context.
192 for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
193 cache.add(null);
194 }
195 } else {
196 service = cache.get(mContextCacheIndex);
197 if (service != null) {
198 return service;
199 }
200 }
201 service = createService(ctx);
202 cache.set(mContextCacheIndex, service);
203 return service;
204 }
205 }
206
207 /**
208 * Override this to create a new per-Context instance of the
209 * service. getService() will handle locking and caching.
210 */
211 public Object createService(ContextImpl ctx) {
212 throw new RuntimeException("Not implemented");
213 }
214 }
215
216 /**
217 * Override this class for services to be cached process-wide.
218 */
219 abstract static class StaticServiceFetcher extends ServiceFetcher {
220 private Object mCachedInstance;
221
222 @Override
223 public final Object getService(ContextImpl unused) {
224 synchronized (StaticServiceFetcher.this) {
225 Object service = mCachedInstance;
226 if (service != null) {
227 return service;
228 }
229 return mCachedInstance = createStaticService();
230 }
231 }
232
233 public abstract Object createStaticService();
234 }
235
236 private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
237 new HashMap<String, ServiceFetcher>();
238
239 private static int sNextPerContextServiceCacheIndex = 0;
240 private static void registerService(String serviceName, ServiceFetcher fetcher) {
241 if (!(fetcher instanceof StaticServiceFetcher)) {
242 fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
243 }
244 SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
245 }
246
247 // This one's defined separately and given a variable name so it
248 // can be re-used by getWallpaperManager(), avoiding a HashMap
249 // lookup.
250 private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() {
251 public Object createService(ContextImpl ctx) {
252 return new WallpaperManager(ctx.getOuterContext(),
253 ctx.mMainThread.getHandler());
254 }};
255
256 static {
257 registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
258 public Object getService(ContextImpl ctx) {
259 return AccessibilityManager.getInstance(ctx);
260 }});
261
262 registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
263 public Object createService(ContextImpl ctx) {
264 IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
265 IAccountManager service = IAccountManager.Stub.asInterface(b);
266 return new AccountManager(ctx, service);
267 }});
268
269 registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
270 public Object createService(ContextImpl ctx) {
271 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
272 }});
273
274 registerService(ALARM_SERVICE, new StaticServiceFetcher() {
275 public Object createStaticService() {
276 IBinder b = ServiceManager.getService(ALARM_SERVICE);
277 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
278 return new AlarmManager(service);
279 }});
280
281 registerService(AUDIO_SERVICE, new ServiceFetcher() {
282 public Object createService(ContextImpl ctx) {
283 return new AudioManager(ctx);
284 }});
285
286 registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
287 public Object createService(ContextImpl ctx) {
288 return new ClipboardManager(ctx.getOuterContext(),
289 ctx.mMainThread.getHandler());
290 }});
291
292 registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
293 public Object createStaticService() {
294 IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
295 return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
296 }});
297
298 registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
299 public Object createStaticService() {
300 IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
301 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
302 }});
303
304 registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
305 public Object createService(ContextImpl ctx) {
306 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
307 }});
308
309 registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
310 public Object createService(ContextImpl ctx) {
311 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
312 }});
313
Nick Pellyd2507462010-12-13 12:22:34 -0800314 registerService(NFC_SERVICE, new ServiceFetcher() {
315 public Object createService(ContextImpl ctx) {
316 return new NfcManager(ctx);
317 }});
318
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800319 registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
320 public Object createStaticService() {
321 return createDropBoxManager();
322 }});
323
324 registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() {
325 public Object createService(ContextImpl ctx) {
326 return InputMethodManager.getInstance(ctx);
327 }});
328
satok988323c2011-06-22 16:38:13 +0900329 registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
330 public Object createService(ContextImpl ctx) {
331 return TextServicesManager.getInstance();
332 }});
333
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800334 registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
335 public Object getService(ContextImpl ctx) {
336 // TODO: why isn't this caching it? It wasn't
337 // before, so I'm preserving the old behavior and
338 // using getService(), instead of createService()
339 // which would do the caching.
340 return new KeyguardManager();
341 }});
342
343 registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
344 public Object createService(ContextImpl ctx) {
345 return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
346 }});
347
348 registerService(LOCATION_SERVICE, new StaticServiceFetcher() {
349 public Object createStaticService() {
350 IBinder b = ServiceManager.getService(LOCATION_SERVICE);
351 return new LocationManager(ILocationManager.Stub.asInterface(b));
352 }});
353
Jeff Sharkey1a303952011-06-16 13:04:20 -0700354 registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
355 @Override
356 public Object createService(ContextImpl ctx) {
357 return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
358 ServiceManager.getService(NETWORK_POLICY_SERVICE)));
359 }
360 });
361
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800362 registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
363 public Object createService(ContextImpl ctx) {
364 final Context outerContext = ctx.getOuterContext();
365 return new NotificationManager(
366 new ContextThemeWrapper(outerContext,
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800367 Resources.selectSystemTheme(0,
368 outerContext.getApplicationInfo().targetSdkVersion,
369 com.android.internal.R.style.Theme_Dialog,
Adam Powell6e90a362011-08-14 16:48:32 -0700370 com.android.internal.R.style.Theme_Holo_Dialog,
371 com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800372 ctx.mMainThread.getHandler());
373 }});
374
375 // Note: this was previously cached in a static variable, but
376 // constructed using mMainThread.getHandler(), so converting
377 // it to be a regular Context-cached service...
378 registerService(POWER_SERVICE, new ServiceFetcher() {
379 public Object createService(ContextImpl ctx) {
380 IBinder b = ServiceManager.getService(POWER_SERVICE);
381 IPowerManager service = IPowerManager.Stub.asInterface(b);
382 return new PowerManager(service, ctx.mMainThread.getHandler());
383 }});
384
385 registerService(SEARCH_SERVICE, new ServiceFetcher() {
386 public Object createService(ContextImpl ctx) {
387 return new SearchManager(ctx.getOuterContext(),
388 ctx.mMainThread.getHandler());
389 }});
390
391 registerService(SENSOR_SERVICE, new ServiceFetcher() {
392 public Object createService(ContextImpl ctx) {
393 return new SensorManager(ctx.mMainThread.getHandler().getLooper());
394 }});
395
396 registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
397 public Object createService(ContextImpl ctx) {
398 return new StatusBarManager(ctx.getOuterContext());
399 }});
400
401 registerService(STORAGE_SERVICE, new ServiceFetcher() {
402 public Object createService(ContextImpl ctx) {
403 try {
404 return new StorageManager(ctx.mMainThread.getHandler().getLooper());
405 } catch (RemoteException rex) {
406 Log.e(TAG, "Failed to create StorageManager", rex);
407 return null;
408 }
409 }});
410
411 registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
412 public Object createService(ContextImpl ctx) {
413 return new TelephonyManager(ctx.getOuterContext());
414 }});
415
416 registerService(THROTTLE_SERVICE, new StaticServiceFetcher() {
417 public Object createStaticService() {
418 IBinder b = ServiceManager.getService(THROTTLE_SERVICE);
419 return new ThrottleManager(IThrottleManager.Stub.asInterface(b));
420 }});
421
422 registerService(UI_MODE_SERVICE, new ServiceFetcher() {
423 public Object createService(ContextImpl ctx) {
424 return new UiModeManager();
425 }});
426
Mike Lockwood3a68b832011-03-08 10:08:59 -0500427 registerService(USB_SERVICE, new ServiceFetcher() {
428 public Object createService(ContextImpl ctx) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500429 IBinder b = ServiceManager.getService(USB_SERVICE);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500430 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500431 }});
432
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400433 registerService(SERIAL_SERVICE, new ServiceFetcher() {
434 public Object createService(ContextImpl ctx) {
435 IBinder b = ServiceManager.getService(SERIAL_SERVICE);
436 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
437 }});
438
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800439 registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
440 public Object createService(ContextImpl ctx) {
441 return new Vibrator();
442 }});
443
444 registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
445
446 registerService(WIFI_SERVICE, new ServiceFetcher() {
447 public Object createService(ContextImpl ctx) {
448 IBinder b = ServiceManager.getService(WIFI_SERVICE);
449 IWifiManager service = IWifiManager.Stub.asInterface(b);
450 return new WifiManager(service, ctx.mMainThread.getHandler());
451 }});
452
repo sync55bc5f32011-06-24 14:23:07 -0700453 registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
454 public Object createService(ContextImpl ctx) {
455 IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
456 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
457 return new WifiP2pManager(service);
458 }});
459
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800460 registerService(WINDOW_SERVICE, new ServiceFetcher() {
461 public Object getService(ContextImpl ctx) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700462 return WindowManagerImpl.getDefault(ctx.mPackageInfo.mCompatibilityInfo);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800463 }});
464 }
465
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700466 static ContextImpl getImpl(Context context) {
467 Context nextContext;
468 while ((context instanceof ContextWrapper) &&
469 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
470 context = nextContext;
471 }
472 return (ContextImpl)context;
473 }
474
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800475 // The system service cache for the system services that are
476 // cached per-ContextImpl. Package-scoped to avoid accessor
477 // methods.
478 final ArrayList<Object> mServiceCache = new ArrayList<Object>();
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 @Override
481 public AssetManager getAssets() {
482 return mResources.getAssets();
483 }
484
485 @Override
486 public Resources getResources() {
487 return mResources;
488 }
489
490 @Override
491 public PackageManager getPackageManager() {
492 if (mPackageManager != null) {
493 return mPackageManager;
494 }
495
496 IPackageManager pm = ActivityThread.getPackageManager();
497 if (pm != null) {
498 // Doesn't matter if we make more than one instance.
499 return (mPackageManager = new ApplicationPackageManager(this, pm));
500 }
501
502 return null;
503 }
504
505 @Override
506 public ContentResolver getContentResolver() {
507 return mContentResolver;
508 }
509
510 @Override
511 public Looper getMainLooper() {
512 return mMainThread.getLooper();
513 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 @Override
516 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700517 return (mPackageInfo != null) ?
518 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 @Override
522 public void setTheme(int resid) {
523 mThemeResource = resid;
524 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800527 public int getThemeResId() {
528 return mThemeResource;
529 }
530
531 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 public Resources.Theme getTheme() {
533 if (mTheme == null) {
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800534 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
535 getOuterContext().getApplicationInfo().targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 mTheme = mResources.newTheme();
537 mTheme.applyStyle(mThemeResource, true);
538 }
539 return mTheme;
540 }
541
542 @Override
543 public ClassLoader getClassLoader() {
544 return mPackageInfo != null ?
545 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
546 }
547
548 @Override
549 public String getPackageName() {
550 if (mPackageInfo != null) {
551 return mPackageInfo.getPackageName();
552 }
553 throw new RuntimeException("Not supported in system context");
554 }
555
556 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700557 public ApplicationInfo getApplicationInfo() {
558 if (mPackageInfo != null) {
559 return mPackageInfo.getApplicationInfo();
560 }
561 throw new RuntimeException("Not supported in system context");
562 }
563
564 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public String getPackageResourcePath() {
566 if (mPackageInfo != null) {
567 return mPackageInfo.getResDir();
568 }
569 throw new RuntimeException("Not supported in system context");
570 }
571
572 @Override
573 public String getPackageCodePath() {
574 if (mPackageInfo != null) {
575 return mPackageInfo.getAppDir();
576 }
577 throw new RuntimeException("Not supported in system context");
578 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200579
Joe Onorato23ecae32009-06-10 17:07:15 -0700580 public File getSharedPrefsFile(String name) {
581 return makeFilename(getPreferencesDir(), name + ".xml");
582 }
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 @Override
585 public SharedPreferences getSharedPreferences(String name, int mode) {
586 SharedPreferencesImpl sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 synchronized (sSharedPrefs) {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700588 sp = sSharedPrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700589 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800590 File prefsFile = getSharedPrefsFile(name);
591 sp = new SharedPreferencesImpl(prefsFile, mode);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700592 sSharedPrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700593 return sp;
594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800596 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
597 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
598 // If somebody else (some other process) changed the prefs
599 // file behind our back, we reload it. This has been the
600 // historical (if undocumented) behavior.
601 sp.startReloadIfChangedUnexpectedly();
602 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700603 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
605
606 private File getPreferencesDir() {
607 synchronized (mSync) {
608 if (mPreferencesDir == null) {
609 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
610 }
611 return mPreferencesDir;
612 }
613 }
614
615 @Override
616 public FileInputStream openFileInput(String name)
617 throws FileNotFoundException {
618 File f = makeFilename(getFilesDir(), name);
619 return new FileInputStream(f);
620 }
621
622 @Override
623 public FileOutputStream openFileOutput(String name, int mode)
624 throws FileNotFoundException {
625 final boolean append = (mode&MODE_APPEND) != 0;
626 File f = makeFilename(getFilesDir(), name);
627 try {
628 FileOutputStream fos = new FileOutputStream(f, append);
629 setFilePermissionsFromMode(f.getPath(), mode, 0);
630 return fos;
631 } catch (FileNotFoundException e) {
632 }
633
634 File parent = f.getParentFile();
635 parent.mkdir();
636 FileUtils.setPermissions(
637 parent.getPath(),
638 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
639 -1, -1);
640 FileOutputStream fos = new FileOutputStream(f, append);
641 setFilePermissionsFromMode(f.getPath(), mode, 0);
642 return fos;
643 }
644
645 @Override
646 public boolean deleteFile(String name) {
647 File f = makeFilename(getFilesDir(), name);
648 return f.delete();
649 }
650
651 @Override
652 public File getFilesDir() {
653 synchronized (mSync) {
654 if (mFilesDir == null) {
655 mFilesDir = new File(getDataDirFile(), "files");
656 }
657 if (!mFilesDir.exists()) {
658 if(!mFilesDir.mkdirs()) {
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200659 Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 return null;
661 }
662 FileUtils.setPermissions(
663 mFilesDir.getPath(),
664 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
665 -1, -1);
666 }
667 return mFilesDir;
668 }
669 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800672 public File getExternalFilesDir(String type) {
673 synchronized (mSync) {
674 if (mExternalFilesDir == null) {
675 mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory(
676 getPackageName());
677 }
678 if (!mExternalFilesDir.exists()) {
679 try {
680 (new File(Environment.getExternalStorageAndroidDataDir(),
681 ".nomedia")).createNewFile();
682 } catch (IOException e) {
683 }
684 if (!mExternalFilesDir.mkdirs()) {
685 Log.w(TAG, "Unable to create external files directory");
686 return null;
687 }
688 }
689 if (type == null) {
690 return mExternalFilesDir;
691 }
692 File dir = new File(mExternalFilesDir, type);
693 if (!dir.exists()) {
694 if (!dir.mkdirs()) {
695 Log.w(TAG, "Unable to create external media directory " + dir);
696 return null;
697 }
698 }
699 return dir;
700 }
701 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200702
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800703 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800704 public File getObbDir() {
705 synchronized (mSync) {
706 if (mObbDir == null) {
707 mObbDir = Environment.getExternalStorageAppObbDirectory(
708 getPackageName());
709 }
710 return mObbDir;
711 }
712 }
713
714 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 public File getCacheDir() {
716 synchronized (mSync) {
717 if (mCacheDir == null) {
718 mCacheDir = new File(getDataDirFile(), "cache");
719 }
720 if (!mCacheDir.exists()) {
721 if(!mCacheDir.mkdirs()) {
722 Log.w(TAG, "Unable to create cache directory");
723 return null;
724 }
725 FileUtils.setPermissions(
726 mCacheDir.getPath(),
727 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
728 -1, -1);
729 }
730 }
731 return mCacheDir;
732 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200733
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800734 @Override
735 public File getExternalCacheDir() {
736 synchronized (mSync) {
737 if (mExternalCacheDir == null) {
738 mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory(
739 getPackageName());
740 }
741 if (!mExternalCacheDir.exists()) {
742 try {
743 (new File(Environment.getExternalStorageAndroidDataDir(),
744 ".nomedia")).createNewFile();
745 } catch (IOException e) {
746 }
747 if (!mExternalCacheDir.mkdirs()) {
748 Log.w(TAG, "Unable to create external cache directory");
749 return null;
750 }
751 }
752 return mExternalCacheDir;
753 }
754 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 @Override
757 public File getFileStreamPath(String name) {
758 return makeFilename(getFilesDir(), name);
759 }
760
761 @Override
762 public String[] fileList() {
763 final String[] list = getFilesDir().list();
764 return (list != null) ? list : EMPTY_FILE_LIST;
765 }
766
767 @Override
768 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800769 File f = validateFilePath(name, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f, factory);
771 setFilePermissionsFromMode(f.getPath(), mode, 0);
772 return db;
773 }
774
775 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700776 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
777 DatabaseErrorHandler errorHandler) {
778 File f = validateFilePath(name, true);
779 SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f.getPath(), factory, errorHandler);
780 setFilePermissionsFromMode(f.getPath(), mode, 0);
781 return db;
782 }
783
784 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 public boolean deleteDatabase(String name) {
786 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800787 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800788 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 } catch (Exception e) {
790 }
791 return false;
792 }
793
794 @Override
795 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800796 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 }
798
799 @Override
800 public String[] databaseList() {
801 final String[] list = getDatabasesDir().list();
802 return (list != null) ? list : EMPTY_FILE_LIST;
803 }
804
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 private File getDatabasesDir() {
807 synchronized (mSync) {
808 if (mDatabasesDir == null) {
809 mDatabasesDir = new File(getDataDirFile(), "databases");
810 }
811 if (mDatabasesDir.getPath().equals("databases")) {
812 mDatabasesDir = new File("/data/system");
813 }
814 return mDatabasesDir;
815 }
816 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 @Override
819 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700820 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822
823 @Override
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700824 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700825 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
827
828 @Override
829 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700830 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 }
832
833 @Override
834 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700835 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 }
837
838 @Override
839 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700840 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842
843 @Override
844 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700845 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 }
847
848 @Override
849 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700850 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
852
853 @Override
854 public void startActivity(Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700855 startActivity(intent, null);
856 }
857
858 @Override
859 public void startActivity(Intent intent, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
861 throw new AndroidRuntimeException(
862 "Calling startActivity() from outside of an Activity "
863 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
864 + " Is this really what you want?");
865 }
866 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700867 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700868 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870
871 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800872 public void startActivities(Intent[] intents) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700873 startActivities(intents, null);
874 }
875
876 @Override
877 public void startActivities(Intent[] intents, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800878 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
879 throw new AndroidRuntimeException(
880 "Calling startActivities() from outside of an Activity "
881 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
882 + " Is this really what you want?");
883 }
884 mMainThread.getInstrumentation().execStartActivities(
885 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700886 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800887 }
888
889 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700890 public void startIntentSender(IntentSender intent,
891 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
892 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700893 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
894 }
895
896 @Override
897 public void startIntentSender(IntentSender intent, Intent fillInIntent,
898 int flagsMask, int flagsValues, int extraFlags, Bundle options)
899 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700900 try {
901 String resolvedType = null;
902 if (fillInIntent != null) {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400903 fillInIntent.setAllowFds(false);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700904 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
905 }
906 int result = ActivityManagerNative.getDefault()
907 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
908 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700909 0, flagsMask, flagsValues, options);
910 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700911 throw new IntentSender.SendIntentException();
912 }
913 Instrumentation.checkStartActivityResult(result, null);
914 } catch (RemoteException e) {
915 }
916 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200917
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700918 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 public void sendBroadcast(Intent intent) {
920 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
921 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400922 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 ActivityManagerNative.getDefault().broadcastIntent(
924 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700925 Activity.RESULT_OK, null, null, null, false, false,
926 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 } catch (RemoteException e) {
928 }
929 }
930
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800931 /** @hide */
932 @Override
933 public void sendBroadcast(Intent intent, int userId) {
934 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
935 try {
936 intent.setAllowFds(false);
937 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
938 intent, resolvedType, null, Activity.RESULT_OK, null, null, null, false, false,
939 userId);
940 } catch (RemoteException e) {
941 }
942 }
943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 @Override
945 public void sendBroadcast(Intent intent, String receiverPermission) {
946 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
947 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400948 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 ActivityManagerNative.getDefault().broadcastIntent(
950 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700951 Activity.RESULT_OK, null, null, receiverPermission, false, false,
952 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 } catch (RemoteException e) {
954 }
955 }
956
957 @Override
958 public void sendOrderedBroadcast(Intent intent,
959 String receiverPermission) {
960 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
961 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400962 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 ActivityManagerNative.getDefault().broadcastIntent(
964 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700965 Activity.RESULT_OK, null, null, receiverPermission, true, false,
966 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 } catch (RemoteException e) {
968 }
969 }
970
971 @Override
972 public void sendOrderedBroadcast(Intent intent,
973 String receiverPermission, BroadcastReceiver resultReceiver,
974 Handler scheduler, int initialCode, String initialData,
975 Bundle initialExtras) {
976 IIntentReceiver rd = null;
977 if (resultReceiver != null) {
978 if (mPackageInfo != null) {
979 if (scheduler == null) {
980 scheduler = mMainThread.getHandler();
981 }
982 rd = mPackageInfo.getReceiverDispatcher(
983 resultReceiver, getOuterContext(), scheduler,
984 mMainThread.getInstrumentation(), false);
985 } else {
986 if (scheduler == null) {
987 scheduler = mMainThread.getHandler();
988 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700989 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
991 }
992 }
993 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
994 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400995 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 ActivityManagerNative.getDefault().broadcastIntent(
997 mMainThread.getApplicationThread(), intent, resolvedType, rd,
998 initialCode, initialData, initialExtras, receiverPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -0700999 true, false, Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 } catch (RemoteException e) {
1001 }
1002 }
1003
1004 @Override
1005 public void sendStickyBroadcast(Intent intent) {
1006 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1007 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001008 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 ActivityManagerNative.getDefault().broadcastIntent(
1010 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001011 Activity.RESULT_OK, null, null, null, false, true,
1012 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 } catch (RemoteException e) {
1014 }
1015 }
1016
1017 @Override
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001018 public void sendStickyOrderedBroadcast(Intent intent,
1019 BroadcastReceiver resultReceiver,
1020 Handler scheduler, int initialCode, String initialData,
1021 Bundle initialExtras) {
1022 IIntentReceiver rd = null;
1023 if (resultReceiver != null) {
1024 if (mPackageInfo != null) {
1025 if (scheduler == null) {
1026 scheduler = mMainThread.getHandler();
1027 }
1028 rd = mPackageInfo.getReceiverDispatcher(
1029 resultReceiver, getOuterContext(), scheduler,
1030 mMainThread.getInstrumentation(), false);
1031 } else {
1032 if (scheduler == null) {
1033 scheduler = mMainThread.getHandler();
1034 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001035 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001036 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1037 }
1038 }
1039 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1040 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001041 intent.setAllowFds(false);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001042 ActivityManagerNative.getDefault().broadcastIntent(
1043 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1044 initialCode, initialData, initialExtras, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001045 true, true, Binder.getOrigCallingUser());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001046 } catch (RemoteException e) {
1047 }
1048 }
1049
1050 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 public void removeStickyBroadcast(Intent intent) {
1052 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1053 if (resolvedType != null) {
1054 intent = new Intent(intent);
1055 intent.setDataAndType(intent.getData(), resolvedType);
1056 }
1057 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001058 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 ActivityManagerNative.getDefault().unbroadcastIntent(
Amith Yamasani742a6712011-05-04 14:49:28 -07001060 mMainThread.getApplicationThread(), intent, Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 } catch (RemoteException e) {
1062 }
1063 }
1064
1065 @Override
1066 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1067 return registerReceiver(receiver, filter, null, null);
1068 }
1069
1070 @Override
1071 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1072 String broadcastPermission, Handler scheduler) {
1073 return registerReceiverInternal(receiver, filter, broadcastPermission,
1074 scheduler, getOuterContext());
1075 }
1076
1077 private Intent registerReceiverInternal(BroadcastReceiver receiver,
1078 IntentFilter filter, String broadcastPermission,
1079 Handler scheduler, Context context) {
1080 IIntentReceiver rd = null;
1081 if (receiver != null) {
1082 if (mPackageInfo != null && context != null) {
1083 if (scheduler == null) {
1084 scheduler = mMainThread.getHandler();
1085 }
1086 rd = mPackageInfo.getReceiverDispatcher(
1087 receiver, context, scheduler,
1088 mMainThread.getInstrumentation(), true);
1089 } else {
1090 if (scheduler == null) {
1091 scheduler = mMainThread.getHandler();
1092 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001093 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001094 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
1096 }
1097 try {
1098 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001099 mMainThread.getApplicationThread(), mBasePackageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 rd, filter, broadcastPermission);
1101 } catch (RemoteException e) {
1102 return null;
1103 }
1104 }
1105
1106 @Override
1107 public void unregisterReceiver(BroadcastReceiver receiver) {
1108 if (mPackageInfo != null) {
1109 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1110 getOuterContext(), receiver);
1111 try {
1112 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1113 } catch (RemoteException e) {
1114 }
1115 } else {
1116 throw new RuntimeException("Not supported in system context");
1117 }
1118 }
1119
1120 @Override
1121 public ComponentName startService(Intent service) {
1122 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001123 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 ComponentName cn = ActivityManagerNative.getDefault().startService(
1125 mMainThread.getApplicationThread(), service,
1126 service.resolveTypeIfNeeded(getContentResolver()));
1127 if (cn != null && cn.getPackageName().equals("!")) {
1128 throw new SecurityException(
1129 "Not allowed to start service " + service
1130 + " without permission " + cn.getClassName());
1131 }
1132 return cn;
1133 } catch (RemoteException e) {
1134 return null;
1135 }
1136 }
1137
1138 @Override
1139 public boolean stopService(Intent service) {
1140 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001141 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 int res = ActivityManagerNative.getDefault().stopService(
1143 mMainThread.getApplicationThread(), service,
1144 service.resolveTypeIfNeeded(getContentResolver()));
1145 if (res < 0) {
1146 throw new SecurityException(
1147 "Not allowed to stop service " + service);
1148 }
1149 return res != 0;
1150 } catch (RemoteException e) {
1151 return false;
1152 }
1153 }
1154
1155 @Override
1156 public boolean bindService(Intent service, ServiceConnection conn,
1157 int flags) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001158 return bindService(service, conn, flags, UserId.getUserId(Process.myUid()));
1159 }
1160
1161 /** @hide */
1162 @Override
1163 public boolean bindService(Intent service, ServiceConnection conn, int flags, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 IServiceConnection sd;
1165 if (mPackageInfo != null) {
1166 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1167 mMainThread.getHandler(), flags);
1168 } else {
1169 throw new RuntimeException("Not supported in system context");
1170 }
1171 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001172 IBinder token = getActivityToken();
1173 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1174 && mPackageInfo.getApplicationInfo().targetSdkVersion
1175 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1176 flags |= BIND_WAIVE_PRIORITY;
1177 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001178 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 int res = ActivityManagerNative.getDefault().bindService(
1180 mMainThread.getApplicationThread(), getActivityToken(),
1181 service, service.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001182 sd, flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 if (res < 0) {
1184 throw new SecurityException(
1185 "Not allowed to bind to service " + service);
1186 }
1187 return res != 0;
1188 } catch (RemoteException e) {
1189 return false;
1190 }
1191 }
1192
1193 @Override
1194 public void unbindService(ServiceConnection conn) {
1195 if (mPackageInfo != null) {
1196 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1197 getOuterContext(), conn);
1198 try {
1199 ActivityManagerNative.getDefault().unbindService(sd);
1200 } catch (RemoteException e) {
1201 }
1202 } else {
1203 throw new RuntimeException("Not supported in system context");
1204 }
1205 }
1206
1207 @Override
1208 public boolean startInstrumentation(ComponentName className,
1209 String profileFile, Bundle arguments) {
1210 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001211 if (arguments != null) {
1212 arguments.setAllowFds(false);
1213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 return ActivityManagerNative.getDefault().startInstrumentation(
1215 className, profileFile, 0, arguments, null);
1216 } catch (RemoteException e) {
1217 // System has crashed, nothing we can do.
1218 }
1219 return false;
1220 }
1221
1222 @Override
1223 public Object getSystemService(String name) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001224 ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
1225 return fetcher == null ? null : fetcher.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 }
1227
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001228 private WallpaperManager getWallpaperManager() {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001229 return (WallpaperManager) WALLPAPER_FETCHER.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 }
1231
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001232 /* package */ static DropBoxManager createDropBoxManager() {
1233 IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
1234 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001235 if (service == null) {
1236 // Don't return a DropBoxManager that will NPE upon use.
1237 // This also avoids caching a broken DropBoxManager in
1238 // getDropBoxManager during early boot, before the
1239 // DROPBOX_SERVICE is registered.
1240 return null;
1241 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001242 return new DropBoxManager(service);
1243 }
1244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 @Override
1246 public int checkPermission(String permission, int pid, int uid) {
1247 if (permission == null) {
1248 throw new IllegalArgumentException("permission is null");
1249 }
1250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 try {
1252 return ActivityManagerNative.getDefault().checkPermission(
1253 permission, pid, uid);
1254 } catch (RemoteException e) {
1255 return PackageManager.PERMISSION_DENIED;
1256 }
1257 }
1258
1259 @Override
1260 public int checkCallingPermission(String permission) {
1261 if (permission == null) {
1262 throw new IllegalArgumentException("permission is null");
1263 }
1264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 int pid = Binder.getCallingPid();
1266 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001267 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269 return PackageManager.PERMISSION_DENIED;
1270 }
1271
1272 @Override
1273 public int checkCallingOrSelfPermission(String permission) {
1274 if (permission == null) {
1275 throw new IllegalArgumentException("permission is null");
1276 }
1277
1278 return checkPermission(permission, Binder.getCallingPid(),
1279 Binder.getCallingUid());
1280 }
1281
1282 private void enforce(
1283 String permission, int resultOfCheck,
1284 boolean selfToo, int uid, String message) {
1285 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1286 throw new SecurityException(
1287 (message != null ? (message + ": ") : "") +
1288 (selfToo
1289 ? "Neither user " + uid + " nor current process has "
1290 : "User " + uid + " does not have ") +
1291 permission +
1292 ".");
1293 }
1294 }
1295
1296 public void enforcePermission(
1297 String permission, int pid, int uid, String message) {
1298 enforce(permission,
1299 checkPermission(permission, pid, uid),
1300 false,
1301 uid,
1302 message);
1303 }
1304
1305 public void enforceCallingPermission(String permission, String message) {
1306 enforce(permission,
1307 checkCallingPermission(permission),
1308 false,
1309 Binder.getCallingUid(),
1310 message);
1311 }
1312
1313 public void enforceCallingOrSelfPermission(
1314 String permission, String message) {
1315 enforce(permission,
1316 checkCallingOrSelfPermission(permission),
1317 true,
1318 Binder.getCallingUid(),
1319 message);
1320 }
1321
1322 @Override
1323 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1324 try {
1325 ActivityManagerNative.getDefault().grantUriPermission(
1326 mMainThread.getApplicationThread(), toPackage, uri,
1327 modeFlags);
1328 } catch (RemoteException e) {
1329 }
1330 }
1331
1332 @Override
1333 public void revokeUriPermission(Uri uri, int modeFlags) {
1334 try {
1335 ActivityManagerNative.getDefault().revokeUriPermission(
1336 mMainThread.getApplicationThread(), uri,
1337 modeFlags);
1338 } catch (RemoteException e) {
1339 }
1340 }
1341
1342 @Override
1343 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 try {
1345 return ActivityManagerNative.getDefault().checkUriPermission(
1346 uri, pid, uid, modeFlags);
1347 } catch (RemoteException e) {
1348 return PackageManager.PERMISSION_DENIED;
1349 }
1350 }
1351
1352 @Override
1353 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 int pid = Binder.getCallingPid();
1355 if (pid != Process.myPid()) {
1356 return checkUriPermission(uri, pid,
1357 Binder.getCallingUid(), modeFlags);
1358 }
1359 return PackageManager.PERMISSION_DENIED;
1360 }
1361
1362 @Override
1363 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1364 return checkUriPermission(uri, Binder.getCallingPid(),
1365 Binder.getCallingUid(), modeFlags);
1366 }
1367
1368 @Override
1369 public int checkUriPermission(Uri uri, String readPermission,
1370 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001371 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1373 + readPermission + " writePermission=" + writePermission
1374 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1375 }
1376 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1377 if (readPermission == null
1378 || checkPermission(readPermission, pid, uid)
1379 == PackageManager.PERMISSION_GRANTED) {
1380 return PackageManager.PERMISSION_GRANTED;
1381 }
1382 }
1383 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1384 if (writePermission == null
1385 || checkPermission(writePermission, pid, uid)
1386 == PackageManager.PERMISSION_GRANTED) {
1387 return PackageManager.PERMISSION_GRANTED;
1388 }
1389 }
1390 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1391 : PackageManager.PERMISSION_DENIED;
1392 }
1393
1394 private String uriModeFlagToString(int uriModeFlags) {
1395 switch (uriModeFlags) {
1396 case Intent.FLAG_GRANT_READ_URI_PERMISSION |
1397 Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1398 return "read and write";
1399 case Intent.FLAG_GRANT_READ_URI_PERMISSION:
1400 return "read";
1401 case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1402 return "write";
1403 }
1404 throw new IllegalArgumentException(
1405 "Unknown permission mode flags: " + uriModeFlags);
1406 }
1407
1408 private void enforceForUri(
1409 int modeFlags, int resultOfCheck, boolean selfToo,
1410 int uid, Uri uri, String message) {
1411 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1412 throw new SecurityException(
1413 (message != null ? (message + ": ") : "") +
1414 (selfToo
1415 ? "Neither user " + uid + " nor current process has "
1416 : "User " + uid + " does not have ") +
1417 uriModeFlagToString(modeFlags) +
1418 " permission on " +
1419 uri +
1420 ".");
1421 }
1422 }
1423
1424 public void enforceUriPermission(
1425 Uri uri, int pid, int uid, int modeFlags, String message) {
1426 enforceForUri(
1427 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1428 false, uid, uri, message);
1429 }
1430
1431 public void enforceCallingUriPermission(
1432 Uri uri, int modeFlags, String message) {
1433 enforceForUri(
1434 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001435 false,
1436 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
1438
1439 public void enforceCallingOrSelfUriPermission(
1440 Uri uri, int modeFlags, String message) {
1441 enforceForUri(
1442 modeFlags,
1443 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1444 Binder.getCallingUid(), uri, message);
1445 }
1446
1447 public void enforceUriPermission(
1448 Uri uri, String readPermission, String writePermission,
1449 int pid, int uid, int modeFlags, String message) {
1450 enforceForUri(modeFlags,
1451 checkUriPermission(
1452 uri, readPermission, writePermission, pid, uid,
1453 modeFlags),
1454 false,
1455 uid,
1456 uri,
1457 message);
1458 }
1459
1460 @Override
1461 public Context createPackageContext(String packageName, int flags)
1462 throws PackageManager.NameNotFoundException {
1463 if (packageName.equals("system") || packageName.equals("android")) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001464 return new ContextImpl(mMainThread.getSystemContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001467 LoadedApk pi =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001468 mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 if (pi != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001470 ContextImpl c = new ContextImpl();
Romain Guy870e09f2009-07-06 16:35:25 -07001471 c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001472 c.init(pi, null, mMainThread, mResources, mBasePackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 if (c.mResources != null) {
1474 return c;
1475 }
1476 }
1477
1478 // Should be a better exception.
1479 throw new PackageManager.NameNotFoundException(
1480 "Application package " + packageName + " not found");
1481 }
1482
Romain Guy870e09f2009-07-06 16:35:25 -07001483 @Override
1484 public boolean isRestricted() {
1485 return mRestricted;
1486 }
1487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 private File getDataDirFile() {
1489 if (mPackageInfo != null) {
1490 return mPackageInfo.getDataDirFile();
1491 }
1492 throw new RuntimeException("Not supported in system context");
1493 }
1494
1495 @Override
1496 public File getDir(String name, int mode) {
1497 name = "app_" + name;
1498 File file = makeFilename(getDataDirFile(), name);
1499 if (!file.exists()) {
1500 file.mkdir();
1501 setFilePermissionsFromMode(file.getPath(), mode,
1502 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1503 }
1504 return file;
1505 }
1506
Dianne Hackborn21556372010-02-04 16:34:40 -08001507 static ContextImpl createSystemContext(ActivityThread mainThread) {
1508 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 context.init(Resources.getSystem(), mainThread);
1510 return context;
1511 }
1512
Dianne Hackborn21556372010-02-04 16:34:40 -08001513 ContextImpl() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 mOuterContext = this;
1515 }
1516
1517 /**
1518 * Create a new ApplicationContext from an existing one. The new one
1519 * works and operates the same as the one it is copying.
1520 *
1521 * @param context Existing application context.
1522 */
Dianne Hackborn21556372010-02-04 16:34:40 -08001523 public ContextImpl(ContextImpl context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 mPackageInfo = context.mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001525 mBasePackageName = context.mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 mResources = context.mResources;
1527 mMainThread = context.mMainThread;
1528 mContentResolver = context.mContentResolver;
1529 mOuterContext = this;
1530 }
1531
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001532 final void init(LoadedApk packageInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 IBinder activityToken, ActivityThread mainThread) {
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001534 init(packageInfo, activityToken, mainThread, null, null);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001535 }
1536
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001537 final void init(LoadedApk packageInfo,
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001538 IBinder activityToken, ActivityThread mainThread,
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001539 Resources container, String basePackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 mPackageInfo = packageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001541 mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 mResources = mPackageInfo.getResources(mainThread);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001543
Dianne Hackborn559a7872010-04-07 18:19:41 -07001544 if (mResources != null && container != null
1545 && container.getCompatibilityInfo().applicationScale !=
1546 mResources.getCompatibilityInfo().applicationScale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001547 if (DEBUG) {
1548 Log.d(TAG, "loaded context has different scaling. Using container's" +
1549 " compatiblity info:" + container.getDisplayMetrics());
1550 }
1551 mResources = mainThread.getTopLevelResources(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001552 mPackageInfo.getResDir(), container.getCompatibilityInfo());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 mMainThread = mainThread;
1555 mContentResolver = new ApplicationContentResolver(this, mainThread);
1556
1557 setActivityToken(activityToken);
1558 }
1559
1560 final void init(Resources resources, ActivityThread mainThread) {
1561 mPackageInfo = null;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001562 mBasePackageName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 mResources = resources;
1564 mMainThread = mainThread;
1565 mContentResolver = new ApplicationContentResolver(this, mainThread);
1566 }
1567
1568 final void scheduleFinalCleanup(String who, String what) {
1569 mMainThread.scheduleContextCleanup(this, who, what);
1570 }
1571
1572 final void performFinalCleanup(String who, String what) {
1573 //Log.i(TAG, "Cleanup up context: " + this);
1574 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1575 }
1576
1577 final Context getReceiverRestrictedContext() {
1578 if (mReceiverRestrictedContext != null) {
1579 return mReceiverRestrictedContext;
1580 }
1581 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1582 }
1583
1584 final void setActivityToken(IBinder token) {
1585 mActivityToken = token;
1586 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 final void setOuterContext(Context context) {
1589 mOuterContext = context;
1590 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 final Context getOuterContext() {
1593 return mOuterContext;
1594 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 final IBinder getActivityToken() {
1597 return mActivityToken;
1598 }
1599
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08001600 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 int extraPermissions) {
1602 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
1603 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
1604 |extraPermissions;
1605 if ((mode&MODE_WORLD_READABLE) != 0) {
1606 perms |= FileUtils.S_IROTH;
1607 }
1608 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
1609 perms |= FileUtils.S_IWOTH;
1610 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001611 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
1613 + ", perms=0x" + Integer.toHexString(perms));
1614 }
1615 FileUtils.setPermissions(name, perms, -1, -1);
1616 }
1617
Oscar Montemayora8529f62009-11-18 10:14:20 -08001618 private File validateFilePath(String name, boolean createDirectory) {
1619 File dir;
1620 File f;
1621
1622 if (name.charAt(0) == File.separatorChar) {
1623 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
1624 dir = new File(dirPath);
1625 name = name.substring(name.lastIndexOf(File.separatorChar));
1626 f = new File(dir, name);
1627 } else {
1628 dir = getDatabasesDir();
1629 f = makeFilename(dir, name);
1630 }
1631
1632 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
1633 FileUtils.setPermissions(dir.getPath(),
1634 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1635 -1, -1);
1636 }
1637
1638 return f;
1639 }
1640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 private File makeFilename(File base, String name) {
1642 if (name.indexOf(File.separatorChar) < 0) {
1643 return new File(base, name);
1644 }
1645 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08001646 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 }
1648
1649 // ----------------------------------------------------------------------
1650 // ----------------------------------------------------------------------
1651 // ----------------------------------------------------------------------
1652
1653 private static final class ApplicationContentResolver extends ContentResolver {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001654 public ApplicationContentResolver(Context context, ActivityThread mainThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 super(context);
1656 mMainThread = mainThread;
1657 }
1658
1659 @Override
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001660 protected IContentProvider acquireProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 return mMainThread.acquireProvider(context, name);
1662 }
1663
1664 @Override
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001665 protected IContentProvider acquireExistingProvider(Context context, String name) {
1666 return mMainThread.acquireExistingProvider(context, name);
1667 }
1668
1669 @Override
1670 public boolean releaseProvider(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 return mMainThread.releaseProvider(provider);
1672 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 private final ActivityThread mMainThread;
1675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676}