ContextImpl.java revision 36e5f667bc889a7b1bc17f9a5bd44f33b2d47020
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; 20import com.android.internal.util.XmlUtils; 21import com.google.android.collect.Maps; 22 23import org.xmlpull.v1.XmlPullParserException; 24 25import android.content.BroadcastReceiver; 26import android.content.ComponentName; 27import android.content.ContentResolver; 28import android.content.Context; 29import android.content.ContextWrapper; 30import android.content.IContentProvider; 31import android.content.Intent; 32import android.content.IntentFilter; 33import android.content.IIntentReceiver; 34import android.content.IntentSender; 35import android.content.ReceiverCallNotAllowedException; 36import android.content.ServiceConnection; 37import android.content.SharedPreferences; 38import android.content.pm.ActivityInfo; 39import android.content.pm.ApplicationInfo; 40import android.content.pm.ComponentInfo; 41import android.content.pm.FeatureInfo; 42import android.content.pm.IPackageDataObserver; 43import android.content.pm.IPackageDeleteObserver; 44import android.content.pm.IPackageInstallObserver; 45import android.content.pm.IPackageMoveObserver; 46import android.content.pm.IPackageManager; 47import android.content.pm.IPackageStatsObserver; 48import android.content.pm.InstrumentationInfo; 49import android.content.pm.PackageInfo; 50import android.content.pm.PackageManager; 51import android.content.pm.PermissionGroupInfo; 52import android.content.pm.PermissionInfo; 53import android.content.pm.ProviderInfo; 54import android.content.pm.ResolveInfo; 55import android.content.pm.ServiceInfo; 56import android.content.pm.PackageParser.Package; 57import android.content.res.AssetManager; 58import android.content.res.Resources; 59import android.content.res.XmlResourceParser; 60import android.database.DatabaseErrorHandler; 61import android.database.sqlite.SQLiteDatabase; 62import android.database.sqlite.SQLiteDatabase.CursorFactory; 63import android.graphics.Bitmap; 64import android.graphics.drawable.Drawable; 65import android.hardware.SensorManager; 66import android.location.ILocationManager; 67import android.location.LocationManager; 68import android.media.AudioManager; 69import android.net.ConnectivityManager; 70import android.net.IConnectivityManager; 71import android.net.ThrottleManager; 72import android.net.IThrottleManager; 73import android.net.Uri; 74import android.net.wifi.IWifiManager; 75import android.net.wifi.WifiManager; 76import android.os.Binder; 77import android.os.Bundle; 78import android.os.DropBoxManager; 79import android.os.Environment; 80import android.os.FileUtils; 81import android.os.Handler; 82import android.os.IBinder; 83import android.os.IPowerManager; 84import android.os.Looper; 85import android.os.PowerManager; 86import android.os.Process; 87import android.os.RemoteException; 88import android.os.ServiceManager; 89import android.os.StatFs; 90import android.os.Vibrator; 91import android.os.FileUtils.FileStatus; 92import android.os.storage.StorageManager; 93import android.provider.Settings; 94import android.telephony.TelephonyManager; 95import android.text.ClipboardManager; 96import android.util.AndroidRuntimeException; 97import android.util.Log; 98import android.view.ContextThemeWrapper; 99import android.view.LayoutInflater; 100import android.view.WindowManagerImpl; 101import android.view.accessibility.AccessibilityManager; 102import android.view.inputmethod.InputMethodManager; 103import android.accounts.AccountManager; 104import android.accounts.IAccountManager; 105import android.app.admin.DevicePolicyManager; 106 107import com.android.internal.os.IDropBoxManagerService; 108 109import java.io.File; 110import java.io.FileInputStream; 111import java.io.FileNotFoundException; 112import java.io.FileOutputStream; 113import java.io.IOException; 114import java.io.InputStream; 115import java.lang.ref.WeakReference; 116import java.util.ArrayList; 117import java.util.HashMap; 118import java.util.HashSet; 119import java.util.Iterator; 120import java.util.List; 121import java.util.Map; 122import java.util.Set; 123import java.util.WeakHashMap; 124import java.util.Map.Entry; 125 126class ReceiverRestrictedContext extends ContextWrapper { 127 ReceiverRestrictedContext(Context base) { 128 super(base); 129 } 130 131 @Override 132 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 133 return registerReceiver(receiver, filter, null, null); 134 } 135 136 @Override 137 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 138 String broadcastPermission, Handler scheduler) { 139 throw new ReceiverCallNotAllowedException( 140 "IntentReceiver components are not allowed to register to receive intents"); 141 //ex.fillInStackTrace(); 142 //Log.e("IntentReceiver", ex.getMessage(), ex); 143 //return mContext.registerReceiver(receiver, filter, broadcastPermission, 144 // scheduler); 145 } 146 147 @Override 148 public boolean bindService(Intent service, ServiceConnection conn, int flags) { 149 throw new ReceiverCallNotAllowedException( 150 "IntentReceiver components are not allowed to bind to services"); 151 //ex.fillInStackTrace(); 152 //Log.e("IntentReceiver", ex.getMessage(), ex); 153 //return mContext.bindService(service, interfaceName, conn, flags); 154 } 155} 156 157/** 158 * Common implementation of Context API, which provides the base 159 * context object for Activity and other application components. 160 */ 161class ContextImpl extends Context { 162 private final static String TAG = "ApplicationContext"; 163 private final static boolean DEBUG = false; 164 private final static boolean DEBUG_ICONS = false; 165 166 private static final Object sSync = new Object(); 167 private static AlarmManager sAlarmManager; 168 private static PowerManager sPowerManager; 169 private static ConnectivityManager sConnectivityManager; 170 private static ThrottleManager sThrottleManager; 171 private static WifiManager sWifiManager; 172 private static LocationManager sLocationManager; 173 private static final HashMap<File, SharedPreferencesImpl> sSharedPrefs = 174 new HashMap<File, SharedPreferencesImpl>(); 175 176 private AudioManager mAudioManager; 177 /*package*/ LoadedApk mPackageInfo; 178 private Resources mResources; 179 /*package*/ ActivityThread mMainThread; 180 private Context mOuterContext; 181 private IBinder mActivityToken = null; 182 private ApplicationContentResolver mContentResolver; 183 private int mThemeResource = 0; 184 private Resources.Theme mTheme = null; 185 private PackageManager mPackageManager; 186 private NotificationManager mNotificationManager = null; 187 private ActivityManager mActivityManager = null; 188 private WallpaperManager mWallpaperManager = null; 189 private Context mReceiverRestrictedContext = null; 190 private SearchManager mSearchManager = null; 191 private SensorManager mSensorManager = null; 192 private StorageManager mStorageManager = null; 193 private Vibrator mVibrator = null; 194 private LayoutInflater mLayoutInflater = null; 195 private StatusBarManager mStatusBarManager = null; 196 private TelephonyManager mTelephonyManager = null; 197 private ClipboardManager mClipboardManager = null; 198 private boolean mRestricted; 199 private AccountManager mAccountManager; // protected by mSync 200 private DropBoxManager mDropBoxManager = null; 201 private DevicePolicyManager mDevicePolicyManager = null; 202 private UiModeManager mUiModeManager = null; 203 204 private final Object mSync = new Object(); 205 206 private File mDatabasesDir; 207 private File mPreferencesDir; 208 private File mFilesDir; 209 private File mCacheDir; 210 private File mExternalFilesDir; 211 private File mExternalCacheDir; 212 213 private static long sInstanceCount = 0; 214 215 private static final String[] EMPTY_FILE_LIST = {}; 216 217 // For debug only 218 /* 219 @Override 220 protected void finalize() throws Throwable { 221 super.finalize(); 222 --sInstanceCount; 223 } 224 */ 225 226 public static long getInstanceCount() { 227 return sInstanceCount; 228 } 229 230 @Override 231 public AssetManager getAssets() { 232 return mResources.getAssets(); 233 } 234 235 @Override 236 public Resources getResources() { 237 return mResources; 238 } 239 240 @Override 241 public PackageManager getPackageManager() { 242 if (mPackageManager != null) { 243 return mPackageManager; 244 } 245 246 IPackageManager pm = ActivityThread.getPackageManager(); 247 if (pm != null) { 248 // Doesn't matter if we make more than one instance. 249 return (mPackageManager = new ApplicationPackageManager(this, pm)); 250 } 251 252 return null; 253 } 254 255 @Override 256 public ContentResolver getContentResolver() { 257 return mContentResolver; 258 } 259 260 @Override 261 public Looper getMainLooper() { 262 return mMainThread.getLooper(); 263 } 264 265 @Override 266 public Context getApplicationContext() { 267 return (mPackageInfo != null) ? 268 mPackageInfo.getApplication() : mMainThread.getApplication(); 269 } 270 271 @Override 272 public void setTheme(int resid) { 273 mThemeResource = resid; 274 } 275 276 @Override 277 public Resources.Theme getTheme() { 278 if (mTheme == null) { 279 if (mThemeResource == 0) { 280 mThemeResource = com.android.internal.R.style.Theme; 281 } 282 mTheme = mResources.newTheme(); 283 mTheme.applyStyle(mThemeResource, true); 284 } 285 return mTheme; 286 } 287 288 @Override 289 public ClassLoader getClassLoader() { 290 return mPackageInfo != null ? 291 mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader(); 292 } 293 294 @Override 295 public String getPackageName() { 296 if (mPackageInfo != null) { 297 return mPackageInfo.getPackageName(); 298 } 299 throw new RuntimeException("Not supported in system context"); 300 } 301 302 @Override 303 public ApplicationInfo getApplicationInfo() { 304 if (mPackageInfo != null) { 305 return mPackageInfo.getApplicationInfo(); 306 } 307 throw new RuntimeException("Not supported in system context"); 308 } 309 310 @Override 311 public String getPackageResourcePath() { 312 if (mPackageInfo != null) { 313 return mPackageInfo.getResDir(); 314 } 315 throw new RuntimeException("Not supported in system context"); 316 } 317 318 @Override 319 public String getPackageCodePath() { 320 if (mPackageInfo != null) { 321 return mPackageInfo.getAppDir(); 322 } 323 throw new RuntimeException("Not supported in system context"); 324 } 325 326 private static File makeBackupFile(File prefsFile) { 327 return new File(prefsFile.getPath() + ".bak"); 328 } 329 330 public File getSharedPrefsFile(String name) { 331 return makeFilename(getPreferencesDir(), name + ".xml"); 332 } 333 334 @Override 335 public SharedPreferences getSharedPreferences(String name, int mode) { 336 SharedPreferencesImpl sp; 337 File f = getSharedPrefsFile(name); 338 synchronized (sSharedPrefs) { 339 sp = sSharedPrefs.get(f); 340 if (sp != null && !sp.hasFileChanged()) { 341 //Log.i(TAG, "Returning existing prefs " + name + ": " + sp); 342 return sp; 343 } 344 } 345 346 FileInputStream str = null; 347 File backup = makeBackupFile(f); 348 if (backup.exists()) { 349 f.delete(); 350 backup.renameTo(f); 351 } 352 353 // Debugging 354 if (f.exists() && !f.canRead()) { 355 Log.w(TAG, "Attempt to read preferences file " + f + " without permission"); 356 } 357 358 Map map = null; 359 if (f.exists() && f.canRead()) { 360 try { 361 str = new FileInputStream(f); 362 map = XmlUtils.readMapXml(str); 363 str.close(); 364 } catch (org.xmlpull.v1.XmlPullParserException e) { 365 Log.w(TAG, "getSharedPreferences", e); 366 } catch (FileNotFoundException e) { 367 Log.w(TAG, "getSharedPreferences", e); 368 } catch (IOException e) { 369 Log.w(TAG, "getSharedPreferences", e); 370 } 371 } 372 373 synchronized (sSharedPrefs) { 374 if (sp != null) { 375 //Log.i(TAG, "Updating existing prefs " + name + " " + sp + ": " + map); 376 sp.replace(map); 377 } else { 378 sp = sSharedPrefs.get(f); 379 if (sp == null) { 380 sp = new SharedPreferencesImpl(f, mode, map); 381 sSharedPrefs.put(f, sp); 382 } 383 } 384 return sp; 385 } 386 } 387 388 private File getPreferencesDir() { 389 synchronized (mSync) { 390 if (mPreferencesDir == null) { 391 mPreferencesDir = new File(getDataDirFile(), "shared_prefs"); 392 } 393 return mPreferencesDir; 394 } 395 } 396 397 @Override 398 public FileInputStream openFileInput(String name) 399 throws FileNotFoundException { 400 File f = makeFilename(getFilesDir(), name); 401 return new FileInputStream(f); 402 } 403 404 @Override 405 public FileOutputStream openFileOutput(String name, int mode) 406 throws FileNotFoundException { 407 final boolean append = (mode&MODE_APPEND) != 0; 408 File f = makeFilename(getFilesDir(), name); 409 try { 410 FileOutputStream fos = new FileOutputStream(f, append); 411 setFilePermissionsFromMode(f.getPath(), mode, 0); 412 return fos; 413 } catch (FileNotFoundException e) { 414 } 415 416 File parent = f.getParentFile(); 417 parent.mkdir(); 418 FileUtils.setPermissions( 419 parent.getPath(), 420 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, 421 -1, -1); 422 FileOutputStream fos = new FileOutputStream(f, append); 423 setFilePermissionsFromMode(f.getPath(), mode, 0); 424 return fos; 425 } 426 427 @Override 428 public boolean deleteFile(String name) { 429 File f = makeFilename(getFilesDir(), name); 430 return f.delete(); 431 } 432 433 @Override 434 public File getFilesDir() { 435 synchronized (mSync) { 436 if (mFilesDir == null) { 437 mFilesDir = new File(getDataDirFile(), "files"); 438 } 439 if (!mFilesDir.exists()) { 440 if(!mFilesDir.mkdirs()) { 441 Log.w(TAG, "Unable to create files directory"); 442 return null; 443 } 444 FileUtils.setPermissions( 445 mFilesDir.getPath(), 446 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, 447 -1, -1); 448 } 449 return mFilesDir; 450 } 451 } 452 453 @Override 454 public File getExternalFilesDir(String type) { 455 synchronized (mSync) { 456 if (mExternalFilesDir == null) { 457 mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory( 458 getPackageName()); 459 } 460 if (!mExternalFilesDir.exists()) { 461 try { 462 (new File(Environment.getExternalStorageAndroidDataDir(), 463 ".nomedia")).createNewFile(); 464 } catch (IOException e) { 465 } 466 if (!mExternalFilesDir.mkdirs()) { 467 Log.w(TAG, "Unable to create external files directory"); 468 return null; 469 } 470 } 471 if (type == null) { 472 return mExternalFilesDir; 473 } 474 File dir = new File(mExternalFilesDir, type); 475 if (!dir.exists()) { 476 if (!dir.mkdirs()) { 477 Log.w(TAG, "Unable to create external media directory " + dir); 478 return null; 479 } 480 } 481 return dir; 482 } 483 } 484 485 @Override 486 public File getCacheDir() { 487 synchronized (mSync) { 488 if (mCacheDir == null) { 489 mCacheDir = new File(getDataDirFile(), "cache"); 490 } 491 if (!mCacheDir.exists()) { 492 if(!mCacheDir.mkdirs()) { 493 Log.w(TAG, "Unable to create cache directory"); 494 return null; 495 } 496 FileUtils.setPermissions( 497 mCacheDir.getPath(), 498 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, 499 -1, -1); 500 } 501 } 502 return mCacheDir; 503 } 504 505 @Override 506 public File getExternalCacheDir() { 507 synchronized (mSync) { 508 if (mExternalCacheDir == null) { 509 mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory( 510 getPackageName()); 511 } 512 if (!mExternalCacheDir.exists()) { 513 try { 514 (new File(Environment.getExternalStorageAndroidDataDir(), 515 ".nomedia")).createNewFile(); 516 } catch (IOException e) { 517 } 518 if (!mExternalCacheDir.mkdirs()) { 519 Log.w(TAG, "Unable to create external cache directory"); 520 return null; 521 } 522 } 523 return mExternalCacheDir; 524 } 525 } 526 527 @Override 528 public File getFileStreamPath(String name) { 529 return makeFilename(getFilesDir(), name); 530 } 531 532 @Override 533 public String[] fileList() { 534 final String[] list = getFilesDir().list(); 535 return (list != null) ? list : EMPTY_FILE_LIST; 536 } 537 538 @Override 539 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) { 540 File f = validateFilePath(name, true); 541 SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f, factory); 542 setFilePermissionsFromMode(f.getPath(), mode, 0); 543 return db; 544 } 545 546 @Override 547 public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory, 548 DatabaseErrorHandler errorHandler) { 549 File f = validateFilePath(name, true); 550 SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(f.getPath(), factory, errorHandler); 551 setFilePermissionsFromMode(f.getPath(), mode, 0); 552 return db; 553 } 554 555 @Override 556 public boolean deleteDatabase(String name) { 557 try { 558 File f = validateFilePath(name, false); 559 return f.delete(); 560 } catch (Exception e) { 561 } 562 return false; 563 } 564 565 @Override 566 public File getDatabasePath(String name) { 567 return validateFilePath(name, false); 568 } 569 570 @Override 571 public String[] databaseList() { 572 final String[] list = getDatabasesDir().list(); 573 return (list != null) ? list : EMPTY_FILE_LIST; 574 } 575 576 577 private File getDatabasesDir() { 578 synchronized (mSync) { 579 if (mDatabasesDir == null) { 580 mDatabasesDir = new File(getDataDirFile(), "databases"); 581 } 582 if (mDatabasesDir.getPath().equals("databases")) { 583 mDatabasesDir = new File("/data/system"); 584 } 585 return mDatabasesDir; 586 } 587 } 588 589 @Override 590 public Drawable getWallpaper() { 591 return getWallpaperManager().getDrawable(); 592 } 593 594 @Override 595 public Drawable peekWallpaper() { 596 return getWallpaperManager().peekDrawable(); 597 } 598 599 @Override 600 public int getWallpaperDesiredMinimumWidth() { 601 return getWallpaperManager().getDesiredMinimumWidth(); 602 } 603 604 @Override 605 public int getWallpaperDesiredMinimumHeight() { 606 return getWallpaperManager().getDesiredMinimumHeight(); 607 } 608 609 @Override 610 public void setWallpaper(Bitmap bitmap) throws IOException { 611 getWallpaperManager().setBitmap(bitmap); 612 } 613 614 @Override 615 public void setWallpaper(InputStream data) throws IOException { 616 getWallpaperManager().setStream(data); 617 } 618 619 @Override 620 public void clearWallpaper() throws IOException { 621 getWallpaperManager().clear(); 622 } 623 624 @Override 625 public void startActivity(Intent intent) { 626 if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) { 627 throw new AndroidRuntimeException( 628 "Calling startActivity() from outside of an Activity " 629 + " context requires the FLAG_ACTIVITY_NEW_TASK flag." 630 + " Is this really what you want?"); 631 } 632 mMainThread.getInstrumentation().execStartActivity( 633 getOuterContext(), mMainThread.getApplicationThread(), null, 634 (Activity)null, intent, -1); 635 } 636 637 @Override 638 public void startIntentSender(IntentSender intent, 639 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags) 640 throws IntentSender.SendIntentException { 641 try { 642 String resolvedType = null; 643 if (fillInIntent != null) { 644 resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver()); 645 } 646 int result = ActivityManagerNative.getDefault() 647 .startActivityIntentSender(mMainThread.getApplicationThread(), intent, 648 fillInIntent, resolvedType, null, null, 649 0, flagsMask, flagsValues); 650 if (result == IActivityManager.START_CANCELED) { 651 throw new IntentSender.SendIntentException(); 652 } 653 Instrumentation.checkStartActivityResult(result, null); 654 } catch (RemoteException e) { 655 } 656 } 657 658 @Override 659 public void sendBroadcast(Intent intent) { 660 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); 661 try { 662 ActivityManagerNative.getDefault().broadcastIntent( 663 mMainThread.getApplicationThread(), intent, resolvedType, null, 664 Activity.RESULT_OK, null, null, null, false, false); 665 } catch (RemoteException e) { 666 } 667 } 668 669 @Override 670 public void sendBroadcast(Intent intent, String receiverPermission) { 671 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); 672 try { 673 ActivityManagerNative.getDefault().broadcastIntent( 674 mMainThread.getApplicationThread(), intent, resolvedType, null, 675 Activity.RESULT_OK, null, null, receiverPermission, false, false); 676 } catch (RemoteException e) { 677 } 678 } 679 680 @Override 681 public void sendOrderedBroadcast(Intent intent, 682 String receiverPermission) { 683 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); 684 try { 685 ActivityManagerNative.getDefault().broadcastIntent( 686 mMainThread.getApplicationThread(), intent, resolvedType, null, 687 Activity.RESULT_OK, null, null, receiverPermission, true, false); 688 } catch (RemoteException e) { 689 } 690 } 691 692 @Override 693 public void sendOrderedBroadcast(Intent intent, 694 String receiverPermission, BroadcastReceiver resultReceiver, 695 Handler scheduler, int initialCode, String initialData, 696 Bundle initialExtras) { 697 IIntentReceiver rd = null; 698 if (resultReceiver != null) { 699 if (mPackageInfo != null) { 700 if (scheduler == null) { 701 scheduler = mMainThread.getHandler(); 702 } 703 rd = mPackageInfo.getReceiverDispatcher( 704 resultReceiver, getOuterContext(), scheduler, 705 mMainThread.getInstrumentation(), false); 706 } else { 707 if (scheduler == null) { 708 scheduler = mMainThread.getHandler(); 709 } 710 rd = new LoadedApk.ReceiverDispatcher( 711 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); 712 } 713 } 714 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); 715 try { 716 ActivityManagerNative.getDefault().broadcastIntent( 717 mMainThread.getApplicationThread(), intent, resolvedType, rd, 718 initialCode, initialData, initialExtras, receiverPermission, 719 true, false); 720 } catch (RemoteException e) { 721 } 722 } 723 724 @Override 725 public void sendStickyBroadcast(Intent intent) { 726 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); 727 try { 728 ActivityManagerNative.getDefault().broadcastIntent( 729 mMainThread.getApplicationThread(), intent, resolvedType, null, 730 Activity.RESULT_OK, null, null, null, false, true); 731 } catch (RemoteException e) { 732 } 733 } 734 735 @Override 736 public void sendStickyOrderedBroadcast(Intent intent, 737 BroadcastReceiver resultReceiver, 738 Handler scheduler, int initialCode, String initialData, 739 Bundle initialExtras) { 740 IIntentReceiver rd = null; 741 if (resultReceiver != null) { 742 if (mPackageInfo != null) { 743 if (scheduler == null) { 744 scheduler = mMainThread.getHandler(); 745 } 746 rd = mPackageInfo.getReceiverDispatcher( 747 resultReceiver, getOuterContext(), scheduler, 748 mMainThread.getInstrumentation(), false); 749 } else { 750 if (scheduler == null) { 751 scheduler = mMainThread.getHandler(); 752 } 753 rd = new LoadedApk.ReceiverDispatcher( 754 resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver(); 755 } 756 } 757 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); 758 try { 759 ActivityManagerNative.getDefault().broadcastIntent( 760 mMainThread.getApplicationThread(), intent, resolvedType, rd, 761 initialCode, initialData, initialExtras, null, 762 true, true); 763 } catch (RemoteException e) { 764 } 765 } 766 767 @Override 768 public void removeStickyBroadcast(Intent intent) { 769 String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); 770 if (resolvedType != null) { 771 intent = new Intent(intent); 772 intent.setDataAndType(intent.getData(), resolvedType); 773 } 774 try { 775 ActivityManagerNative.getDefault().unbroadcastIntent( 776 mMainThread.getApplicationThread(), intent); 777 } catch (RemoteException e) { 778 } 779 } 780 781 @Override 782 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) { 783 return registerReceiver(receiver, filter, null, null); 784 } 785 786 @Override 787 public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter, 788 String broadcastPermission, Handler scheduler) { 789 return registerReceiverInternal(receiver, filter, broadcastPermission, 790 scheduler, getOuterContext()); 791 } 792 793 private Intent registerReceiverInternal(BroadcastReceiver receiver, 794 IntentFilter filter, String broadcastPermission, 795 Handler scheduler, Context context) { 796 IIntentReceiver rd = null; 797 if (receiver != null) { 798 if (mPackageInfo != null && context != null) { 799 if (scheduler == null) { 800 scheduler = mMainThread.getHandler(); 801 } 802 rd = mPackageInfo.getReceiverDispatcher( 803 receiver, context, scheduler, 804 mMainThread.getInstrumentation(), true); 805 } else { 806 if (scheduler == null) { 807 scheduler = mMainThread.getHandler(); 808 } 809 rd = new LoadedApk.ReceiverDispatcher( 810 receiver, context, scheduler, null, true).getIIntentReceiver(); 811 } 812 } 813 try { 814 return ActivityManagerNative.getDefault().registerReceiver( 815 mMainThread.getApplicationThread(), 816 rd, filter, broadcastPermission); 817 } catch (RemoteException e) { 818 return null; 819 } 820 } 821 822 @Override 823 public void unregisterReceiver(BroadcastReceiver receiver) { 824 if (mPackageInfo != null) { 825 IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher( 826 getOuterContext(), receiver); 827 try { 828 ActivityManagerNative.getDefault().unregisterReceiver(rd); 829 } catch (RemoteException e) { 830 } 831 } else { 832 throw new RuntimeException("Not supported in system context"); 833 } 834 } 835 836 @Override 837 public ComponentName startService(Intent service) { 838 try { 839 ComponentName cn = ActivityManagerNative.getDefault().startService( 840 mMainThread.getApplicationThread(), service, 841 service.resolveTypeIfNeeded(getContentResolver())); 842 if (cn != null && cn.getPackageName().equals("!")) { 843 throw new SecurityException( 844 "Not allowed to start service " + service 845 + " without permission " + cn.getClassName()); 846 } 847 return cn; 848 } catch (RemoteException e) { 849 return null; 850 } 851 } 852 853 @Override 854 public boolean stopService(Intent service) { 855 try { 856 int res = ActivityManagerNative.getDefault().stopService( 857 mMainThread.getApplicationThread(), service, 858 service.resolveTypeIfNeeded(getContentResolver())); 859 if (res < 0) { 860 throw new SecurityException( 861 "Not allowed to stop service " + service); 862 } 863 return res != 0; 864 } catch (RemoteException e) { 865 return false; 866 } 867 } 868 869 @Override 870 public boolean bindService(Intent service, ServiceConnection conn, 871 int flags) { 872 IServiceConnection sd; 873 if (mPackageInfo != null) { 874 sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), 875 mMainThread.getHandler(), flags); 876 } else { 877 throw new RuntimeException("Not supported in system context"); 878 } 879 try { 880 int res = ActivityManagerNative.getDefault().bindService( 881 mMainThread.getApplicationThread(), getActivityToken(), 882 service, service.resolveTypeIfNeeded(getContentResolver()), 883 sd, flags); 884 if (res < 0) { 885 throw new SecurityException( 886 "Not allowed to bind to service " + service); 887 } 888 return res != 0; 889 } catch (RemoteException e) { 890 return false; 891 } 892 } 893 894 @Override 895 public void unbindService(ServiceConnection conn) { 896 if (mPackageInfo != null) { 897 IServiceConnection sd = mPackageInfo.forgetServiceDispatcher( 898 getOuterContext(), conn); 899 try { 900 ActivityManagerNative.getDefault().unbindService(sd); 901 } catch (RemoteException e) { 902 } 903 } else { 904 throw new RuntimeException("Not supported in system context"); 905 } 906 } 907 908 @Override 909 public boolean startInstrumentation(ComponentName className, 910 String profileFile, Bundle arguments) { 911 try { 912 return ActivityManagerNative.getDefault().startInstrumentation( 913 className, profileFile, 0, arguments, null); 914 } catch (RemoteException e) { 915 // System has crashed, nothing we can do. 916 } 917 return false; 918 } 919 920 @Override 921 public Object getSystemService(String name) { 922 if (WINDOW_SERVICE.equals(name)) { 923 return WindowManagerImpl.getDefault(); 924 } else if (LAYOUT_INFLATER_SERVICE.equals(name)) { 925 synchronized (mSync) { 926 LayoutInflater inflater = mLayoutInflater; 927 if (inflater != null) { 928 return inflater; 929 } 930 mLayoutInflater = inflater = 931 PolicyManager.makeNewLayoutInflater(getOuterContext()); 932 return inflater; 933 } 934 } else if (ACTIVITY_SERVICE.equals(name)) { 935 return getActivityManager(); 936 } else if (INPUT_METHOD_SERVICE.equals(name)) { 937 return InputMethodManager.getInstance(this); 938 } else if (ALARM_SERVICE.equals(name)) { 939 return getAlarmManager(); 940 } else if (ACCOUNT_SERVICE.equals(name)) { 941 return getAccountManager(); 942 } else if (POWER_SERVICE.equals(name)) { 943 return getPowerManager(); 944 } else if (CONNECTIVITY_SERVICE.equals(name)) { 945 return getConnectivityManager(); 946 } else if (THROTTLE_SERVICE.equals(name)) { 947 return getThrottleManager(); 948 } else if (WIFI_SERVICE.equals(name)) { 949 return getWifiManager(); 950 } else if (NOTIFICATION_SERVICE.equals(name)) { 951 return getNotificationManager(); 952 } else if (KEYGUARD_SERVICE.equals(name)) { 953 return new KeyguardManager(); 954 } else if (ACCESSIBILITY_SERVICE.equals(name)) { 955 return AccessibilityManager.getInstance(this); 956 } else if (LOCATION_SERVICE.equals(name)) { 957 return getLocationManager(); 958 } else if (SEARCH_SERVICE.equals(name)) { 959 return getSearchManager(); 960 } else if (SENSOR_SERVICE.equals(name)) { 961 return getSensorManager(); 962 } else if (STORAGE_SERVICE.equals(name)) { 963 return getStorageManager(); 964 } else if (VIBRATOR_SERVICE.equals(name)) { 965 return getVibrator(); 966 } else if (STATUS_BAR_SERVICE.equals(name)) { 967 synchronized (mSync) { 968 if (mStatusBarManager == null) { 969 mStatusBarManager = new StatusBarManager(getOuterContext()); 970 } 971 return mStatusBarManager; 972 } 973 } else if (AUDIO_SERVICE.equals(name)) { 974 return getAudioManager(); 975 } else if (TELEPHONY_SERVICE.equals(name)) { 976 return getTelephonyManager(); 977 } else if (CLIPBOARD_SERVICE.equals(name)) { 978 return getClipboardManager(); 979 } else if (WALLPAPER_SERVICE.equals(name)) { 980 return getWallpaperManager(); 981 } else if (DROPBOX_SERVICE.equals(name)) { 982 return getDropBoxManager(); 983 } else if (DEVICE_POLICY_SERVICE.equals(name)) { 984 return getDevicePolicyManager(); 985 } else if (UI_MODE_SERVICE.equals(name)) { 986 return getUiModeManager(); 987 } 988 989 return null; 990 } 991 992 private AccountManager getAccountManager() { 993 synchronized (mSync) { 994 if (mAccountManager == null) { 995 IBinder b = ServiceManager.getService(ACCOUNT_SERVICE); 996 IAccountManager service = IAccountManager.Stub.asInterface(b); 997 mAccountManager = new AccountManager(this, service); 998 } 999 return mAccountManager; 1000 } 1001 } 1002 1003 private ActivityManager getActivityManager() { 1004 synchronized (mSync) { 1005 if (mActivityManager == null) { 1006 mActivityManager = new ActivityManager(getOuterContext(), 1007 mMainThread.getHandler()); 1008 } 1009 } 1010 return mActivityManager; 1011 } 1012 1013 private AlarmManager getAlarmManager() { 1014 synchronized (sSync) { 1015 if (sAlarmManager == null) { 1016 IBinder b = ServiceManager.getService(ALARM_SERVICE); 1017 IAlarmManager service = IAlarmManager.Stub.asInterface(b); 1018 sAlarmManager = new AlarmManager(service); 1019 } 1020 } 1021 return sAlarmManager; 1022 } 1023 1024 private PowerManager getPowerManager() { 1025 synchronized (sSync) { 1026 if (sPowerManager == null) { 1027 IBinder b = ServiceManager.getService(POWER_SERVICE); 1028 IPowerManager service = IPowerManager.Stub.asInterface(b); 1029 sPowerManager = new PowerManager(service, mMainThread.getHandler()); 1030 } 1031 } 1032 return sPowerManager; 1033 } 1034 1035 private ConnectivityManager getConnectivityManager() 1036 { 1037 synchronized (sSync) { 1038 if (sConnectivityManager == null) { 1039 IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE); 1040 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b); 1041 sConnectivityManager = new ConnectivityManager(service); 1042 } 1043 } 1044 return sConnectivityManager; 1045 } 1046 1047 private ThrottleManager getThrottleManager() 1048 { 1049 synchronized (sSync) { 1050 if (sThrottleManager == null) { 1051 IBinder b = ServiceManager.getService(THROTTLE_SERVICE); 1052 IThrottleManager service = IThrottleManager.Stub.asInterface(b); 1053 sThrottleManager = new ThrottleManager(service); 1054 } 1055 } 1056 return sThrottleManager; 1057 } 1058 1059 private WifiManager getWifiManager() 1060 { 1061 synchronized (sSync) { 1062 if (sWifiManager == null) { 1063 IBinder b = ServiceManager.getService(WIFI_SERVICE); 1064 IWifiManager service = IWifiManager.Stub.asInterface(b); 1065 sWifiManager = new WifiManager(service, mMainThread.getHandler()); 1066 } 1067 } 1068 return sWifiManager; 1069 } 1070 1071 private NotificationManager getNotificationManager() { 1072 synchronized (mSync) { 1073 if (mNotificationManager == null) { 1074 mNotificationManager = new NotificationManager( 1075 new ContextThemeWrapper(getOuterContext(), com.android.internal.R.style.Theme_Dialog), 1076 mMainThread.getHandler()); 1077 } 1078 } 1079 return mNotificationManager; 1080 } 1081 1082 private WallpaperManager getWallpaperManager() { 1083 synchronized (mSync) { 1084 if (mWallpaperManager == null) { 1085 mWallpaperManager = new WallpaperManager(getOuterContext(), 1086 mMainThread.getHandler()); 1087 } 1088 } 1089 return mWallpaperManager; 1090 } 1091 1092 private TelephonyManager getTelephonyManager() { 1093 synchronized (mSync) { 1094 if (mTelephonyManager == null) { 1095 mTelephonyManager = new TelephonyManager(getOuterContext()); 1096 } 1097 } 1098 return mTelephonyManager; 1099 } 1100 1101 private ClipboardManager getClipboardManager() { 1102 synchronized (mSync) { 1103 if (mClipboardManager == null) { 1104 mClipboardManager = new ClipboardManager(getOuterContext(), 1105 mMainThread.getHandler()); 1106 } 1107 } 1108 return mClipboardManager; 1109 } 1110 1111 private LocationManager getLocationManager() { 1112 synchronized (sSync) { 1113 if (sLocationManager == null) { 1114 IBinder b = ServiceManager.getService(LOCATION_SERVICE); 1115 ILocationManager service = ILocationManager.Stub.asInterface(b); 1116 sLocationManager = new LocationManager(service); 1117 } 1118 } 1119 return sLocationManager; 1120 } 1121 1122 private SearchManager getSearchManager() { 1123 synchronized (mSync) { 1124 if (mSearchManager == null) { 1125 mSearchManager = new SearchManager(getOuterContext(), mMainThread.getHandler()); 1126 } 1127 } 1128 return mSearchManager; 1129 } 1130 1131 private SensorManager getSensorManager() { 1132 synchronized (mSync) { 1133 if (mSensorManager == null) { 1134 mSensorManager = new SensorManager(mMainThread.getHandler().getLooper()); 1135 } 1136 } 1137 return mSensorManager; 1138 } 1139 1140 private StorageManager getStorageManager() { 1141 synchronized (mSync) { 1142 if (mStorageManager == null) { 1143 try { 1144 mStorageManager = new StorageManager(mMainThread.getHandler().getLooper()); 1145 } catch (RemoteException rex) { 1146 Log.e(TAG, "Failed to create StorageManager", rex); 1147 mStorageManager = null; 1148 } 1149 } 1150 } 1151 return mStorageManager; 1152 } 1153 1154 private Vibrator getVibrator() { 1155 synchronized (mSync) { 1156 if (mVibrator == null) { 1157 mVibrator = new Vibrator(); 1158 } 1159 } 1160 return mVibrator; 1161 } 1162 1163 private AudioManager getAudioManager() 1164 { 1165 if (mAudioManager == null) { 1166 mAudioManager = new AudioManager(this); 1167 } 1168 return mAudioManager; 1169 } 1170 1171 /* package */ static DropBoxManager createDropBoxManager() { 1172 IBinder b = ServiceManager.getService(DROPBOX_SERVICE); 1173 IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b); 1174 return new DropBoxManager(service); 1175 } 1176 1177 private DropBoxManager getDropBoxManager() { 1178 synchronized (mSync) { 1179 if (mDropBoxManager == null) { 1180 mDropBoxManager = createDropBoxManager(); 1181 } 1182 } 1183 return mDropBoxManager; 1184 } 1185 1186 private DevicePolicyManager getDevicePolicyManager() { 1187 synchronized (mSync) { 1188 if (mDevicePolicyManager == null) { 1189 mDevicePolicyManager = DevicePolicyManager.create(this, 1190 mMainThread.getHandler()); 1191 } 1192 } 1193 return mDevicePolicyManager; 1194 } 1195 1196 private UiModeManager getUiModeManager() { 1197 synchronized (mSync) { 1198 if (mUiModeManager == null) { 1199 mUiModeManager = new UiModeManager(); 1200 } 1201 } 1202 return mUiModeManager; 1203 } 1204 1205 @Override 1206 public int checkPermission(String permission, int pid, int uid) { 1207 if (permission == null) { 1208 throw new IllegalArgumentException("permission is null"); 1209 } 1210 1211 if (!Process.supportsProcesses()) { 1212 return PackageManager.PERMISSION_GRANTED; 1213 } 1214 try { 1215 return ActivityManagerNative.getDefault().checkPermission( 1216 permission, pid, uid); 1217 } catch (RemoteException e) { 1218 return PackageManager.PERMISSION_DENIED; 1219 } 1220 } 1221 1222 @Override 1223 public int checkCallingPermission(String permission) { 1224 if (permission == null) { 1225 throw new IllegalArgumentException("permission is null"); 1226 } 1227 1228 if (!Process.supportsProcesses()) { 1229 return PackageManager.PERMISSION_GRANTED; 1230 } 1231 int pid = Binder.getCallingPid(); 1232 if (pid != Process.myPid()) { 1233 return checkPermission(permission, pid, 1234 Binder.getCallingUid()); 1235 } 1236 return PackageManager.PERMISSION_DENIED; 1237 } 1238 1239 @Override 1240 public int checkCallingOrSelfPermission(String permission) { 1241 if (permission == null) { 1242 throw new IllegalArgumentException("permission is null"); 1243 } 1244 1245 return checkPermission(permission, Binder.getCallingPid(), 1246 Binder.getCallingUid()); 1247 } 1248 1249 private void enforce( 1250 String permission, int resultOfCheck, 1251 boolean selfToo, int uid, String message) { 1252 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { 1253 throw new SecurityException( 1254 (message != null ? (message + ": ") : "") + 1255 (selfToo 1256 ? "Neither user " + uid + " nor current process has " 1257 : "User " + uid + " does not have ") + 1258 permission + 1259 "."); 1260 } 1261 } 1262 1263 public void enforcePermission( 1264 String permission, int pid, int uid, String message) { 1265 enforce(permission, 1266 checkPermission(permission, pid, uid), 1267 false, 1268 uid, 1269 message); 1270 } 1271 1272 public void enforceCallingPermission(String permission, String message) { 1273 enforce(permission, 1274 checkCallingPermission(permission), 1275 false, 1276 Binder.getCallingUid(), 1277 message); 1278 } 1279 1280 public void enforceCallingOrSelfPermission( 1281 String permission, String message) { 1282 enforce(permission, 1283 checkCallingOrSelfPermission(permission), 1284 true, 1285 Binder.getCallingUid(), 1286 message); 1287 } 1288 1289 @Override 1290 public void grantUriPermission(String toPackage, Uri uri, int modeFlags) { 1291 try { 1292 ActivityManagerNative.getDefault().grantUriPermission( 1293 mMainThread.getApplicationThread(), toPackage, uri, 1294 modeFlags); 1295 } catch (RemoteException e) { 1296 } 1297 } 1298 1299 @Override 1300 public void revokeUriPermission(Uri uri, int modeFlags) { 1301 try { 1302 ActivityManagerNative.getDefault().revokeUriPermission( 1303 mMainThread.getApplicationThread(), uri, 1304 modeFlags); 1305 } catch (RemoteException e) { 1306 } 1307 } 1308 1309 @Override 1310 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) { 1311 if (!Process.supportsProcesses()) { 1312 return PackageManager.PERMISSION_GRANTED; 1313 } 1314 try { 1315 return ActivityManagerNative.getDefault().checkUriPermission( 1316 uri, pid, uid, modeFlags); 1317 } catch (RemoteException e) { 1318 return PackageManager.PERMISSION_DENIED; 1319 } 1320 } 1321 1322 @Override 1323 public int checkCallingUriPermission(Uri uri, int modeFlags) { 1324 if (!Process.supportsProcesses()) { 1325 return PackageManager.PERMISSION_GRANTED; 1326 } 1327 int pid = Binder.getCallingPid(); 1328 if (pid != Process.myPid()) { 1329 return checkUriPermission(uri, pid, 1330 Binder.getCallingUid(), modeFlags); 1331 } 1332 return PackageManager.PERMISSION_DENIED; 1333 } 1334 1335 @Override 1336 public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) { 1337 return checkUriPermission(uri, Binder.getCallingPid(), 1338 Binder.getCallingUid(), modeFlags); 1339 } 1340 1341 @Override 1342 public int checkUriPermission(Uri uri, String readPermission, 1343 String writePermission, int pid, int uid, int modeFlags) { 1344 if (DEBUG) { 1345 Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission=" 1346 + readPermission + " writePermission=" + writePermission 1347 + " pid=" + pid + " uid=" + uid + " mode" + modeFlags); 1348 } 1349 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) { 1350 if (readPermission == null 1351 || checkPermission(readPermission, pid, uid) 1352 == PackageManager.PERMISSION_GRANTED) { 1353 return PackageManager.PERMISSION_GRANTED; 1354 } 1355 } 1356 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) { 1357 if (writePermission == null 1358 || checkPermission(writePermission, pid, uid) 1359 == PackageManager.PERMISSION_GRANTED) { 1360 return PackageManager.PERMISSION_GRANTED; 1361 } 1362 } 1363 return uri != null ? checkUriPermission(uri, pid, uid, modeFlags) 1364 : PackageManager.PERMISSION_DENIED; 1365 } 1366 1367 private String uriModeFlagToString(int uriModeFlags) { 1368 switch (uriModeFlags) { 1369 case Intent.FLAG_GRANT_READ_URI_PERMISSION | 1370 Intent.FLAG_GRANT_WRITE_URI_PERMISSION: 1371 return "read and write"; 1372 case Intent.FLAG_GRANT_READ_URI_PERMISSION: 1373 return "read"; 1374 case Intent.FLAG_GRANT_WRITE_URI_PERMISSION: 1375 return "write"; 1376 } 1377 throw new IllegalArgumentException( 1378 "Unknown permission mode flags: " + uriModeFlags); 1379 } 1380 1381 private void enforceForUri( 1382 int modeFlags, int resultOfCheck, boolean selfToo, 1383 int uid, Uri uri, String message) { 1384 if (resultOfCheck != PackageManager.PERMISSION_GRANTED) { 1385 throw new SecurityException( 1386 (message != null ? (message + ": ") : "") + 1387 (selfToo 1388 ? "Neither user " + uid + " nor current process has " 1389 : "User " + uid + " does not have ") + 1390 uriModeFlagToString(modeFlags) + 1391 " permission on " + 1392 uri + 1393 "."); 1394 } 1395 } 1396 1397 public void enforceUriPermission( 1398 Uri uri, int pid, int uid, int modeFlags, String message) { 1399 enforceForUri( 1400 modeFlags, checkUriPermission(uri, pid, uid, modeFlags), 1401 false, uid, uri, message); 1402 } 1403 1404 public void enforceCallingUriPermission( 1405 Uri uri, int modeFlags, String message) { 1406 enforceForUri( 1407 modeFlags, checkCallingUriPermission(uri, modeFlags), 1408 false, Binder.getCallingUid(), uri, message); 1409 } 1410 1411 public void enforceCallingOrSelfUriPermission( 1412 Uri uri, int modeFlags, String message) { 1413 enforceForUri( 1414 modeFlags, 1415 checkCallingOrSelfUriPermission(uri, modeFlags), true, 1416 Binder.getCallingUid(), uri, message); 1417 } 1418 1419 public void enforceUriPermission( 1420 Uri uri, String readPermission, String writePermission, 1421 int pid, int uid, int modeFlags, String message) { 1422 enforceForUri(modeFlags, 1423 checkUriPermission( 1424 uri, readPermission, writePermission, pid, uid, 1425 modeFlags), 1426 false, 1427 uid, 1428 uri, 1429 message); 1430 } 1431 1432 @Override 1433 public Context createPackageContext(String packageName, int flags) 1434 throws PackageManager.NameNotFoundException { 1435 if (packageName.equals("system") || packageName.equals("android")) { 1436 return new ContextImpl(mMainThread.getSystemContext()); 1437 } 1438 1439 LoadedApk pi = 1440 mMainThread.getPackageInfo(packageName, flags); 1441 if (pi != null) { 1442 ContextImpl c = new ContextImpl(); 1443 c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED; 1444 c.init(pi, null, mMainThread, mResources); 1445 if (c.mResources != null) { 1446 return c; 1447 } 1448 } 1449 1450 // Should be a better exception. 1451 throw new PackageManager.NameNotFoundException( 1452 "Application package " + packageName + " not found"); 1453 } 1454 1455 @Override 1456 public boolean isRestricted() { 1457 return mRestricted; 1458 } 1459 1460 private File getDataDirFile() { 1461 if (mPackageInfo != null) { 1462 return mPackageInfo.getDataDirFile(); 1463 } 1464 throw new RuntimeException("Not supported in system context"); 1465 } 1466 1467 @Override 1468 public File getDir(String name, int mode) { 1469 name = "app_" + name; 1470 File file = makeFilename(getDataDirFile(), name); 1471 if (!file.exists()) { 1472 file.mkdir(); 1473 setFilePermissionsFromMode(file.getPath(), mode, 1474 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH); 1475 } 1476 return file; 1477 } 1478 1479 static ContextImpl createSystemContext(ActivityThread mainThread) { 1480 ContextImpl context = new ContextImpl(); 1481 context.init(Resources.getSystem(), mainThread); 1482 return context; 1483 } 1484 1485 ContextImpl() { 1486 // For debug only 1487 //++sInstanceCount; 1488 mOuterContext = this; 1489 } 1490 1491 /** 1492 * Create a new ApplicationContext from an existing one. The new one 1493 * works and operates the same as the one it is copying. 1494 * 1495 * @param context Existing application context. 1496 */ 1497 public ContextImpl(ContextImpl context) { 1498 ++sInstanceCount; 1499 mPackageInfo = context.mPackageInfo; 1500 mResources = context.mResources; 1501 mMainThread = context.mMainThread; 1502 mContentResolver = context.mContentResolver; 1503 mOuterContext = this; 1504 } 1505 1506 final void init(LoadedApk packageInfo, 1507 IBinder activityToken, ActivityThread mainThread) { 1508 init(packageInfo, activityToken, mainThread, null); 1509 } 1510 1511 final void init(LoadedApk packageInfo, 1512 IBinder activityToken, ActivityThread mainThread, 1513 Resources container) { 1514 mPackageInfo = packageInfo; 1515 mResources = mPackageInfo.getResources(mainThread); 1516 1517 if (mResources != null && container != null 1518 && container.getCompatibilityInfo().applicationScale != 1519 mResources.getCompatibilityInfo().applicationScale) { 1520 if (DEBUG) { 1521 Log.d(TAG, "loaded context has different scaling. Using container's" + 1522 " compatiblity info:" + container.getDisplayMetrics()); 1523 } 1524 mResources = mainThread.getTopLevelResources( 1525 mPackageInfo.getResDir(), container.getCompatibilityInfo().copy()); 1526 } 1527 mMainThread = mainThread; 1528 mContentResolver = new ApplicationContentResolver(this, mainThread); 1529 1530 setActivityToken(activityToken); 1531 } 1532 1533 final void init(Resources resources, ActivityThread mainThread) { 1534 mPackageInfo = null; 1535 mResources = resources; 1536 mMainThread = mainThread; 1537 mContentResolver = new ApplicationContentResolver(this, mainThread); 1538 } 1539 1540 final void scheduleFinalCleanup(String who, String what) { 1541 mMainThread.scheduleContextCleanup(this, who, what); 1542 } 1543 1544 final void performFinalCleanup(String who, String what) { 1545 //Log.i(TAG, "Cleanup up context: " + this); 1546 mPackageInfo.removeContextRegistrations(getOuterContext(), who, what); 1547 } 1548 1549 final Context getReceiverRestrictedContext() { 1550 if (mReceiverRestrictedContext != null) { 1551 return mReceiverRestrictedContext; 1552 } 1553 return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext()); 1554 } 1555 1556 final void setActivityToken(IBinder token) { 1557 mActivityToken = token; 1558 } 1559 1560 final void setOuterContext(Context context) { 1561 mOuterContext = context; 1562 } 1563 1564 final Context getOuterContext() { 1565 return mOuterContext; 1566 } 1567 1568 final IBinder getActivityToken() { 1569 return mActivityToken; 1570 } 1571 1572 private static void setFilePermissionsFromMode(String name, int mode, 1573 int extraPermissions) { 1574 int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR 1575 |FileUtils.S_IRGRP|FileUtils.S_IWGRP 1576 |extraPermissions; 1577 if ((mode&MODE_WORLD_READABLE) != 0) { 1578 perms |= FileUtils.S_IROTH; 1579 } 1580 if ((mode&MODE_WORLD_WRITEABLE) != 0) { 1581 perms |= FileUtils.S_IWOTH; 1582 } 1583 if (DEBUG) { 1584 Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode) 1585 + ", perms=0x" + Integer.toHexString(perms)); 1586 } 1587 FileUtils.setPermissions(name, perms, -1, -1); 1588 } 1589 1590 private File validateFilePath(String name, boolean createDirectory) { 1591 File dir; 1592 File f; 1593 1594 if (name.charAt(0) == File.separatorChar) { 1595 String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar)); 1596 dir = new File(dirPath); 1597 name = name.substring(name.lastIndexOf(File.separatorChar)); 1598 f = new File(dir, name); 1599 } else { 1600 dir = getDatabasesDir(); 1601 f = makeFilename(dir, name); 1602 } 1603 1604 if (createDirectory && !dir.isDirectory() && dir.mkdir()) { 1605 FileUtils.setPermissions(dir.getPath(), 1606 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, 1607 -1, -1); 1608 } 1609 1610 return f; 1611 } 1612 1613 private File makeFilename(File base, String name) { 1614 if (name.indexOf(File.separatorChar) < 0) { 1615 return new File(base, name); 1616 } 1617 throw new IllegalArgumentException( 1618 "File " + name + " contains a path separator"); 1619 } 1620 1621 // ---------------------------------------------------------------------- 1622 // ---------------------------------------------------------------------- 1623 // ---------------------------------------------------------------------- 1624 1625 private static final class ApplicationContentResolver extends ContentResolver { 1626 public ApplicationContentResolver(Context context, 1627 ActivityThread mainThread) 1628 { 1629 super(context); 1630 mMainThread = mainThread; 1631 } 1632 1633 @Override 1634 protected IContentProvider acquireProvider(Context context, String name) 1635 { 1636 return mMainThread.acquireProvider(context, name); 1637 } 1638 1639 @Override 1640 public boolean releaseProvider(IContentProvider provider) 1641 { 1642 return mMainThread.releaseProvider(provider); 1643 } 1644 1645 private final ActivityThread mMainThread; 1646 } 1647 1648 // ---------------------------------------------------------------------- 1649 // ---------------------------------------------------------------------- 1650 // ---------------------------------------------------------------------- 1651 1652 /*package*/ 1653 static final class ApplicationPackageManager extends PackageManager { 1654 @Override 1655 public PackageInfo getPackageInfo(String packageName, int flags) 1656 throws NameNotFoundException { 1657 try { 1658 PackageInfo pi = mPM.getPackageInfo(packageName, flags); 1659 if (pi != null) { 1660 return pi; 1661 } 1662 } catch (RemoteException e) { 1663 throw new RuntimeException("Package manager has died", e); 1664 } 1665 1666 throw new NameNotFoundException(packageName); 1667 } 1668 1669 @Override 1670 public String[] currentToCanonicalPackageNames(String[] names) { 1671 try { 1672 return mPM.currentToCanonicalPackageNames(names); 1673 } catch (RemoteException e) { 1674 throw new RuntimeException("Package manager has died", e); 1675 } 1676 } 1677 1678 @Override 1679 public String[] canonicalToCurrentPackageNames(String[] names) { 1680 try { 1681 return mPM.canonicalToCurrentPackageNames(names); 1682 } catch (RemoteException e) { 1683 throw new RuntimeException("Package manager has died", e); 1684 } 1685 } 1686 1687 @Override 1688 public Intent getLaunchIntentForPackage(String packageName) { 1689 // First see if the package has an INFO activity; the existence of 1690 // such an activity is implied to be the desired front-door for the 1691 // overall package (such as if it has multiple launcher entries). 1692 Intent intentToResolve = new Intent(Intent.ACTION_MAIN); 1693 intentToResolve.addCategory(Intent.CATEGORY_INFO); 1694 intentToResolve.setPackage(packageName); 1695 ResolveInfo resolveInfo = resolveActivity(intentToResolve, 0); 1696 1697 // Otherwise, try to find a main launcher activity. 1698 if (resolveInfo == null) { 1699 // reuse the intent instance 1700 intentToResolve.removeCategory(Intent.CATEGORY_INFO); 1701 intentToResolve.addCategory(Intent.CATEGORY_LAUNCHER); 1702 intentToResolve.setPackage(packageName); 1703 resolveInfo = resolveActivity(intentToResolve, 0); 1704 } 1705 if (resolveInfo == null) { 1706 return null; 1707 } 1708 Intent intent = new Intent(Intent.ACTION_MAIN); 1709 intent.setClassName(packageName, resolveInfo.activityInfo.name); 1710 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 1711 return intent; 1712 } 1713 1714 @Override 1715 public int[] getPackageGids(String packageName) 1716 throws NameNotFoundException { 1717 try { 1718 int[] gids = mPM.getPackageGids(packageName); 1719 if (gids == null || gids.length > 0) { 1720 return gids; 1721 } 1722 } catch (RemoteException e) { 1723 throw new RuntimeException("Package manager has died", e); 1724 } 1725 1726 throw new NameNotFoundException(packageName); 1727 } 1728 1729 @Override 1730 public PermissionInfo getPermissionInfo(String name, int flags) 1731 throws NameNotFoundException { 1732 try { 1733 PermissionInfo pi = mPM.getPermissionInfo(name, flags); 1734 if (pi != null) { 1735 return pi; 1736 } 1737 } catch (RemoteException e) { 1738 throw new RuntimeException("Package manager has died", e); 1739 } 1740 1741 throw new NameNotFoundException(name); 1742 } 1743 1744 @Override 1745 public List<PermissionInfo> queryPermissionsByGroup(String group, int flags) 1746 throws NameNotFoundException { 1747 try { 1748 List<PermissionInfo> pi = mPM.queryPermissionsByGroup(group, flags); 1749 if (pi != null) { 1750 return pi; 1751 } 1752 } catch (RemoteException e) { 1753 throw new RuntimeException("Package manager has died", e); 1754 } 1755 1756 throw new NameNotFoundException(group); 1757 } 1758 1759 @Override 1760 public PermissionGroupInfo getPermissionGroupInfo(String name, 1761 int flags) throws NameNotFoundException { 1762 try { 1763 PermissionGroupInfo pgi = mPM.getPermissionGroupInfo(name, flags); 1764 if (pgi != null) { 1765 return pgi; 1766 } 1767 } catch (RemoteException e) { 1768 throw new RuntimeException("Package manager has died", e); 1769 } 1770 1771 throw new NameNotFoundException(name); 1772 } 1773 1774 @Override 1775 public List<PermissionGroupInfo> getAllPermissionGroups(int flags) { 1776 try { 1777 return mPM.getAllPermissionGroups(flags); 1778 } catch (RemoteException e) { 1779 throw new RuntimeException("Package manager has died", e); 1780 } 1781 } 1782 1783 @Override 1784 public ApplicationInfo getApplicationInfo(String packageName, int flags) 1785 throws NameNotFoundException { 1786 try { 1787 ApplicationInfo ai = mPM.getApplicationInfo(packageName, flags); 1788 if (ai != null) { 1789 return ai; 1790 } 1791 } catch (RemoteException e) { 1792 throw new RuntimeException("Package manager has died", e); 1793 } 1794 1795 throw new NameNotFoundException(packageName); 1796 } 1797 1798 @Override 1799 public ActivityInfo getActivityInfo(ComponentName className, int flags) 1800 throws NameNotFoundException { 1801 try { 1802 ActivityInfo ai = mPM.getActivityInfo(className, flags); 1803 if (ai != null) { 1804 return ai; 1805 } 1806 } catch (RemoteException e) { 1807 throw new RuntimeException("Package manager has died", e); 1808 } 1809 1810 throw new NameNotFoundException(className.toString()); 1811 } 1812 1813 @Override 1814 public ActivityInfo getReceiverInfo(ComponentName className, int flags) 1815 throws NameNotFoundException { 1816 try { 1817 ActivityInfo ai = mPM.getReceiverInfo(className, flags); 1818 if (ai != null) { 1819 return ai; 1820 } 1821 } catch (RemoteException e) { 1822 throw new RuntimeException("Package manager has died", e); 1823 } 1824 1825 throw new NameNotFoundException(className.toString()); 1826 } 1827 1828 @Override 1829 public ServiceInfo getServiceInfo(ComponentName className, int flags) 1830 throws NameNotFoundException { 1831 try { 1832 ServiceInfo si = mPM.getServiceInfo(className, flags); 1833 if (si != null) { 1834 return si; 1835 } 1836 } catch (RemoteException e) { 1837 throw new RuntimeException("Package manager has died", e); 1838 } 1839 1840 throw new NameNotFoundException(className.toString()); 1841 } 1842 1843 @Override 1844 public String[] getSystemSharedLibraryNames() { 1845 try { 1846 return mPM.getSystemSharedLibraryNames(); 1847 } catch (RemoteException e) { 1848 throw new RuntimeException("Package manager has died", e); 1849 } 1850 } 1851 1852 @Override 1853 public FeatureInfo[] getSystemAvailableFeatures() { 1854 try { 1855 return mPM.getSystemAvailableFeatures(); 1856 } catch (RemoteException e) { 1857 throw new RuntimeException("Package manager has died", e); 1858 } 1859 } 1860 1861 @Override 1862 public boolean hasSystemFeature(String name) { 1863 try { 1864 return mPM.hasSystemFeature(name); 1865 } catch (RemoteException e) { 1866 throw new RuntimeException("Package manager has died", e); 1867 } 1868 } 1869 1870 @Override 1871 public int checkPermission(String permName, String pkgName) { 1872 try { 1873 return mPM.checkPermission(permName, pkgName); 1874 } catch (RemoteException e) { 1875 throw new RuntimeException("Package manager has died", e); 1876 } 1877 } 1878 1879 @Override 1880 public boolean addPermission(PermissionInfo info) { 1881 try { 1882 return mPM.addPermission(info); 1883 } catch (RemoteException e) { 1884 throw new RuntimeException("Package manager has died", e); 1885 } 1886 } 1887 1888 @Override 1889 public boolean addPermissionAsync(PermissionInfo info) { 1890 try { 1891 return mPM.addPermissionAsync(info); 1892 } catch (RemoteException e) { 1893 throw new RuntimeException("Package manager has died", e); 1894 } 1895 } 1896 1897 @Override 1898 public void removePermission(String name) { 1899 try { 1900 mPM.removePermission(name); 1901 } catch (RemoteException e) { 1902 throw new RuntimeException("Package manager has died", e); 1903 } 1904 } 1905 1906 @Override 1907 public int checkSignatures(String pkg1, String pkg2) { 1908 try { 1909 return mPM.checkSignatures(pkg1, pkg2); 1910 } catch (RemoteException e) { 1911 throw new RuntimeException("Package manager has died", e); 1912 } 1913 } 1914 1915 @Override 1916 public int checkSignatures(int uid1, int uid2) { 1917 try { 1918 return mPM.checkUidSignatures(uid1, uid2); 1919 } catch (RemoteException e) { 1920 throw new RuntimeException("Package manager has died", e); 1921 } 1922 } 1923 1924 @Override 1925 public String[] getPackagesForUid(int uid) { 1926 try { 1927 return mPM.getPackagesForUid(uid); 1928 } catch (RemoteException e) { 1929 throw new RuntimeException("Package manager has died", e); 1930 } 1931 } 1932 1933 @Override 1934 public String getNameForUid(int uid) { 1935 try { 1936 return mPM.getNameForUid(uid); 1937 } catch (RemoteException e) { 1938 throw new RuntimeException("Package manager has died", e); 1939 } 1940 } 1941 1942 @Override 1943 public int getUidForSharedUser(String sharedUserName) 1944 throws NameNotFoundException { 1945 try { 1946 int uid = mPM.getUidForSharedUser(sharedUserName); 1947 if(uid != -1) { 1948 return uid; 1949 } 1950 } catch (RemoteException e) { 1951 throw new RuntimeException("Package manager has died", e); 1952 } 1953 throw new NameNotFoundException("No shared userid for user:"+sharedUserName); 1954 } 1955 1956 @Override 1957 public List<PackageInfo> getInstalledPackages(int flags) { 1958 try { 1959 return mPM.getInstalledPackages(flags); 1960 } catch (RemoteException e) { 1961 throw new RuntimeException("Package manager has died", e); 1962 } 1963 } 1964 1965 @Override 1966 public List<ApplicationInfo> getInstalledApplications(int flags) { 1967 try { 1968 return mPM.getInstalledApplications(flags); 1969 } catch (RemoteException e) { 1970 throw new RuntimeException("Package manager has died", e); 1971 } 1972 } 1973 1974 @Override 1975 public ResolveInfo resolveActivity(Intent intent, int flags) { 1976 try { 1977 return mPM.resolveIntent( 1978 intent, 1979 intent.resolveTypeIfNeeded(mContext.getContentResolver()), 1980 flags); 1981 } catch (RemoteException e) { 1982 throw new RuntimeException("Package manager has died", e); 1983 } 1984 } 1985 1986 @Override 1987 public List<ResolveInfo> queryIntentActivities(Intent intent, 1988 int flags) { 1989 try { 1990 return mPM.queryIntentActivities( 1991 intent, 1992 intent.resolveTypeIfNeeded(mContext.getContentResolver()), 1993 flags); 1994 } catch (RemoteException e) { 1995 throw new RuntimeException("Package manager has died", e); 1996 } 1997 } 1998 1999 @Override 2000 public List<ResolveInfo> queryIntentActivityOptions( 2001 ComponentName caller, Intent[] specifics, Intent intent, 2002 int flags) { 2003 final ContentResolver resolver = mContext.getContentResolver(); 2004 2005 String[] specificTypes = null; 2006 if (specifics != null) { 2007 final int N = specifics.length; 2008 for (int i=0; i<N; i++) { 2009 Intent sp = specifics[i]; 2010 if (sp != null) { 2011 String t = sp.resolveTypeIfNeeded(resolver); 2012 if (t != null) { 2013 if (specificTypes == null) { 2014 specificTypes = new String[N]; 2015 } 2016 specificTypes[i] = t; 2017 } 2018 } 2019 } 2020 } 2021 2022 try { 2023 return mPM.queryIntentActivityOptions(caller, specifics, 2024 specificTypes, intent, intent.resolveTypeIfNeeded(resolver), 2025 flags); 2026 } catch (RemoteException e) { 2027 throw new RuntimeException("Package manager has died", e); 2028 } 2029 } 2030 2031 @Override 2032 public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags) { 2033 try { 2034 return mPM.queryIntentReceivers( 2035 intent, 2036 intent.resolveTypeIfNeeded(mContext.getContentResolver()), 2037 flags); 2038 } catch (RemoteException e) { 2039 throw new RuntimeException("Package manager has died", e); 2040 } 2041 } 2042 2043 @Override 2044 public ResolveInfo resolveService(Intent intent, int flags) { 2045 try { 2046 return mPM.resolveService( 2047 intent, 2048 intent.resolveTypeIfNeeded(mContext.getContentResolver()), 2049 flags); 2050 } catch (RemoteException e) { 2051 throw new RuntimeException("Package manager has died", e); 2052 } 2053 } 2054 2055 @Override 2056 public List<ResolveInfo> queryIntentServices(Intent intent, int flags) { 2057 try { 2058 return mPM.queryIntentServices( 2059 intent, 2060 intent.resolveTypeIfNeeded(mContext.getContentResolver()), 2061 flags); 2062 } catch (RemoteException e) { 2063 throw new RuntimeException("Package manager has died", e); 2064 } 2065 } 2066 2067 @Override 2068 public ProviderInfo resolveContentProvider(String name, 2069 int flags) { 2070 try { 2071 return mPM.resolveContentProvider(name, flags); 2072 } catch (RemoteException e) { 2073 throw new RuntimeException("Package manager has died", e); 2074 } 2075 } 2076 2077 @Override 2078 public List<ProviderInfo> queryContentProviders(String processName, 2079 int uid, int flags) { 2080 try { 2081 return mPM.queryContentProviders(processName, uid, flags); 2082 } catch (RemoteException e) { 2083 throw new RuntimeException("Package manager has died", e); 2084 } 2085 } 2086 2087 @Override 2088 public InstrumentationInfo getInstrumentationInfo( 2089 ComponentName className, int flags) 2090 throws NameNotFoundException { 2091 try { 2092 InstrumentationInfo ii = mPM.getInstrumentationInfo( 2093 className, flags); 2094 if (ii != null) { 2095 return ii; 2096 } 2097 } catch (RemoteException e) { 2098 throw new RuntimeException("Package manager has died", e); 2099 } 2100 2101 throw new NameNotFoundException(className.toString()); 2102 } 2103 2104 @Override 2105 public List<InstrumentationInfo> queryInstrumentation( 2106 String targetPackage, int flags) { 2107 try { 2108 return mPM.queryInstrumentation(targetPackage, flags); 2109 } catch (RemoteException e) { 2110 throw new RuntimeException("Package manager has died", e); 2111 } 2112 } 2113 2114 @Override public Drawable getDrawable(String packageName, int resid, 2115 ApplicationInfo appInfo) { 2116 ResourceName name = new ResourceName(packageName, resid); 2117 Drawable dr = getCachedIcon(name); 2118 if (dr != null) { 2119 return dr; 2120 } 2121 if (appInfo == null) { 2122 try { 2123 appInfo = getApplicationInfo(packageName, 0); 2124 } catch (NameNotFoundException e) { 2125 return null; 2126 } 2127 } 2128 try { 2129 Resources r = getResourcesForApplication(appInfo); 2130 dr = r.getDrawable(resid); 2131 if (false) { 2132 RuntimeException e = new RuntimeException("here"); 2133 e.fillInStackTrace(); 2134 Log.w(TAG, "Getting drawable 0x" + Integer.toHexString(resid) 2135 + " from package " + packageName 2136 + ": app scale=" + r.getCompatibilityInfo().applicationScale 2137 + ", caller scale=" + mContext.getResources().getCompatibilityInfo().applicationScale, 2138 e); 2139 } 2140 if (DEBUG_ICONS) Log.v(TAG, "Getting drawable 0x" 2141 + Integer.toHexString(resid) + " from " + r 2142 + ": " + dr); 2143 putCachedIcon(name, dr); 2144 return dr; 2145 } catch (NameNotFoundException e) { 2146 Log.w("PackageManager", "Failure retrieving resources for" 2147 + appInfo.packageName); 2148 } catch (RuntimeException e) { 2149 // If an exception was thrown, fall through to return 2150 // default icon. 2151 Log.w("PackageManager", "Failure retrieving icon 0x" 2152 + Integer.toHexString(resid) + " in package " 2153 + packageName, e); 2154 } 2155 return null; 2156 } 2157 2158 @Override public Drawable getActivityIcon(ComponentName activityName) 2159 throws NameNotFoundException { 2160 return getActivityInfo(activityName, 0).loadIcon(this); 2161 } 2162 2163 @Override public Drawable getActivityIcon(Intent intent) 2164 throws NameNotFoundException { 2165 if (intent.getComponent() != null) { 2166 return getActivityIcon(intent.getComponent()); 2167 } 2168 2169 ResolveInfo info = resolveActivity( 2170 intent, PackageManager.MATCH_DEFAULT_ONLY); 2171 if (info != null) { 2172 return info.activityInfo.loadIcon(this); 2173 } 2174 2175 throw new NameNotFoundException(intent.toURI()); 2176 } 2177 2178 @Override public Drawable getDefaultActivityIcon() { 2179 return Resources.getSystem().getDrawable( 2180 com.android.internal.R.drawable.sym_def_app_icon); 2181 } 2182 2183 @Override public Drawable getApplicationIcon(ApplicationInfo info) { 2184 return info.loadIcon(this); 2185 } 2186 2187 @Override public Drawable getApplicationIcon(String packageName) 2188 throws NameNotFoundException { 2189 return getApplicationIcon(getApplicationInfo(packageName, 0)); 2190 } 2191 2192 @Override 2193 public Drawable getActivityLogo(ComponentName activityName) 2194 throws NameNotFoundException { 2195 return getActivityInfo(activityName, 0).loadLogo(this); 2196 } 2197 2198 @Override 2199 public Drawable getActivityLogo(Intent intent) 2200 throws NameNotFoundException { 2201 if (intent.getComponent() != null) { 2202 return getActivityLogo(intent.getComponent()); 2203 } 2204 2205 ResolveInfo info = resolveActivity( 2206 intent, PackageManager.MATCH_DEFAULT_ONLY); 2207 if (info != null) { 2208 return info.activityInfo.loadLogo(this); 2209 } 2210 2211 throw new NameNotFoundException(intent.toUri(0)); 2212 } 2213 2214 @Override 2215 public Drawable getApplicationLogo(ApplicationInfo info) { 2216 return info.loadLogo(this); 2217 } 2218 2219 @Override 2220 public Drawable getApplicationLogo(String packageName) 2221 throws NameNotFoundException { 2222 return getApplicationLogo(getApplicationInfo(packageName, 0)); 2223 } 2224 2225 @Override public Resources getResourcesForActivity( 2226 ComponentName activityName) throws NameNotFoundException { 2227 return getResourcesForApplication( 2228 getActivityInfo(activityName, 0).applicationInfo); 2229 } 2230 2231 @Override public Resources getResourcesForApplication( 2232 ApplicationInfo app) throws NameNotFoundException { 2233 if (app.packageName.equals("system")) { 2234 return mContext.mMainThread.getSystemContext().getResources(); 2235 } 2236 Resources r = mContext.mMainThread.getTopLevelResources( 2237 app.uid == Process.myUid() ? app.sourceDir 2238 : app.publicSourceDir, mContext.mPackageInfo); 2239 if (r != null) { 2240 return r; 2241 } 2242 throw new NameNotFoundException("Unable to open " + app.publicSourceDir); 2243 } 2244 2245 @Override public Resources getResourcesForApplication( 2246 String appPackageName) throws NameNotFoundException { 2247 return getResourcesForApplication( 2248 getApplicationInfo(appPackageName, 0)); 2249 } 2250 2251 int mCachedSafeMode = -1; 2252 @Override public boolean isSafeMode() { 2253 try { 2254 if (mCachedSafeMode < 0) { 2255 mCachedSafeMode = mPM.isSafeMode() ? 1 : 0; 2256 } 2257 return mCachedSafeMode != 0; 2258 } catch (RemoteException e) { 2259 throw new RuntimeException("Package manager has died", e); 2260 } 2261 } 2262 2263 static void configurationChanged() { 2264 synchronized (sSync) { 2265 sIconCache.clear(); 2266 sStringCache.clear(); 2267 } 2268 } 2269 2270 ApplicationPackageManager(ContextImpl context, 2271 IPackageManager pm) { 2272 mContext = context; 2273 mPM = pm; 2274 } 2275 2276 private Drawable getCachedIcon(ResourceName name) { 2277 synchronized (sSync) { 2278 WeakReference<Drawable> wr = sIconCache.get(name); 2279 if (DEBUG_ICONS) Log.v(TAG, "Get cached weak drawable ref for " 2280 + name + ": " + wr); 2281 if (wr != null) { // we have the activity 2282 Drawable dr = wr.get(); 2283 if (dr != null) { 2284 if (DEBUG_ICONS) Log.v(TAG, "Get cached drawable for " 2285 + name + ": " + dr); 2286 return dr; 2287 } 2288 // our entry has been purged 2289 sIconCache.remove(name); 2290 } 2291 } 2292 return null; 2293 } 2294 2295 private void putCachedIcon(ResourceName name, Drawable dr) { 2296 synchronized (sSync) { 2297 sIconCache.put(name, new WeakReference<Drawable>(dr)); 2298 if (DEBUG_ICONS) Log.v(TAG, "Added cached drawable for " 2299 + name + ": " + dr); 2300 } 2301 } 2302 2303 static final void handlePackageBroadcast(int cmd, String[] pkgList, 2304 boolean hasPkgInfo) { 2305 boolean immediateGc = false; 2306 if (cmd == IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE) { 2307 immediateGc = true; 2308 } 2309 if (pkgList != null && (pkgList.length > 0)) { 2310 boolean needCleanup = false; 2311 for (String ssp : pkgList) { 2312 synchronized (sSync) { 2313 if (sIconCache.size() > 0) { 2314 Iterator<ResourceName> it = sIconCache.keySet().iterator(); 2315 while (it.hasNext()) { 2316 ResourceName nm = it.next(); 2317 if (nm.packageName.equals(ssp)) { 2318 //Log.i(TAG, "Removing cached drawable for " + nm); 2319 it.remove(); 2320 needCleanup = true; 2321 } 2322 } 2323 } 2324 if (sStringCache.size() > 0) { 2325 Iterator<ResourceName> it = sStringCache.keySet().iterator(); 2326 while (it.hasNext()) { 2327 ResourceName nm = it.next(); 2328 if (nm.packageName.equals(ssp)) { 2329 //Log.i(TAG, "Removing cached string for " + nm); 2330 it.remove(); 2331 needCleanup = true; 2332 } 2333 } 2334 } 2335 } 2336 } 2337 if (needCleanup || hasPkgInfo) { 2338 if (immediateGc) { 2339 // Schedule an immediate gc. 2340 Runtime.getRuntime().gc(); 2341 } else { 2342 ActivityThread.currentActivityThread().scheduleGcIdler(); 2343 } 2344 } 2345 } 2346 } 2347 2348 private static final class ResourceName { 2349 final String packageName; 2350 final int iconId; 2351 2352 ResourceName(String _packageName, int _iconId) { 2353 packageName = _packageName; 2354 iconId = _iconId; 2355 } 2356 2357 ResourceName(ApplicationInfo aInfo, int _iconId) { 2358 this(aInfo.packageName, _iconId); 2359 } 2360 2361 ResourceName(ComponentInfo cInfo, int _iconId) { 2362 this(cInfo.applicationInfo.packageName, _iconId); 2363 } 2364 2365 ResourceName(ResolveInfo rInfo, int _iconId) { 2366 this(rInfo.activityInfo.applicationInfo.packageName, _iconId); 2367 } 2368 2369 @Override 2370 public boolean equals(Object o) { 2371 if (this == o) return true; 2372 if (o == null || getClass() != o.getClass()) return false; 2373 2374 ResourceName that = (ResourceName) o; 2375 2376 if (iconId != that.iconId) return false; 2377 return !(packageName != null ? 2378 !packageName.equals(that.packageName) : that.packageName != null); 2379 2380 } 2381 2382 @Override 2383 public int hashCode() { 2384 int result; 2385 result = packageName.hashCode(); 2386 result = 31 * result + iconId; 2387 return result; 2388 } 2389 2390 @Override 2391 public String toString() { 2392 return "{ResourceName " + packageName + " / " + iconId + "}"; 2393 } 2394 } 2395 2396 private CharSequence getCachedString(ResourceName name) { 2397 synchronized (sSync) { 2398 WeakReference<CharSequence> wr = sStringCache.get(name); 2399 if (wr != null) { // we have the activity 2400 CharSequence cs = wr.get(); 2401 if (cs != null) { 2402 return cs; 2403 } 2404 // our entry has been purged 2405 sStringCache.remove(name); 2406 } 2407 } 2408 return null; 2409 } 2410 2411 private void putCachedString(ResourceName name, CharSequence cs) { 2412 synchronized (sSync) { 2413 sStringCache.put(name, new WeakReference<CharSequence>(cs)); 2414 } 2415 } 2416 2417 @Override 2418 public CharSequence getText(String packageName, int resid, 2419 ApplicationInfo appInfo) { 2420 ResourceName name = new ResourceName(packageName, resid); 2421 CharSequence text = getCachedString(name); 2422 if (text != null) { 2423 return text; 2424 } 2425 if (appInfo == null) { 2426 try { 2427 appInfo = getApplicationInfo(packageName, 0); 2428 } catch (NameNotFoundException e) { 2429 return null; 2430 } 2431 } 2432 try { 2433 Resources r = getResourcesForApplication(appInfo); 2434 text = r.getText(resid); 2435 putCachedString(name, text); 2436 return text; 2437 } catch (NameNotFoundException e) { 2438 Log.w("PackageManager", "Failure retrieving resources for" 2439 + appInfo.packageName); 2440 } catch (RuntimeException e) { 2441 // If an exception was thrown, fall through to return 2442 // default icon. 2443 Log.w("PackageManager", "Failure retrieving text 0x" 2444 + Integer.toHexString(resid) + " in package " 2445 + packageName, e); 2446 } 2447 return null; 2448 } 2449 2450 @Override 2451 public XmlResourceParser getXml(String packageName, int resid, 2452 ApplicationInfo appInfo) { 2453 if (appInfo == null) { 2454 try { 2455 appInfo = getApplicationInfo(packageName, 0); 2456 } catch (NameNotFoundException e) { 2457 return null; 2458 } 2459 } 2460 try { 2461 Resources r = getResourcesForApplication(appInfo); 2462 return r.getXml(resid); 2463 } catch (RuntimeException e) { 2464 // If an exception was thrown, fall through to return 2465 // default icon. 2466 Log.w("PackageManager", "Failure retrieving xml 0x" 2467 + Integer.toHexString(resid) + " in package " 2468 + packageName, e); 2469 } catch (NameNotFoundException e) { 2470 Log.w("PackageManager", "Failure retrieving resources for" 2471 + appInfo.packageName); 2472 } 2473 return null; 2474 } 2475 2476 @Override 2477 public CharSequence getApplicationLabel(ApplicationInfo info) { 2478 return info.loadLabel(this); 2479 } 2480 2481 @Override 2482 public void installPackage(Uri packageURI, IPackageInstallObserver observer, int flags, 2483 String installerPackageName) { 2484 try { 2485 mPM.installPackage(packageURI, observer, flags, installerPackageName); 2486 } catch (RemoteException e) { 2487 // Should never happen! 2488 } 2489 } 2490 2491 @Override 2492 public void movePackage(String packageName, IPackageMoveObserver observer, int flags) { 2493 try { 2494 mPM.movePackage(packageName, observer, flags); 2495 } catch (RemoteException e) { 2496 // Should never happen! 2497 } 2498 } 2499 2500 @Override 2501 public String getInstallerPackageName(String packageName) { 2502 try { 2503 return mPM.getInstallerPackageName(packageName); 2504 } catch (RemoteException e) { 2505 // Should never happen! 2506 } 2507 return null; 2508 } 2509 2510 @Override 2511 public void deletePackage(String packageName, IPackageDeleteObserver observer, int flags) { 2512 try { 2513 mPM.deletePackage(packageName, observer, flags); 2514 } catch (RemoteException e) { 2515 // Should never happen! 2516 } 2517 } 2518 @Override 2519 public void clearApplicationUserData(String packageName, 2520 IPackageDataObserver observer) { 2521 try { 2522 mPM.clearApplicationUserData(packageName, observer); 2523 } catch (RemoteException e) { 2524 // Should never happen! 2525 } 2526 } 2527 @Override 2528 public void deleteApplicationCacheFiles(String packageName, 2529 IPackageDataObserver observer) { 2530 try { 2531 mPM.deleteApplicationCacheFiles(packageName, observer); 2532 } catch (RemoteException e) { 2533 // Should never happen! 2534 } 2535 } 2536 @Override 2537 public void freeStorageAndNotify(long idealStorageSize, IPackageDataObserver observer) { 2538 try { 2539 mPM.freeStorageAndNotify(idealStorageSize, observer); 2540 } catch (RemoteException e) { 2541 // Should never happen! 2542 } 2543 } 2544 2545 @Override 2546 public void freeStorage(long freeStorageSize, IntentSender pi) { 2547 try { 2548 mPM.freeStorage(freeStorageSize, pi); 2549 } catch (RemoteException e) { 2550 // Should never happen! 2551 } 2552 } 2553 2554 @Override 2555 public void getPackageSizeInfo(String packageName, 2556 IPackageStatsObserver observer) { 2557 try { 2558 mPM.getPackageSizeInfo(packageName, observer); 2559 } catch (RemoteException e) { 2560 // Should never happen! 2561 } 2562 } 2563 @Override 2564 public void addPackageToPreferred(String packageName) { 2565 try { 2566 mPM.addPackageToPreferred(packageName); 2567 } catch (RemoteException e) { 2568 // Should never happen! 2569 } 2570 } 2571 2572 @Override 2573 public void removePackageFromPreferred(String packageName) { 2574 try { 2575 mPM.removePackageFromPreferred(packageName); 2576 } catch (RemoteException e) { 2577 // Should never happen! 2578 } 2579 } 2580 2581 @Override 2582 public List<PackageInfo> getPreferredPackages(int flags) { 2583 try { 2584 return mPM.getPreferredPackages(flags); 2585 } catch (RemoteException e) { 2586 // Should never happen! 2587 } 2588 return new ArrayList<PackageInfo>(); 2589 } 2590 2591 @Override 2592 public void addPreferredActivity(IntentFilter filter, 2593 int match, ComponentName[] set, ComponentName activity) { 2594 try { 2595 mPM.addPreferredActivity(filter, match, set, activity); 2596 } catch (RemoteException e) { 2597 // Should never happen! 2598 } 2599 } 2600 2601 @Override 2602 public void replacePreferredActivity(IntentFilter filter, 2603 int match, ComponentName[] set, ComponentName activity) { 2604 try { 2605 mPM.replacePreferredActivity(filter, match, set, activity); 2606 } catch (RemoteException e) { 2607 // Should never happen! 2608 } 2609 } 2610 2611 @Override 2612 public void clearPackagePreferredActivities(String packageName) { 2613 try { 2614 mPM.clearPackagePreferredActivities(packageName); 2615 } catch (RemoteException e) { 2616 // Should never happen! 2617 } 2618 } 2619 2620 @Override 2621 public int getPreferredActivities(List<IntentFilter> outFilters, 2622 List<ComponentName> outActivities, String packageName) { 2623 try { 2624 return mPM.getPreferredActivities(outFilters, outActivities, packageName); 2625 } catch (RemoteException e) { 2626 // Should never happen! 2627 } 2628 return 0; 2629 } 2630 2631 @Override 2632 public void setComponentEnabledSetting(ComponentName componentName, 2633 int newState, int flags) { 2634 try { 2635 mPM.setComponentEnabledSetting(componentName, newState, flags); 2636 } catch (RemoteException e) { 2637 // Should never happen! 2638 } 2639 } 2640 2641 @Override 2642 public int getComponentEnabledSetting(ComponentName componentName) { 2643 try { 2644 return mPM.getComponentEnabledSetting(componentName); 2645 } catch (RemoteException e) { 2646 // Should never happen! 2647 } 2648 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; 2649 } 2650 2651 @Override 2652 public void setApplicationEnabledSetting(String packageName, 2653 int newState, int flags) { 2654 try { 2655 mPM.setApplicationEnabledSetting(packageName, newState, flags); 2656 } catch (RemoteException e) { 2657 // Should never happen! 2658 } 2659 } 2660 2661 @Override 2662 public int getApplicationEnabledSetting(String packageName) { 2663 try { 2664 return mPM.getApplicationEnabledSetting(packageName); 2665 } catch (RemoteException e) { 2666 // Should never happen! 2667 } 2668 return PackageManager.COMPONENT_ENABLED_STATE_DEFAULT; 2669 } 2670 2671 private final ContextImpl mContext; 2672 private final IPackageManager mPM; 2673 2674 private static final Object sSync = new Object(); 2675 private static HashMap<ResourceName, WeakReference<Drawable> > sIconCache 2676 = new HashMap<ResourceName, WeakReference<Drawable> >(); 2677 private static HashMap<ResourceName, WeakReference<CharSequence> > sStringCache 2678 = new HashMap<ResourceName, WeakReference<CharSequence> >(); 2679 } 2680 2681 // ---------------------------------------------------------------------- 2682 // ---------------------------------------------------------------------- 2683 // ---------------------------------------------------------------------- 2684 2685 private static final class SharedPreferencesImpl implements SharedPreferences { 2686 2687 private final File mFile; 2688 private final File mBackupFile; 2689 private final int mMode; 2690 private Map mMap; 2691 private final FileStatus mFileStatus = new FileStatus(); 2692 private long mTimestamp; 2693 2694 private static final Object mContent = new Object(); 2695 private WeakHashMap<OnSharedPreferenceChangeListener, Object> mListeners; 2696 2697 SharedPreferencesImpl( 2698 File file, int mode, Map initialContents) { 2699 mFile = file; 2700 mBackupFile = makeBackupFile(file); 2701 mMode = mode; 2702 mMap = initialContents != null ? initialContents : new HashMap(); 2703 if (FileUtils.getFileStatus(file.getPath(), mFileStatus)) { 2704 mTimestamp = mFileStatus.mtime; 2705 } 2706 mListeners = new WeakHashMap<OnSharedPreferenceChangeListener, Object>(); 2707 } 2708 2709 public boolean hasFileChanged() { 2710 synchronized (this) { 2711 if (!FileUtils.getFileStatus(mFile.getPath(), mFileStatus)) { 2712 return true; 2713 } 2714 return mTimestamp != mFileStatus.mtime; 2715 } 2716 } 2717 2718 public void replace(Map newContents) { 2719 if (newContents != null) { 2720 synchronized (this) { 2721 mMap = newContents; 2722 } 2723 } 2724 } 2725 2726 public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { 2727 synchronized(this) { 2728 mListeners.put(listener, mContent); 2729 } 2730 } 2731 2732 public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { 2733 synchronized(this) { 2734 mListeners.remove(listener); 2735 } 2736 } 2737 2738 public Map<String, ?> getAll() { 2739 synchronized(this) { 2740 //noinspection unchecked 2741 return new HashMap(mMap); 2742 } 2743 } 2744 2745 public String getString(String key, String defValue) { 2746 synchronized (this) { 2747 String v = (String)mMap.get(key); 2748 return v != null ? v : defValue; 2749 } 2750 } 2751 2752 public Set<String> getStringSet(String key, Set<String> defValues) { 2753 synchronized (this) { 2754 Set<String> v = (Set<String>) mMap.get(key); 2755 return v != null ? v : defValues; 2756 } 2757 } 2758 2759 public int getInt(String key, int defValue) { 2760 synchronized (this) { 2761 Integer v = (Integer)mMap.get(key); 2762 return v != null ? v : defValue; 2763 } 2764 } 2765 public long getLong(String key, long defValue) { 2766 synchronized (this) { 2767 Long v = (Long) mMap.get(key); 2768 return v != null ? v : defValue; 2769 } 2770 } 2771 public float getFloat(String key, float defValue) { 2772 synchronized (this) { 2773 Float v = (Float)mMap.get(key); 2774 return v != null ? v : defValue; 2775 } 2776 } 2777 public boolean getBoolean(String key, boolean defValue) { 2778 synchronized (this) { 2779 Boolean v = (Boolean)mMap.get(key); 2780 return v != null ? v : defValue; 2781 } 2782 } 2783 2784 public boolean contains(String key) { 2785 synchronized (this) { 2786 return mMap.containsKey(key); 2787 } 2788 } 2789 2790 public final class EditorImpl implements Editor { 2791 private final Map<String, Object> mModified = Maps.newHashMap(); 2792 private boolean mClear = false; 2793 2794 public Editor putString(String key, String value) { 2795 synchronized (this) { 2796 mModified.put(key, value); 2797 return this; 2798 } 2799 } 2800 public Editor putStringSet(String key, Set<String> values) { 2801 synchronized (this) { 2802 mModified.put(key, values); 2803 return this; 2804 } 2805 } 2806 public Editor putInt(String key, int value) { 2807 synchronized (this) { 2808 mModified.put(key, value); 2809 return this; 2810 } 2811 } 2812 public Editor putLong(String key, long value) { 2813 synchronized (this) { 2814 mModified.put(key, value); 2815 return this; 2816 } 2817 } 2818 public Editor putFloat(String key, float value) { 2819 synchronized (this) { 2820 mModified.put(key, value); 2821 return this; 2822 } 2823 } 2824 public Editor putBoolean(String key, boolean value) { 2825 synchronized (this) { 2826 mModified.put(key, value); 2827 return this; 2828 } 2829 } 2830 2831 public Editor remove(String key) { 2832 synchronized (this) { 2833 mModified.put(key, this); 2834 return this; 2835 } 2836 } 2837 2838 public Editor clear() { 2839 synchronized (this) { 2840 mClear = true; 2841 return this; 2842 } 2843 } 2844 2845 public boolean commit() { 2846 boolean returnValue; 2847 2848 boolean hasListeners; 2849 List<String> keysModified = null; 2850 Set<OnSharedPreferenceChangeListener> listeners = null; 2851 2852 synchronized (SharedPreferencesImpl.this) { 2853 hasListeners = mListeners.size() > 0; 2854 if (hasListeners) { 2855 keysModified = new ArrayList<String>(); 2856 listeners = 2857 new HashSet<OnSharedPreferenceChangeListener>(mListeners.keySet()); 2858 } 2859 2860 synchronized (this) { 2861 if (mClear) { 2862 mMap.clear(); 2863 mClear = false; 2864 } 2865 2866 for (Entry<String, Object> e : mModified.entrySet()) { 2867 String k = e.getKey(); 2868 Object v = e.getValue(); 2869 if (v == this) { 2870 mMap.remove(k); 2871 } else { 2872 mMap.put(k, v); 2873 } 2874 2875 if (hasListeners) { 2876 keysModified.add(k); 2877 } 2878 } 2879 2880 mModified.clear(); 2881 } 2882 2883 returnValue = writeFileLocked(); 2884 } 2885 2886 if (hasListeners) { 2887 for (int i = keysModified.size() - 1; i >= 0; i--) { 2888 final String key = keysModified.get(i); 2889 for (OnSharedPreferenceChangeListener listener : listeners) { 2890 if (listener != null) { 2891 listener.onSharedPreferenceChanged(SharedPreferencesImpl.this, key); 2892 } 2893 } 2894 } 2895 } 2896 2897 return returnValue; 2898 } 2899 } 2900 2901 public Editor edit() { 2902 return new EditorImpl(); 2903 } 2904 2905 private FileOutputStream createFileOutputStream(File file) { 2906 FileOutputStream str = null; 2907 try { 2908 str = new FileOutputStream(file); 2909 } catch (FileNotFoundException e) { 2910 File parent = file.getParentFile(); 2911 if (!parent.mkdir()) { 2912 Log.e(TAG, "Couldn't create directory for SharedPreferences file " + file); 2913 return null; 2914 } 2915 FileUtils.setPermissions( 2916 parent.getPath(), 2917 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, 2918 -1, -1); 2919 try { 2920 str = new FileOutputStream(file); 2921 } catch (FileNotFoundException e2) { 2922 Log.e(TAG, "Couldn't create SharedPreferences file " + file, e2); 2923 } 2924 } 2925 return str; 2926 } 2927 2928 private boolean writeFileLocked() { 2929 // Rename the current file so it may be used as a backup during the next read 2930 if (mFile.exists()) { 2931 if (!mBackupFile.exists()) { 2932 if (!mFile.renameTo(mBackupFile)) { 2933 Log.e(TAG, "Couldn't rename file " + mFile 2934 + " to backup file " + mBackupFile); 2935 return false; 2936 } 2937 } else { 2938 mFile.delete(); 2939 } 2940 } 2941 2942 // Attempt to write the file, delete the backup and return true as atomically as 2943 // possible. If any exception occurs, delete the new file; next time we will restore 2944 // from the backup. 2945 try { 2946 FileOutputStream str = createFileOutputStream(mFile); 2947 if (str == null) { 2948 return false; 2949 } 2950 XmlUtils.writeMapXml(mMap, str); 2951 str.close(); 2952 setFilePermissionsFromMode(mFile.getPath(), mMode, 0); 2953 if (FileUtils.getFileStatus(mFile.getPath(), mFileStatus)) { 2954 mTimestamp = mFileStatus.mtime; 2955 } 2956 2957 // Writing was successful, delete the backup file if there is one. 2958 mBackupFile.delete(); 2959 return true; 2960 } catch (XmlPullParserException e) { 2961 Log.w(TAG, "writeFileLocked: Got exception:", e); 2962 } catch (IOException e) { 2963 Log.w(TAG, "writeFileLocked: Got exception:", e); 2964 } 2965 // Clean up an unsuccessfully written file 2966 if (mFile.exists()) { 2967 if (!mFile.delete()) { 2968 Log.e(TAG, "Couldn't clean up partially-written file " + mFile); 2969 } 2970 } 2971 return false; 2972 } 2973 } 2974} 2975