ContextImpl.java revision 7d19e0242faac8017033dabb872cdf1542fa184c
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;
20
21import android.bluetooth.BluetoothAdapter;
22import android.content.BroadcastReceiver;
23import android.content.ComponentName;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.ContextWrapper;
27import android.content.IContentProvider;
28import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.IIntentReceiver;
31import android.content.IntentSender;
32import android.content.ReceiverCallNotAllowedException;
33import android.content.ServiceConnection;
34import android.content.SharedPreferences;
35import android.content.pm.ApplicationInfo;
36import android.content.pm.IPackageManager;
37import android.content.pm.PackageManager;
38import android.content.res.AssetManager;
39import android.content.res.CompatibilityInfo;
40import android.content.res.Resources;
41import android.database.DatabaseErrorHandler;
42import android.database.sqlite.SQLiteDatabase;
43import android.database.sqlite.SQLiteDatabase.CursorFactory;
44import android.graphics.Bitmap;
45import android.graphics.drawable.Drawable;
46import android.hardware.ISerialManager;
47import android.hardware.SensorManager;
48import android.hardware.SerialManager;
49import android.hardware.SystemSensorManager;
50import android.hardware.display.DisplayManager;
51import android.hardware.input.IInputManager;
52import android.hardware.input.InputManager;
53import android.hardware.usb.IUsbManager;
54import android.hardware.usb.UsbManager;
55import android.location.CountryDetector;
56import android.location.ICountryDetector;
57import android.location.ILocationManager;
58import android.location.LocationManager;
59import android.media.AudioManager;
60import android.media.MediaRouter;
61import android.net.ConnectivityManager;
62import android.net.IConnectivityManager;
63import android.net.INetworkPolicyManager;
64import android.net.NetworkPolicyManager;
65import android.net.ThrottleManager;
66import android.net.IThrottleManager;
67import android.net.Uri;
68import android.net.nsd.INsdManager;
69import android.net.nsd.NsdManager;
70import android.net.wifi.IWifiManager;
71import android.net.wifi.WifiManager;
72import android.net.wifi.p2p.IWifiP2pManager;
73import android.net.wifi.p2p.WifiP2pManager;
74import android.nfc.NfcManager;
75import android.os.Binder;
76import android.os.Bundle;
77import android.os.DropBoxManager;
78import android.os.Environment;
79import android.os.FileUtils;
80import android.os.Handler;
81import android.os.IBinder;
82import android.os.IPowerManager;
83import android.os.Looper;
84import android.os.PowerManager;
85import android.os.Process;
86import android.os.RemoteException;
87import android.os.ServiceManager;
88import android.os.UserId;
89import android.os.SystemVibrator;
90import android.os.storage.StorageManager;
91import android.telephony.TelephonyManager;
92import android.content.ClipboardManager;
93import android.util.AndroidRuntimeException;
94import android.util.Log;
95import android.view.ContextThemeWrapper;
96import android.view.WindowManagerImpl;
97import android.view.accessibility.AccessibilityManager;
98import android.view.inputmethod.InputMethodManager;
99import android.view.textservice.TextServicesManager;
100import android.accounts.AccountManager;
101import android.accounts.IAccountManager;
102import android.app.admin.DevicePolicyManager;
103import com.android.internal.os.IDropBoxManagerService;
104
105import java.io.File;
106import java.io.FileInputStream;
107import java.io.FileNotFoundException;
108import java.io.FileOutputStream;
109import java.io.IOException;
110import java.io.InputStream;
111import java.util.ArrayList;
112import java.util.HashMap;
113
114class ReceiverRestrictedContext extends ContextWrapper {
115    ReceiverRestrictedContext(Context base) {
116        super(base);
117    }
118
119    @Override
120    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
121        return registerReceiver(receiver, filter, null, null);
122    }
123
124    @Override
125    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
126            String broadcastPermission, Handler scheduler) {
127        throw new ReceiverCallNotAllowedException(
128                "IntentReceiver components are not allowed to register to receive intents");
129        //ex.fillInStackTrace();
130        //Log.e("IntentReceiver", ex.getMessage(), ex);
131        //return mContext.registerReceiver(receiver, filter, broadcastPermission,
132        //        scheduler);
133    }
134
135    @Override
136    public boolean bindService(Intent service, ServiceConnection conn, int flags) {
137        throw new ReceiverCallNotAllowedException(
138                "IntentReceiver components are not allowed to bind to services");
139        //ex.fillInStackTrace();
140        //Log.e("IntentReceiver", ex.getMessage(), ex);
141        //return mContext.bindService(service, interfaceName, conn, flags);
142    }
143}
144
145/**
146 * Common implementation of Context API, which provides the base
147 * context object for Activity and other application components.
148 */
149class ContextImpl extends Context {
150    private final static String TAG = "ApplicationContext";
151    private final static boolean DEBUG = false;
152
153    private static final HashMap<String, SharedPreferencesImpl> sSharedPrefs =
154            new HashMap<String, SharedPreferencesImpl>();
155
156    /*package*/ LoadedApk mPackageInfo;
157    private String mBasePackageName;
158    private Resources mResources;
159    /*package*/ ActivityThread mMainThread;
160    private Context mOuterContext;
161    private IBinder mActivityToken = null;
162    private ApplicationContentResolver mContentResolver;
163    private int mThemeResource = 0;
164    private Resources.Theme mTheme = null;
165    private PackageManager mPackageManager;
166    private Context mReceiverRestrictedContext = null;
167    private boolean mRestricted;
168
169    private final Object mSync = new Object();
170
171    private File mDatabasesDir;
172    private File mPreferencesDir;
173    private File mFilesDir;
174    private File mCacheDir;
175    private File mObbDir;
176    private File mExternalFilesDir;
177    private File mExternalCacheDir;
178
179    private static final String[] EMPTY_FILE_LIST = {};
180
181    /**
182     * Override this class when the system service constructor needs a
183     * ContextImpl.  Else, use StaticServiceFetcher below.
184     */
185    /*package*/ static class ServiceFetcher {
186        int mContextCacheIndex = -1;
187
188        /**
189         * Main entrypoint; only override if you don't need caching.
190         */
191        public Object getService(ContextImpl ctx) {
192            ArrayList<Object> cache = ctx.mServiceCache;
193            Object service;
194            synchronized (cache) {
195                if (cache.size() == 0) {
196                    // Initialize the cache vector on first access.
197                    // At this point sNextPerContextServiceCacheIndex
198                    // is the number of potential services that are
199                    // cached per-Context.
200                    for (int i = 0; i < sNextPerContextServiceCacheIndex; i++) {
201                        cache.add(null);
202                    }
203                } else {
204                    service = cache.get(mContextCacheIndex);
205                    if (service != null) {
206                        return service;
207                    }
208                }
209                service = createService(ctx);
210                cache.set(mContextCacheIndex, service);
211                return service;
212            }
213        }
214
215        /**
216         * Override this to create a new per-Context instance of the
217         * service.  getService() will handle locking and caching.
218         */
219        public Object createService(ContextImpl ctx) {
220            throw new RuntimeException("Not implemented");
221        }
222    }
223
224    /**
225     * Override this class for services to be cached process-wide.
226     */
227    abstract static class StaticServiceFetcher extends ServiceFetcher {
228        private Object mCachedInstance;
229
230        @Override
231        public final Object getService(ContextImpl unused) {
232            synchronized (StaticServiceFetcher.this) {
233                Object service = mCachedInstance;
234                if (service != null) {
235                    return service;
236                }
237                return mCachedInstance = createStaticService();
238            }
239        }
240
241        public abstract Object createStaticService();
242    }
243
244    private static final HashMap<String, ServiceFetcher> SYSTEM_SERVICE_MAP =
245            new HashMap<String, ServiceFetcher>();
246
247    private static int sNextPerContextServiceCacheIndex = 0;
248    private static void registerService(String serviceName, ServiceFetcher fetcher) {
249        if (!(fetcher instanceof StaticServiceFetcher)) {
250            fetcher.mContextCacheIndex = sNextPerContextServiceCacheIndex++;
251        }
252        SYSTEM_SERVICE_MAP.put(serviceName, fetcher);
253    }
254
255    // This one's defined separately and given a variable name so it
256    // can be re-used by getWallpaperManager(), avoiding a HashMap
257    // lookup.
258    private static ServiceFetcher WALLPAPER_FETCHER = new ServiceFetcher() {
259            public Object createService(ContextImpl ctx) {
260                return new WallpaperManager(ctx.getOuterContext(),
261                        ctx.mMainThread.getHandler());
262            }};
263
264    static {
265        registerService(ACCESSIBILITY_SERVICE, new ServiceFetcher() {
266                public Object getService(ContextImpl ctx) {
267                    return AccessibilityManager.getInstance(ctx);
268                }});
269
270        registerService(ACCOUNT_SERVICE, new ServiceFetcher() {
271                public Object createService(ContextImpl ctx) {
272                    IBinder b = ServiceManager.getService(ACCOUNT_SERVICE);
273                    IAccountManager service = IAccountManager.Stub.asInterface(b);
274                    return new AccountManager(ctx, service);
275                }});
276
277        registerService(ACTIVITY_SERVICE, new ServiceFetcher() {
278                public Object createService(ContextImpl ctx) {
279                    return new ActivityManager(ctx.getOuterContext(), ctx.mMainThread.getHandler());
280                }});
281
282        registerService(ALARM_SERVICE, new StaticServiceFetcher() {
283                public Object createStaticService() {
284                    IBinder b = ServiceManager.getService(ALARM_SERVICE);
285                    IAlarmManager service = IAlarmManager.Stub.asInterface(b);
286                    return new AlarmManager(service);
287                }});
288
289        registerService(AUDIO_SERVICE, new ServiceFetcher() {
290                public Object createService(ContextImpl ctx) {
291                    return new AudioManager(ctx);
292                }});
293
294        registerService(MEDIA_ROUTER_SERVICE, new ServiceFetcher() {
295                public Object createService(ContextImpl ctx) {
296                    return new MediaRouter(ctx);
297                }});
298
299        registerService(BLUETOOTH_SERVICE, new ServiceFetcher() {
300                public Object createService(ContextImpl ctx) {
301                    return BluetoothAdapter.getDefaultAdapter();
302                }});
303
304        registerService(CLIPBOARD_SERVICE, new ServiceFetcher() {
305                public Object createService(ContextImpl ctx) {
306                    return new ClipboardManager(ctx.getOuterContext(),
307                            ctx.mMainThread.getHandler());
308                }});
309
310        registerService(CONNECTIVITY_SERVICE, new StaticServiceFetcher() {
311                public Object createStaticService() {
312                    IBinder b = ServiceManager.getService(CONNECTIVITY_SERVICE);
313                    return new ConnectivityManager(IConnectivityManager.Stub.asInterface(b));
314                }});
315
316        registerService(COUNTRY_DETECTOR, new StaticServiceFetcher() {
317                public Object createStaticService() {
318                    IBinder b = ServiceManager.getService(COUNTRY_DETECTOR);
319                    return new CountryDetector(ICountryDetector.Stub.asInterface(b));
320                }});
321
322        registerService(DEVICE_POLICY_SERVICE, new ServiceFetcher() {
323                public Object createService(ContextImpl ctx) {
324                    return DevicePolicyManager.create(ctx, ctx.mMainThread.getHandler());
325                }});
326
327        registerService(DOWNLOAD_SERVICE, new ServiceFetcher() {
328                public Object createService(ContextImpl ctx) {
329                    return new DownloadManager(ctx.getContentResolver(), ctx.getPackageName());
330                }});
331
332        registerService(NFC_SERVICE, new ServiceFetcher() {
333                public Object createService(ContextImpl ctx) {
334                    return new NfcManager(ctx);
335                }});
336
337        registerService(DROPBOX_SERVICE, new StaticServiceFetcher() {
338                public Object createStaticService() {
339                    return createDropBoxManager();
340                }});
341
342        registerService(INPUT_SERVICE, new StaticServiceFetcher() {
343                public Object createStaticService() {
344                    return InputManager.getInstance();
345                }});
346
347        registerService(DISPLAY_SERVICE, new StaticServiceFetcher() {
348            public Object createStaticService() {
349                return DisplayManager.getInstance();
350            }});
351
352        registerService(INPUT_METHOD_SERVICE, new ServiceFetcher() {
353                public Object createService(ContextImpl ctx) {
354                    return InputMethodManager.getInstance(ctx);
355                }});
356
357        registerService(TEXT_SERVICES_MANAGER_SERVICE, new ServiceFetcher() {
358                public Object createService(ContextImpl ctx) {
359                    return TextServicesManager.getInstance();
360                }});
361
362        registerService(KEYGUARD_SERVICE, new ServiceFetcher() {
363                public Object getService(ContextImpl ctx) {
364                    // TODO: why isn't this caching it?  It wasn't
365                    // before, so I'm preserving the old behavior and
366                    // using getService(), instead of createService()
367                    // which would do the caching.
368                    return new KeyguardManager();
369                }});
370
371        registerService(LAYOUT_INFLATER_SERVICE, new ServiceFetcher() {
372                public Object createService(ContextImpl ctx) {
373                    return PolicyManager.makeNewLayoutInflater(ctx.getOuterContext());
374                }});
375
376        registerService(LOCATION_SERVICE, new ServiceFetcher() {
377                public Object createService(ContextImpl ctx) {
378                    IBinder b = ServiceManager.getService(LOCATION_SERVICE);
379                    return new LocationManager(ctx, ILocationManager.Stub.asInterface(b));
380                }});
381
382        registerService(NETWORK_POLICY_SERVICE, new ServiceFetcher() {
383            @Override
384            public Object createService(ContextImpl ctx) {
385                return new NetworkPolicyManager(INetworkPolicyManager.Stub.asInterface(
386                        ServiceManager.getService(NETWORK_POLICY_SERVICE)));
387            }
388        });
389
390        registerService(NOTIFICATION_SERVICE, new ServiceFetcher() {
391                public Object createService(ContextImpl ctx) {
392                    final Context outerContext = ctx.getOuterContext();
393                    return new NotificationManager(
394                        new ContextThemeWrapper(outerContext,
395                                Resources.selectSystemTheme(0,
396                                        outerContext.getApplicationInfo().targetSdkVersion,
397                                        com.android.internal.R.style.Theme_Dialog,
398                                        com.android.internal.R.style.Theme_Holo_Dialog,
399                                        com.android.internal.R.style.Theme_DeviceDefault_Dialog)),
400                        ctx.mMainThread.getHandler());
401                }});
402
403        registerService(NSD_SERVICE, new ServiceFetcher() {
404                @Override
405                public Object createService(ContextImpl ctx) {
406                    IBinder b = ServiceManager.getService(NSD_SERVICE);
407                    INsdManager service = INsdManager.Stub.asInterface(b);
408                    return new NsdManager(ctx.getOuterContext(), service);
409                }});
410
411        // Note: this was previously cached in a static variable, but
412        // constructed using mMainThread.getHandler(), so converting
413        // it to be a regular Context-cached service...
414        registerService(POWER_SERVICE, new ServiceFetcher() {
415                public Object createService(ContextImpl ctx) {
416                    IBinder b = ServiceManager.getService(POWER_SERVICE);
417                    IPowerManager service = IPowerManager.Stub.asInterface(b);
418                    return new PowerManager(service, ctx.mMainThread.getHandler());
419                }});
420
421        registerService(SEARCH_SERVICE, new ServiceFetcher() {
422                public Object createService(ContextImpl ctx) {
423                    return new SearchManager(ctx.getOuterContext(),
424                            ctx.mMainThread.getHandler());
425                }});
426
427        registerService(SENSOR_SERVICE, new ServiceFetcher() {
428                public Object createService(ContextImpl ctx) {
429                    return new SystemSensorManager(ctx.mMainThread.getHandler().getLooper());
430                }});
431
432        registerService(STATUS_BAR_SERVICE, new ServiceFetcher() {
433                public Object createService(ContextImpl ctx) {
434                    return new StatusBarManager(ctx.getOuterContext());
435                }});
436
437        registerService(STORAGE_SERVICE, new ServiceFetcher() {
438                public Object createService(ContextImpl ctx) {
439                    try {
440                        return new StorageManager(ctx.mMainThread.getHandler().getLooper());
441                    } catch (RemoteException rex) {
442                        Log.e(TAG, "Failed to create StorageManager", rex);
443                        return null;
444                    }
445                }});
446
447        registerService(TELEPHONY_SERVICE, new ServiceFetcher() {
448                public Object createService(ContextImpl ctx) {
449                    return new TelephonyManager(ctx.getOuterContext());
450                }});
451
452        registerService(THROTTLE_SERVICE, new StaticServiceFetcher() {
453                public Object createStaticService() {
454                    IBinder b = ServiceManager.getService(THROTTLE_SERVICE);
455                    return new ThrottleManager(IThrottleManager.Stub.asInterface(b));
456                }});
457
458        registerService(UI_MODE_SERVICE, new ServiceFetcher() {
459                public Object createService(ContextImpl ctx) {
460                    return new UiModeManager();
461                }});
462
463        registerService(USB_SERVICE, new ServiceFetcher() {
464                public Object createService(ContextImpl ctx) {
465                    IBinder b = ServiceManager.getService(USB_SERVICE);
466                    return new UsbManager(ctx, IUsbManager.Stub.asInterface(b));
467                }});
468
469        registerService(SERIAL_SERVICE, new ServiceFetcher() {
470                public Object createService(ContextImpl ctx) {
471                    IBinder b = ServiceManager.getService(SERIAL_SERVICE);
472                    return new SerialManager(ctx, ISerialManager.Stub.asInterface(b));
473                }});
474
475        registerService(VIBRATOR_SERVICE, new ServiceFetcher() {
476                public Object createService(ContextImpl ctx) {
477                    return new SystemVibrator();
478                }});
479
480        registerService(WALLPAPER_SERVICE, WALLPAPER_FETCHER);
481
482        registerService(WIFI_SERVICE, new ServiceFetcher() {
483                public Object createService(ContextImpl ctx) {
484                    IBinder b = ServiceManager.getService(WIFI_SERVICE);
485                    IWifiManager service = IWifiManager.Stub.asInterface(b);
486                    return new WifiManager(ctx.getOuterContext(), service);
487                }});
488
489        registerService(WIFI_P2P_SERVICE, new ServiceFetcher() {
490                public Object createService(ContextImpl ctx) {
491                    IBinder b = ServiceManager.getService(WIFI_P2P_SERVICE);
492                    IWifiP2pManager service = IWifiP2pManager.Stub.asInterface(b);
493                    return new WifiP2pManager(service);
494                }});
495
496        registerService(WINDOW_SERVICE, new ServiceFetcher() {
497                public Object getService(ContextImpl ctx) {
498                    return WindowManagerImpl.getDefault().makeCompatible(
499                            ctx.mPackageInfo.mCompatibilityInfo);
500                }});
501    }
502
503    static ContextImpl getImpl(Context context) {
504        Context nextContext;
505        while ((context instanceof ContextWrapper) &&
506                (nextContext=((ContextWrapper)context).getBaseContext()) != null) {
507            context = nextContext;
508        }
509        return (ContextImpl)context;
510    }
511
512    // The system service cache for the system services that are
513    // cached per-ContextImpl.  Package-scoped to avoid accessor
514    // methods.
515    final ArrayList<Object> mServiceCache = new ArrayList<Object>();
516
517    @Override
518    public AssetManager getAssets() {
519        return mResources.getAssets();
520    }
521
522    @Override
523    public Resources getResources() {
524        return mResources;
525    }
526
527    @Override
528    public PackageManager getPackageManager() {
529        if (mPackageManager != null) {
530            return mPackageManager;
531        }
532
533        IPackageManager pm = ActivityThread.getPackageManager();
534        if (pm != null) {
535            // Doesn't matter if we make more than one instance.
536            return (mPackageManager = new ApplicationPackageManager(this, pm));
537        }
538
539        return null;
540    }
541
542    @Override
543    public ContentResolver getContentResolver() {
544        return mContentResolver;
545    }
546
547    @Override
548    public Looper getMainLooper() {
549        return mMainThread.getLooper();
550    }
551
552    @Override
553    public Context getApplicationContext() {
554        return (mPackageInfo != null) ?
555                mPackageInfo.getApplication() : mMainThread.getApplication();
556    }
557
558    @Override
559    public void setTheme(int resid) {
560        mThemeResource = resid;
561    }
562
563    @Override
564    public int getThemeResId() {
565        return mThemeResource;
566    }
567
568    @Override
569    public Resources.Theme getTheme() {
570        if (mTheme == null) {
571            mThemeResource = Resources.selectDefaultTheme(mThemeResource,
572                    getOuterContext().getApplicationInfo().targetSdkVersion);
573            mTheme = mResources.newTheme();
574            mTheme.applyStyle(mThemeResource, true);
575        }
576        return mTheme;
577    }
578
579    @Override
580    public ClassLoader getClassLoader() {
581        return mPackageInfo != null ?
582                mPackageInfo.getClassLoader() : ClassLoader.getSystemClassLoader();
583    }
584
585    @Override
586    public String getPackageName() {
587        if (mPackageInfo != null) {
588            return mPackageInfo.getPackageName();
589        }
590        throw new RuntimeException("Not supported in system context");
591    }
592
593    @Override
594    public ApplicationInfo getApplicationInfo() {
595        if (mPackageInfo != null) {
596            return mPackageInfo.getApplicationInfo();
597        }
598        throw new RuntimeException("Not supported in system context");
599    }
600
601    @Override
602    public String getPackageResourcePath() {
603        if (mPackageInfo != null) {
604            return mPackageInfo.getResDir();
605        }
606        throw new RuntimeException("Not supported in system context");
607    }
608
609    @Override
610    public String getPackageCodePath() {
611        if (mPackageInfo != null) {
612            return mPackageInfo.getAppDir();
613        }
614        throw new RuntimeException("Not supported in system context");
615    }
616
617    public File getSharedPrefsFile(String name) {
618        return makeFilename(getPreferencesDir(), name + ".xml");
619    }
620
621    @Override
622    public SharedPreferences getSharedPreferences(String name, int mode) {
623        SharedPreferencesImpl sp;
624        synchronized (sSharedPrefs) {
625            sp = sSharedPrefs.get(name);
626            if (sp == null) {
627                File prefsFile = getSharedPrefsFile(name);
628                sp = new SharedPreferencesImpl(prefsFile, mode);
629                sSharedPrefs.put(name, sp);
630                return sp;
631            }
632        }
633        if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
634            getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
635            // If somebody else (some other process) changed the prefs
636            // file behind our back, we reload it.  This has been the
637            // historical (if undocumented) behavior.
638            sp.startReloadIfChangedUnexpectedly();
639        }
640        return sp;
641    }
642
643    private File getPreferencesDir() {
644        synchronized (mSync) {
645            if (mPreferencesDir == null) {
646                mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
647            }
648            return mPreferencesDir;
649        }
650    }
651
652    @Override
653    public FileInputStream openFileInput(String name)
654        throws FileNotFoundException {
655        File f = makeFilename(getFilesDir(), name);
656        return new FileInputStream(f);
657    }
658
659    @Override
660    public FileOutputStream openFileOutput(String name, int mode)
661        throws FileNotFoundException {
662        final boolean append = (mode&MODE_APPEND) != 0;
663        File f = makeFilename(getFilesDir(), name);
664        try {
665            FileOutputStream fos = new FileOutputStream(f, append);
666            setFilePermissionsFromMode(f.getPath(), mode, 0);
667            return fos;
668        } catch (FileNotFoundException e) {
669        }
670
671        File parent = f.getParentFile();
672        parent.mkdir();
673        FileUtils.setPermissions(
674            parent.getPath(),
675            FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
676            -1, -1);
677        FileOutputStream fos = new FileOutputStream(f, append);
678        setFilePermissionsFromMode(f.getPath(), mode, 0);
679        return fos;
680    }
681
682    @Override
683    public boolean deleteFile(String name) {
684        File f = makeFilename(getFilesDir(), name);
685        return f.delete();
686    }
687
688    @Override
689    public File getFilesDir() {
690        synchronized (mSync) {
691            if (mFilesDir == null) {
692                mFilesDir = new File(getDataDirFile(), "files");
693            }
694            if (!mFilesDir.exists()) {
695                if(!mFilesDir.mkdirs()) {
696                    Log.w(TAG, "Unable to create files directory " + mFilesDir.getPath());
697                    return null;
698                }
699                FileUtils.setPermissions(
700                        mFilesDir.getPath(),
701                        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
702                        -1, -1);
703            }
704            return mFilesDir;
705        }
706    }
707
708    @Override
709    public File getExternalFilesDir(String type) {
710        synchronized (mSync) {
711            if (mExternalFilesDir == null) {
712                mExternalFilesDir = Environment.getExternalStorageAppFilesDirectory(
713                        getPackageName());
714            }
715            if (!mExternalFilesDir.exists()) {
716                try {
717                    (new File(Environment.getExternalStorageAndroidDataDir(),
718                            ".nomedia")).createNewFile();
719                } catch (IOException e) {
720                }
721                if (!mExternalFilesDir.mkdirs()) {
722                    Log.w(TAG, "Unable to create external files directory");
723                    return null;
724                }
725            }
726            if (type == null) {
727                return mExternalFilesDir;
728            }
729            File dir = new File(mExternalFilesDir, type);
730            if (!dir.exists()) {
731                if (!dir.mkdirs()) {
732                    Log.w(TAG, "Unable to create external media directory " + dir);
733                    return null;
734                }
735            }
736            return dir;
737        }
738    }
739
740    @Override
741    public File getObbDir() {
742        synchronized (mSync) {
743            if (mObbDir == null) {
744                mObbDir = Environment.getExternalStorageAppObbDirectory(
745                        getPackageName());
746            }
747            return mObbDir;
748        }
749    }
750
751    @Override
752    public File getCacheDir() {
753        synchronized (mSync) {
754            if (mCacheDir == null) {
755                mCacheDir = new File(getDataDirFile(), "cache");
756            }
757            if (!mCacheDir.exists()) {
758                if(!mCacheDir.mkdirs()) {
759                    Log.w(TAG, "Unable to create cache directory");
760                    return null;
761                }
762                FileUtils.setPermissions(
763                        mCacheDir.getPath(),
764                        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
765                        -1, -1);
766            }
767        }
768        return mCacheDir;
769    }
770
771    @Override
772    public File getExternalCacheDir() {
773        synchronized (mSync) {
774            if (mExternalCacheDir == null) {
775                mExternalCacheDir = Environment.getExternalStorageAppCacheDirectory(
776                        getPackageName());
777            }
778            if (!mExternalCacheDir.exists()) {
779                try {
780                    (new File(Environment.getExternalStorageAndroidDataDir(),
781                            ".nomedia")).createNewFile();
782                } catch (IOException e) {
783                }
784                if (!mExternalCacheDir.mkdirs()) {
785                    Log.w(TAG, "Unable to create external cache directory");
786                    return null;
787                }
788            }
789            return mExternalCacheDir;
790        }
791    }
792
793    @Override
794    public File getFileStreamPath(String name) {
795        return makeFilename(getFilesDir(), name);
796    }
797
798    @Override
799    public String[] fileList() {
800        final String[] list = getFilesDir().list();
801        return (list != null) ? list : EMPTY_FILE_LIST;
802    }
803
804    @Override
805    public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
806        return openOrCreateDatabase(name, mode, factory, null);
807    }
808
809    @Override
810    public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory,
811            DatabaseErrorHandler errorHandler) {
812        File f = validateFilePath(name, true);
813        int flags = SQLiteDatabase.CREATE_IF_NECESSARY;
814        if ((mode & MODE_ENABLE_WRITE_AHEAD_LOGGING) != 0) {
815            flags |= SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING;
816        }
817        SQLiteDatabase db = SQLiteDatabase.openDatabase(f.getPath(), factory, flags, errorHandler);
818        setFilePermissionsFromMode(f.getPath(), mode, 0);
819        return db;
820    }
821
822    @Override
823    public boolean deleteDatabase(String name) {
824        try {
825            File f = validateFilePath(name, false);
826            return SQLiteDatabase.deleteDatabase(f);
827        } catch (Exception e) {
828        }
829        return false;
830    }
831
832    @Override
833    public File getDatabasePath(String name) {
834        return validateFilePath(name, false);
835    }
836
837    @Override
838    public String[] databaseList() {
839        final String[] list = getDatabasesDir().list();
840        return (list != null) ? list : EMPTY_FILE_LIST;
841    }
842
843
844    private File getDatabasesDir() {
845        synchronized (mSync) {
846            if (mDatabasesDir == null) {
847                mDatabasesDir = new File(getDataDirFile(), "databases");
848            }
849            if (mDatabasesDir.getPath().equals("databases")) {
850                mDatabasesDir = new File("/data/system");
851            }
852            return mDatabasesDir;
853        }
854    }
855
856    @Override
857    public Drawable getWallpaper() {
858        return getWallpaperManager().getDrawable();
859    }
860
861    @Override
862    public Drawable peekWallpaper() {
863        return getWallpaperManager().peekDrawable();
864    }
865
866    @Override
867    public int getWallpaperDesiredMinimumWidth() {
868        return getWallpaperManager().getDesiredMinimumWidth();
869    }
870
871    @Override
872    public int getWallpaperDesiredMinimumHeight() {
873        return getWallpaperManager().getDesiredMinimumHeight();
874    }
875
876    @Override
877    public void setWallpaper(Bitmap bitmap) throws IOException  {
878        getWallpaperManager().setBitmap(bitmap);
879    }
880
881    @Override
882    public void setWallpaper(InputStream data) throws IOException {
883        getWallpaperManager().setStream(data);
884    }
885
886    @Override
887    public void clearWallpaper() throws IOException {
888        getWallpaperManager().clear();
889    }
890
891    @Override
892    public void startActivity(Intent intent) {
893        startActivity(intent, null);
894    }
895
896    @Override
897    public void startActivity(Intent intent, Bundle options) {
898        if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
899            throw new AndroidRuntimeException(
900                    "Calling startActivity() from outside of an Activity "
901                    + " context requires the FLAG_ACTIVITY_NEW_TASK flag."
902                    + " Is this really what you want?");
903        }
904        mMainThread.getInstrumentation().execStartActivity(
905            getOuterContext(), mMainThread.getApplicationThread(), null,
906            (Activity)null, intent, -1, options);
907    }
908
909    @Override
910    public void startActivities(Intent[] intents) {
911        startActivities(intents, null);
912    }
913
914    @Override
915    public void startActivities(Intent[] intents, Bundle options) {
916        if ((intents[0].getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
917            throw new AndroidRuntimeException(
918                    "Calling startActivities() from outside of an Activity "
919                    + " context requires the FLAG_ACTIVITY_NEW_TASK flag on first Intent."
920                    + " Is this really what you want?");
921        }
922        mMainThread.getInstrumentation().execStartActivities(
923            getOuterContext(), mMainThread.getApplicationThread(), null,
924            (Activity)null, intents, options);
925    }
926
927    @Override
928    public void startIntentSender(IntentSender intent,
929            Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags)
930            throws IntentSender.SendIntentException {
931        startIntentSender(intent, fillInIntent, flagsMask, flagsValues, extraFlags, null);
932    }
933
934    @Override
935    public void startIntentSender(IntentSender intent, Intent fillInIntent,
936            int flagsMask, int flagsValues, int extraFlags, Bundle options)
937            throws IntentSender.SendIntentException {
938        try {
939            String resolvedType = null;
940            if (fillInIntent != null) {
941                fillInIntent.setAllowFds(false);
942                resolvedType = fillInIntent.resolveTypeIfNeeded(getContentResolver());
943            }
944            int result = ActivityManagerNative.getDefault()
945                .startActivityIntentSender(mMainThread.getApplicationThread(), intent,
946                        fillInIntent, resolvedType, null, null,
947                        0, flagsMask, flagsValues, options);
948            if (result == ActivityManager.START_CANCELED) {
949                throw new IntentSender.SendIntentException();
950            }
951            Instrumentation.checkStartActivityResult(result, null);
952        } catch (RemoteException e) {
953        }
954    }
955
956    @Override
957    public void sendBroadcast(Intent intent) {
958        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
959        try {
960            intent.setAllowFds(false);
961            ActivityManagerNative.getDefault().broadcastIntent(
962                mMainThread.getApplicationThread(), intent, resolvedType, null,
963                Activity.RESULT_OK, null, null, null, false, false,
964                Binder.getOrigCallingUser());
965        } catch (RemoteException e) {
966        }
967    }
968
969    @Override
970    public void sendBroadcast(Intent intent, String receiverPermission) {
971        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
972        try {
973            intent.setAllowFds(false);
974            ActivityManagerNative.getDefault().broadcastIntent(
975                mMainThread.getApplicationThread(), intent, resolvedType, null,
976                Activity.RESULT_OK, null, null, receiverPermission, false, false,
977                Binder.getOrigCallingUser());
978        } catch (RemoteException e) {
979        }
980    }
981
982    @Override
983    public void sendOrderedBroadcast(Intent intent,
984            String receiverPermission) {
985        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
986        try {
987            intent.setAllowFds(false);
988            ActivityManagerNative.getDefault().broadcastIntent(
989                mMainThread.getApplicationThread(), intent, resolvedType, null,
990                Activity.RESULT_OK, null, null, receiverPermission, true, false,
991                Binder.getOrigCallingUser());
992        } catch (RemoteException e) {
993        }
994    }
995
996    @Override
997    public void sendOrderedBroadcast(Intent intent,
998            String receiverPermission, BroadcastReceiver resultReceiver,
999            Handler scheduler, int initialCode, String initialData,
1000            Bundle initialExtras) {
1001        IIntentReceiver rd = null;
1002        if (resultReceiver != null) {
1003            if (mPackageInfo != null) {
1004                if (scheduler == null) {
1005                    scheduler = mMainThread.getHandler();
1006                }
1007                rd = mPackageInfo.getReceiverDispatcher(
1008                    resultReceiver, getOuterContext(), scheduler,
1009                    mMainThread.getInstrumentation(), false);
1010            } else {
1011                if (scheduler == null) {
1012                    scheduler = mMainThread.getHandler();
1013                }
1014                rd = new LoadedApk.ReceiverDispatcher(
1015                        resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1016            }
1017        }
1018        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1019        try {
1020            intent.setAllowFds(false);
1021            ActivityManagerNative.getDefault().broadcastIntent(
1022                mMainThread.getApplicationThread(), intent, resolvedType, rd,
1023                initialCode, initialData, initialExtras, receiverPermission,
1024                true, false, Binder.getOrigCallingUser());
1025        } catch (RemoteException e) {
1026        }
1027    }
1028
1029    @Override
1030    public void sendBroadcastToUser(Intent intent, int userHandle) {
1031        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1032        try {
1033            intent.setAllowFds(false);
1034            ActivityManagerNative.getDefault().broadcastIntent(mMainThread.getApplicationThread(),
1035                    intent, resolvedType, null, Activity.RESULT_OK, null, null, null, false, false,
1036                    userHandle);
1037        } catch (RemoteException e) {
1038        }
1039    }
1040
1041    @Override
1042    public void sendOrderedBroadcastToUser(Intent intent, int userHandle,
1043            BroadcastReceiver resultReceiver, Handler scheduler,
1044            int initialCode, String initialData, Bundle initialExtras) {
1045        IIntentReceiver rd = null;
1046        if (resultReceiver != null) {
1047            if (mPackageInfo != null) {
1048                if (scheduler == null) {
1049                    scheduler = mMainThread.getHandler();
1050                }
1051                rd = mPackageInfo.getReceiverDispatcher(
1052                    resultReceiver, getOuterContext(), scheduler,
1053                    mMainThread.getInstrumentation(), false);
1054            } else {
1055                if (scheduler == null) {
1056                    scheduler = mMainThread.getHandler();
1057                }
1058                rd = new LoadedApk.ReceiverDispatcher(
1059                        resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1060            }
1061        }
1062        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1063        try {
1064            intent.setAllowFds(false);
1065            ActivityManagerNative.getDefault().broadcastIntent(
1066                mMainThread.getApplicationThread(), intent, resolvedType, rd,
1067                initialCode, initialData, initialExtras, null,
1068                true, false, userHandle);
1069        } catch (RemoteException e) {
1070        }
1071    }
1072
1073    @Override
1074    public void sendStickyBroadcast(Intent intent) {
1075        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1076        try {
1077            intent.setAllowFds(false);
1078            ActivityManagerNative.getDefault().broadcastIntent(
1079                mMainThread.getApplicationThread(), intent, resolvedType, null,
1080                Activity.RESULT_OK, null, null, null, false, true,
1081                Binder.getOrigCallingUser());
1082        } catch (RemoteException e) {
1083        }
1084    }
1085
1086    @Override
1087    public void sendStickyOrderedBroadcast(Intent intent,
1088            BroadcastReceiver resultReceiver,
1089            Handler scheduler, int initialCode, String initialData,
1090            Bundle initialExtras) {
1091        IIntentReceiver rd = null;
1092        if (resultReceiver != null) {
1093            if (mPackageInfo != null) {
1094                if (scheduler == null) {
1095                    scheduler = mMainThread.getHandler();
1096                }
1097                rd = mPackageInfo.getReceiverDispatcher(
1098                    resultReceiver, getOuterContext(), scheduler,
1099                    mMainThread.getInstrumentation(), false);
1100            } else {
1101                if (scheduler == null) {
1102                    scheduler = mMainThread.getHandler();
1103                }
1104                rd = new LoadedApk.ReceiverDispatcher(
1105                        resultReceiver, getOuterContext(), scheduler, null, false).getIIntentReceiver();
1106            }
1107        }
1108        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1109        try {
1110            intent.setAllowFds(false);
1111            ActivityManagerNative.getDefault().broadcastIntent(
1112                mMainThread.getApplicationThread(), intent, resolvedType, rd,
1113                initialCode, initialData, initialExtras, null,
1114                true, true, Binder.getOrigCallingUser());
1115        } catch (RemoteException e) {
1116        }
1117    }
1118
1119    @Override
1120    public void removeStickyBroadcast(Intent intent) {
1121        String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
1122        if (resolvedType != null) {
1123            intent = new Intent(intent);
1124            intent.setDataAndType(intent.getData(), resolvedType);
1125        }
1126        try {
1127            intent.setAllowFds(false);
1128            ActivityManagerNative.getDefault().unbroadcastIntent(
1129                    mMainThread.getApplicationThread(), intent, Binder.getOrigCallingUser());
1130        } catch (RemoteException e) {
1131        }
1132    }
1133
1134    @Override
1135    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
1136        return registerReceiver(receiver, filter, null, null);
1137    }
1138
1139    @Override
1140    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter,
1141            String broadcastPermission, Handler scheduler) {
1142        return registerReceiverInternal(receiver, filter, broadcastPermission,
1143                scheduler, getOuterContext());
1144    }
1145
1146    private Intent registerReceiverInternal(BroadcastReceiver receiver,
1147            IntentFilter filter, String broadcastPermission,
1148            Handler scheduler, Context context) {
1149        IIntentReceiver rd = null;
1150        if (receiver != null) {
1151            if (mPackageInfo != null && context != null) {
1152                if (scheduler == null) {
1153                    scheduler = mMainThread.getHandler();
1154                }
1155                rd = mPackageInfo.getReceiverDispatcher(
1156                    receiver, context, scheduler,
1157                    mMainThread.getInstrumentation(), true);
1158            } else {
1159                if (scheduler == null) {
1160                    scheduler = mMainThread.getHandler();
1161                }
1162                rd = new LoadedApk.ReceiverDispatcher(
1163                        receiver, context, scheduler, null, true).getIIntentReceiver();
1164            }
1165        }
1166        try {
1167            return ActivityManagerNative.getDefault().registerReceiver(
1168                    mMainThread.getApplicationThread(), mBasePackageName,
1169                    rd, filter, broadcastPermission);
1170        } catch (RemoteException e) {
1171            return null;
1172        }
1173    }
1174
1175    @Override
1176    public void unregisterReceiver(BroadcastReceiver receiver) {
1177        if (mPackageInfo != null) {
1178            IIntentReceiver rd = mPackageInfo.forgetReceiverDispatcher(
1179                    getOuterContext(), receiver);
1180            try {
1181                ActivityManagerNative.getDefault().unregisterReceiver(rd);
1182            } catch (RemoteException e) {
1183            }
1184        } else {
1185            throw new RuntimeException("Not supported in system context");
1186        }
1187    }
1188
1189    @Override
1190    public ComponentName startService(Intent service) {
1191        try {
1192            service.setAllowFds(false);
1193            ComponentName cn = ActivityManagerNative.getDefault().startService(
1194                mMainThread.getApplicationThread(), service,
1195                service.resolveTypeIfNeeded(getContentResolver()));
1196            if (cn != null && cn.getPackageName().equals("!")) {
1197                throw new SecurityException(
1198                        "Not allowed to start service " + service
1199                        + " without permission " + cn.getClassName());
1200            }
1201            return cn;
1202        } catch (RemoteException e) {
1203            return null;
1204        }
1205    }
1206
1207    @Override
1208    public boolean stopService(Intent service) {
1209        try {
1210            service.setAllowFds(false);
1211            int res = ActivityManagerNative.getDefault().stopService(
1212                mMainThread.getApplicationThread(), service,
1213                service.resolveTypeIfNeeded(getContentResolver()));
1214            if (res < 0) {
1215                throw new SecurityException(
1216                        "Not allowed to stop service " + service);
1217            }
1218            return res != 0;
1219        } catch (RemoteException e) {
1220            return false;
1221        }
1222    }
1223
1224    @Override
1225    public boolean bindService(Intent service, ServiceConnection conn,
1226            int flags) {
1227        return bindService(service, conn, flags, UserId.getUserId(Process.myUid()));
1228    }
1229
1230    /** @hide */
1231    @Override
1232    public boolean bindService(Intent service, ServiceConnection conn, int flags, int userHandle) {
1233        IServiceConnection sd;
1234        if (conn == null) {
1235            throw new IllegalArgumentException("connection is null");
1236        }
1237        if (mPackageInfo != null) {
1238            sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
1239                    mMainThread.getHandler(), flags);
1240        } else {
1241            throw new RuntimeException("Not supported in system context");
1242        }
1243        try {
1244            IBinder token = getActivityToken();
1245            if (token == null && (flags&BIND_AUTO_CREATE) == 0 && mPackageInfo != null
1246                    && mPackageInfo.getApplicationInfo().targetSdkVersion
1247                    < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
1248                flags |= BIND_WAIVE_PRIORITY;
1249            }
1250            service.setAllowFds(false);
1251            int res = ActivityManagerNative.getDefault().bindService(
1252                mMainThread.getApplicationThread(), getActivityToken(),
1253                service, service.resolveTypeIfNeeded(getContentResolver()),
1254                sd, flags, userHandle);
1255            if (res < 0) {
1256                throw new SecurityException(
1257                        "Not allowed to bind to service " + service);
1258            }
1259            return res != 0;
1260        } catch (RemoteException e) {
1261            return false;
1262        }
1263    }
1264
1265    @Override
1266    public void unbindService(ServiceConnection conn) {
1267        if (conn == null) {
1268            throw new IllegalArgumentException("connection is null");
1269        }
1270        if (mPackageInfo != null) {
1271            IServiceConnection sd = mPackageInfo.forgetServiceDispatcher(
1272                    getOuterContext(), conn);
1273            try {
1274                ActivityManagerNative.getDefault().unbindService(sd);
1275            } catch (RemoteException e) {
1276            }
1277        } else {
1278            throw new RuntimeException("Not supported in system context");
1279        }
1280    }
1281
1282    @Override
1283    public boolean startInstrumentation(ComponentName className,
1284            String profileFile, Bundle arguments) {
1285        try {
1286            if (arguments != null) {
1287                arguments.setAllowFds(false);
1288            }
1289            return ActivityManagerNative.getDefault().startInstrumentation(
1290                    className, profileFile, 0, arguments, null);
1291        } catch (RemoteException e) {
1292            // System has crashed, nothing we can do.
1293        }
1294        return false;
1295    }
1296
1297    @Override
1298    public Object getSystemService(String name) {
1299        ServiceFetcher fetcher = SYSTEM_SERVICE_MAP.get(name);
1300        return fetcher == null ? null : fetcher.getService(this);
1301    }
1302
1303    private WallpaperManager getWallpaperManager() {
1304        return (WallpaperManager) WALLPAPER_FETCHER.getService(this);
1305    }
1306
1307    /* package */ static DropBoxManager createDropBoxManager() {
1308        IBinder b = ServiceManager.getService(DROPBOX_SERVICE);
1309        IDropBoxManagerService service = IDropBoxManagerService.Stub.asInterface(b);
1310        if (service == null) {
1311            // Don't return a DropBoxManager that will NPE upon use.
1312            // This also avoids caching a broken DropBoxManager in
1313            // getDropBoxManager during early boot, before the
1314            // DROPBOX_SERVICE is registered.
1315            return null;
1316        }
1317        return new DropBoxManager(service);
1318    }
1319
1320    @Override
1321    public int checkPermission(String permission, int pid, int uid) {
1322        if (permission == null) {
1323            throw new IllegalArgumentException("permission is null");
1324        }
1325
1326        try {
1327            return ActivityManagerNative.getDefault().checkPermission(
1328                    permission, pid, uid);
1329        } catch (RemoteException e) {
1330            return PackageManager.PERMISSION_DENIED;
1331        }
1332    }
1333
1334    @Override
1335    public int checkCallingPermission(String permission) {
1336        if (permission == null) {
1337            throw new IllegalArgumentException("permission is null");
1338        }
1339
1340        int pid = Binder.getCallingPid();
1341        if (pid != Process.myPid()) {
1342            return checkPermission(permission, pid, Binder.getCallingUid());
1343        }
1344        return PackageManager.PERMISSION_DENIED;
1345    }
1346
1347    @Override
1348    public int checkCallingOrSelfPermission(String permission) {
1349        if (permission == null) {
1350            throw new IllegalArgumentException("permission is null");
1351        }
1352
1353        return checkPermission(permission, Binder.getCallingPid(),
1354                Binder.getCallingUid());
1355    }
1356
1357    private void enforce(
1358            String permission, int resultOfCheck,
1359            boolean selfToo, int uid, String message) {
1360        if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1361            throw new SecurityException(
1362                    (message != null ? (message + ": ") : "") +
1363                    (selfToo
1364                     ? "Neither user " + uid + " nor current process has "
1365                     : "User " + uid + " does not have ") +
1366                    permission +
1367                    ".");
1368        }
1369    }
1370
1371    public void enforcePermission(
1372            String permission, int pid, int uid, String message) {
1373        enforce(permission,
1374                checkPermission(permission, pid, uid),
1375                false,
1376                uid,
1377                message);
1378    }
1379
1380    public void enforceCallingPermission(String permission, String message) {
1381        enforce(permission,
1382                checkCallingPermission(permission),
1383                false,
1384                Binder.getCallingUid(),
1385                message);
1386    }
1387
1388    public void enforceCallingOrSelfPermission(
1389            String permission, String message) {
1390        enforce(permission,
1391                checkCallingOrSelfPermission(permission),
1392                true,
1393                Binder.getCallingUid(),
1394                message);
1395    }
1396
1397    @Override
1398    public void grantUriPermission(String toPackage, Uri uri, int modeFlags) {
1399         try {
1400            ActivityManagerNative.getDefault().grantUriPermission(
1401                    mMainThread.getApplicationThread(), toPackage, uri,
1402                    modeFlags);
1403        } catch (RemoteException e) {
1404        }
1405    }
1406
1407    @Override
1408    public void revokeUriPermission(Uri uri, int modeFlags) {
1409         try {
1410            ActivityManagerNative.getDefault().revokeUriPermission(
1411                    mMainThread.getApplicationThread(), uri,
1412                    modeFlags);
1413        } catch (RemoteException e) {
1414        }
1415    }
1416
1417    @Override
1418    public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
1419        try {
1420            return ActivityManagerNative.getDefault().checkUriPermission(
1421                    uri, pid, uid, modeFlags);
1422        } catch (RemoteException e) {
1423            return PackageManager.PERMISSION_DENIED;
1424        }
1425    }
1426
1427    @Override
1428    public int checkCallingUriPermission(Uri uri, int modeFlags) {
1429        int pid = Binder.getCallingPid();
1430        if (pid != Process.myPid()) {
1431            return checkUriPermission(uri, pid,
1432                    Binder.getCallingUid(), modeFlags);
1433        }
1434        return PackageManager.PERMISSION_DENIED;
1435    }
1436
1437    @Override
1438    public int checkCallingOrSelfUriPermission(Uri uri, int modeFlags) {
1439        return checkUriPermission(uri, Binder.getCallingPid(),
1440                Binder.getCallingUid(), modeFlags);
1441    }
1442
1443    @Override
1444    public int checkUriPermission(Uri uri, String readPermission,
1445            String writePermission, int pid, int uid, int modeFlags) {
1446        if (DEBUG) {
1447            Log.i("foo", "checkUriPermission: uri=" + uri + "readPermission="
1448                    + readPermission + " writePermission=" + writePermission
1449                    + " pid=" + pid + " uid=" + uid + " mode" + modeFlags);
1450        }
1451        if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
1452            if (readPermission == null
1453                    || checkPermission(readPermission, pid, uid)
1454                    == PackageManager.PERMISSION_GRANTED) {
1455                return PackageManager.PERMISSION_GRANTED;
1456            }
1457        }
1458        if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
1459            if (writePermission == null
1460                    || checkPermission(writePermission, pid, uid)
1461                    == PackageManager.PERMISSION_GRANTED) {
1462                return PackageManager.PERMISSION_GRANTED;
1463            }
1464        }
1465        return uri != null ? checkUriPermission(uri, pid, uid, modeFlags)
1466                : PackageManager.PERMISSION_DENIED;
1467    }
1468
1469    private String uriModeFlagToString(int uriModeFlags) {
1470        switch (uriModeFlags) {
1471            case Intent.FLAG_GRANT_READ_URI_PERMISSION |
1472                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1473                return "read and write";
1474            case Intent.FLAG_GRANT_READ_URI_PERMISSION:
1475                return "read";
1476            case Intent.FLAG_GRANT_WRITE_URI_PERMISSION:
1477                return "write";
1478        }
1479        throw new IllegalArgumentException(
1480                "Unknown permission mode flags: " + uriModeFlags);
1481    }
1482
1483    private void enforceForUri(
1484            int modeFlags, int resultOfCheck, boolean selfToo,
1485            int uid, Uri uri, String message) {
1486        if (resultOfCheck != PackageManager.PERMISSION_GRANTED) {
1487            throw new SecurityException(
1488                    (message != null ? (message + ": ") : "") +
1489                    (selfToo
1490                     ? "Neither user " + uid + " nor current process has "
1491                     : "User " + uid + " does not have ") +
1492                    uriModeFlagToString(modeFlags) +
1493                    " permission on " +
1494                    uri +
1495                    ".");
1496        }
1497    }
1498
1499    public void enforceUriPermission(
1500            Uri uri, int pid, int uid, int modeFlags, String message) {
1501        enforceForUri(
1502                modeFlags, checkUriPermission(uri, pid, uid, modeFlags),
1503                false, uid, uri, message);
1504    }
1505
1506    public void enforceCallingUriPermission(
1507            Uri uri, int modeFlags, String message) {
1508        enforceForUri(
1509                modeFlags, checkCallingUriPermission(uri, modeFlags),
1510                false,
1511                Binder.getCallingUid(), uri, message);
1512    }
1513
1514    public void enforceCallingOrSelfUriPermission(
1515            Uri uri, int modeFlags, String message) {
1516        enforceForUri(
1517                modeFlags,
1518                checkCallingOrSelfUriPermission(uri, modeFlags), true,
1519                Binder.getCallingUid(), uri, message);
1520    }
1521
1522    public void enforceUriPermission(
1523            Uri uri, String readPermission, String writePermission,
1524            int pid, int uid, int modeFlags, String message) {
1525        enforceForUri(modeFlags,
1526                      checkUriPermission(
1527                              uri, readPermission, writePermission, pid, uid,
1528                              modeFlags),
1529                      false,
1530                      uid,
1531                      uri,
1532                      message);
1533    }
1534
1535    @Override
1536    public Context createPackageContext(String packageName, int flags)
1537        throws PackageManager.NameNotFoundException {
1538        if (packageName.equals("system") || packageName.equals("android")) {
1539            final ContextImpl context = new ContextImpl(mMainThread.getSystemContext());
1540            context.mBasePackageName = mBasePackageName;
1541            return context;
1542        }
1543
1544        LoadedApk pi =
1545            mMainThread.getPackageInfo(packageName, mResources.getCompatibilityInfo(), flags);
1546        if (pi != null) {
1547            ContextImpl c = new ContextImpl();
1548            c.mRestricted = (flags & CONTEXT_RESTRICTED) == CONTEXT_RESTRICTED;
1549            c.init(pi, null, mMainThread, mResources, mBasePackageName);
1550            if (c.mResources != null) {
1551                return c;
1552            }
1553        }
1554
1555        // Should be a better exception.
1556        throw new PackageManager.NameNotFoundException(
1557            "Application package " + packageName + " not found");
1558    }
1559
1560    @Override
1561    public boolean isRestricted() {
1562        return mRestricted;
1563    }
1564
1565    private File getDataDirFile() {
1566        if (mPackageInfo != null) {
1567            return mPackageInfo.getDataDirFile();
1568        }
1569        throw new RuntimeException("Not supported in system context");
1570    }
1571
1572    @Override
1573    public File getDir(String name, int mode) {
1574        name = "app_" + name;
1575        File file = makeFilename(getDataDirFile(), name);
1576        if (!file.exists()) {
1577            file.mkdir();
1578            setFilePermissionsFromMode(file.getPath(), mode,
1579                    FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
1580        }
1581        return file;
1582    }
1583
1584    static ContextImpl createSystemContext(ActivityThread mainThread) {
1585        ContextImpl context = new ContextImpl();
1586        context.init(Resources.getSystem(), mainThread);
1587        return context;
1588    }
1589
1590    ContextImpl() {
1591        mOuterContext = this;
1592    }
1593
1594    /**
1595     * Create a new ApplicationContext from an existing one.  The new one
1596     * works and operates the same as the one it is copying.
1597     *
1598     * @param context Existing application context.
1599     */
1600    public ContextImpl(ContextImpl context) {
1601        mPackageInfo = context.mPackageInfo;
1602        mBasePackageName = context.mBasePackageName;
1603        mResources = context.mResources;
1604        mMainThread = context.mMainThread;
1605        mContentResolver = context.mContentResolver;
1606        mOuterContext = this;
1607    }
1608
1609    final void init(LoadedApk packageInfo,
1610            IBinder activityToken, ActivityThread mainThread) {
1611        init(packageInfo, activityToken, mainThread, null, null);
1612    }
1613
1614    final void init(LoadedApk packageInfo,
1615                IBinder activityToken, ActivityThread mainThread,
1616                Resources container, String basePackageName) {
1617        mPackageInfo = packageInfo;
1618        mBasePackageName = basePackageName != null ? basePackageName : packageInfo.mPackageName;
1619        mResources = mPackageInfo.getResources(mainThread);
1620
1621        if (mResources != null && container != null
1622                && container.getCompatibilityInfo().applicationScale !=
1623                        mResources.getCompatibilityInfo().applicationScale) {
1624            if (DEBUG) {
1625                Log.d(TAG, "loaded context has different scaling. Using container's" +
1626                        " compatiblity info:" + container.getDisplayMetrics());
1627            }
1628            mResources = mainThread.getTopLevelResources(
1629                    mPackageInfo.getResDir(), container.getCompatibilityInfo());
1630        }
1631        mMainThread = mainThread;
1632        mContentResolver = new ApplicationContentResolver(this, mainThread);
1633
1634        setActivityToken(activityToken);
1635    }
1636
1637    final void init(Resources resources, ActivityThread mainThread) {
1638        mPackageInfo = null;
1639        mBasePackageName = null;
1640        mResources = resources;
1641        mMainThread = mainThread;
1642        mContentResolver = new ApplicationContentResolver(this, mainThread);
1643    }
1644
1645    final void scheduleFinalCleanup(String who, String what) {
1646        mMainThread.scheduleContextCleanup(this, who, what);
1647    }
1648
1649    final void performFinalCleanup(String who, String what) {
1650        //Log.i(TAG, "Cleanup up context: " + this);
1651        mPackageInfo.removeContextRegistrations(getOuterContext(), who, what);
1652    }
1653
1654    final Context getReceiverRestrictedContext() {
1655        if (mReceiverRestrictedContext != null) {
1656            return mReceiverRestrictedContext;
1657        }
1658        return mReceiverRestrictedContext = new ReceiverRestrictedContext(getOuterContext());
1659    }
1660
1661    final void setActivityToken(IBinder token) {
1662        mActivityToken = token;
1663    }
1664
1665    final void setOuterContext(Context context) {
1666        mOuterContext = context;
1667    }
1668
1669    final Context getOuterContext() {
1670        return mOuterContext;
1671    }
1672
1673    final IBinder getActivityToken() {
1674        return mActivityToken;
1675    }
1676
1677    static void setFilePermissionsFromMode(String name, int mode,
1678            int extraPermissions) {
1679        int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR
1680            |FileUtils.S_IRGRP|FileUtils.S_IWGRP
1681            |extraPermissions;
1682        if ((mode&MODE_WORLD_READABLE) != 0) {
1683            perms |= FileUtils.S_IROTH;
1684        }
1685        if ((mode&MODE_WORLD_WRITEABLE) != 0) {
1686            perms |= FileUtils.S_IWOTH;
1687        }
1688        if (DEBUG) {
1689            Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode)
1690                  + ", perms=0x" + Integer.toHexString(perms));
1691        }
1692        FileUtils.setPermissions(name, perms, -1, -1);
1693    }
1694
1695    private File validateFilePath(String name, boolean createDirectory) {
1696        File dir;
1697        File f;
1698
1699        if (name.charAt(0) == File.separatorChar) {
1700            String dirPath = name.substring(0, name.lastIndexOf(File.separatorChar));
1701            dir = new File(dirPath);
1702            name = name.substring(name.lastIndexOf(File.separatorChar));
1703            f = new File(dir, name);
1704        } else {
1705            dir = getDatabasesDir();
1706            f = makeFilename(dir, name);
1707        }
1708
1709        if (createDirectory && !dir.isDirectory() && dir.mkdir()) {
1710            FileUtils.setPermissions(dir.getPath(),
1711                FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1712                -1, -1);
1713        }
1714
1715        return f;
1716    }
1717
1718    private File makeFilename(File base, String name) {
1719        if (name.indexOf(File.separatorChar) < 0) {
1720            return new File(base, name);
1721        }
1722        throw new IllegalArgumentException(
1723                "File " + name + " contains a path separator");
1724    }
1725
1726    // ----------------------------------------------------------------------
1727    // ----------------------------------------------------------------------
1728    // ----------------------------------------------------------------------
1729
1730    private static final class ApplicationContentResolver extends ContentResolver {
1731        public ApplicationContentResolver(Context context, ActivityThread mainThread) {
1732            super(context);
1733            mMainThread = mainThread;
1734        }
1735
1736        @Override
1737        protected IContentProvider acquireProvider(Context context, String name) {
1738            return mMainThread.acquireProvider(context, name, true);
1739        }
1740
1741        @Override
1742        protected IContentProvider acquireExistingProvider(Context context, String name) {
1743            return mMainThread.acquireExistingProvider(context, name, true);
1744        }
1745
1746        @Override
1747        public boolean releaseProvider(IContentProvider provider) {
1748            return mMainThread.releaseProvider(provider, true);
1749        }
1750
1751        @Override
1752        protected IContentProvider acquireUnstableProvider(Context c, String name) {
1753            return mMainThread.acquireProvider(c, name, false);
1754        }
1755
1756        @Override
1757        public boolean releaseUnstableProvider(IContentProvider icp) {
1758            return mMainThread.releaseProvider(icp, false);
1759        }
1760
1761        @Override
1762        public void unstableProviderDied(IContentProvider icp) {
1763            mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
1764        }
1765
1766        private final ActivityThread mMainThread;
1767    }
1768}
1769