blob: 8884949f361cf1050a8cbf1a4bf1577583e7eb90 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019import android.content.BroadcastReceiver;
20import android.content.ComponentName;
Nicolas Prevotd85fc722014-04-16 19:52:08 +010021import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ContentResolver;
23import android.content.Context;
24import android.content.ContextWrapper;
25import android.content.IContentProvider;
Jeff Brown6e539312015-02-24 18:53:21 -080026import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Intent;
28import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070029import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.ReceiverCallNotAllowedException;
31import android.content.ServiceConnection;
32import android.content.SharedPreferences;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.IPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070036import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.res.AssetManager;
Dianne Hackborn5be8de32011-05-24 18:11:57 -070038import android.content.res.CompatibilityInfo;
Dianne Hackborn756220b2012-08-14 16:45:30 -070039import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.content.res.Resources;
Vasu Nori74f170f2010-06-01 18:06:18 -070041import android.database.DatabaseErrorHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.database.sqlite.SQLiteDatabase;
43import android.database.sqlite.SQLiteDatabase.CursorFactory;
44import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.graphics.drawable.Drawable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Binder;
Jeff Brown6e539312015-02-24 18:53:21 -080048import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Bundle;
Amith Yamasanicd757062012-10-19 18:23:52 -070050import android.os.Debug;
Oscar Montemayor539d3c42010-01-29 15:27:00 -080051import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.FileUtils;
53import android.os.Handler;
54import android.os.IBinder;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.os.Looper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070059import android.os.UserHandle;
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -070060import android.os.storage.IMountService;
Jeff Sharkeye84bdd32016-02-08 12:16:00 -070061import android.system.ErrnoException;
62import android.system.Os;
63import android.system.OsConstants;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.util.AndroidRuntimeException;
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -070065import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.Log;
Amith Yamasanicd757062012-10-19 18:23:52 -070067import android.util.Slog;
Jeff Brown98365d72012-08-19 20:30:52 -070068import android.view.Display;
Jeff Brown6e539312015-02-24 18:53:21 -080069import android.view.DisplayAdjustments;
Dan Egnor95240272009-10-27 18:23:39 -070070
Jeff Sharkey35871f22016-01-29 17:13:29 -070071import com.android.internal.annotations.GuardedBy;
72import com.android.internal.util.Preconditions;
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.io.File;
75import java.io.FileInputStream;
76import java.io.FileNotFoundException;
77import java.io.FileOutputStream;
Jeff Sharkey35871f22016-01-29 17:13:29 -070078import java.io.FilenameFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.io.IOException;
80import java.io.InputStream;
Jeff Sharkey7a30a302015-12-08 14:20:06 -070081import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083class ReceiverRestrictedContext extends ContextWrapper {
84 ReceiverRestrictedContext(Context base) {
85 super(base);
86 }
87
88 @Override
89 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
90 return registerReceiver(receiver, filter, null, null);
91 }
92
93 @Override
94 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
95 String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070096 if (receiver == null) {
97 // Allow retrieving current sticky broadcast; this is safe since we
98 // aren't actually registering a receiver.
99 return super.registerReceiver(null, filter, broadcastPermission, scheduler);
100 } else {
101 throw new ReceiverCallNotAllowedException(
102 "BroadcastReceiver components are not allowed to register to receive intents");
103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 }
105
106 @Override
Dianne Hackborn20e80982012-08-31 19:00:44 -0700107 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
108 IntentFilter filter, String broadcastPermission, Handler scheduler) {
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700109 if (receiver == null) {
110 // Allow retrieving current sticky broadcast; this is safe since we
111 // aren't actually registering a receiver.
112 return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler);
113 } else {
114 throw new ReceiverCallNotAllowedException(
115 "BroadcastReceiver components are not allowed to register to receive intents");
116 }
Dianne Hackborn20e80982012-08-31 19:00:44 -0700117 }
118
119 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public boolean bindService(Intent service, ServiceConnection conn, int flags) {
121 throw new ReceiverCallNotAllowedException(
Jeff Sharkey27bd34d2012-09-16 12:49:00 -0700122 "BroadcastReceiver components are not allowed to bind to services");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
124}
125
126/**
Dianne Hackborn21556372010-02-04 16:34:40 -0800127 * Common implementation of Context API, which provides the base
128 * context object for Activity and other application components.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
Dianne Hackborn21556372010-02-04 16:34:40 -0800130class ContextImpl extends Context {
Dianne Hackborn40e9f292012-11-27 19:12:23 -0800131 private final static String TAG = "ContextImpl";
Mitsuru Oshima569076c2009-07-02 20:06:08 -0700132 private final static boolean DEBUG = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
Jeff Sharkey8e3ddab2013-06-17 18:26:37 -0700134 /**
135 * Map from package name, to preference name, to cached preferences.
136 */
Jeff Sharkeybe782582016-02-15 18:35:57 -0700137 @GuardedBy("ContextImpl.class")
138 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefsCache;
139
140 /**
141 * Map from preference name to generated path.
142 */
143 @GuardedBy("ContextImpl.class")
144 private ArrayMap<String, File> mSharedPrefsPaths;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Jeff Browndefd4a62014-03-10 21:24:37 -0700146 final ActivityThread mMainThread;
147 final LoadedApk mPackageInfo;
148
149 private final IBinder mActivityToken;
150
151 private final UserHandle mUser;
152
153 private final ApplicationContentResolver mContentResolver;
154
155 private final String mBasePackageName;
156 private final String mOpPackageName;
157
158 private final ResourcesManager mResourcesManager;
159 private final Resources mResources;
160 private final Display mDisplay; // may be null if default display
161 private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments();
Jeff Browndefd4a62014-03-10 21:24:37 -0700162
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700163 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 private int mThemeResource = 0;
167 private Resources.Theme mTheme = null;
168 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170
171 private final Object mSync = new Object();
172
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700173 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700175 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700177 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700179 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700180 private File mNoBackupFilesDir;
181 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700183 @GuardedBy("mSync")
184 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700185
186 @GuardedBy("mSync")
187 private File[] mExternalObbDirs;
188 @GuardedBy("mSync")
189 private File[] mExternalFilesDirs;
190 @GuardedBy("mSync")
191 private File[] mExternalCacheDirs;
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700192 @GuardedBy("mSync")
193 private File[] mExternalMediaDirs;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200194
Jeff Brown6e539312015-02-24 18:53:21 -0800195 // The system service cache for the system services that are cached per-ContextImpl.
196 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800197
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700198 static ContextImpl getImpl(Context context) {
199 Context nextContext;
200 while ((context instanceof ContextWrapper) &&
201 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
202 context = nextContext;
203 }
204 return (ContextImpl)context;
205 }
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 @Override
208 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700209 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211
212 @Override
213 public Resources getResources() {
214 return mResources;
215 }
216
217 @Override
218 public PackageManager getPackageManager() {
219 if (mPackageManager != null) {
220 return mPackageManager;
221 }
222
223 IPackageManager pm = ActivityThread.getPackageManager();
224 if (pm != null) {
225 // Doesn't matter if we make more than one instance.
226 return (mPackageManager = new ApplicationPackageManager(this, pm));
227 }
228
229 return null;
230 }
231
232 @Override
233 public ContentResolver getContentResolver() {
234 return mContentResolver;
235 }
236
237 @Override
238 public Looper getMainLooper() {
239 return mMainThread.getLooper();
240 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 @Override
243 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700244 return (mPackageInfo != null) ?
245 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700249 public void setTheme(int resId) {
250 if (mThemeResource != resId) {
251 mThemeResource = resId;
252 initializeTheme();
253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800257 public int getThemeResId() {
258 return mThemeResource;
259 }
260
261 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 public Resources.Theme getTheme() {
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700263 if (mTheme != null) {
264 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700266
267 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
268 getOuterContext().getApplicationInfo().targetSdkVersion);
269 initializeTheme();
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 return mTheme;
272 }
273
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700274 private void initializeTheme() {
275 if (mTheme == null) {
276 mTheme = mResources.newTheme();
277 }
278 mTheme.applyStyle(mThemeResource, true);
279 }
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 @Override
282 public ClassLoader getClassLoader() {
283 return mPackageInfo != null ?
284 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
285 }
286
287 @Override
288 public String getPackageName() {
289 if (mPackageInfo != null) {
290 return mPackageInfo.getPackageName();
291 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800292 // No mPackageInfo means this is a Context for the system itself,
293 // and this here is its name.
294 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 }
296
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800297 /** @hide */
298 @Override
299 public String getBasePackageName() {
300 return mBasePackageName != null ? mBasePackageName : getPackageName();
301 }
302
Dianne Hackborn95d78532013-09-11 09:51:14 -0700303 /** @hide */
304 @Override
305 public String getOpPackageName() {
306 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
307 }
308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700310 public ApplicationInfo getApplicationInfo() {
311 if (mPackageInfo != null) {
312 return mPackageInfo.getApplicationInfo();
313 }
314 throw new RuntimeException("Not supported in system context");
315 }
316
317 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 public String getPackageResourcePath() {
319 if (mPackageInfo != null) {
320 return mPackageInfo.getResDir();
321 }
322 throw new RuntimeException("Not supported in system context");
323 }
324
325 @Override
326 public String getPackageCodePath() {
327 if (mPackageInfo != null) {
328 return mPackageInfo.getAppDir();
329 }
330 throw new RuntimeException("Not supported in system context");
331 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200332
Jeff Brown6e539312015-02-24 18:53:21 -0800333 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700335 // At least one application in the world actually passes in a null
336 // name. This happened to work because when we generated the file name
337 // we would stringify it to "null.xml". Nice.
338 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
339 Build.VERSION_CODES.KITKAT) {
340 if (name == null) {
341 name = "null";
342 }
343 }
344
Jeff Sharkeybe782582016-02-15 18:35:57 -0700345 File file;
346 synchronized (ContextImpl.class) {
347 if (mSharedPrefsPaths == null) {
348 mSharedPrefsPaths = new ArrayMap<>();
349 }
350 file = mSharedPrefsPaths.get(name);
351 if (file == null) {
352 file = getSharedPreferencesPath(name);
353 mSharedPrefsPaths.put(name, file);
354 }
355 }
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700356 return getSharedPreferences(file, mode);
357 }
358
359 @Override
360 public SharedPreferences getSharedPreferences(File file, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700361 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700363 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700364 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
365 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700366 if (sp == null) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700367 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700368 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700369 return sp;
370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800372 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
373 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
374 // If somebody else (some other process) changed the prefs
375 // file behind our back, we reload it. This has been the
376 // historical (if undocumented) behavior.
377 sp.startReloadIfChangedUnexpectedly();
378 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700379 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 }
381
Jeff Sharkey35871f22016-01-29 17:13:29 -0700382 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
Jeff Sharkeybe782582016-02-15 18:35:57 -0700383 if (sSharedPrefsCache == null) {
384 sSharedPrefsCache = new ArrayMap<>();
Jeff Sharkey35871f22016-01-29 17:13:29 -0700385 }
386
387 final String packageName = getPackageName();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700388 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefsCache.get(packageName);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700389 if (packagePrefs == null) {
390 packagePrefs = new ArrayMap<>();
Jeff Sharkeybe782582016-02-15 18:35:57 -0700391 sSharedPrefsCache.put(packageName, packagePrefs);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700392 }
393
394 return packagePrefs;
395 }
396
397 /**
398 * Try our best to migrate all files from source to target that match
399 * requested prefix. Return false if we have any trouble migrating.
400 */
401 private static boolean migrateFiles(File sourceDir, File targetDir, final String prefix) {
402 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
403 @Override
404 public boolean accept(File dir, String name) {
405 return name.startsWith(prefix);
406 }
407 });
408
409 boolean res = true;
410 for (File sourceFile : sourceFiles) {
411 final File targetFile = new File(targetDir, sourceFile.getName());
412 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
413 try {
414 FileUtils.copyFileOrThrow(sourceFile, targetFile);
415 FileUtils.copyPermissions(sourceFile, targetFile);
416 if (!sourceFile.delete()) {
417 throw new IOException("Failed to clean up " + sourceFile);
418 }
419 } catch (IOException e) {
420 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
421 res = false;
422 }
423 }
424 return res;
425 }
426
427 @Override
428 public boolean migrateSharedPreferencesFrom(Context sourceContext, String name) {
429 synchronized (ContextImpl.class) {
430 final File source = sourceContext.getSharedPreferencesPath(name);
431 final File target = getSharedPreferencesPath(name);
432
433 // Evict any in-memory caches for either location
434 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
435 cache.remove(source);
436 cache.remove(target);
437
438 return migrateFiles(source.getParentFile(), target.getParentFile(), source.getName());
439 }
440 }
441
442 @Override
443 public boolean deleteSharedPreferences(String name) {
444 synchronized (ContextImpl.class) {
445 final File prefs = getSharedPreferencesPath(name);
446 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
447
448 // Evict any in-memory caches
449 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
450 cache.remove(prefs);
451
452 prefs.delete();
453 prefsBackup.delete();
454
455 // We failed if files are still lingering
456 return !(prefs.exists() || prefsBackup.exists());
457 }
458 }
459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 private File getPreferencesDir() {
461 synchronized (mSync) {
462 if (mPreferencesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700463 mPreferencesDir = new File(getDataDir(), "shared_prefs");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700465 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467 }
468
469 @Override
470 public FileInputStream openFileInput(String name)
471 throws FileNotFoundException {
472 File f = makeFilename(getFilesDir(), name);
473 return new FileInputStream(f);
474 }
475
476 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700477 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
478 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 final boolean append = (mode&MODE_APPEND) != 0;
480 File f = makeFilename(getFilesDir(), name);
481 try {
482 FileOutputStream fos = new FileOutputStream(f, append);
483 setFilePermissionsFromMode(f.getPath(), mode, 0);
484 return fos;
485 } catch (FileNotFoundException e) {
486 }
487
488 File parent = f.getParentFile();
489 parent.mkdir();
490 FileUtils.setPermissions(
491 parent.getPath(),
492 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
493 -1, -1);
494 FileOutputStream fos = new FileOutputStream(f, append);
495 setFilePermissionsFromMode(f.getPath(), mode, 0);
496 return fos;
497 }
498
499 @Override
500 public boolean deleteFile(String name) {
501 File f = makeFilename(getFilesDir(), name);
502 return f.delete();
503 }
504
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700505 /**
506 * Common-path handling of app data dir creation
507 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700508 private static File ensurePrivateDirExists(File file) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700509 if (!file.exists()) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700510 try {
511 Os.mkdir(file.getAbsolutePath(), 0771);
Jeff Sharkey46ed6f42016-02-15 14:16:08 -0700512 Os.chmod(file.getAbsolutePath(), 0771);
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700513 } catch (ErrnoException e) {
514 if (e.errno == OsConstants.EEXIST) {
515 // We must have raced with someone; that's okay
516 } else {
517 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700518 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700519 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700520 }
521 return file;
522 }
523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 @Override
525 public File getFilesDir() {
526 synchronized (mSync) {
527 if (mFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700528 mFilesDir = new File(getDataDir(), "files");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700530 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700531 }
532 }
533
534 @Override
535 public File getNoBackupFilesDir() {
536 synchronized (mSync) {
537 if (mNoBackupFilesDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700538 mNoBackupFilesDir = new File(getDataDir(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700540 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 }
542 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800545 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700546 // Operates on primary external storage
547 return getExternalFilesDirs(type)[0];
548 }
549
550 @Override
551 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800552 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700553 if (mExternalFilesDirs == null) {
554 mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800555 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700556
557 // Splice in requested type, if any
558 File[] dirs = mExternalFilesDirs;
559 if (type != null) {
560 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800561 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700562
563 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700564 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800565 }
566 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200567
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800568 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800569 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700570 // Operates on primary external storage
571 return getObbDirs()[0];
572 }
573
574 @Override
575 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800576 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700577 if (mExternalObbDirs == null) {
578 mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800579 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700580
581 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700582 return ensureExternalDirsExistOrFilter(mExternalObbDirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800583 }
584 }
585
586 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 public File getCacheDir() {
588 synchronized (mSync) {
589 if (mCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700590 mCacheDir = new File(getDataDir(), "cache");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700592 return ensurePrivateDirExists(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200595
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800596 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700597 public File getCodeCacheDir() {
598 synchronized (mSync) {
599 if (mCodeCacheDir == null) {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700600 mCodeCacheDir = new File(getDataDir(), "code_cache");
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700601 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700602 return ensurePrivateDirExists(mCodeCacheDir);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700603 }
604 }
605
606 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800607 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700608 // Operates on primary external storage
609 return getExternalCacheDirs()[0];
610 }
611
612 @Override
613 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800614 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700615 if (mExternalCacheDirs == null) {
616 mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800617 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700618
619 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700620 return ensureExternalDirsExistOrFilter(mExternalCacheDirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800621 }
622 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700625 public File[] getExternalMediaDirs() {
626 synchronized (mSync) {
627 if (mExternalMediaDirs == null) {
628 mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
629 }
630
631 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700632 return ensureExternalDirsExistOrFilter(mExternalMediaDirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700633 }
634 }
635
636 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 public File getFileStreamPath(String name) {
638 return makeFilename(getFilesDir(), name);
639 }
640
641 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700642 public File getSharedPreferencesPath(String name) {
643 return makeFilename(getPreferencesDir(), name + ".xml");
644 }
645
646 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700648 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
650
651 @Override
652 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700653 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 }
655
656 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700657 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
658 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700659 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700660 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700661 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
662 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
663 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
664 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800665 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
666 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
667 }
Jeff Brown47847f32012-03-22 19:13:11 -0700668 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700669 setFilePermissionsFromMode(f.getPath(), mode, 0);
670 return db;
671 }
672
673 @Override
Jeff Sharkey35871f22016-01-29 17:13:29 -0700674 public boolean migrateDatabaseFrom(Context sourceContext, String name) {
675 synchronized (ContextImpl.class) {
676 final File source = sourceContext.getDatabasePath(name);
677 final File target = getDatabasePath(name);
678 return migrateFiles(source.getParentFile(), target.getParentFile(), source.getName());
679 }
680 }
681
682 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 public boolean deleteDatabase(String name) {
684 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700685 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800686 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 } catch (Exception e) {
688 }
689 return false;
690 }
691
692 @Override
693 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700694 File dir;
695 File f;
696
697 if (name.charAt(0) == File.separatorChar) {
698 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
699 dir = new File(dirPath);
700 name = name.substring(name.lastIndexOf(File.separatorChar));
701 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700702
703 if (!dir.isDirectory() && dir.mkdir()) {
704 FileUtils.setPermissions(dir.getPath(),
705 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
706 -1, -1);
707 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700708 } else {
709 dir = getDatabasesDir();
710 f = makeFilename(dir, name);
711 }
712
Jeff Sharkey35871f22016-01-29 17:13:29 -0700713 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715
716 @Override
717 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700718 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 private File getDatabasesDir() {
722 synchronized (mSync) {
723 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700724 if ("android".equals(getPackageName())) {
725 mDatabasesDir = new File("/data/system");
726 } else {
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -0700727 mDatabasesDir = new File(getDataDir(), "databases");
Jeff Sharkey35871f22016-01-29 17:13:29 -0700728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700730 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800735 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700737 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
739
740 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800741 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700742 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700743 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745
746 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800747 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700749 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751
752 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800753 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700755 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
758 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800759 @Deprecated
760 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700761 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
763
764 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800765 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700767 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
770 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800771 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700773 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 }
775
Jeff Brown6e539312015-02-24 18:53:21 -0800776 private WallpaperManager getWallpaperManager() {
777 return getSystemService(WallpaperManager.class);
778 }
779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 @Override
781 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700782 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700783 startActivity(intent, null);
784 }
785
Amith Yamasani82644082012-08-03 13:09:11 -0700786 /** @hide */
787 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700788 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700789 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700790 }
791
Dianne Hackborna4972e92012-03-14 10:38:05 -0700792 @Override
793 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700794 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
796 throw new AndroidRuntimeException(
797 "Calling startActivity() from outside of an Activity "
798 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
799 + " Is this really what you want?");
800 }
801 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700802 getOuterContext(), mMainThread.getApplicationThread(), null,
803 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805
Amith Yamasani258848d2012-08-10 17:06:33 -0700806 /** @hide */
807 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700808 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700809 try {
810 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800811 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700812 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700813 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700814 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700815 } catch (RemoteException e) {
816 throw new RuntimeException("Failure from system", e);
Amith Yamasani258848d2012-08-10 17:06:33 -0700817 }
818 }
819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800821 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700822 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700823 startActivities(intents, null);
824 }
825
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700826 /** @hide */
827 @Override
828 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
829 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
830 throw new AndroidRuntimeException(
831 "Calling startActivities() from outside of an Activity "
832 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
833 + " Is this really what you want?");
834 }
835 mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700836 getOuterContext(), mMainThread.getApplicationThread(), null,
837 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700838 }
839
Dianne Hackborna4972e92012-03-14 10:38:05 -0700840 @Override
841 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700842 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800843 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
844 throw new AndroidRuntimeException(
845 "Calling startActivities() from outside of an Activity "
846 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
847 + " Is this really what you want?");
848 }
849 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700850 getOuterContext(), mMainThread.getApplicationThread(), null,
851 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800852 }
853
854 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700855 public void startIntentSender(IntentSender intent,
856 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
857 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700858 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
859 }
860
861 @Override
862 public void startIntentSender(IntentSender intent, Intent fillInIntent,
863 int flagsMask, int flagsValues, int extraFlags, Bundle options)
864 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700865 try {
866 String resolvedType = null;
867 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700868 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -0700869 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700870 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
871 }
872 int result = ActivityManagerNative.getDefault()
873 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
874 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700875 0, flagsMask, flagsValues, options);
876 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700877 throw new IntentSender.SendIntentException();
878 }
879 Instrumentation.checkStartActivityResult(result, null);
880 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700881 throw new RuntimeException("Failure from system", e);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700882 }
883 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200884
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700885 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700887 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
889 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700890 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700892 mMainThread.getApplicationThread(), intent, resolvedType, null,
893 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
894 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700896 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898 }
899
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800900 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700902 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700904 String[] receiverPermissions = receiverPermission == null ? null
905 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700907 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700909 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700910 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
911 null, false, false, getUserId());
912 } catch (RemoteException e) {
913 throw new RuntimeException("Failure from system", e);
914 }
915 }
916
917 @Override
918 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
919 warnIfCallingFromSystemProcess();
920 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
921 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700922 intent.prepareToLeaveProcess(this);
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700923 ActivityManagerNative.getDefault().broadcastIntent(
924 mMainThread.getApplicationThread(), intent, resolvedType, null,
925 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700926 null, false, false, getUserId());
927 } catch (RemoteException e) {
928 throw new RuntimeException("Failure from system", e);
929 }
930 }
931
932 @Override
933 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
934 warnIfCallingFromSystemProcess();
935 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700936 String[] receiverPermissions = receiverPermission == null ? null
937 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -0700938 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700939 intent.prepareToLeaveProcess(this);
Dianne Hackborna750a632015-06-16 17:18:23 -0700940 ActivityManagerNative.getDefault().broadcastIntent(
941 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700942 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700943 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800944 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700945 throw new RuntimeException("Failure from system", e);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800946 }
947 }
948
949 @Override
950 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
951 warnIfCallingFromSystemProcess();
952 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700953 String[] receiverPermissions = receiverPermission == null ? null
954 : new String[] {receiverPermission};
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800955 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700956 intent.prepareToLeaveProcess(this);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800957 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700958 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700959 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -0700960 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700962 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964 }
965
966 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -0700967 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700968 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700970 String[] receiverPermissions = receiverPermission == null ? null
971 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700973 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700975 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700976 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700977 null, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700979 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981 }
982
983 @Override
984 public void sendOrderedBroadcast(Intent intent,
985 String receiverPermission, BroadcastReceiver resultReceiver,
986 Handler scheduler, int initialCode, String initialData,
987 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800988 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700989 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
990 }
991
992 @Override
993 public void sendOrderedBroadcast(Intent intent,
994 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
995 Handler scheduler, int initialCode, String initialData,
996 Bundle initialExtras) {
997 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
998 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800999 }
1000
1001 @Override
1002 public void sendOrderedBroadcast(Intent intent,
1003 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1004 Handler scheduler, int initialCode, String initialData,
1005 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -07001006 sendOrderedBroadcast(intent, receiverPermission, appOp,
1007 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
1008 }
1009
1010 void sendOrderedBroadcast(Intent intent,
1011 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
1012 Handler scheduler, int initialCode, String initialData,
1013 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001014 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 IIntentReceiver rd = null;
1016 if (resultReceiver != null) {
1017 if (mPackageInfo != null) {
1018 if (scheduler == null) {
1019 scheduler = mMainThread.getHandler();
1020 }
1021 rd = mPackageInfo.getReceiverDispatcher(
1022 resultReceiver, getOuterContext(), scheduler,
1023 mMainThread.getInstrumentation(), false);
1024 } else {
1025 if (scheduler == null) {
1026 scheduler = mMainThread.getHandler();
1027 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001028 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1030 }
1031 }
1032 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001033 String[] receiverPermissions = receiverPermission == null ? null
1034 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001036 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 ActivityManagerNative.getDefault().broadcastIntent(
1038 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001039 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001040 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001042 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
1044 }
1045
1046 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001047 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001048 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1049 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001050 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001051 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001052 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001053 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001054 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001055 throw new RuntimeException("Failure from system", e);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001056 }
1057 }
1058
1059 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001060 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1061 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001062 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1063 }
1064
1065 @Override
1066 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1067 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001068 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001069 String[] receiverPermissions = receiverPermission == null ? null
1070 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001071 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001072 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001073 ActivityManagerNative.getDefault().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001074 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001075 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001076 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001077 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001078 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001079 }
1080 }
1081
1082 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001083 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001084 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001085 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001086 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001087 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001088 }
1089
1090 @Override
1091 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1092 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001093 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1094 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1095 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1096 }
1097
1098 @Override
1099 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1100 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1101 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001102 IIntentReceiver rd = null;
1103 if (resultReceiver != null) {
1104 if (mPackageInfo != null) {
1105 if (scheduler == null) {
1106 scheduler = mMainThread.getHandler();
1107 }
1108 rd = mPackageInfo.getReceiverDispatcher(
1109 resultReceiver, getOuterContext(), scheduler,
1110 mMainThread.getInstrumentation(), false);
1111 } else {
1112 if (scheduler == null) {
1113 scheduler = mMainThread.getHandler();
1114 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001115 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1116 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001117 }
1118 }
1119 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001120 String[] receiverPermissions = receiverPermission == null ? null
1121 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001122 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001123 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001124 ActivityManagerNative.getDefault().broadcastIntent(
1125 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001126 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001127 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001128 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001129 throw new RuntimeException("Failure from system", e);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001130 }
1131 }
1132
1133 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001134 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001136 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1138 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001139 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 ActivityManagerNative.getDefault().broadcastIntent(
1141 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001142 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001143 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001145 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 }
1147 }
1148
1149 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001150 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001151 public void sendStickyOrderedBroadcast(Intent intent,
1152 BroadcastReceiver resultReceiver,
1153 Handler scheduler, int initialCode, String initialData,
1154 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001155 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001156 IIntentReceiver rd = null;
1157 if (resultReceiver != null) {
1158 if (mPackageInfo != null) {
1159 if (scheduler == null) {
1160 scheduler = mMainThread.getHandler();
1161 }
1162 rd = mPackageInfo.getReceiverDispatcher(
1163 resultReceiver, getOuterContext(), scheduler,
1164 mMainThread.getInstrumentation(), false);
1165 } else {
1166 if (scheduler == null) {
1167 scheduler = mMainThread.getHandler();
1168 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001169 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001170 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1171 }
1172 }
1173 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1174 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001175 intent.prepareToLeaveProcess(this);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001176 ActivityManagerNative.getDefault().broadcastIntent(
1177 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1178 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001179 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001180 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001181 throw new RuntimeException("Failure from system", e);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001182 }
1183 }
1184
1185 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001186 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 public void removeStickyBroadcast(Intent intent) {
1188 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1189 if (resolvedType != null) {
1190 intent = new Intent(intent);
1191 intent.setDataAndType(intent.getData(), resolvedType);
1192 }
1193 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001194 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001196 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001198 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 }
1200 }
1201
1202 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001203 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001204 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1205 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1206 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001207 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001208 ActivityManagerNative.getDefault().broadcastIntent(
1209 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001210 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1211 user.getIdentifier());
1212 } catch (RemoteException e) {
1213 throw new RuntimeException("Failure from system", e);
1214 }
1215 }
1216
1217 @Override
1218 @Deprecated
1219 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1220 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1221 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001222 intent.prepareToLeaveProcess(this);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001223 ActivityManagerNative.getDefault().broadcastIntent(
1224 mMainThread.getApplicationThread(), intent, resolvedType, null,
1225 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1226 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001227 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001228 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001229 }
1230 }
1231
1232 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001233 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001234 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1235 UserHandle user, BroadcastReceiver resultReceiver,
1236 Handler scheduler, int initialCode, String initialData,
1237 Bundle initialExtras) {
1238 IIntentReceiver rd = null;
1239 if (resultReceiver != null) {
1240 if (mPackageInfo != null) {
1241 if (scheduler == null) {
1242 scheduler = mMainThread.getHandler();
1243 }
1244 rd = mPackageInfo.getReceiverDispatcher(
1245 resultReceiver, getOuterContext(), scheduler,
1246 mMainThread.getInstrumentation(), false);
1247 } else {
1248 if (scheduler == null) {
1249 scheduler = mMainThread.getHandler();
1250 }
1251 rd = new LoadedApk.ReceiverDispatcher(
1252 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1253 }
1254 }
1255 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1256 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001257 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001258 ActivityManagerNative.getDefault().broadcastIntent(
1259 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1260 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001261 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001262 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001263 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001264 }
1265 }
1266
1267 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001268 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001269 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1270 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1271 if (resolvedType != null) {
1272 intent = new Intent(intent);
1273 intent.setDataAndType(intent.getData(), resolvedType);
1274 }
1275 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001276 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001277 ActivityManagerNative.getDefault().unbroadcastIntent(
1278 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1279 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001280 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001281 }
1282 }
1283
1284 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1286 return registerReceiver(receiver, filter, null, null);
1287 }
1288
1289 @Override
1290 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1291 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001292 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001293 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 }
1295
Dianne Hackborn20e80982012-08-31 19:00:44 -07001296 @Override
1297 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1298 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1299 return registerReceiverInternal(receiver, user.getIdentifier(),
1300 filter, broadcastPermission, scheduler, getOuterContext());
1301 }
1302
1303 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 IntentFilter filter, String broadcastPermission,
1305 Handler scheduler, Context context) {
1306 IIntentReceiver rd = null;
1307 if (receiver != null) {
1308 if (mPackageInfo != null && context != null) {
1309 if (scheduler == null) {
1310 scheduler = mMainThread.getHandler();
1311 }
1312 rd = mPackageInfo.getReceiverDispatcher(
1313 receiver, context, scheduler,
1314 mMainThread.getInstrumentation(), true);
1315 } else {
1316 if (scheduler == null) {
1317 scheduler = mMainThread.getHandler();
1318 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001319 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001320 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322 }
1323 try {
1324 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001325 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001326 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 } catch (RemoteException e) {
1328 return null;
1329 }
1330 }
1331
1332 @Override
1333 public void unregisterReceiver(BroadcastReceiver receiver) {
1334 if (mPackageInfo != null) {
1335 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1336 getOuterContext(), receiver);
1337 try {
1338 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1339 } catch (RemoteException e) {
1340 }
1341 } else {
1342 throw new RuntimeException("Not supported in system context");
1343 }
1344 }
1345
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001346 private void validateServiceIntent(Intent service) {
1347 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001348 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001349 IllegalArgumentException ex = new IllegalArgumentException(
1350 "Service Intent must be explicit: " + service);
1351 throw ex;
1352 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001353 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1354 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001355 }
1356 }
1357 }
1358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 @Override
1360 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001361 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001362 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001363 }
1364
1365 @Override
1366 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001367 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001368 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001369 }
1370
1371 @Override
1372 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001373 return startServiceCommon(service, user);
1374 }
1375
1376 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001378 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001379 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 ComponentName cn = ActivityManagerNative.getDefault().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001381 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
1382 getContentResolver()), getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001383 if (cn != null) {
1384 if (cn.getPackageName().equals("!")) {
1385 throw new SecurityException(
1386 "Not allowed to start service " + service
1387 + " without permission " + cn.getClassName());
1388 } else if (cn.getPackageName().equals("!!")) {
1389 throw new SecurityException(
1390 "Unable to start service " + service
1391 + ": " + cn.getClassName());
1392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001393 }
1394 return cn;
1395 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001396 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
1398 }
1399
1400 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001401 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001402 return stopServiceCommon(service, user);
1403 }
1404
1405 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001407 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001408 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 int res = ActivityManagerNative.getDefault().stopService(
1410 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001411 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 if (res < 0) {
1413 throw new SecurityException(
1414 "Not allowed to stop service " + service);
1415 }
1416 return res != 0;
1417 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001418 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
1420 }
1421
1422 @Override
1423 public boolean bindService(Intent service, ServiceConnection conn,
1424 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001425 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001426 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1427 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001428 }
1429
1430 /** @hide */
1431 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001432 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1433 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001434 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001435 }
1436
Adrian Roos691546e2016-02-09 10:13:41 -08001437 /** @hide */
1438 @Override
1439 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1440 Handler handler, UserHandle user) {
1441 if (handler == null) {
1442 throw new IllegalArgumentException("handler must not be null.");
1443 }
1444 return bindServiceCommon(service, conn, flags, handler, user);
1445 }
1446
1447 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1448 handler, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001450 if (conn == null) {
1451 throw new IllegalArgumentException("connection is null");
1452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 if (mPackageInfo != null) {
Adrian Roos691546e2016-02-09 10:13:41 -08001454 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 } else {
1456 throw new RuntimeException("Not supported in system context");
1457 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001458 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001460 IBinder token = getActivityToken();
1461 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1462 && mPackageInfo.getApplicationInfo().targetSdkVersion
1463 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1464 flags |= BIND_WAIVE_PRIORITY;
1465 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001466 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 int res = ActivityManagerNative.getDefault().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001468 mMainThread.getApplicationThread(), getActivityToken(), service,
1469 service.resolveTypeIfNeeded(getContentResolver()),
1470 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 if (res < 0) {
1472 throw new SecurityException(
1473 "Not allowed to bind to service " + service);
1474 }
1475 return res != 0;
1476 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001477 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 }
1479 }
1480
1481 @Override
1482 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001483 if (conn == null) {
1484 throw new IllegalArgumentException("connection is null");
1485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 if (mPackageInfo != null) {
1487 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1488 getOuterContext(), conn);
1489 try {
1490 ActivityManagerNative.getDefault().unbindService(sd);
1491 } catch (RemoteException e) {
1492 }
1493 } else {
1494 throw new RuntimeException("Not supported in system context");
1495 }
1496 }
1497
1498 @Override
1499 public boolean startInstrumentation(ComponentName className,
1500 String profileFile, Bundle arguments) {
1501 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001502 if (arguments != null) {
1503 arguments.setAllowFds(false);
1504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001506 className, profileFile, 0, arguments, null, null, getUserId(),
1507 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001509 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 }
1512
1513 @Override
1514 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001515 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
1517
Jeff Brown6e539312015-02-24 18:53:21 -08001518 @Override
1519 public String getSystemServiceName(Class<?> serviceClass) {
1520 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001521 }
1522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 @Override
1524 public int checkPermission(String permission, int pid, int uid) {
1525 if (permission == null) {
1526 throw new IllegalArgumentException("permission is null");
1527 }
1528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 try {
1530 return ActivityManagerNative.getDefault().checkPermission(
1531 permission, pid, uid);
1532 } catch (RemoteException e) {
1533 return PackageManager.PERMISSION_DENIED;
1534 }
1535 }
1536
Dianne Hackbornff170242014-11-19 10:59:01 -08001537 /** @hide */
1538 @Override
1539 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1540 if (permission == null) {
1541 throw new IllegalArgumentException("permission is null");
1542 }
1543
1544 try {
1545 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1546 permission, pid, uid, callerToken);
1547 } catch (RemoteException e) {
1548 return PackageManager.PERMISSION_DENIED;
1549 }
1550 }
1551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 @Override
1553 public int checkCallingPermission(String permission) {
1554 if (permission == null) {
1555 throw new IllegalArgumentException("permission is null");
1556 }
1557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 int pid = Binder.getCallingPid();
1559 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001560 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
1562 return PackageManager.PERMISSION_DENIED;
1563 }
1564
1565 @Override
1566 public int checkCallingOrSelfPermission(String permission) {
1567 if (permission == null) {
1568 throw new IllegalArgumentException("permission is null");
1569 }
1570
1571 return checkPermission(permission, Binder.getCallingPid(),
1572 Binder.getCallingUid());
1573 }
1574
Svetoslavc6d1c342015-02-26 14:44:43 -08001575 @Override
1576 public int checkSelfPermission(String permission) {
1577 if (permission == null) {
1578 throw new IllegalArgumentException("permission is null");
1579 }
1580
1581 return checkPermission(permission, Process.myPid(), Process.myUid());
1582 }
1583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 private void enforce(
1585 String permission, int resultOfCheck,
1586 boolean selfToo, int uid, String message) {
1587 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1588 throw new SecurityException(
1589 (message != null ? (message + ": ") : "") +
1590 (selfToo
1591 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001592 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 permission +
1594 ".");
1595 }
1596 }
1597
Jeff Brown6e539312015-02-24 18:53:21 -08001598 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 public void enforcePermission(
1600 String permission, int pid, int uid, String message) {
1601 enforce(permission,
1602 checkPermission(permission, pid, uid),
1603 false,
1604 uid,
1605 message);
1606 }
1607
Jeff Brown6e539312015-02-24 18:53:21 -08001608 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 public void enforceCallingPermission(String permission, String message) {
1610 enforce(permission,
1611 checkCallingPermission(permission),
1612 false,
1613 Binder.getCallingUid(),
1614 message);
1615 }
1616
Jeff Brown6e539312015-02-24 18:53:21 -08001617 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 public void enforceCallingOrSelfPermission(
1619 String permission, String message) {
1620 enforce(permission,
1621 checkCallingOrSelfPermission(permission),
1622 true,
1623 Binder.getCallingUid(),
1624 message);
1625 }
1626
1627 @Override
1628 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1629 try {
1630 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001631 mMainThread.getApplicationThread(), toPackage,
1632 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001633 } catch (RemoteException e) {
1634 }
1635 }
1636
1637 @Override
1638 public void revokeUriPermission(Uri uri, int modeFlags) {
1639 try {
1640 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001641 mMainThread.getApplicationThread(),
1642 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 } catch (RemoteException e) {
1644 }
1645 }
1646
1647 @Override
1648 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 try {
1650 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001651 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001652 resolveUserId(uri), null);
1653 } catch (RemoteException e) {
1654 return PackageManager.PERMISSION_DENIED;
1655 }
1656 }
1657
1658 /** @hide */
1659 @Override
1660 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1661 try {
1662 return ActivityManagerNative.getDefault().checkUriPermission(
1663 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1664 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 } catch (RemoteException e) {
1666 return PackageManager.PERMISSION_DENIED;
1667 }
1668 }
1669
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001670 private int resolveUserId(Uri uri) {
1671 return ContentProvider.getUserIdFromUri(uri, getUserId());
1672 }
1673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 @Override
1675 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 int pid = Binder.getCallingPid();
1677 if (pid != Process.myPid()) {
1678 return checkUriPermission(uri, pid,
1679 Binder.getCallingUid(), modeFlags);
1680 }
1681 return PackageManager.PERMISSION_DENIED;
1682 }
1683
1684 @Override
1685 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1686 return checkUriPermission(uri, Binder.getCallingPid(),
1687 Binder.getCallingUid(), modeFlags);
1688 }
1689
1690 @Override
1691 public int checkUriPermission(Uri uri, String readPermission,
1692 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001693 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1695 + readPermission + " writePermission=" + writePermission
1696 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1697 }
1698 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1699 if (readPermission == null
1700 || checkPermission(readPermission, pid, uid)
1701 == PackageManager.PERMISSION_GRANTED) {
1702 return PackageManager.PERMISSION_GRANTED;
1703 }
1704 }
1705 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1706 if (writePermission == null
1707 || checkPermission(writePermission, pid, uid)
1708 == PackageManager.PERMISSION_GRANTED) {
1709 return PackageManager.PERMISSION_GRANTED;
1710 }
1711 }
1712 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1713 : PackageManager.PERMISSION_DENIED;
1714 }
1715
1716 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001717 StringBuilder builder = new StringBuilder();
1718 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1719 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001721 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1722 builder.append("write and ");
1723 }
1724 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1725 builder.append("persistable and ");
1726 }
1727 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1728 builder.append("prefix and ");
1729 }
1730
1731 if (builder.length() > 5) {
1732 builder.setLength(builder.length() - 5);
1733 return builder.toString();
1734 } else {
1735 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 }
1738
1739 private void enforceForUri(
1740 int modeFlags, int resultOfCheck, boolean selfToo,
1741 int uid, Uri uri, String message) {
1742 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1743 throw new SecurityException(
1744 (message != null ? (message + ": ") : "") +
1745 (selfToo
1746 ? "Neither user " + uid + " nor current process has "
1747 : "User " + uid + " does not have ") +
1748 uriModeFlagToString(modeFlags) +
1749 " permission on " +
1750 uri +
1751 ".");
1752 }
1753 }
1754
Jeff Brown6e539312015-02-24 18:53:21 -08001755 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 public void enforceUriPermission(
1757 Uri uri, int pid, int uid, int modeFlags, String message) {
1758 enforceForUri(
1759 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1760 false, uid, uri, message);
1761 }
1762
Jeff Brown6e539312015-02-24 18:53:21 -08001763 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 public void enforceCallingUriPermission(
1765 Uri uri, int modeFlags, String message) {
1766 enforceForUri(
1767 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001768 false,
1769 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
1771
Jeff Brown6e539312015-02-24 18:53:21 -08001772 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 public void enforceCallingOrSelfUriPermission(
1774 Uri uri, int modeFlags, String message) {
1775 enforceForUri(
1776 modeFlags,
1777 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1778 Binder.getCallingUid(), uri, message);
1779 }
1780
Jeff Brown6e539312015-02-24 18:53:21 -08001781 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 public void enforceUriPermission(
1783 Uri uri, String readPermission, String writePermission,
1784 int pid, int uid, int modeFlags, String message) {
1785 enforceForUri(modeFlags,
1786 checkUriPermission(
1787 uri, readPermission, writePermission, pid, uid,
1788 modeFlags),
1789 false,
1790 uid,
1791 uri,
1792 message);
1793 }
1794
Tom O'Neill365632e2013-09-09 09:34:58 -07001795 /**
1796 * Logs a warning if the system process directly called a method such as
1797 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1798 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1799 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001800 private void warnIfCallingFromSystemProcess() {
1801 if (Process.myUid() == Process.SYSTEM_UID) {
1802 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001803 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001804 }
1805 }
1806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001808 public Context createApplicationContext(ApplicationInfo application, int flags)
1809 throws NameNotFoundException {
1810 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1811 flags | CONTEXT_REGISTER_PACKAGE);
1812 if (pi != null) {
Svetoslav976e8bd2014-07-16 15:12:03 -07001813 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001814 new UserHandle(UserHandle.getUserId(application.uid)), flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001815 mDisplay, null, Display.INVALID_DISPLAY);
Svetoslav976e8bd2014-07-16 15:12:03 -07001816 if (c.mResources != null) {
1817 return c;
1818 }
1819 }
1820
1821 throw new PackageManager.NameNotFoundException(
1822 "Application package " + application.packageName + " not found");
1823 }
1824
1825 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001827 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001828 return createPackageContextAsUser(packageName, flags,
1829 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001830 }
1831
1832 @Override
1833 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1834 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00001835 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001836 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001837 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
1839
Jeff Browndefd4a62014-03-10 21:24:37 -07001840 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001841 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 if (pi != null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001843 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001844 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 if (c.mResources != null) {
1846 return c;
1847 }
1848 }
1849
1850 // Should be a better exception.
1851 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001852 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854
Romain Guy870e09f2009-07-06 16:35:25 -07001855 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001856 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001857 if (overrideConfiguration == null) {
1858 throw new IllegalArgumentException("overrideConfiguration must not be null");
1859 }
1860
Jeff Browndefd4a62014-03-10 21:24:37 -07001861 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001862 mUser, mFlags, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001863 }
1864
1865 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001866 public Context createDisplayContext(Display display) {
1867 if (display == null) {
1868 throw new IllegalArgumentException("display must not be null");
1869 }
1870
Jeff Browndefd4a62014-03-10 21:24:37 -07001871 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001872 mUser, mFlags, display, null, Display.INVALID_DISPLAY);
Jeff Browna492c3a2012-08-23 19:48:44 -07001873 }
1874
Jeff Brown6e539312015-02-24 18:53:21 -08001875 Display getDisplay() {
1876 if (mDisplay != null) {
1877 return mDisplay;
1878 }
Wale Ogunwale26698512015-06-05 16:55:33 -07001879 return ResourcesManager.getInstance().getAdjustedDisplay(
1880 Display.DEFAULT_DISPLAY, mDisplayAdjustments);
Jeff Brown6e539312015-02-24 18:53:21 -08001881 }
1882
Jeff Browna492c3a2012-08-23 19:48:44 -07001883 private int getDisplayId() {
1884 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1885 }
1886
1887 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001888 public Context createDeviceEncryptedStorageContext() {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001889 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE)
1890 | Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001891 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1892 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1893 }
1894
1895 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001896 public Context createCredentialEncryptedStorageContext() {
1897 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE)
1898 | Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001899 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1900 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1901 }
1902
1903 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001904 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001905 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
1906 }
1907
1908 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001909 public boolean isDeviceEncryptedStorage() {
1910 return (mFlags & Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001911 }
1912
1913 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001914 public boolean isCredentialEncryptedStorage() {
1915 return (mFlags & Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07001916 }
1917
Jeff Brown98365d72012-08-19 20:30:52 -07001918 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001919 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1920 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -07001921 }
1922
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001923 @Override
1924 public File getDataDir() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001926 File res = null;
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001927 if (isCredentialEncryptedStorage()) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001928 res = mPackageInfo.getCredentialEncryptedDataDirFile();
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001929 } else if (isDeviceEncryptedStorage()) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001930 res = mPackageInfo.getDeviceEncryptedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001931 } else {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001932 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001933 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001934
1935 if (res != null) {
1936 return res;
1937 } else {
1938 throw new RuntimeException(
1939 "No data directory found for package " + getPackageName());
1940 }
1941 } else {
1942 throw new RuntimeException(
1943 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946
1947 @Override
1948 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07001949 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 name = "app_" + name;
Jeff Sharkey2c1ba9a2016-02-17 15:29:38 -07001951 File file = makeFilename(getDataDir(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 if (!file.exists()) {
1953 file.mkdir();
1954 setFilePermissionsFromMode(file.getPath(), mode,
1955 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1956 }
1957 return file;
1958 }
1959
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001960 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08001961 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001962 public int getUserId() {
1963 return mUser.getIdentifier();
1964 }
1965
Dianne Hackborn21556372010-02-04 16:34:40 -08001966 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001967 LoadedApk packageInfo = new LoadedApk(mainThread);
1968 ContextImpl context = new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001969 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001970 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Wale Ogunwale7c726682015-02-06 17:34:28 -08001971 context.mResourcesManager.getDisplayMetricsLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 return context;
1973 }
1974
Jeff Browndefd4a62014-03-10 21:24:37 -07001975 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
1976 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
1977 return new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001978 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001979 }
1980
1981 static ContextImpl createActivityContext(ActivityThread mainThread,
Wale Ogunwale7c726682015-02-06 17:34:28 -08001982 LoadedApk packageInfo, int displayId, Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001983 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001984 return new ContextImpl(null, mainThread, packageInfo, null, null, 0,
Wale Ogunwale26698512015-06-05 16:55:33 -07001985 null, overrideConfiguration, displayId);
Jeff Browndefd4a62014-03-10 21:24:37 -07001986 }
1987
1988 private ContextImpl(ContextImpl container, ActivityThread mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001989 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001990 Display display, Configuration overrideConfiguration, int createDisplayWithId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001993 // If creator didn't specify which storage to use, use the default
1994 // location for application.
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001995 if ((flags & (Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE
1996 | Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE)) == 0) {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001997 final File dataDir = packageInfo.getDataDirFile();
1998 if (Objects.equals(dataDir, packageInfo.getCredentialEncryptedDataDirFile())) {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001999 flags |= Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002000 } else if (Objects.equals(dataDir, packageInfo.getDeviceEncryptedDataDirFile())) {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07002001 flags |= Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002002 }
2003 }
2004
Jeff Browndefd4a62014-03-10 21:24:37 -07002005 mMainThread = mainThread;
2006 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07002007 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008
Jeff Browndefd4a62014-03-10 21:24:37 -07002009 if (user == null) {
2010 user = Process.myUserHandle();
2011 }
2012 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07002013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07002015 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07002016
Wale Ogunwale26698512015-06-05 16:55:33 -07002017 final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
Wale Ogunwalecac3dc62015-06-09 15:35:02 -07002018 ? createDisplayWithId
2019 : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
Wale Ogunwale26698512015-06-05 16:55:33 -07002020
Jeff Browndefd4a62014-03-10 21:24:37 -07002021 CompatibilityInfo compatInfo = null;
2022 if (container != null) {
2023 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
2024 }
Wale Ogunwale9cf99542015-02-18 16:31:34 -08002025 if (compatInfo == null) {
2026 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2027 ? packageInfo.getCompatibilityInfo()
2028 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Jeff Browndefd4a62014-03-10 21:24:37 -07002029 }
2030 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002031 mDisplayAdjustments.setConfiguration(overrideConfiguration);
Jeff Browndefd4a62014-03-10 21:24:37 -07002032
Wale Ogunwale26698512015-06-05 16:55:33 -07002033 mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
2034 : ResourcesManager.getInstance().getAdjustedDisplay(displayId, mDisplayAdjustments);
2035
Jeff Browndefd4a62014-03-10 21:24:37 -07002036 Resources resources = packageInfo.getResources(mainThread);
2037 if (resources != null) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002038 if (displayId != Display.DEFAULT_DISPLAY
Jeff Browndefd4a62014-03-10 21:24:37 -07002039 || overrideConfiguration != null
2040 || (compatInfo != null && compatInfo.applicationScale
2041 != resources.getCompatibilityInfo().applicationScale)) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07002042 resources = mResourcesManager.getTopLevelResources(packageInfo.getResDir(),
2043 packageInfo.getSplitResDirs(), packageInfo.getOverlayDirs(),
2044 packageInfo.getApplicationInfo().sharedLibraryFiles, displayId,
Alan Viverette02fc5fe2015-08-27 13:16:09 -04002045 overrideConfiguration, compatInfo, packageInfo.getClassLoader());
Jeff Browndefd4a62014-03-10 21:24:37 -07002046 }
2047 }
2048 mResources = resources;
2049
2050 if (container != null) {
2051 mBasePackageName = container.mBasePackageName;
2052 mOpPackageName = container.mOpPackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002053 } else {
2054 mBasePackageName = packageInfo.mPackageName;
2055 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2056 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2057 // Special case: system components allow themselves to be loaded in to other
2058 // processes. For purposes of app ops, we must then consider the context as
2059 // belonging to the package of this process, not the system itself, otherwise
2060 // the package+uid verifications in app ops will fail.
2061 mOpPackageName = ActivityThread.currentPackageName();
2062 } else {
2063 mOpPackageName = mBasePackageName;
2064 }
2065 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002066
2067 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
2069
Narayan Kamath29564cd2014-08-07 10:57:40 +01002070 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2071 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 }
2073
2074 final void scheduleFinalCleanup(String who, String what) {
2075 mMainThread.scheduleContextCleanup(this, who, what);
2076 }
2077
2078 final void performFinalCleanup(String who, String what) {
2079 //Log.i(TAG, "Cleanup up context: " + this);
2080 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2081 }
2082
2083 final Context getReceiverRestrictedContext() {
2084 if (mReceiverRestrictedContext != null) {
2085 return mReceiverRestrictedContext;
2086 }
2087 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2088 }
2089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 final void setOuterContext(Context context) {
2091 mOuterContext = context;
2092 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 final Context getOuterContext() {
2095 return mOuterContext;
2096 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 final IBinder getActivityToken() {
2099 return mActivityToken;
2100 }
2101
Jeff Sharkey634dc422016-01-30 17:44:15 -07002102 private void checkMode(int mode) {
2103 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2104 if ((mode & MODE_WORLD_READABLE) != 0) {
2105 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2106 }
2107 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2108 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2109 }
2110 }
2111 }
2112
Jeff Brown6e539312015-02-24 18:53:21 -08002113 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002114 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 int extraPermissions) {
2116 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2117 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2118 |extraPermissions;
2119 if ((mode&MODE_WORLD_READABLE) != 0) {
2120 perms |= FileUtils.S_IROTH;
2121 }
2122 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2123 perms |= FileUtils.S_IWOTH;
2124 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002125 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2127 + ", perms=0x" + Integer.toHexString(perms));
2128 }
2129 FileUtils.setPermissions(name, perms, -1, -1);
2130 }
2131
2132 private File makeFilename(File base, String name) {
2133 if (name.indexOf(File.separatorChar) < 0) {
2134 return new File(base, name);
2135 }
2136 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002137 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 }
2139
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002140 /**
2141 * Ensure that given directories exist, trying to create them if missing. If
2142 * unable to create, they are filtered by replacing with {@code null}.
2143 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002144 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002145 File[] result = new File[dirs.length];
2146 for (int i = 0; i < dirs.length; i++) {
2147 File dir = dirs[i];
2148 if (!dir.exists()) {
2149 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002150 // recheck existence in case of cross-process race
2151 if (!dir.exists()) {
2152 // Failing to mkdir() may be okay, since we might not have
2153 // enough permissions; ask vold to create on our behalf.
2154 final IMountService mount = IMountService.Stub.asInterface(
2155 ServiceManager.getService("mount"));
Christopher Tatecc866da2013-10-02 18:11:01 -07002156 try {
Jeff Sharkey983294592015-07-13 10:25:31 -07002157 final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
2158 if (res != 0) {
2159 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2160 dir = null;
2161 }
2162 } catch (Exception e) {
2163 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002164 dir = null;
2165 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002166 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002167 }
2168 }
2169 result[i] = dir;
2170 }
2171 return result;
2172 }
2173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 // ----------------------------------------------------------------------
2175 // ----------------------------------------------------------------------
2176 // ----------------------------------------------------------------------
2177
2178 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002179 private final ActivityThread mMainThread;
2180 private final UserHandle mUser;
2181
2182 public ApplicationContentResolver(
2183 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002185 mMainThread = Preconditions.checkNotNull(mainThread);
2186 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 }
2188
2189 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002190 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002191 return mMainThread.acquireProvider(context,
2192 ContentProvider.getAuthorityWithoutUserId(auth),
2193 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195
2196 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002197 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002198 return mMainThread.acquireExistingProvider(context,
2199 ContentProvider.getAuthorityWithoutUserId(auth),
2200 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002201 }
2202
2203 @Override
2204 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002205 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002207
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002208 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002209 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002210 return mMainThread.acquireProvider(c,
2211 ContentProvider.getAuthorityWithoutUserId(auth),
2212 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002213 }
2214
2215 @Override
2216 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002217 return mMainThread.releaseProvider(icp, false);
2218 }
2219
2220 @Override
2221 public void unstableProviderDied(IContentProvider icp) {
2222 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002223 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002224
2225 @Override
2226 public void appNotRespondingViaProvider(IContentProvider icp) {
2227 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2228 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002229
2230 /** @hide */
2231 protected int resolveUserIdFromAuthority(String auth) {
2232 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235}