blob: be36af7dab16486b7f7b08020e6d6bcbadc13568 [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
Jeff Brown6e539312015-02-24 18:53:21 -080019import com.android.internal.annotations.GuardedBy;
Jeff Sharkey6d515712012-09-20 16:06:08 -070020import com.android.internal.util.Preconditions;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
Nicolas Prevotd85fc722014-04-16 19:52:08 +010024import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.ContentResolver;
26import android.content.Context;
27import android.content.ContextWrapper;
28import android.content.IContentProvider;
Jeff Brown6e539312015-02-24 18:53:21 -080029import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Intent;
31import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070032import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.ReceiverCallNotAllowedException;
34import android.content.ServiceConnection;
35import android.content.SharedPreferences;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070039import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070041import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070042import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070044import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.database.sqlite.SQLiteDatabase;
46import android.database.sqlite.SQLiteDatabase.CursorFactory;
47import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.graphics.drawable.Drawable;
Jeff Brownfa25bf52012-07-23 19:26:30 -070049import android.hardware.display.DisplayManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080052import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070054import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080055import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.FileUtils;
57import android.os.Handler;
58import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070063import android.os.UserHandle;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070064import android.os.storage.IMountService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070066import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070068import android.util.Slog;
Jeff Brown98365d72012-08-19 20:30:52 -070069import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080070import android.view.DisplayAdjustments;
Dan Egnor95240272009-10-27 18:23:39 -070071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import java.io.File;
73import java.io.FileInputStream;
74import java.io.FileNotFoundException;
75import java.io.FileOutputStream;
76import java.io.IOException;
77import java.io.InputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079class ReceiverRestrictedContext extends ContextWrapper {
80 ReceiverRestrictedContext(Context base) {
81 super(base);
82 }
83
84 @Override
85 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
86 return registerReceiver(receiver, filter, null, null);
87 }
88
89 @Override
90 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
91 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070092 if (receiver == null) {
93 // Allow retrieving current sticky broadcast; this is safe since we
94 // aren't actually registering a receiver.
95 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
96 } else {
97 throw new ReceiverCallNotAllowedException(
98 "BroadcastReceiver components are not allowed to register to receive intents");
99 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 }
101
102 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700103 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
104 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700105 if (receiver == null) {
106 // Allow retrieving current sticky broadcast; this is safe since we
107 // aren't actually registering a receiver.
108 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
109 } else {
110 throw new ReceiverCallNotAllowedException(
111 "BroadcastReceiver components are not allowed to register to receive intents");
112 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700113 }
114
115 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
117 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700118 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 }
120}
121
122/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800123 * Common implementation of Context API, which provides the base
124 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800126class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800127 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700128 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700130 /**
131 * Map from package name, to preference name, to cached preferences.
132 */
133 private static ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>> sSharedPrefs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
Jeff Browndefd4a62014-03-10 21:24:37 -0700135 final ActivityThread mMainThread;
136 final LoadedApk mPackageInfo;
137
138 private final IBinder mActivityToken;
139
140 private final UserHandle mUser;
141
142 private final ApplicationContentResolver mContentResolver;
143
144 private final String mBasePackageName;
145 private final String mOpPackageName;
146
147 private final ResourcesManager mResourcesManager;
148 private final Resources mResources;
149 private final Display mDisplay; // may be null if default display
150 private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments();
Jeff Browndefd4a62014-03-10 21:24:37 -0700151
152 private final boolean mRestricted;
153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
160 private final Object mSync = new Object();
161
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700162 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700164 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700166 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700168 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700169 private File mNoBackupFilesDir;
170 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700172 @GuardedBy("mSync")
173 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700174
175 @GuardedBy("mSync")
176 private File[] mExternalObbDirs;
177 @GuardedBy("mSync")
178 private File[] mExternalFilesDirs;
179 @GuardedBy("mSync")
180 private File[] mExternalCacheDirs;
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700181 @GuardedBy("mSync")
182 private File[] mExternalMediaDirs;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 private static final String[] EMPTY_FILE_LIST = {};
185
Jeff Brown6e539312015-02-24 18:53:21 -0800186 // The system service cache for the system services that are cached per-ContextImpl.
187 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800188
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700189 static ContextImpl getImpl(Context context) {
190 Context nextContext;
191 while ((context instanceof ContextWrapper) &&
192 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
193 context = nextContext;
194 }
195 return (ContextImpl)context;
196 }
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 @Override
199 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700200 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
202
203 @Override
204 public Resources getResources() {
205 return mResources;
206 }
207
208 @Override
209 public PackageManager getPackageManager() {
210 if (mPackageManager != null) {
211 return mPackageManager;
212 }
213
214 IPackageManager pm = ActivityThread.getPackageManager();
215 if (pm != null) {
216 // Doesn't matter if we make more than one instance.
217 return (mPackageManager = new ApplicationPackageManager(this, pm));
218 }
219
220 return null;
221 }
222
223 @Override
224 public ContentResolver getContentResolver() {
225 return mContentResolver;
226 }
227
228 @Override
229 public Looper getMainLooper() {
230 return mMainThread.getLooper();
231 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 @Override
234 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700235 return (mPackageInfo != null) ?
236 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700240 public void setTheme(int resId) {
241 if (mThemeResource != resId) {
242 mThemeResource = resId;
243 initializeTheme();
244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800248 public int getThemeResId() {
249 return mThemeResource;
250 }
251
252 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 public Resources.Theme getTheme() {
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700254 if (mTheme != null) {
255 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700257
258 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
259 getOuterContext().getApplicationInfo().targetSdkVersion);
260 initializeTheme();
261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 return mTheme;
263 }
264
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700265 private void initializeTheme() {
266 if (mTheme == null) {
267 mTheme = mResources.newTheme();
268 }
269 mTheme.applyStyle(mThemeResource, true);
270 }
271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 @Override
273 public ClassLoader getClassLoader() {
274 return mPackageInfo != null ?
275 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
276 }
277
278 @Override
279 public String getPackageName() {
280 if (mPackageInfo != null) {
281 return mPackageInfo.getPackageName();
282 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800283 // No mPackageInfo means this is a Context for the system itself,
284 // and this here is its name.
285 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
287
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800288 /** @hide */
289 @Override
290 public String getBasePackageName() {
291 return mBasePackageName != null ? mBasePackageName : getPackageName();
292 }
293
Dianne Hackborn95d78532013-09-11 09:51:14 -0700294 /** @hide */
295 @Override
296 public String getOpPackageName() {
297 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
298 }
299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700301 public ApplicationInfo getApplicationInfo() {
302 if (mPackageInfo != null) {
303 return mPackageInfo.getApplicationInfo();
304 }
305 throw new RuntimeException("Not supported in system context");
306 }
307
308 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 public String getPackageResourcePath() {
310 if (mPackageInfo != null) {
311 return mPackageInfo.getResDir();
312 }
313 throw new RuntimeException("Not supported in system context");
314 }
315
316 @Override
317 public String getPackageCodePath() {
318 if (mPackageInfo != null) {
319 return mPackageInfo.getAppDir();
320 }
321 throw new RuntimeException("Not supported in system context");
322 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200323
Jeff Brown6e539312015-02-24 18:53:21 -0800324 @Override
Joe Onorato23ecae32009-06-10 17:07:15 -0700325 public File getSharedPrefsFile(String name) {
326 return makeFilename(getPreferencesDir(), name + ".xml");
327 }
328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 @Override
330 public SharedPreferences getSharedPreferences(String name, int mode) {
331 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700332 synchronized (ContextImpl.class) {
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700333 if (sSharedPrefs == null) {
334 sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>();
335 }
336
337 final String packageName = getPackageName();
338 ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
339 if (packagePrefs == null) {
340 packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
341 sSharedPrefs.put(packageName, packagePrefs);
342 }
343
Dianne Hackbornff32f352013-07-18 17:31:13 -0700344 // At least one application in the world actually passes in a null
345 // name. This happened to work because when we generated the file name
346 // we would stringify it to "null.xml". Nice.
347 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
Chet Haasee8222dd2013-09-05 07:44:18 -0700348 Build.VERSION_CODES.KITKAT) {
Dianne Hackbornff32f352013-07-18 17:31:13 -0700349 if (name == null) {
350 name = "null";
351 }
352 }
353
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700354 sp = packagePrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700355 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800356 File prefsFile = getSharedPrefsFile(name);
357 sp = new SharedPreferencesImpl(prefsFile, mode);
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700358 packagePrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700359 return sp;
360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800362 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
363 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
364 // If somebody else (some other process) changed the prefs
365 // file behind our back, we reload it. This has been the
366 // historical (if undocumented) behavior.
367 sp.startReloadIfChangedUnexpectedly();
368 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700369 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 }
371
372 private File getPreferencesDir() {
373 synchronized (mSync) {
374 if (mPreferencesDir == null) {
375 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
376 }
377 return mPreferencesDir;
378 }
379 }
380
381 @Override
382 public FileInputStream openFileInput(String name)
383 throws FileNotFoundException {
384 File f = makeFilename(getFilesDir(), name);
385 return new FileInputStream(f);
386 }
387
388 @Override
389 public FileOutputStream openFileOutput(String name, int mode)
390 throws FileNotFoundException {
391 final boolean append = (mode&MODE_APPEND) != 0;
392 File f = makeFilename(getFilesDir(), name);
393 try {
394 FileOutputStream fos = new FileOutputStream(f, append);
395 setFilePermissionsFromMode(f.getPath(), mode, 0);
396 return fos;
397 } catch (FileNotFoundException e) {
398 }
399
400 File parent = f.getParentFile();
401 parent.mkdir();
402 FileUtils.setPermissions(
403 parent.getPath(),
404 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
405 -1, -1);
406 FileOutputStream fos = new FileOutputStream(f, append);
407 setFilePermissionsFromMode(f.getPath(), mode, 0);
408 return fos;
409 }
410
411 @Override
412 public boolean deleteFile(String name) {
413 File f = makeFilename(getFilesDir(), name);
414 return f.delete();
415 }
416
Christopher Tatea7835b62014-07-11 17:25:57 -0700417 // Common-path handling of app data dir creation
418 private static File createFilesDirLocked(File file) {
419 if (!file.exists()) {
420 if (!file.mkdirs()) {
421 if (file.exists()) {
422 // spurious failure; probably racing with another process for this app
423 return file;
424 }
425 Log.w(TAG, "Unable to create files subdir " + file.getPath());
426 return null;
427 }
428 FileUtils.setPermissions(
429 file.getPath(),
430 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
431 -1, -1);
432 }
433 return file;
434 }
435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 @Override
437 public File getFilesDir() {
438 synchronized (mSync) {
439 if (mFilesDir == null) {
440 mFilesDir = new File(getDataDirFile(), "files");
441 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700442 return createFilesDirLocked(mFilesDir);
443 }
444 }
445
446 @Override
447 public File getNoBackupFilesDir() {
448 synchronized (mSync) {
449 if (mNoBackupFilesDir == null) {
450 mNoBackupFilesDir = new File(getDataDirFile(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700452 return createFilesDirLocked(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
454 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800457 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700458 // Operates on primary external storage
459 return getExternalFilesDirs(type)[0];
460 }
461
462 @Override
463 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800464 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700465 if (mExternalFilesDirs == null) {
466 mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800467 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700468
469 // Splice in requested type, if any
470 File[] dirs = mExternalFilesDirs;
471 if (type != null) {
472 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800473 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700474
475 // Create dirs if needed
476 return ensureDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800477 }
478 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200479
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800480 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800481 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700482 // Operates on primary external storage
483 return getObbDirs()[0];
484 }
485
486 @Override
487 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800488 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700489 if (mExternalObbDirs == null) {
490 mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800491 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700492
493 // Create dirs if needed
494 return ensureDirsExistOrFilter(mExternalObbDirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800495 }
496 }
497
498 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 public File getCacheDir() {
500 synchronized (mSync) {
501 if (mCacheDir == null) {
502 mCacheDir = new File(getDataDirFile(), "cache");
503 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700504 return createFilesDirLocked(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200507
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800508 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700509 public File getCodeCacheDir() {
510 synchronized (mSync) {
511 if (mCodeCacheDir == null) {
512 mCodeCacheDir = new File(getDataDirFile(), "code_cache");
513 }
514 return createFilesDirLocked(mCodeCacheDir);
515 }
516 }
517
518 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800519 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700520 // Operates on primary external storage
521 return getExternalCacheDirs()[0];
522 }
523
524 @Override
525 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800526 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700527 if (mExternalCacheDirs == null) {
528 mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800529 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700530
531 // Create dirs if needed
532 return ensureDirsExistOrFilter(mExternalCacheDirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800533 }
534 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700537 public File[] getExternalMediaDirs() {
538 synchronized (mSync) {
539 if (mExternalMediaDirs == null) {
540 mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
541 }
542
543 // Create dirs if needed
544 return ensureDirsExistOrFilter(mExternalMediaDirs);
545 }
546 }
547
548 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 public File getFileStreamPath(String name) {
550 return makeFilename(getFilesDir(), name);
551 }
552
553 @Override
554 public String[] fileList() {
555 final String[] list = getFilesDir().list();
556 return (list != null) ? list : EMPTY_FILE_LIST;
557 }
558
559 @Override
560 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700561 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563
564 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700565 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
566 DatabaseErrorHandler errorHandler) {
567 File f = validateFilePath(name, true);
Jeff Brown47847f32012-03-22 19:13:11 -0700568 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
569 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
570 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
571 }
572 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700573 setFilePermissionsFromMode(f.getPath(), mode, 0);
574 return db;
575 }
576
577 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 public boolean deleteDatabase(String name) {
579 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800580 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800581 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 } catch (Exception e) {
583 }
584 return false;
585 }
586
587 @Override
588 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800589 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 }
591
592 @Override
593 public String[] databaseList() {
594 final String[] list = getDatabasesDir().list();
595 return (list != null) ? list : EMPTY_FILE_LIST;
596 }
597
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 private File getDatabasesDir() {
600 synchronized (mSync) {
601 if (mDatabasesDir == null) {
602 mDatabasesDir = new File(getDataDirFile(), "databases");
603 }
604 if (mDatabasesDir.getPath().equals("databases")) {
605 mDatabasesDir = new File("/data/system");
606 }
607 return mDatabasesDir;
608 }
609 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800612 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700614 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
616
617 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800618 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700619 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700620 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622
623 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800624 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700626 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 }
628
629 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800630 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700632 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634
635 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800636 @Deprecated
637 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700638 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
640
641 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800642 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700644 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 }
646
647 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800648 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700650 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 }
652
Jeff Brown6e539312015-02-24 18:53:21 -0800653 private WallpaperManager getWallpaperManager() {
654 return getSystemService(WallpaperManager.class);
655 }
656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 @Override
658 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700659 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700660 startActivity(intent, null);
661 }
662
Amith Yamasani82644082012-08-03 13:09:11 -0700663 /** @hide */
664 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700665 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700666 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700667 }
668
Dianne Hackborna4972e92012-03-14 10:38:05 -0700669 @Override
670 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700671 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
673 throw new AndroidRuntimeException(
674 "Calling startActivity() from outside of an Activity "
675 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
676 + " Is this really what you want?");
677 }
678 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700679 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700680 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682
Amith Yamasani258848d2012-08-10 17:06:33 -0700683 /** @hide */
684 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700685 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700686 try {
687 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800688 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700689 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700690 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700691 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700692 } catch (RemoteException e) {
693 throw new RuntimeException("Failure from system", e);
Amith Yamasani258848d2012-08-10 17:06:33 -0700694 }
695 }
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800698 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700699 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700700 startActivities(intents, null);
701 }
702
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700703 /** @hide */
704 @Override
705 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
706 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
707 throw new AndroidRuntimeException(
708 "Calling startActivities() from outside of an Activity "
709 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
710 + " Is this really what you want?");
711 }
712 mMainThread.getInstrumentation().execStartActivitiesAsUser(
713 getOuterContext(), mMainThread.getApplicationThread(), null,
714 (Activity)null, intents, options, userHandle.getIdentifier());
715 }
716
Dianne Hackborna4972e92012-03-14 10:38:05 -0700717 @Override
718 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700719 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800720 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
721 throw new AndroidRuntimeException(
722 "Calling startActivities() from outside of an Activity "
723 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
724 + " Is this really what you want?");
725 }
726 mMainThread.getInstrumentation().execStartActivities(
727 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700728 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800729 }
730
731 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700732 public void startIntentSender(IntentSender intent,
733 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
734 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700735 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
736 }
737
738 @Override
739 public void startIntentSender(IntentSender intent, Intent fillInIntent,
740 int flagsMask, int flagsValues, int extraFlags, Bundle options)
741 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700742 try {
743 String resolvedType = null;
744 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700745 fillInIntent.migrateExtraStreamToClipData();
746 fillInIntent.prepareToLeaveProcess();
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700747 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
748 }
749 int result = ActivityManagerNative.getDefault()
750 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
751 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700752 0, flagsMask, flagsValues, options);
753 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700754 throw new IntentSender.SendIntentException();
755 }
756 Instrumentation.checkStartActivityResult(result, null);
757 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700758 throw new RuntimeException("Failure from system", e);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700759 }
760 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200761
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700764 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
766 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700767 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 ActivityManagerNative.getDefault().broadcastIntent(
769 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800770 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700771 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700773 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775 }
776
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800777 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700779 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
781 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700782 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 ActivityManagerNative.getDefault().broadcastIntent(
784 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800785 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE,
786 false, false, getUserId());
787 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700788 throw new RuntimeException("Failure from system", e);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800789 }
790 }
791
792 @Override
793 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
794 warnIfCallingFromSystemProcess();
795 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
796 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700797 intent.prepareToLeaveProcess();
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800798 ActivityManagerNative.getDefault().broadcastIntent(
799 mMainThread.getApplicationThread(), intent, resolvedType, null,
800 Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700801 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700803 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805 }
806
807 @Override
808 public void sendOrderedBroadcast(Intent intent,
809 String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700810 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
812 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700813 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 ActivityManagerNative.getDefault().broadcastIntent(
815 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800816 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, true, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700817 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700819 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
821 }
822
823 @Override
824 public void sendOrderedBroadcast(Intent intent,
825 String receiverPermission, BroadcastReceiver resultReceiver,
826 Handler scheduler, int initialCode, String initialData,
827 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800828 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
829 resultReceiver, scheduler, initialCode, initialData, initialExtras);
830 }
831
832 @Override
833 public void sendOrderedBroadcast(Intent intent,
834 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
835 Handler scheduler, int initialCode, String initialData,
836 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700837 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 IIntentReceiver rd = null;
839 if (resultReceiver != null) {
840 if (mPackageInfo != null) {
841 if (scheduler == null) {
842 scheduler = mMainThread.getHandler();
843 }
844 rd = mPackageInfo.getReceiverDispatcher(
845 resultReceiver, getOuterContext(), scheduler,
846 mMainThread.getInstrumentation(), false);
847 } else {
848 if (scheduler == null) {
849 scheduler = mMainThread.getHandler();
850 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700851 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
853 }
854 }
855 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
856 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700857 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 ActivityManagerNative.getDefault().broadcastIntent(
859 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800860 initialCode, initialData, initialExtras, receiverPermission, appOp,
861 true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700863 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 }
865 }
866
867 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700868 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700869 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
870 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700871 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700872 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800873 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
874 AppOpsManager.OP_NONE, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700875 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700876 throw new RuntimeException("Failure from system", e);
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700877 }
878 }
879
880 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700881 public void sendBroadcastAsUser(Intent intent, UserHandle user,
882 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -0700883 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
884 }
885
886 @Override
887 public void sendBroadcastAsUser(Intent intent, UserHandle user,
888 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700889 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
890 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700891 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700892 ActivityManagerNative.getDefault().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -0700893 mMainThread.getApplicationThread(), intent, resolvedType, null,
894 Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false,
895 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700896 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700897 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700898 }
899 }
900
901 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700902 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700903 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700904 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -0700905 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
906 resultReceiver, scheduler, initialCode, initialData, initialExtras);
907 }
908
909 @Override
910 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
911 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
912 Handler scheduler,
913 int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700914 IIntentReceiver rd = null;
915 if (resultReceiver != null) {
916 if (mPackageInfo != null) {
917 if (scheduler == null) {
918 scheduler = mMainThread.getHandler();
919 }
920 rd = mPackageInfo.getReceiverDispatcher(
921 resultReceiver, getOuterContext(), scheduler,
922 mMainThread.getInstrumentation(), false);
923 } else {
924 if (scheduler == null) {
925 scheduler = mMainThread.getHandler();
926 }
927 rd = new LoadedApk.ReceiverDispatcher(
928 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
929 }
930 }
931 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
932 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700933 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700934 ActivityManagerNative.getDefault().broadcastIntent(
935 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700936 initialCode, initialData, initialExtras, receiverPermission,
Amith Yamasani0261b9b2014-05-20 16:57:26 -0700937 appOp, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700938 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700939 throw new RuntimeException("Failure from system", e);
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700940 }
941 }
942
943 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800944 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700946 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
948 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700949 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 ActivityManagerNative.getDefault().broadcastIntent(
951 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800952 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700953 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700955 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 }
957 }
958
959 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800960 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700961 public void sendStickyOrderedBroadcast(Intent intent,
962 BroadcastReceiver resultReceiver,
963 Handler scheduler, int initialCode, String initialData,
964 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700965 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700966 IIntentReceiver rd = null;
967 if (resultReceiver != null) {
968 if (mPackageInfo != null) {
969 if (scheduler == null) {
970 scheduler = mMainThread.getHandler();
971 }
972 rd = mPackageInfo.getReceiverDispatcher(
973 resultReceiver, getOuterContext(), scheduler,
974 mMainThread.getInstrumentation(), false);
975 } else {
976 if (scheduler == null) {
977 scheduler = mMainThread.getHandler();
978 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700979 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700980 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
981 }
982 }
983 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
984 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700985 intent.prepareToLeaveProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700986 ActivityManagerNative.getDefault().broadcastIntent(
987 mMainThread.getApplicationThread(), intent, resolvedType, rd,
988 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800989 AppOpsManager.OP_NONE, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700990 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700991 throw new RuntimeException("Failure from system", e);
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700992 }
993 }
994
995 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800996 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 public void removeStickyBroadcast(Intent intent) {
998 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
999 if (resolvedType != null) {
1000 intent = new Intent(intent);
1001 intent.setDataAndType(intent.getData(), resolvedType);
1002 }
1003 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001004 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001006 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001008 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
1010 }
1011
1012 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001013 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001014 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1015 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1016 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001017 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001018 ActivityManagerNative.getDefault().broadcastIntent(
1019 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001020 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001021 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001022 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001023 }
1024 }
1025
1026 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001027 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001028 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1029 UserHandle user, BroadcastReceiver resultReceiver,
1030 Handler scheduler, int initialCode, String initialData,
1031 Bundle initialExtras) {
1032 IIntentReceiver rd = null;
1033 if (resultReceiver != null) {
1034 if (mPackageInfo != null) {
1035 if (scheduler == null) {
1036 scheduler = mMainThread.getHandler();
1037 }
1038 rd = mPackageInfo.getReceiverDispatcher(
1039 resultReceiver, getOuterContext(), scheduler,
1040 mMainThread.getInstrumentation(), false);
1041 } else {
1042 if (scheduler == null) {
1043 scheduler = mMainThread.getHandler();
1044 }
1045 rd = new LoadedApk.ReceiverDispatcher(
1046 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1047 }
1048 }
1049 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1050 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001051 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001052 ActivityManagerNative.getDefault().broadcastIntent(
1053 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1054 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001055 AppOpsManager.OP_NONE, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001056 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001057 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001058 }
1059 }
1060
1061 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001062 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001063 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1064 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1065 if (resolvedType != null) {
1066 intent = new Intent(intent);
1067 intent.setDataAndType(intent.getData(), resolvedType);
1068 }
1069 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001070 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001071 ActivityManagerNative.getDefault().unbroadcastIntent(
1072 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1073 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001074 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001075 }
1076 }
1077
1078 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1080 return registerReceiver(receiver, filter, null, null);
1081 }
1082
1083 @Override
1084 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1085 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001086 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001087 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 }
1089
Dianne Hackborn20e80982012-08-31 19:00:44 -07001090 @Override
1091 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1092 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1093 return registerReceiverInternal(receiver, user.getIdentifier(),
1094 filter, broadcastPermission, scheduler, getOuterContext());
1095 }
1096
1097 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 IntentFilter filter, String broadcastPermission,
1099 Handler scheduler, Context context) {
1100 IIntentReceiver rd = null;
1101 if (receiver != null) {
1102 if (mPackageInfo != null && context != null) {
1103 if (scheduler == null) {
1104 scheduler = mMainThread.getHandler();
1105 }
1106 rd = mPackageInfo.getReceiverDispatcher(
1107 receiver, context, scheduler,
1108 mMainThread.getInstrumentation(), true);
1109 } else {
1110 if (scheduler == null) {
1111 scheduler = mMainThread.getHandler();
1112 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001113 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001114 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116 }
1117 try {
1118 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001119 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001120 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 } catch (RemoteException e) {
1122 return null;
1123 }
1124 }
1125
1126 @Override
1127 public void unregisterReceiver(BroadcastReceiver receiver) {
1128 if (mPackageInfo != null) {
1129 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1130 getOuterContext(), receiver);
1131 try {
1132 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1133 } catch (RemoteException e) {
1134 }
1135 } else {
1136 throw new RuntimeException("Not supported in system context");
1137 }
1138 }
1139
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001140 private void validateServiceIntent(Intent service) {
1141 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001142 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001143 IllegalArgumentException ex = new IllegalArgumentException(
1144 "Service Intent must be explicit: " + service);
1145 throw ex;
1146 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001147 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1148 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001149 }
1150 }
1151 }
1152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 @Override
1154 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001155 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001156 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001157 }
1158
1159 @Override
1160 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001161 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001162 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001163 }
1164
1165 @Override
1166 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001167 return startServiceCommon(service, user);
1168 }
1169
1170 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001172 validateServiceIntent(service);
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001173 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 ComponentName cn = ActivityManagerNative.getDefault().startService(
1175 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001176 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001177 if (cn != null) {
1178 if (cn.getPackageName().equals("!")) {
1179 throw new SecurityException(
1180 "Not allowed to start service " + service
1181 + " without permission " + cn.getClassName());
1182 } else if (cn.getPackageName().equals("!!")) {
1183 throw new SecurityException(
1184 "Unable to start service " + service
1185 + ": " + cn.getClassName());
1186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 return cn;
1189 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001190 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
1193
1194 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001195 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001196 return stopServiceCommon(service, user);
1197 }
1198
1199 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001201 validateServiceIntent(service);
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001202 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 int res = ActivityManagerNative.getDefault().stopService(
1204 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001205 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 if (res < 0) {
1207 throw new SecurityException(
1208 "Not allowed to stop service " + service);
1209 }
1210 return res != 0;
1211 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001212 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 }
1214 }
1215
1216 @Override
1217 public boolean bindService(Intent service, ServiceConnection conn,
1218 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001219 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001220 return bindServiceCommon(service, conn, flags, Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001221 }
1222
1223 /** @hide */
1224 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001225 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1226 UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001227 return bindServiceCommon(service, conn, flags, user);
1228 }
1229
1230 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,
1231 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001233 if (conn == null) {
1234 throw new IllegalArgumentException("connection is null");
1235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 if (mPackageInfo != null) {
1237 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1238 mMainThread.getHandler(), flags);
1239 } else {
1240 throw new RuntimeException("Not supported in system context");
1241 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001242 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001244 IBinder token = getActivityToken();
1245 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1246 && mPackageInfo.getApplicationInfo().targetSdkVersion
1247 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1248 flags |= BIND_WAIVE_PRIORITY;
1249 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001250 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 int res = ActivityManagerNative.getDefault().bindService(
1252 mMainThread.getApplicationThread(), getActivityToken(),
1253 service, service.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasani27b89e62013-01-16 12:30:11 -08001254 sd, flags, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 if (res < 0) {
1256 throw new SecurityException(
1257 "Not allowed to bind to service " + service);
1258 }
1259 return res != 0;
1260 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001261 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263 }
1264
1265 @Override
1266 public void unbindService(ServiceConnection conn) {
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 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1272 getOuterContext(), conn);
1273 try {
1274 ActivityManagerNative.getDefault().unbindService(sd);
1275 } catch (RemoteException e) {
1276 }
1277 } else {
1278 throw new RuntimeException("Not supported in system context");
1279 }
1280 }
1281
1282 @Override
1283 public boolean startInstrumentation(ComponentName className,
1284 String profileFile, Bundle arguments) {
1285 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001286 if (arguments != null) {
1287 arguments.setAllowFds(false);
1288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001290 className, profileFile, 0, arguments, null, null, getUserId(),
1291 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001293 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 }
1296
1297 @Override
1298 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001299 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 }
1301
Jeff Brown6e539312015-02-24 18:53:21 -08001302 @Override
1303 public String getSystemServiceName(Class<?> serviceClass) {
1304 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001305 }
1306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 @Override
1308 public int checkPermission(String permission, int pid, int uid) {
1309 if (permission == null) {
1310 throw new IllegalArgumentException("permission is null");
1311 }
1312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 try {
1314 return ActivityManagerNative.getDefault().checkPermission(
1315 permission, pid, uid);
1316 } catch (RemoteException e) {
1317 return PackageManager.PERMISSION_DENIED;
1318 }
1319 }
1320
Dianne Hackbornff170242014-11-19 10:59:01 -08001321 /** @hide */
1322 @Override
1323 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1324 if (permission == null) {
1325 throw new IllegalArgumentException("permission is null");
1326 }
1327
1328 try {
1329 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1330 permission, pid, uid, callerToken);
1331 } catch (RemoteException e) {
1332 return PackageManager.PERMISSION_DENIED;
1333 }
1334 }
1335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 @Override
1337 public int checkCallingPermission(String permission) {
1338 if (permission == null) {
1339 throw new IllegalArgumentException("permission is null");
1340 }
1341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 int pid = Binder.getCallingPid();
1343 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001344 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
1346 return PackageManager.PERMISSION_DENIED;
1347 }
1348
1349 @Override
1350 public int checkCallingOrSelfPermission(String permission) {
1351 if (permission == null) {
1352 throw new IllegalArgumentException("permission is null");
1353 }
1354
1355 return checkPermission(permission, Binder.getCallingPid(),
1356 Binder.getCallingUid());
1357 }
1358
Svetoslavc6d1c342015-02-26 14:44:43 -08001359 @Override
1360 public int checkSelfPermission(String permission) {
1361 if (permission == null) {
1362 throw new IllegalArgumentException("permission is null");
1363 }
1364
1365 return checkPermission(permission, Process.myPid(), Process.myUid());
1366 }
1367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 private void enforce(
1369 String permission, int resultOfCheck,
1370 boolean selfToo, int uid, String message) {
1371 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1372 throw new SecurityException(
1373 (message != null ? (message + ": ") : "") +
1374 (selfToo
1375 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001376 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 permission +
1378 ".");
1379 }
1380 }
1381
Jeff Brown6e539312015-02-24 18:53:21 -08001382 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 public void enforcePermission(
1384 String permission, int pid, int uid, String message) {
1385 enforce(permission,
1386 checkPermission(permission, pid, uid),
1387 false,
1388 uid,
1389 message);
1390 }
1391
Jeff Brown6e539312015-02-24 18:53:21 -08001392 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 public void enforceCallingPermission(String permission, String message) {
1394 enforce(permission,
1395 checkCallingPermission(permission),
1396 false,
1397 Binder.getCallingUid(),
1398 message);
1399 }
1400
Jeff Brown6e539312015-02-24 18:53:21 -08001401 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 public void enforceCallingOrSelfPermission(
1403 String permission, String message) {
1404 enforce(permission,
1405 checkCallingOrSelfPermission(permission),
1406 true,
1407 Binder.getCallingUid(),
1408 message);
1409 }
1410
1411 @Override
1412 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1413 try {
1414 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001415 mMainThread.getApplicationThread(), toPackage,
1416 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 } catch (RemoteException e) {
1418 }
1419 }
1420
1421 @Override
1422 public void revokeUriPermission(Uri uri, int modeFlags) {
1423 try {
1424 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001425 mMainThread.getApplicationThread(),
1426 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 } catch (RemoteException e) {
1428 }
1429 }
1430
1431 @Override
1432 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 try {
1434 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001435 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001436 resolveUserId(uri), null);
1437 } catch (RemoteException e) {
1438 return PackageManager.PERMISSION_DENIED;
1439 }
1440 }
1441
1442 /** @hide */
1443 @Override
1444 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1445 try {
1446 return ActivityManagerNative.getDefault().checkUriPermission(
1447 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1448 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 } catch (RemoteException e) {
1450 return PackageManager.PERMISSION_DENIED;
1451 }
1452 }
1453
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001454 private int resolveUserId(Uri uri) {
1455 return ContentProvider.getUserIdFromUri(uri, getUserId());
1456 }
1457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 @Override
1459 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 int pid = Binder.getCallingPid();
1461 if (pid != Process.myPid()) {
1462 return checkUriPermission(uri, pid,
1463 Binder.getCallingUid(), modeFlags);
1464 }
1465 return PackageManager.PERMISSION_DENIED;
1466 }
1467
1468 @Override
1469 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1470 return checkUriPermission(uri, Binder.getCallingPid(),
1471 Binder.getCallingUid(), modeFlags);
1472 }
1473
1474 @Override
1475 public int checkUriPermission(Uri uri, String readPermission,
1476 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001477 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1479 + readPermission + " writePermission=" + writePermission
1480 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1481 }
1482 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1483 if (readPermission == null
1484 || checkPermission(readPermission, pid, uid)
1485 == PackageManager.PERMISSION_GRANTED) {
1486 return PackageManager.PERMISSION_GRANTED;
1487 }
1488 }
1489 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1490 if (writePermission == null
1491 || checkPermission(writePermission, pid, uid)
1492 == PackageManager.PERMISSION_GRANTED) {
1493 return PackageManager.PERMISSION_GRANTED;
1494 }
1495 }
1496 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1497 : PackageManager.PERMISSION_DENIED;
1498 }
1499
1500 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001501 StringBuilder builder = new StringBuilder();
1502 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1503 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001505 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1506 builder.append("write and ");
1507 }
1508 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1509 builder.append("persistable and ");
1510 }
1511 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1512 builder.append("prefix and ");
1513 }
1514
1515 if (builder.length() > 5) {
1516 builder.setLength(builder.length() - 5);
1517 return builder.toString();
1518 } else {
1519 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 }
1522
1523 private void enforceForUri(
1524 int modeFlags, int resultOfCheck, boolean selfToo,
1525 int uid, Uri uri, String message) {
1526 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1527 throw new SecurityException(
1528 (message != null ? (message + ": ") : "") +
1529 (selfToo
1530 ? "Neither user " + uid + " nor current process has "
1531 : "User " + uid + " does not have ") +
1532 uriModeFlagToString(modeFlags) +
1533 " permission on " +
1534 uri +
1535 ".");
1536 }
1537 }
1538
Jeff Brown6e539312015-02-24 18:53:21 -08001539 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 public void enforceUriPermission(
1541 Uri uri, int pid, int uid, int modeFlags, String message) {
1542 enforceForUri(
1543 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1544 false, uid, uri, message);
1545 }
1546
Jeff Brown6e539312015-02-24 18:53:21 -08001547 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 public void enforceCallingUriPermission(
1549 Uri uri, int modeFlags, String message) {
1550 enforceForUri(
1551 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001552 false,
1553 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 }
1555
Jeff Brown6e539312015-02-24 18:53:21 -08001556 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 public void enforceCallingOrSelfUriPermission(
1558 Uri uri, int modeFlags, String message) {
1559 enforceForUri(
1560 modeFlags,
1561 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1562 Binder.getCallingUid(), uri, message);
1563 }
1564
Jeff Brown6e539312015-02-24 18:53:21 -08001565 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 public void enforceUriPermission(
1567 Uri uri, String readPermission, String writePermission,
1568 int pid, int uid, int modeFlags, String message) {
1569 enforceForUri(modeFlags,
1570 checkUriPermission(
1571 uri, readPermission, writePermission, pid, uid,
1572 modeFlags),
1573 false,
1574 uid,
1575 uri,
1576 message);
1577 }
1578
Tom O'Neill365632e2013-09-09 09:34:58 -07001579 /**
1580 * Logs a warning if the system process directly called a method such as
1581 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1582 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1583 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001584 private void warnIfCallingFromSystemProcess() {
1585 if (Process.myUid() == Process.SYSTEM_UID) {
1586 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001587 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001588 }
1589 }
1590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001592 public Context createApplicationContext(ApplicationInfo application, int flags)
1593 throws NameNotFoundException {
1594 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1595 flags | CONTEXT_REGISTER_PACKAGE);
1596 if (pi != null) {
1597 final boolean restricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
1598 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
1599 new UserHandle(UserHandle.getUserId(application.uid)), restricted,
Wale Ogunwale26698512015-06-05 16:55:33 -07001600 mDisplay, null, Display.INVALID_DISPLAY);
Svetoslav976e8bd2014-07-16 15:12:03 -07001601 if (c.mResources != null) {
1602 return c;
1603 }
1604 }
1605
1606 throw new PackageManager.NameNotFoundException(
1607 "Application package " + application.packageName + " not found");
1608 }
1609
1610 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001612 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001613 return createPackageContextAsUser(packageName, flags,
1614 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001615 }
1616
1617 @Override
1618 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1619 throws NameNotFoundException {
Jeff Browndefd4a62014-03-10 21:24:37 -07001620 final boolean restricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Kenny Guyc2a40602014-09-08 18:51:15 +00001621 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001622 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Wale Ogunwale26698512015-06-05 16:55:33 -07001623 user, restricted, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625
Jeff Browndefd4a62014-03-10 21:24:37 -07001626 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001627 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 if (pi != null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001629 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Wale Ogunwale26698512015-06-05 16:55:33 -07001630 user, restricted, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 if (c.mResources != null) {
1632 return c;
1633 }
1634 }
1635
1636 // Should be a better exception.
1637 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001638 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640
Romain Guy870e09f2009-07-06 16:35:25 -07001641 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001642 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001643 if (overrideConfiguration == null) {
1644 throw new IllegalArgumentException("overrideConfiguration must not be null");
1645 }
1646
Jeff Browndefd4a62014-03-10 21:24:37 -07001647 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Wale Ogunwale26698512015-06-05 16:55:33 -07001648 mUser, mRestricted, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001649 }
1650
1651 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001652 public Context createDisplayContext(Display display) {
1653 if (display == null) {
1654 throw new IllegalArgumentException("display must not be null");
1655 }
1656
Jeff Browndefd4a62014-03-10 21:24:37 -07001657 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Wale Ogunwale26698512015-06-05 16:55:33 -07001658 mUser, mRestricted, display, null, Display.INVALID_DISPLAY);
Jeff Browna492c3a2012-08-23 19:48:44 -07001659 }
1660
Jeff Brown6e539312015-02-24 18:53:21 -08001661 Display getDisplay() {
1662 if (mDisplay != null) {
1663 return mDisplay;
1664 }
Wale Ogunwale26698512015-06-05 16:55:33 -07001665 return ResourcesManager.getInstance().getAdjustedDisplay(
1666 Display.DEFAULT_DISPLAY, mDisplayAdjustments);
Jeff Brown6e539312015-02-24 18:53:21 -08001667 }
1668
Jeff Browna492c3a2012-08-23 19:48:44 -07001669 private int getDisplayId() {
1670 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1671 }
1672
1673 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001674 public boolean isRestricted() {
1675 return mRestricted;
1676 }
1677
Jeff Brown98365d72012-08-19 20:30:52 -07001678 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001679 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1680 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -07001681 }
1682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 private File getDataDirFile() {
1684 if (mPackageInfo != null) {
1685 return mPackageInfo.getDataDirFile();
1686 }
1687 throw new RuntimeException("Not supported in system context");
1688 }
1689
1690 @Override
1691 public File getDir(String name, int mode) {
1692 name = "app_" + name;
1693 File file = makeFilename(getDataDirFile(), name);
1694 if (!file.exists()) {
1695 file.mkdir();
1696 setFilePermissionsFromMode(file.getPath(), mode,
1697 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1698 }
1699 return file;
1700 }
1701
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001702 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08001703 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001704 public int getUserId() {
1705 return mUser.getIdentifier();
1706 }
1707
Dianne Hackborn21556372010-02-04 16:34:40 -08001708 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001709 LoadedApk packageInfo = new LoadedApk(mainThread);
1710 ContextImpl context = new ContextImpl(null, mainThread,
Wale Ogunwale26698512015-06-05 16:55:33 -07001711 packageInfo, null, null, false, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001712 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Wale Ogunwale7c726682015-02-06 17:34:28 -08001713 context.mResourcesManager.getDisplayMetricsLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 return context;
1715 }
1716
Jeff Browndefd4a62014-03-10 21:24:37 -07001717 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
1718 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
1719 return new ContextImpl(null, mainThread,
Wale Ogunwale26698512015-06-05 16:55:33 -07001720 packageInfo, null, null, false, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001721 }
1722
1723 static ContextImpl createActivityContext(ActivityThread mainThread,
Wale Ogunwale7c726682015-02-06 17:34:28 -08001724 LoadedApk packageInfo, int displayId, Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001725 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Wale Ogunwale26698512015-06-05 16:55:33 -07001726 return new ContextImpl(null, mainThread, packageInfo, null, null, false,
1727 null, overrideConfiguration, displayId);
Jeff Browndefd4a62014-03-10 21:24:37 -07001728 }
1729
1730 private ContextImpl(ContextImpl container, ActivityThread mainThread,
1731 LoadedApk packageInfo, IBinder activityToken, UserHandle user, boolean restricted,
Wale Ogunwale26698512015-06-05 16:55:33 -07001732 Display display, Configuration overrideConfiguration, int createDisplayWithId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734
Jeff Browndefd4a62014-03-10 21:24:37 -07001735 mMainThread = mainThread;
1736 mActivityToken = activityToken;
1737 mRestricted = restricted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738
Jeff Browndefd4a62014-03-10 21:24:37 -07001739 if (user == null) {
1740 user = Process.myUserHandle();
1741 }
1742 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07001745 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07001746
Wale Ogunwale26698512015-06-05 16:55:33 -07001747 final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
Wale Ogunwalecac3dc62015-06-09 15:35:02 -07001748 ? createDisplayWithId
1749 : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
Wale Ogunwale26698512015-06-05 16:55:33 -07001750
Jeff Browndefd4a62014-03-10 21:24:37 -07001751 CompatibilityInfo compatInfo = null;
1752 if (container != null) {
1753 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
1754 }
Wale Ogunwale9cf99542015-02-18 16:31:34 -08001755 if (compatInfo == null) {
1756 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
1757 ? packageInfo.getCompatibilityInfo()
1758 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Jeff Browndefd4a62014-03-10 21:24:37 -07001759 }
1760 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
Wale Ogunwale7c726682015-02-06 17:34:28 -08001761 mDisplayAdjustments.setConfiguration(overrideConfiguration);
Jeff Browndefd4a62014-03-10 21:24:37 -07001762
Wale Ogunwale26698512015-06-05 16:55:33 -07001763 mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
1764 : ResourcesManager.getInstance().getAdjustedDisplay(displayId, mDisplayAdjustments);
1765
Jeff Browndefd4a62014-03-10 21:24:37 -07001766 Resources resources = packageInfo.getResources(mainThread);
1767 if (resources != null) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08001768 if (displayId != Display.DEFAULT_DISPLAY
Jeff Browndefd4a62014-03-10 21:24:37 -07001769 || overrideConfiguration != null
1770 || (compatInfo != null && compatInfo.applicationScale
1771 != resources.getCompatibilityInfo().applicationScale)) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001772 resources = mResourcesManager.getTopLevelResources(packageInfo.getResDir(),
1773 packageInfo.getSplitResDirs(), packageInfo.getOverlayDirs(),
1774 packageInfo.getApplicationInfo().sharedLibraryFiles, displayId,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001775 overrideConfiguration, compatInfo);
Jeff Browndefd4a62014-03-10 21:24:37 -07001776 }
1777 }
1778 mResources = resources;
1779
1780 if (container != null) {
1781 mBasePackageName = container.mBasePackageName;
1782 mOpPackageName = container.mOpPackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07001783 } else {
1784 mBasePackageName = packageInfo.mPackageName;
1785 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
1786 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
1787 // Special case: system components allow themselves to be loaded in to other
1788 // processes. For purposes of app ops, we must then consider the context as
1789 // belonging to the package of this process, not the system itself, otherwise
1790 // the package+uid verifications in app ops will fail.
1791 mOpPackageName = ActivityThread.currentPackageName();
1792 } else {
1793 mOpPackageName = mBasePackageName;
1794 }
1795 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05001796
1797 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
1799
Narayan Kamath29564cd2014-08-07 10:57:40 +01001800 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
1801 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803
1804 final void scheduleFinalCleanup(String who, String what) {
1805 mMainThread.scheduleContextCleanup(this, who, what);
1806 }
1807
1808 final void performFinalCleanup(String who, String what) {
1809 //Log.i(TAG, "Cleanup up context: " + this);
1810 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1811 }
1812
1813 final Context getReceiverRestrictedContext() {
1814 if (mReceiverRestrictedContext != null) {
1815 return mReceiverRestrictedContext;
1816 }
1817 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1818 }
1819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 final void setOuterContext(Context context) {
1821 mOuterContext = context;
1822 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 final Context getOuterContext() {
1825 return mOuterContext;
1826 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 final IBinder getActivityToken() {
1829 return mActivityToken;
1830 }
1831
Jeff Brown6e539312015-02-24 18:53:21 -08001832 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08001833 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 int extraPermissions) {
1835 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
1836 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
1837 |extraPermissions;
1838 if ((mode&MODE_WORLD_READABLE) != 0) {
1839 perms |= FileUtils.S_IROTH;
1840 }
1841 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
1842 perms |= FileUtils.S_IWOTH;
1843 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001844 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
1846 + ", perms=0x" + Integer.toHexString(perms));
1847 }
1848 FileUtils.setPermissions(name, perms, -1, -1);
1849 }
1850
Oscar Montemayora8529f62009-11-18 10:14:20 -08001851 private File validateFilePath(String name, boolean createDirectory) {
1852 File dir;
1853 File f;
1854
1855 if (name.charAt(0) == File.separatorChar) {
1856 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
1857 dir = new File(dirPath);
1858 name = name.substring(name.lastIndexOf(File.separatorChar));
1859 f = new File(dir, name);
1860 } else {
1861 dir = getDatabasesDir();
1862 f = makeFilename(dir, name);
1863 }
1864
1865 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
1866 FileUtils.setPermissions(dir.getPath(),
1867 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1868 -1, -1);
1869 }
1870
1871 return f;
1872 }
1873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 private File makeFilename(File base, String name) {
1875 if (name.indexOf(File.separatorChar) < 0) {
1876 return new File(base, name);
1877 }
1878 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08001879 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 }
1881
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001882 /**
1883 * Ensure that given directories exist, trying to create them if missing. If
1884 * unable to create, they are filtered by replacing with {@code null}.
1885 */
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07001886 private File[] ensureDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001887 File[] result = new File[dirs.length];
1888 for (int i = 0; i < dirs.length; i++) {
1889 File dir = dirs[i];
1890 if (!dir.exists()) {
1891 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07001892 // recheck existence in case of cross-process race
1893 if (!dir.exists()) {
1894 // Failing to mkdir() may be okay, since we might not have
1895 // enough permissions; ask vold to create on our behalf.
1896 final IMountService mount = IMountService.Stub.asInterface(
1897 ServiceManager.getService("mount"));
1898 int res = -1;
1899 try {
1900 res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
Jeff Sharkey97086692015-01-08 14:00:13 -08001901 } catch (Exception ignored) {
Christopher Tatecc866da2013-10-02 18:11:01 -07001902 }
1903 if (res != 0) {
1904 Log.w(TAG, "Failed to ensure directory: " + dir);
1905 dir = null;
1906 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07001907 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001908 }
1909 }
1910 result[i] = dir;
1911 }
1912 return result;
1913 }
1914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 // ----------------------------------------------------------------------
1916 // ----------------------------------------------------------------------
1917 // ----------------------------------------------------------------------
1918
1919 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001920 private final ActivityThread mMainThread;
1921 private final UserHandle mUser;
1922
1923 public ApplicationContentResolver(
1924 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07001926 mMainThread = Preconditions.checkNotNull(mainThread);
1927 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
1929
1930 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001931 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001932 return mMainThread.acquireProvider(context,
1933 ContentProvider.getAuthorityWithoutUserId(auth),
1934 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 }
1936
1937 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001938 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001939 return mMainThread.acquireExistingProvider(context,
1940 ContentProvider.getAuthorityWithoutUserId(auth),
1941 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001942 }
1943
1944 @Override
1945 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001946 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001948
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001949 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001950 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001951 return mMainThread.acquireProvider(c,
1952 ContentProvider.getAuthorityWithoutUserId(auth),
1953 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001954 }
1955
1956 @Override
1957 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001958 return mMainThread.releaseProvider(icp, false);
1959 }
1960
1961 @Override
1962 public void unstableProviderDied(IContentProvider icp) {
1963 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001964 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001965
1966 @Override
1967 public void appNotRespondingViaProvider(IContentProvider icp) {
1968 mMainThread.appNotRespondingViaProvider(icp.asBinder());
1969 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001970
1971 /** @hide */
1972 protected int resolveUserIdFromAuthority(String auth) {
1973 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
1974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976}