Document which sys services can be accessed by instant apps

This change documents that some system services are not available
to instant apps due to the restricted security sandbox they run in.
Also ensure that if target SDK is P WallpaperManager is null if the
backing system service cannot be looked up to be consistent with
the way other system services behave.

Test: Manual

Fixes: 70984822
Fixes: 69848394

Change-Id: I9aae884d5840c92136c7d713fac87e7732dc2642
diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java
index 495fd3c..66cf991 100644
--- a/core/java/android/app/SystemServiceRegistry.java
+++ b/core/java/android/app/SystemServiceRegistry.java
@@ -552,8 +552,16 @@
         registerService(Context.WALLPAPER_SERVICE, WallpaperManager.class,
                 new CachedServiceFetcher<WallpaperManager>() {
             @Override
-            public WallpaperManager createService(ContextImpl ctx) {
-                return new WallpaperManager(ctx.getOuterContext(),
+            public WallpaperManager createService(ContextImpl ctx)
+                    throws ServiceNotFoundException {
+                final IBinder b;
+                if (ctx.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
+                    b = ServiceManager.getServiceOrThrow(Context.WALLPAPER_SERVICE);
+                } else {
+                    b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
+                }
+                IWallpaperManager service = IWallpaperManager.Stub.asInterface(b);
+                return new WallpaperManager(service, ctx.getOuterContext(),
                         ctx.mMainThread.getHandler());
             }});
 
diff --git a/core/java/android/app/WallpaperManager.java b/core/java/android/app/WallpaperManager.java
index 3829afb..f21746c 100644
--- a/core/java/android/app/WallpaperManager.java
+++ b/core/java/android/app/WallpaperManager.java
@@ -286,9 +286,8 @@
         private Bitmap mDefaultWallpaper;
         private Handler mMainLooperHandler;
 
-        Globals(Looper looper) {
-            IBinder b = ServiceManager.getService(Context.WALLPAPER_SERVICE);
-            mService = IWallpaperManager.Stub.asInterface(b);
+        Globals(IWallpaperManager service, Looper looper) {
+            mService = service;
             mMainLooperHandler = new Handler(looper);
             forgetLoadedWallpaper();
         }
@@ -497,17 +496,17 @@
     private static final Object sSync = new Object[0];
     private static Globals sGlobals;
 
-    static void initGlobals(Looper looper) {
+    static void initGlobals(IWallpaperManager service, Looper looper) {
         synchronized (sSync) {
             if (sGlobals == null) {
-                sGlobals = new Globals(looper);
+                sGlobals = new Globals(service, looper);
             }
         }
     }
 
-    /*package*/ WallpaperManager(Context context, Handler handler) {
+    /*package*/ WallpaperManager(IWallpaperManager service, Context context, Handler handler) {
         mContext = context;
-        initGlobals(context.getMainLooper());
+        initGlobals(service, context.getMainLooper());
     }
 
     /**
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index bd8103a..74ba60d 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -3100,6 +3100,14 @@
      * service objects between various different contexts (Activities, Applications,
      * Services, Providers, etc.)
      *
+     * <p>Note: Instant apps, for which {@link PackageManager#isInstantApp()} returns true,
+     * don't have access to the following system services: {@link #DEVICE_POLICY_SERVICE},
+     * {@link #FINGERPRINT_SERVICE}, {@link #SHORTCUT_SERVICE}, {@link #USB_SERVICE},
+     * {@link #WALLPAPER_SERVICE}, {@link #WIFI_P2P_SERVICE}, {@link #WIFI_SERVICE},
+     * {@link #WIFI_AWARE_SERVICE}. For these services this method will return <code>null</code>.
+     * Generally, if you are running as an instant app you should always check whether the result
+     * of this method is null.
+     *
      * @param name The name of the desired service.
      *
      * @return The service or null if the name does not exist.
@@ -3183,6 +3191,14 @@
      * Services, Providers, etc.)
      * </p>
      *
+     * <p>Note: Instant apps, for which {@link PackageManager#isInstantApp()} returns true,
+     * don't have access to the following system services: {@link #DEVICE_POLICY_SERVICE},
+     * {@link #FINGERPRINT_SERVICE}, {@link #SHORTCUT_SERVICE}, {@link #USB_SERVICE},
+     * {@link #WALLPAPER_SERVICE}, {@link #WIFI_P2P_SERVICE}, {@link #WIFI_SERVICE},
+     * {@link #WIFI_AWARE_SERVICE}. For these services this method will return <code>null</code>.
+     * Generally, if you are running as an instant app you should always check whether the result
+     * of this method is null.
+     *
      * @param serviceClass The class of the desired service.
      * @return The service or null if the class is not a supported system service.
      */
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index c6deecc..c57eba2 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -11133,6 +11133,7 @@
             INSTANT_APP_SETTINGS.add(DEBUG_VIEW_ATTRIBUTES);
             INSTANT_APP_SETTINGS.add(WTF_IS_FATAL);
             INSTANT_APP_SETTINGS.add(SEND_ACTION_APP_ERROR);
+            INSTANT_APP_SETTINGS.add(ZEN_MODE);
         }
 
         /**