blob: 4ccd69fa50883e4d06147da5d4dfa6a8a42622fa [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
240 public void setTheme(int resid) {
241 mThemeResource = resid;
242 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800245 public int getThemeResId() {
246 return mThemeResource;
247 }
248
249 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 public Resources.Theme getTheme() {
251 if (mTheme == null) {
Alan Viverette5effd7e2014-05-05 12:25:33 -0700252 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
Dianne Hackbornd922ae02011-01-14 11:43:24 -0800253 getOuterContext().getApplicationInfo().targetSdkVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 mTheme = mResources.newTheme();
255 mTheme.applyStyle(mThemeResource, true);
256 }
257 return mTheme;
258 }
259
260 @Override
261 public ClassLoader getClassLoader() {
262 return mPackageInfo != null ?
263 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
264 }
265
266 @Override
267 public String getPackageName() {
268 if (mPackageInfo != null) {
269 return mPackageInfo.getPackageName();
270 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800271 // No mPackageInfo means this is a Context for the system itself,
272 // and this here is its name.
273 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800276 /** @hide */
277 @Override
278 public String getBasePackageName() {
279 return mBasePackageName != null ? mBasePackageName : getPackageName();
280 }
281
Dianne Hackborn95d78532013-09-11 09:51:14 -0700282 /** @hide */
283 @Override
284 public String getOpPackageName() {
285 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
286 }
287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700289 public ApplicationInfo getApplicationInfo() {
290 if (mPackageInfo != null) {
291 return mPackageInfo.getApplicationInfo();
292 }
293 throw new RuntimeException("Not supported in system context");
294 }
295
296 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 public String getPackageResourcePath() {
298 if (mPackageInfo != null) {
299 return mPackageInfo.getResDir();
300 }
301 throw new RuntimeException("Not supported in system context");
302 }
303
304 @Override
305 public String getPackageCodePath() {
306 if (mPackageInfo != null) {
307 return mPackageInfo.getAppDir();
308 }
309 throw new RuntimeException("Not supported in system context");
310 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200311
Jeff Brown6e539312015-02-24 18:53:21 -0800312 @Override
Joe Onorato23ecae32009-06-10 17:07:15 -0700313 public File getSharedPrefsFile(String name) {
314 return makeFilename(getPreferencesDir(), name + ".xml");
315 }
316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 @Override
318 public SharedPreferences getSharedPreferences(String name, int mode) {
319 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700320 synchronized (ContextImpl.class) {
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700321 if (sSharedPrefs == null) {
322 sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>();
323 }
324
325 final String packageName = getPackageName();
326 ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
327 if (packagePrefs == null) {
328 packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
329 sSharedPrefs.put(packageName, packagePrefs);
330 }
331
Dianne Hackbornff32f352013-07-18 17:31:13 -0700332 // At least one application in the world actually passes in a null
333 // name. This happened to work because when we generated the file name
334 // we would stringify it to "null.xml". Nice.
335 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
Chet Haasee8222dd2013-09-05 07:44:18 -0700336 Build.VERSION_CODES.KITKAT) {
Dianne Hackbornff32f352013-07-18 17:31:13 -0700337 if (name == null) {
338 name = "null";
339 }
340 }
341
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700342 sp = packagePrefs.get(name);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700343 if (sp == null) {
Brad Fitzpatrick4cd50b82010-12-01 17:31:45 -0800344 File prefsFile = getSharedPrefsFile(name);
345 sp = new SharedPreferencesImpl(prefsFile, mode);
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700346 packagePrefs.put(name, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700347 return sp;
348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800350 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
351 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
352 // If somebody else (some other process) changed the prefs
353 // file behind our back, we reload it. This has been the
354 // historical (if undocumented) behavior.
355 sp.startReloadIfChangedUnexpectedly();
356 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700357 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
359
360 private File getPreferencesDir() {
361 synchronized (mSync) {
362 if (mPreferencesDir == null) {
363 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
364 }
365 return mPreferencesDir;
366 }
367 }
368
369 @Override
370 public FileInputStream openFileInput(String name)
371 throws FileNotFoundException {
372 File f = makeFilename(getFilesDir(), name);
373 return new FileInputStream(f);
374 }
375
376 @Override
377 public FileOutputStream openFileOutput(String name, int mode)
378 throws FileNotFoundException {
379 final boolean append = (mode&MODE_APPEND) != 0;
380 File f = makeFilename(getFilesDir(), name);
381 try {
382 FileOutputStream fos = new FileOutputStream(f, append);
383 setFilePermissionsFromMode(f.getPath(), mode, 0);
384 return fos;
385 } catch (FileNotFoundException e) {
386 }
387
388 File parent = f.getParentFile();
389 parent.mkdir();
390 FileUtils.setPermissions(
391 parent.getPath(),
392 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
393 -1, -1);
394 FileOutputStream fos = new FileOutputStream(f, append);
395 setFilePermissionsFromMode(f.getPath(), mode, 0);
396 return fos;
397 }
398
399 @Override
400 public boolean deleteFile(String name) {
401 File f = makeFilename(getFilesDir(), name);
402 return f.delete();
403 }
404
Christopher Tatea7835b62014-07-11 17:25:57 -0700405 // Common-path handling of app data dir creation
406 private static File createFilesDirLocked(File file) {
407 if (!file.exists()) {
408 if (!file.mkdirs()) {
409 if (file.exists()) {
410 // spurious failure; probably racing with another process for this app
411 return file;
412 }
413 Log.w(TAG, "Unable to create files subdir " + file.getPath());
414 return null;
415 }
416 FileUtils.setPermissions(
417 file.getPath(),
418 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
419 -1, -1);
420 }
421 return file;
422 }
423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 @Override
425 public File getFilesDir() {
426 synchronized (mSync) {
427 if (mFilesDir == null) {
428 mFilesDir = new File(getDataDirFile(), "files");
429 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700430 return createFilesDirLocked(mFilesDir);
431 }
432 }
433
434 @Override
435 public File getNoBackupFilesDir() {
436 synchronized (mSync) {
437 if (mNoBackupFilesDir == null) {
438 mNoBackupFilesDir = new File(getDataDirFile(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700440 return createFilesDirLocked(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800445 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700446 // Operates on primary external storage
447 return getExternalFilesDirs(type)[0];
448 }
449
450 @Override
451 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800452 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700453 if (mExternalFilesDirs == null) {
454 mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800455 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700456
457 // Splice in requested type, if any
458 File[] dirs = mExternalFilesDirs;
459 if (type != null) {
460 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800461 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700462
463 // Create dirs if needed
464 return ensureDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800465 }
466 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200467
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800468 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800469 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700470 // Operates on primary external storage
471 return getObbDirs()[0];
472 }
473
474 @Override
475 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800476 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700477 if (mExternalObbDirs == null) {
478 mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800479 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700480
481 // Create dirs if needed
482 return ensureDirsExistOrFilter(mExternalObbDirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800483 }
484 }
485
486 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 public File getCacheDir() {
488 synchronized (mSync) {
489 if (mCacheDir == null) {
490 mCacheDir = new File(getDataDirFile(), "cache");
491 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700492 return createFilesDirLocked(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200495
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800496 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700497 public File getCodeCacheDir() {
498 synchronized (mSync) {
499 if (mCodeCacheDir == null) {
500 mCodeCacheDir = new File(getDataDirFile(), "code_cache");
501 }
502 return createFilesDirLocked(mCodeCacheDir);
503 }
504 }
505
506 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800507 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700508 // Operates on primary external storage
509 return getExternalCacheDirs()[0];
510 }
511
512 @Override
513 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800514 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700515 if (mExternalCacheDirs == null) {
516 mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800517 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700518
519 // Create dirs if needed
520 return ensureDirsExistOrFilter(mExternalCacheDirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800521 }
522 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700525 public File[] getExternalMediaDirs() {
526 synchronized (mSync) {
527 if (mExternalMediaDirs == null) {
528 mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
529 }
530
531 // Create dirs if needed
532 return ensureDirsExistOrFilter(mExternalMediaDirs);
533 }
534 }
535
536 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 public File getFileStreamPath(String name) {
538 return makeFilename(getFilesDir(), name);
539 }
540
541 @Override
542 public String[] fileList() {
543 final String[] list = getFilesDir().list();
544 return (list != null) ? list : EMPTY_FILE_LIST;
545 }
546
547 @Override
548 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700549 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
551
552 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700553 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
554 DatabaseErrorHandler errorHandler) {
555 File f = validateFilePath(name, true);
Jeff Brown47847f32012-03-22 19:13:11 -0700556 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
557 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
558 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
559 }
560 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700561 setFilePermissionsFromMode(f.getPath(), mode, 0);
562 return db;
563 }
564
565 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 public boolean deleteDatabase(String name) {
567 try {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800568 File f = validateFilePath(name, false);
Jeff Brown79087e42012-03-01 19:52:44 -0800569 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 } catch (Exception e) {
571 }
572 return false;
573 }
574
575 @Override
576 public File getDatabasePath(String name) {
Oscar Montemayora8529f62009-11-18 10:14:20 -0800577 return validateFilePath(name, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579
580 @Override
581 public String[] databaseList() {
582 final String[] list = getDatabasesDir().list();
583 return (list != null) ? list : EMPTY_FILE_LIST;
584 }
585
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 private File getDatabasesDir() {
588 synchronized (mSync) {
589 if (mDatabasesDir == null) {
590 mDatabasesDir = new File(getDataDirFile(), "databases");
591 }
592 if (mDatabasesDir.getPath().equals("databases")) {
593 mDatabasesDir = new File("/data/system");
594 }
595 return mDatabasesDir;
596 }
597 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800600 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700602 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 }
604
605 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800606 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700607 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700608 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 }
610
611 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800612 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700614 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
616
617 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800618 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700620 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622
623 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800624 @Deprecated
625 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700626 getWallpaperManager().setBitmap(bitmap);
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 void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700632 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634
635 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800636 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700638 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 }
640
Jeff Brown6e539312015-02-24 18:53:21 -0800641 private WallpaperManager getWallpaperManager() {
642 return getSystemService(WallpaperManager.class);
643 }
644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 @Override
646 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700647 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700648 startActivity(intent, null);
649 }
650
Amith Yamasani82644082012-08-03 13:09:11 -0700651 /** @hide */
652 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700653 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700654 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700655 }
656
Dianne Hackborna4972e92012-03-14 10:38:05 -0700657 @Override
658 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700659 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
661 throw new AndroidRuntimeException(
662 "Calling startActivity() from outside of an Activity "
663 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
664 + " Is this really what you want?");
665 }
666 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborn6e8304e2010-05-14 00:42:53 -0700667 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700668 (Activity)null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670
Amith Yamasani258848d2012-08-10 17:06:33 -0700671 /** @hide */
672 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700673 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700674 try {
675 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800676 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700677 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700678 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700679 user.getIdentifier());
Amith Yamasani258848d2012-08-10 17:06:33 -0700680 } catch (RemoteException re) {
681 }
682 }
683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800685 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700686 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700687 startActivities(intents, null);
688 }
689
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700690 /** @hide */
691 @Override
692 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
693 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
694 throw new AndroidRuntimeException(
695 "Calling startActivities() from outside of an Activity "
696 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
697 + " Is this really what you want?");
698 }
699 mMainThread.getInstrumentation().execStartActivitiesAsUser(
700 getOuterContext(), mMainThread.getApplicationThread(), null,
701 (Activity)null, intents, options, userHandle.getIdentifier());
702 }
703
Dianne Hackborna4972e92012-03-14 10:38:05 -0700704 @Override
705 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700706 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800707 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
708 throw new AndroidRuntimeException(
709 "Calling startActivities() from outside of an Activity "
710 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
711 + " Is this really what you want?");
712 }
713 mMainThread.getInstrumentation().execStartActivities(
714 getOuterContext(), mMainThread.getApplicationThread(), null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700715 (Activity)null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800716 }
717
718 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700719 public void startIntentSender(IntentSender intent,
720 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
721 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700722 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
723 }
724
725 @Override
726 public void startIntentSender(IntentSender intent, Intent fillInIntent,
727 int flagsMask, int flagsValues, int extraFlags, Bundle options)
728 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700729 try {
730 String resolvedType = null;
731 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700732 fillInIntent.migrateExtraStreamToClipData();
733 fillInIntent.prepareToLeaveProcess();
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700734 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
735 }
736 int result = ActivityManagerNative.getDefault()
737 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
738 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700739 0, flagsMask, flagsValues, options);
740 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700741 throw new IntentSender.SendIntentException();
742 }
743 Instrumentation.checkStartActivityResult(result, null);
744 } catch (RemoteException e) {
745 }
746 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200747
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700748 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700750 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
752 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700753 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 ActivityManagerNative.getDefault().broadcastIntent(
755 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800756 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700757 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 } catch (RemoteException e) {
759 }
760 }
761
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 public void sendBroadcast(Intent intent, String receiverPermission) {
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, receiverPermission, AppOpsManager.OP_NONE,
771 false, false, getUserId());
772 } catch (RemoteException e) {
773 }
774 }
775
776 @Override
777 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
778 warnIfCallingFromSystemProcess();
779 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
780 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700781 intent.prepareToLeaveProcess();
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800782 ActivityManagerNative.getDefault().broadcastIntent(
783 mMainThread.getApplicationThread(), intent, resolvedType, null,
784 Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700785 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 } catch (RemoteException e) {
787 }
788 }
789
790 @Override
791 public void sendOrderedBroadcast(Intent intent,
792 String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700793 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
795 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700796 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 ActivityManagerNative.getDefault().broadcastIntent(
798 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800799 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, true, false,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700800 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 } catch (RemoteException e) {
802 }
803 }
804
805 @Override
806 public void sendOrderedBroadcast(Intent intent,
807 String receiverPermission, BroadcastReceiver resultReceiver,
808 Handler scheduler, int initialCode, String initialData,
809 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800810 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
811 resultReceiver, scheduler, initialCode, initialData, initialExtras);
812 }
813
814 @Override
815 public void sendOrderedBroadcast(Intent intent,
816 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
817 Handler scheduler, int initialCode, String initialData,
818 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700819 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 IIntentReceiver rd = null;
821 if (resultReceiver != null) {
822 if (mPackageInfo != null) {
823 if (scheduler == null) {
824 scheduler = mMainThread.getHandler();
825 }
826 rd = mPackageInfo.getReceiverDispatcher(
827 resultReceiver, getOuterContext(), scheduler,
828 mMainThread.getInstrumentation(), false);
829 } else {
830 if (scheduler == null) {
831 scheduler = mMainThread.getHandler();
832 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700833 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
835 }
836 }
837 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
838 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700839 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 ActivityManagerNative.getDefault().broadcastIntent(
841 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800842 initialCode, initialData, initialExtras, receiverPermission, appOp,
843 true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 } catch (RemoteException e) {
845 }
846 }
847
848 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700849 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700850 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
851 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700852 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700853 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800854 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
855 AppOpsManager.OP_NONE, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700856 } catch (RemoteException e) {
857 }
858 }
859
860 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700861 public void sendBroadcastAsUser(Intent intent, UserHandle user,
862 String receiverPermission) {
863 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
864 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700865 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700866 ActivityManagerNative.getDefault().broadcastIntent(
867 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800868 Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, false, false,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700869 user.getIdentifier());
870 } catch (RemoteException e) {
871 }
872 }
873
874 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700875 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700876 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700877 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -0700878 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
879 resultReceiver, scheduler, initialCode, initialData, initialExtras);
880 }
881
882 @Override
883 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
884 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
885 Handler scheduler,
886 int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700887 IIntentReceiver rd = null;
888 if (resultReceiver != null) {
889 if (mPackageInfo != null) {
890 if (scheduler == null) {
891 scheduler = mMainThread.getHandler();
892 }
893 rd = mPackageInfo.getReceiverDispatcher(
894 resultReceiver, getOuterContext(), scheduler,
895 mMainThread.getInstrumentation(), false);
896 } else {
897 if (scheduler == null) {
898 scheduler = mMainThread.getHandler();
899 }
900 rd = new LoadedApk.ReceiverDispatcher(
901 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
902 }
903 }
904 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
905 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700906 intent.prepareToLeaveProcess();
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700907 ActivityManagerNative.getDefault().broadcastIntent(
908 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700909 initialCode, initialData, initialExtras, receiverPermission,
Amith Yamasani0261b9b2014-05-20 16:57:26 -0700910 appOp, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -0700911 } catch (RemoteException e) {
912 }
913 }
914
915 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800916 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700918 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
920 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700921 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 ActivityManagerNative.getDefault().broadcastIntent(
923 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800924 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700925 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 } catch (RemoteException e) {
927 }
928 }
929
930 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800931 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700932 public void sendStickyOrderedBroadcast(Intent intent,
933 BroadcastReceiver resultReceiver,
934 Handler scheduler, int initialCode, String initialData,
935 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700936 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700937 IIntentReceiver rd = null;
938 if (resultReceiver != null) {
939 if (mPackageInfo != null) {
940 if (scheduler == null) {
941 scheduler = mMainThread.getHandler();
942 }
943 rd = mPackageInfo.getReceiverDispatcher(
944 resultReceiver, getOuterContext(), scheduler,
945 mMainThread.getInstrumentation(), false);
946 } else {
947 if (scheduler == null) {
948 scheduler = mMainThread.getHandler();
949 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700950 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700951 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
952 }
953 }
954 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
955 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700956 intent.prepareToLeaveProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700957 ActivityManagerNative.getDefault().broadcastIntent(
958 mMainThread.getApplicationThread(), intent, resolvedType, rd,
959 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800960 AppOpsManager.OP_NONE, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -0700961 } catch (RemoteException e) {
962 }
963 }
964
965 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800966 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 public void removeStickyBroadcast(Intent intent) {
968 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
969 if (resolvedType != null) {
970 intent = new Intent(intent);
971 intent.setDataAndType(intent.getData(), resolvedType);
972 }
973 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700974 intent.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -0700976 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 } catch (RemoteException e) {
978 }
979 }
980
981 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800982 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700983 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
984 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
985 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700986 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700987 ActivityManagerNative.getDefault().broadcastIntent(
988 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800989 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700990 } catch (RemoteException e) {
991 }
992 }
993
994 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800995 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -0700996 public void sendStickyOrderedBroadcastAsUser(Intent intent,
997 UserHandle user, BroadcastReceiver resultReceiver,
998 Handler scheduler, int initialCode, String initialData,
999 Bundle initialExtras) {
1000 IIntentReceiver rd = null;
1001 if (resultReceiver != null) {
1002 if (mPackageInfo != null) {
1003 if (scheduler == null) {
1004 scheduler = mMainThread.getHandler();
1005 }
1006 rd = mPackageInfo.getReceiverDispatcher(
1007 resultReceiver, getOuterContext(), scheduler,
1008 mMainThread.getInstrumentation(), false);
1009 } else {
1010 if (scheduler == null) {
1011 scheduler = mMainThread.getHandler();
1012 }
1013 rd = new LoadedApk.ReceiverDispatcher(
1014 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1015 }
1016 }
1017 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1018 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001019 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001020 ActivityManagerNative.getDefault().broadcastIntent(
1021 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1022 initialCode, initialData, initialExtras, null,
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001023 AppOpsManager.OP_NONE, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001024 } catch (RemoteException e) {
1025 }
1026 }
1027
1028 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001029 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001030 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1031 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1032 if (resolvedType != null) {
1033 intent = new Intent(intent);
1034 intent.setDataAndType(intent.getData(), resolvedType);
1035 }
1036 try {
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001037 intent.prepareToLeaveProcess();
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001038 ActivityManagerNative.getDefault().unbroadcastIntent(
1039 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1040 } catch (RemoteException e) {
1041 }
1042 }
1043
1044 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1046 return registerReceiver(receiver, filter, null, null);
1047 }
1048
1049 @Override
1050 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1051 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001052 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001053 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
Dianne Hackborn20e80982012-08-31 19:00:44 -07001056 @Override
1057 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1058 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1059 return registerReceiverInternal(receiver, user.getIdentifier(),
1060 filter, broadcastPermission, scheduler, getOuterContext());
1061 }
1062
1063 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 IntentFilter filter, String broadcastPermission,
1065 Handler scheduler, Context context) {
1066 IIntentReceiver rd = null;
1067 if (receiver != null) {
1068 if (mPackageInfo != null && context != null) {
1069 if (scheduler == null) {
1070 scheduler = mMainThread.getHandler();
1071 }
1072 rd = mPackageInfo.getReceiverDispatcher(
1073 receiver, context, scheduler,
1074 mMainThread.getInstrumentation(), true);
1075 } else {
1076 if (scheduler == null) {
1077 scheduler = mMainThread.getHandler();
1078 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001079 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001080 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082 }
1083 try {
1084 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001085 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001086 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 } catch (RemoteException e) {
1088 return null;
1089 }
1090 }
1091
1092 @Override
1093 public void unregisterReceiver(BroadcastReceiver receiver) {
1094 if (mPackageInfo != null) {
1095 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1096 getOuterContext(), receiver);
1097 try {
1098 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1099 } catch (RemoteException e) {
1100 }
1101 } else {
1102 throw new RuntimeException("Not supported in system context");
1103 }
1104 }
1105
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001106 private void validateServiceIntent(Intent service) {
1107 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001108 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001109 IllegalArgumentException ex = new IllegalArgumentException(
1110 "Service Intent must be explicit: " + service);
1111 throw ex;
1112 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001113 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1114 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001115 }
1116 }
1117 }
1118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 @Override
1120 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001121 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001122 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001123 }
1124
1125 @Override
1126 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001127 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001128 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001129 }
1130
1131 @Override
1132 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001133 return startServiceCommon(service, user);
1134 }
1135
1136 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001138 validateServiceIntent(service);
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001139 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 ComponentName cn = ActivityManagerNative.getDefault().startService(
1141 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001142 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001143 if (cn != null) {
1144 if (cn.getPackageName().equals("!")) {
1145 throw new SecurityException(
1146 "Not allowed to start service " + service
1147 + " without permission " + cn.getClassName());
1148 } else if (cn.getPackageName().equals("!!")) {
1149 throw new SecurityException(
1150 "Unable to start service " + service
1151 + ": " + cn.getClassName());
1152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 }
1154 return cn;
1155 } catch (RemoteException e) {
1156 return null;
1157 }
1158 }
1159
1160 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001161 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001162 return stopServiceCommon(service, user);
1163 }
1164
1165 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001167 validateServiceIntent(service);
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001168 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 int res = ActivityManagerNative.getDefault().stopService(
1170 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001171 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 if (res < 0) {
1173 throw new SecurityException(
1174 "Not allowed to stop service " + service);
1175 }
1176 return res != 0;
1177 } catch (RemoteException e) {
1178 return false;
1179 }
1180 }
1181
1182 @Override
1183 public boolean bindService(Intent service, ServiceConnection conn,
1184 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001185 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001186 return bindServiceCommon(service, conn, flags, Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001187 }
1188
1189 /** @hide */
1190 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001191 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1192 UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001193 return bindServiceCommon(service, conn, flags, user);
1194 }
1195
1196 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags,
1197 UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001199 if (conn == null) {
1200 throw new IllegalArgumentException("connection is null");
1201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 if (mPackageInfo != null) {
1203 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1204 mMainThread.getHandler(), flags);
1205 } else {
1206 throw new RuntimeException("Not supported in system context");
1207 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001208 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001210 IBinder token = getActivityToken();
1211 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1212 && mPackageInfo.getApplicationInfo().targetSdkVersion
1213 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1214 flags |= BIND_WAIVE_PRIORITY;
1215 }
Jeff Sharkeya14acd22013-04-02 18:27:45 -07001216 service.prepareToLeaveProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 int res = ActivityManagerNative.getDefault().bindService(
1218 mMainThread.getApplicationThread(), getActivityToken(),
1219 service, service.resolveTypeIfNeeded(getContentResolver()),
Amith Yamasani27b89e62013-01-16 12:30:11 -08001220 sd, flags, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 if (res < 0) {
1222 throw new SecurityException(
1223 "Not allowed to bind to service " + service);
1224 }
1225 return res != 0;
1226 } catch (RemoteException e) {
1227 return false;
1228 }
1229 }
1230
1231 @Override
1232 public void unbindService(ServiceConnection conn) {
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 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1238 getOuterContext(), conn);
1239 try {
1240 ActivityManagerNative.getDefault().unbindService(sd);
1241 } catch (RemoteException e) {
1242 }
1243 } else {
1244 throw new RuntimeException("Not supported in system context");
1245 }
1246 }
1247
1248 @Override
1249 public boolean startInstrumentation(ComponentName className,
1250 String profileFile, Bundle arguments) {
1251 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001252 if (arguments != null) {
1253 arguments.setAllowFds(false);
1254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001256 className, profileFile, 0, arguments, null, null, getUserId(),
1257 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 } catch (RemoteException e) {
1259 // System has crashed, nothing we can do.
1260 }
1261 return false;
1262 }
1263
1264 @Override
1265 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001266 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 }
1268
Jeff Brown6e539312015-02-24 18:53:21 -08001269 @Override
1270 public String getSystemServiceName(Class<?> serviceClass) {
1271 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001272 }
1273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 @Override
1275 public int checkPermission(String permission, int pid, int uid) {
1276 if (permission == null) {
1277 throw new IllegalArgumentException("permission is null");
1278 }
1279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 try {
1281 return ActivityManagerNative.getDefault().checkPermission(
1282 permission, pid, uid);
1283 } catch (RemoteException e) {
1284 return PackageManager.PERMISSION_DENIED;
1285 }
1286 }
1287
Dianne Hackbornff170242014-11-19 10:59:01 -08001288 /** @hide */
1289 @Override
1290 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1291 if (permission == null) {
1292 throw new IllegalArgumentException("permission is null");
1293 }
1294
1295 try {
1296 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1297 permission, pid, uid, callerToken);
1298 } catch (RemoteException e) {
1299 return PackageManager.PERMISSION_DENIED;
1300 }
1301 }
1302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 @Override
1304 public int checkCallingPermission(String permission) {
1305 if (permission == null) {
1306 throw new IllegalArgumentException("permission is null");
1307 }
1308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 int pid = Binder.getCallingPid();
1310 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001311 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 }
1313 return PackageManager.PERMISSION_DENIED;
1314 }
1315
1316 @Override
1317 public int checkCallingOrSelfPermission(String permission) {
1318 if (permission == null) {
1319 throw new IllegalArgumentException("permission is null");
1320 }
1321
1322 return checkPermission(permission, Binder.getCallingPid(),
1323 Binder.getCallingUid());
1324 }
1325
Svetoslavc6d1c342015-02-26 14:44:43 -08001326 @Override
1327 public int checkSelfPermission(String permission) {
1328 if (permission == null) {
1329 throw new IllegalArgumentException("permission is null");
1330 }
1331
1332 return checkPermission(permission, Process.myPid(), Process.myUid());
1333 }
1334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 private void enforce(
1336 String permission, int resultOfCheck,
1337 boolean selfToo, int uid, String message) {
1338 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1339 throw new SecurityException(
1340 (message != null ? (message + ": ") : "") +
1341 (selfToo
1342 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001343 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 permission +
1345 ".");
1346 }
1347 }
1348
Jeff Brown6e539312015-02-24 18:53:21 -08001349 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 public void enforcePermission(
1351 String permission, int pid, int uid, String message) {
1352 enforce(permission,
1353 checkPermission(permission, pid, uid),
1354 false,
1355 uid,
1356 message);
1357 }
1358
Jeff Brown6e539312015-02-24 18:53:21 -08001359 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 public void enforceCallingPermission(String permission, String message) {
1361 enforce(permission,
1362 checkCallingPermission(permission),
1363 false,
1364 Binder.getCallingUid(),
1365 message);
1366 }
1367
Jeff Brown6e539312015-02-24 18:53:21 -08001368 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 public void enforceCallingOrSelfPermission(
1370 String permission, String message) {
1371 enforce(permission,
1372 checkCallingOrSelfPermission(permission),
1373 true,
1374 Binder.getCallingUid(),
1375 message);
1376 }
1377
1378 @Override
1379 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1380 try {
1381 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001382 mMainThread.getApplicationThread(), toPackage,
1383 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 } catch (RemoteException e) {
1385 }
1386 }
1387
1388 @Override
1389 public void revokeUriPermission(Uri uri, int modeFlags) {
1390 try {
1391 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001392 mMainThread.getApplicationThread(),
1393 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 } catch (RemoteException e) {
1395 }
1396 }
1397
1398 @Override
1399 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 try {
1401 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001402 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001403 resolveUserId(uri), null);
1404 } catch (RemoteException e) {
1405 return PackageManager.PERMISSION_DENIED;
1406 }
1407 }
1408
1409 /** @hide */
1410 @Override
1411 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1412 try {
1413 return ActivityManagerNative.getDefault().checkUriPermission(
1414 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1415 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 } catch (RemoteException e) {
1417 return PackageManager.PERMISSION_DENIED;
1418 }
1419 }
1420
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001421 private int resolveUserId(Uri uri) {
1422 return ContentProvider.getUserIdFromUri(uri, getUserId());
1423 }
1424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 @Override
1426 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 int pid = Binder.getCallingPid();
1428 if (pid != Process.myPid()) {
1429 return checkUriPermission(uri, pid,
1430 Binder.getCallingUid(), modeFlags);
1431 }
1432 return PackageManager.PERMISSION_DENIED;
1433 }
1434
1435 @Override
1436 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1437 return checkUriPermission(uri, Binder.getCallingPid(),
1438 Binder.getCallingUid(), modeFlags);
1439 }
1440
1441 @Override
1442 public int checkUriPermission(Uri uri, String readPermission,
1443 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001444 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1446 + readPermission + " writePermission=" + writePermission
1447 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1448 }
1449 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1450 if (readPermission == null
1451 || checkPermission(readPermission, pid, uid)
1452 == PackageManager.PERMISSION_GRANTED) {
1453 return PackageManager.PERMISSION_GRANTED;
1454 }
1455 }
1456 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1457 if (writePermission == null
1458 || checkPermission(writePermission, pid, uid)
1459 == PackageManager.PERMISSION_GRANTED) {
1460 return PackageManager.PERMISSION_GRANTED;
1461 }
1462 }
1463 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1464 : PackageManager.PERMISSION_DENIED;
1465 }
1466
1467 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001468 StringBuilder builder = new StringBuilder();
1469 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1470 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001472 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1473 builder.append("write and ");
1474 }
1475 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1476 builder.append("persistable and ");
1477 }
1478 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1479 builder.append("prefix and ");
1480 }
1481
1482 if (builder.length() > 5) {
1483 builder.setLength(builder.length() - 5);
1484 return builder.toString();
1485 } else {
1486 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 }
1489
1490 private void enforceForUri(
1491 int modeFlags, int resultOfCheck, boolean selfToo,
1492 int uid, Uri uri, String message) {
1493 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1494 throw new SecurityException(
1495 (message != null ? (message + ": ") : "") +
1496 (selfToo
1497 ? "Neither user " + uid + " nor current process has "
1498 : "User " + uid + " does not have ") +
1499 uriModeFlagToString(modeFlags) +
1500 " permission on " +
1501 uri +
1502 ".");
1503 }
1504 }
1505
Jeff Brown6e539312015-02-24 18:53:21 -08001506 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 public void enforceUriPermission(
1508 Uri uri, int pid, int uid, int modeFlags, String message) {
1509 enforceForUri(
1510 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1511 false, uid, uri, message);
1512 }
1513
Jeff Brown6e539312015-02-24 18:53:21 -08001514 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 public void enforceCallingUriPermission(
1516 Uri uri, int modeFlags, String message) {
1517 enforceForUri(
1518 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001519 false,
1520 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 }
1522
Jeff Brown6e539312015-02-24 18:53:21 -08001523 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 public void enforceCallingOrSelfUriPermission(
1525 Uri uri, int modeFlags, String message) {
1526 enforceForUri(
1527 modeFlags,
1528 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1529 Binder.getCallingUid(), uri, message);
1530 }
1531
Jeff Brown6e539312015-02-24 18:53:21 -08001532 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 public void enforceUriPermission(
1534 Uri uri, String readPermission, String writePermission,
1535 int pid, int uid, int modeFlags, String message) {
1536 enforceForUri(modeFlags,
1537 checkUriPermission(
1538 uri, readPermission, writePermission, pid, uid,
1539 modeFlags),
1540 false,
1541 uid,
1542 uri,
1543 message);
1544 }
1545
Tom O'Neill365632e2013-09-09 09:34:58 -07001546 /**
1547 * Logs a warning if the system process directly called a method such as
1548 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1549 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1550 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001551 private void warnIfCallingFromSystemProcess() {
1552 if (Process.myUid() == Process.SYSTEM_UID) {
1553 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001554 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001555 }
1556 }
1557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001559 public Context createApplicationContext(ApplicationInfo application, int flags)
1560 throws NameNotFoundException {
1561 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1562 flags | CONTEXT_REGISTER_PACKAGE);
1563 if (pi != null) {
1564 final boolean restricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
1565 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
1566 new UserHandle(UserHandle.getUserId(application.uid)), restricted,
Wale Ogunwaleb7405372015-02-09 16:35:10 -08001567 mDisplay, null);
Svetoslav976e8bd2014-07-16 15:12:03 -07001568 if (c.mResources != null) {
1569 return c;
1570 }
1571 }
1572
1573 throw new PackageManager.NameNotFoundException(
1574 "Application package " + application.packageName + " not found");
1575 }
1576
1577 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001579 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001580 return createPackageContextAsUser(packageName, flags,
1581 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001582 }
1583
1584 @Override
1585 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1586 throws NameNotFoundException {
Jeff Browndefd4a62014-03-10 21:24:37 -07001587 final boolean restricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
Kenny Guyc2a40602014-09-08 18:51:15 +00001588 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001589 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Wale Ogunwaleb7405372015-02-09 16:35:10 -08001590 user, restricted, mDisplay, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
1592
Jeff Browndefd4a62014-03-10 21:24:37 -07001593 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001594 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 if (pi != null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001596 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Wale Ogunwaleb7405372015-02-09 16:35:10 -08001597 user, restricted, mDisplay, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 if (c.mResources != null) {
1599 return c;
1600 }
1601 }
1602
1603 // Should be a better exception.
1604 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001605 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 }
1607
Romain Guy870e09f2009-07-06 16:35:25 -07001608 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001609 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001610 if (overrideConfiguration == null) {
1611 throw new IllegalArgumentException("overrideConfiguration must not be null");
1612 }
1613
Jeff Browndefd4a62014-03-10 21:24:37 -07001614 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1615 mUser, mRestricted, mDisplay, overrideConfiguration);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001616 }
1617
1618 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001619 public Context createDisplayContext(Display display) {
1620 if (display == null) {
1621 throw new IllegalArgumentException("display must not be null");
1622 }
1623
Jeff Browndefd4a62014-03-10 21:24:37 -07001624 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Wale Ogunwaleb7405372015-02-09 16:35:10 -08001625 mUser, mRestricted, display, null);
Jeff Browna492c3a2012-08-23 19:48:44 -07001626 }
1627
Jeff Brown6e539312015-02-24 18:53:21 -08001628 Display getDisplay() {
1629 if (mDisplay != null) {
1630 return mDisplay;
1631 }
1632 DisplayManager dm = getSystemService(DisplayManager.class);
1633 return dm.getDisplay(Display.DEFAULT_DISPLAY);
1634 }
1635
Jeff Browna492c3a2012-08-23 19:48:44 -07001636 private int getDisplayId() {
1637 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1638 }
1639
1640 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001641 public boolean isRestricted() {
1642 return mRestricted;
1643 }
1644
Jeff Brown98365d72012-08-19 20:30:52 -07001645 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001646 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1647 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -07001648 }
1649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 private File getDataDirFile() {
1651 if (mPackageInfo != null) {
1652 return mPackageInfo.getDataDirFile();
1653 }
1654 throw new RuntimeException("Not supported in system context");
1655 }
1656
1657 @Override
1658 public File getDir(String name, int mode) {
1659 name = "app_" + name;
1660 File file = makeFilename(getDataDirFile(), name);
1661 if (!file.exists()) {
1662 file.mkdir();
1663 setFilePermissionsFromMode(file.getPath(), mode,
1664 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1665 }
1666 return file;
1667 }
1668
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001669 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08001670 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001671 public int getUserId() {
1672 return mUser.getIdentifier();
1673 }
1674
Dianne Hackborn21556372010-02-04 16:34:40 -08001675 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001676 LoadedApk packageInfo = new LoadedApk(mainThread);
1677 ContextImpl context = new ContextImpl(null, mainThread,
1678 packageInfo, null, null, false, null, null);
1679 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Wale Ogunwale7c726682015-02-06 17:34:28 -08001680 context.mResourcesManager.getDisplayMetricsLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 return context;
1682 }
1683
Jeff Browndefd4a62014-03-10 21:24:37 -07001684 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
1685 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
1686 return new ContextImpl(null, mainThread,
1687 packageInfo, null, null, false, null, null);
1688 }
1689
1690 static ContextImpl createActivityContext(ActivityThread mainThread,
Wale Ogunwale7c726682015-02-06 17:34:28 -08001691 LoadedApk packageInfo, int displayId, Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001692 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Wale Ogunwale7c726682015-02-06 17:34:28 -08001693 final Display display = ResourcesManager.getInstance().getAdjustedDisplay(
1694 displayId, overrideConfiguration);
1695 return new ContextImpl(null, mainThread, packageInfo, null, null, false, display,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001696 overrideConfiguration);
Jeff Browndefd4a62014-03-10 21:24:37 -07001697 }
1698
1699 private ContextImpl(ContextImpl container, ActivityThread mainThread,
1700 LoadedApk packageInfo, IBinder activityToken, UserHandle user, boolean restricted,
1701 Display display, Configuration overrideConfiguration) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703
Jeff Browndefd4a62014-03-10 21:24:37 -07001704 mMainThread = mainThread;
1705 mActivityToken = activityToken;
1706 mRestricted = restricted;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707
Jeff Browndefd4a62014-03-10 21:24:37 -07001708 if (user == null) {
1709 user = Process.myUserHandle();
1710 }
1711 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07001714 mResourcesManager = ResourcesManager.getInstance();
1715 mDisplay = display;
Jeff Browndefd4a62014-03-10 21:24:37 -07001716
1717 final int displayId = getDisplayId();
1718 CompatibilityInfo compatInfo = null;
1719 if (container != null) {
1720 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
1721 }
Wale Ogunwale9cf99542015-02-18 16:31:34 -08001722 if (compatInfo == null) {
1723 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
1724 ? packageInfo.getCompatibilityInfo()
1725 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Jeff Browndefd4a62014-03-10 21:24:37 -07001726 }
1727 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
Wale Ogunwale7c726682015-02-06 17:34:28 -08001728 mDisplayAdjustments.setConfiguration(overrideConfiguration);
Jeff Browndefd4a62014-03-10 21:24:37 -07001729
1730 Resources resources = packageInfo.getResources(mainThread);
1731 if (resources != null) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08001732 if (displayId != Display.DEFAULT_DISPLAY
Jeff Browndefd4a62014-03-10 21:24:37 -07001733 || overrideConfiguration != null
1734 || (compatInfo != null && compatInfo.applicationScale
1735 != resources.getCompatibilityInfo().applicationScale)) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001736 resources = mResourcesManager.getTopLevelResources(packageInfo.getResDir(),
1737 packageInfo.getSplitResDirs(), packageInfo.getOverlayDirs(),
1738 packageInfo.getApplicationInfo().sharedLibraryFiles, displayId,
Wale Ogunwale60454db2015-01-23 16:05:07 -08001739 overrideConfiguration, compatInfo);
Jeff Browndefd4a62014-03-10 21:24:37 -07001740 }
1741 }
1742 mResources = resources;
1743
1744 if (container != null) {
1745 mBasePackageName = container.mBasePackageName;
1746 mOpPackageName = container.mOpPackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07001747 } else {
1748 mBasePackageName = packageInfo.mPackageName;
1749 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
1750 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
1751 // Special case: system components allow themselves to be loaded in to other
1752 // processes. For purposes of app ops, we must then consider the context as
1753 // belonging to the package of this process, not the system itself, otherwise
1754 // the package+uid verifications in app ops will fail.
1755 mOpPackageName = ActivityThread.currentPackageName();
1756 } else {
1757 mOpPackageName = mBasePackageName;
1758 }
1759 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05001760
1761 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763
Narayan Kamath29564cd2014-08-07 10:57:40 +01001764 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
1765 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 }
1767
1768 final void scheduleFinalCleanup(String who, String what) {
1769 mMainThread.scheduleContextCleanup(this, who, what);
1770 }
1771
1772 final void performFinalCleanup(String who, String what) {
1773 //Log.i(TAG, "Cleanup up context: " + this);
1774 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1775 }
1776
1777 final Context getReceiverRestrictedContext() {
1778 if (mReceiverRestrictedContext != null) {
1779 return mReceiverRestrictedContext;
1780 }
1781 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1782 }
1783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 final void setOuterContext(Context context) {
1785 mOuterContext = context;
1786 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001787
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 final Context getOuterContext() {
1789 return mOuterContext;
1790 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 final IBinder getActivityToken() {
1793 return mActivityToken;
1794 }
1795
Jeff Brown6e539312015-02-24 18:53:21 -08001796 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08001797 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 int extraPermissions) {
1799 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
1800 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
1801 |extraPermissions;
1802 if ((mode&MODE_WORLD_READABLE) != 0) {
1803 perms |= FileUtils.S_IROTH;
1804 }
1805 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
1806 perms |= FileUtils.S_IWOTH;
1807 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001808 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
1810 + ", perms=0x" + Integer.toHexString(perms));
1811 }
1812 FileUtils.setPermissions(name, perms, -1, -1);
1813 }
1814
Oscar Montemayora8529f62009-11-18 10:14:20 -08001815 private File validateFilePath(String name, boolean createDirectory) {
1816 File dir;
1817 File f;
1818
1819 if (name.charAt(0) == File.separatorChar) {
1820 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
1821 dir = new File(dirPath);
1822 name = name.substring(name.lastIndexOf(File.separatorChar));
1823 f = new File(dir, name);
1824 } else {
1825 dir = getDatabasesDir();
1826 f = makeFilename(dir, name);
1827 }
1828
1829 if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
1830 FileUtils.setPermissions(dir.getPath(),
1831 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1832 -1, -1);
1833 }
1834
1835 return f;
1836 }
1837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 private File makeFilename(File base, String name) {
1839 if (name.indexOf(File.separatorChar) < 0) {
1840 return new File(base, name);
1841 }
1842 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08001843 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
1845
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001846 /**
1847 * Ensure that given directories exist, trying to create them if missing. If
1848 * unable to create, they are filtered by replacing with {@code null}.
1849 */
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07001850 private File[] ensureDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001851 File[] result = new File[dirs.length];
1852 for (int i = 0; i < dirs.length; i++) {
1853 File dir = dirs[i];
1854 if (!dir.exists()) {
1855 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07001856 // recheck existence in case of cross-process race
1857 if (!dir.exists()) {
1858 // Failing to mkdir() may be okay, since we might not have
1859 // enough permissions; ask vold to create on our behalf.
1860 final IMountService mount = IMountService.Stub.asInterface(
1861 ServiceManager.getService("mount"));
1862 int res = -1;
1863 try {
1864 res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
Jeff Sharkey97086692015-01-08 14:00:13 -08001865 } catch (Exception ignored) {
Christopher Tatecc866da2013-10-02 18:11:01 -07001866 }
1867 if (res != 0) {
1868 Log.w(TAG, "Failed to ensure directory: " + dir);
1869 dir = null;
1870 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07001871 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07001872 }
1873 }
1874 result[i] = dir;
1875 }
1876 return result;
1877 }
1878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 // ----------------------------------------------------------------------
1880 // ----------------------------------------------------------------------
1881 // ----------------------------------------------------------------------
1882
1883 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001884 private final ActivityThread mMainThread;
1885 private final UserHandle mUser;
1886
1887 public ApplicationContentResolver(
1888 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07001890 mMainThread = Preconditions.checkNotNull(mainThread);
1891 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 }
1893
1894 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001895 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001896 return mMainThread.acquireProvider(context,
1897 ContentProvider.getAuthorityWithoutUserId(auth),
1898 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 }
1900
1901 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001902 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001903 return mMainThread.acquireExistingProvider(context,
1904 ContentProvider.getAuthorityWithoutUserId(auth),
1905 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07001906 }
1907
1908 @Override
1909 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001910 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02001912
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001913 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07001914 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001915 return mMainThread.acquireProvider(c,
1916 ContentProvider.getAuthorityWithoutUserId(auth),
1917 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001918 }
1919
1920 @Override
1921 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001922 return mMainThread.releaseProvider(icp, false);
1923 }
1924
1925 @Override
1926 public void unstableProviderDied(IContentProvider icp) {
1927 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07001928 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07001929
1930 @Override
1931 public void appNotRespondingViaProvider(IContentProvider icp) {
1932 mMainThread.appNotRespondingViaProvider(icp.asBinder());
1933 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001934
1935 /** @hide */
1936 protected int resolveUserIdFromAuthority(String auth) {
1937 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
1938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940}