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 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 19 | import com.android.internal.policy.PolicyManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 21 | import android.bluetooth.BluetoothAdapter; |
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; |
| 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.content.ContextWrapper; |
| 27 | import android.content.IContentProvider; |
| 28 | import android.content.Intent; |
| 29 | import android.content.IntentFilter; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 30 | import android.content.IIntentReceiver; |
| 31 | import android.content.IntentSender; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.content.ReceiverCallNotAllowedException; |
| 33 | import android.content.ServiceConnection; |
| 34 | import android.content.SharedPreferences; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.content.pm.IPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.content.res.AssetManager; |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 39 | import android.content.res.CompatibilityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.res.Resources; |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 41 | import android.database.DatabaseErrorHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.database.sqlite.SQLiteDatabase; |
| 43 | import android.database.sqlite.SQLiteDatabase.CursorFactory; |
| 44 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.graphics.drawable.Drawable; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 46 | import android.hardware.ISerialManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.hardware.SensorManager; |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 48 | import android.hardware.SerialManager; |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 49 | import android.hardware.SystemSensorManager; |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 50 | import android.hardware.display.DisplayManager; |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 51 | import android.hardware.input.IInputManager; |
Jeff Brown | 9df6e7a | 2012-04-05 11:49:26 -0700 | [diff] [blame] | 52 | import android.hardware.input.InputManager; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 53 | import android.hardware.usb.IUsbManager; |
| 54 | import android.hardware.usb.UsbManager; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 55 | import android.location.CountryDetector; |
| 56 | import android.location.ICountryDetector; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.location.ILocationManager; |
| 58 | import android.location.LocationManager; |
| 59 | import android.media.AudioManager; |
Dianne Hackborn | b58b8f8 | 2012-06-11 15:08:39 -0700 | [diff] [blame] | 60 | import android.media.MediaRouter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.net.ConnectivityManager; |
| 62 | import android.net.IConnectivityManager; |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 63 | import android.net.INetworkPolicyManager; |
| 64 | import android.net.NetworkPolicyManager; |
Irfan Sheriff | c9b6851 | 2010-04-08 14:12:33 -0700 | [diff] [blame] | 65 | import android.net.ThrottleManager; |
| 66 | import android.net.IThrottleManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.net.Uri; |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 68 | import android.net.nsd.INsdManager; |
| 69 | import android.net.nsd.NsdManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | import android.net.wifi.IWifiManager; |
| 71 | import android.net.wifi.WifiManager; |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 72 | import android.net.wifi.p2p.IWifiP2pManager; |
| 73 | import android.net.wifi.p2p.WifiP2pManager; |
Nick Pelly | 50b4d8f | 2010-12-07 22:40:28 -0800 | [diff] [blame] | 74 | import android.nfc.NfcManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.os.Binder; |
| 76 | import android.os.Bundle; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 77 | import android.os.DropBoxManager; |
Oscar Montemayor | 539d3c4 | 2010-01-29 15:27:00 -0800 | [diff] [blame] | 78 | import android.os.Environment; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | import android.os.FileUtils; |
| 80 | import android.os.Handler; |
| 81 | import android.os.IBinder; |
| 82 | import android.os.IPowerManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame^] | 83 | import android.os.IUserManager; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 84 | import android.os.Looper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.os.PowerManager; |
| 86 | import android.os.Process; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 87 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.os.ServiceManager; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 89 | import android.os.UserId; |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 90 | import android.os.SystemVibrator; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame^] | 91 | import android.os.UserManager; |
San Mehat | b104340 | 2010-02-05 08:26:50 -0800 | [diff] [blame] | 92 | import android.os.storage.StorageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | import android.telephony.TelephonyManager; |
Dianne Hackborn | 9f53119 | 2010-08-04 17:48:03 -0700 | [diff] [blame] | 94 | import android.content.ClipboardManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.util.AndroidRuntimeException; |
| 96 | import android.util.Log; |
| 97 | import android.view.ContextThemeWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.view.WindowManagerImpl; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 99 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | import android.view.inputmethod.InputMethodManager; |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 101 | import android.view.textservice.TextServicesManager; |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 102 | import android.accounts.AccountManager; |
| 103 | import android.accounts.IAccountManager; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 104 | import android.app.admin.DevicePolicyManager; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 105 | import com.android.internal.os.IDropBoxManagerService; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | import java.io.File; |
| 108 | import java.io.FileInputStream; |
| 109 | import java.io.FileNotFoundException; |
| 110 | import java.io.FileOutputStream; |
| 111 | import java.io.IOException; |
| 112 | import java.io.InputStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import java.util.ArrayList; |
| 114 | import java.util.HashMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | class ReceiverRestrictedContext extends ContextWrapper { |
| 117 | ReceiverRestrictedContext(Context base) { |
| 118 | super(base); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 123 | return registerReceiver(receiver, filter, null, null); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 128 | String broadcastPermission, Handler scheduler) { |
| 129 | throw new ReceiverCallNotAllowedException( |
| 130 | "IntentReceiver components are not allowed to register to receive intents"); |
| 131 | //ex.fillInStackTrace(); |
| 132 | //Log.e("IntentReceiver", ex.getMessage(), ex); |
| 133 | //return mContext.registerReceiver(receiver, filter, broadcastPermission, |
| 134 | // scheduler); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public boolean bindService(Intent service, ServiceConnection conn, int flags) { |
| 139 | throw new ReceiverCallNotAllowedException( |
| 140 | "IntentReceiver components are not allowed to bind to services"); |
| 141 | //ex.fillInStackTrace(); |
| 142 | //Log.e("IntentReceiver", ex.getMessage(), ex); |
| 143 | //return mContext.bindService(service, interfaceName, conn, flags); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 148 | * Common implementation of Context API, which provides the base |
| 149 | * context object for Activity and other application components. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | */ |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 151 | class ContextImpl extends Context { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | private final static String TAG = "ApplicationContext"; |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 153 | private final static boolean DEBUG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 155 | private static final HashMap<String, SharedPreferencesImpl> sSharedPrefs = |
| 156 | new HashMap<String, SharedPreferencesImpl>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 158 | /*package*/ LoadedApk mPackageInfo; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 159 | private String mBasePackageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | private Resources mResources; |
| 161 | /*package*/ ActivityThread mMainThread; |
| 162 | private Context mOuterContext; |
| 163 | private IBinder mActivityToken = null; |
| 164 | private ApplicationContentResolver mContentResolver; |
| 165 | private int mThemeResource = 0; |
| 166 | private Resources.Theme mTheme = null; |
| 167 | private PackageManager mPackageManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | private Context mReceiverRestrictedContext = null; |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 169 | private boolean mRestricted; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | |
| 171 | private final Object mSync = new Object(); |
| 172 | |
| 173 | private File mDatabasesDir; |
| 174 | private File mPreferencesDir; |
| 175 | private File mFilesDir; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | private File mCacheDir; |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 177 | private File mObbDir; |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 178 | private File mExternalFilesDir; |
| 179 | private File mExternalCacheDir; |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | private static final String[] EMPTY_FILE_LIST = {}; |
| 182 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 183 | /** |
| 184 | * Override this class when the system service constructor needs a |
| 185 | * ContextImpl. Else, use StaticServiceFetcher below. |
| 186 | */ |
| 187 | /*package*/ static class ServiceFetcher { |
| 188 | int mContextCacheIndex = -1; |
| 189 | |
| 190 | /** |
| 191 | * Main entrypoint; only override if you don't need caching. |
| 192 | */ |
| 193 | public Object getService(ContextImpl ctx) { |
| 194 | ArrayList<Object> cache = ctx.mServiceCache; |
| 195 | Object service; |
| 196 | synchronized (cache) { |
| 197 | if (cache.size() == 0) { |
| 198 | // Initialize the cache vector on first access. |
| 199 | // At this point sNextPerContextServiceCacheIndex |
| 200 | // is the number of potential services that are |
| 201 | // cached per-Context. |
| 202 | for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) { |
| 203 | cache.add(null); |
| 204 | } |
| 205 | } else { |
| 206 | service = cache.get(mContextCacheIndex); |
| 207 | if (service != null) { |
| 208 | return service; |
| 209 | } |
| 210 | } |
| 211 | service = createService(ctx); |
| 212 | cache.set(mContextCacheIndex, service); |
| 213 | return service; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Override this to create a new per-Context instance of the |
| 219 | * service. getService() will handle locking and caching. |
| 220 | */ |
| 221 | public Object createService(ContextImpl ctx) { |
| 222 | throw new RuntimeException("Not implemented"); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Override this class for services to be cached process-wide. |
| 228 | */ |
| 229 | abstract static class StaticServiceFetcher extends ServiceFetcher { |
| 230 | private Object mCachedInstance; |
| 231 | |
| 232 | @Override |
| 233 | public final Object getService(ContextImpl unused) { |
| 234 | synchronized (StaticServiceFetcher.this) { |
| 235 | Object service = mCachedInstance; |
| 236 | if (service != null) { |
| 237 | return service; |
| 238 | } |
| 239 | return mCachedInstance = createStaticService(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | public abstract Object createStaticService(); |
| 244 | } |
| 245 | |
| 246 | private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP = |
| 247 | new HashMap<String, ServiceFetcher>(); |
| 248 | |
| 249 | private static int sNextPerContextServiceCacheIndex = 0; |
| 250 | private static void registerService(String serviceName, ServiceFetcher fetcher) { |
| 251 | if (!(fetcher instanceof StaticServiceFetcher)) { |
| 252 | fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++; |
| 253 | } |
| 254 | SYSTEM_SERVICE_MAP.put(serviceName, fetcher); |
| 255 | } |
| 256 | |
| 257 | // This one's defined separately and given a variable name so it |
| 258 | // can be re-used by getWallpaperManager(), avoiding a HashMap |
| 259 | // lookup. |
| 260 | private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() { |
| 261 | public Object createService(ContextImpl ctx) { |
| 262 | return new WallpaperManager(ctx.getOuterContext(), |
| 263 | ctx.mMainThread.getHandler()); |
| 264 | }}; |
| 265 | |
| 266 | static { |
| 267 | registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() { |
| 268 | public Object getService(ContextImpl ctx) { |
| 269 | return AccessibilityManager.getInstance(ctx); |
| 270 | }}); |
| 271 | |
| 272 | registerService(ACCOUNT_SERVICE, new ServiceFetcher() { |
| 273 | public Object createService(ContextImpl ctx) { |
| 274 | IBinder b = ServiceManager.getService(ACCOUNT_SERVICE); |
| 275 | IAccountManager service = IAccountManager.Stub.asInterface(b); |
| 276 | return new AccountManager(ctx, service); |
| 277 | }}); |
| 278 | |
| 279 | registerService(ACTIVITY_SERVICE, new ServiceFetcher() { |
| 280 | public Object createService(ContextImpl ctx) { |
| 281 | return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler()); |
| 282 | }}); |
| 283 | |
| 284 | registerService(ALARM_SERVICE, new StaticServiceFetcher() { |
| 285 | public Object createStaticService() { |
| 286 | IBinder b = ServiceManager.getService(ALARM_SERVICE); |
| 287 | IAlarmManager service = IAlarmManager.Stub.asInterface(b); |
| 288 | return new AlarmManager(service); |
| 289 | }}); |
| 290 | |
| 291 | registerService(AUDIO_SERVICE, new ServiceFetcher() { |
| 292 | public Object createService(ContextImpl ctx) { |
| 293 | return new AudioManager(ctx); |
| 294 | }}); |
| 295 | |
Dianne Hackborn | b58b8f8 | 2012-06-11 15:08:39 -0700 | [diff] [blame] | 296 | registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() { |
| 297 | public Object createService(ContextImpl ctx) { |
| 298 | return new MediaRouter(ctx); |
| 299 | }}); |
| 300 | |
Jaikumar Ganesh | 1abb1cb | 2012-01-25 16:14:50 -0800 | [diff] [blame] | 301 | registerService(BLUETOOTH_SERVICE, new ServiceFetcher() { |
| 302 | public Object createService(ContextImpl ctx) { |
| 303 | return BluetoothAdapter.getDefaultAdapter(); |
| 304 | }}); |
| 305 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 306 | registerService(CLIPBOARD_SERVICE, new ServiceFetcher() { |
| 307 | public Object createService(ContextImpl ctx) { |
| 308 | return new ClipboardManager(ctx.getOuterContext(), |
| 309 | ctx.mMainThread.getHandler()); |
| 310 | }}); |
| 311 | |
| 312 | registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() { |
| 313 | public Object createStaticService() { |
| 314 | IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE); |
| 315 | return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b)); |
| 316 | }}); |
| 317 | |
| 318 | registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() { |
| 319 | public Object createStaticService() { |
| 320 | IBinder b = ServiceManager.getService(COUNTRY_DETECTOR); |
| 321 | return new CountryDetector(ICountryDetector.Stub.asInterface(b)); |
| 322 | }}); |
| 323 | |
| 324 | registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() { |
| 325 | public Object createService(ContextImpl ctx) { |
| 326 | return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler()); |
| 327 | }}); |
| 328 | |
| 329 | registerService(DOWNLOAD_SERVICE, new ServiceFetcher() { |
| 330 | public Object createService(ContextImpl ctx) { |
| 331 | return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName()); |
| 332 | }}); |
| 333 | |
Nick Pelly | d250746 | 2010-12-13 12:22:34 -0800 | [diff] [blame] | 334 | registerService(NFC_SERVICE, new ServiceFetcher() { |
| 335 | public Object createService(ContextImpl ctx) { |
| 336 | return new NfcManager(ctx); |
| 337 | }}); |
| 338 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 339 | registerService(DROPBOX_SERVICE, new StaticServiceFetcher() { |
| 340 | public Object createStaticService() { |
| 341 | return createDropBoxManager(); |
| 342 | }}); |
| 343 | |
Jeff Brown | 9f25b7f | 2012-04-10 14:30:49 -0700 | [diff] [blame] | 344 | registerService(INPUT_SERVICE, new StaticServiceFetcher() { |
| 345 | public Object createStaticService() { |
| 346 | return InputManager.getInstance(); |
Jeff Brown | ac14351 | 2012-04-05 18:57:33 -0700 | [diff] [blame] | 347 | }}); |
Jeff Brown | 9df6e7a | 2012-04-05 11:49:26 -0700 | [diff] [blame] | 348 | |
Jeff Brown | fa25bf5 | 2012-07-23 19:26:30 -0700 | [diff] [blame] | 349 | registerService(DISPLAY_SERVICE, new StaticServiceFetcher() { |
| 350 | public Object createStaticService() { |
| 351 | return DisplayManager.getInstance(); |
| 352 | }}); |
| 353 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 354 | registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() { |
| 355 | public Object createService(ContextImpl ctx) { |
| 356 | return InputMethodManager.getInstance(ctx); |
| 357 | }}); |
| 358 | |
satok | 988323c | 2011-06-22 16:38:13 +0900 | [diff] [blame] | 359 | registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() { |
| 360 | public Object createService(ContextImpl ctx) { |
| 361 | return TextServicesManager.getInstance(); |
| 362 | }}); |
| 363 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 364 | registerService(KEYGUARD_SERVICE, new ServiceFetcher() { |
| 365 | public Object getService(ContextImpl ctx) { |
| 366 | // TODO: why isn't this caching it? It wasn't |
| 367 | // before, so I'm preserving the old behavior and |
| 368 | // using getService(), instead of createService() |
| 369 | // which would do the caching. |
| 370 | return new KeyguardManager(); |
| 371 | }}); |
| 372 | |
| 373 | registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() { |
| 374 | public Object createService(ContextImpl ctx) { |
| 375 | return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext()); |
| 376 | }}); |
| 377 | |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 378 | registerService(LOCATION_SERVICE, new ServiceFetcher() { |
| 379 | public Object createService(ContextImpl ctx) { |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 380 | IBinder b = ServiceManager.getService(LOCATION_SERVICE); |
Nick Pelly | e0fd693 | 2012-07-11 10:26:13 -0700 | [diff] [blame] | 381 | return new LocationManager(ctx, ILocationManager.Stub.asInterface(b)); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 382 | }}); |
| 383 | |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 384 | registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() { |
| 385 | @Override |
| 386 | public Object createService(ContextImpl ctx) { |
| 387 | return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface( |
| 388 | ServiceManager.getService(NETWORK_POLICY_SERVICE))); |
| 389 | } |
| 390 | }); |
| 391 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 392 | registerService(NOTIFICATION_SERVICE, new ServiceFetcher() { |
| 393 | public Object createService(ContextImpl ctx) { |
| 394 | final Context outerContext = ctx.getOuterContext(); |
| 395 | return new NotificationManager( |
| 396 | new ContextThemeWrapper(outerContext, |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 397 | Resources.selectSystemTheme(0, |
| 398 | outerContext.getApplicationInfo().targetSdkVersion, |
| 399 | com.android.internal.R.style.Theme_Dialog, |
Adam Powell | 6e90a36 | 2011-08-14 16:48:32 -0700 | [diff] [blame] | 400 | com.android.internal.R.style.Theme_Holo_Dialog, |
| 401 | com.android.internal.R.style.Theme_DeviceDefault_Dialog)), |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 402 | ctx.mMainThread.getHandler()); |
| 403 | }}); |
| 404 | |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 405 | registerService(NSD_SERVICE, new ServiceFetcher() { |
| 406 | @Override |
| 407 | public Object createService(ContextImpl ctx) { |
| 408 | IBinder b = ServiceManager.getService(NSD_SERVICE); |
| 409 | INsdManager service = INsdManager.Stub.asInterface(b); |
Irfan Sheriff | 22af38c | 2012-05-03 16:44:27 -0700 | [diff] [blame] | 410 | return new NsdManager(ctx.getOuterContext(), service); |
Irfan Sheriff | 7d024d3 | 2012-03-22 17:01:39 -0700 | [diff] [blame] | 411 | }}); |
| 412 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 413 | // Note: this was previously cached in a static variable, but |
| 414 | // constructed using mMainThread.getHandler(), so converting |
| 415 | // it to be a regular Context-cached service... |
| 416 | registerService(POWER_SERVICE, new ServiceFetcher() { |
| 417 | public Object createService(ContextImpl ctx) { |
| 418 | IBinder b = ServiceManager.getService(POWER_SERVICE); |
| 419 | IPowerManager service = IPowerManager.Stub.asInterface(b); |
| 420 | return new PowerManager(service, ctx.mMainThread.getHandler()); |
| 421 | }}); |
| 422 | |
| 423 | registerService(SEARCH_SERVICE, new ServiceFetcher() { |
| 424 | public Object createService(ContextImpl ctx) { |
| 425 | return new SearchManager(ctx.getOuterContext(), |
| 426 | ctx.mMainThread.getHandler()); |
| 427 | }}); |
| 428 | |
| 429 | registerService(SENSOR_SERVICE, new ServiceFetcher() { |
| 430 | public Object createService(ContextImpl ctx) { |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 431 | return new SystemSensorManager(ctx.mMainThread.getHandler().getLooper()); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 432 | }}); |
| 433 | |
| 434 | registerService(STATUS_BAR_SERVICE, new ServiceFetcher() { |
| 435 | public Object createService(ContextImpl ctx) { |
| 436 | return new StatusBarManager(ctx.getOuterContext()); |
| 437 | }}); |
| 438 | |
| 439 | registerService(STORAGE_SERVICE, new ServiceFetcher() { |
| 440 | public Object createService(ContextImpl ctx) { |
| 441 | try { |
| 442 | return new StorageManager(ctx.mMainThread.getHandler().getLooper()); |
| 443 | } catch (RemoteException rex) { |
| 444 | Log.e(TAG, "Failed to create StorageManager", rex); |
| 445 | return null; |
| 446 | } |
| 447 | }}); |
| 448 | |
| 449 | registerService(TELEPHONY_SERVICE, new ServiceFetcher() { |
| 450 | public Object createService(ContextImpl ctx) { |
| 451 | return new TelephonyManager(ctx.getOuterContext()); |
| 452 | }}); |
| 453 | |
| 454 | registerService(THROTTLE_SERVICE, new StaticServiceFetcher() { |
| 455 | public Object createStaticService() { |
| 456 | IBinder b = ServiceManager.getService(THROTTLE_SERVICE); |
| 457 | return new ThrottleManager(IThrottleManager.Stub.asInterface(b)); |
| 458 | }}); |
| 459 | |
| 460 | registerService(UI_MODE_SERVICE, new ServiceFetcher() { |
| 461 | public Object createService(ContextImpl ctx) { |
| 462 | return new UiModeManager(); |
| 463 | }}); |
| 464 | |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 465 | registerService(USB_SERVICE, new ServiceFetcher() { |
| 466 | public Object createService(ContextImpl ctx) { |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 467 | IBinder b = ServiceManager.getService(USB_SERVICE); |
Mike Lockwood | 3a68b83 | 2011-03-08 10:08:59 -0500 | [diff] [blame] | 468 | return new UsbManager(ctx, IUsbManager.Stub.asInterface(b)); |
Mike Lockwood | e7d511e | 2010-12-30 13:39:37 -0500 | [diff] [blame] | 469 | }}); |
| 470 | |
Mike Lockwood | b01e8bf | 2011-08-29 20:11:07 -0400 | [diff] [blame] | 471 | registerService(SERIAL_SERVICE, new ServiceFetcher() { |
| 472 | public Object createService(ContextImpl ctx) { |
| 473 | IBinder b = ServiceManager.getService(SERIAL_SERVICE); |
| 474 | return new SerialManager(ctx, ISerialManager.Stub.asInterface(b)); |
| 475 | }}); |
| 476 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 477 | registerService(VIBRATOR_SERVICE, new ServiceFetcher() { |
| 478 | public Object createService(ContextImpl ctx) { |
Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 479 | return new SystemVibrator(); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 480 | }}); |
| 481 | |
| 482 | registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER); |
| 483 | |
| 484 | registerService(WIFI_SERVICE, new ServiceFetcher() { |
| 485 | public Object createService(ContextImpl ctx) { |
| 486 | IBinder b = ServiceManager.getService(WIFI_SERVICE); |
| 487 | IWifiManager service = IWifiManager.Stub.asInterface(b); |
Irfan Sheriff | 88759bb | 2012-07-02 15:58:28 -0700 | [diff] [blame] | 488 | return new WifiManager(ctx.getOuterContext(), service); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 489 | }}); |
| 490 | |
repo sync | 55bc5f3 | 2011-06-24 14:23:07 -0700 | [diff] [blame] | 491 | registerService(WIFI_P2P_SERVICE, new ServiceFetcher() { |
| 492 | public Object createService(ContextImpl ctx) { |
| 493 | IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE); |
| 494 | IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b); |
| 495 | return new WifiP2pManager(service); |
| 496 | }}); |
| 497 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 498 | registerService(WINDOW_SERVICE, new ServiceFetcher() { |
| 499 | public Object getService(ContextImpl ctx) { |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 500 | return WindowManagerImpl.getDefault().makeCompatible( |
| 501 | ctx.mPackageInfo.mCompatibilityInfo); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 502 | }}); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame^] | 503 | |
| 504 | registerService(USER_SERVICE, new ServiceFetcher() { |
| 505 | public Object getService(ContextImpl ctx) { |
| 506 | IBinder b = ServiceManager.getService(USER_SERVICE); |
| 507 | IUserManager service = IUserManager.Stub.asInterface(b); |
| 508 | return new UserManager(ctx, service); |
| 509 | }}); |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Dianne Hackborn | 5fd2169 | 2011-06-07 14:09:47 -0700 | [diff] [blame] | 512 | static ContextImpl getImpl(Context context) { |
| 513 | Context nextContext; |
| 514 | while ((context instanceof ContextWrapper) && |
| 515 | (nextContext=((ContextWrapper)context).getBaseContext()) != null) { |
| 516 | context = nextContext; |
| 517 | } |
| 518 | return (ContextImpl)context; |
| 519 | } |
| 520 | |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 521 | // The system service cache for the system services that are |
| 522 | // cached per-ContextImpl. Package-scoped to avoid accessor |
| 523 | // methods. |
| 524 | final ArrayList<Object> mServiceCache = new ArrayList<Object>(); |
| 525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | @Override |
| 527 | public AssetManager getAssets() { |
| 528 | return mResources.getAssets(); |
| 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public Resources getResources() { |
| 533 | return mResources; |
| 534 | } |
| 535 | |
| 536 | @Override |
| 537 | public PackageManager getPackageManager() { |
| 538 | if (mPackageManager != null) { |
| 539 | return mPackageManager; |
| 540 | } |
| 541 | |
| 542 | IPackageManager pm = ActivityThread.getPackageManager(); |
| 543 | if (pm != null) { |
| 544 | // Doesn't matter if we make more than one instance. |
| 545 | return (mPackageManager = new ApplicationPackageManager(this, pm)); |
| 546 | } |
| 547 | |
| 548 | return null; |
| 549 | } |
| 550 | |
| 551 | @Override |
| 552 | public ContentResolver getContentResolver() { |
| 553 | return mContentResolver; |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public Looper getMainLooper() { |
| 558 | return mMainThread.getLooper(); |
| 559 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 560 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | @Override |
| 562 | public Context getApplicationContext() { |
Christopher Tate | eb9e9ec | 2010-03-23 17:14:36 -0700 | [diff] [blame] | 563 | return (mPackageInfo != null) ? |
| 564 | mPackageInfo.getApplication() : mMainThread.getApplication(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | @Override |
| 568 | public void setTheme(int resid) { |
| 569 | mThemeResource = resid; |
| 570 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 571 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | @Override |
Dianne Hackborn | 247fe74 | 2011-01-08 17:25:57 -0800 | [diff] [blame] | 573 | public int getThemeResId() { |
| 574 | return mThemeResource; |
| 575 | } |
| 576 | |
| 577 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | public Resources.Theme getTheme() { |
| 579 | if (mTheme == null) { |
Dianne Hackborn | d922ae0 | 2011-01-14 11:43:24 -0800 | [diff] [blame] | 580 | mThemeResource = Resources.selectDefaultTheme(mThemeResource, |
| 581 | getOuterContext().getApplicationInfo().targetSdkVersion); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | mTheme = mResources.newTheme(); |
| 583 | mTheme.applyStyle(mThemeResource, true); |
| 584 | } |
| 585 | return mTheme; |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public ClassLoader getClassLoader() { |
| 590 | return mPackageInfo != null ? |
| 591 | mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader(); |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public String getPackageName() { |
| 596 | if (mPackageInfo != null) { |
| 597 | return mPackageInfo.getPackageName(); |
| 598 | } |
| 599 | throw new RuntimeException("Not supported in system context"); |
| 600 | } |
| 601 | |
| 602 | @Override |
Dianne Hackborn | 5c1e00b | 2009-06-18 17:10:57 -0700 | [diff] [blame] | 603 | public ApplicationInfo getApplicationInfo() { |
| 604 | if (mPackageInfo != null) { |
| 605 | return mPackageInfo.getApplicationInfo(); |
| 606 | } |
| 607 | throw new RuntimeException("Not supported in system context"); |
| 608 | } |
| 609 | |
| 610 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | public String getPackageResourcePath() { |
| 612 | if (mPackageInfo != null) { |
| 613 | return mPackageInfo.getResDir(); |
| 614 | } |
| 615 | throw new RuntimeException("Not supported in system context"); |
| 616 | } |
| 617 | |
| 618 | @Override |
| 619 | public String getPackageCodePath() { |
| 620 | if (mPackageInfo != null) { |
| 621 | return mPackageInfo.getAppDir(); |
| 622 | } |
| 623 | throw new RuntimeException("Not supported in system context"); |
| 624 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 625 | |
Joe Onorato | 23ecae3 | 2009-06-10 17:07:15 -0700 | [diff] [blame] | 626 | public File getSharedPrefsFile(String name) { |
| 627 | return makeFilename(getPreferencesDir(), name + ".xml"); |
| 628 | } |
| 629 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | @Override |
| 631 | public SharedPreferences getSharedPreferences(String name, int mode) { |
| 632 | SharedPreferencesImpl sp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | synchronized (sSharedPrefs) { |
Brad Fitzpatrick | 333b8cb | 2010-08-26 12:04:57 -0700 | [diff] [blame] | 634 | sp = sSharedPrefs.get(name); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 635 | if (sp == null) { |
Brad Fitzpatrick | 4cd50b8 | 2010-12-01 17:31:45 -0800 | [diff] [blame] | 636 | File prefsFile = getSharedPrefsFile(name); |
| 637 | sp = new SharedPreferencesImpl(prefsFile, mode); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 638 | sSharedPrefs.put(name, sp); |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 639 | return sp; |
| 640 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | } |
Brad Fitzpatrick | 4e920f7 | 2010-12-14 11:52:13 -0800 | [diff] [blame] | 642 | if ((mode & Context.MODE_MULTI_PROCESS) != 0 || |
| 643 | getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { |
| 644 | // If somebody else (some other process) changed the prefs |
| 645 | // file behind our back, we reload it. This has been the |
| 646 | // historical (if undocumented) behavior. |
| 647 | sp.startReloadIfChangedUnexpectedly(); |
| 648 | } |
Brad Fitzpatrick | 6194c53 | 2010-09-07 18:00:33 -0700 | [diff] [blame] | 649 | return sp; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | private File getPreferencesDir() { |
| 653 | synchronized (mSync) { |
| 654 | if (mPreferencesDir == null) { |
| 655 | mPreferencesDir = new File(getDataDirFile(), "shared_prefs"); |
| 656 | } |
| 657 | return mPreferencesDir; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | @Override |
| 662 | public FileInputStream openFileInput(String name) |
| 663 | throws FileNotFoundException { |
| 664 | File f = makeFilename(getFilesDir(), name); |
| 665 | return new FileInputStream(f); |
| 666 | } |
| 667 | |
| 668 | @Override |
| 669 | public FileOutputStream openFileOutput(String name, int mode) |
| 670 | throws FileNotFoundException { |
| 671 | final boolean append = (mode&MODE_APPEND) != 0; |
| 672 | File f = makeFilename(getFilesDir(), name); |
| 673 | try { |
| 674 | FileOutputStream fos = new FileOutputStream(f, append); |
| 675 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 676 | return fos; |
| 677 | } catch (FileNotFoundException e) { |
| 678 | } |
| 679 | |
| 680 | File parent = f.getParentFile(); |
| 681 | parent.mkdir(); |
| 682 | FileUtils.setPermissions( |
| 683 | parent.getPath(), |
| 684 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 685 | -1, -1); |
| 686 | FileOutputStream fos = new FileOutputStream(f, append); |
| 687 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 688 | return fos; |
| 689 | } |
| 690 | |
| 691 | @Override |
| 692 | public boolean deleteFile(String name) { |
| 693 | File f = makeFilename(getFilesDir(), name); |
| 694 | return f.delete(); |
| 695 | } |
| 696 | |
| 697 | @Override |
| 698 | public File getFilesDir() { |
| 699 | synchronized (mSync) { |
| 700 | if (mFilesDir == null) { |
| 701 | mFilesDir = new File(getDataDirFile(), "files"); |
| 702 | } |
| 703 | if (!mFilesDir.exists()) { |
| 704 | if(!mFilesDir.mkdirs()) { |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 705 | Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | return null; |
| 707 | } |
| 708 | FileUtils.setPermissions( |
| 709 | mFilesDir.getPath(), |
| 710 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 711 | -1, -1); |
| 712 | } |
| 713 | return mFilesDir; |
| 714 | } |
| 715 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | @Override |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 718 | public File getExternalFilesDir(String type) { |
| 719 | synchronized (mSync) { |
| 720 | if (mExternalFilesDir == null) { |
| 721 | mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory( |
| 722 | getPackageName()); |
| 723 | } |
| 724 | if (!mExternalFilesDir.exists()) { |
| 725 | try { |
| 726 | (new File(Environment.getExternalStorageAndroidDataDir(), |
| 727 | ".nomedia")).createNewFile(); |
| 728 | } catch (IOException e) { |
| 729 | } |
| 730 | if (!mExternalFilesDir.mkdirs()) { |
| 731 | Log.w(TAG, "Unable to create external files directory"); |
| 732 | return null; |
| 733 | } |
| 734 | } |
| 735 | if (type == null) { |
| 736 | return mExternalFilesDir; |
| 737 | } |
| 738 | File dir = new File(mExternalFilesDir, type); |
| 739 | if (!dir.exists()) { |
| 740 | if (!dir.mkdirs()) { |
| 741 | Log.w(TAG, "Unable to create external media directory " + dir); |
| 742 | return null; |
| 743 | } |
| 744 | } |
| 745 | return dir; |
| 746 | } |
| 747 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 748 | |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 749 | @Override |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 750 | public File getObbDir() { |
| 751 | synchronized (mSync) { |
| 752 | if (mObbDir == null) { |
| 753 | mObbDir = Environment.getExternalStorageAppObbDirectory( |
| 754 | getPackageName()); |
| 755 | } |
| 756 | return mObbDir; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | public File getCacheDir() { |
| 762 | synchronized (mSync) { |
| 763 | if (mCacheDir == null) { |
| 764 | mCacheDir = new File(getDataDirFile(), "cache"); |
| 765 | } |
| 766 | if (!mCacheDir.exists()) { |
| 767 | if(!mCacheDir.mkdirs()) { |
| 768 | Log.w(TAG, "Unable to create cache directory"); |
| 769 | return null; |
| 770 | } |
| 771 | FileUtils.setPermissions( |
| 772 | mCacheDir.getPath(), |
| 773 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 774 | -1, -1); |
| 775 | } |
| 776 | } |
| 777 | return mCacheDir; |
| 778 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 779 | |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 780 | @Override |
| 781 | public File getExternalCacheDir() { |
| 782 | synchronized (mSync) { |
| 783 | if (mExternalCacheDir == null) { |
| 784 | mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory( |
| 785 | getPackageName()); |
| 786 | } |
| 787 | if (!mExternalCacheDir.exists()) { |
| 788 | try { |
| 789 | (new File(Environment.getExternalStorageAndroidDataDir(), |
| 790 | ".nomedia")).createNewFile(); |
| 791 | } catch (IOException e) { |
| 792 | } |
| 793 | if (!mExternalCacheDir.mkdirs()) { |
| 794 | Log.w(TAG, "Unable to create external cache directory"); |
| 795 | return null; |
| 796 | } |
| 797 | } |
| 798 | return mExternalCacheDir; |
| 799 | } |
| 800 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 801 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 802 | @Override |
| 803 | public File getFileStreamPath(String name) { |
| 804 | return makeFilename(getFilesDir(), name); |
| 805 | } |
| 806 | |
| 807 | @Override |
| 808 | public String[] fileList() { |
| 809 | final String[] list = getFilesDir().list(); |
| 810 | return (list != null) ? list : EMPTY_FILE_LIST; |
| 811 | } |
| 812 | |
| 813 | @Override |
| 814 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 815 | return openOrCreateDatabase(name, mode, factory, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | @Override |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 819 | public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, |
| 820 | DatabaseErrorHandler errorHandler) { |
| 821 | File f = validateFilePath(name, true); |
Jeff Brown | 47847f3 | 2012-03-22 19:13:11 -0700 | [diff] [blame] | 822 | int flags = SQLiteDatabase.CREATE_IF_NECESSARY; |
| 823 | if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) { |
| 824 | flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING; |
| 825 | } |
| 826 | SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler); |
Vasu Nori | 74f170f | 2010-06-01 18:06:18 -0700 | [diff] [blame] | 827 | setFilePermissionsFromMode(f.getPath(), mode, 0); |
| 828 | return db; |
| 829 | } |
| 830 | |
| 831 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | public boolean deleteDatabase(String name) { |
| 833 | try { |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 834 | File f = validateFilePath(name, false); |
Jeff Brown | 79087e4 | 2012-03-01 19:52:44 -0800 | [diff] [blame] | 835 | return SQLiteDatabase.deleteDatabase(f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 836 | } catch (Exception e) { |
| 837 | } |
| 838 | return false; |
| 839 | } |
| 840 | |
| 841 | @Override |
| 842 | public File getDatabasePath(String name) { |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 843 | return validateFilePath(name, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | @Override |
| 847 | public String[] databaseList() { |
| 848 | final String[] list = getDatabasesDir().list(); |
| 849 | return (list != null) ? list : EMPTY_FILE_LIST; |
| 850 | } |
| 851 | |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 852 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | private File getDatabasesDir() { |
| 854 | synchronized (mSync) { |
| 855 | if (mDatabasesDir == null) { |
| 856 | mDatabasesDir = new File(getDataDirFile(), "databases"); |
| 857 | } |
| 858 | if (mDatabasesDir.getPath().equals("databases")) { |
| 859 | mDatabasesDir = new File("/data/system"); |
| 860 | } |
| 861 | return mDatabasesDir; |
| 862 | } |
| 863 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 864 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | @Override |
| 866 | public Drawable getWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 867 | return getWallpaperManager().getDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | @Override |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 871 | public Drawable peekWallpaper() { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 872 | return getWallpaperManager().peekDrawable(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | @Override |
| 876 | public int getWallpaperDesiredMinimumWidth() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 877 | return getWallpaperManager().getDesiredMinimumWidth(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | @Override |
| 881 | public int getWallpaperDesiredMinimumHeight() { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 882 | return getWallpaperManager().getDesiredMinimumHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | @Override |
| 886 | public void setWallpaper(Bitmap bitmap) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 887 | getWallpaperManager().setBitmap(bitmap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | @Override |
| 891 | public void setWallpaper(InputStream data) throws IOException { |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 892 | getWallpaperManager().setStream(data); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | @Override |
| 896 | public void clearWallpaper() throws IOException { |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 897 | getWallpaperManager().clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | @Override |
| 901 | public void startActivity(Intent intent) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 902 | startActivity(intent, null); |
| 903 | } |
| 904 | |
Amith Yamasani | 8264408 | 2012-08-03 13:09:11 -0700 | [diff] [blame] | 905 | /** @hide */ |
| 906 | @Override |
| 907 | public void startActivityAsUser(Intent intent, int userId) { |
| 908 | try { |
| 909 | ActivityManagerNative.getDefault().startActivityAsUser( |
| 910 | mMainThread.getApplicationThread(), intent, |
| 911 | intent.resolveTypeIfNeeded(getContentResolver()), |
| 912 | null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, null, userId); |
| 913 | } catch (RemoteException re) { |
| 914 | } |
| 915 | } |
| 916 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 917 | @Override |
| 918 | public void startActivity(Intent intent, Bundle options) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 920 | throw new AndroidRuntimeException( |
| 921 | "Calling startActivity() from outside of an Activity " |
| 922 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag." |
| 923 | + " Is this really what you want?"); |
| 924 | } |
| 925 | mMainThread.getInstrumentation().execStartActivity( |
Dianne Hackborn | 6e8304e | 2010-05-14 00:42:53 -0700 | [diff] [blame] | 926 | getOuterContext(), mMainThread.getApplicationThread(), null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 927 | (Activity)null, intent, -1, options); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | } |
| 929 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame^] | 930 | /** @hide */ |
| 931 | @Override |
| 932 | public void startActivityAsUser(Intent intent, Bundle options, int userId) { |
| 933 | try { |
| 934 | ActivityManagerNative.getDefault().startActivityAsUser( |
| 935 | mMainThread.getApplicationThread(), intent, |
| 936 | intent.resolveTypeIfNeeded(getContentResolver()), |
| 937 | null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null, options, userId); |
| 938 | } catch (RemoteException re) { |
| 939 | } |
| 940 | } |
| 941 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | @Override |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 943 | public void startActivities(Intent[] intents) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 944 | startActivities(intents, null); |
| 945 | } |
| 946 | |
| 947 | @Override |
| 948 | public void startActivities(Intent[] intents, Bundle options) { |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 949 | if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { |
| 950 | throw new AndroidRuntimeException( |
| 951 | "Calling startActivities() from outside of an Activity " |
| 952 | + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent." |
| 953 | + " Is this really what you want?"); |
| 954 | } |
| 955 | mMainThread.getInstrumentation().execStartActivities( |
| 956 | getOuterContext(), mMainThread.getApplicationThread(), null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 957 | (Activity)null, intents, options); |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | @Override |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 961 | public void startIntentSender(IntentSender intent, |
| 962 | Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) |
| 963 | throws IntentSender.SendIntentException { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 964 | startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null); |
| 965 | } |
| 966 | |
| 967 | @Override |
| 968 | public void startIntentSender(IntentSender intent, Intent fillInIntent, |
| 969 | int flagsMask, int flagsValues, int extraFlags, Bundle options) |
| 970 | throws IntentSender.SendIntentException { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 971 | try { |
| 972 | String resolvedType = null; |
| 973 | if (fillInIntent != null) { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 974 | fillInIntent.setAllowFds(false); |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 975 | resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); |
| 976 | } |
| 977 | int result = ActivityManagerNative.getDefault() |
| 978 | .startActivityIntentSender(mMainThread.getApplicationThread(), intent, |
| 979 | fillInIntent, resolvedType, null, null, |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 980 | 0, flagsMask, flagsValues, options); |
| 981 | if (result == ActivityManager.START_CANCELED) { |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 982 | throw new IntentSender.SendIntentException(); |
| 983 | } |
| 984 | Instrumentation.checkStartActivityResult(result, null); |
| 985 | } catch (RemoteException e) { |
| 986 | } |
| 987 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 988 | |
Dianne Hackborn | fa82f22 | 2009-09-17 15:14:12 -0700 | [diff] [blame] | 989 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | public void sendBroadcast(Intent intent) { |
| 991 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 992 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 993 | intent.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | ActivityManagerNative.getDefault().broadcastIntent( |
| 995 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 996 | Activity.RESULT_OK, null, null, null, false, false, |
| 997 | Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | } catch (RemoteException e) { |
| 999 | } |
| 1000 | } |
| 1001 | |
Amith Yamasani | 67cf7d3 | 2012-02-16 14:31:23 -0800 | [diff] [blame] | 1002 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1003 | public void sendBroadcast(Intent intent, String receiverPermission) { |
| 1004 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1005 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1006 | intent.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1008 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1009 | Activity.RESULT_OK, null, null, receiverPermission, false, false, |
| 1010 | Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | } catch (RemoteException e) { |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | @Override |
| 1016 | public void sendOrderedBroadcast(Intent intent, |
| 1017 | String receiverPermission) { |
| 1018 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1019 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1020 | intent.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1021 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1022 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1023 | Activity.RESULT_OK, null, null, receiverPermission, true, false, |
| 1024 | Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | } catch (RemoteException e) { |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
| 1030 | public void sendOrderedBroadcast(Intent intent, |
| 1031 | String receiverPermission, BroadcastReceiver resultReceiver, |
| 1032 | Handler scheduler, int initialCode, String initialData, |
| 1033 | Bundle initialExtras) { |
| 1034 | IIntentReceiver rd = null; |
| 1035 | if (resultReceiver != null) { |
| 1036 | if (mPackageInfo != null) { |
| 1037 | if (scheduler == null) { |
| 1038 | scheduler = mMainThread.getHandler(); |
| 1039 | } |
| 1040 | rd = mPackageInfo.getReceiverDispatcher( |
| 1041 | resultReceiver, getOuterContext(), scheduler, |
| 1042 | mMainThread.getInstrumentation(), false); |
| 1043 | } else { |
| 1044 | if (scheduler == null) { |
| 1045 | scheduler = mMainThread.getHandler(); |
| 1046 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1047 | rd = new LoadedApk.ReceiverDispatcher( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1049 | } |
| 1050 | } |
| 1051 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1052 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1053 | intent.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1054 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1055 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1056 | initialCode, initialData, initialExtras, receiverPermission, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1057 | true, false, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | } catch (RemoteException e) { |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | @Override |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1063 | public void sendBroadcastToUser(Intent intent, int userHandle) { |
| 1064 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1065 | try { |
| 1066 | intent.setAllowFds(false); |
| 1067 | ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(), |
| 1068 | intent, resolvedType, null, Activity.RESULT_OK, null, null, null, false, false, |
| 1069 | userHandle); |
| 1070 | } catch (RemoteException e) { |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | @Override |
| 1075 | public void sendOrderedBroadcastToUser(Intent intent, int userHandle, |
| 1076 | BroadcastReceiver resultReceiver, Handler scheduler, |
| 1077 | int initialCode, String initialData, Bundle initialExtras) { |
| 1078 | IIntentReceiver rd = null; |
| 1079 | if (resultReceiver != null) { |
| 1080 | if (mPackageInfo != null) { |
| 1081 | if (scheduler == null) { |
| 1082 | scheduler = mMainThread.getHandler(); |
| 1083 | } |
| 1084 | rd = mPackageInfo.getReceiverDispatcher( |
| 1085 | resultReceiver, getOuterContext(), scheduler, |
| 1086 | mMainThread.getInstrumentation(), false); |
| 1087 | } else { |
| 1088 | if (scheduler == null) { |
| 1089 | scheduler = mMainThread.getHandler(); |
| 1090 | } |
| 1091 | rd = new LoadedApk.ReceiverDispatcher( |
| 1092 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1093 | } |
| 1094 | } |
| 1095 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1096 | try { |
| 1097 | intent.setAllowFds(false); |
| 1098 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1099 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1100 | initialCode, initialData, initialExtras, null, |
| 1101 | true, false, userHandle); |
| 1102 | } catch (RemoteException e) { |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | public void sendStickyBroadcast(Intent intent) { |
| 1108 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1109 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1110 | intent.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1112 | mMainThread.getApplicationThread(), intent, resolvedType, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1113 | Activity.RESULT_OK, null, null, null, false, true, |
| 1114 | Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | } catch (RemoteException e) { |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | @Override |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1120 | public void sendStickyOrderedBroadcast(Intent intent, |
| 1121 | BroadcastReceiver resultReceiver, |
| 1122 | Handler scheduler, int initialCode, String initialData, |
| 1123 | Bundle initialExtras) { |
| 1124 | IIntentReceiver rd = null; |
| 1125 | if (resultReceiver != null) { |
| 1126 | if (mPackageInfo != null) { |
| 1127 | if (scheduler == null) { |
| 1128 | scheduler = mMainThread.getHandler(); |
| 1129 | } |
| 1130 | rd = mPackageInfo.getReceiverDispatcher( |
| 1131 | resultReceiver, getOuterContext(), scheduler, |
| 1132 | mMainThread.getInstrumentation(), false); |
| 1133 | } else { |
| 1134 | if (scheduler == null) { |
| 1135 | scheduler = mMainThread.getHandler(); |
| 1136 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1137 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1138 | resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); |
| 1139 | } |
| 1140 | } |
| 1141 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1142 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1143 | intent.setAllowFds(false); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1144 | ActivityManagerNative.getDefault().broadcastIntent( |
| 1145 | mMainThread.getApplicationThread(), intent, resolvedType, rd, |
| 1146 | initialCode, initialData, initialExtras, null, |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1147 | true, true, Binder.getOrigCallingUser()); |
Dianne Hackborn | efa199f | 2009-09-19 12:03:15 -0700 | [diff] [blame] | 1148 | } catch (RemoteException e) { |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | public void removeStickyBroadcast(Intent intent) { |
| 1154 | String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); |
| 1155 | if (resolvedType != null) { |
| 1156 | intent = new Intent(intent); |
| 1157 | intent.setDataAndType(intent.getData(), resolvedType); |
| 1158 | } |
| 1159 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1160 | intent.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | ActivityManagerNative.getDefault().unbroadcastIntent( |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1162 | mMainThread.getApplicationThread(), intent, Binder.getOrigCallingUser()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | } catch (RemoteException e) { |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | @Override |
| 1168 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { |
| 1169 | return registerReceiver(receiver, filter, null, null); |
| 1170 | } |
| 1171 | |
| 1172 | @Override |
| 1173 | public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, |
| 1174 | String broadcastPermission, Handler scheduler) { |
| 1175 | return registerReceiverInternal(receiver, filter, broadcastPermission, |
| 1176 | scheduler, getOuterContext()); |
| 1177 | } |
| 1178 | |
| 1179 | private Intent registerReceiverInternal(BroadcastReceiver receiver, |
| 1180 | IntentFilter filter, String broadcastPermission, |
| 1181 | Handler scheduler, Context context) { |
| 1182 | IIntentReceiver rd = null; |
| 1183 | if (receiver != null) { |
| 1184 | if (mPackageInfo != null && context != null) { |
| 1185 | if (scheduler == null) { |
| 1186 | scheduler = mMainThread.getHandler(); |
| 1187 | } |
| 1188 | rd = mPackageInfo.getReceiverDispatcher( |
| 1189 | receiver, context, scheduler, |
| 1190 | mMainThread.getInstrumentation(), true); |
| 1191 | } else { |
| 1192 | if (scheduler == null) { |
| 1193 | scheduler = mMainThread.getHandler(); |
| 1194 | } |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1195 | rd = new LoadedApk.ReceiverDispatcher( |
Dianne Hackborn | 399cccb | 2010-04-13 22:57:49 -0700 | [diff] [blame] | 1196 | receiver, context, scheduler, null, true).getIIntentReceiver(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | try { |
| 1200 | return ActivityManagerNative.getDefault().registerReceiver( |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1201 | mMainThread.getApplicationThread(), mBasePackageName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | rd, filter, broadcastPermission); |
| 1203 | } catch (RemoteException e) { |
| 1204 | return null; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | @Override |
| 1209 | public void unregisterReceiver(BroadcastReceiver receiver) { |
| 1210 | if (mPackageInfo != null) { |
| 1211 | IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher( |
| 1212 | getOuterContext(), receiver); |
| 1213 | try { |
| 1214 | ActivityManagerNative.getDefault().unregisterReceiver(rd); |
| 1215 | } catch (RemoteException e) { |
| 1216 | } |
| 1217 | } else { |
| 1218 | throw new RuntimeException("Not supported in system context"); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | @Override |
| 1223 | public ComponentName startService(Intent service) { |
| 1224 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1225 | service.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | ComponentName cn = ActivityManagerNative.getDefault().startService( |
| 1227 | mMainThread.getApplicationThread(), service, |
| 1228 | service.resolveTypeIfNeeded(getContentResolver())); |
| 1229 | if (cn != null && cn.getPackageName().equals("!")) { |
| 1230 | throw new SecurityException( |
| 1231 | "Not allowed to start service " + service |
| 1232 | + " without permission " + cn.getClassName()); |
| 1233 | } |
| 1234 | return cn; |
| 1235 | } catch (RemoteException e) { |
| 1236 | return null; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | @Override |
| 1241 | public boolean stopService(Intent service) { |
| 1242 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1243 | service.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1244 | int res = ActivityManagerNative.getDefault().stopService( |
| 1245 | mMainThread.getApplicationThread(), service, |
| 1246 | service.resolveTypeIfNeeded(getContentResolver())); |
| 1247 | if (res < 0) { |
| 1248 | throw new SecurityException( |
| 1249 | "Not allowed to stop service " + service); |
| 1250 | } |
| 1251 | return res != 0; |
| 1252 | } catch (RemoteException e) { |
| 1253 | return false; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | @Override |
| 1258 | public boolean bindService(Intent service, ServiceConnection conn, |
| 1259 | int flags) { |
Amith Yamasani | 37ce3a8 | 2012-02-06 12:04:42 -0800 | [diff] [blame] | 1260 | return bindService(service, conn, flags, UserId.getUserId(Process.myUid())); |
| 1261 | } |
| 1262 | |
| 1263 | /** @hide */ |
| 1264 | @Override |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1265 | public boolean bindService(Intent service, ServiceConnection conn, int flags, int userHandle) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1266 | IServiceConnection sd; |
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 | sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), |
| 1272 | mMainThread.getHandler(), flags); |
| 1273 | } else { |
| 1274 | throw new RuntimeException("Not supported in system context"); |
| 1275 | } |
| 1276 | try { |
Dianne Hackborn | c68c913 | 2011-07-29 01:25:18 -0700 | [diff] [blame] | 1277 | IBinder token = getActivityToken(); |
| 1278 | if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null |
| 1279 | && mPackageInfo.getApplicationInfo().targetSdkVersion |
| 1280 | < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 1281 | flags |= BIND_WAIVE_PRIORITY; |
| 1282 | } |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1283 | service.setAllowFds(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | int res = ActivityManagerNative.getDefault().bindService( |
| 1285 | mMainThread.getApplicationThread(), getActivityToken(), |
| 1286 | service, service.resolveTypeIfNeeded(getContentResolver()), |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1287 | sd, flags, userHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1288 | if (res < 0) { |
| 1289 | throw new SecurityException( |
| 1290 | "Not allowed to bind to service " + service); |
| 1291 | } |
| 1292 | return res != 0; |
| 1293 | } catch (RemoteException e) { |
| 1294 | return false; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | @Override |
| 1299 | public void unbindService(ServiceConnection conn) { |
Christopher Tate | 79b3317 | 2012-06-18 14:54:21 -0700 | [diff] [blame] | 1300 | if (conn == null) { |
| 1301 | throw new IllegalArgumentException("connection is null"); |
| 1302 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | if (mPackageInfo != null) { |
| 1304 | IServiceConnection sd = mPackageInfo.forgetServiceDispatcher( |
| 1305 | getOuterContext(), conn); |
| 1306 | try { |
| 1307 | ActivityManagerNative.getDefault().unbindService(sd); |
| 1308 | } catch (RemoteException e) { |
| 1309 | } |
| 1310 | } else { |
| 1311 | throw new RuntimeException("Not supported in system context"); |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | @Override |
| 1316 | public boolean startInstrumentation(ComponentName className, |
| 1317 | String profileFile, Bundle arguments) { |
| 1318 | try { |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 1319 | if (arguments != null) { |
| 1320 | arguments.setAllowFds(false); |
| 1321 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1322 | return ActivityManagerNative.getDefault().startInstrumentation( |
| 1323 | className, profileFile, 0, arguments, null); |
| 1324 | } catch (RemoteException e) { |
| 1325 | // System has crashed, nothing we can do. |
| 1326 | } |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
| 1330 | @Override |
| 1331 | public Object getSystemService(String name) { |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 1332 | ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name); |
| 1333 | return fetcher == null ? null : fetcher.getService(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
Dianne Hackborn | 8cc6a50 | 2009-08-05 21:29:42 -0700 | [diff] [blame] | 1336 | private WallpaperManager getWallpaperManager() { |
Brad Fitzpatrick | 224ba0c | 2010-11-12 12:22:15 -0800 | [diff] [blame] | 1337 | return (WallpaperManager) WALLPAPER_FETCHER.getService(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1340 | /* package */ static DropBoxManager createDropBoxManager() { |
| 1341 | IBinder b = ServiceManager.getService(DROPBOX_SERVICE); |
| 1342 | IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b); |
Brad Fitzpatrick | ec062f6 | 2010-11-03 09:56:54 -0700 | [diff] [blame] | 1343 | if (service == null) { |
| 1344 | // Don't return a DropBoxManager that will NPE upon use. |
| 1345 | // This also avoids caching a broken DropBoxManager in |
| 1346 | // getDropBoxManager during early boot, before the |
| 1347 | // DROPBOX_SERVICE is registered. |
| 1348 | return null; |
| 1349 | } |
Brad Fitzpatrick | 438d059 | 2010-06-10 12:19:19 -0700 | [diff] [blame] | 1350 | return new DropBoxManager(service); |
| 1351 | } |
| 1352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | @Override |
| 1354 | public int checkPermission(String permission, int pid, int uid) { |
| 1355 | if (permission == null) { |
| 1356 | throw new IllegalArgumentException("permission is null"); |
| 1357 | } |
| 1358 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | try { |
| 1360 | return ActivityManagerNative.getDefault().checkPermission( |
| 1361 | permission, pid, uid); |
| 1362 | } catch (RemoteException e) { |
| 1363 | return PackageManager.PERMISSION_DENIED; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | @Override |
| 1368 | public int checkCallingPermission(String permission) { |
| 1369 | if (permission == null) { |
| 1370 | throw new IllegalArgumentException("permission is null"); |
| 1371 | } |
| 1372 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | int pid = Binder.getCallingPid(); |
| 1374 | if (pid != Process.myPid()) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1375 | return checkPermission(permission, pid, Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | } |
| 1377 | return PackageManager.PERMISSION_DENIED; |
| 1378 | } |
| 1379 | |
| 1380 | @Override |
| 1381 | public int checkCallingOrSelfPermission(String permission) { |
| 1382 | if (permission == null) { |
| 1383 | throw new IllegalArgumentException("permission is null"); |
| 1384 | } |
| 1385 | |
| 1386 | return checkPermission(permission, Binder.getCallingPid(), |
| 1387 | Binder.getCallingUid()); |
| 1388 | } |
| 1389 | |
| 1390 | private void enforce( |
| 1391 | String permission, int resultOfCheck, |
| 1392 | boolean selfToo, int uid, String message) { |
| 1393 | if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { |
| 1394 | throw new SecurityException( |
| 1395 | (message != null ? (message + ": ") : "") + |
| 1396 | (selfToo |
| 1397 | ? "Neither user " + uid + " nor current process has " |
| 1398 | : "User " + uid + " does not have ") + |
| 1399 | permission + |
| 1400 | "."); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | public void enforcePermission( |
| 1405 | String permission, int pid, int uid, String message) { |
| 1406 | enforce(permission, |
| 1407 | checkPermission(permission, pid, uid), |
| 1408 | false, |
| 1409 | uid, |
| 1410 | message); |
| 1411 | } |
| 1412 | |
| 1413 | public void enforceCallingPermission(String permission, String message) { |
| 1414 | enforce(permission, |
| 1415 | checkCallingPermission(permission), |
| 1416 | false, |
| 1417 | Binder.getCallingUid(), |
| 1418 | message); |
| 1419 | } |
| 1420 | |
| 1421 | public void enforceCallingOrSelfPermission( |
| 1422 | String permission, String message) { |
| 1423 | enforce(permission, |
| 1424 | checkCallingOrSelfPermission(permission), |
| 1425 | true, |
| 1426 | Binder.getCallingUid(), |
| 1427 | message); |
| 1428 | } |
| 1429 | |
| 1430 | @Override |
| 1431 | public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { |
| 1432 | try { |
| 1433 | ActivityManagerNative.getDefault().grantUriPermission( |
| 1434 | mMainThread.getApplicationThread(), toPackage, uri, |
| 1435 | modeFlags); |
| 1436 | } catch (RemoteException e) { |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | @Override |
| 1441 | public void revokeUriPermission(Uri uri, int modeFlags) { |
| 1442 | try { |
| 1443 | ActivityManagerNative.getDefault().revokeUriPermission( |
| 1444 | mMainThread.getApplicationThread(), uri, |
| 1445 | modeFlags); |
| 1446 | } catch (RemoteException e) { |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | @Override |
| 1451 | 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] | 1452 | try { |
| 1453 | return ActivityManagerNative.getDefault().checkUriPermission( |
| 1454 | uri, pid, uid, modeFlags); |
| 1455 | } catch (RemoteException e) { |
| 1456 | return PackageManager.PERMISSION_DENIED; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | @Override |
| 1461 | public int checkCallingUriPermission(Uri uri, int modeFlags) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | int pid = Binder.getCallingPid(); |
| 1463 | if (pid != Process.myPid()) { |
| 1464 | return checkUriPermission(uri, pid, |
| 1465 | Binder.getCallingUid(), modeFlags); |
| 1466 | } |
| 1467 | return PackageManager.PERMISSION_DENIED; |
| 1468 | } |
| 1469 | |
| 1470 | @Override |
| 1471 | public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { |
| 1472 | return checkUriPermission(uri, Binder.getCallingPid(), |
| 1473 | Binder.getCallingUid(), modeFlags); |
| 1474 | } |
| 1475 | |
| 1476 | @Override |
| 1477 | public int checkUriPermission(Uri uri, String readPermission, |
| 1478 | String writePermission, int pid, int uid, int modeFlags) { |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 1479 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission=" |
| 1481 | + readPermission + " writePermission=" + writePermission |
| 1482 | + " pid=" + pid + " uid=" + uid + " mode" + modeFlags); |
| 1483 | } |
| 1484 | if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { |
| 1485 | if (readPermission == null |
| 1486 | || checkPermission(readPermission, pid, uid) |
| 1487 | == PackageManager.PERMISSION_GRANTED) { |
| 1488 | return PackageManager.PERMISSION_GRANTED; |
| 1489 | } |
| 1490 | } |
| 1491 | if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { |
| 1492 | if (writePermission == null |
| 1493 | || checkPermission(writePermission, pid, uid) |
| 1494 | == PackageManager.PERMISSION_GRANTED) { |
| 1495 | return PackageManager.PERMISSION_GRANTED; |
| 1496 | } |
| 1497 | } |
| 1498 | return uri != null ? checkUriPermission(uri, pid, uid, modeFlags) |
| 1499 | : PackageManager.PERMISSION_DENIED; |
| 1500 | } |
| 1501 | |
| 1502 | private String uriModeFlagToString(int uriModeFlags) { |
| 1503 | switch (uriModeFlags) { |
| 1504 | case Intent.FLAG_GRANT_READ_URI_PERMISSION | |
| 1505 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION: |
| 1506 | return "read and write"; |
| 1507 | case Intent.FLAG_GRANT_READ_URI_PERMISSION: |
| 1508 | return "read"; |
| 1509 | case Intent.FLAG_GRANT_WRITE_URI_PERMISSION: |
| 1510 | return "write"; |
| 1511 | } |
| 1512 | throw new IllegalArgumentException( |
| 1513 | "Unknown permission mode flags: " + uriModeFlags); |
| 1514 | } |
| 1515 | |
| 1516 | private void enforceForUri( |
| 1517 | int modeFlags, int resultOfCheck, boolean selfToo, |
| 1518 | int uid, Uri uri, String message) { |
| 1519 | if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { |
| 1520 | throw new SecurityException( |
| 1521 | (message != null ? (message + ": ") : "") + |
| 1522 | (selfToo |
| 1523 | ? "Neither user " + uid + " nor current process has " |
| 1524 | : "User " + uid + " does not have ") + |
| 1525 | uriModeFlagToString(modeFlags) + |
| 1526 | " permission on " + |
| 1527 | uri + |
| 1528 | "."); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | public void enforceUriPermission( |
| 1533 | Uri uri, int pid, int uid, int modeFlags, String message) { |
| 1534 | enforceForUri( |
| 1535 | modeFlags, checkUriPermission(uri, pid, uid, modeFlags), |
| 1536 | false, uid, uri, message); |
| 1537 | } |
| 1538 | |
| 1539 | public void enforceCallingUriPermission( |
| 1540 | Uri uri, int modeFlags, String message) { |
| 1541 | enforceForUri( |
| 1542 | modeFlags, checkCallingUriPermission(uri, modeFlags), |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1543 | false, |
| 1544 | Binder.getCallingUid(), uri, message); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | public void enforceCallingOrSelfUriPermission( |
| 1548 | Uri uri, int modeFlags, String message) { |
| 1549 | enforceForUri( |
| 1550 | modeFlags, |
| 1551 | checkCallingOrSelfUriPermission(uri, modeFlags), true, |
| 1552 | Binder.getCallingUid(), uri, message); |
| 1553 | } |
| 1554 | |
| 1555 | public void enforceUriPermission( |
| 1556 | Uri uri, String readPermission, String writePermission, |
| 1557 | int pid, int uid, int modeFlags, String message) { |
| 1558 | enforceForUri(modeFlags, |
| 1559 | checkUriPermission( |
| 1560 | uri, readPermission, writePermission, pid, uid, |
| 1561 | modeFlags), |
| 1562 | false, |
| 1563 | uid, |
| 1564 | uri, |
| 1565 | message); |
| 1566 | } |
| 1567 | |
| 1568 | @Override |
| 1569 | public Context createPackageContext(String packageName, int flags) |
| 1570 | throws PackageManager.NameNotFoundException { |
| 1571 | if (packageName.equals("system") || packageName.equals("android")) { |
Jeff Sharkey | 816e4f7 | 2012-04-22 17:08:52 -0700 | [diff] [blame] | 1572 | final ContextImpl context = new ContextImpl(mMainThread.getSystemContext()); |
| 1573 | context.mBasePackageName = mBasePackageName; |
| 1574 | return context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | } |
| 1576 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1577 | LoadedApk pi = |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1578 | mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | if (pi != null) { |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 1580 | ContextImpl c = new ContextImpl(); |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 1581 | c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1582 | c.init(pi, null, mMainThread, mResources, mBasePackageName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1583 | if (c.mResources != null) { |
| 1584 | return c; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | // Should be a better exception. |
| 1589 | throw new PackageManager.NameNotFoundException( |
| 1590 | "Application package " + packageName + " not found"); |
| 1591 | } |
| 1592 | |
Romain Guy | 870e09f | 2009-07-06 16:35:25 -0700 | [diff] [blame] | 1593 | @Override |
| 1594 | public boolean isRestricted() { |
| 1595 | return mRestricted; |
| 1596 | } |
| 1597 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | private File getDataDirFile() { |
| 1599 | if (mPackageInfo != null) { |
| 1600 | return mPackageInfo.getDataDirFile(); |
| 1601 | } |
| 1602 | throw new RuntimeException("Not supported in system context"); |
| 1603 | } |
| 1604 | |
| 1605 | @Override |
| 1606 | public File getDir(String name, int mode) { |
| 1607 | name = "app_" + name; |
| 1608 | File file = makeFilename(getDataDirFile(), name); |
| 1609 | if (!file.exists()) { |
| 1610 | file.mkdir(); |
| 1611 | setFilePermissionsFromMode(file.getPath(), mode, |
| 1612 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH); |
| 1613 | } |
| 1614 | return file; |
| 1615 | } |
| 1616 | |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 1617 | static ContextImpl createSystemContext(ActivityThread mainThread) { |
| 1618 | ContextImpl context = new ContextImpl(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1619 | context.init(Resources.getSystem(), mainThread); |
| 1620 | return context; |
| 1621 | } |
| 1622 | |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 1623 | ContextImpl() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1624 | mOuterContext = this; |
| 1625 | } |
| 1626 | |
| 1627 | /** |
| 1628 | * Create a new ApplicationContext from an existing one. The new one |
| 1629 | * works and operates the same as the one it is copying. |
| 1630 | * |
| 1631 | * @param context Existing application context. |
| 1632 | */ |
Dianne Hackborn | 2155637 | 2010-02-04 16:34:40 -0800 | [diff] [blame] | 1633 | public ContextImpl(ContextImpl context) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1634 | mPackageInfo = context.mPackageInfo; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1635 | mBasePackageName = context.mBasePackageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1636 | mResources = context.mResources; |
| 1637 | mMainThread = context.mMainThread; |
| 1638 | mContentResolver = context.mContentResolver; |
| 1639 | mOuterContext = this; |
| 1640 | } |
| 1641 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1642 | final void init(LoadedApk packageInfo, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1643 | IBinder activityToken, ActivityThread mainThread) { |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1644 | init(packageInfo, activityToken, mainThread, null, null); |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
Dianne Hackborn | 01e4cfc | 2010-06-24 15:07:24 -0700 | [diff] [blame] | 1647 | final void init(LoadedApk packageInfo, |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1648 | IBinder activityToken, ActivityThread mainThread, |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1649 | Resources container, String basePackageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | mPackageInfo = packageInfo; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1651 | mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1652 | mResources = mPackageInfo.getResources(mainThread); |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1653 | |
Dianne Hackborn | 559a787 | 2010-04-07 18:19:41 -0700 | [diff] [blame] | 1654 | if (mResources != null && container != null |
| 1655 | && container.getCompatibilityInfo().applicationScale != |
| 1656 | mResources.getCompatibilityInfo().applicationScale) { |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1657 | if (DEBUG) { |
| 1658 | Log.d(TAG, "loaded context has different scaling. Using container's" + |
| 1659 | " compatiblity info:" + container.getDisplayMetrics()); |
| 1660 | } |
| 1661 | mResources = mainThread.getTopLevelResources( |
Dianne Hackborn | e2515ee | 2011-04-27 18:52:56 -0400 | [diff] [blame] | 1662 | mPackageInfo.getResDir(), container.getCompatibilityInfo()); |
Mitsuru Oshima | ba3ba57 | 2009-07-08 18:49:26 -0700 | [diff] [blame] | 1663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | mMainThread = mainThread; |
| 1665 | mContentResolver = new ApplicationContentResolver(this, mainThread); |
| 1666 | |
| 1667 | setActivityToken(activityToken); |
| 1668 | } |
| 1669 | |
| 1670 | final void init(Resources resources, ActivityThread mainThread) { |
| 1671 | mPackageInfo = null; |
Dianne Hackborn | 6c418d5 | 2011-06-29 14:05:33 -0700 | [diff] [blame] | 1672 | mBasePackageName = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | mResources = resources; |
| 1674 | mMainThread = mainThread; |
| 1675 | mContentResolver = new ApplicationContentResolver(this, mainThread); |
| 1676 | } |
| 1677 | |
| 1678 | final void scheduleFinalCleanup(String who, String what) { |
| 1679 | mMainThread.scheduleContextCleanup(this, who, what); |
| 1680 | } |
| 1681 | |
| 1682 | final void performFinalCleanup(String who, String what) { |
| 1683 | //Log.i(TAG, "Cleanup up context: " + this); |
| 1684 | mPackageInfo.removeContextRegistrations(getOuterContext(), who, what); |
| 1685 | } |
| 1686 | |
| 1687 | final Context getReceiverRestrictedContext() { |
| 1688 | if (mReceiverRestrictedContext != null) { |
| 1689 | return mReceiverRestrictedContext; |
| 1690 | } |
| 1691 | return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext()); |
| 1692 | } |
| 1693 | |
| 1694 | final void setActivityToken(IBinder token) { |
| 1695 | mActivityToken = token; |
| 1696 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1697 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | final void setOuterContext(Context context) { |
| 1699 | mOuterContext = context; |
| 1700 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1701 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1702 | final Context getOuterContext() { |
| 1703 | return mOuterContext; |
| 1704 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1706 | final IBinder getActivityToken() { |
| 1707 | return mActivityToken; |
| 1708 | } |
| 1709 | |
Brad Fitzpatrick | d3da440 | 2010-11-10 08:27:11 -0800 | [diff] [blame] | 1710 | static void setFilePermissionsFromMode(String name, int mode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | int extraPermissions) { |
| 1712 | int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR |
| 1713 | |FileUtils.S_IRGRP|FileUtils.S_IWGRP |
| 1714 | |extraPermissions; |
| 1715 | if ((mode&MODE_WORLD_READABLE) != 0) { |
| 1716 | perms |= FileUtils.S_IROTH; |
| 1717 | } |
| 1718 | if ((mode&MODE_WORLD_WRITEABLE) != 0) { |
| 1719 | perms |= FileUtils.S_IWOTH; |
| 1720 | } |
Mitsuru Oshima | 569076c | 2009-07-02 20:06:08 -0700 | [diff] [blame] | 1721 | if (DEBUG) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode) |
| 1723 | + ", perms=0x" + Integer.toHexString(perms)); |
| 1724 | } |
| 1725 | FileUtils.setPermissions(name, perms, -1, -1); |
| 1726 | } |
| 1727 | |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 1728 | private File validateFilePath(String name, boolean createDirectory) { |
| 1729 | File dir; |
| 1730 | File f; |
| 1731 | |
| 1732 | if (name.charAt(0) == File.separatorChar) { |
| 1733 | String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar)); |
| 1734 | dir = new File(dirPath); |
| 1735 | name = name.substring(name.lastIndexOf(File.separatorChar)); |
| 1736 | f = new File(dir, name); |
| 1737 | } else { |
| 1738 | dir = getDatabasesDir(); |
| 1739 | f = makeFilename(dir, name); |
| 1740 | } |
| 1741 | |
| 1742 | if (createDirectory && !dir.isDirectory() && dir.mkdir()) { |
| 1743 | FileUtils.setPermissions(dir.getPath(), |
| 1744 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 1745 | -1, -1); |
| 1746 | } |
| 1747 | |
| 1748 | return f; |
| 1749 | } |
| 1750 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | private File makeFilename(File base, String name) { |
| 1752 | if (name.indexOf(File.separatorChar) < 0) { |
| 1753 | return new File(base, name); |
| 1754 | } |
| 1755 | throw new IllegalArgumentException( |
Oscar Montemayor | a8529f6 | 2009-11-18 10:14:20 -0800 | [diff] [blame] | 1756 | "File " + name + " contains a path separator"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | // ---------------------------------------------------------------------- |
| 1760 | // ---------------------------------------------------------------------- |
| 1761 | // ---------------------------------------------------------------------- |
| 1762 | |
| 1763 | private static final class ApplicationContentResolver extends ContentResolver { |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1764 | public ApplicationContentResolver(Context context, ActivityThread mainThread) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1765 | super(context); |
| 1766 | mMainThread = mainThread; |
| 1767 | } |
| 1768 | |
| 1769 | @Override |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1770 | protected IContentProvider acquireProvider(Context context, String name) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1771 | return mMainThread.acquireProvider(context, name, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | @Override |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1775 | protected IContentProvider acquireExistingProvider(Context context, String name) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1776 | return mMainThread.acquireExistingProvider(context, name, true); |
Dianne Hackborn | cca1f0e | 2010-09-26 18:34:53 -0700 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | @Override |
| 1780 | public boolean releaseProvider(IContentProvider provider) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1781 | return mMainThread.releaseProvider(provider, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1782 | } |
Christian Mehlmauer | 5f5acca | 2010-06-25 19:06:18 +0200 | [diff] [blame] | 1783 | |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1784 | @Override |
| 1785 | protected IContentProvider acquireUnstableProvider(Context c, String name) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1786 | return mMainThread.acquireProvider(c, name, false); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | @Override |
| 1790 | public boolean releaseUnstableProvider(IContentProvider icp) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1791 | return mMainThread.releaseProvider(icp, false); |
| 1792 | } |
| 1793 | |
| 1794 | @Override |
| 1795 | public void unstableProviderDied(IContentProvider icp) { |
| 1796 | mMainThread.handleUnstableProviderDied(icp.asBinder(), true); |
Dianne Hackborn | 652b6d1 | 2012-05-09 18:18:40 -0700 | [diff] [blame] | 1797 | } |
| 1798 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1799 | private final ActivityThread mMainThread; |
| 1800 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | } |