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