blob: 7f86600338534aeac45b02620c79356d88e91ead [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 Sharkey8fc29cf2015-11-30 17:51:00 -0700137 private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
Jeff Browndefd4a62014-03-10 21:24:37 -0700139 final ActivityThread mMainThread;
140 final LoadedApk mPackageInfo;
141
142 private final IBinder mActivityToken;
143
144 private final UserHandle mUser;
145
146 private final ApplicationContentResolver mContentResolver;
147
148 private final String mBasePackageName;
149 private final String mOpPackageName;
150
151 private final ResourcesManager mResourcesManager;
152 private final Resources mResources;
153 private final Display mDisplay; // may be null if default display
154 private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments();
Jeff Browndefd4a62014-03-10 21:24:37 -0700155
Jeff Sharkey7a30a302015-12-08 14:20:06 -0700156 private final int mFlags;
Jeff Browndefd4a62014-03-10 21:24:37 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 private Context mOuterContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 private int mThemeResource = 0;
160 private Resources.Theme mTheme = null;
161 private PackageManager mPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private Context mReceiverRestrictedContext = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 private final Object mSync = new Object();
165
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700166 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 private File mDatabasesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700168 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private File mPreferencesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700170 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 private File mFilesDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700172 @GuardedBy("mSync")
Christopher Tatea7835b62014-07-11 17:25:57 -0700173 private File mNoBackupFilesDir;
174 @GuardedBy("mSync")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 private File mCacheDir;
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700176 @GuardedBy("mSync")
177 private File mCodeCacheDir;
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700178
179 @GuardedBy("mSync")
180 private File[] mExternalObbDirs;
181 @GuardedBy("mSync")
182 private File[] mExternalFilesDirs;
183 @GuardedBy("mSync")
184 private File[] mExternalCacheDirs;
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700185 @GuardedBy("mSync")
186 private File[] mExternalMediaDirs;
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200187
Jeff Brown6e539312015-02-24 18:53:21 -0800188 // The system service cache for the system services that are cached per-ContextImpl.
189 final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
Brad Fitzpatrick224ba0c2010-11-12 12:22:15 -0800190
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700191 static ContextImpl getImpl(Context context) {
192 Context nextContext;
193 while ((context instanceof ContextWrapper) &&
194 (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
195 context = nextContext;
196 }
197 return (ContextImpl)context;
198 }
199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 @Override
201 public AssetManager getAssets() {
Dianne Hackborn756220b2012-08-14 16:45:30 -0700202 return getResources().getAssets();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
204
205 @Override
206 public Resources getResources() {
207 return mResources;
208 }
209
210 @Override
211 public PackageManager getPackageManager() {
212 if (mPackageManager != null) {
213 return mPackageManager;
214 }
215
216 IPackageManager pm = ActivityThread.getPackageManager();
217 if (pm != null) {
218 // Doesn't matter if we make more than one instance.
219 return (mPackageManager = new ApplicationPackageManager(this, pm));
220 }
221
222 return null;
223 }
224
225 @Override
226 public ContentResolver getContentResolver() {
227 return mContentResolver;
228 }
229
230 @Override
231 public Looper getMainLooper() {
232 return mMainThread.getLooper();
233 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 @Override
236 public Context getApplicationContext() {
Christopher Tateeb9e9ec2010-03-23 17:14:36 -0700237 return (mPackageInfo != null) ?
238 mPackageInfo.getApplication() : mMainThread.getApplication();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 @Override
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700242 public void setTheme(int resId) {
243 if (mThemeResource != resId) {
244 mThemeResource = resId;
245 initializeTheme();
246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 @Override
Dianne Hackborn247fe742011-01-08 17:25:57 -0800250 public int getThemeResId() {
251 return mThemeResource;
252 }
253
254 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 public Resources.Theme getTheme() {
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700256 if (mTheme != null) {
257 return mTheme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 }
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700259
260 mThemeResource = Resources.selectDefaultTheme(mThemeResource,
261 getOuterContext().getApplicationInfo().targetSdkVersion);
262 initializeTheme();
263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 return mTheme;
265 }
266
Alan Viverettecc2a1d42015-05-01 11:28:37 -0700267 private void initializeTheme() {
268 if (mTheme == null) {
269 mTheme = mResources.newTheme();
270 }
271 mTheme.applyStyle(mThemeResource, true);
272 }
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 @Override
275 public ClassLoader getClassLoader() {
276 return mPackageInfo != null ?
277 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
278 }
279
280 @Override
281 public String getPackageName() {
282 if (mPackageInfo != null) {
283 return mPackageInfo.getPackageName();
284 }
Dianne Hackborn35654b62013-01-14 17:38:02 -0800285 // No mPackageInfo means this is a Context for the system itself,
286 // and this here is its name.
287 return "android";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 }
289
Dianne Hackbornd8e1dbb2013-01-17 17:47:37 -0800290 /** @hide */
291 @Override
292 public String getBasePackageName() {
293 return mBasePackageName != null ? mBasePackageName : getPackageName();
294 }
295
Dianne Hackborn95d78532013-09-11 09:51:14 -0700296 /** @hide */
297 @Override
298 public String getOpPackageName() {
299 return mOpPackageName != null ? mOpPackageName : getBasePackageName();
300 }
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 @Override
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700303 public ApplicationInfo getApplicationInfo() {
304 if (mPackageInfo != null) {
305 return mPackageInfo.getApplicationInfo();
306 }
307 throw new RuntimeException("Not supported in system context");
308 }
309
310 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 public String getPackageResourcePath() {
312 if (mPackageInfo != null) {
313 return mPackageInfo.getResDir();
314 }
315 throw new RuntimeException("Not supported in system context");
316 }
317
318 @Override
319 public String getPackageCodePath() {
320 if (mPackageInfo != null) {
321 return mPackageInfo.getAppDir();
322 }
323 throw new RuntimeException("Not supported in system context");
324 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200325
Jeff Brown6e539312015-02-24 18:53:21 -0800326 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 public SharedPreferences getSharedPreferences(String name, int mode) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700328 // At least one application in the world actually passes in a null
329 // name. This happened to work because when we generated the file name
330 // we would stringify it to "null.xml". Nice.
331 if (mPackageInfo.getApplicationInfo().targetSdkVersion <
332 Build.VERSION_CODES.KITKAT) {
333 if (name == null) {
334 name = "null";
335 }
336 }
337
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700338 final File file = getSharedPreferencesPath(name);
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700339 return getSharedPreferences(file, mode);
340 }
341
342 @Override
343 public SharedPreferences getSharedPreferences(File file, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700344 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 SharedPreferencesImpl sp;
Dianne Hackbornf6913592013-09-05 13:21:24 -0700346 synchronized (ContextImpl.class) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700347 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
348 sp = cache.get(file);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700349 if (sp == null) {
Jeff Sharkey8fc29cf2015-11-30 17:51:00 -0700350 sp = new SharedPreferencesImpl(file, mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700351 cache.put(file, sp);
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700352 return sp;
353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 }
Brad Fitzpatrick4e920f72010-12-14 11:52:13 -0800355 if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
356 getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
357 // If somebody else (some other process) changed the prefs
358 // file behind our back, we reload it. This has been the
359 // historical (if undocumented) behavior.
360 sp.startReloadIfChangedUnexpectedly();
361 }
Brad Fitzpatrick6194c532010-09-07 18:00:33 -0700362 return sp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364
Jeff Sharkey35871f22016-01-29 17:13:29 -0700365 private ArrayMap<File, SharedPreferencesImpl> getSharedPreferencesCacheLocked() {
366 if (sSharedPrefs == null) {
367 sSharedPrefs = new ArrayMap<>();
368 }
369
370 final String packageName = getPackageName();
371 ArrayMap<File, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
372 if (packagePrefs == null) {
373 packagePrefs = new ArrayMap<>();
374 sSharedPrefs.put(packageName, packagePrefs);
375 }
376
377 return packagePrefs;
378 }
379
380 /**
381 * Try our best to migrate all files from source to target that match
382 * requested prefix. Return false if we have any trouble migrating.
383 */
384 private static boolean migrateFiles(File sourceDir, File targetDir, final String prefix) {
385 final File[] sourceFiles = FileUtils.listFilesOrEmpty(sourceDir, new FilenameFilter() {
386 @Override
387 public boolean accept(File dir, String name) {
388 return name.startsWith(prefix);
389 }
390 });
391
392 boolean res = true;
393 for (File sourceFile : sourceFiles) {
394 final File targetFile = new File(targetDir, sourceFile.getName());
395 Log.d(TAG, "Migrating " + sourceFile + " to " + targetFile);
396 try {
397 FileUtils.copyFileOrThrow(sourceFile, targetFile);
398 FileUtils.copyPermissions(sourceFile, targetFile);
399 if (!sourceFile.delete()) {
400 throw new IOException("Failed to clean up " + sourceFile);
401 }
402 } catch (IOException e) {
403 Log.w(TAG, "Failed to migrate " + sourceFile + ": " + e);
404 res = false;
405 }
406 }
407 return res;
408 }
409
410 @Override
411 public boolean migrateSharedPreferencesFrom(Context sourceContext, String name) {
412 synchronized (ContextImpl.class) {
413 final File source = sourceContext.getSharedPreferencesPath(name);
414 final File target = getSharedPreferencesPath(name);
415
416 // Evict any in-memory caches for either location
417 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
418 cache.remove(source);
419 cache.remove(target);
420
421 return migrateFiles(source.getParentFile(), target.getParentFile(), source.getName());
422 }
423 }
424
425 @Override
426 public boolean deleteSharedPreferences(String name) {
427 synchronized (ContextImpl.class) {
428 final File prefs = getSharedPreferencesPath(name);
429 final File prefsBackup = SharedPreferencesImpl.makeBackupFile(prefs);
430
431 // Evict any in-memory caches
432 final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked();
433 cache.remove(prefs);
434
435 prefs.delete();
436 prefsBackup.delete();
437
438 // We failed if files are still lingering
439 return !(prefs.exists() || prefsBackup.exists());
440 }
441 }
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 private File getPreferencesDir() {
444 synchronized (mSync) {
445 if (mPreferencesDir == null) {
446 mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
447 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700448 return ensurePrivateDirExists(mPreferencesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 }
450 }
451
452 @Override
453 public FileInputStream openFileInput(String name)
454 throws FileNotFoundException {
455 File f = makeFilename(getFilesDir(), name);
456 return new FileInputStream(f);
457 }
458
459 @Override
Jeff Sharkey634dc422016-01-30 17:44:15 -0700460 public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException {
461 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 final boolean append = (mode&MODE_APPEND) != 0;
463 File f = makeFilename(getFilesDir(), name);
464 try {
465 FileOutputStream fos = new FileOutputStream(f, append);
466 setFilePermissionsFromMode(f.getPath(), mode, 0);
467 return fos;
468 } catch (FileNotFoundException e) {
469 }
470
471 File parent = f.getParentFile();
472 parent.mkdir();
473 FileUtils.setPermissions(
474 parent.getPath(),
475 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
476 -1, -1);
477 FileOutputStream fos = new FileOutputStream(f, append);
478 setFilePermissionsFromMode(f.getPath(), mode, 0);
479 return fos;
480 }
481
482 @Override
483 public boolean deleteFile(String name) {
484 File f = makeFilename(getFilesDir(), name);
485 return f.delete();
486 }
487
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700488 /**
489 * Common-path handling of app data dir creation
490 */
Jeff Sharkey35871f22016-01-29 17:13:29 -0700491 private static File ensurePrivateDirExists(File file) {
Christopher Tatea7835b62014-07-11 17:25:57 -0700492 if (!file.exists()) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700493 try {
494 Os.mkdir(file.getAbsolutePath(), 0771);
Jeff Sharkey46ed6f42016-02-15 14:16:08 -0700495 Os.chmod(file.getAbsolutePath(), 0771);
Jeff Sharkeye84bdd32016-02-08 12:16:00 -0700496 } catch (ErrnoException e) {
497 if (e.errno == OsConstants.EEXIST) {
498 // We must have raced with someone; that's okay
499 } else {
500 Log.w(TAG, "Failed to ensure " + file + ": " + e.getMessage());
Christopher Tatea7835b62014-07-11 17:25:57 -0700501 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700502 }
Christopher Tatea7835b62014-07-11 17:25:57 -0700503 }
504 return file;
505 }
506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 @Override
508 public File getFilesDir() {
509 synchronized (mSync) {
510 if (mFilesDir == null) {
511 mFilesDir = new File(getDataDirFile(), "files");
512 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700513 return ensurePrivateDirExists(mFilesDir);
Christopher Tatea7835b62014-07-11 17:25:57 -0700514 }
515 }
516
517 @Override
518 public File getNoBackupFilesDir() {
519 synchronized (mSync) {
520 if (mNoBackupFilesDir == null) {
521 mNoBackupFilesDir = new File(getDataDirFile(), "no_backup");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700523 return ensurePrivateDirExists(mNoBackupFilesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800528 public File getExternalFilesDir(String type) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700529 // Operates on primary external storage
530 return getExternalFilesDirs(type)[0];
531 }
532
533 @Override
534 public File[] getExternalFilesDirs(String type) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800535 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700536 if (mExternalFilesDirs == null) {
537 mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800538 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700539
540 // Splice in requested type, if any
541 File[] dirs = mExternalFilesDirs;
542 if (type != null) {
543 dirs = Environment.buildPaths(dirs, type);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800544 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700545
546 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700547 return ensureExternalDirsExistOrFilter(dirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800548 }
549 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200550
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800551 @Override
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800552 public File getObbDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700553 // Operates on primary external storage
554 return getObbDirs()[0];
555 }
556
557 @Override
558 public File[] getObbDirs() {
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800559 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700560 if (mExternalObbDirs == null) {
561 mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800562 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -0700563
564 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700565 return ensureExternalDirsExistOrFilter(mExternalObbDirs);
Dianne Hackborn805fd7e2011-01-16 18:30:29 -0800566 }
567 }
568
569 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 public File getCacheDir() {
571 synchronized (mSync) {
572 if (mCacheDir == null) {
573 mCacheDir = new File(getDataDirFile(), "cache");
574 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700575 return ensurePrivateDirExists(mCacheDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200578
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800579 @Override
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700580 public File getCodeCacheDir() {
581 synchronized (mSync) {
582 if (mCodeCacheDir == null) {
583 mCodeCacheDir = new File(getDataDirFile(), "code_cache");
584 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700585 return ensurePrivateDirExists(mCodeCacheDir);
Jeff Sharkey4ed745d2014-07-15 20:39:15 -0700586 }
587 }
588
589 @Override
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800590 public File getExternalCacheDir() {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700591 // Operates on primary external storage
592 return getExternalCacheDirs()[0];
593 }
594
595 @Override
596 public File[] getExternalCacheDirs() {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800597 synchronized (mSync) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700598 if (mExternalCacheDirs == null) {
599 mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800600 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -0700601
602 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700603 return ensureExternalDirsExistOrFilter(mExternalCacheDirs);
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800604 }
605 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 @Override
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700608 public File[] getExternalMediaDirs() {
609 synchronized (mSync) {
610 if (mExternalMediaDirs == null) {
611 mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
612 }
613
614 // Create dirs if needed
Jeff Sharkey35871f22016-01-29 17:13:29 -0700615 return ensureExternalDirsExistOrFilter(mExternalMediaDirs);
Jeff Sharkey2ee3c1e2014-05-30 15:38:35 -0700616 }
617 }
618
619 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 public File getFileStreamPath(String name) {
621 return makeFilename(getFilesDir(), name);
622 }
623
624 @Override
Jeff Sharkey6a6cdaf2015-12-07 19:25:19 -0700625 public File getSharedPreferencesPath(String name) {
626 return makeFilename(getPreferencesDir(), name + ".xml");
627 }
628
629 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 public String[] fileList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700631 return FileUtils.listOrEmpty(getFilesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633
634 @Override
635 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
Jeff Brown47847f32012-03-22 19:13:11 -0700636 return openOrCreateDatabase(name, mode, factory, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638
639 @Override
Vasu Nori74f170f2010-06-01 18:06:18 -0700640 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
641 DatabaseErrorHandler errorHandler) {
Jeff Sharkey634dc422016-01-30 17:44:15 -0700642 checkMode(mode);
Jeff Sharkey35871f22016-01-29 17:13:29 -0700643 File f = getDatabasePath(name);
Jeff Brown47847f32012-03-22 19:13:11 -0700644 int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
645 if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
646 flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
647 }
Sunny Goyala21e6b22015-12-02 09:51:02 -0800648 if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) {
649 flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS;
650 }
Jeff Brown47847f32012-03-22 19:13:11 -0700651 SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
Vasu Nori74f170f2010-06-01 18:06:18 -0700652 setFilePermissionsFromMode(f.getPath(), mode, 0);
653 return db;
654 }
655
656 @Override
Jeff Sharkey35871f22016-01-29 17:13:29 -0700657 public boolean migrateDatabaseFrom(Context sourceContext, String name) {
658 synchronized (ContextImpl.class) {
659 final File source = sourceContext.getDatabasePath(name);
660 final File target = getDatabasePath(name);
661 return migrateFiles(source.getParentFile(), target.getParentFile(), source.getName());
662 }
663 }
664
665 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 public boolean deleteDatabase(String name) {
667 try {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700668 File f = getDatabasePath(name);
Jeff Brown79087e42012-03-01 19:52:44 -0800669 return SQLiteDatabase.deleteDatabase(f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 } catch (Exception e) {
671 }
672 return false;
673 }
674
675 @Override
676 public File getDatabasePath(String name) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700677 File dir;
678 File f;
679
680 if (name.charAt(0) == File.separatorChar) {
681 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
682 dir = new File(dirPath);
683 name = name.substring(name.lastIndexOf(File.separatorChar));
684 f = new File(dir, name);
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700685
686 if (!dir.isDirectory() && dir.mkdir()) {
687 FileUtils.setPermissions(dir.getPath(),
688 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
689 -1, -1);
690 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700691 } else {
692 dir = getDatabasesDir();
693 f = makeFilename(dir, name);
694 }
695
Jeff Sharkey35871f22016-01-29 17:13:29 -0700696 return f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
698
699 @Override
700 public String[] databaseList() {
Jeff Sharkeyc4bab982016-02-01 10:16:01 -0700701 return FileUtils.listOrEmpty(getDatabasesDir());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 private File getDatabasesDir() {
705 synchronized (mSync) {
706 if (mDatabasesDir == null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -0700707 if ("android".equals(getPackageName())) {
708 mDatabasesDir = new File("/data/system");
709 } else {
710 mDatabasesDir = new File(getDataDirFile(), "databases");
711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 }
Jeff Sharkey35871f22016-01-29 17:13:29 -0700713 return ensurePrivateDirExists(mDatabasesDir);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800718 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 public Drawable getWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700720 return getWallpaperManager().getDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722
723 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800724 @Deprecated
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700725 public Drawable peekWallpaper() {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700726 return getWallpaperManager().peekDrawable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
728
729 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800730 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 public int getWallpaperDesiredMinimumWidth() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700732 return getWallpaperManager().getDesiredMinimumWidth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734
735 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800736 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 public int getWallpaperDesiredMinimumHeight() {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700738 return getWallpaperManager().getDesiredMinimumHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740
741 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800742 @Deprecated
743 public void setWallpaper(Bitmap bitmap) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700744 getWallpaperManager().setBitmap(bitmap);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746
747 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800748 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 public void setWallpaper(InputStream data) throws IOException {
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700750 getWallpaperManager().setStream(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 }
752
753 @Override
Jeff Brown6e539312015-02-24 18:53:21 -0800754 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 public void clearWallpaper() throws IOException {
Dianne Hackborn8cc6a502009-08-05 21:29:42 -0700756 getWallpaperManager().clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 }
758
Jeff Brown6e539312015-02-24 18:53:21 -0800759 private WallpaperManager getWallpaperManager() {
760 return getSystemService(WallpaperManager.class);
761 }
762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 @Override
764 public void startActivity(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700765 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700766 startActivity(intent, null);
767 }
768
Amith Yamasani82644082012-08-03 13:09:11 -0700769 /** @hide */
770 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700771 public void startActivityAsUser(Intent intent, UserHandle user) {
Dianne Hackbornf1c26e22012-08-23 13:54:58 -0700772 startActivityAsUser(intent, null, user);
Amith Yamasani82644082012-08-03 13:09:11 -0700773 }
774
Dianne Hackborna4972e92012-03-14 10:38:05 -0700775 @Override
776 public void startActivity(Intent intent, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700777 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
779 throw new AndroidRuntimeException(
780 "Calling startActivity() from outside of an Activity "
781 + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
782 + " Is this really what you want?");
783 }
784 mMainThread.getInstrumentation().execStartActivity(
Dianne Hackborna750a632015-06-16 17:18:23 -0700785 getOuterContext(), mMainThread.getApplicationThread(), null,
786 (Activity) null, intent, -1, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 }
788
Amith Yamasani258848d2012-08-10 17:06:33 -0700789 /** @hide */
790 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700791 public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) {
Amith Yamasani258848d2012-08-10 17:06:33 -0700792 try {
793 ActivityManagerNative.getDefault().startActivityAsUser(
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800794 mMainThread.getApplicationThread(), getBasePackageName(), intent,
Amith Yamasani258848d2012-08-10 17:06:33 -0700795 intent.resolveTypeIfNeeded(getContentResolver()),
Jeff Hao1b012d32014-08-20 10:35:34 -0700796 null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options,
Dianne Hackborn79af1dd2012-08-16 16:42:52 -0700797 user.getIdentifier());
Dianne Hackborne5c42622015-05-19 16:04:04 -0700798 } catch (RemoteException e) {
799 throw new RuntimeException("Failure from system", e);
Amith Yamasani258848d2012-08-10 17:06:33 -0700800 }
801 }
802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 @Override
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800804 public void startActivities(Intent[] intents) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700805 warnIfCallingFromSystemProcess();
Dianne Hackborna4972e92012-03-14 10:38:05 -0700806 startActivities(intents, null);
807 }
808
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700809 /** @hide */
810 @Override
811 public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) {
812 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
813 throw new AndroidRuntimeException(
814 "Calling startActivities() from outside of an Activity "
815 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
816 + " Is this really what you want?");
817 }
818 mMainThread.getInstrumentation().execStartActivitiesAsUser(
Dianne Hackborna750a632015-06-16 17:18:23 -0700819 getOuterContext(), mMainThread.getApplicationThread(), null,
820 (Activity) null, intents, options, userHandle.getIdentifier());
Amith Yamasaniea7e9152012-09-24 16:11:18 -0700821 }
822
Dianne Hackborna4972e92012-03-14 10:38:05 -0700823 @Override
824 public void startActivities(Intent[] intents, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700825 warnIfCallingFromSystemProcess();
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800826 if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
827 throw new AndroidRuntimeException(
828 "Calling startActivities() from outside of an Activity "
829 + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
830 + " Is this really what you want?");
831 }
832 mMainThread.getInstrumentation().execStartActivities(
Dianne Hackborna750a632015-06-16 17:18:23 -0700833 getOuterContext(), mMainThread.getApplicationThread(), null,
834 (Activity) null, intents, options);
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800835 }
836
837 @Override
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700838 public void startIntentSender(IntentSender intent,
839 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
840 throws IntentSender.SendIntentException {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700841 startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
842 }
843
844 @Override
845 public void startIntentSender(IntentSender intent, Intent fillInIntent,
846 int flagsMask, int flagsValues, int extraFlags, Bundle options)
847 throws IntentSender.SendIntentException {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700848 try {
849 String resolvedType = null;
850 if (fillInIntent != null) {
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700851 fillInIntent.migrateExtraStreamToClipData();
Jeff Sharkey344744b2016-01-28 19:03:30 -0700852 fillInIntent.prepareToLeaveProcess(this);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700853 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
854 }
855 int result = ActivityManagerNative.getDefault()
856 .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
857 fillInIntent, resolvedType, null, null,
Dianne Hackborna4972e92012-03-14 10:38:05 -0700858 0, flagsMask, flagsValues, options);
859 if (result == ActivityManager.START_CANCELED) {
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700860 throw new IntentSender.SendIntentException();
861 }
862 Instrumentation.checkStartActivityResult(result, null);
863 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700864 throw new RuntimeException("Failure from system", e);
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700865 }
866 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +0200867
Dianne Hackbornfa82f222009-09-17 15:14:12 -0700868 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 public void sendBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700870 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
872 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700873 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700875 mMainThread.getApplicationThread(), intent, resolvedType, null,
876 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false,
877 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700879 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881 }
882
Amith Yamasani67cf7d32012-02-16 14:31:23 -0800883 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 public void sendBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700885 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700887 String[] receiverPermissions = receiverPermission == null ? null
888 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 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,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700893 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
894 null, false, false, getUserId());
895 } catch (RemoteException e) {
896 throw new RuntimeException("Failure from system", e);
897 }
898 }
899
900 @Override
901 public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions) {
902 warnIfCallingFromSystemProcess();
903 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
904 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700905 intent.prepareToLeaveProcess(this);
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700906 ActivityManagerNative.getDefault().broadcastIntent(
907 mMainThread.getApplicationThread(), intent, resolvedType, null,
908 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700909 null, false, false, getUserId());
910 } catch (RemoteException e) {
911 throw new RuntimeException("Failure from system", e);
912 }
913 }
914
915 @Override
916 public void sendBroadcast(Intent intent, String receiverPermission, Bundle options) {
917 warnIfCallingFromSystemProcess();
918 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700919 String[] receiverPermissions = receiverPermission == null ? null
920 : new String[] {receiverPermission};
Dianne Hackborna750a632015-06-16 17:18:23 -0700921 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700922 intent.prepareToLeaveProcess(this);
Dianne Hackborna750a632015-06-16 17:18:23 -0700923 ActivityManagerNative.getDefault().broadcastIntent(
924 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700925 Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700926 options, false, false, getUserId());
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800927 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700928 throw new RuntimeException("Failure from system", e);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800929 }
930 }
931
932 @Override
933 public void sendBroadcast(Intent intent, String receiverPermission, int appOp) {
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 Hackbornf51f6122013-02-04 18:23:34 -0800938 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700939 intent.prepareToLeaveProcess(this);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800940 ActivityManagerNative.getDefault().broadcastIntent(
Dianne Hackborna750a632015-06-16 17:18:23 -0700941 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700942 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Dianne Hackborna750a632015-06-16 17:18:23 -0700943 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -0700945 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
947 }
948
949 @Override
Dianne Hackborna750a632015-06-16 17:18:23 -0700950 public void sendOrderedBroadcast(Intent intent, String receiverPermission) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700951 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -0700953 String[] receiverPermissions = receiverPermission == null ? null
954 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -0700956 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -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, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700960 null, true, false, 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
967 public void sendOrderedBroadcast(Intent intent,
968 String receiverPermission, BroadcastReceiver resultReceiver,
969 Handler scheduler, int initialCode, String initialData,
970 Bundle initialExtras) {
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800971 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackborna750a632015-06-16 17:18:23 -0700972 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
973 }
974
975 @Override
976 public void sendOrderedBroadcast(Intent intent,
977 String receiverPermission, Bundle options, BroadcastReceiver resultReceiver,
978 Handler scheduler, int initialCode, String initialData,
979 Bundle initialExtras) {
980 sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE,
981 resultReceiver, scheduler, initialCode, initialData, initialExtras, options);
Dianne Hackbornf51f6122013-02-04 18:23:34 -0800982 }
983
984 @Override
985 public void sendOrderedBroadcast(Intent intent,
986 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
987 Handler scheduler, int initialCode, String initialData,
988 Bundle initialExtras) {
Dianne Hackborna750a632015-06-16 17:18:23 -0700989 sendOrderedBroadcast(intent, receiverPermission, appOp,
990 resultReceiver, scheduler, initialCode, initialData, initialExtras, null);
991 }
992
993 void sendOrderedBroadcast(Intent intent,
994 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
995 Handler scheduler, int initialCode, String initialData,
996 Bundle initialExtras, Bundle options) {
Amith Yamasanicd757062012-10-19 18:23:52 -0700997 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 IIntentReceiver rd = null;
999 if (resultReceiver != null) {
1000 if (mPackageInfo != null) {
1001 if (scheduler == null) {
1002 scheduler = mMainThread.getHandler();
1003 }
1004 rd = mPackageInfo.getReceiverDispatcher(
1005 resultReceiver, getOuterContext(), scheduler,
1006 mMainThread.getInstrumentation(), false);
1007 } else {
1008 if (scheduler == null) {
1009 scheduler = mMainThread.getHandler();
1010 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001011 rd = new LoadedApk.ReceiverDispatcher(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1013 }
1014 }
1015 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001016 String[] receiverPermissions = receiverPermission == null ? null
1017 : new String[] {receiverPermission};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001019 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 ActivityManagerNative.getDefault().broadcastIntent(
1021 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001022 initialCode, initialData, initialExtras, receiverPermissions, appOp,
Dianne Hackborna750a632015-06-16 17:18:23 -07001023 options, true, false, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001025 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 }
1028
1029 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001030 public void sendBroadcastAsUser(Intent intent, UserHandle user) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001031 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1032 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001033 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001034 ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
Dianne Hackbornf51f6122013-02-04 18:23:34 -08001035 intent, resolvedType, null, Activity.RESULT_OK, null, null, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001036 AppOpsManager.OP_NONE, null, false, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001037 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001038 throw new RuntimeException("Failure from system", e);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001039 }
1040 }
1041
1042 @Override
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001043 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1044 String receiverPermission) {
Svet Ganov16a16892015-04-16 10:32:04 -07001045 sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE);
1046 }
1047
1048 @Override
1049 public void sendBroadcastAsUser(Intent intent, UserHandle user,
1050 String receiverPermission, int appOp) {
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001051 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001052 String[] receiverPermissions = receiverPermission == null ? null
1053 : new String[] {receiverPermission};
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001054 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001055 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001056 ActivityManagerNative.getDefault().broadcastIntent(
Svet Ganov16a16892015-04-16 10:32:04 -07001057 mMainThread.getApplicationThread(), intent, resolvedType, null,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001058 Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false,
Svet Ganov16a16892015-04-16 10:32:04 -07001059 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001060 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001061 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001062 }
1063 }
1064
1065 @Override
Dianne Hackborn79af1dd2012-08-16 16:42:52 -07001066 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001067 String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler,
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001068 int initialCode, String initialData, Bundle initialExtras) {
Amith Yamasani3cf75722014-05-16 12:37:29 -07001069 sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001070 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
Amith Yamasani3cf75722014-05-16 12:37:29 -07001071 }
1072
1073 @Override
1074 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1075 String receiverPermission, int appOp, BroadcastReceiver resultReceiver,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001076 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
1077 sendOrderedBroadcastAsUser(intent, user, receiverPermission, appOp,
1078 null, resultReceiver, scheduler, initialCode, initialData, initialExtras);
1079 }
1080
1081 @Override
1082 public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user,
1083 String receiverPermission, int appOp, Bundle options, BroadcastReceiver resultReceiver,
1084 Handler scheduler, int initialCode, String initialData, Bundle initialExtras) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001085 IIntentReceiver rd = null;
1086 if (resultReceiver != null) {
1087 if (mPackageInfo != null) {
1088 if (scheduler == null) {
1089 scheduler = mMainThread.getHandler();
1090 }
1091 rd = mPackageInfo.getReceiverDispatcher(
1092 resultReceiver, getOuterContext(), scheduler,
1093 mMainThread.getInstrumentation(), false);
1094 } else {
1095 if (scheduler == null) {
1096 scheduler = mMainThread.getHandler();
1097 }
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001098 rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(),
1099 scheduler, null, false).getIIntentReceiver();
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001100 }
1101 }
1102 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001103 String[] receiverPermissions = receiverPermission == null ? null
1104 : new String[] {receiverPermission};
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001105 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001106 intent.prepareToLeaveProcess(this);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001107 ActivityManagerNative.getDefault().broadcastIntent(
1108 mMainThread.getApplicationThread(), intent, resolvedType, rd,
Fyodor Kupolovd4fd8c72015-07-13 19:19:25 -07001109 initialCode, initialData, initialExtras, receiverPermissions,
Dianne Hackbornfd854ee2015-07-13 18:00:37 -07001110 appOp, options, true, false, user.getIdentifier());
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001111 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001112 throw new RuntimeException("Failure from system", e);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07001113 }
1114 }
1115
1116 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001117 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 public void sendStickyBroadcast(Intent intent) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001119 warnIfCallingFromSystemProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1121 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001122 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 ActivityManagerNative.getDefault().broadcastIntent(
1124 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001125 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001126 getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001128 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 }
1130 }
1131
1132 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001133 @Deprecated
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001134 public void sendStickyOrderedBroadcast(Intent intent,
1135 BroadcastReceiver resultReceiver,
1136 Handler scheduler, int initialCode, String initialData,
1137 Bundle initialExtras) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001138 warnIfCallingFromSystemProcess();
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001139 IIntentReceiver rd = null;
1140 if (resultReceiver != null) {
1141 if (mPackageInfo != null) {
1142 if (scheduler == null) {
1143 scheduler = mMainThread.getHandler();
1144 }
1145 rd = mPackageInfo.getReceiverDispatcher(
1146 resultReceiver, getOuterContext(), scheduler,
1147 mMainThread.getInstrumentation(), false);
1148 } else {
1149 if (scheduler == null) {
1150 scheduler = mMainThread.getHandler();
1151 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001152 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001153 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1154 }
1155 }
1156 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1157 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001158 intent.prepareToLeaveProcess(this);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001159 ActivityManagerNative.getDefault().broadcastIntent(
1160 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1161 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001162 AppOpsManager.OP_NONE, null, true, true, getUserId());
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001163 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001164 throw new RuntimeException("Failure from system", e);
Dianne Hackbornefa199f2009-09-19 12:03:15 -07001165 }
1166 }
1167
1168 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001169 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 public void removeStickyBroadcast(Intent intent) {
1171 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1172 if (resolvedType != null) {
1173 intent = new Intent(intent);
1174 intent.setDataAndType(intent.getData(), resolvedType);
1175 }
1176 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001177 intent.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 ActivityManagerNative.getDefault().unbroadcastIntent(
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001179 mMainThread.getApplicationThread(), intent, getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001181 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183 }
1184
1185 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001186 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001187 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) {
1188 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1189 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001190 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001191 ActivityManagerNative.getDefault().broadcastIntent(
1192 mMainThread.getApplicationThread(), intent, resolvedType, null,
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001193 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true,
1194 user.getIdentifier());
1195 } catch (RemoteException e) {
1196 throw new RuntimeException("Failure from system", e);
1197 }
1198 }
1199
1200 @Override
1201 @Deprecated
1202 public void sendStickyBroadcastAsUser(Intent intent, UserHandle user, Bundle options) {
1203 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1204 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001205 intent.prepareToLeaveProcess(this);
Dianne Hackborne0e413e2015-12-09 17:22:26 -08001206 ActivityManagerNative.getDefault().broadcastIntent(
1207 mMainThread.getApplicationThread(), intent, resolvedType, null,
1208 Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, options, false, true,
1209 user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001210 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001211 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001212 }
1213 }
1214
1215 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001216 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001217 public void sendStickyOrderedBroadcastAsUser(Intent intent,
1218 UserHandle user, BroadcastReceiver resultReceiver,
1219 Handler scheduler, int initialCode, String initialData,
1220 Bundle initialExtras) {
1221 IIntentReceiver rd = null;
1222 if (resultReceiver != null) {
1223 if (mPackageInfo != null) {
1224 if (scheduler == null) {
1225 scheduler = mMainThread.getHandler();
1226 }
1227 rd = mPackageInfo.getReceiverDispatcher(
1228 resultReceiver, getOuterContext(), scheduler,
1229 mMainThread.getInstrumentation(), false);
1230 } else {
1231 if (scheduler == null) {
1232 scheduler = mMainThread.getHandler();
1233 }
1234 rd = new LoadedApk.ReceiverDispatcher(
1235 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1236 }
1237 }
1238 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1239 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001240 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001241 ActivityManagerNative.getDefault().broadcastIntent(
1242 mMainThread.getApplicationThread(), intent, resolvedType, rd,
1243 initialCode, initialData, initialExtras, null,
Dianne Hackborna750a632015-06-16 17:18:23 -07001244 AppOpsManager.OP_NONE, null, true, true, user.getIdentifier());
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001245 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001246 throw new RuntimeException("Failure from system", e);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001247 }
1248 }
1249
1250 @Override
Jeff Brown6e539312015-02-24 18:53:21 -08001251 @Deprecated
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001252 public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) {
1253 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1254 if (resolvedType != null) {
1255 intent = new Intent(intent);
1256 intent.setDataAndType(intent.getData(), resolvedType);
1257 }
1258 try {
Jeff Sharkey344744b2016-01-28 19:03:30 -07001259 intent.prepareToLeaveProcess(this);
Dianne Hackborn5ac72a22012-08-29 18:32:08 -07001260 ActivityManagerNative.getDefault().unbroadcastIntent(
1261 mMainThread.getApplicationThread(), intent, user.getIdentifier());
1262 } 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1269 return registerReceiver(receiver, filter, null, null);
1270 }
1271
1272 @Override
1273 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1274 String broadcastPermission, Handler scheduler) {
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001275 return registerReceiverInternal(receiver, getUserId(),
Dianne Hackborn20e80982012-08-31 19:00:44 -07001276 filter, broadcastPermission, scheduler, getOuterContext());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 }
1278
Dianne Hackborn20e80982012-08-31 19:00:44 -07001279 @Override
1280 public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
1281 IntentFilter filter, String broadcastPermission, Handler scheduler) {
1282 return registerReceiverInternal(receiver, user.getIdentifier(),
1283 filter, broadcastPermission, scheduler, getOuterContext());
1284 }
1285
1286 private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 IntentFilter filter, String broadcastPermission,
1288 Handler scheduler, Context context) {
1289 IIntentReceiver rd = null;
1290 if (receiver != null) {
1291 if (mPackageInfo != null && context != null) {
1292 if (scheduler == null) {
1293 scheduler = mMainThread.getHandler();
1294 }
1295 rd = mPackageInfo.getReceiverDispatcher(
1296 receiver, context, scheduler,
1297 mMainThread.getInstrumentation(), true);
1298 } else {
1299 if (scheduler == null) {
1300 scheduler = mMainThread.getHandler();
1301 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001302 rd = new LoadedApk.ReceiverDispatcher(
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001303 receiver, context, scheduler, null, true).getIIntentReceiver();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 }
1305 }
1306 try {
1307 return ActivityManagerNative.getDefault().registerReceiver(
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001308 mMainThread.getApplicationThread(), mBasePackageName,
Dianne Hackborn20e80982012-08-31 19:00:44 -07001309 rd, filter, broadcastPermission, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 } catch (RemoteException e) {
1311 return null;
1312 }
1313 }
1314
1315 @Override
1316 public void unregisterReceiver(BroadcastReceiver receiver) {
1317 if (mPackageInfo != null) {
1318 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1319 getOuterContext(), receiver);
1320 try {
1321 ActivityManagerNative.getDefault().unregisterReceiver(rd);
1322 } catch (RemoteException e) {
1323 }
1324 } else {
1325 throw new RuntimeException("Not supported in system context");
1326 }
1327 }
1328
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001329 private void validateServiceIntent(Intent service) {
1330 if (service.getComponent() == null && service.getPackage() == null) {
Dianne Hackborn955d8d62014-10-07 20:17:19 -07001331 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001332 IllegalArgumentException ex = new IllegalArgumentException(
1333 "Service Intent must be explicit: " + service);
1334 throw ex;
1335 } else {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001336 Log.w(TAG, "Implicit intents with startService are not safe: " + service
1337 + " " + Debug.getCallers(2, 3));
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001338 }
1339 }
1340 }
1341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 @Override
1343 public ComponentName startService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001344 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001345 return startServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001346 }
1347
1348 @Override
1349 public boolean stopService(Intent service) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001350 warnIfCallingFromSystemProcess();
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001351 return stopServiceCommon(service, mUser);
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001352 }
1353
1354 @Override
1355 public ComponentName startServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001356 return startServiceCommon(service, user);
1357 }
1358
1359 private ComponentName startServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001361 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001362 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 ComponentName cn = ActivityManagerNative.getDefault().startService(
Svet Ganov99b60432015-06-27 13:15:22 -07001364 mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
1365 getContentResolver()), getOpPackageName(), user.getIdentifier());
Dianne Hackbornc0bd7472012-10-09 14:00:30 -07001366 if (cn != null) {
1367 if (cn.getPackageName().equals("!")) {
1368 throw new SecurityException(
1369 "Not allowed to start service " + service
1370 + " without permission " + cn.getClassName());
1371 } else if (cn.getPackageName().equals("!!")) {
1372 throw new SecurityException(
1373 "Unable to start service " + service
1374 + ": " + cn.getClassName());
1375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
1377 return cn;
1378 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001379 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
1381 }
1382
1383 @Override
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001384 public boolean stopServiceAsUser(Intent service, UserHandle user) {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001385 return stopServiceCommon(service, user);
1386 }
1387
1388 private boolean stopServiceCommon(Intent service, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 try {
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001390 validateServiceIntent(service);
Jeff Sharkey344744b2016-01-28 19:03:30 -07001391 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 int res = ActivityManagerNative.getDefault().stopService(
1393 mMainThread.getApplicationThread(), service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001394 service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 if (res < 0) {
1396 throw new SecurityException(
1397 "Not allowed to stop service " + service);
1398 }
1399 return res != 0;
1400 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001401 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403 }
1404
1405 @Override
1406 public boolean bindService(Intent service, ServiceConnection conn,
1407 int flags) {
Amith Yamasanicd757062012-10-19 18:23:52 -07001408 warnIfCallingFromSystemProcess();
Adrian Roos691546e2016-02-09 10:13:41 -08001409 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(),
1410 Process.myUserHandle());
Amith Yamasani37ce3a82012-02-06 12:04:42 -08001411 }
1412
1413 /** @hide */
1414 @Override
Amith Yamasani27b89e62013-01-16 12:30:11 -08001415 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1416 UserHandle user) {
Adrian Roos691546e2016-02-09 10:13:41 -08001417 return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user);
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001418 }
1419
Adrian Roos691546e2016-02-09 10:13:41 -08001420 /** @hide */
1421 @Override
1422 public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags,
1423 Handler handler, UserHandle user) {
1424 if (handler == null) {
1425 throw new IllegalArgumentException("handler must not be null.");
1426 }
1427 return bindServiceCommon(service, conn, flags, handler, user);
1428 }
1429
1430 private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
1431 handler, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 IServiceConnection sd;
Christopher Tate79b33172012-06-18 14:54:21 -07001433 if (conn == null) {
1434 throw new IllegalArgumentException("connection is null");
1435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 if (mPackageInfo != null) {
Adrian Roos691546e2016-02-09 10:13:41 -08001437 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 } else {
1439 throw new RuntimeException("Not supported in system context");
1440 }
Dianne Hackbornfd6c7b12013-10-03 10:42:26 -07001441 validateServiceIntent(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07001443 IBinder token = getActivityToken();
1444 if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1445 && mPackageInfo.getApplicationInfo().targetSdkVersion
1446 < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1447 flags |= BIND_WAIVE_PRIORITY;
1448 }
Jeff Sharkey344744b2016-01-28 19:03:30 -07001449 service.prepareToLeaveProcess(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 int res = ActivityManagerNative.getDefault().bindService(
Svet Ganov99b60432015-06-27 13:15:22 -07001451 mMainThread.getApplicationThread(), getActivityToken(), service,
1452 service.resolveTypeIfNeeded(getContentResolver()),
1453 sd, flags, getOpPackageName(), user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 if (res < 0) {
1455 throw new SecurityException(
1456 "Not allowed to bind to service " + service);
1457 }
1458 return res != 0;
1459 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001460 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 }
1462 }
1463
1464 @Override
1465 public void unbindService(ServiceConnection conn) {
Christopher Tate79b33172012-06-18 14:54:21 -07001466 if (conn == null) {
1467 throw new IllegalArgumentException("connection is null");
1468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 if (mPackageInfo != null) {
1470 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1471 getOuterContext(), conn);
1472 try {
1473 ActivityManagerNative.getDefault().unbindService(sd);
1474 } catch (RemoteException e) {
1475 }
1476 } else {
1477 throw new RuntimeException("Not supported in system context");
1478 }
1479 }
1480
1481 @Override
1482 public boolean startInstrumentation(ComponentName className,
1483 String profileFile, Bundle arguments) {
1484 try {
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04001485 if (arguments != null) {
1486 arguments.setAllowFds(false);
1487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 return ActivityManagerNative.getDefault().startInstrumentation(
Narayan Kamath8dcfefd2014-05-15 18:12:59 +01001489 className, profileFile, 0, arguments, null, null, getUserId(),
1490 null /* ABI override */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 } catch (RemoteException e) {
Dianne Hackborne5c42622015-05-19 16:04:04 -07001492 throw new RuntimeException("Failure from system", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 }
1495
1496 @Override
1497 public Object getSystemService(String name) {
Jeff Brown6e539312015-02-24 18:53:21 -08001498 return SystemServiceRegistry.getSystemService(this, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 }
1500
Jeff Brown6e539312015-02-24 18:53:21 -08001501 @Override
1502 public String getSystemServiceName(Class<?> serviceClass) {
1503 return SystemServiceRegistry.getSystemServiceName(serviceClass);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001504 }
1505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 @Override
1507 public int checkPermission(String permission, int pid, int uid) {
1508 if (permission == null) {
1509 throw new IllegalArgumentException("permission is null");
1510 }
1511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 try {
1513 return ActivityManagerNative.getDefault().checkPermission(
1514 permission, pid, uid);
1515 } catch (RemoteException e) {
1516 return PackageManager.PERMISSION_DENIED;
1517 }
1518 }
1519
Dianne Hackbornff170242014-11-19 10:59:01 -08001520 /** @hide */
1521 @Override
1522 public int checkPermission(String permission, int pid, int uid, IBinder callerToken) {
1523 if (permission == null) {
1524 throw new IllegalArgumentException("permission is null");
1525 }
1526
1527 try {
1528 return ActivityManagerNative.getDefault().checkPermissionWithToken(
1529 permission, pid, uid, callerToken);
1530 } catch (RemoteException e) {
1531 return PackageManager.PERMISSION_DENIED;
1532 }
1533 }
1534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 @Override
1536 public int checkCallingPermission(String permission) {
1537 if (permission == null) {
1538 throw new IllegalArgumentException("permission is null");
1539 }
1540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 int pid = Binder.getCallingPid();
1542 if (pid != Process.myPid()) {
Amith Yamasani742a6712011-05-04 14:49:28 -07001543 return checkPermission(permission, pid, Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545 return PackageManager.PERMISSION_DENIED;
1546 }
1547
1548 @Override
1549 public int checkCallingOrSelfPermission(String permission) {
1550 if (permission == null) {
1551 throw new IllegalArgumentException("permission is null");
1552 }
1553
1554 return checkPermission(permission, Binder.getCallingPid(),
1555 Binder.getCallingUid());
1556 }
1557
Svetoslavc6d1c342015-02-26 14:44:43 -08001558 @Override
1559 public int checkSelfPermission(String permission) {
1560 if (permission == null) {
1561 throw new IllegalArgumentException("permission is null");
1562 }
1563
1564 return checkPermission(permission, Process.myPid(), Process.myUid());
1565 }
1566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 private void enforce(
1568 String permission, int resultOfCheck,
1569 boolean selfToo, int uid, String message) {
1570 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1571 throw new SecurityException(
1572 (message != null ? (message + ": ") : "") +
1573 (selfToo
1574 ? "Neither user " + uid + " nor current process has "
Christopher Tate4dc7a682012-09-11 12:15:49 -07001575 : "uid " + uid + " does not have ") +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 permission +
1577 ".");
1578 }
1579 }
1580
Jeff Brown6e539312015-02-24 18:53:21 -08001581 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 public void enforcePermission(
1583 String permission, int pid, int uid, String message) {
1584 enforce(permission,
1585 checkPermission(permission, pid, uid),
1586 false,
1587 uid,
1588 message);
1589 }
1590
Jeff Brown6e539312015-02-24 18:53:21 -08001591 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 public void enforceCallingPermission(String permission, String message) {
1593 enforce(permission,
1594 checkCallingPermission(permission),
1595 false,
1596 Binder.getCallingUid(),
1597 message);
1598 }
1599
Jeff Brown6e539312015-02-24 18:53:21 -08001600 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 public void enforceCallingOrSelfPermission(
1602 String permission, String message) {
1603 enforce(permission,
1604 checkCallingOrSelfPermission(permission),
1605 true,
1606 Binder.getCallingUid(),
1607 message);
1608 }
1609
1610 @Override
1611 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1612 try {
1613 ActivityManagerNative.getDefault().grantUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001614 mMainThread.getApplicationThread(), toPackage,
1615 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 } catch (RemoteException e) {
1617 }
1618 }
1619
1620 @Override
1621 public void revokeUriPermission(Uri uri, int modeFlags) {
1622 try {
1623 ActivityManagerNative.getDefault().revokeUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001624 mMainThread.getApplicationThread(),
1625 ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 } catch (RemoteException e) {
1627 }
1628 }
1629
1630 @Override
1631 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 try {
1633 return ActivityManagerNative.getDefault().checkUriPermission(
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001634 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
Dianne Hackbornff170242014-11-19 10:59:01 -08001635 resolveUserId(uri), null);
1636 } catch (RemoteException e) {
1637 return PackageManager.PERMISSION_DENIED;
1638 }
1639 }
1640
1641 /** @hide */
1642 @Override
1643 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) {
1644 try {
1645 return ActivityManagerNative.getDefault().checkUriPermission(
1646 ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags,
1647 resolveUserId(uri), callerToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 } catch (RemoteException e) {
1649 return PackageManager.PERMISSION_DENIED;
1650 }
1651 }
1652
Nicolas Prevotd85fc722014-04-16 19:52:08 +01001653 private int resolveUserId(Uri uri) {
1654 return ContentProvider.getUserIdFromUri(uri, getUserId());
1655 }
1656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 @Override
1658 public int checkCallingUriPermission(Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 int pid = Binder.getCallingPid();
1660 if (pid != Process.myPid()) {
1661 return checkUriPermission(uri, pid,
1662 Binder.getCallingUid(), modeFlags);
1663 }
1664 return PackageManager.PERMISSION_DENIED;
1665 }
1666
1667 @Override
1668 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1669 return checkUriPermission(uri, Binder.getCallingPid(),
1670 Binder.getCallingUid(), modeFlags);
1671 }
1672
1673 @Override
1674 public int checkUriPermission(Uri uri, String readPermission,
1675 String writePermission, int pid, int uid, int modeFlags) {
Mitsuru Oshima569076c2009-07-02 20:06:08 -07001676 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1678 + readPermission + " writePermission=" + writePermission
1679 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1680 }
1681 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1682 if (readPermission == null
1683 || checkPermission(readPermission, pid, uid)
1684 == PackageManager.PERMISSION_GRANTED) {
1685 return PackageManager.PERMISSION_GRANTED;
1686 }
1687 }
1688 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1689 if (writePermission == null
1690 || checkPermission(writePermission, pid, uid)
1691 == PackageManager.PERMISSION_GRANTED) {
1692 return PackageManager.PERMISSION_GRANTED;
1693 }
1694 }
1695 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1696 : PackageManager.PERMISSION_DENIED;
1697 }
1698
1699 private String uriModeFlagToString(int uriModeFlags) {
Jeff Sharkey846318a2014-04-04 12:12:41 -07001700 StringBuilder builder = new StringBuilder();
1701 if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1702 builder.append("read and ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 }
Jeff Sharkey846318a2014-04-04 12:12:41 -07001704 if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1705 builder.append("write and ");
1706 }
1707 if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) {
1708 builder.append("persistable and ");
1709 }
1710 if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) {
1711 builder.append("prefix and ");
1712 }
1713
1714 if (builder.length() > 5) {
1715 builder.setLength(builder.length() - 5);
1716 return builder.toString();
1717 } else {
1718 throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags);
1719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
1721
1722 private void enforceForUri(
1723 int modeFlags, int resultOfCheck, boolean selfToo,
1724 int uid, Uri uri, String message) {
1725 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1726 throw new SecurityException(
1727 (message != null ? (message + ": ") : "") +
1728 (selfToo
1729 ? "Neither user " + uid + " nor current process has "
1730 : "User " + uid + " does not have ") +
1731 uriModeFlagToString(modeFlags) +
1732 " permission on " +
1733 uri +
1734 ".");
1735 }
1736 }
1737
Jeff Brown6e539312015-02-24 18:53:21 -08001738 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 public void enforceUriPermission(
1740 Uri uri, int pid, int uid, int modeFlags, String message) {
1741 enforceForUri(
1742 modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1743 false, uid, uri, message);
1744 }
1745
Jeff Brown6e539312015-02-24 18:53:21 -08001746 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 public void enforceCallingUriPermission(
1748 Uri uri, int modeFlags, String message) {
1749 enforceForUri(
1750 modeFlags, checkCallingUriPermission(uri, modeFlags),
Amith Yamasani742a6712011-05-04 14:49:28 -07001751 false,
1752 Binder.getCallingUid(), uri, message);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
1754
Jeff Brown6e539312015-02-24 18:53:21 -08001755 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 public void enforceCallingOrSelfUriPermission(
1757 Uri uri, int modeFlags, String message) {
1758 enforceForUri(
1759 modeFlags,
1760 checkCallingOrSelfUriPermission(uri, modeFlags), true,
1761 Binder.getCallingUid(), uri, message);
1762 }
1763
Jeff Brown6e539312015-02-24 18:53:21 -08001764 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 public void enforceUriPermission(
1766 Uri uri, String readPermission, String writePermission,
1767 int pid, int uid, int modeFlags, String message) {
1768 enforceForUri(modeFlags,
1769 checkUriPermission(
1770 uri, readPermission, writePermission, pid, uid,
1771 modeFlags),
1772 false,
1773 uid,
1774 uri,
1775 message);
1776 }
1777
Tom O'Neill365632e2013-09-09 09:34:58 -07001778 /**
1779 * Logs a warning if the system process directly called a method such as
1780 * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}.
1781 * The "AsUser" variants allow us to properly enforce the user's restrictions.
1782 */
Amith Yamasanicd757062012-10-19 18:23:52 -07001783 private void warnIfCallingFromSystemProcess() {
1784 if (Process.myUid() == Process.SYSTEM_UID) {
1785 Slog.w(TAG, "Calling a method in the system process without a qualified user: "
Dianne Hackborn40e9f292012-11-27 19:12:23 -08001786 + Debug.getCallers(5));
Amith Yamasanicd757062012-10-19 18:23:52 -07001787 }
1788 }
1789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 @Override
Svetoslav976e8bd2014-07-16 15:12:03 -07001791 public Context createApplicationContext(ApplicationInfo application, int flags)
1792 throws NameNotFoundException {
1793 LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(),
1794 flags | CONTEXT_REGISTER_PACKAGE);
1795 if (pi != null) {
Svetoslav976e8bd2014-07-16 15:12:03 -07001796 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001797 new UserHandle(UserHandle.getUserId(application.uid)), flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001798 mDisplay, null, Display.INVALID_DISPLAY);
Svetoslav976e8bd2014-07-16 15:12:03 -07001799 if (c.mResources != null) {
1800 return c;
1801 }
1802 }
1803
1804 throw new PackageManager.NameNotFoundException(
1805 "Application package " + application.packageName + " not found");
1806 }
1807
1808 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 public Context createPackageContext(String packageName, int flags)
Jeff Sharkey6d515712012-09-20 16:06:08 -07001810 throws NameNotFoundException {
Amith Yamasani64442c12012-10-07 08:17:46 -07001811 return createPackageContextAsUser(packageName, flags,
1812 mUser != null ? mUser : Process.myUserHandle());
Jeff Sharkey6d515712012-09-20 16:06:08 -07001813 }
1814
1815 @Override
1816 public Context createPackageContextAsUser(String packageName, int flags, UserHandle user)
1817 throws NameNotFoundException {
Kenny Guyc2a40602014-09-08 18:51:15 +00001818 if (packageName.equals("system") || packageName.equals("android")) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001819 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001820 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
1822
Jeff Browndefd4a62014-03-10 21:24:37 -07001823 LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(),
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001824 flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 if (pi != null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001826 ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001827 user, flags, mDisplay, null, Display.INVALID_DISPLAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 if (c.mResources != null) {
1829 return c;
1830 }
1831 }
1832
1833 // Should be a better exception.
1834 throw new PackageManager.NameNotFoundException(
Jeff Browndefd4a62014-03-10 21:24:37 -07001835 "Application package " + packageName + " not found");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
1837
Romain Guy870e09f2009-07-06 16:35:25 -07001838 @Override
Dianne Hackborn756220b2012-08-14 16:45:30 -07001839 public Context createConfigurationContext(Configuration overrideConfiguration) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001840 if (overrideConfiguration == null) {
1841 throw new IllegalArgumentException("overrideConfiguration must not be null");
1842 }
1843
Jeff Browndefd4a62014-03-10 21:24:37 -07001844 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001845 mUser, mFlags, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001846 }
1847
1848 @Override
Jeff Browna492c3a2012-08-23 19:48:44 -07001849 public Context createDisplayContext(Display display) {
1850 if (display == null) {
1851 throw new IllegalArgumentException("display must not be null");
1852 }
1853
Jeff Browndefd4a62014-03-10 21:24:37 -07001854 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001855 mUser, mFlags, display, null, Display.INVALID_DISPLAY);
Jeff Browna492c3a2012-08-23 19:48:44 -07001856 }
1857
Jeff Brown6e539312015-02-24 18:53:21 -08001858 Display getDisplay() {
1859 if (mDisplay != null) {
1860 return mDisplay;
1861 }
Wale Ogunwale26698512015-06-05 16:55:33 -07001862 return ResourcesManager.getInstance().getAdjustedDisplay(
1863 Display.DEFAULT_DISPLAY, mDisplayAdjustments);
Jeff Brown6e539312015-02-24 18:53:21 -08001864 }
1865
Jeff Browna492c3a2012-08-23 19:48:44 -07001866 private int getDisplayId() {
1867 return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY;
1868 }
1869
1870 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001871 public Context createDeviceEncryptedStorageContext() {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001872 final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE)
1873 | Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001874 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1875 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1876 }
1877
1878 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001879 public Context createCredentialEncryptedStorageContext() {
1880 final int flags = (mFlags & ~Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE)
1881 | Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001882 return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken,
1883 mUser, flags, mDisplay, null, Display.INVALID_DISPLAY);
1884 }
1885
1886 @Override
Romain Guy870e09f2009-07-06 16:35:25 -07001887 public boolean isRestricted() {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001888 return (mFlags & Context.CONTEXT_RESTRICTED) != 0;
1889 }
1890
1891 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001892 public boolean isDeviceEncryptedStorage() {
1893 return (mFlags & Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE) != 0;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001894 }
1895
1896 @Override
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001897 public boolean isCredentialEncryptedStorage() {
1898 return (mFlags & Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE) != 0;
Romain Guy870e09f2009-07-06 16:35:25 -07001899 }
1900
Jeff Brown98365d72012-08-19 20:30:52 -07001901 @Override
Craig Mautner48d0d182013-06-11 07:53:06 -07001902 public DisplayAdjustments getDisplayAdjustments(int displayId) {
1903 return mDisplayAdjustments;
Jeff Brown98365d72012-08-19 20:30:52 -07001904 }
1905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 private File getDataDirFile() {
1907 if (mPackageInfo != null) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001908 File res = null;
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001909 if (isCredentialEncryptedStorage()) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001910 res = mPackageInfo.getCredentialEncryptedDataDirFile();
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001911 } else if (isDeviceEncryptedStorage()) {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001912 res = mPackageInfo.getDeviceEncryptedDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001913 } else {
Jeff Sharkey35871f22016-01-29 17:13:29 -07001914 res = mPackageInfo.getDataDirFile();
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001915 }
Jeff Sharkey35871f22016-01-29 17:13:29 -07001916
1917 if (res != null) {
1918 return res;
1919 } else {
1920 throw new RuntimeException(
1921 "No data directory found for package " + getPackageName());
1922 }
1923 } else {
1924 throw new RuntimeException(
1925 "No package details found for package " + getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928
1929 @Override
1930 public File getDir(String name, int mode) {
Jeff Sharkey634dc422016-01-30 17:44:15 -07001931 checkMode(mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 name = "app_" + name;
1933 File file = makeFilename(getDataDirFile(), name);
1934 if (!file.exists()) {
1935 file.mkdir();
1936 setFilePermissionsFromMode(file.getPath(), mode,
1937 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1938 }
1939 return file;
1940 }
1941
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001942 /** {@hide} */
Jeff Brown6e539312015-02-24 18:53:21 -08001943 @Override
Jeff Sharkeyded653b2012-09-27 19:09:24 -07001944 public int getUserId() {
1945 return mUser.getIdentifier();
1946 }
1947
Dianne Hackborn21556372010-02-04 16:34:40 -08001948 static ContextImpl createSystemContext(ActivityThread mainThread) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001949 LoadedApk packageInfo = new LoadedApk(mainThread);
1950 ContextImpl context = new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001951 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001952 context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
Wale Ogunwale7c726682015-02-06 17:34:28 -08001953 context.mResourcesManager.getDisplayMetricsLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 return context;
1955 }
1956
Jeff Browndefd4a62014-03-10 21:24:37 -07001957 static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) {
1958 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
1959 return new ContextImpl(null, mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001960 packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY);
Jeff Browndefd4a62014-03-10 21:24:37 -07001961 }
1962
1963 static ContextImpl createActivityContext(ActivityThread mainThread,
Wale Ogunwale7c726682015-02-06 17:34:28 -08001964 LoadedApk packageInfo, int displayId, Configuration overrideConfiguration) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001965 if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001966 return new ContextImpl(null, mainThread, packageInfo, null, null, 0,
Wale Ogunwale26698512015-06-05 16:55:33 -07001967 null, overrideConfiguration, displayId);
Jeff Browndefd4a62014-03-10 21:24:37 -07001968 }
1969
1970 private ContextImpl(ContextImpl container, ActivityThread mainThread,
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001971 LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags,
Wale Ogunwale26698512015-06-05 16:55:33 -07001972 Display display, Configuration overrideConfiguration, int createDisplayWithId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 mOuterContext = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001975 // If creator didn't specify which storage to use, use the default
1976 // location for application.
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001977 if ((flags & (Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE
1978 | Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE)) == 0) {
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001979 final File dataDir = packageInfo.getDataDirFile();
1980 if (Objects.equals(dataDir, packageInfo.getCredentialEncryptedDataDirFile())) {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001981 flags |= Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001982 } else if (Objects.equals(dataDir, packageInfo.getDeviceEncryptedDataDirFile())) {
Jeff Sharkeye13529a2015-12-09 14:15:27 -07001983 flags |= Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001984 }
1985 }
1986
Jeff Browndefd4a62014-03-10 21:24:37 -07001987 mMainThread = mainThread;
1988 mActivityToken = activityToken;
Jeff Sharkey7a30a302015-12-08 14:20:06 -07001989 mFlags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990
Jeff Browndefd4a62014-03-10 21:24:37 -07001991 if (user == null) {
1992 user = Process.myUserHandle();
1993 }
1994 mUser = user;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 mPackageInfo = packageInfo;
Jeff Browndefd4a62014-03-10 21:24:37 -07001997 mResourcesManager = ResourcesManager.getInstance();
Jeff Browndefd4a62014-03-10 21:24:37 -07001998
Wale Ogunwale26698512015-06-05 16:55:33 -07001999 final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY)
Wale Ogunwalecac3dc62015-06-09 15:35:02 -07002000 ? createDisplayWithId
2001 : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY;
Wale Ogunwale26698512015-06-05 16:55:33 -07002002
Jeff Browndefd4a62014-03-10 21:24:37 -07002003 CompatibilityInfo compatInfo = null;
2004 if (container != null) {
2005 compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo();
2006 }
Wale Ogunwale9cf99542015-02-18 16:31:34 -08002007 if (compatInfo == null) {
2008 compatInfo = (displayId == Display.DEFAULT_DISPLAY)
2009 ? packageInfo.getCompatibilityInfo()
2010 : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Jeff Browndefd4a62014-03-10 21:24:37 -07002011 }
2012 mDisplayAdjustments.setCompatibilityInfo(compatInfo);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002013 mDisplayAdjustments.setConfiguration(overrideConfiguration);
Jeff Browndefd4a62014-03-10 21:24:37 -07002014
Wale Ogunwale26698512015-06-05 16:55:33 -07002015 mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display
2016 : ResourcesManager.getInstance().getAdjustedDisplay(displayId, mDisplayAdjustments);
2017
Jeff Browndefd4a62014-03-10 21:24:37 -07002018 Resources resources = packageInfo.getResources(mainThread);
2019 if (resources != null) {
Wale Ogunwale60454db2015-01-23 16:05:07 -08002020 if (displayId != Display.DEFAULT_DISPLAY
Jeff Browndefd4a62014-03-10 21:24:37 -07002021 || overrideConfiguration != null
2022 || (compatInfo != null && compatInfo.applicationScale
2023 != resources.getCompatibilityInfo().applicationScale)) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07002024 resources = mResourcesManager.getTopLevelResources(packageInfo.getResDir(),
2025 packageInfo.getSplitResDirs(), packageInfo.getOverlayDirs(),
2026 packageInfo.getApplicationInfo().sharedLibraryFiles, displayId,
Alan Viverette02fc5fe2015-08-27 13:16:09 -04002027 overrideConfiguration, compatInfo, packageInfo.getClassLoader());
Jeff Browndefd4a62014-03-10 21:24:37 -07002028 }
2029 }
2030 mResources = resources;
2031
2032 if (container != null) {
2033 mBasePackageName = container.mBasePackageName;
2034 mOpPackageName = container.mOpPackageName;
Dianne Hackborn95d78532013-09-11 09:51:14 -07002035 } else {
2036 mBasePackageName = packageInfo.mPackageName;
2037 ApplicationInfo ainfo = packageInfo.getApplicationInfo();
2038 if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
2039 // Special case: system components allow themselves to be loaded in to other
2040 // processes. For purposes of app ops, we must then consider the context as
2041 // belonging to the package of this process, not the system itself, otherwise
2042 // the package+uid verifications in app ops will fail.
2043 mOpPackageName = ActivityThread.currentPackageName();
2044 } else {
2045 mOpPackageName = mBasePackageName;
2046 }
2047 }
Xin Guan2bcb97b2014-09-10 15:24:30 -05002048
2049 mContentResolver = new ApplicationContentResolver(this, mainThread, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002050 }
2051
Narayan Kamath29564cd2014-08-07 10:57:40 +01002052 void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
2053 mPackageInfo.installSystemApplicationInfo(info, classLoader);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
2055
2056 final void scheduleFinalCleanup(String who, String what) {
2057 mMainThread.scheduleContextCleanup(this, who, what);
2058 }
2059
2060 final void performFinalCleanup(String who, String what) {
2061 //Log.i(TAG, "Cleanup up context: " + this);
2062 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
2063 }
2064
2065 final Context getReceiverRestrictedContext() {
2066 if (mReceiverRestrictedContext != null) {
2067 return mReceiverRestrictedContext;
2068 }
2069 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
2070 }
2071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 final void setOuterContext(Context context) {
2073 mOuterContext = context;
2074 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 final Context getOuterContext() {
2077 return mOuterContext;
2078 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 final IBinder getActivityToken() {
2081 return mActivityToken;
2082 }
2083
Jeff Sharkey634dc422016-01-30 17:44:15 -07002084 private void checkMode(int mode) {
2085 if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.N) {
2086 if ((mode & MODE_WORLD_READABLE) != 0) {
2087 throw new SecurityException("MODE_WORLD_READABLE no longer supported");
2088 }
2089 if ((mode & MODE_WORLD_WRITEABLE) != 0) {
2090 throw new SecurityException("MODE_WORLD_WRITEABLE no longer supported");
2091 }
2092 }
2093 }
2094
Jeff Brown6e539312015-02-24 18:53:21 -08002095 @SuppressWarnings("deprecation")
Brad Fitzpatrickd3da4402010-11-10 08:27:11 -08002096 static void setFilePermissionsFromMode(String name, int mode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 int extraPermissions) {
2098 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
2099 |FileUtils.S_IRGRP|FileUtils.S_IWGRP
2100 |extraPermissions;
2101 if ((mode&MODE_WORLD_READABLE) != 0) {
2102 perms |= FileUtils.S_IROTH;
2103 }
2104 if ((mode&MODE_WORLD_WRITEABLE) != 0) {
2105 perms |= FileUtils.S_IWOTH;
2106 }
Mitsuru Oshima569076c2009-07-02 20:06:08 -07002107 if (DEBUG) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
2109 + ", perms=0x" + Integer.toHexString(perms));
2110 }
2111 FileUtils.setPermissions(name, perms, -1, -1);
2112 }
2113
2114 private File makeFilename(File base, String name) {
2115 if (name.indexOf(File.separatorChar) < 0) {
2116 return new File(base, name);
2117 }
2118 throw new IllegalArgumentException(
Oscar Montemayora8529f62009-11-18 10:14:20 -08002119 "File " + name + " contains a path separator");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 }
2121
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002122 /**
2123 * Ensure that given directories exist, trying to create them if missing. If
2124 * unable to create, they are filtered by replacing with {@code null}.
2125 */
Jeff Sharkey35871f22016-01-29 17:13:29 -07002126 private File[] ensureExternalDirsExistOrFilter(File[] dirs) {
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002127 File[] result = new File[dirs.length];
2128 for (int i = 0; i < dirs.length; i++) {
2129 File dir = dirs[i];
2130 if (!dir.exists()) {
2131 if (!dir.mkdirs()) {
Christopher Tatecc866da2013-10-02 18:11:01 -07002132 // recheck existence in case of cross-process race
2133 if (!dir.exists()) {
2134 // Failing to mkdir() may be okay, since we might not have
2135 // enough permissions; ask vold to create on our behalf.
2136 final IMountService mount = IMountService.Stub.asInterface(
2137 ServiceManager.getService("mount"));
Christopher Tatecc866da2013-10-02 18:11:01 -07002138 try {
Jeff Sharkey983294592015-07-13 10:25:31 -07002139 final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath());
2140 if (res != 0) {
2141 Log.w(TAG, "Failed to ensure " + dir + ": " + res);
2142 dir = null;
2143 }
2144 } catch (Exception e) {
2145 Log.w(TAG, "Failed to ensure " + dir + ": " + e);
Christopher Tatecc866da2013-10-02 18:11:01 -07002146 dir = null;
2147 }
Jeff Sharkey2d8b4e82013-09-17 17:30:33 -07002148 }
Jeff Sharkey1abdb712013-08-11 16:28:14 -07002149 }
2150 }
2151 result[i] = dir;
2152 }
2153 return result;
2154 }
2155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 // ----------------------------------------------------------------------
2157 // ----------------------------------------------------------------------
2158 // ----------------------------------------------------------------------
2159
2160 private static final class ApplicationContentResolver extends ContentResolver {
Jeff Sharkey6d515712012-09-20 16:06:08 -07002161 private final ActivityThread mMainThread;
2162 private final UserHandle mUser;
2163
2164 public ApplicationContentResolver(
2165 Context context, ActivityThread mainThread, UserHandle user) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 super(context);
Jeff Sharkey6d515712012-09-20 16:06:08 -07002167 mMainThread = Preconditions.checkNotNull(mainThread);
2168 mUser = Preconditions.checkNotNull(user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 }
2170
2171 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002172 protected IContentProvider acquireProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002173 return mMainThread.acquireProvider(context,
2174 ContentProvider.getAuthorityWithoutUserId(auth),
2175 resolveUserIdFromAuthority(auth), true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
2177
2178 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002179 protected IContentProvider acquireExistingProvider(Context context, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002180 return mMainThread.acquireExistingProvider(context,
2181 ContentProvider.getAuthorityWithoutUserId(auth),
2182 resolveUserIdFromAuthority(auth), true);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07002183 }
2184
2185 @Override
2186 public boolean releaseProvider(IContentProvider provider) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002187 return mMainThread.releaseProvider(provider, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 }
Christian Mehlmauer5f5acca2010-06-25 19:06:18 +02002189
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002190 @Override
Jeff Sharkey6d515712012-09-20 16:06:08 -07002191 protected IContentProvider acquireUnstableProvider(Context c, String auth) {
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002192 return mMainThread.acquireProvider(c,
2193 ContentProvider.getAuthorityWithoutUserId(auth),
2194 resolveUserIdFromAuthority(auth), false);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002195 }
2196
2197 @Override
2198 public boolean releaseUnstableProvider(IContentProvider icp) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002199 return mMainThread.releaseProvider(icp, false);
2200 }
2201
2202 @Override
2203 public void unstableProviderDied(IContentProvider icp) {
2204 mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
Dianne Hackborn652b6d12012-05-09 18:18:40 -07002205 }
Jeff Sharkey7aa76012013-09-30 14:26:27 -07002206
2207 @Override
2208 public void appNotRespondingViaProvider(IContentProvider icp) {
2209 mMainThread.appNotRespondingViaProvider(icp.asBinder());
2210 }
Nicolas Prevotd85fc722014-04-16 19:52:08 +01002211
2212 /** @hide */
2213 protected int resolveUserIdFromAuthority(String auth) {
2214 return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier());
2215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217}