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