The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.ComponentName; |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 21 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.ContextWrapper; |
| 25 | import android.content.IContentProvider; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 26 | import android.content.IIntentReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.Intent; |
| 28 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 29 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.ReceiverCallNotAllowedException; |
| 31 | import android.content.ServiceConnection; |
| 32 | import android.content.SharedPreferences; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 36 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.res.AssetManager; |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 38 | import android.content.res.CompatibilityInfo; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 39 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.res.Resources; |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 41 | import android.database.DatabaseErrorHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.database.sqlite.SQLiteDatabase; |
| 43 | import android.database.sqlite.SQLiteDatabase.CursorFactory; |
| 44 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.Binder; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 48 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.os.Bundle; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 50 | import android.os.Debug; |
Oscar Montemayor | 539d3c4 | 2010-01-29 15:27:00 -0800 | [diff] [blame] | 51 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.os.FileUtils; |
| 53 | import android.os.Handler; |
| 54 | import android.os.IBinder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 55 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 57 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.os.ServiceManager; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 59 | import android.os.UserHandle; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 60 | import android.os.storage.IMountService; |
Jeff Sharkey | e84bdd3 | 2016-02-08 12:16:00 -0700 | [diff] [blame] | 61 | import android.system.ErrnoException; |
| 62 | import android.system.Os; |
| 63 | import android.system.OsConstants; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.util.AndroidRuntimeException; |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 65 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.util.Log; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 67 | import android.util.Slog; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 68 | import android.view.Display; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 69 | import android.view.DisplayAdjustments; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 70 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 71 | import com.android.internal.annotations.GuardedBy; |
| 72 | import com.android.internal.util.Preconditions; |
| 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | import java.io.File; |
| 75 | import java.io.FileInputStream; |
| 76 | import java.io.FileNotFoundException; |
| 77 | import java.io.FileOutputStream; |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 78 | import java.io.FilenameFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import java.io.IOException; |
| 80 | import java.io.InputStream; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 81 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | class 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 Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 96 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 107 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 108 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 109 | 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 Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 121 | throw new ReceiverCallNotAllowedException( |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 122 | "BroadcastReceiver components are not allowed to bind to services"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | /** |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 127 | * Common implementation of Context API, which provides the base |
| 128 | * context object for Activity and other application components. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | */ |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 130 | class ContextImpl extends Context { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 131 | private final static String TAG = "ContextImpl"; |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 132 | private final static boolean DEBUG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Map from package name, to preference name, to cached preferences. |
| 136 | */ |
Jeff Sharkey | 8fc29cf | 2015-11-30 17:51:00 -0700 | [diff] [blame] | 137 | private static ArrayMap<String, ArrayMap<File, SharedPreferencesImpl>> sSharedPrefs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 139 | 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 Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 155 | |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 156 | private final int mFlags; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | private Context mOuterContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | private int mThemeResource = 0; |
| 160 | private Resources.Theme mTheme = null; |
| 161 | private PackageManager mPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | private Context mReceiverRestrictedContext = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | |
| 164 | private final Object mSync = new Object(); |
| 165 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 166 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | private File mDatabasesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 168 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | private File mPreferencesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 170 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | private File mFilesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 172 | @GuardedBy("mSync") |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 173 | private File mNoBackupFilesDir; |
| 174 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | private File mCacheDir; |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 176 | @GuardedBy("mSync") |
| 177 | private File mCodeCacheDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 178 | |
| 179 | @GuardedBy("mSync") |
| 180 | private File[] mExternalObbDirs; |
| 181 | @GuardedBy("mSync") |
| 182 | private File[] mExternalFilesDirs; |
| 183 | @GuardedBy("mSync") |
| 184 | private File[] mExternalCacheDirs; |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 185 | @GuardedBy("mSync") |
| 186 | private File[] mExternalMediaDirs; |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 187 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 188 | // The system service cache for the system services that are cached per-ContextImpl. |
| 189 | final Object[] mServiceCache = SystemServiceRegistry.createServiceCache(); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 190 | |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 191 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | @Override |
| 201 | public AssetManager getAssets() { |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 202 | return getResources().getAssets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |
| 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 Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | @Override |
| 236 | public Context getApplicationContext() { |
Christopher Tate | eb9e9ec | 2010-03-23 17:14:36 -0700 | [diff] [blame] | 237 | return (mPackageInfo != null) ? |
| 238 | mPackageInfo.getApplication() : mMainThread.getApplication(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | @Override |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 242 | public void setTheme(int resId) { |
| 243 | if (mThemeResource != resId) { |
| 244 | mThemeResource = resId; |
| 245 | initializeTheme(); |
| 246 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 248 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | @Override |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 250 | public int getThemeResId() { |
| 251 | return mThemeResource; |
| 252 | } |
| 253 | |
| 254 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | public Resources.Theme getTheme() { |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 256 | if (mTheme != null) { |
| 257 | return mTheme; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 259 | |
| 260 | mThemeResource = Resources.selectDefaultTheme(mThemeResource, |
| 261 | getOuterContext().getApplicationInfo().targetSdkVersion); |
| 262 | initializeTheme(); |
| 263 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | return mTheme; |
| 265 | } |
| 266 | |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 267 | private void initializeTheme() { |
| 268 | if (mTheme == null) { |
| 269 | mTheme = mResources.newTheme(); |
| 270 | } |
| 271 | mTheme.applyStyle(mThemeResource, true); |
| 272 | } |
| 273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | @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 Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 285 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 290 | /** @hide */ |
| 291 | @Override |
| 292 | public String getBasePackageName() { |
| 293 | return mBasePackageName != null ? mBasePackageName : getPackageName(); |
| 294 | } |
| 295 | |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 296 | /** @hide */ |
| 297 | @Override |
| 298 | public String getOpPackageName() { |
| 299 | return mOpPackageName != null ? mOpPackageName : getBasePackageName(); |
| 300 | } |
| 301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | @Override |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 303 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | 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 Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 325 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 326 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | public SharedPreferences getSharedPreferences(String name, int mode) { |
Jeff Sharkey | 8fc29cf | 2015-11-30 17:51:00 -0700 | [diff] [blame] | 328 | // 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 Sharkey | 6a6cdaf | 2015-12-07 19:25:19 -0700 | [diff] [blame] | 338 | final File file = getSharedPreferencesPath(name); |
Jeff Sharkey | 8fc29cf | 2015-11-30 17:51:00 -0700 | [diff] [blame] | 339 | return getSharedPreferences(file, mode); |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public SharedPreferences getSharedPreferences(File file, int mode) { |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 344 | checkMode(mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | SharedPreferencesImpl sp; |
Dianne Hackborn | f691359 | 2013-09-05 13:21:24 -0700 | [diff] [blame] | 346 | synchronized (ContextImpl.class) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 347 | final ArrayMap<File, SharedPreferencesImpl> cache = getSharedPreferencesCacheLocked(); |
| 348 | sp = cache.get(file); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 349 | if (sp == null) { |
Jeff Sharkey | 8fc29cf | 2015-11-30 17:51:00 -0700 | [diff] [blame] | 350 | sp = new SharedPreferencesImpl(file, mode); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 351 | cache.put(file, sp); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 352 | return sp; |
| 353 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | } |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 355 | 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 Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 362 | return sp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 365 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | private File getPreferencesDir() { |
| 444 | synchronized (mSync) { |
| 445 | if (mPreferencesDir == null) { |
| 446 | mPreferencesDir = new File(getDataDirFile(), "shared_prefs"); |
| 447 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 448 | return ensurePrivateDirExists(mPreferencesDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | } |
| 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 Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 460 | public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException { |
| 461 | checkMode(mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 462 | 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 Sharkey | e84bdd3 | 2016-02-08 12:16:00 -0700 | [diff] [blame] | 488 | /** |
| 489 | * Common-path handling of app data dir creation |
| 490 | */ |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 491 | private static File ensurePrivateDirExists(File file) { |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 492 | if (!file.exists()) { |
Jeff Sharkey | e84bdd3 | 2016-02-08 12:16:00 -0700 | [diff] [blame] | 493 | try { |
| 494 | Os.mkdir(file.getAbsolutePath(), 0771); |
Jeff Sharkey | 46ed6f4 | 2016-02-15 14:16:08 -0700 | [diff] [blame^] | 495 | Os.chmod(file.getAbsolutePath(), 0771); |
Jeff Sharkey | e84bdd3 | 2016-02-08 12:16:00 -0700 | [diff] [blame] | 496 | } 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 Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 501 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 502 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 503 | } |
| 504 | return file; |
| 505 | } |
| 506 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 507 | @Override |
| 508 | public File getFilesDir() { |
| 509 | synchronized (mSync) { |
| 510 | if (mFilesDir == null) { |
| 511 | mFilesDir = new File(getDataDirFile(), "files"); |
| 512 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 513 | return ensurePrivateDirExists(mFilesDir); |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 514 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 523 | return ensurePrivateDirExists(mNoBackupFilesDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 526 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | @Override |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 528 | public File getExternalFilesDir(String type) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 529 | // Operates on primary external storage |
| 530 | return getExternalFilesDirs(type)[0]; |
| 531 | } |
| 532 | |
| 533 | @Override |
| 534 | public File[] getExternalFilesDirs(String type) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 535 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 536 | if (mExternalFilesDirs == null) { |
| 537 | mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName()); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 538 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 539 | |
| 540 | // Splice in requested type, if any |
| 541 | File[] dirs = mExternalFilesDirs; |
| 542 | if (type != null) { |
| 543 | dirs = Environment.buildPaths(dirs, type); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 544 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 545 | |
| 546 | // Create dirs if needed |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 547 | return ensureExternalDirsExistOrFilter(dirs); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 548 | } |
| 549 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 550 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 551 | @Override |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 552 | public File getObbDir() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 553 | // Operates on primary external storage |
| 554 | return getObbDirs()[0]; |
| 555 | } |
| 556 | |
| 557 | @Override |
| 558 | public File[] getObbDirs() { |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 559 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 560 | if (mExternalObbDirs == null) { |
| 561 | mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName()); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 562 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 563 | |
| 564 | // Create dirs if needed |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 565 | return ensureExternalDirsExistOrFilter(mExternalObbDirs); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | public File getCacheDir() { |
| 571 | synchronized (mSync) { |
| 572 | if (mCacheDir == null) { |
| 573 | mCacheDir = new File(getDataDirFile(), "cache"); |
| 574 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 575 | return ensurePrivateDirExists(mCacheDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 578 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 579 | @Override |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 580 | public File getCodeCacheDir() { |
| 581 | synchronized (mSync) { |
| 582 | if (mCodeCacheDir == null) { |
| 583 | mCodeCacheDir = new File(getDataDirFile(), "code_cache"); |
| 584 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 585 | return ensurePrivateDirExists(mCodeCacheDir); |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
| 589 | @Override |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 590 | public File getExternalCacheDir() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 591 | // Operates on primary external storage |
| 592 | return getExternalCacheDirs()[0]; |
| 593 | } |
| 594 | |
| 595 | @Override |
| 596 | public File[] getExternalCacheDirs() { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 597 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 598 | if (mExternalCacheDirs == null) { |
| 599 | mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName()); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 600 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 601 | |
| 602 | // Create dirs if needed |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 603 | return ensureExternalDirsExistOrFilter(mExternalCacheDirs); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 604 | } |
| 605 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | @Override |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 608 | public File[] getExternalMediaDirs() { |
| 609 | synchronized (mSync) { |
| 610 | if (mExternalMediaDirs == null) { |
| 611 | mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName()); |
| 612 | } |
| 613 | |
| 614 | // Create dirs if needed |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 615 | return ensureExternalDirsExistOrFilter(mExternalMediaDirs); |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | public File getFileStreamPath(String name) { |
| 621 | return makeFilename(getFilesDir(), name); |
| 622 | } |
| 623 | |
| 624 | @Override |
Jeff Sharkey | 6a6cdaf | 2015-12-07 19:25:19 -0700 | [diff] [blame] | 625 | public File getSharedPreferencesPath(String name) { |
| 626 | return makeFilename(getPreferencesDir(), name + ".xml"); |
| 627 | } |
| 628 | |
| 629 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | public String[] fileList() { |
Jeff Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 631 | return FileUtils.listOrEmpty(getFilesDir()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | @Override |
| 635 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 636 | return openOrCreateDatabase(name, mode, factory, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | @Override |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 640 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, |
| 641 | DatabaseErrorHandler errorHandler) { |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 642 | checkMode(mode); |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 643 | File f = getDatabasePath(name); |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 644 | int flags = SQLiteDatabase.CREATE_IF_NECESSARY; |
| 645 | if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { |
| 646 | flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; |
| 647 | } |
Sunny Goyal | a21e6b2 | 2015-12-02 09:51:02 -0800 | [diff] [blame] | 648 | if ((mode & MODE_NO_LOCALIZED_COLLATORS) != 0) { |
| 649 | flags |= SQLiteDatabase.NO_LOCALIZED_COLLATORS; |
| 650 | } |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 651 | SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 652 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 653 | return db; |
| 654 | } |
| 655 | |
| 656 | @Override |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 657 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | public boolean deleteDatabase(String name) { |
| 667 | try { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 668 | File f = getDatabasePath(name); |
Jeff Brown | 79087e4 | 2012-03-01 19:52:44 -0800 | [diff] [blame] | 669 | return SQLiteDatabase.deleteDatabase(f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | } catch (Exception e) { |
| 671 | } |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | @Override |
| 676 | public File getDatabasePath(String name) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 677 | 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 Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 685 | |
| 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 Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 691 | } else { |
| 692 | dir = getDatabasesDir(); |
| 693 | f = makeFilename(dir, name); |
| 694 | } |
| 695 | |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 696 | return f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | @Override |
| 700 | public String[] databaseList() { |
Jeff Sharkey | c4bab98 | 2016-02-01 10:16:01 -0700 | [diff] [blame] | 701 | return FileUtils.listOrEmpty(getDatabasesDir()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | } |
| 703 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | private File getDatabasesDir() { |
| 705 | synchronized (mSync) { |
| 706 | if (mDatabasesDir == null) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 707 | if ("android".equals(getPackageName())) { |
| 708 | mDatabasesDir = new File("/data/system"); |
| 709 | } else { |
| 710 | mDatabasesDir = new File(getDataDirFile(), "databases"); |
| 711 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 712 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 713 | return ensurePrivateDirExists(mDatabasesDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | } |
| 715 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 718 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | public Drawable getWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 720 | return getWallpaperManager().getDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 724 | @Deprecated |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 725 | public Drawable peekWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 726 | return getWallpaperManager().peekDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 730 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 731 | public int getWallpaperDesiredMinimumWidth() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 732 | return getWallpaperManager().getDesiredMinimumWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 736 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 737 | public int getWallpaperDesiredMinimumHeight() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 738 | return getWallpaperManager().getDesiredMinimumHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 742 | @Deprecated |
| 743 | public void setWallpaper(Bitmap bitmap) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 744 | getWallpaperManager().setBitmap(bitmap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 748 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | public void setWallpaper(InputStream data) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 750 | getWallpaperManager().setStream(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 754 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | public void clearWallpaper() throws IOException { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 756 | getWallpaperManager().clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 759 | private WallpaperManager getWallpaperManager() { |
| 760 | return getSystemService(WallpaperManager.class); |
| 761 | } |
| 762 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | @Override |
| 764 | public void startActivity(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 765 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 766 | startActivity(intent, null); |
| 767 | } |
| 768 | |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 769 | /** @hide */ |
| 770 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 771 | public void startActivityAsUser(Intent intent, UserHandle user) { |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 772 | startActivityAsUser(intent, null, user); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 775 | @Override |
| 776 | public void startActivity(Intent intent, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 777 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | 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 Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 785 | getOuterContext(), mMainThread.getApplicationThread(), null, |
| 786 | (Activity) null, intent, -1, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | } |
| 788 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 789 | /** @hide */ |
| 790 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 791 | public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 792 | try { |
| 793 | ActivityManagerNative.getDefault().startActivityAsUser( |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 794 | mMainThread.getApplicationThread(), getBasePackageName(), intent, |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 795 | intent.resolveTypeIfNeeded(getContentResolver()), |
Jeff Hao | 1b012d3 | 2014-08-20 10:35:34 -0700 | [diff] [blame] | 796 | null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options, |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 797 | user.getIdentifier()); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 798 | } catch (RemoteException e) { |
| 799 | throw new RuntimeException("Failure from system", e); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 800 | } |
| 801 | } |
| 802 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | @Override |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 804 | public void startActivities(Intent[] intents) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 805 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 806 | startActivities(intents, null); |
| 807 | } |
| 808 | |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 809 | /** @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 Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 819 | getOuterContext(), mMainThread.getApplicationThread(), null, |
| 820 | (Activity) null, intents, options, userHandle.getIdentifier()); |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 823 | @Override |
| 824 | public void startActivities(Intent[] intents, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 825 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 826 | 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 Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 833 | getOuterContext(), mMainThread.getApplicationThread(), null, |
| 834 | (Activity) null, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | @Override |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 838 | public void startIntentSender(IntentSender intent, |
| 839 | Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
| 840 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 841 | 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 Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 848 | try { |
| 849 | String resolvedType = null; |
| 850 | if (fillInIntent != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 851 | fillInIntent.migrateExtraStreamToClipData(); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 852 | fillInIntent.prepareToLeaveProcess(this); |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 853 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 854 | } |
| 855 | int result = ActivityManagerNative.getDefault() |
| 856 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
| 857 | fillInIntent, resolvedType, null, null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 858 | 0, flagsMask, flagsValues, options); |
| 859 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 860 | throw new IntentSender.SendIntentException(); |
| 861 | } |
| 862 | Instrumentation.checkStartActivityResult(result, null); |
| 863 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 864 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 865 | } |
| 866 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 867 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 868 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | public void sendBroadcast(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 870 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 872 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 873 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | ActivityManagerNative.getDefault().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 875 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 876 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, false, |
| 877 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 879 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Amith Yamasani | 67cf7d3 | 2012-02-16 14:31:23 -0800 | [diff] [blame] | 883 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 884 | public void sendBroadcast(Intent intent, String receiverPermission) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 885 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 887 | String[] receiverPermissions = receiverPermission == null ? null |
| 888 | : new String[] {receiverPermission}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 889 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 890 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | ActivityManagerNative.getDefault().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 892 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 893 | 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 Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 905 | intent.prepareToLeaveProcess(this); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 906 | ActivityManagerNative.getDefault().broadcastIntent( |
| 907 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 908 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 909 | 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 Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 919 | String[] receiverPermissions = receiverPermission == null ? null |
| 920 | : new String[] {receiverPermission}; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 921 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 922 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 923 | ActivityManagerNative.getDefault().broadcastIntent( |
| 924 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 925 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 926 | options, false, false, getUserId()); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 927 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 928 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 929 | } |
| 930 | } |
| 931 | |
| 932 | @Override |
| 933 | public void sendBroadcast(Intent intent, String receiverPermission, int appOp) { |
| 934 | warnIfCallingFromSystemProcess(); |
| 935 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 936 | String[] receiverPermissions = receiverPermission == null ? null |
| 937 | : new String[] {receiverPermission}; |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 938 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 939 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 940 | ActivityManagerNative.getDefault().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 941 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 942 | Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 943 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 944 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 945 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | |
| 949 | @Override |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 950 | public void sendOrderedBroadcast(Intent intent, String receiverPermission) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 951 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 953 | String[] receiverPermissions = receiverPermission == null ? null |
| 954 | : new String[] {receiverPermission}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 956 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | ActivityManagerNative.getDefault().broadcastIntent( |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 958 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 959 | Activity.RESULT_OK, null, null, receiverPermissions, AppOpsManager.OP_NONE, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 960 | null, true, false, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 962 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | } |
| 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 Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 971 | sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 972 | 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 Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 982 | } |
| 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 Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 989 | 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 Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 997 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | 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 Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1011 | rd = new LoadedApk.ReceiverDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1013 | } |
| 1014 | } |
| 1015 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1016 | String[] receiverPermissions = receiverPermission == null ? null |
| 1017 | : new String[] {receiverPermission}; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1019 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1021 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1022 | initialCode, initialData, initialExtras, receiverPermissions, appOp, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1023 | options, true, false, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1025 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1030 | public void sendBroadcastAsUser(Intent intent, UserHandle user) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1031 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1032 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1033 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1034 | ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(), |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1035 | intent, resolvedType, null, Activity.RESULT_OK, null, null, null, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1036 | AppOpsManager.OP_NONE, null, false, false, user.getIdentifier()); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1037 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1038 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1043 | public void sendBroadcastAsUser(Intent intent, UserHandle user, |
| 1044 | String receiverPermission) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1045 | 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 Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1051 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1052 | String[] receiverPermissions = receiverPermission == null ? null |
| 1053 | : new String[] {receiverPermission}; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1054 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1055 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1056 | ActivityManagerNative.getDefault().broadcastIntent( |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1057 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1058 | Activity.RESULT_OK, null, null, receiverPermissions, appOp, null, false, false, |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 1059 | user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1060 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1061 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 1066 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1067 | String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1068 | int initialCode, String initialData, Bundle initialExtras) { |
Amith Yamasani | 3cf7572 | 2014-05-16 12:37:29 -0700 | [diff] [blame] | 1069 | sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1070 | null, resultReceiver, scheduler, initialCode, initialData, initialExtras); |
Amith Yamasani | 3cf7572 | 2014-05-16 12:37:29 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | @Override |
| 1074 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
| 1075 | String receiverPermission, int appOp, BroadcastReceiver resultReceiver, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1076 | 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 Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1085 | 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 Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1098 | rd = new LoadedApk.ReceiverDispatcher(resultReceiver, getOuterContext(), |
| 1099 | scheduler, null, false).getIIntentReceiver(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1103 | String[] receiverPermissions = receiverPermission == null ? null |
| 1104 | : new String[] {receiverPermission}; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1105 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1106 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1107 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1108 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1109 | initialCode, initialData, initialExtras, receiverPermissions, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1110 | appOp, options, true, false, user.getIdentifier()); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1111 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1112 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1117 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | public void sendStickyBroadcast(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1119 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1121 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1122 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1124 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1125 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, null, false, true, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1126 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1127 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1128 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1133 | @Deprecated |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1134 | public void sendStickyOrderedBroadcast(Intent intent, |
| 1135 | BroadcastReceiver resultReceiver, |
| 1136 | Handler scheduler, int initialCode, String initialData, |
| 1137 | Bundle initialExtras) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1138 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1139 | 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 Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1152 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1153 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1154 | } |
| 1155 | } |
| 1156 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1157 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1158 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1159 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1160 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1161 | initialCode, initialData, initialExtras, null, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1162 | AppOpsManager.OP_NONE, null, true, true, getUserId()); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1163 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1164 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1169 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1170 | 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 Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1177 | intent.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | ActivityManagerNative.getDefault().unbroadcastIntent( |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1179 | mMainThread.getApplicationThread(), intent, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1181 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1186 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1187 | public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { |
| 1188 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1189 | try { |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1190 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1191 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1192 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1193 | 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 Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1205 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1206 | 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 Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1210 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1211 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1216 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1217 | 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 Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1240 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1241 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1242 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1243 | initialCode, initialData, initialExtras, null, |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1244 | AppOpsManager.OP_NONE, null, true, true, user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1245 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1246 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1251 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1252 | 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 Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1259 | intent.prepareToLeaveProcess(this); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1260 | ActivityManagerNative.getDefault().unbroadcastIntent( |
| 1261 | mMainThread.getApplicationThread(), intent, user.getIdentifier()); |
| 1262 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1263 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1268 | 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 Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1275 | return registerReceiverInternal(receiver, getUserId(), |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1276 | filter, broadcastPermission, scheduler, getOuterContext()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | } |
| 1278 | |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1279 | @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | 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 Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1302 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 1303 | receiver, context, scheduler, null, true).getIIntentReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | try { |
| 1307 | return ActivityManagerNative.getDefault().registerReceiver( |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1308 | mMainThread.getApplicationThread(), mBasePackageName, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1309 | rd, filter, broadcastPermission, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | } 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 Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1329 | private void validateServiceIntent(Intent service) { |
| 1330 | if (service.getComponent() == null && service.getPackage() == null) { |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 1331 | if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) { |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1332 | IllegalArgumentException ex = new IllegalArgumentException( |
| 1333 | "Service Intent must be explicit: " + service); |
| 1334 | throw ex; |
| 1335 | } else { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1336 | Log.w(TAG, "Implicit intents with startService are not safe: " + service |
| 1337 | + " " + Debug.getCallers(2, 3)); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | @Override |
| 1343 | public ComponentName startService(Intent service) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1344 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1345 | return startServiceCommon(service, mUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | @Override |
| 1349 | public boolean stopService(Intent service) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1350 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1351 | return stopServiceCommon(service, mUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | @Override |
| 1355 | public ComponentName startServiceAsUser(Intent service, UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1356 | return startServiceCommon(service, user); |
| 1357 | } |
| 1358 | |
| 1359 | private ComponentName startServiceCommon(Intent service, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | try { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1361 | validateServiceIntent(service); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1362 | service.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | ComponentName cn = ActivityManagerNative.getDefault().startService( |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1364 | mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded( |
| 1365 | getContentResolver()), getOpPackageName(), user.getIdentifier()); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1366 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | } |
| 1377 | return cn; |
| 1378 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1379 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | @Override |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1384 | public boolean stopServiceAsUser(Intent service, UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1385 | return stopServiceCommon(service, user); |
| 1386 | } |
| 1387 | |
| 1388 | private boolean stopServiceCommon(Intent service, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | try { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1390 | validateServiceIntent(service); |
Jeff Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1391 | service.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1392 | int res = ActivityManagerNative.getDefault().stopService( |
| 1393 | mMainThread.getApplicationThread(), service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1394 | service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1395 | if (res < 0) { |
| 1396 | throw new SecurityException( |
| 1397 | "Not allowed to stop service " + service); |
| 1398 | } |
| 1399 | return res != 0; |
| 1400 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1401 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | @Override |
| 1406 | public boolean bindService(Intent service, ServiceConnection conn, |
| 1407 | int flags) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1408 | warnIfCallingFromSystemProcess(); |
Adrian Roos | 691546e | 2016-02-09 10:13:41 -0800 | [diff] [blame] | 1409 | return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), |
| 1410 | Process.myUserHandle()); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | /** @hide */ |
| 1414 | @Override |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1415 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 1416 | UserHandle user) { |
Adrian Roos | 691546e | 2016-02-09 10:13:41 -0800 | [diff] [blame] | 1417 | return bindServiceCommon(service, conn, flags, mMainThread.getHandler(), user); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
Adrian Roos | 691546e | 2016-02-09 10:13:41 -0800 | [diff] [blame] | 1420 | /** @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | IServiceConnection sd; |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1433 | if (conn == null) { |
| 1434 | throw new IllegalArgumentException("connection is null"); |
| 1435 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1436 | if (mPackageInfo != null) { |
Adrian Roos | 691546e | 2016-02-09 10:13:41 -0800 | [diff] [blame] | 1437 | sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | } else { |
| 1439 | throw new RuntimeException("Not supported in system context"); |
| 1440 | } |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1441 | validateServiceIntent(service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1443 | 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 Sharkey | 344744b | 2016-01-28 19:03:30 -0700 | [diff] [blame] | 1449 | service.prepareToLeaveProcess(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | int res = ActivityManagerNative.getDefault().bindService( |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1451 | mMainThread.getApplicationThread(), getActivityToken(), service, |
| 1452 | service.resolveTypeIfNeeded(getContentResolver()), |
| 1453 | sd, flags, getOpPackageName(), user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1454 | 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 Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1460 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | @Override |
| 1465 | public void unbindService(ServiceConnection conn) { |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1466 | if (conn == null) { |
| 1467 | throw new IllegalArgumentException("connection is null"); |
| 1468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1469 | 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 Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1485 | if (arguments != null) { |
| 1486 | arguments.setAllowFds(false); |
| 1487 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | return ActivityManagerNative.getDefault().startInstrumentation( |
Narayan Kamath | 8dcfefd | 2014-05-15 18:12:59 +0100 | [diff] [blame] | 1489 | className, profileFile, 0, arguments, null, null, getUserId(), |
| 1490 | null /* ABI override */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1492 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | @Override |
| 1497 | public Object getSystemService(String name) { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1498 | return SystemServiceRegistry.getSystemService(this, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1501 | @Override |
| 1502 | public String getSystemServiceName(Class<?> serviceClass) { |
| 1503 | return SystemServiceRegistry.getSystemServiceName(serviceClass); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1506 | @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | try { |
| 1513 | return ActivityManagerNative.getDefault().checkPermission( |
| 1514 | permission, pid, uid); |
| 1515 | } catch (RemoteException e) { |
| 1516 | return PackageManager.PERMISSION_DENIED; |
| 1517 | } |
| 1518 | } |
| 1519 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 1520 | /** @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | @Override |
| 1536 | public int checkCallingPermission(String permission) { |
| 1537 | if (permission == null) { |
| 1538 | throw new IllegalArgumentException("permission is null"); |
| 1539 | } |
| 1540 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | int pid = Binder.getCallingPid(); |
| 1542 | if (pid != Process.myPid()) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1543 | return checkPermission(permission, pid, Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1544 | } |
| 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 | |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1558 | @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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | 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 Tate | 4dc7a68 | 2012-09-11 12:15:49 -0700 | [diff] [blame] | 1575 | : "uid " + uid + " does not have ") + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1576 | permission + |
| 1577 | "."); |
| 1578 | } |
| 1579 | } |
| 1580 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1581 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1582 | 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 Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1591 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1592 | public void enforceCallingPermission(String permission, String message) { |
| 1593 | enforce(permission, |
| 1594 | checkCallingPermission(permission), |
| 1595 | false, |
| 1596 | Binder.getCallingUid(), |
| 1597 | message); |
| 1598 | } |
| 1599 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1600 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1601 | 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 Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1614 | mMainThread.getApplicationThread(), toPackage, |
| 1615 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1616 | } catch (RemoteException e) { |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | @Override |
| 1621 | public void revokeUriPermission(Uri uri, int modeFlags) { |
| 1622 | try { |
| 1623 | ActivityManagerNative.getDefault().revokeUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1624 | mMainThread.getApplicationThread(), |
| 1625 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | } catch (RemoteException e) { |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | @Override |
| 1631 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1632 | try { |
| 1633 | return ActivityManagerNative.getDefault().checkUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1634 | ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags, |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 1635 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1648 | } catch (RemoteException e) { |
| 1649 | return PackageManager.PERMISSION_DENIED; |
| 1650 | } |
| 1651 | } |
| 1652 | |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1653 | private int resolveUserId(Uri uri) { |
| 1654 | return ContentProvider.getUserIdFromUri(uri, getUserId()); |
| 1655 | } |
| 1656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | @Override |
| 1658 | public int checkCallingUriPermission(Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1659 | 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 Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 1676 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | 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 Sharkey | 846318a | 2014-04-04 12:12:41 -0700 | [diff] [blame] | 1700 | StringBuilder builder = new StringBuilder(); |
| 1701 | if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 1702 | builder.append("read and "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | } |
Jeff Sharkey | 846318a | 2014-04-04 12:12:41 -0700 | [diff] [blame] | 1704 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | } |
| 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 Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1738 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1739 | 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 Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1746 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1747 | public void enforceCallingUriPermission( |
| 1748 | Uri uri, int modeFlags, String message) { |
| 1749 | enforceForUri( |
| 1750 | modeFlags, checkCallingUriPermission(uri, modeFlags), |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1751 | false, |
| 1752 | Binder.getCallingUid(), uri, message); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1753 | } |
| 1754 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1755 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1756 | 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 Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1764 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1765 | 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'Neill | 365632e | 2013-09-09 09:34:58 -0700 | [diff] [blame] | 1778 | /** |
| 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 Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1783 | 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 Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 1786 | + Debug.getCallers(5)); |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1787 | } |
| 1788 | } |
| 1789 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1790 | @Override |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 1791 | 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) { |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 1796 | ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken, |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1797 | new UserHandle(UserHandle.getUserId(application.uid)), flags, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1798 | mDisplay, null, Display.INVALID_DISPLAY); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 1799 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1809 | public Context createPackageContext(String packageName, int flags) |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1810 | throws NameNotFoundException { |
Amith Yamasani | 64442c1 | 2012-10-07 08:17:46 -0700 | [diff] [blame] | 1811 | return createPackageContextAsUser(packageName, flags, |
| 1812 | mUser != null ? mUser : Process.myUserHandle()); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | @Override |
| 1816 | public Context createPackageContextAsUser(String packageName, int flags, UserHandle user) |
| 1817 | throws NameNotFoundException { |
Kenny Guy | c2a4060 | 2014-09-08 18:51:15 +0000 | [diff] [blame] | 1818 | if (packageName.equals("system") || packageName.equals("android")) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1819 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1820 | user, flags, mDisplay, null, Display.INVALID_DISPLAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1821 | } |
| 1822 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1823 | LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 1824 | flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1825 | if (pi != null) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1826 | ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken, |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1827 | user, flags, mDisplay, null, Display.INVALID_DISPLAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1828 | if (c.mResources != null) { |
| 1829 | return c; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | // Should be a better exception. |
| 1834 | throw new PackageManager.NameNotFoundException( |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1835 | "Application package " + packageName + " not found"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1836 | } |
| 1837 | |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 1838 | @Override |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 1839 | public Context createConfigurationContext(Configuration overrideConfiguration) { |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 1840 | if (overrideConfiguration == null) { |
| 1841 | throw new IllegalArgumentException("overrideConfiguration must not be null"); |
| 1842 | } |
| 1843 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1844 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1845 | mUser, mFlags, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | @Override |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 1849 | public Context createDisplayContext(Display display) { |
| 1850 | if (display == null) { |
| 1851 | throw new IllegalArgumentException("display must not be null"); |
| 1852 | } |
| 1853 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1854 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1855 | mUser, mFlags, display, null, Display.INVALID_DISPLAY); |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1858 | Display getDisplay() { |
| 1859 | if (mDisplay != null) { |
| 1860 | return mDisplay; |
| 1861 | } |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1862 | return ResourcesManager.getInstance().getAdjustedDisplay( |
| 1863 | Display.DEFAULT_DISPLAY, mDisplayAdjustments); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1864 | } |
| 1865 | |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 1866 | private int getDisplayId() { |
| 1867 | return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY; |
| 1868 | } |
| 1869 | |
| 1870 | @Override |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1871 | public Context createDeviceEncryptedStorageContext() { |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1872 | final int flags = (mFlags & ~Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE) |
| 1873 | | Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1874 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
| 1875 | mUser, flags, mDisplay, null, Display.INVALID_DISPLAY); |
| 1876 | } |
| 1877 | |
| 1878 | @Override |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1879 | public Context createCredentialEncryptedStorageContext() { |
| 1880 | final int flags = (mFlags & ~Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE) |
| 1881 | | Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1882 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
| 1883 | mUser, flags, mDisplay, null, Display.INVALID_DISPLAY); |
| 1884 | } |
| 1885 | |
| 1886 | @Override |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 1887 | public boolean isRestricted() { |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1888 | return (mFlags & Context.CONTEXT_RESTRICTED) != 0; |
| 1889 | } |
| 1890 | |
| 1891 | @Override |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1892 | public boolean isDeviceEncryptedStorage() { |
| 1893 | return (mFlags & Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE) != 0; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | @Override |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1897 | public boolean isCredentialEncryptedStorage() { |
| 1898 | return (mFlags & Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE) != 0; |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 1901 | @Override |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 1902 | public DisplayAdjustments getDisplayAdjustments(int displayId) { |
| 1903 | return mDisplayAdjustments; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1906 | private File getDataDirFile() { |
| 1907 | if (mPackageInfo != null) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 1908 | File res = null; |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1909 | if (isCredentialEncryptedStorage()) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 1910 | res = mPackageInfo.getCredentialEncryptedDataDirFile(); |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1911 | } else if (isDeviceEncryptedStorage()) { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 1912 | res = mPackageInfo.getDeviceEncryptedDataDirFile(); |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1913 | } else { |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 1914 | res = mPackageInfo.getDataDirFile(); |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1915 | } |
Jeff Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 1916 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1926 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | @Override |
| 1930 | public File getDir(String name, int mode) { |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 1931 | checkMode(mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1932 | 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 Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1942 | /** {@hide} */ |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1943 | @Override |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1944 | public int getUserId() { |
| 1945 | return mUser.getIdentifier(); |
| 1946 | } |
| 1947 | |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 1948 | static ContextImpl createSystemContext(ActivityThread mainThread) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1949 | LoadedApk packageInfo = new LoadedApk(mainThread); |
| 1950 | ContextImpl context = new ContextImpl(null, mainThread, |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1951 | packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1952 | context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(), |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 1953 | context.mResourcesManager.getDisplayMetricsLocked()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1954 | return context; |
| 1955 | } |
| 1956 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1957 | static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) { |
| 1958 | if (packageInfo == null) throw new IllegalArgumentException("packageInfo"); |
| 1959 | return new ContextImpl(null, mainThread, |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1960 | packageInfo, null, null, 0, null, null, Display.INVALID_DISPLAY); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | static ContextImpl createActivityContext(ActivityThread mainThread, |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 1964 | LoadedApk packageInfo, int displayId, Configuration overrideConfiguration) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1965 | if (packageInfo == null) throw new IllegalArgumentException("packageInfo"); |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1966 | return new ContextImpl(null, mainThread, packageInfo, null, null, 0, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1967 | null, overrideConfiguration, displayId); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | private ContextImpl(ContextImpl container, ActivityThread mainThread, |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1971 | LoadedApk packageInfo, IBinder activityToken, UserHandle user, int flags, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1972 | Display display, Configuration overrideConfiguration, int createDisplayWithId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1973 | mOuterContext = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1974 | |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1975 | // If creator didn't specify which storage to use, use the default |
| 1976 | // location for application. |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1977 | if ((flags & (Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE |
| 1978 | | Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE)) == 0) { |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1979 | final File dataDir = packageInfo.getDataDirFile(); |
| 1980 | if (Objects.equals(dataDir, packageInfo.getCredentialEncryptedDataDirFile())) { |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1981 | flags |= Context.CONTEXT_CREDENTIAL_ENCRYPTED_STORAGE; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1982 | } else if (Objects.equals(dataDir, packageInfo.getDeviceEncryptedDataDirFile())) { |
Jeff Sharkey | e13529a | 2015-12-09 14:15:27 -0700 | [diff] [blame] | 1983 | flags |= Context.CONTEXT_DEVICE_ENCRYPTED_STORAGE; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1984 | } |
| 1985 | } |
| 1986 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1987 | mMainThread = mainThread; |
| 1988 | mActivityToken = activityToken; |
Jeff Sharkey | 7a30a30 | 2015-12-08 14:20:06 -0700 | [diff] [blame] | 1989 | mFlags = flags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1991 | if (user == null) { |
| 1992 | user = Process.myUserHandle(); |
| 1993 | } |
| 1994 | mUser = user; |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1995 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | mPackageInfo = packageInfo; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1997 | mResourcesManager = ResourcesManager.getInstance(); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1998 | |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1999 | final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY) |
Wale Ogunwale | cac3dc6 | 2015-06-09 15:35:02 -0700 | [diff] [blame] | 2000 | ? createDisplayWithId |
| 2001 | : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY; |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 2002 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2003 | CompatibilityInfo compatInfo = null; |
| 2004 | if (container != null) { |
| 2005 | compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo(); |
| 2006 | } |
Wale Ogunwale | 9cf9954 | 2015-02-18 16:31:34 -0800 | [diff] [blame] | 2007 | if (compatInfo == null) { |
| 2008 | compatInfo = (displayId == Display.DEFAULT_DISPLAY) |
| 2009 | ? packageInfo.getCompatibilityInfo() |
| 2010 | : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2011 | } |
| 2012 | mDisplayAdjustments.setCompatibilityInfo(compatInfo); |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 2013 | mDisplayAdjustments.setConfiguration(overrideConfiguration); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2014 | |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 2015 | mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display |
| 2016 | : ResourcesManager.getInstance().getAdjustedDisplay(displayId, mDisplayAdjustments); |
| 2017 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2018 | Resources resources = packageInfo.getResources(mainThread); |
| 2019 | if (resources != null) { |
Wale Ogunwale | 60454db | 2015-01-23 16:05:07 -0800 | [diff] [blame] | 2020 | if (displayId != Display.DEFAULT_DISPLAY |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2021 | || overrideConfiguration != null |
| 2022 | || (compatInfo != null && compatInfo.applicationScale |
| 2023 | != resources.getCompatibilityInfo().applicationScale)) { |
Jeff Sharkey | 8a4c972 | 2014-06-16 13:48:42 -0700 | [diff] [blame] | 2024 | resources = mResourcesManager.getTopLevelResources(packageInfo.getResDir(), |
| 2025 | packageInfo.getSplitResDirs(), packageInfo.getOverlayDirs(), |
| 2026 | packageInfo.getApplicationInfo().sharedLibraryFiles, displayId, |
Alan Viverette | 02fc5fe | 2015-08-27 13:16:09 -0400 | [diff] [blame] | 2027 | overrideConfiguration, compatInfo, packageInfo.getClassLoader()); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 2028 | } |
| 2029 | } |
| 2030 | mResources = resources; |
| 2031 | |
| 2032 | if (container != null) { |
| 2033 | mBasePackageName = container.mBasePackageName; |
| 2034 | mOpPackageName = container.mOpPackageName; |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 2035 | } 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 Guan | 2bcb97b | 2014-09-10 15:24:30 -0500 | [diff] [blame] | 2048 | |
| 2049 | mContentResolver = new ApplicationContentResolver(this, mainThread, user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2050 | } |
| 2051 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 2052 | void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) { |
| 2053 | mPackageInfo.installSystemApplicationInfo(info, classLoader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2054 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2072 | final void setOuterContext(Context context) { |
| 2073 | mOuterContext = context; |
| 2074 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2075 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2076 | final Context getOuterContext() { |
| 2077 | return mOuterContext; |
| 2078 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2079 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2080 | final IBinder getActivityToken() { |
| 2081 | return mActivityToken; |
| 2082 | } |
| 2083 | |
Jeff Sharkey | 634dc42 | 2016-01-30 17:44:15 -0700 | [diff] [blame] | 2084 | 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 Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 2095 | @SuppressWarnings("deprecation") |
Brad Fitzpatrick | d3da440 | 2010-11-10 08:27:11 -0800 | [diff] [blame] | 2096 | static void setFilePermissionsFromMode(String name, int mode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2097 | 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 Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 2107 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2108 | 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 Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 2119 | "File " + name + " contains a path separator"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2120 | } |
| 2121 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2122 | /** |
| 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 Sharkey | 35871f2 | 2016-01-29 17:13:29 -0700 | [diff] [blame] | 2126 | private File[] ensureExternalDirsExistOrFilter(File[] dirs) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2127 | 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 Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 2132 | // 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 Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 2138 | try { |
Jeff Sharkey | 98329459 | 2015-07-13 10:25:31 -0700 | [diff] [blame] | 2139 | 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 Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 2146 | dir = null; |
| 2147 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 2148 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 2149 | } |
| 2150 | } |
| 2151 | result[i] = dir; |
| 2152 | } |
| 2153 | return result; |
| 2154 | } |
| 2155 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2156 | // ---------------------------------------------------------------------- |
| 2157 | // ---------------------------------------------------------------------- |
| 2158 | // ---------------------------------------------------------------------- |
| 2159 | |
| 2160 | private static final class ApplicationContentResolver extends ContentResolver { |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2161 | private final ActivityThread mMainThread; |
| 2162 | private final UserHandle mUser; |
| 2163 | |
| 2164 | public ApplicationContentResolver( |
| 2165 | Context context, ActivityThread mainThread, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2166 | super(context); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2167 | mMainThread = Preconditions.checkNotNull(mainThread); |
| 2168 | mUser = Preconditions.checkNotNull(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2172 | protected IContentProvider acquireProvider(Context context, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2173 | return mMainThread.acquireProvider(context, |
| 2174 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2175 | resolveUserIdFromAuthority(auth), true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2179 | protected IContentProvider acquireExistingProvider(Context context, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2180 | return mMainThread.acquireExistingProvider(context, |
| 2181 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2182 | resolveUserIdFromAuthority(auth), true); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | @Override |
| 2186 | public boolean releaseProvider(IContentProvider provider) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 2187 | return mMainThread.releaseProvider(provider, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2188 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 2189 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2190 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 2191 | protected IContentProvider acquireUnstableProvider(Context c, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2192 | return mMainThread.acquireProvider(c, |
| 2193 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 2194 | resolveUserIdFromAuthority(auth), false); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | @Override |
| 2198 | public boolean releaseUnstableProvider(IContentProvider icp) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 2199 | return mMainThread.releaseProvider(icp, false); |
| 2200 | } |
| 2201 | |
| 2202 | @Override |
| 2203 | public void unstableProviderDied(IContentProvider icp) { |
| 2204 | mMainThread.handleUnstableProviderDied(icp.asBinder(), true); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 2205 | } |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 2206 | |
| 2207 | @Override |
| 2208 | public void appNotRespondingViaProvider(IContentProvider icp) { |
| 2209 | mMainThread.appNotRespondingViaProvider(icp.asBinder()); |
| 2210 | } |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 2211 | |
| 2212 | /** @hide */ |
| 2213 | protected int resolveUserIdFromAuthority(String auth) { |
| 2214 | return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier()); |
| 2215 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2217 | } |