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