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