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