blob: 4496ce81484a52d8b2323ef1a1067d984c0272f9 [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
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -080021import android.bluetooth.BluetoothAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.ContextWrapper;
27import android.content.IContentProvider;
28import android.content.Intent;
29import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070030import android.content.IIntentReceiver;
31import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.ReceiverCallNotAllowedException;
33import android.content.ServiceConnection;
34import android.content.SharedPreferences;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070039import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070041import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.database.sqlite.SQLiteDatabase;
43import android.database.sqlite.SQLiteDatabase.CursorFactory;
44import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.graphics.drawable.Drawable;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040046import android.hardware.ISerialManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.hardware.SensorManager;
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -040048import android.hardware.SerialManager;
Jeff Brown25157e42012-04-16 12:13:05 -070049import android.hardware.SystemSensorManager;
Jeff Brownfa25bf52012-07-23 19:26:30 -070050import android.hardware.display.DisplayManager;
Jeff Brownac143512012-04-05 18:57:33 -070051import android.hardware.input.IInputManager;
Jeff Brown9df6e7a2012-04-05 11:49:26 -070052import android.hardware.input.InputManager;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080053import android.hardware.usb.IUsbManager;
54import android.hardware.usb.UsbManager;
Bai Taoa58a8752010-07-13 15:32:16 +080055import android.location.CountryDetector;
56import android.location.ICountryDetector;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.location.ILocationManager;
58import android.location.LocationManager;
59import android.media.AudioManager;
Dianne Hackbornb58b8f82012-06-11 15:08:39 -070060import android.media.MediaRouter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.net.ConnectivityManager;
62import android.net.IConnectivityManager;
Jeff Sharkey1a303952011-06-16 13:04:20 -070063import android.net.INetworkPolicyManager;
64import android.net.NetworkPolicyManager;
Irfan Sheriffc9b68512010-04-08 14:12:33 -070065import android.net.ThrottleManager;
66import android.net.IThrottleManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.net.Uri;
Irfan Sheriff7d024d32012-03-22 17:01:39 -070068import android.net.nsd.INsdManager;
69import android.net.nsd.NsdManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.net.wifi.IWifiManager;
71import android.net.wifi.WifiManager;
repo sync55bc5f32011-06-24 14:23:07 -070072import android.net.wifi.p2p.IWifiP2pManager;
73import android.net.wifi.p2p.WifiP2pManager;
Nick Pelly50b4d8f2010-12-07 22:40:28 -080074import android.nfc.NfcManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Binder;
76import android.os.Bundle;
Dan Egnorf18a01c2009-11-12 11:32:50 -080077import android.os.DropBoxManager;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080078import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.FileUtils;
80import android.os.Handler;
81import android.os.IBinder;
82import android.os.IPowerManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070083import android.os.IUserManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070084import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.os.PowerManager;
86import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070087import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.os.ServiceManager;
Amith Yamasani742a6712011-05-04 14:49:28 -070089import android.os.UserId;
Jeff Brownc2346132012-04-13 01:55:38 -070090import android.os.SystemVibrator;
Amith Yamasani258848d2012-08-10 17:06:33 -070091import android.os.UserManager;
San Mehatb1043402010-02-05 08:26:50 -080092import android.os.storage.StorageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.telephony.TelephonyManager;
Dianne Hackborn9f531192010-08-04 17:48:03 -070094import android.content.ClipboardManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.util.AndroidRuntimeException;
96import android.util.Log;
97import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.view.WindowManagerImpl;
svetoslavganov75986cf2009-05-14 22:28:01 -070099import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.view.inputmethod.InputMethodManager;
satok988323c2011-06-22 16:38:13 +0900101import android.view.textservice.TextServicesManager;
Fred Quintana60307342009-03-24 22:48:12 -0700102import android.accounts.AccountManager;
103import android.accounts.IAccountManager;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800104import android.app.admin.DevicePolicyManager;
Dan Egnorf18a01c2009-11-12 11:32:50 -0800105import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -0700106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107import java.io.File;
108import java.io.FileInputStream;
109import java.io.FileNotFoundException;
110import java.io.FileOutputStream;
111import java.io.IOException;
112import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.util.ArrayList;
114import java.util.HashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116class ReceiverRestrictedContext extends ContextWrapper {
117 ReceiverRestrictedContext(Context base) {
118 super(base);
119 }
120
121 @Override
122 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
123 return registerReceiver(receiver, filter, null, null);
124 }
125
126 @Override
127 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
128 String broadcastPermission, Handler scheduler) {
129 throw new ReceiverCallNotAllowedException(
130 "IntentReceiver components are not allowed to register to receive intents");
131 //ex.fillInStackTrace();
132 //Log.e("IntentReceiver", ex.getMessage(), ex);
133 //return mContext.registerReceiver(receiver, filter, broadcastPermission,
134 // scheduler);
135 }
136
137 @Override
138 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
139 throw new ReceiverCallNotAllowedException(
140 "IntentReceiver components are not allowed to bind to services");
141 //ex.fillInStackTrace();
142 //Log.e("IntentReceiver", ex.getMessage(), ex);
143 //return mContext.bindService(service, interfaceName, conn, flags);
144 }
145}
146
147/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800148 * Common implementation of Context API, which provides the base
149 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800151class ContextImpl extends Context {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 private final static String TAG = "ApplicationContext";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700153 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700155 private static final HashMap<String, SharedPreferencesImpl> sSharedPrefs =
156 new HashMap<String, SharedPreferencesImpl>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700158 /*package*/ LoadedApk mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700159 private String mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 private Resources mResources;
161 /*package*/ ActivityThread mMainThread;
162 private Context mOuterContext;
163 private IBinder mActivityToken = null;
164 private ApplicationContentResolver mContentResolver;
165 private int mThemeResource = 0;
166 private Resources.Theme mTheme = null;
167 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 private Context mReceiverRestrictedContext = null;
Romain Guy870e09f2009-07-06 16:35:25 -0700169 private boolean mRestricted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
171 private final Object mSync = new Object();
172
173 private File mDatabasesDir;
174 private File mPreferencesDir;
175 private File mFilesDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private File mCacheDir;
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800177 private File mObbDir;
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800178 private File mExternalFilesDir;
179 private File mExternalCacheDir;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 private static final String[] EMPTY_FILE_LIST = {};
182
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800183 /**
184 * Override this class when the system service constructor needs a
185 * ContextImpl. Else, use StaticServiceFetcher below.
186 */
187 /*package*/ static class ServiceFetcher {
188 int mContextCacheIndex = -1;
189
190 /**
191 * Main entrypoint; only override if you don't need caching.
192 */
193 public Object getService(ContextImpl ctx) {
194 ArrayList<Object> cache = ctx.mServiceCache;
195 Object service;
196 synchronized (cache) {
197 if (cache.size() == 0) {
198 // Initialize the cache vector on first access.
199 // At this point sNextPerContextServiceCacheIndex
200 // is the number of potential services that are
201 // cached per-Context.
202 for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
203 cache.add(null);
204 }
205 } else {
206 service = cache.get(mContextCacheIndex);
207 if (service != null) {
208 return service;
209 }
210 }
211 service = createService(ctx);
212 cache.set(mContextCacheIndex, service);
213 return service;
214 }
215 }
216
217 /**
218 * Override this to create a new per-Context instance of the
219 * service. getService() will handle locking and caching.
220 */
221 public Object createService(ContextImpl ctx) {
222 throw new RuntimeException("Not implemented");
223 }
224 }
225
226 /**
227 * Override this class for services to be cached process-wide.
228 */
229 abstract static class StaticServiceFetcher extends ServiceFetcher {
230 private Object mCachedInstance;
231
232 @Override
233 public final Object getService(ContextImpl unused) {
234 synchronized (StaticServiceFetcher.this) {
235 Object service = mCachedInstance;
236 if (service != null) {
237 return service;
238 }
239 return mCachedInstance = createStaticService();
240 }
241 }
242
243 public abstract Object createStaticService();
244 }
245
246 private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
247 new HashMap<String, ServiceFetcher>();
248
249 private static int sNextPerContextServiceCacheIndex = 0;
250 private static void registerService(String serviceName, ServiceFetcher fetcher) {
251 if (!(fetcher instanceof StaticServiceFetcher)) {
252 fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
253 }
254 SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
255 }
256
257 // This one's defined separately and given a variable name so it
258 // can be re-used by getWallpaperManager(), avoiding a HashMap
259 // lookup.
260 private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() {
261 public Object createService(ContextImpl ctx) {
262 return new WallpaperManager(ctx.getOuterContext(),
263 ctx.mMainThread.getHandler());
264 }};
265
266 static {
267 registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
268 public Object getService(ContextImpl ctx) {
269 return AccessibilityManager.getInstance(ctx);
270 }});
271
272 registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
273 public Object createService(ContextImpl ctx) {
274 IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
275 IAccountManager service = IAccountManager.Stub.asInterface(b);
276 return new AccountManager(ctx, service);
277 }});
278
279 registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
280 public Object createService(ContextImpl ctx) {
281 return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
282 }});
283
284 registerService(ALARM_SERVICE, new StaticServiceFetcher() {
285 public Object createStaticService() {
286 IBinder b = ServiceManager.getService(ALARM_SERVICE);
287 IAlarmManager service = IAlarmManager.Stub.asInterface(b);
288 return new AlarmManager(service);
289 }});
290
291 registerService(AUDIO_SERVICE, new ServiceFetcher() {
292 public Object createService(ContextImpl ctx) {
293 return new AudioManager(ctx);
294 }});
295
Dianne Hackbornb58b8f82012-06-11 15:08:39 -0700296 registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() {
297 public Object createService(ContextImpl ctx) {
298 return new MediaRouter(ctx);
299 }});
300
Jaikumar Ganesh1abb1cb2012-01-25 16:14:50 -0800301 registerService(BLUETOOTH_SERVICE, new ServiceFetcher() {
302 public Object createService(ContextImpl ctx) {
303 return BluetoothAdapter.getDefaultAdapter();
304 }});
305
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800306 registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
307 public Object createService(ContextImpl ctx) {
308 return new ClipboardManager(ctx.getOuterContext(),
309 ctx.mMainThread.getHandler());
310 }});
311
312 registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
313 public Object createStaticService() {
314 IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
315 return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
316 }});
317
318 registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
319 public Object createStaticService() {
320 IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
321 return new CountryDetector(ICountryDetector.Stub.asInterface(b));
322 }});
323
324 registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
325 public Object createService(ContextImpl ctx) {
326 return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
327 }});
328
329 registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
330 public Object createService(ContextImpl ctx) {
331 return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
332 }});
333
Nick Pellyd2507462010-12-13 12:22:34 -0800334 registerService(NFC_SERVICE, new ServiceFetcher() {
335 public Object createService(ContextImpl ctx) {
336 return new NfcManager(ctx);
337 }});
338
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800339 registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
340 public Object createStaticService() {
341 return createDropBoxManager();
342 }});
343
Jeff Brown9f25b7f2012-04-10 14:30:49 -0700344 registerService(INPUT_SERVICE, new StaticServiceFetcher() {
345 public Object createStaticService() {
346 return InputManager.getInstance();
Jeff Brownac143512012-04-05 18:57:33 -0700347 }});
Jeff Brown9df6e7a2012-04-05 11:49:26 -0700348
Jeff Brownfa25bf52012-07-23 19:26:30 -0700349 registerService(DISPLAY_SERVICE, new StaticServiceFetcher() {
350 public Object createStaticService() {
351 return DisplayManager.getInstance();
352 }});
353
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800354 registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() {
355 public Object createService(ContextImpl ctx) {
356 return InputMethodManager.getInstance(ctx);
357 }});
358
satok988323c2011-06-22 16:38:13 +0900359 registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
360 public Object createService(ContextImpl ctx) {
361 return TextServicesManager.getInstance();
362 }});
363
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800364 registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
365 public Object getService(ContextImpl ctx) {
366 // TODO: why isn't this caching it? It wasn't
367 // before, so I'm preserving the old behavior and
368 // using getService(), instead of createService()
369 // which would do the caching.
370 return new KeyguardManager();
371 }});
372
373 registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
374 public Object createService(ContextImpl ctx) {
375 return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
376 }});
377
Nick Pellye0fd6932012-07-11 10:26:13 -0700378 registerService(LOCATION_SERVICE, new ServiceFetcher() {
379 public Object createService(ContextImpl ctx) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800380 IBinder b = ServiceManager.getService(LOCATION_SERVICE);
Nick Pellye0fd6932012-07-11 10:26:13 -0700381 return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800382 }});
383
Jeff Sharkey1a303952011-06-16 13:04:20 -0700384 registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
385 @Override
386 public Object createService(ContextImpl ctx) {
387 return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
388 ServiceManager.getService(NETWORK_POLICY_SERVICE)));
389 }
390 });
391
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800392 registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
393 public Object createService(ContextImpl ctx) {
394 final Context outerContext = ctx.getOuterContext();
395 return new NotificationManager(
396 new ContextThemeWrapper(outerContext,
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800397 Resources.selectSystemTheme(0,
398 outerContext.getApplicationInfo().targetSdkVersion,
399 com.android.internal.R.style.Theme_Dialog,
Adam Powell6e90a362011-08-14 16:48:32 -0700400 com.android.internal.R.style.Theme_Holo_Dialog,
401 com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800402 ctx.mMainThread.getHandler());
403 }});
404
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700405 registerService(NSD_SERVICE, new ServiceFetcher() {
406 @Override
407 public Object createService(ContextImpl ctx) {
408 IBinder b = ServiceManager.getService(NSD_SERVICE);
409 INsdManager service = INsdManager.Stub.asInterface(b);
Irfan Sheriff22af38c2012-05-03 16:44:27 -0700410 return new NsdManager(ctx.getOuterContext(), service);
Irfan Sheriff7d024d32012-03-22 17:01:39 -0700411 }});
412
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800413 // Note: this was previously cached in a static variable, but
414 // constructed using mMainThread.getHandler(), so converting
415 // it to be a regular Context-cached service...
416 registerService(POWER_SERVICE, new ServiceFetcher() {
417 public Object createService(ContextImpl ctx) {
418 IBinder b = ServiceManager.getService(POWER_SERVICE);
419 IPowerManager service = IPowerManager.Stub.asInterface(b);
420 return new PowerManager(service, ctx.mMainThread.getHandler());
421 }});
422
423 registerService(SEARCH_SERVICE, new ServiceFetcher() {
424 public Object createService(ContextImpl ctx) {
425 return new SearchManager(ctx.getOuterContext(),
426 ctx.mMainThread.getHandler());
427 }});
428
429 registerService(SENSOR_SERVICE, new ServiceFetcher() {
430 public Object createService(ContextImpl ctx) {
Jeff Brown25157e42012-04-16 12:13:05 -0700431 return new SystemSensorManager(ctx.mMainThread.getHandler().getLooper());
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800432 }});
433
434 registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
435 public Object createService(ContextImpl ctx) {
436 return new StatusBarManager(ctx.getOuterContext());
437 }});
438
439 registerService(STORAGE_SERVICE, new ServiceFetcher() {
440 public Object createService(ContextImpl ctx) {
441 try {
442 return new StorageManager(ctx.mMainThread.getHandler().getLooper());
443 } catch (RemoteException rex) {
444 Log.e(TAG, "Failed to create StorageManager", rex);
445 return null;
446 }
447 }});
448
449 registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
450 public Object createService(ContextImpl ctx) {
451 return new TelephonyManager(ctx.getOuterContext());
452 }});
453
454 registerService(THROTTLE_SERVICE, new StaticServiceFetcher() {
455 public Object createStaticService() {
456 IBinder b = ServiceManager.getService(THROTTLE_SERVICE);
457 return new ThrottleManager(IThrottleManager.Stub.asInterface(b));
458 }});
459
460 registerService(UI_MODE_SERVICE, new ServiceFetcher() {
461 public Object createService(ContextImpl ctx) {
462 return new UiModeManager();
463 }});
464
Mike Lockwood3a68b832011-03-08 10:08:59 -0500465 registerService(USB_SERVICE, new ServiceFetcher() {
466 public Object createService(ContextImpl ctx) {
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500467 IBinder b = ServiceManager.getService(USB_SERVICE);
Mike Lockwood3a68b832011-03-08 10:08:59 -0500468 return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
Mike Lockwoode7d511e2010-12-30 13:39:37 -0500469 }});
470
Mike Lockwoodb01e8bf2011-08-29 20:11:07 -0400471 registerService(SERIAL_SERVICE, new ServiceFetcher() {
472 public Object createService(ContextImpl ctx) {
473 IBinder b = ServiceManager.getService(SERIAL_SERVICE);
474 return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
475 }});
476
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800477 registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
478 public Object createService(ContextImpl ctx) {
Jeff Brownc2346132012-04-13 01:55:38 -0700479 return new SystemVibrator();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800480 }});
481
482 registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
483
484 registerService(WIFI_SERVICE, new ServiceFetcher() {
485 public Object createService(ContextImpl ctx) {
486 IBinder b = ServiceManager.getService(WIFI_SERVICE);
487 IWifiManager service = IWifiManager.Stub.asInterface(b);
Irfan Sheriff88759bb2012-07-02 15:58:28 -0700488 return new WifiManager(ctx.getOuterContext(), service);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800489 }});
490
repo sync55bc5f32011-06-24 14:23:07 -0700491 registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
492 public Object createService(ContextImpl ctx) {
493 IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
494 IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
495 return new WifiP2pManager(service);
496 }});
497
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800498 registerService(WINDOW_SERVICE, new ServiceFetcher() {
499 public Object getService(ContextImpl ctx) {
Jeff Brownd32460c2012-07-20 16:15:36 -0700500 return WindowManagerImpl.getDefault().makeCompatible(
501 ctx.mPackageInfo.mCompatibilityInfo);
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800502 }});
Amith Yamasani258848d2012-08-10 17:06:33 -0700503
504 registerService(USER_SERVICE, new ServiceFetcher() {
505 public Object getService(ContextImpl ctx) {
506 IBinder b = ServiceManager.getService(USER_SERVICE);
507 IUserManager service = IUserManager.Stub.asInterface(b);
508 return new UserManager(ctx, service);
509 }});
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800510 }
511
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700512 static ContextImpl getImpl(Context context) {
513 Context nextContext;
514 while ((context instanceof ContextWrapper) &&
515 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
516 context = nextContext;
517 }
518 return (ContextImpl)context;
519 }
520
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800521 // The system service cache for the system services that are
522 // cached per-ContextImpl. Package-scoped to avoid accessor
523 // methods.
524 final ArrayList<Object> mServiceCache = new ArrayList<Object>();
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 @Override
527 public AssetManager getAssets() {
528 return mResources.getAssets();
529 }
530
531 @Override
532 public Resources getResources() {
533 return mResources;
534 }
535
536 @Override
537 public PackageManager getPackageManager() {
538 if (mPackageManager != null) {
539 return mPackageManager;
540 }
541
542 IPackageManager pm = ActivityThread.getPackageManager();
543 if (pm != null) {
544 // Doesn't matter if we make more than one instance.
545 return (mPackageManager = new ApplicationPackageManager(this, pm));
546 }
547
548 return null;
549 }
550
551 @Override
552 public ContentResolver getContentResolver() {
553 return mContentResolver;
554 }
555
556 @Override
557 public Looper getMainLooper() {
558 return mMainThread.getLooper();
559 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 @Override
562 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700563 return (mPackageInfo != null) ?
564 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 @Override
568 public void setTheme(int resid) {
569 mThemeResource = resid;
570 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800573 public int getThemeResId() {
574 return mThemeResource;
575 }
576
577 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 public Resources.Theme getTheme() {
579 if (mTheme == null) {
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800580 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
581 getOuterContext().getApplicationInfo().targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 mTheme = mResources.newTheme();
583 mTheme.applyStyle(mThemeResource, true);
584 }
585 return mTheme;
586 }
587
588 @Override
589 public ClassLoader getClassLoader() {
590 return mPackageInfo != null ?
591 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
592 }
593
594 @Override
595 public String getPackageName() {
596 if (mPackageInfo != null) {
597 return mPackageInfo.getPackageName();
598 }
599 throw new RuntimeException("Not supported in system context");
600 }
601
602 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700603 public ApplicationInfo getApplicationInfo() {
604 if (mPackageInfo != null) {
605 return mPackageInfo.getApplicationInfo();
606 }
607 throw new RuntimeException("Not supported in system context");
608 }
609
610 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 public String getPackageResourcePath() {
612 if (mPackageInfo != null) {
613 return mPackageInfo.getResDir();
614 }
615 throw new RuntimeException("Not supported in system context");
616 }
617
618 @Override
619 public String getPackageCodePath() {
620 if (mPackageInfo != null) {
621 return mPackageInfo.getAppDir();
622 }
623 throw new RuntimeException("Not supported in system context");
624 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200625
Joe Onorato23ecae32009-06-10 17:07:15 -0700626 public File getSharedPrefsFile(String name) {
627 return makeFilename(getPreferencesDir(), name + ".xml");
628 }
629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 @Override
631 public SharedPreferences getSharedPreferences(String name, int mode) {
632 SharedPreferencesImpl sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 synchronized (sSharedPrefs) {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700634 sp = sSharedPrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700635 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800636 File prefsFile = getSharedPrefsFile(name);
637 sp = new SharedPreferencesImpl(prefsFile, mode);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700638 sSharedPrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700639 return sp;
640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800642 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
643 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
644 // If somebody else (some other process) changed the prefs
645 // file behind our back, we reload it. This has been the
646 // historical (if undocumented) behavior.
647 sp.startReloadIfChangedUnexpectedly();
648 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700649 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651
652 private File getPreferencesDir() {
653 synchronized (mSync) {
654 if (mPreferencesDir == null) {
655 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
656 }
657 return mPreferencesDir;
658 }
659 }
660
661 @Override
662 public FileInputStream openFileInput(String name)
663 throws FileNotFoundException {
664 File f = makeFilename(getFilesDir(), name);
665 return new FileInputStream(f);
666 }
667
668 @Override
669 public FileOutputStream openFileOutput(String name, int mode)
670 throws FileNotFoundException {
671 final boolean append = (mode&MODE_APPEND) != 0;
672 File f = makeFilename(getFilesDir(), name);
673 try {
674 FileOutputStream fos = new FileOutputStream(f, append);
675 setFilePermissionsFromMode(f.getPath(), mode, 0);
676 return fos;
677 } catch (FileNotFoundException e) {
678 }
679
680 File parent = f.getParentFile();
681 parent.mkdir();
682 FileUtils.setPermissions(
683 parent.getPath(),
684 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
685 -1, -1);
686 FileOutputStream fos = new FileOutputStream(f, append);
687 setFilePermissionsFromMode(f.getPath(), mode, 0);
688 return fos;
689 }
690
691 @Override
692 public boolean deleteFile(String name) {
693 File f = makeFilename(getFilesDir(), name);
694 return f.delete();
695 }
696
697 @Override
698 public File getFilesDir() {
699 synchronized (mSync) {
700 if (mFilesDir == null) {
701 mFilesDir = new File(getDataDirFile(), "files");
702 }
703 if (!mFilesDir.exists()) {
704 if(!mFilesDir.mkdirs()) {
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200705 Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 return null;
707 }
708 FileUtils.setPermissions(
709 mFilesDir.getPath(),
710 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
711 -1, -1);
712 }
713 return mFilesDir;
714 }
715 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 @Override
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800718 public File getExternalFilesDir(String type) {
719 synchronized (mSync) {
720 if (mExternalFilesDir == null) {
721 mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory(
722 getPackageName());
723 }
724 if (!mExternalFilesDir.exists()) {
725 try {
726 (new File(Environment.getExternalStorageAndroidDataDir(),
727 ".nomedia")).createNewFile();
728 } catch (IOException e) {
729 }
730 if (!mExternalFilesDir.mkdirs()) {
731 Log.w(TAG, "Unable to create external files directory");
732 return null;
733 }
734 }
735 if (type == null) {
736 return mExternalFilesDir;
737 }
738 File dir = new File(mExternalFilesDir, type);
739 if (!dir.exists()) {
740 if (!dir.mkdirs()) {
741 Log.w(TAG, "Unable to create external media directory " + dir);
742 return null;
743 }
744 }
745 return dir;
746 }
747 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200748
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800749 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800750 public File getObbDir() {
751 synchronized (mSync) {
752 if (mObbDir == null) {
753 mObbDir = Environment.getExternalStorageAppObbDirectory(
754 getPackageName());
755 }
756 return mObbDir;
757 }
758 }
759
760 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 public File getCacheDir() {
762 synchronized (mSync) {
763 if (mCacheDir == null) {
764 mCacheDir = new File(getDataDirFile(), "cache");
765 }
766 if (!mCacheDir.exists()) {
767 if(!mCacheDir.mkdirs()) {
768 Log.w(TAG, "Unable to create cache directory");
769 return null;
770 }
771 FileUtils.setPermissions(
772 mCacheDir.getPath(),
773 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
774 -1, -1);
775 }
776 }
777 return mCacheDir;
778 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200779
Dianne Hackborne83cefc2010-02-04 17:38:14 -0800780 @Override
781 public File getExternalCacheDir() {
782 synchronized (mSync) {
783 if (mExternalCacheDir == null) {
784 mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory(
785 getPackageName());
786 }
787 if (!mExternalCacheDir.exists()) {
788 try {
789 (new File(Environment.getExternalStorageAndroidDataDir(),
790 ".nomedia")).createNewFile();
791 } catch (IOException e) {
792 }
793 if (!mExternalCacheDir.mkdirs()) {
794 Log.w(TAG, "Unable to create external cache directory");
795 return null;
796 }
797 }
798 return mExternalCacheDir;
799 }
800 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 @Override
803 public File getFileStreamPath(String name) {
804 return makeFilename(getFilesDir(), name);
805 }
806
807 @Override
808 public String[] fileList() {
809 final String[] list = getFilesDir().list();
810 return (list != null) ? list : EMPTY_FILE_LIST;
811 }
812
813 @Override
814 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700815 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 }
817
818 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700819 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
820 DatabaseErrorHandler errorHandler) {
821 File f = validateFilePath(name, true);
Jeff Brown47847f32012-03-22 19:13:11 -0700822 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
823 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
824 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
825 }
826 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700827 setFilePermissionsFromMode(f.getPath(), mode, 0);
828 return db;
829 }
830
831 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 public boolean deleteDatabase(String name) {
833 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800834 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800835 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 } catch (Exception e) {
837 }
838 return false;
839 }
840
841 @Override
842 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800843 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
845
846 @Override
847 public String[] databaseList() {
848 final String[] list = getDatabasesDir().list();
849 return (list != null) ? list : EMPTY_FILE_LIST;
850 }
851
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 private File getDatabasesDir() {
854 synchronized (mSync) {
855 if (mDatabasesDir == null) {
856 mDatabasesDir = new File(getDataDirFile(), "databases");
857 }
858 if (mDatabasesDir.getPath().equals("databases")) {
859 mDatabasesDir = new File("/data/system");
860 }
861 return mDatabasesDir;
862 }
863 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 @Override
866 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700867 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 }
869
870 @Override
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700871 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700872 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 }
874
875 @Override
876 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700877 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
880 @Override
881 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700882 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 }
884
885 @Override
886 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700887 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889
890 @Override
891 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700892 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
895 @Override
896 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700897 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 }
899
900 @Override
901 public void startActivity(Intent intent) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700902 startActivity(intent, null);
903 }
904
Amith Yamasani82644082012-08-03 13:09:11 -0700905 /** @hide */
906 @Override
907 public void startActivityAsUser(Intent intent, int userId) {
908 try {
909 ActivityManagerNative.getDefault().startActivityAsUser(
910 mMainThread.getApplicationThread(), intent,
911 intent.resolveTypeIfNeeded(getContentResolver()),
912 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, null, userId);
913 } catch (RemoteException re) {
914 }
915 }
916
Dianne Hackborna4972e92012-03-14 10:38:05 -0700917 @Override
918 public void startActivity(Intent intent, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
920 throw new AndroidRuntimeException(
921 "Calling startActivity() from outside of an Activity "
922 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
923 + " Is this really what you want?");
924 }
925 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700926 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700927 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929
Amith Yamasani258848d2012-08-10 17:06:33 -0700930 /** @hide */
931 @Override
932 public void startActivityAsUser(Intent intent, Bundle options, int userId) {
933 try {
934 ActivityManagerNative.getDefault().startActivityAsUser(
935 mMainThread.getApplicationThread(), intent,
936 intent.resolveTypeIfNeeded(getContentResolver()),
937 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options, userId);
938 } catch (RemoteException re) {
939 }
940 }
941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800943 public void startActivities(Intent[] intents) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700944 startActivities(intents, null);
945 }
946
947 @Override
948 public void startActivities(Intent[] intents, Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800949 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
950 throw new AndroidRuntimeException(
951 "Calling startActivities() from outside of an Activity "
952 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
953 + " Is this really what you want?");
954 }
955 mMainThread.getInstrumentation().execStartActivities(
956 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700957 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800958 }
959
960 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700961 public void startIntentSender(IntentSender intent,
962 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
963 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700964 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
965 }
966
967 @Override
968 public void startIntentSender(IntentSender intent, Intent fillInIntent,
969 int flagsMask, int flagsValues, int extraFlags, Bundle options)
970 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700971 try {
972 String resolvedType = null;
973 if (fillInIntent != null) {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400974 fillInIntent.setAllowFds(false);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700975 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
976 }
977 int result = ActivityManagerNative.getDefault()
978 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
979 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700980 0, flagsMask, flagsValues, options);
981 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700982 throw new IntentSender.SendIntentException();
983 }
984 Instrumentation.checkStartActivityResult(result, null);
985 } catch (RemoteException e) {
986 }
987 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200988
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700989 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 public void sendBroadcast(Intent intent) {
991 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
992 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400993 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 ActivityManagerNative.getDefault().broadcastIntent(
995 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700996 Activity.RESULT_OK, null, null, null, false, false,
997 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 } catch (RemoteException e) {
999 }
1000 }
1001
Amith Yamasani67cf7d32012-02-16 14:31:23 -08001002 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 public void sendBroadcast(Intent intent, String receiverPermission) {
1004 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1005 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001006 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 ActivityManagerNative.getDefault().broadcastIntent(
1008 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001009 Activity.RESULT_OK, null, null, receiverPermission, false, false,
1010 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 } catch (RemoteException e) {
1012 }
1013 }
1014
1015 @Override
1016 public void sendOrderedBroadcast(Intent intent,
1017 String receiverPermission) {
1018 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1019 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001020 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 ActivityManagerNative.getDefault().broadcastIntent(
1022 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001023 Activity.RESULT_OK, null, null, receiverPermission, true, false,
1024 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 } catch (RemoteException e) {
1026 }
1027 }
1028
1029 @Override
1030 public void sendOrderedBroadcast(Intent intent,
1031 String receiverPermission, BroadcastReceiver resultReceiver,
1032 Handler scheduler, int initialCode, String initialData,
1033 Bundle initialExtras) {
1034 IIntentReceiver rd = null;
1035 if (resultReceiver != null) {
1036 if (mPackageInfo != null) {
1037 if (scheduler == null) {
1038 scheduler = mMainThread.getHandler();
1039 }
1040 rd = mPackageInfo.getReceiverDispatcher(
1041 resultReceiver, getOuterContext(), scheduler,
1042 mMainThread.getInstrumentation(), false);
1043 } else {
1044 if (scheduler == null) {
1045 scheduler = mMainThread.getHandler();
1046 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001047 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1049 }
1050 }
1051 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1052 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001053 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 ActivityManagerNative.getDefault().broadcastIntent(
1055 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1056 initialCode, initialData, initialExtras, receiverPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -07001057 true, false, Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 } catch (RemoteException e) {
1059 }
1060 }
1061
1062 @Override
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001063 public void sendBroadcastToUser(Intent intent, int userHandle) {
1064 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1065 try {
1066 intent.setAllowFds(false);
1067 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
1068 intent, resolvedType, null, Activity.RESULT_OK, null, null, null, false, false,
1069 userHandle);
1070 } catch (RemoteException e) {
1071 }
1072 }
1073
1074 @Override
1075 public void sendOrderedBroadcastToUser(Intent intent, int userHandle,
1076 BroadcastReceiver resultReceiver, Handler scheduler,
1077 int initialCode, String initialData, Bundle initialExtras) {
1078 IIntentReceiver rd = null;
1079 if (resultReceiver != null) {
1080 if (mPackageInfo != null) {
1081 if (scheduler == null) {
1082 scheduler = mMainThread.getHandler();
1083 }
1084 rd = mPackageInfo.getReceiverDispatcher(
1085 resultReceiver, getOuterContext(), scheduler,
1086 mMainThread.getInstrumentation(), false);
1087 } else {
1088 if (scheduler == null) {
1089 scheduler = mMainThread.getHandler();
1090 }
1091 rd = new LoadedApk.ReceiverDispatcher(
1092 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1093 }
1094 }
1095 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1096 try {
1097 intent.setAllowFds(false);
1098 ActivityManagerNative.getDefault().broadcastIntent(
1099 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1100 initialCode, initialData, initialExtras, null,
1101 true, false, userHandle);
1102 } catch (RemoteException e) {
1103 }
1104 }
1105
1106 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 public void sendStickyBroadcast(Intent intent) {
1108 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1109 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001110 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 ActivityManagerNative.getDefault().broadcastIntent(
1112 mMainThread.getApplicationThread(), intent, resolvedType, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001113 Activity.RESULT_OK, null, null, null, false, true,
1114 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 } catch (RemoteException e) {
1116 }
1117 }
1118
1119 @Override
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001120 public void sendStickyOrderedBroadcast(Intent intent,
1121 BroadcastReceiver resultReceiver,
1122 Handler scheduler, int initialCode, String initialData,
1123 Bundle initialExtras) {
1124 IIntentReceiver rd = null;
1125 if (resultReceiver != null) {
1126 if (mPackageInfo != null) {
1127 if (scheduler == null) {
1128 scheduler = mMainThread.getHandler();
1129 }
1130 rd = mPackageInfo.getReceiverDispatcher(
1131 resultReceiver, getOuterContext(), scheduler,
1132 mMainThread.getInstrumentation(), false);
1133 } else {
1134 if (scheduler == null) {
1135 scheduler = mMainThread.getHandler();
1136 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001137 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001138 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1139 }
1140 }
1141 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1142 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001143 intent.setAllowFds(false);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001144 ActivityManagerNative.getDefault().broadcastIntent(
1145 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1146 initialCode, initialData, initialExtras, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07001147 true, true, Binder.getOrigCallingUser());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001148 } catch (RemoteException e) {
1149 }
1150 }
1151
1152 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 public void removeStickyBroadcast(Intent intent) {
1154 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1155 if (resolvedType != null) {
1156 intent = new Intent(intent);
1157 intent.setDataAndType(intent.getData(), resolvedType);
1158 }
1159 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001160 intent.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 ActivityManagerNative.getDefault().unbroadcastIntent(
Amith Yamasani742a6712011-05-04 14:49:28 -07001162 mMainThread.getApplicationThread(), intent, Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 } catch (RemoteException e) {
1164 }
1165 }
1166
1167 @Override
1168 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1169 return registerReceiver(receiver, filter, null, null);
1170 }
1171
1172 @Override
1173 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1174 String broadcastPermission, Handler scheduler) {
1175 return registerReceiverInternal(receiver, filter, broadcastPermission,
1176 scheduler, getOuterContext());
1177 }
1178
1179 private Intent registerReceiverInternal(BroadcastReceiver receiver,
1180 IntentFilter filter, String broadcastPermission,
1181 Handler scheduler, Context context) {
1182 IIntentReceiver rd = null;
1183 if (receiver != null) {
1184 if (mPackageInfo != null && context != null) {
1185 if (scheduler == null) {
1186 scheduler = mMainThread.getHandler();
1187 }
1188 rd = mPackageInfo.getReceiverDispatcher(
1189 receiver, context, scheduler,
1190 mMainThread.getInstrumentation(), true);
1191 } else {
1192 if (scheduler == null) {
1193 scheduler = mMainThread.getHandler();
1194 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001195 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001196 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 }
1198 }
1199 try {
1200 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001201 mMainThread.getApplicationThread(), mBasePackageName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 rd, filter, broadcastPermission);
1203 } catch (RemoteException e) {
1204 return null;
1205 }
1206 }
1207
1208 @Override
1209 public void unregisterReceiver(BroadcastReceiver receiver) {
1210 if (mPackageInfo != null) {
1211 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1212 getOuterContext(), receiver);
1213 try {
1214 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1215 } catch (RemoteException e) {
1216 }
1217 } else {
1218 throw new RuntimeException("Not supported in system context");
1219 }
1220 }
1221
1222 @Override
1223 public ComponentName startService(Intent service) {
1224 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001225 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 ComponentName cn = ActivityManagerNative.getDefault().startService(
1227 mMainThread.getApplicationThread(), service,
1228 service.resolveTypeIfNeeded(getContentResolver()));
1229 if (cn != null && cn.getPackageName().equals("!")) {
1230 throw new SecurityException(
1231 "Not allowed to start service " + service
1232 + " without permission " + cn.getClassName());
1233 }
1234 return cn;
1235 } catch (RemoteException e) {
1236 return null;
1237 }
1238 }
1239
1240 @Override
1241 public boolean stopService(Intent service) {
1242 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001243 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 int res = ActivityManagerNative.getDefault().stopService(
1245 mMainThread.getApplicationThread(), service,
1246 service.resolveTypeIfNeeded(getContentResolver()));
1247 if (res < 0) {
1248 throw new SecurityException(
1249 "Not allowed to stop service " + service);
1250 }
1251 return res != 0;
1252 } catch (RemoteException e) {
1253 return false;
1254 }
1255 }
1256
1257 @Override
1258 public boolean bindService(Intent service, ServiceConnection conn,
1259 int flags) {
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001260 return bindService(service, conn, flags, UserId.getUserId(Process.myUid()));
1261 }
1262
1263 /** @hide */
1264 @Override
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001265 public boolean bindService(Intent service, ServiceConnection conn, int flags, int userHandle) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001267 if (conn == null) {
1268 throw new IllegalArgumentException("connection is null");
1269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 if (mPackageInfo != null) {
1271 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1272 mMainThread.getHandler(), flags);
1273 } else {
1274 throw new RuntimeException("Not supported in system context");
1275 }
1276 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001277 IBinder token = getActivityToken();
1278 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1279 && mPackageInfo.getApplicationInfo().targetSdkVersion
1280 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1281 flags |= BIND_WAIVE_PRIORITY;
1282 }
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001283 service.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 int res = ActivityManagerNative.getDefault().bindService(
1285 mMainThread.getApplicationThread(), getActivityToken(),
1286 service, service.resolveTypeIfNeeded(getContentResolver()),
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001287 sd, flags, userHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 if (res < 0) {
1289 throw new SecurityException(
1290 "Not allowed to bind to service " + service);
1291 }
1292 return res != 0;
1293 } catch (RemoteException e) {
1294 return false;
1295 }
1296 }
1297
1298 @Override
1299 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001300 if (conn == null) {
1301 throw new IllegalArgumentException("connection is null");
1302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 if (mPackageInfo != null) {
1304 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1305 getOuterContext(), conn);
1306 try {
1307 ActivityManagerNative.getDefault().unbindService(sd);
1308 } catch (RemoteException e) {
1309 }
1310 } else {
1311 throw new RuntimeException("Not supported in system context");
1312 }
1313 }
1314
1315 @Override
1316 public boolean startInstrumentation(ComponentName className,
1317 String profileFile, Bundle arguments) {
1318 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001319 if (arguments != null) {
1320 arguments.setAllowFds(false);
1321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 return ActivityManagerNative.getDefault().startInstrumentation(
1323 className, profileFile, 0, arguments, null);
1324 } catch (RemoteException e) {
1325 // System has crashed, nothing we can do.
1326 }
1327 return false;
1328 }
1329
1330 @Override
1331 public Object getSystemService(String name) {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001332 ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
1333 return fetcher == null ? null : fetcher.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 }
1335
Dianne Hackborn8cc6a502009-08-05 21:29:42 -07001336 private WallpaperManager getWallpaperManager() {
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -08001337 return (WallpaperManager) WALLPAPER_FETCHER.getService(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 }
1339
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001340 /* package */ static DropBoxManager createDropBoxManager() {
1341 IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
1342 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001343 if (service == null) {
1344 // Don't return a DropBoxManager that will NPE upon use.
1345 // This also avoids caching a broken DropBoxManager in
1346 // getDropBoxManager during early boot, before the
1347 // DROPBOX_SERVICE is registered.
1348 return null;
1349 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001350 return new DropBoxManager(service);
1351 }
1352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 @Override
1354 public int checkPermission(String permission, int pid, int uid) {
1355 if (permission == null) {
1356 throw new IllegalArgumentException("permission is null");
1357 }
1358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 try {
1360 return ActivityManagerNative.getDefault().checkPermission(
1361 permission, pid, uid);
1362 } catch (RemoteException e) {
1363 return PackageManager.PERMISSION_DENIED;
1364 }
1365 }
1366
1367 @Override
1368 public int checkCallingPermission(String permission) {
1369 if (permission == null) {
1370 throw new IllegalArgumentException("permission is null");
1371 }
1372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 int pid = Binder.getCallingPid();
1374 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001375 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
1377 return PackageManager.PERMISSION_DENIED;
1378 }
1379
1380 @Override
1381 public int checkCallingOrSelfPermission(String permission) {
1382 if (permission == null) {
1383 throw new IllegalArgumentException("permission is null");
1384 }
1385
1386 return checkPermission(permission, Binder.getCallingPid(),
1387 Binder.getCallingUid());
1388 }
1389
1390 private void enforce(
1391 String permission, int resultOfCheck,
1392 boolean selfToo, int uid, String message) {
1393 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1394 throw new SecurityException(
1395 (message != null ? (message + ": ") : "") +
1396 (selfToo
1397 ? "Neither user " + uid + " nor current process has "
1398 : "User " + uid + " does not have ") +
1399 permission +
1400 ".");
1401 }
1402 }
1403
1404 public void enforcePermission(
1405 String permission, int pid, int uid, String message) {
1406 enforce(permission,
1407 checkPermission(permission, pid, uid),
1408 false,
1409 uid,
1410 message);
1411 }
1412
1413 public void enforceCallingPermission(String permission, String message) {
1414 enforce(permission,
1415 checkCallingPermission(permission),
1416 false,
1417 Binder.getCallingUid(),
1418 message);
1419 }
1420
1421 public void enforceCallingOrSelfPermission(
1422 String permission, String message) {
1423 enforce(permission,
1424 checkCallingOrSelfPermission(permission),
1425 true,
1426 Binder.getCallingUid(),
1427 message);
1428 }
1429
1430 @Override
1431 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1432 try {
1433 ActivityManagerNative.getDefault().grantUriPermission(
1434 mMainThread.getApplicationThread(), toPackage, uri,
1435 modeFlags);
1436 } catch (RemoteException e) {
1437 }
1438 }
1439
1440 @Override
1441 public void revokeUriPermission(Uri uri, int modeFlags) {
1442 try {
1443 ActivityManagerNative.getDefault().revokeUriPermission(
1444 mMainThread.getApplicationThread(), uri,
1445 modeFlags);
1446 } catch (RemoteException e) {
1447 }
1448 }
1449
1450 @Override
1451 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 try {
1453 return ActivityManagerNative.getDefault().checkUriPermission(
1454 uri, pid, uid, modeFlags);
1455 } catch (RemoteException e) {
1456 return PackageManager.PERMISSION_DENIED;
1457 }
1458 }
1459
1460 @Override
1461 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 int pid = Binder.getCallingPid();
1463 if (pid != Process.myPid()) {
1464 return checkUriPermission(uri, pid,
1465 Binder.getCallingUid(), modeFlags);
1466 }
1467 return PackageManager.PERMISSION_DENIED;
1468 }
1469
1470 @Override
1471 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1472 return checkUriPermission(uri, Binder.getCallingPid(),
1473 Binder.getCallingUid(), modeFlags);
1474 }
1475
1476 @Override
1477 public int checkUriPermission(Uri uri, String readPermission,
1478 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001479 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1481 + readPermission + " writePermission=" + writePermission
1482 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1483 }
1484 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1485 if (readPermission == null
1486 || checkPermission(readPermission, pid, uid)
1487 == PackageManager.PERMISSION_GRANTED) {
1488 return PackageManager.PERMISSION_GRANTED;
1489 }
1490 }
1491 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1492 if (writePermission == null
1493 || checkPermission(writePermission, pid, uid)
1494 == PackageManager.PERMISSION_GRANTED) {
1495 return PackageManager.PERMISSION_GRANTED;
1496 }
1497 }
1498 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1499 : PackageManager.PERMISSION_DENIED;
1500 }
1501
1502 private String uriModeFlagToString(int uriModeFlags) {
1503 switch (uriModeFlags) {
1504 case Intent.FLAG_GRANT_READ_URI_PERMISSION |
1505 Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1506 return "read and write";
1507 case Intent.FLAG_GRANT_READ_URI_PERMISSION:
1508 return "read";
1509 case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1510 return "write";
1511 }
1512 throw new IllegalArgumentException(
1513 "Unknown permission mode flags: " + uriModeFlags);
1514 }
1515
1516 private void enforceForUri(
1517 int modeFlags, int resultOfCheck, boolean selfToo,
1518 int uid, Uri uri, String message) {
1519 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1520 throw new SecurityException(
1521 (message != null ? (message + ": ") : "") +
1522 (selfToo
1523 ? "Neither user " + uid + " nor current process has "
1524 : "User " + uid + " does not have ") +
1525 uriModeFlagToString(modeFlags) +
1526 " permission on " +
1527 uri +
1528 ".");
1529 }
1530 }
1531
1532 public void enforceUriPermission(
1533 Uri uri, int pid, int uid, int modeFlags, String message) {
1534 enforceForUri(
1535 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1536 false, uid, uri, message);
1537 }
1538
1539 public void enforceCallingUriPermission(
1540 Uri uri, int modeFlags, String message) {
1541 enforceForUri(
1542 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001543 false,
1544 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 }
1546
1547 public void enforceCallingOrSelfUriPermission(
1548 Uri uri, int modeFlags, String message) {
1549 enforceForUri(
1550 modeFlags,
1551 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1552 Binder.getCallingUid(), uri, message);
1553 }
1554
1555 public void enforceUriPermission(
1556 Uri uri, String readPermission, String writePermission,
1557 int pid, int uid, int modeFlags, String message) {
1558 enforceForUri(modeFlags,
1559 checkUriPermission(
1560 uri, readPermission, writePermission, pid, uid,
1561 modeFlags),
1562 false,
1563 uid,
1564 uri,
1565 message);
1566 }
1567
1568 @Override
1569 public Context createPackageContext(String packageName, int flags)
1570 throws PackageManager.NameNotFoundException {
1571 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Sharkey816e4f72012-04-22 17:08:52 -07001572 final ContextImpl context = new ContextImpl(mMainThread.getSystemContext());
1573 context.mBasePackageName = mBasePackageName;
1574 return context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001577 LoadedApk pi =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001578 mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 if (pi != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001580 ContextImpl c = new ContextImpl();
Romain Guy870e09f2009-07-06 16:35:25 -07001581 c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001582 c.init(pi, null, mMainThread, mResources, mBasePackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 if (c.mResources != null) {
1584 return c;
1585 }
1586 }
1587
1588 // Should be a better exception.
1589 throw new PackageManager.NameNotFoundException(
1590 "Application package " + packageName + " not found");
1591 }
1592
Romain Guy870e09f2009-07-06 16:35:25 -07001593 @Override
1594 public boolean isRestricted() {
1595 return mRestricted;
1596 }
1597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 private File getDataDirFile() {
1599 if (mPackageInfo != null) {
1600 return mPackageInfo.getDataDirFile();
1601 }
1602 throw new RuntimeException("Not supported in system context");
1603 }
1604
1605 @Override
1606 public File getDir(String name, int mode) {
1607 name = "app_" + name;
1608 File file = makeFilename(getDataDirFile(), name);
1609 if (!file.exists()) {
1610 file.mkdir();
1611 setFilePermissionsFromMode(file.getPath(), mode,
1612 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1613 }
1614 return file;
1615 }
1616
Dianne Hackborn21556372010-02-04 16:34:40 -08001617 static ContextImpl createSystemContext(ActivityThread mainThread) {
1618 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 context.init(Resources.getSystem(), mainThread);
1620 return context;
1621 }
1622
Dianne Hackborn21556372010-02-04 16:34:40 -08001623 ContextImpl() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 mOuterContext = this;
1625 }
1626
1627 /**
1628 * Create a new ApplicationContext from an existing one. The new one
1629 * works and operates the same as the one it is copying.
1630 *
1631 * @param context Existing application context.
1632 */
Dianne Hackborn21556372010-02-04 16:34:40 -08001633 public ContextImpl(ContextImpl context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 mPackageInfo = context.mPackageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001635 mBasePackageName = context.mBasePackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 mResources = context.mResources;
1637 mMainThread = context.mMainThread;
1638 mContentResolver = context.mContentResolver;
1639 mOuterContext = this;
1640 }
1641
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001642 final void init(LoadedApk packageInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 IBinder activityToken, ActivityThread mainThread) {
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001644 init(packageInfo, activityToken, mainThread, null, null);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001645 }
1646
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001647 final void init(LoadedApk packageInfo,
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001648 IBinder activityToken, ActivityThread mainThread,
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001649 Resources container, String basePackageName) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 mPackageInfo = packageInfo;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001651 mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 mResources = mPackageInfo.getResources(mainThread);
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001653
Dianne Hackborn559a7872010-04-07 18:19:41 -07001654 if (mResources != null && container != null
1655 && container.getCompatibilityInfo().applicationScale !=
1656 mResources.getCompatibilityInfo().applicationScale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001657 if (DEBUG) {
1658 Log.d(TAG, "loaded context has different scaling. Using container's" +
1659 " compatiblity info:" + container.getDisplayMetrics());
1660 }
1661 mResources = mainThread.getTopLevelResources(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001662 mPackageInfo.getResDir(), container.getCompatibilityInfo());
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 mMainThread = mainThread;
1665 mContentResolver = new ApplicationContentResolver(this, mainThread);
1666
1667 setActivityToken(activityToken);
1668 }
1669
1670 final void init(Resources resources, ActivityThread mainThread) {
1671 mPackageInfo = null;
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001672 mBasePackageName = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 mResources = resources;
1674 mMainThread = mainThread;
1675 mContentResolver = new ApplicationContentResolver(this, mainThread);
1676 }
1677
1678 final void scheduleFinalCleanup(String who, String what) {
1679 mMainThread.scheduleContextCleanup(this, who, what);
1680 }
1681
1682 final void performFinalCleanup(String who, String what) {
1683 //Log.i(TAG, "Cleanup up context: " + this);
1684 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1685 }
1686
1687 final Context getReceiverRestrictedContext() {
1688 if (mReceiverRestrictedContext != null) {
1689 return mReceiverRestrictedContext;
1690 }
1691 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1692 }
1693
1694 final void setActivityToken(IBinder token) {
1695 mActivityToken = token;
1696 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 final void setOuterContext(Context context) {
1699 mOuterContext = context;
1700 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 final Context getOuterContext() {
1703 return mOuterContext;
1704 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 final IBinder getActivityToken() {
1707 return mActivityToken;
1708 }
1709
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08001710 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 int extraPermissions) {
1712 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
1713 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
1714 |extraPermissions;
1715 if ((mode&MODE_WORLD_READABLE) != 0) {
1716 perms |= FileUtils.S_IROTH;
1717 }
1718 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
1719 perms |= FileUtils.S_IWOTH;
1720 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001721 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
1723 + ", perms=0x" + Integer.toHexString(perms));
1724 }
1725 FileUtils.setPermissions(name, perms, -1, -1);
1726 }
1727
Oscar Montemayora8529f62009-11-18 10:14:20 -08001728 private File validateFilePath(String name, boolean createDirectory) {
1729 File dir;
1730 File f;
1731
1732 if (name.charAt(0) == File.separatorChar) {
1733 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
1734 dir = new File(dirPath);
1735 name = name.substring(name.lastIndexOf(File.separatorChar));
1736 f = new File(dir, name);
1737 } else {
1738 dir = getDatabasesDir();
1739 f = makeFilename(dir, name);
1740 }
1741
1742 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
1743 FileUtils.setPermissions(dir.getPath(),
1744 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1745 -1, -1);
1746 }
1747
1748 return f;
1749 }
1750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 private File makeFilename(File base, String name) {
1752 if (name.indexOf(File.separatorChar) < 0) {
1753 return new File(base, name);
1754 }
1755 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08001756 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 }
1758
1759 // ----------------------------------------------------------------------
1760 // ----------------------------------------------------------------------
1761 // ----------------------------------------------------------------------
1762
1763 private static final class ApplicationContentResolver extends ContentResolver {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001764 public ApplicationContentResolver(Context context, ActivityThread mainThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 super(context);
1766 mMainThread = mainThread;
1767 }
1768
1769 @Override
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001770 protected IContentProvider acquireProvider(Context context, String name) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001771 return mMainThread.acquireProvider(context, name, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 }
1773
1774 @Override
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001775 protected IContentProvider acquireExistingProvider(Context context, String name) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001776 return mMainThread.acquireExistingProvider(context, name, true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001777 }
1778
1779 @Override
1780 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001781 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001783
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001784 @Override
1785 protected IContentProvider acquireUnstableProvider(Context c, String name) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001786 return mMainThread.acquireProvider(c, name, false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001787 }
1788
1789 @Override
1790 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001791 return mMainThread.releaseProvider(icp, false);
1792 }
1793
1794 @Override
1795 public void unstableProviderDied(IContentProvider icp) {
1796 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001797 }
1798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 private final ActivityThread mMainThread;
1800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801}