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