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 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 19 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 20 | import com.android.internal.util.Preconditions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ComponentName; |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 24 | import android.content.ContentProvider; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.content.ContentResolver; |
| 26 | import android.content.Context; |
| 27 | import android.content.ContextWrapper; |
| 28 | import android.content.IContentProvider; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 29 | import android.content.IIntentReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 32 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.ReceiverCallNotAllowedException; |
| 34 | import android.content.ServiceConnection; |
| 35 | import android.content.SharedPreferences; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.content.pm.PackageManager; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 39 | import android.content.pm.PackageManager.NameNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.res.AssetManager; |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 41 | import android.content.res.CompatibilityInfo; |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 42 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.content.res.Resources; |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 44 | import android.database.DatabaseErrorHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.database.sqlite.SQLiteDatabase; |
| 46 | import android.database.sqlite.SQLiteDatabase.CursorFactory; |
| 47 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.graphics.drawable.Drawable; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 49 | import android.hardware.display.DisplayManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.os.Binder; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 52 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.os.Bundle; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 54 | import android.os.Debug; |
Oscar Montemayor | 539d3c4 | 2010-01-29 15:27:00 -0800 | [diff] [blame] | 55 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.os.FileUtils; |
| 57 | import android.os.Handler; |
| 58 | import android.os.IBinder; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 59 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 61 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.os.ServiceManager; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 63 | import android.os.UserHandle; |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 64 | import android.os.storage.IMountService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.util.AndroidRuntimeException; |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 66 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.util.Log; |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 68 | import android.util.Slog; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 69 | import android.view.Display; |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 70 | import android.view.DisplayAdjustments; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import java.io.File; |
| 73 | import java.io.FileInputStream; |
| 74 | import java.io.FileNotFoundException; |
| 75 | import java.io.FileOutputStream; |
| 76 | import java.io.IOException; |
| 77 | import java.io.InputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | class ReceiverRestrictedContext extends ContextWrapper { |
| 80 | ReceiverRestrictedContext(Context base) { |
| 81 | super(base); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 86 | return registerReceiver(receiver, filter, null, null); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 91 | String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 92 | if (receiver == null) { |
| 93 | // Allow retrieving current sticky broadcast; this is safe since we |
| 94 | // aren't actually registering a receiver. |
| 95 | return super.registerReceiver(null, filter, broadcastPermission, scheduler); |
| 96 | } else { |
| 97 | throw new ReceiverCallNotAllowedException( |
| 98 | "BroadcastReceiver components are not allowed to register to receive intents"); |
| 99 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 103 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 104 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 105 | if (receiver == null) { |
| 106 | // Allow retrieving current sticky broadcast; this is safe since we |
| 107 | // aren't actually registering a receiver. |
| 108 | return super.registerReceiverAsUser(null, user, filter, broadcastPermission, scheduler); |
| 109 | } else { |
| 110 | throw new ReceiverCallNotAllowedException( |
| 111 | "BroadcastReceiver components are not allowed to register to receive intents"); |
| 112 | } |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 117 | throw new ReceiverCallNotAllowedException( |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 118 | "BroadcastReceiver components are not allowed to bind to services"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 123 | * Common implementation of Context API, which provides the base |
| 124 | * context object for Activity and other application components. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | */ |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 126 | class ContextImpl extends Context { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 127 | private final static String TAG = "ContextImpl"; |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 128 | private final static boolean DEBUG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Map from package name, to preference name, to cached preferences. |
| 132 | */ |
| 133 | private static ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>> sSharedPrefs; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 135 | final ActivityThread mMainThread; |
| 136 | final LoadedApk mPackageInfo; |
| 137 | |
| 138 | private final IBinder mActivityToken; |
| 139 | |
| 140 | private final UserHandle mUser; |
| 141 | |
| 142 | private final ApplicationContentResolver mContentResolver; |
| 143 | |
| 144 | private final String mBasePackageName; |
| 145 | private final String mOpPackageName; |
| 146 | |
| 147 | private final ResourcesManager mResourcesManager; |
| 148 | private final Resources mResources; |
| 149 | private final Display mDisplay; // may be null if default display |
| 150 | private final DisplayAdjustments mDisplayAdjustments = new DisplayAdjustments(); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 151 | |
| 152 | private final boolean mRestricted; |
| 153 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | private Context mOuterContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | private int mThemeResource = 0; |
| 156 | private Resources.Theme mTheme = null; |
| 157 | private PackageManager mPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | private Context mReceiverRestrictedContext = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
| 160 | private final Object mSync = new Object(); |
| 161 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 162 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | private File mDatabasesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 164 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | private File mPreferencesDir; |
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 mFilesDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 168 | @GuardedBy("mSync") |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 169 | private File mNoBackupFilesDir; |
| 170 | @GuardedBy("mSync") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | private File mCacheDir; |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 172 | @GuardedBy("mSync") |
| 173 | private File mCodeCacheDir; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 174 | |
| 175 | @GuardedBy("mSync") |
| 176 | private File[] mExternalObbDirs; |
| 177 | @GuardedBy("mSync") |
| 178 | private File[] mExternalFilesDirs; |
| 179 | @GuardedBy("mSync") |
| 180 | private File[] mExternalCacheDirs; |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 181 | @GuardedBy("mSync") |
| 182 | private File[] mExternalMediaDirs; |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | private static final String[] EMPTY_FILE_LIST = {}; |
| 185 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 186 | // The system service cache for the system services that are cached per-ContextImpl. |
| 187 | final Object[] mServiceCache = SystemServiceRegistry.createServiceCache(); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 188 | |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 189 | static ContextImpl getImpl(Context context) { |
| 190 | Context nextContext; |
| 191 | while ((context instanceof ContextWrapper) && |
| 192 | (nextContext=((ContextWrapper)context).getBaseContext()) != null) { |
| 193 | context = nextContext; |
| 194 | } |
| 195 | return (ContextImpl)context; |
| 196 | } |
| 197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | @Override |
| 199 | public AssetManager getAssets() { |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 200 | return getResources().getAssets(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public Resources getResources() { |
| 205 | return mResources; |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public PackageManager getPackageManager() { |
| 210 | if (mPackageManager != null) { |
| 211 | return mPackageManager; |
| 212 | } |
| 213 | |
| 214 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 215 | if (pm != null) { |
| 216 | // Doesn't matter if we make more than one instance. |
| 217 | return (mPackageManager = new ApplicationPackageManager(this, pm)); |
| 218 | } |
| 219 | |
| 220 | return null; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public ContentResolver getContentResolver() { |
| 225 | return mContentResolver; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public Looper getMainLooper() { |
| 230 | return mMainThread.getLooper(); |
| 231 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | @Override |
| 234 | public Context getApplicationContext() { |
Christopher Tate | eb9e9ec | 2010-03-23 17:14:36 -0700 | [diff] [blame] | 235 | return (mPackageInfo != null) ? |
| 236 | mPackageInfo.getApplication() : mMainThread.getApplication(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | @Override |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 240 | public void setTheme(int resId) { |
| 241 | if (mThemeResource != resId) { |
| 242 | mThemeResource = resId; |
| 243 | initializeTheme(); |
| 244 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | @Override |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 248 | public int getThemeResId() { |
| 249 | return mThemeResource; |
| 250 | } |
| 251 | |
| 252 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | public Resources.Theme getTheme() { |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 254 | if (mTheme != null) { |
| 255 | return mTheme; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | } |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 257 | |
| 258 | mThemeResource = Resources.selectDefaultTheme(mThemeResource, |
| 259 | getOuterContext().getApplicationInfo().targetSdkVersion); |
| 260 | initializeTheme(); |
| 261 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | return mTheme; |
| 263 | } |
| 264 | |
Alan Viverette | cc2a1d4 | 2015-05-01 11:28:37 -0700 | [diff] [blame] | 265 | private void initializeTheme() { |
| 266 | if (mTheme == null) { |
| 267 | mTheme = mResources.newTheme(); |
| 268 | } |
| 269 | mTheme.applyStyle(mThemeResource, true); |
| 270 | } |
| 271 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | @Override |
| 273 | public ClassLoader getClassLoader() { |
| 274 | return mPackageInfo != null ? |
| 275 | mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader(); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public String getPackageName() { |
| 280 | if (mPackageInfo != null) { |
| 281 | return mPackageInfo.getPackageName(); |
| 282 | } |
Dianne Hackborn | 35654b6 | 2013-01-14 17:38:02 -0800 | [diff] [blame] | 283 | // No mPackageInfo means this is a Context for the system itself, |
| 284 | // and this here is its name. |
| 285 | return "android"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Dianne Hackborn | d8e1dbb | 2013-01-17 17:47:37 -0800 | [diff] [blame] | 288 | /** @hide */ |
| 289 | @Override |
| 290 | public String getBasePackageName() { |
| 291 | return mBasePackageName != null ? mBasePackageName : getPackageName(); |
| 292 | } |
| 293 | |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 294 | /** @hide */ |
| 295 | @Override |
| 296 | public String getOpPackageName() { |
| 297 | return mOpPackageName != null ? mOpPackageName : getBasePackageName(); |
| 298 | } |
| 299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | @Override |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 301 | public ApplicationInfo getApplicationInfo() { |
| 302 | if (mPackageInfo != null) { |
| 303 | return mPackageInfo.getApplicationInfo(); |
| 304 | } |
| 305 | throw new RuntimeException("Not supported in system context"); |
| 306 | } |
| 307 | |
| 308 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | public String getPackageResourcePath() { |
| 310 | if (mPackageInfo != null) { |
| 311 | return mPackageInfo.getResDir(); |
| 312 | } |
| 313 | throw new RuntimeException("Not supported in system context"); |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public String getPackageCodePath() { |
| 318 | if (mPackageInfo != null) { |
| 319 | return mPackageInfo.getAppDir(); |
| 320 | } |
| 321 | throw new RuntimeException("Not supported in system context"); |
| 322 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 323 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 324 | @Override |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 325 | public File getSharedPrefsFile(String name) { |
| 326 | return makeFilename(getPreferencesDir(), name + ".xml"); |
| 327 | } |
| 328 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | @Override |
| 330 | public SharedPreferences getSharedPreferences(String name, int mode) { |
| 331 | SharedPreferencesImpl sp; |
Dianne Hackborn | f691359 | 2013-09-05 13:21:24 -0700 | [diff] [blame] | 332 | synchronized (ContextImpl.class) { |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 333 | if (sSharedPrefs == null) { |
| 334 | sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>(); |
| 335 | } |
| 336 | |
| 337 | final String packageName = getPackageName(); |
| 338 | ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName); |
| 339 | if (packagePrefs == null) { |
| 340 | packagePrefs = new ArrayMap<String, SharedPreferencesImpl>(); |
| 341 | sSharedPrefs.put(packageName, packagePrefs); |
| 342 | } |
| 343 | |
Dianne Hackborn | ff32f35 | 2013-07-18 17:31:13 -0700 | [diff] [blame] | 344 | // At least one application in the world actually passes in a null |
| 345 | // name. This happened to work because when we generated the file name |
| 346 | // we would stringify it to "null.xml". Nice. |
| 347 | if (mPackageInfo.getApplicationInfo().targetSdkVersion < |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 348 | Build.VERSION_CODES.KITKAT) { |
Dianne Hackborn | ff32f35 | 2013-07-18 17:31:13 -0700 | [diff] [blame] | 349 | if (name == null) { |
| 350 | name = "null"; |
| 351 | } |
| 352 | } |
| 353 | |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 354 | sp = packagePrefs.get(name); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 355 | if (sp == null) { |
Brad Fitzpatrick | 4cd50b8 | 2010-12-01 17:31:45 -0800 | [diff] [blame] | 356 | File prefsFile = getSharedPrefsFile(name); |
| 357 | sp = new SharedPreferencesImpl(prefsFile, mode); |
Jeff Sharkey | 8e3ddab | 2013-06-17 18:26:37 -0700 | [diff] [blame] | 358 | packagePrefs.put(name, sp); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 359 | return sp; |
| 360 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 362 | if ((mode & Context.MODE_MULTI_PROCESS) != 0 || |
| 363 | getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { |
| 364 | // If somebody else (some other process) changed the prefs |
| 365 | // file behind our back, we reload it. This has been the |
| 366 | // historical (if undocumented) behavior. |
| 367 | sp.startReloadIfChangedUnexpectedly(); |
| 368 | } |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 369 | return sp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | private File getPreferencesDir() { |
| 373 | synchronized (mSync) { |
| 374 | if (mPreferencesDir == null) { |
| 375 | mPreferencesDir = new File(getDataDirFile(), "shared_prefs"); |
| 376 | } |
| 377 | return mPreferencesDir; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public FileInputStream openFileInput(String name) |
| 383 | throws FileNotFoundException { |
| 384 | File f = makeFilename(getFilesDir(), name); |
| 385 | return new FileInputStream(f); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public FileOutputStream openFileOutput(String name, int mode) |
| 390 | throws FileNotFoundException { |
| 391 | final boolean append = (mode&MODE_APPEND) != 0; |
| 392 | File f = makeFilename(getFilesDir(), name); |
| 393 | try { |
| 394 | FileOutputStream fos = new FileOutputStream(f, append); |
| 395 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 396 | return fos; |
| 397 | } catch (FileNotFoundException e) { |
| 398 | } |
| 399 | |
| 400 | File parent = f.getParentFile(); |
| 401 | parent.mkdir(); |
| 402 | FileUtils.setPermissions( |
| 403 | parent.getPath(), |
| 404 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 405 | -1, -1); |
| 406 | FileOutputStream fos = new FileOutputStream(f, append); |
| 407 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 408 | return fos; |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public boolean deleteFile(String name) { |
| 413 | File f = makeFilename(getFilesDir(), name); |
| 414 | return f.delete(); |
| 415 | } |
| 416 | |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 417 | // Common-path handling of app data dir creation |
| 418 | private static File createFilesDirLocked(File file) { |
| 419 | if (!file.exists()) { |
| 420 | if (!file.mkdirs()) { |
| 421 | if (file.exists()) { |
| 422 | // spurious failure; probably racing with another process for this app |
| 423 | return file; |
| 424 | } |
| 425 | Log.w(TAG, "Unable to create files subdir " + file.getPath()); |
| 426 | return null; |
| 427 | } |
| 428 | FileUtils.setPermissions( |
| 429 | file.getPath(), |
| 430 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 431 | -1, -1); |
| 432 | } |
| 433 | return file; |
| 434 | } |
| 435 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | @Override |
| 437 | public File getFilesDir() { |
| 438 | synchronized (mSync) { |
| 439 | if (mFilesDir == null) { |
| 440 | mFilesDir = new File(getDataDirFile(), "files"); |
| 441 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 442 | return createFilesDirLocked(mFilesDir); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | @Override |
| 447 | public File getNoBackupFilesDir() { |
| 448 | synchronized (mSync) { |
| 449 | if (mNoBackupFilesDir == null) { |
| 450 | mNoBackupFilesDir = new File(getDataDirFile(), "no_backup"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 452 | return createFilesDirLocked(mNoBackupFilesDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | } |
| 454 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 455 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | @Override |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 457 | public File getExternalFilesDir(String type) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 458 | // Operates on primary external storage |
| 459 | return getExternalFilesDirs(type)[0]; |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | public File[] getExternalFilesDirs(String type) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 464 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 465 | if (mExternalFilesDirs == null) { |
| 466 | mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName()); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 467 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 468 | |
| 469 | // Splice in requested type, if any |
| 470 | File[] dirs = mExternalFilesDirs; |
| 471 | if (type != null) { |
| 472 | dirs = Environment.buildPaths(dirs, type); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 473 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 474 | |
| 475 | // Create dirs if needed |
| 476 | return ensureDirsExistOrFilter(dirs); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 479 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 480 | @Override |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 481 | public File getObbDir() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 482 | // Operates on primary external storage |
| 483 | return getObbDirs()[0]; |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public File[] getObbDirs() { |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 488 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 489 | if (mExternalObbDirs == null) { |
| 490 | mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName()); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 491 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 492 | |
| 493 | // Create dirs if needed |
| 494 | return ensureDirsExistOrFilter(mExternalObbDirs); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | public File getCacheDir() { |
| 500 | synchronized (mSync) { |
| 501 | if (mCacheDir == null) { |
| 502 | mCacheDir = new File(getDataDirFile(), "cache"); |
| 503 | } |
Christopher Tate | a7835b6 | 2014-07-11 17:25:57 -0700 | [diff] [blame] | 504 | return createFilesDirLocked(mCacheDir); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 507 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 508 | @Override |
Jeff Sharkey | 4ed745d | 2014-07-15 20:39:15 -0700 | [diff] [blame] | 509 | public File getCodeCacheDir() { |
| 510 | synchronized (mSync) { |
| 511 | if (mCodeCacheDir == null) { |
| 512 | mCodeCacheDir = new File(getDataDirFile(), "code_cache"); |
| 513 | } |
| 514 | return createFilesDirLocked(mCodeCacheDir); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | @Override |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 519 | public File getExternalCacheDir() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 520 | // Operates on primary external storage |
| 521 | return getExternalCacheDirs()[0]; |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public File[] getExternalCacheDirs() { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 526 | synchronized (mSync) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 527 | if (mExternalCacheDirs == null) { |
| 528 | mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName()); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 529 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 530 | |
| 531 | // Create dirs if needed |
| 532 | return ensureDirsExistOrFilter(mExternalCacheDirs); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 535 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 536 | @Override |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 537 | public File[] getExternalMediaDirs() { |
| 538 | synchronized (mSync) { |
| 539 | if (mExternalMediaDirs == null) { |
| 540 | mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName()); |
| 541 | } |
| 542 | |
| 543 | // Create dirs if needed |
| 544 | return ensureDirsExistOrFilter(mExternalMediaDirs); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | public File getFileStreamPath(String name) { |
| 550 | return makeFilename(getFilesDir(), name); |
| 551 | } |
| 552 | |
| 553 | @Override |
| 554 | public String[] fileList() { |
| 555 | final String[] list = getFilesDir().list(); |
| 556 | return (list != null) ? list : EMPTY_FILE_LIST; |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 561 | return openOrCreateDatabase(name, mode, factory, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | @Override |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 565 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, |
| 566 | DatabaseErrorHandler errorHandler) { |
| 567 | File f = validateFilePath(name, true); |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 568 | int flags = SQLiteDatabase.CREATE_IF_NECESSARY; |
| 569 | if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { |
| 570 | flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; |
| 571 | } |
| 572 | SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 573 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 574 | return db; |
| 575 | } |
| 576 | |
| 577 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | public boolean deleteDatabase(String name) { |
| 579 | try { |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 580 | File f = validateFilePath(name, false); |
Jeff Brown | 79087e4 | 2012-03-01 19:52:44 -0800 | [diff] [blame] | 581 | return SQLiteDatabase.deleteDatabase(f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | } catch (Exception e) { |
| 583 | } |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | @Override |
| 588 | public File getDatabasePath(String name) { |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 589 | return validateFilePath(name, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | @Override |
| 593 | public String[] databaseList() { |
| 594 | final String[] list = getDatabasesDir().list(); |
| 595 | return (list != null) ? list : EMPTY_FILE_LIST; |
| 596 | } |
| 597 | |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | private File getDatabasesDir() { |
| 600 | synchronized (mSync) { |
| 601 | if (mDatabasesDir == null) { |
| 602 | mDatabasesDir = new File(getDataDirFile(), "databases"); |
| 603 | } |
| 604 | if (mDatabasesDir.getPath().equals("databases")) { |
| 605 | mDatabasesDir = new File("/data/system"); |
| 606 | } |
| 607 | return mDatabasesDir; |
| 608 | } |
| 609 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 610 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 612 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | public Drawable getWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 614 | return getWallpaperManager().getDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 618 | @Deprecated |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 619 | public Drawable peekWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 620 | return getWallpaperManager().peekDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 624 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | public int getWallpaperDesiredMinimumWidth() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 626 | return getWallpaperManager().getDesiredMinimumWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 630 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 631 | public int getWallpaperDesiredMinimumHeight() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 632 | return getWallpaperManager().getDesiredMinimumHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 636 | @Deprecated |
| 637 | public void setWallpaper(Bitmap bitmap) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 638 | getWallpaperManager().setBitmap(bitmap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 642 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | public void setWallpaper(InputStream data) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 644 | getWallpaperManager().setStream(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 648 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | public void clearWallpaper() throws IOException { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 650 | getWallpaperManager().clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 653 | private WallpaperManager getWallpaperManager() { |
| 654 | return getSystemService(WallpaperManager.class); |
| 655 | } |
| 656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | @Override |
| 658 | public void startActivity(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 659 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 660 | startActivity(intent, null); |
| 661 | } |
| 662 | |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 663 | /** @hide */ |
| 664 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 665 | public void startActivityAsUser(Intent intent, UserHandle user) { |
Dianne Hackborn | f1c26e2 | 2012-08-23 13:54:58 -0700 | [diff] [blame] | 666 | startActivityAsUser(intent, null, user); |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 669 | @Override |
| 670 | public void startActivity(Intent intent, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 671 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 673 | throw new AndroidRuntimeException( |
| 674 | "Calling startActivity() from outside of an Activity " |
| 675 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag." |
| 676 | + " Is this really what you want?"); |
| 677 | } |
| 678 | mMainThread.getInstrumentation().execStartActivity( |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 679 | getOuterContext(), mMainThread.getApplicationThread(), null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 680 | (Activity)null, intent, -1, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 683 | /** @hide */ |
| 684 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 685 | public void startActivityAsUser(Intent intent, Bundle options, UserHandle user) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 686 | try { |
| 687 | ActivityManagerNative.getDefault().startActivityAsUser( |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 688 | mMainThread.getApplicationThread(), getBasePackageName(), intent, |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 689 | intent.resolveTypeIfNeeded(getContentResolver()), |
Jeff Hao | 1b012d3 | 2014-08-20 10:35:34 -0700 | [diff] [blame] | 690 | null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, options, |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 691 | user.getIdentifier()); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 692 | } catch (RemoteException e) { |
| 693 | throw new RuntimeException("Failure from system", e); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | @Override |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 698 | public void startActivities(Intent[] intents) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 699 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 700 | startActivities(intents, null); |
| 701 | } |
| 702 | |
Amith Yamasani | ea7e915 | 2012-09-24 16:11:18 -0700 | [diff] [blame] | 703 | /** @hide */ |
| 704 | @Override |
| 705 | public void startActivitiesAsUser(Intent[] intents, Bundle options, UserHandle userHandle) { |
| 706 | if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 707 | throw new AndroidRuntimeException( |
| 708 | "Calling startActivities() from outside of an Activity " |
| 709 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent." |
| 710 | + " Is this really what you want?"); |
| 711 | } |
| 712 | mMainThread.getInstrumentation().execStartActivitiesAsUser( |
| 713 | getOuterContext(), mMainThread.getApplicationThread(), null, |
| 714 | (Activity)null, intents, options, userHandle.getIdentifier()); |
| 715 | } |
| 716 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 717 | @Override |
| 718 | public void startActivities(Intent[] intents, Bundle options) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 719 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 720 | if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 721 | throw new AndroidRuntimeException( |
| 722 | "Calling startActivities() from outside of an Activity " |
| 723 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent." |
| 724 | + " Is this really what you want?"); |
| 725 | } |
| 726 | mMainThread.getInstrumentation().execStartActivities( |
| 727 | getOuterContext(), mMainThread.getApplicationThread(), null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 728 | (Activity)null, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | @Override |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 732 | public void startIntentSender(IntentSender intent, |
| 733 | Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
| 734 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 735 | startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null); |
| 736 | } |
| 737 | |
| 738 | @Override |
| 739 | public void startIntentSender(IntentSender intent, Intent fillInIntent, |
| 740 | int flagsMask, int flagsValues, int extraFlags, Bundle options) |
| 741 | throws IntentSender.SendIntentException { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 742 | try { |
| 743 | String resolvedType = null; |
| 744 | if (fillInIntent != null) { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 745 | fillInIntent.migrateExtraStreamToClipData(); |
| 746 | fillInIntent.prepareToLeaveProcess(); |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 747 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 748 | } |
| 749 | int result = ActivityManagerNative.getDefault() |
| 750 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
| 751 | fillInIntent, resolvedType, null, null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 752 | 0, flagsMask, flagsValues, options); |
| 753 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 754 | throw new IntentSender.SendIntentException(); |
| 755 | } |
| 756 | Instrumentation.checkStartActivityResult(result, null); |
| 757 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 758 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 761 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 762 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | public void sendBroadcast(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 764 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 766 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 767 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 768 | ActivityManagerNative.getDefault().broadcastIntent( |
| 769 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 770 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, false, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 771 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 773 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Amith Yamasani | 67cf7d3 | 2012-02-16 14:31:23 -0800 | [diff] [blame] | 777 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | public void sendBroadcast(Intent intent, String receiverPermission) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 779 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 780 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 781 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 782 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | ActivityManagerNative.getDefault().broadcastIntent( |
| 784 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 785 | Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, |
| 786 | false, false, getUserId()); |
| 787 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 788 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | @Override |
| 793 | public void sendBroadcast(Intent intent, String receiverPermission, int appOp) { |
| 794 | warnIfCallingFromSystemProcess(); |
| 795 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 796 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 797 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 798 | ActivityManagerNative.getDefault().broadcastIntent( |
| 799 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 800 | Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 801 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 803 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | } |
| 805 | } |
| 806 | |
| 807 | @Override |
| 808 | public void sendOrderedBroadcast(Intent intent, |
| 809 | String receiverPermission) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 810 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 812 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 813 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | ActivityManagerNative.getDefault().broadcastIntent( |
| 815 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 816 | Activity.RESULT_OK, null, null, receiverPermission, AppOpsManager.OP_NONE, true, false, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 817 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 819 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
| 823 | @Override |
| 824 | public void sendOrderedBroadcast(Intent intent, |
| 825 | String receiverPermission, BroadcastReceiver resultReceiver, |
| 826 | Handler scheduler, int initialCode, String initialData, |
| 827 | Bundle initialExtras) { |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 828 | sendOrderedBroadcast(intent, receiverPermission, AppOpsManager.OP_NONE, |
| 829 | resultReceiver, scheduler, initialCode, initialData, initialExtras); |
| 830 | } |
| 831 | |
| 832 | @Override |
| 833 | public void sendOrderedBroadcast(Intent intent, |
| 834 | String receiverPermission, int appOp, BroadcastReceiver resultReceiver, |
| 835 | Handler scheduler, int initialCode, String initialData, |
| 836 | Bundle initialExtras) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 837 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | IIntentReceiver rd = null; |
| 839 | if (resultReceiver != null) { |
| 840 | if (mPackageInfo != null) { |
| 841 | if (scheduler == null) { |
| 842 | scheduler = mMainThread.getHandler(); |
| 843 | } |
| 844 | rd = mPackageInfo.getReceiverDispatcher( |
| 845 | resultReceiver, getOuterContext(), scheduler, |
| 846 | mMainThread.getInstrumentation(), false); |
| 847 | } else { |
| 848 | if (scheduler == null) { |
| 849 | scheduler = mMainThread.getHandler(); |
| 850 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 851 | rd = new LoadedApk.ReceiverDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 853 | } |
| 854 | } |
| 855 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 856 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 857 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | ActivityManagerNative.getDefault().broadcastIntent( |
| 859 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 860 | initialCode, initialData, initialExtras, receiverPermission, appOp, |
| 861 | true, false, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 863 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
| 867 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 868 | public void sendBroadcastAsUser(Intent intent, UserHandle user) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 869 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 870 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 871 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 872 | ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(), |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 873 | intent, resolvedType, null, Activity.RESULT_OK, null, null, null, |
| 874 | AppOpsManager.OP_NONE, false, false, user.getIdentifier()); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 875 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 876 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
| 880 | @Override |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 881 | public void sendBroadcastAsUser(Intent intent, UserHandle user, |
| 882 | String receiverPermission) { |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 883 | sendBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE); |
| 884 | } |
| 885 | |
| 886 | @Override |
| 887 | public void sendBroadcastAsUser(Intent intent, UserHandle user, |
| 888 | String receiverPermission, int appOp) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 889 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 890 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 891 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 892 | ActivityManagerNative.getDefault().broadcastIntent( |
Svet Ganov | 16a1689 | 2015-04-16 10:32:04 -0700 | [diff] [blame] | 893 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
| 894 | Activity.RESULT_OK, null, null, receiverPermission, appOp, false, false, |
| 895 | user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 896 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 897 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
| 901 | @Override |
Dianne Hackborn | 79af1dd | 2012-08-16 16:42:52 -0700 | [diff] [blame] | 902 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 903 | String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 904 | int initialCode, String initialData, Bundle initialExtras) { |
Amith Yamasani | 3cf7572 | 2014-05-16 12:37:29 -0700 | [diff] [blame] | 905 | sendOrderedBroadcastAsUser(intent, user, receiverPermission, AppOpsManager.OP_NONE, |
| 906 | resultReceiver, scheduler, initialCode, initialData, initialExtras); |
| 907 | } |
| 908 | |
| 909 | @Override |
| 910 | public void sendOrderedBroadcastAsUser(Intent intent, UserHandle user, |
| 911 | String receiverPermission, int appOp, BroadcastReceiver resultReceiver, |
| 912 | Handler scheduler, |
| 913 | int initialCode, String initialData, Bundle initialExtras) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 914 | IIntentReceiver rd = null; |
| 915 | if (resultReceiver != null) { |
| 916 | if (mPackageInfo != null) { |
| 917 | if (scheduler == null) { |
| 918 | scheduler = mMainThread.getHandler(); |
| 919 | } |
| 920 | rd = mPackageInfo.getReceiverDispatcher( |
| 921 | resultReceiver, getOuterContext(), scheduler, |
| 922 | mMainThread.getInstrumentation(), false); |
| 923 | } else { |
| 924 | if (scheduler == null) { |
| 925 | scheduler = mMainThread.getHandler(); |
| 926 | } |
| 927 | rd = new LoadedApk.ReceiverDispatcher( |
| 928 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 929 | } |
| 930 | } |
| 931 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 932 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 933 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 934 | ActivityManagerNative.getDefault().broadcastIntent( |
| 935 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 936 | initialCode, initialData, initialExtras, receiverPermission, |
Amith Yamasani | 0261b9b | 2014-05-20 16:57:26 -0700 | [diff] [blame] | 937 | appOp, true, false, user.getIdentifier()); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 938 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 939 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | |
| 943 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 944 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | public void sendStickyBroadcast(Intent intent) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 946 | warnIfCallingFromSystemProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 948 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 949 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | ActivityManagerNative.getDefault().broadcastIntent( |
| 951 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 952 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 953 | getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 955 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
| 959 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 960 | @Deprecated |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 961 | public void sendStickyOrderedBroadcast(Intent intent, |
| 962 | BroadcastReceiver resultReceiver, |
| 963 | Handler scheduler, int initialCode, String initialData, |
| 964 | Bundle initialExtras) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 965 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 966 | IIntentReceiver rd = null; |
| 967 | if (resultReceiver != null) { |
| 968 | if (mPackageInfo != null) { |
| 969 | if (scheduler == null) { |
| 970 | scheduler = mMainThread.getHandler(); |
| 971 | } |
| 972 | rd = mPackageInfo.getReceiverDispatcher( |
| 973 | resultReceiver, getOuterContext(), scheduler, |
| 974 | mMainThread.getInstrumentation(), false); |
| 975 | } else { |
| 976 | if (scheduler == null) { |
| 977 | scheduler = mMainThread.getHandler(); |
| 978 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 979 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 980 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 981 | } |
| 982 | } |
| 983 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 984 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 985 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 986 | ActivityManagerNative.getDefault().broadcastIntent( |
| 987 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 988 | initialCode, initialData, initialExtras, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 989 | AppOpsManager.OP_NONE, true, true, getUserId()); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 990 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 991 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | |
| 995 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 996 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | public void removeStickyBroadcast(Intent intent) { |
| 998 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 999 | if (resolvedType != null) { |
| 1000 | intent = new Intent(intent); |
| 1001 | intent.setDataAndType(intent.getData(), resolvedType); |
| 1002 | } |
| 1003 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1004 | intent.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | ActivityManagerNative.getDefault().unbroadcastIntent( |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1006 | mMainThread.getApplicationThread(), intent, getUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1008 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
| 1012 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1013 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1014 | public void sendStickyBroadcastAsUser(Intent intent, UserHandle user) { |
| 1015 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1016 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1017 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1018 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1019 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1020 | Activity.RESULT_OK, null, null, null, AppOpsManager.OP_NONE, false, true, user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1021 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1022 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1027 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1028 | public void sendStickyOrderedBroadcastAsUser(Intent intent, |
| 1029 | UserHandle user, BroadcastReceiver resultReceiver, |
| 1030 | Handler scheduler, int initialCode, String initialData, |
| 1031 | Bundle initialExtras) { |
| 1032 | IIntentReceiver rd = null; |
| 1033 | if (resultReceiver != null) { |
| 1034 | if (mPackageInfo != null) { |
| 1035 | if (scheduler == null) { |
| 1036 | scheduler = mMainThread.getHandler(); |
| 1037 | } |
| 1038 | rd = mPackageInfo.getReceiverDispatcher( |
| 1039 | resultReceiver, getOuterContext(), scheduler, |
| 1040 | mMainThread.getInstrumentation(), false); |
| 1041 | } else { |
| 1042 | if (scheduler == null) { |
| 1043 | scheduler = mMainThread.getHandler(); |
| 1044 | } |
| 1045 | rd = new LoadedApk.ReceiverDispatcher( |
| 1046 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1047 | } |
| 1048 | } |
| 1049 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1050 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1051 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1052 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1053 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1054 | initialCode, initialData, initialExtras, null, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 1055 | AppOpsManager.OP_NONE, true, true, user.getIdentifier()); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1056 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1057 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | @Override |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1062 | @Deprecated |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1063 | public void removeStickyBroadcastAsUser(Intent intent, UserHandle user) { |
| 1064 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1065 | if (resolvedType != null) { |
| 1066 | intent = new Intent(intent); |
| 1067 | intent.setDataAndType(intent.getData(), resolvedType); |
| 1068 | } |
| 1069 | try { |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1070 | intent.prepareToLeaveProcess(); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1071 | ActivityManagerNative.getDefault().unbroadcastIntent( |
| 1072 | mMainThread.getApplicationThread(), intent, user.getIdentifier()); |
| 1073 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1074 | throw new RuntimeException("Failure from system", e); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 1080 | return registerReceiver(receiver, filter, null, null); |
| 1081 | } |
| 1082 | |
| 1083 | @Override |
| 1084 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 1085 | String broadcastPermission, Handler scheduler) { |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1086 | return registerReceiverInternal(receiver, getUserId(), |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1087 | filter, broadcastPermission, scheduler, getOuterContext()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1090 | @Override |
| 1091 | public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user, |
| 1092 | IntentFilter filter, String broadcastPermission, Handler scheduler) { |
| 1093 | return registerReceiverInternal(receiver, user.getIdentifier(), |
| 1094 | filter, broadcastPermission, scheduler, getOuterContext()); |
| 1095 | } |
| 1096 | |
| 1097 | private Intent registerReceiverInternal(BroadcastReceiver receiver, int userId, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1098 | IntentFilter filter, String broadcastPermission, |
| 1099 | Handler scheduler, Context context) { |
| 1100 | IIntentReceiver rd = null; |
| 1101 | if (receiver != null) { |
| 1102 | if (mPackageInfo != null && context != null) { |
| 1103 | if (scheduler == null) { |
| 1104 | scheduler = mMainThread.getHandler(); |
| 1105 | } |
| 1106 | rd = mPackageInfo.getReceiverDispatcher( |
| 1107 | receiver, context, scheduler, |
| 1108 | mMainThread.getInstrumentation(), true); |
| 1109 | } else { |
| 1110 | if (scheduler == null) { |
| 1111 | scheduler = mMainThread.getHandler(); |
| 1112 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1113 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 1114 | receiver, context, scheduler, null, true).getIIntentReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | try { |
| 1118 | return ActivityManagerNative.getDefault().registerReceiver( |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1119 | mMainThread.getApplicationThread(), mBasePackageName, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 1120 | rd, filter, broadcastPermission, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | } catch (RemoteException e) { |
| 1122 | return null; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | @Override |
| 1127 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 1128 | if (mPackageInfo != null) { |
| 1129 | IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher( |
| 1130 | getOuterContext(), receiver); |
| 1131 | try { |
| 1132 | ActivityManagerNative.getDefault().unregisterReceiver(rd); |
| 1133 | } catch (RemoteException e) { |
| 1134 | } |
| 1135 | } else { |
| 1136 | throw new RuntimeException("Not supported in system context"); |
| 1137 | } |
| 1138 | } |
| 1139 | |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1140 | private void validateServiceIntent(Intent service) { |
| 1141 | if (service.getComponent() == null && service.getPackage() == null) { |
Dianne Hackborn | 955d8d6 | 2014-10-07 20:17:19 -0700 | [diff] [blame] | 1142 | if (getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) { |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1143 | IllegalArgumentException ex = new IllegalArgumentException( |
| 1144 | "Service Intent must be explicit: " + service); |
| 1145 | throw ex; |
| 1146 | } else { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1147 | Log.w(TAG, "Implicit intents with startService are not safe: " + service |
| 1148 | + " " + Debug.getCallers(2, 3)); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | @Override |
| 1154 | public ComponentName startService(Intent service) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1155 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1156 | return startServiceCommon(service, mUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | @Override |
| 1160 | public boolean stopService(Intent service) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1161 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1162 | return stopServiceCommon(service, mUser); |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | @Override |
| 1166 | public ComponentName startServiceAsUser(Intent service, UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1167 | return startServiceCommon(service, user); |
| 1168 | } |
| 1169 | |
| 1170 | private ComponentName startServiceCommon(Intent service, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1171 | try { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1172 | validateServiceIntent(service); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1173 | service.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | ComponentName cn = ActivityManagerNative.getDefault().startService( |
| 1175 | mMainThread.getApplicationThread(), service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1176 | service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier()); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1177 | if (cn != null) { |
| 1178 | if (cn.getPackageName().equals("!")) { |
| 1179 | throw new SecurityException( |
| 1180 | "Not allowed to start service " + service |
| 1181 | + " without permission " + cn.getClassName()); |
| 1182 | } else if (cn.getPackageName().equals("!!")) { |
| 1183 | throw new SecurityException( |
| 1184 | "Unable to start service " + service |
| 1185 | + ": " + cn.getClassName()); |
| 1186 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | } |
| 1188 | return cn; |
| 1189 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1190 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | @Override |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1195 | public boolean stopServiceAsUser(Intent service, UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1196 | return stopServiceCommon(service, user); |
| 1197 | } |
| 1198 | |
| 1199 | private boolean stopServiceCommon(Intent service, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | try { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1201 | validateServiceIntent(service); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1202 | service.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | int res = ActivityManagerNative.getDefault().stopService( |
| 1204 | mMainThread.getApplicationThread(), service, |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1205 | service.resolveTypeIfNeeded(getContentResolver()), user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | if (res < 0) { |
| 1207 | throw new SecurityException( |
| 1208 | "Not allowed to stop service " + service); |
| 1209 | } |
| 1210 | return res != 0; |
| 1211 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1212 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | @Override |
| 1217 | public boolean bindService(Intent service, ServiceConnection conn, |
| 1218 | int flags) { |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1219 | warnIfCallingFromSystemProcess(); |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1220 | return bindServiceCommon(service, conn, flags, Process.myUserHandle()); |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | /** @hide */ |
| 1224 | @Override |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1225 | public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, |
| 1226 | UserHandle user) { |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1227 | return bindServiceCommon(service, conn, flags, user); |
| 1228 | } |
| 1229 | |
| 1230 | private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, |
| 1231 | UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1232 | IServiceConnection sd; |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1233 | if (conn == null) { |
| 1234 | throw new IllegalArgumentException("connection is null"); |
| 1235 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | if (mPackageInfo != null) { |
| 1237 | sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), |
| 1238 | mMainThread.getHandler(), flags); |
| 1239 | } else { |
| 1240 | throw new RuntimeException("Not supported in system context"); |
| 1241 | } |
Dianne Hackborn | fd6c7b1 | 2013-10-03 10:42:26 -0700 | [diff] [blame] | 1242 | validateServiceIntent(service); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1244 | IBinder token = getActivityToken(); |
| 1245 | if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null |
| 1246 | && mPackageInfo.getApplicationInfo().targetSdkVersion |
| 1247 | < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 1248 | flags |= BIND_WAIVE_PRIORITY; |
| 1249 | } |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 1250 | service.prepareToLeaveProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | int res = ActivityManagerNative.getDefault().bindService( |
| 1252 | mMainThread.getApplicationThread(), getActivityToken(), |
| 1253 | service, service.resolveTypeIfNeeded(getContentResolver()), |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1254 | sd, flags, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | if (res < 0) { |
| 1256 | throw new SecurityException( |
| 1257 | "Not allowed to bind to service " + service); |
| 1258 | } |
| 1259 | return res != 0; |
| 1260 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1261 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | @Override |
| 1266 | public void unbindService(ServiceConnection conn) { |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1267 | if (conn == null) { |
| 1268 | throw new IllegalArgumentException("connection is null"); |
| 1269 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | if (mPackageInfo != null) { |
| 1271 | IServiceConnection sd = mPackageInfo.forgetServiceDispatcher( |
| 1272 | getOuterContext(), conn); |
| 1273 | try { |
| 1274 | ActivityManagerNative.getDefault().unbindService(sd); |
| 1275 | } catch (RemoteException e) { |
| 1276 | } |
| 1277 | } else { |
| 1278 | throw new RuntimeException("Not supported in system context"); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | @Override |
| 1283 | public boolean startInstrumentation(ComponentName className, |
| 1284 | String profileFile, Bundle arguments) { |
| 1285 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1286 | if (arguments != null) { |
| 1287 | arguments.setAllowFds(false); |
| 1288 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | return ActivityManagerNative.getDefault().startInstrumentation( |
Narayan Kamath | 8dcfefd | 2014-05-15 18:12:59 +0100 | [diff] [blame] | 1290 | className, profileFile, 0, arguments, null, null, getUserId(), |
| 1291 | null /* ABI override */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | } catch (RemoteException e) { |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1293 | throw new RuntimeException("Failure from system", e); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | @Override |
| 1298 | public Object getSystemService(String name) { |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1299 | return SystemServiceRegistry.getSystemService(this, name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1302 | @Override |
| 1303 | public String getSystemServiceName(Class<?> serviceClass) { |
| 1304 | return SystemServiceRegistry.getSystemServiceName(serviceClass); |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | @Override |
| 1308 | public int checkPermission(String permission, int pid, int uid) { |
| 1309 | if (permission == null) { |
| 1310 | throw new IllegalArgumentException("permission is null"); |
| 1311 | } |
| 1312 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1313 | try { |
| 1314 | return ActivityManagerNative.getDefault().checkPermission( |
| 1315 | permission, pid, uid); |
| 1316 | } catch (RemoteException e) { |
| 1317 | return PackageManager.PERMISSION_DENIED; |
| 1318 | } |
| 1319 | } |
| 1320 | |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 1321 | /** @hide */ |
| 1322 | @Override |
| 1323 | public int checkPermission(String permission, int pid, int uid, IBinder callerToken) { |
| 1324 | if (permission == null) { |
| 1325 | throw new IllegalArgumentException("permission is null"); |
| 1326 | } |
| 1327 | |
| 1328 | try { |
| 1329 | return ActivityManagerNative.getDefault().checkPermissionWithToken( |
| 1330 | permission, pid, uid, callerToken); |
| 1331 | } catch (RemoteException e) { |
| 1332 | return PackageManager.PERMISSION_DENIED; |
| 1333 | } |
| 1334 | } |
| 1335 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1336 | @Override |
| 1337 | public int checkCallingPermission(String permission) { |
| 1338 | if (permission == null) { |
| 1339 | throw new IllegalArgumentException("permission is null"); |
| 1340 | } |
| 1341 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | int pid = Binder.getCallingPid(); |
| 1343 | if (pid != Process.myPid()) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1344 | return checkPermission(permission, pid, Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1345 | } |
| 1346 | return PackageManager.PERMISSION_DENIED; |
| 1347 | } |
| 1348 | |
| 1349 | @Override |
| 1350 | public int checkCallingOrSelfPermission(String permission) { |
| 1351 | if (permission == null) { |
| 1352 | throw new IllegalArgumentException("permission is null"); |
| 1353 | } |
| 1354 | |
| 1355 | return checkPermission(permission, Binder.getCallingPid(), |
| 1356 | Binder.getCallingUid()); |
| 1357 | } |
| 1358 | |
Svetoslav | c6d1c34 | 2015-02-26 14:44:43 -0800 | [diff] [blame] | 1359 | @Override |
| 1360 | public int checkSelfPermission(String permission) { |
| 1361 | if (permission == null) { |
| 1362 | throw new IllegalArgumentException("permission is null"); |
| 1363 | } |
| 1364 | |
| 1365 | return checkPermission(permission, Process.myPid(), Process.myUid()); |
| 1366 | } |
| 1367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1368 | private void enforce( |
| 1369 | String permission, int resultOfCheck, |
| 1370 | boolean selfToo, int uid, String message) { |
| 1371 | if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { |
| 1372 | throw new SecurityException( |
| 1373 | (message != null ? (message + ": ") : "") + |
| 1374 | (selfToo |
| 1375 | ? "Neither user " + uid + " nor current process has " |
Christopher Tate | 4dc7a68 | 2012-09-11 12:15:49 -0700 | [diff] [blame] | 1376 | : "uid " + uid + " does not have ") + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | permission + |
| 1378 | "."); |
| 1379 | } |
| 1380 | } |
| 1381 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1382 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1383 | public void enforcePermission( |
| 1384 | String permission, int pid, int uid, String message) { |
| 1385 | enforce(permission, |
| 1386 | checkPermission(permission, pid, uid), |
| 1387 | false, |
| 1388 | uid, |
| 1389 | message); |
| 1390 | } |
| 1391 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1392 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | public void enforceCallingPermission(String permission, String message) { |
| 1394 | enforce(permission, |
| 1395 | checkCallingPermission(permission), |
| 1396 | false, |
| 1397 | Binder.getCallingUid(), |
| 1398 | message); |
| 1399 | } |
| 1400 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1401 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | public void enforceCallingOrSelfPermission( |
| 1403 | String permission, String message) { |
| 1404 | enforce(permission, |
| 1405 | checkCallingOrSelfPermission(permission), |
| 1406 | true, |
| 1407 | Binder.getCallingUid(), |
| 1408 | message); |
| 1409 | } |
| 1410 | |
| 1411 | @Override |
| 1412 | public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { |
| 1413 | try { |
| 1414 | ActivityManagerNative.getDefault().grantUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1415 | mMainThread.getApplicationThread(), toPackage, |
| 1416 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1417 | } catch (RemoteException e) { |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | @Override |
| 1422 | public void revokeUriPermission(Uri uri, int modeFlags) { |
| 1423 | try { |
| 1424 | ActivityManagerNative.getDefault().revokeUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1425 | mMainThread.getApplicationThread(), |
| 1426 | ContentProvider.getUriWithoutUserId(uri), modeFlags, resolveUserId(uri)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | } catch (RemoteException e) { |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | @Override |
| 1432 | 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] | 1433 | try { |
| 1434 | return ActivityManagerNative.getDefault().checkUriPermission( |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1435 | ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags, |
Dianne Hackborn | ff17024 | 2014-11-19 10:59:01 -0800 | [diff] [blame] | 1436 | resolveUserId(uri), null); |
| 1437 | } catch (RemoteException e) { |
| 1438 | return PackageManager.PERMISSION_DENIED; |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /** @hide */ |
| 1443 | @Override |
| 1444 | public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags, IBinder callerToken) { |
| 1445 | try { |
| 1446 | return ActivityManagerNative.getDefault().checkUriPermission( |
| 1447 | ContentProvider.getUriWithoutUserId(uri), pid, uid, modeFlags, |
| 1448 | resolveUserId(uri), callerToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1449 | } catch (RemoteException e) { |
| 1450 | return PackageManager.PERMISSION_DENIED; |
| 1451 | } |
| 1452 | } |
| 1453 | |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1454 | private int resolveUserId(Uri uri) { |
| 1455 | return ContentProvider.getUserIdFromUri(uri, getUserId()); |
| 1456 | } |
| 1457 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1458 | @Override |
| 1459 | public int checkCallingUriPermission(Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | int pid = Binder.getCallingPid(); |
| 1461 | if (pid != Process.myPid()) { |
| 1462 | return checkUriPermission(uri, pid, |
| 1463 | Binder.getCallingUid(), modeFlags); |
| 1464 | } |
| 1465 | return PackageManager.PERMISSION_DENIED; |
| 1466 | } |
| 1467 | |
| 1468 | @Override |
| 1469 | public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { |
| 1470 | return checkUriPermission(uri, Binder.getCallingPid(), |
| 1471 | Binder.getCallingUid(), modeFlags); |
| 1472 | } |
| 1473 | |
| 1474 | @Override |
| 1475 | public int checkUriPermission(Uri uri, String readPermission, |
| 1476 | String writePermission, int pid, int uid, int modeFlags) { |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 1477 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission=" |
| 1479 | + readPermission + " writePermission=" + writePermission |
| 1480 | + " pid=" + pid + " uid=" + uid + " mode" + modeFlags); |
| 1481 | } |
| 1482 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 1483 | if (readPermission == null |
| 1484 | || checkPermission(readPermission, pid, uid) |
| 1485 | == PackageManager.PERMISSION_GRANTED) { |
| 1486 | return PackageManager.PERMISSION_GRANTED; |
| 1487 | } |
| 1488 | } |
| 1489 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 1490 | if (writePermission == null |
| 1491 | || checkPermission(writePermission, pid, uid) |
| 1492 | == PackageManager.PERMISSION_GRANTED) { |
| 1493 | return PackageManager.PERMISSION_GRANTED; |
| 1494 | } |
| 1495 | } |
| 1496 | return uri != null ? checkUriPermission(uri, pid, uid, modeFlags) |
| 1497 | : PackageManager.PERMISSION_DENIED; |
| 1498 | } |
| 1499 | |
| 1500 | private String uriModeFlagToString(int uriModeFlags) { |
Jeff Sharkey | 846318a | 2014-04-04 12:12:41 -0700 | [diff] [blame] | 1501 | StringBuilder builder = new StringBuilder(); |
| 1502 | if ((uriModeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 1503 | builder.append("read and "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | } |
Jeff Sharkey | 846318a | 2014-04-04 12:12:41 -0700 | [diff] [blame] | 1505 | if ((uriModeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 1506 | builder.append("write and "); |
| 1507 | } |
| 1508 | if ((uriModeFlags & Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) != 0) { |
| 1509 | builder.append("persistable and "); |
| 1510 | } |
| 1511 | if ((uriModeFlags & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0) { |
| 1512 | builder.append("prefix and "); |
| 1513 | } |
| 1514 | |
| 1515 | if (builder.length() > 5) { |
| 1516 | builder.setLength(builder.length() - 5); |
| 1517 | return builder.toString(); |
| 1518 | } else { |
| 1519 | throw new IllegalArgumentException("Unknown permission mode flags: " + uriModeFlags); |
| 1520 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | private void enforceForUri( |
| 1524 | int modeFlags, int resultOfCheck, boolean selfToo, |
| 1525 | int uid, Uri uri, String message) { |
| 1526 | if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { |
| 1527 | throw new SecurityException( |
| 1528 | (message != null ? (message + ": ") : "") + |
| 1529 | (selfToo |
| 1530 | ? "Neither user " + uid + " nor current process has " |
| 1531 | : "User " + uid + " does not have ") + |
| 1532 | uriModeFlagToString(modeFlags) + |
| 1533 | " permission on " + |
| 1534 | uri + |
| 1535 | "."); |
| 1536 | } |
| 1537 | } |
| 1538 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1539 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1540 | public void enforceUriPermission( |
| 1541 | Uri uri, int pid, int uid, int modeFlags, String message) { |
| 1542 | enforceForUri( |
| 1543 | modeFlags, checkUriPermission(uri, pid, uid, modeFlags), |
| 1544 | false, uid, uri, message); |
| 1545 | } |
| 1546 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1547 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1548 | public void enforceCallingUriPermission( |
| 1549 | Uri uri, int modeFlags, String message) { |
| 1550 | enforceForUri( |
| 1551 | modeFlags, checkCallingUriPermission(uri, modeFlags), |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1552 | false, |
| 1553 | Binder.getCallingUid(), uri, message); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1554 | } |
| 1555 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1556 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1557 | public void enforceCallingOrSelfUriPermission( |
| 1558 | Uri uri, int modeFlags, String message) { |
| 1559 | enforceForUri( |
| 1560 | modeFlags, |
| 1561 | checkCallingOrSelfUriPermission(uri, modeFlags), true, |
| 1562 | Binder.getCallingUid(), uri, message); |
| 1563 | } |
| 1564 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1565 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1566 | public void enforceUriPermission( |
| 1567 | Uri uri, String readPermission, String writePermission, |
| 1568 | int pid, int uid, int modeFlags, String message) { |
| 1569 | enforceForUri(modeFlags, |
| 1570 | checkUriPermission( |
| 1571 | uri, readPermission, writePermission, pid, uid, |
| 1572 | modeFlags), |
| 1573 | false, |
| 1574 | uid, |
| 1575 | uri, |
| 1576 | message); |
| 1577 | } |
| 1578 | |
Tom O'Neill | 365632e | 2013-09-09 09:34:58 -0700 | [diff] [blame] | 1579 | /** |
| 1580 | * Logs a warning if the system process directly called a method such as |
| 1581 | * {@link #startService(Intent)} instead of {@link #startServiceAsUser(Intent, UserHandle)}. |
| 1582 | * The "AsUser" variants allow us to properly enforce the user's restrictions. |
| 1583 | */ |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1584 | private void warnIfCallingFromSystemProcess() { |
| 1585 | if (Process.myUid() == Process.SYSTEM_UID) { |
| 1586 | Slog.w(TAG, "Calling a method in the system process without a qualified user: " |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 1587 | + Debug.getCallers(5)); |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | @Override |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 1592 | public Context createApplicationContext(ApplicationInfo application, int flags) |
| 1593 | throws NameNotFoundException { |
| 1594 | LoadedApk pi = mMainThread.getPackageInfo(application, mResources.getCompatibilityInfo(), |
| 1595 | flags | CONTEXT_REGISTER_PACKAGE); |
| 1596 | if (pi != null) { |
| 1597 | final boolean restricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED; |
| 1598 | ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken, |
| 1599 | new UserHandle(UserHandle.getUserId(application.uid)), restricted, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1600 | mDisplay, null, Display.INVALID_DISPLAY); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 1601 | if (c.mResources != null) { |
| 1602 | return c; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | throw new PackageManager.NameNotFoundException( |
| 1607 | "Application package " + application.packageName + " not found"); |
| 1608 | } |
| 1609 | |
| 1610 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1611 | public Context createPackageContext(String packageName, int flags) |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1612 | throws NameNotFoundException { |
Amith Yamasani | 64442c1 | 2012-10-07 08:17:46 -0700 | [diff] [blame] | 1613 | return createPackageContextAsUser(packageName, flags, |
| 1614 | mUser != null ? mUser : Process.myUserHandle()); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | @Override |
| 1618 | public Context createPackageContextAsUser(String packageName, int flags, UserHandle user) |
| 1619 | throws NameNotFoundException { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1620 | final boolean restricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED; |
Kenny Guy | c2a4060 | 2014-09-08 18:51:15 +0000 | [diff] [blame] | 1621 | if (packageName.equals("system") || packageName.equals("android")) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1622 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1623 | user, restricted, mDisplay, null, Display.INVALID_DISPLAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | } |
| 1625 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1626 | LoadedApk pi = mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 1627 | flags | CONTEXT_REGISTER_PACKAGE, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | if (pi != null) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1629 | ContextImpl c = new ContextImpl(this, mMainThread, pi, mActivityToken, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1630 | user, restricted, mDisplay, null, Display.INVALID_DISPLAY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | if (c.mResources != null) { |
| 1632 | return c; |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | // Should be a better exception. |
| 1637 | throw new PackageManager.NameNotFoundException( |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1638 | "Application package " + packageName + " not found"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1639 | } |
| 1640 | |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 1641 | @Override |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 1642 | public Context createConfigurationContext(Configuration overrideConfiguration) { |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 1643 | if (overrideConfiguration == null) { |
| 1644 | throw new IllegalArgumentException("overrideConfiguration must not be null"); |
| 1645 | } |
| 1646 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1647 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1648 | mUser, mRestricted, mDisplay, overrideConfiguration, Display.INVALID_DISPLAY); |
Dianne Hackborn | 756220b | 2012-08-14 16:45:30 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | @Override |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 1652 | public Context createDisplayContext(Display display) { |
| 1653 | if (display == null) { |
| 1654 | throw new IllegalArgumentException("display must not be null"); |
| 1655 | } |
| 1656 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1657 | return new ContextImpl(this, mMainThread, mPackageInfo, mActivityToken, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1658 | mUser, mRestricted, display, null, Display.INVALID_DISPLAY); |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1661 | Display getDisplay() { |
| 1662 | if (mDisplay != null) { |
| 1663 | return mDisplay; |
| 1664 | } |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1665 | return ResourcesManager.getInstance().getAdjustedDisplay( |
| 1666 | Display.DEFAULT_DISPLAY, mDisplayAdjustments); |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1667 | } |
| 1668 | |
Jeff Brown | a492c3a | 2012-08-23 19:48:44 -0700 | [diff] [blame] | 1669 | private int getDisplayId() { |
| 1670 | return mDisplay != null ? mDisplay.getDisplayId() : Display.DEFAULT_DISPLAY; |
| 1671 | } |
| 1672 | |
| 1673 | @Override |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 1674 | public boolean isRestricted() { |
| 1675 | return mRestricted; |
| 1676 | } |
| 1677 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 1678 | @Override |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 1679 | public DisplayAdjustments getDisplayAdjustments(int displayId) { |
| 1680 | return mDisplayAdjustments; |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1683 | private File getDataDirFile() { |
| 1684 | if (mPackageInfo != null) { |
| 1685 | return mPackageInfo.getDataDirFile(); |
| 1686 | } |
| 1687 | throw new RuntimeException("Not supported in system context"); |
| 1688 | } |
| 1689 | |
| 1690 | @Override |
| 1691 | public File getDir(String name, int mode) { |
| 1692 | name = "app_" + name; |
| 1693 | File file = makeFilename(getDataDirFile(), name); |
| 1694 | if (!file.exists()) { |
| 1695 | file.mkdir(); |
| 1696 | setFilePermissionsFromMode(file.getPath(), mode, |
| 1697 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH); |
| 1698 | } |
| 1699 | return file; |
| 1700 | } |
| 1701 | |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1702 | /** {@hide} */ |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1703 | @Override |
Jeff Sharkey | ded653b | 2012-09-27 19:09:24 -0700 | [diff] [blame] | 1704 | public int getUserId() { |
| 1705 | return mUser.getIdentifier(); |
| 1706 | } |
| 1707 | |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 1708 | static ContextImpl createSystemContext(ActivityThread mainThread) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1709 | LoadedApk packageInfo = new LoadedApk(mainThread); |
| 1710 | ContextImpl context = new ContextImpl(null, mainThread, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1711 | packageInfo, null, null, false, null, null, Display.INVALID_DISPLAY); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1712 | context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(), |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 1713 | context.mResourcesManager.getDisplayMetricsLocked()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1714 | return context; |
| 1715 | } |
| 1716 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1717 | static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo) { |
| 1718 | if (packageInfo == null) throw new IllegalArgumentException("packageInfo"); |
| 1719 | return new ContextImpl(null, mainThread, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1720 | packageInfo, null, null, false, null, null, Display.INVALID_DISPLAY); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | static ContextImpl createActivityContext(ActivityThread mainThread, |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 1724 | LoadedApk packageInfo, int displayId, Configuration overrideConfiguration) { |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1725 | if (packageInfo == null) throw new IllegalArgumentException("packageInfo"); |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1726 | return new ContextImpl(null, mainThread, packageInfo, null, null, false, |
| 1727 | null, overrideConfiguration, displayId); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | private ContextImpl(ContextImpl container, ActivityThread mainThread, |
| 1731 | LoadedApk packageInfo, IBinder activityToken, UserHandle user, boolean restricted, |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1732 | Display display, Configuration overrideConfiguration, int createDisplayWithId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1733 | mOuterContext = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1735 | mMainThread = mainThread; |
| 1736 | mActivityToken = activityToken; |
| 1737 | mRestricted = restricted; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1739 | if (user == null) { |
| 1740 | user = Process.myUserHandle(); |
| 1741 | } |
| 1742 | mUser = user; |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1743 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1744 | mPackageInfo = packageInfo; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1745 | mResourcesManager = ResourcesManager.getInstance(); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1746 | |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1747 | final int displayId = (createDisplayWithId != Display.INVALID_DISPLAY) |
Wale Ogunwale | cac3dc6 | 2015-06-09 15:35:02 -0700 | [diff] [blame^] | 1748 | ? createDisplayWithId |
| 1749 | : (display != null) ? display.getDisplayId() : Display.DEFAULT_DISPLAY; |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1750 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1751 | CompatibilityInfo compatInfo = null; |
| 1752 | if (container != null) { |
| 1753 | compatInfo = container.getDisplayAdjustments(displayId).getCompatibilityInfo(); |
| 1754 | } |
Wale Ogunwale | 9cf9954 | 2015-02-18 16:31:34 -0800 | [diff] [blame] | 1755 | if (compatInfo == null) { |
| 1756 | compatInfo = (displayId == Display.DEFAULT_DISPLAY) |
| 1757 | ? packageInfo.getCompatibilityInfo() |
| 1758 | : CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1759 | } |
| 1760 | mDisplayAdjustments.setCompatibilityInfo(compatInfo); |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 1761 | mDisplayAdjustments.setConfiguration(overrideConfiguration); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1762 | |
Wale Ogunwale | 2669851 | 2015-06-05 16:55:33 -0700 | [diff] [blame] | 1763 | mDisplay = (createDisplayWithId == Display.INVALID_DISPLAY) ? display |
| 1764 | : ResourcesManager.getInstance().getAdjustedDisplay(displayId, mDisplayAdjustments); |
| 1765 | |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1766 | Resources resources = packageInfo.getResources(mainThread); |
| 1767 | if (resources != null) { |
Wale Ogunwale | 60454db | 2015-01-23 16:05:07 -0800 | [diff] [blame] | 1768 | if (displayId != Display.DEFAULT_DISPLAY |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1769 | || overrideConfiguration != null |
| 1770 | || (compatInfo != null && compatInfo.applicationScale |
| 1771 | != resources.getCompatibilityInfo().applicationScale)) { |
Jeff Sharkey | 8a4c972 | 2014-06-16 13:48:42 -0700 | [diff] [blame] | 1772 | resources = mResourcesManager.getTopLevelResources(packageInfo.getResDir(), |
| 1773 | packageInfo.getSplitResDirs(), packageInfo.getOverlayDirs(), |
| 1774 | packageInfo.getApplicationInfo().sharedLibraryFiles, displayId, |
Wale Ogunwale | 60454db | 2015-01-23 16:05:07 -0800 | [diff] [blame] | 1775 | overrideConfiguration, compatInfo); |
Jeff Brown | defd4a6 | 2014-03-10 21:24:37 -0700 | [diff] [blame] | 1776 | } |
| 1777 | } |
| 1778 | mResources = resources; |
| 1779 | |
| 1780 | if (container != null) { |
| 1781 | mBasePackageName = container.mBasePackageName; |
| 1782 | mOpPackageName = container.mOpPackageName; |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 1783 | } else { |
| 1784 | mBasePackageName = packageInfo.mPackageName; |
| 1785 | ApplicationInfo ainfo = packageInfo.getApplicationInfo(); |
| 1786 | if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) { |
| 1787 | // Special case: system components allow themselves to be loaded in to other |
| 1788 | // processes. For purposes of app ops, we must then consider the context as |
| 1789 | // belonging to the package of this process, not the system itself, otherwise |
| 1790 | // the package+uid verifications in app ops will fail. |
| 1791 | mOpPackageName = ActivityThread.currentPackageName(); |
| 1792 | } else { |
| 1793 | mOpPackageName = mBasePackageName; |
| 1794 | } |
| 1795 | } |
Xin Guan | 2bcb97b | 2014-09-10 15:24:30 -0500 | [diff] [blame] | 1796 | |
| 1797 | mContentResolver = new ApplicationContentResolver(this, mainThread, user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1798 | } |
| 1799 | |
Narayan Kamath | 29564cd | 2014-08-07 10:57:40 +0100 | [diff] [blame] | 1800 | void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) { |
| 1801 | mPackageInfo.installSystemApplicationInfo(info, classLoader); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | final void scheduleFinalCleanup(String who, String what) { |
| 1805 | mMainThread.scheduleContextCleanup(this, who, what); |
| 1806 | } |
| 1807 | |
| 1808 | final void performFinalCleanup(String who, String what) { |
| 1809 | //Log.i(TAG, "Cleanup up context: " + this); |
| 1810 | mPackageInfo.removeContextRegistrations(getOuterContext(), who, what); |
| 1811 | } |
| 1812 | |
| 1813 | final Context getReceiverRestrictedContext() { |
| 1814 | if (mReceiverRestrictedContext != null) { |
| 1815 | return mReceiverRestrictedContext; |
| 1816 | } |
| 1817 | return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext()); |
| 1818 | } |
| 1819 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1820 | final void setOuterContext(Context context) { |
| 1821 | mOuterContext = context; |
| 1822 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1823 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1824 | final Context getOuterContext() { |
| 1825 | return mOuterContext; |
| 1826 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1827 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1828 | final IBinder getActivityToken() { |
| 1829 | return mActivityToken; |
| 1830 | } |
| 1831 | |
Jeff Brown | 6e53931 | 2015-02-24 18:53:21 -0800 | [diff] [blame] | 1832 | @SuppressWarnings("deprecation") |
Brad Fitzpatrick | d3da440 | 2010-11-10 08:27:11 -0800 | [diff] [blame] | 1833 | static void setFilePermissionsFromMode(String name, int mode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1834 | int extraPermissions) { |
| 1835 | int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR |
| 1836 | |FileUtils.S_IRGRP|FileUtils.S_IWGRP |
| 1837 | |extraPermissions; |
| 1838 | if ((mode&MODE_WORLD_READABLE) != 0) { |
| 1839 | perms |= FileUtils.S_IROTH; |
| 1840 | } |
| 1841 | if ((mode&MODE_WORLD_WRITEABLE) != 0) { |
| 1842 | perms |= FileUtils.S_IWOTH; |
| 1843 | } |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 1844 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1845 | Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode) |
| 1846 | + ", perms=0x" + Integer.toHexString(perms)); |
| 1847 | } |
| 1848 | FileUtils.setPermissions(name, perms, -1, -1); |
| 1849 | } |
| 1850 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 1851 | private File validateFilePath(String name, boolean createDirectory) { |
| 1852 | File dir; |
| 1853 | File f; |
| 1854 | |
| 1855 | if (name.charAt(0) == File.separatorChar) { |
| 1856 | String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar)); |
| 1857 | dir = new File(dirPath); |
| 1858 | name = name.substring(name.lastIndexOf(File.separatorChar)); |
| 1859 | f = new File(dir, name); |
| 1860 | } else { |
| 1861 | dir = getDatabasesDir(); |
| 1862 | f = makeFilename(dir, name); |
| 1863 | } |
| 1864 | |
| 1865 | if (createDirectory && !dir.isDirectory() && dir.mkdir()) { |
| 1866 | FileUtils.setPermissions(dir.getPath(), |
| 1867 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 1868 | -1, -1); |
| 1869 | } |
| 1870 | |
| 1871 | return f; |
| 1872 | } |
| 1873 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1874 | private File makeFilename(File base, String name) { |
| 1875 | if (name.indexOf(File.separatorChar) < 0) { |
| 1876 | return new File(base, name); |
| 1877 | } |
| 1878 | throw new IllegalArgumentException( |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 1879 | "File " + name + " contains a path separator"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1882 | /** |
| 1883 | * Ensure that given directories exist, trying to create them if missing. If |
| 1884 | * unable to create, they are filtered by replacing with {@code null}. |
| 1885 | */ |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 1886 | private File[] ensureDirsExistOrFilter(File[] dirs) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1887 | File[] result = new File[dirs.length]; |
| 1888 | for (int i = 0; i < dirs.length; i++) { |
| 1889 | File dir = dirs[i]; |
| 1890 | if (!dir.exists()) { |
| 1891 | if (!dir.mkdirs()) { |
Christopher Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 1892 | // recheck existence in case of cross-process race |
| 1893 | if (!dir.exists()) { |
| 1894 | // Failing to mkdir() may be okay, since we might not have |
| 1895 | // enough permissions; ask vold to create on our behalf. |
| 1896 | final IMountService mount = IMountService.Stub.asInterface( |
| 1897 | ServiceManager.getService("mount")); |
| 1898 | int res = -1; |
| 1899 | try { |
| 1900 | res = mount.mkdirs(getPackageName(), dir.getAbsolutePath()); |
Jeff Sharkey | 9708669 | 2015-01-08 14:00:13 -0800 | [diff] [blame] | 1901 | } catch (Exception ignored) { |
Christopher Tate | cc866da | 2013-10-02 18:11:01 -0700 | [diff] [blame] | 1902 | } |
| 1903 | if (res != 0) { |
| 1904 | Log.w(TAG, "Failed to ensure directory: " + dir); |
| 1905 | dir = null; |
| 1906 | } |
Jeff Sharkey | 2d8b4e8 | 2013-09-17 17:30:33 -0700 | [diff] [blame] | 1907 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | result[i] = dir; |
| 1911 | } |
| 1912 | return result; |
| 1913 | } |
| 1914 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1915 | // ---------------------------------------------------------------------- |
| 1916 | // ---------------------------------------------------------------------- |
| 1917 | // ---------------------------------------------------------------------- |
| 1918 | |
| 1919 | private static final class ApplicationContentResolver extends ContentResolver { |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1920 | private final ActivityThread mMainThread; |
| 1921 | private final UserHandle mUser; |
| 1922 | |
| 1923 | public ApplicationContentResolver( |
| 1924 | Context context, ActivityThread mainThread, UserHandle user) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1925 | super(context); |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1926 | mMainThread = Preconditions.checkNotNull(mainThread); |
| 1927 | mUser = Preconditions.checkNotNull(user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1931 | protected IContentProvider acquireProvider(Context context, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1932 | return mMainThread.acquireProvider(context, |
| 1933 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 1934 | resolveUserIdFromAuthority(auth), true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1938 | protected IContentProvider acquireExistingProvider(Context context, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1939 | return mMainThread.acquireExistingProvider(context, |
| 1940 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 1941 | resolveUserIdFromAuthority(auth), true); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | @Override |
| 1945 | public boolean releaseProvider(IContentProvider provider) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1946 | return mMainThread.releaseProvider(provider, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1947 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1948 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1949 | @Override |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1950 | protected IContentProvider acquireUnstableProvider(Context c, String auth) { |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1951 | return mMainThread.acquireProvider(c, |
| 1952 | ContentProvider.getAuthorityWithoutUserId(auth), |
| 1953 | resolveUserIdFromAuthority(auth), false); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | @Override |
| 1957 | public boolean releaseUnstableProvider(IContentProvider icp) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1958 | return mMainThread.releaseProvider(icp, false); |
| 1959 | } |
| 1960 | |
| 1961 | @Override |
| 1962 | public void unstableProviderDied(IContentProvider icp) { |
| 1963 | mMainThread.handleUnstableProviderDied(icp.asBinder(), true); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1964 | } |
Jeff Sharkey | 7aa7601 | 2013-09-30 14:26:27 -0700 | [diff] [blame] | 1965 | |
| 1966 | @Override |
| 1967 | public void appNotRespondingViaProvider(IContentProvider icp) { |
| 1968 | mMainThread.appNotRespondingViaProvider(icp.asBinder()); |
| 1969 | } |
Nicolas Prevot | d85fc72 | 2014-04-16 19:52:08 +0100 | [diff] [blame] | 1970 | |
| 1971 | /** @hide */ |
| 1972 | protected int resolveUserIdFromAuthority(String auth) { |
| 1973 | return ContentProvider.getUserIdFromAuthority(auth, mUser.getIdentifier()); |
| 1974 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1975 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1976 | } |