From e409f0e46243e943af2a223c00bc7098dc7e5c88 Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Mon, 21 Mar 2016 14:53:15 -0700 Subject: [PATCH] Add API to read the current wallpaper's ID Also regularize cross-user call handling throughout. Bug 27743435 Change-Id: Ibc530d0576d657723a613888357a6ea71b482763 --- api/current.txt | 1 + api/system-current.txt | 1 + api/test-current.txt | 1 + core/java/android/app/IWallpaperManager.aidl | 5 +++ core/java/android/app/WallpaperManager.java | 32 ++++++++++++++++ .../wallpaper/WallpaperManagerService.java | 38 +++++++++++++------ 6 files changed, 66 insertions(+), 12 deletions(-) diff --git a/api/current.txt b/api/current.txt index 936ca882ce149..eb7666afe595c 100644 --- a/api/current.txt +++ b/api/current.txt @@ -5723,6 +5723,7 @@ package android.app { method public android.graphics.drawable.Drawable getFastDrawable(); method public static android.app.WallpaperManager getInstance(android.content.Context); method public android.os.ParcelFileDescriptor getWallpaperFile(int); + method public int getWallpaperId(int); method public android.app.WallpaperInfo getWallpaperInfo(); method public boolean hasResourceWallpaper(int); method public boolean isWallpaperSettingAllowed(); diff --git a/api/system-current.txt b/api/system-current.txt index f8863e53d8ba6..babd335e51809 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -5857,6 +5857,7 @@ package android.app { method public android.graphics.drawable.Drawable getFastDrawable(); method public static android.app.WallpaperManager getInstance(android.content.Context); method public android.os.ParcelFileDescriptor getWallpaperFile(int); + method public int getWallpaperId(int); method public android.app.WallpaperInfo getWallpaperInfo(); method public boolean hasResourceWallpaper(int); method public boolean isWallpaperSettingAllowed(); diff --git a/api/test-current.txt b/api/test-current.txt index 6327dc2200d1e..5c471b33e8b72 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -5727,6 +5727,7 @@ package android.app { method public android.graphics.drawable.Drawable getFastDrawable(); method public static android.app.WallpaperManager getInstance(android.content.Context); method public android.os.ParcelFileDescriptor getWallpaperFile(int); + method public int getWallpaperId(int); method public android.app.WallpaperInfo getWallpaperInfo(); method public boolean hasResourceWallpaper(int); method public boolean isWallpaperSettingAllowed(); diff --git a/core/java/android/app/IWallpaperManager.aidl b/core/java/android/app/IWallpaperManager.aidl index 1143c6aec7a6b..2fc6533f2cd08 100644 --- a/core/java/android/app/IWallpaperManager.aidl +++ b/core/java/android/app/IWallpaperManager.aidl @@ -61,6 +61,11 @@ interface IWallpaperManager { ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, int which, out Bundle outParams, int userId); + /** + * Retrieve the given user's current wallpaper ID of the given kind. + */ + int getWallpaperIdForUser(int which, int userId); + /** * If the current system wallpaper is a live wallpaper component, return the * information about that wallpaper. Otherwise, if it is a static image, diff --git a/core/java/android/app/WallpaperManager.java b/core/java/android/app/WallpaperManager.java index 52e52728c25a8..b52af2762fed7 100644 --- a/core/java/android/app/WallpaperManager.java +++ b/core/java/android/app/WallpaperManager.java @@ -725,6 +725,38 @@ public class WallpaperManager { } } + /** + * Get the ID of the current wallpaper of the given kind. If there is no + * such wallpaper configured, returns a negative number. + * + * @param which The wallpaper whose ID is to be returned. Must be a single + * defined kind of wallpaper, either {@link #FLAG_SET_SYSTEM} or + * {@link #FLAG_SET_LOCK}. + * @return The positive numeric ID of the current wallpaper of the given kind, + * or a negative value if no such wallpaper is configured. + */ + public int getWallpaperId(int which) { + return getWallpaperIdForUser(which, mContext.getUserId()); + } + + /** + * Get the ID of the given user's current wallpaper of the given kind. If there + * is no such wallpaper configured, returns a negative number. + * @hide + */ + public int getWallpaperIdForUser(int which, int userId) { + try { + if (sGlobals.mService == null) { + Log.w(TAG, "WallpaperService not running"); + return -1; + } else { + return sGlobals.mService.getWallpaperIdForUser(which, userId); + } + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + } + /** * Gets an Intent that will launch an activity that crops the given * image and sets the device's wallpaper. If there is a default HOME activity diff --git a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java index 3cf95900edde6..fb9b1cec862a2 100644 --- a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java +++ b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java @@ -20,6 +20,7 @@ import static android.app.WallpaperManager.FLAG_SET_SYSTEM; import static android.app.WallpaperManager.FLAG_SET_LOCK; import static android.os.ParcelFileDescriptor.*; +import android.app.ActivityManager; import android.app.ActivityManagerNative; import android.app.AppGlobals; import android.app.AppOpsManager; @@ -875,12 +876,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub { if (!isWallpaperSupported(callingPackage) || !isWallpaperSettingAllowed(callingPackage)) { return; } - if (userId != UserHandle.getCallingUserId()) { - // cross-user call - mContext.enforceCallingOrSelfPermission( - android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, - "WallpaperManagerService"); - } + userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), + Binder.getCallingUid(), userId, false, true, "clearWallpaper", null); synchronized (mLock) { clearWallpaperLocked(false, which, userId, null); @@ -1103,12 +1100,8 @@ public class WallpaperManagerService extends IWallpaperManager.Stub { @Override public ParcelFileDescriptor getWallpaper(IWallpaperManagerCallback cb, final int which, Bundle outParams, int wallpaperUserId) { - if (wallpaperUserId != UserHandle.getCallingUserId()) { - // cross-user call - mContext.enforceCallingOrSelfPermission( - android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, - "WallpaperManagerService"); - } + wallpaperUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), + Binder.getCallingUid(), wallpaperUserId, false, true, "getWallpaper", null); if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { throw new IllegalArgumentException("Must specify exactly one kind of wallpaper to read"); @@ -1147,6 +1140,7 @@ public class WallpaperManagerService extends IWallpaperManager.Stub { } } + @Override public WallpaperInfo getWallpaperInfo() { int userId = UserHandle.getCallingUserId(); synchronized (mLock) { @@ -1158,6 +1152,26 @@ public class WallpaperManagerService extends IWallpaperManager.Stub { } } + @Override + public int getWallpaperIdForUser(int which, int userId) { + userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), + Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null); + + if (which != FLAG_SET_SYSTEM && which != FLAG_SET_LOCK) { + throw new IllegalArgumentException("Must specify exactly one kind of wallpaper"); + } + + final SparseArray map = + (which == FLAG_SET_LOCK) ? mLockWallpaperMap : mWallpaperMap; + synchronized (mLock) { + WallpaperData wallpaper = map.get(userId); + if (wallpaper != null) { + return wallpaper.wallpaperId; + } + } + return -1; + } + @Override public boolean setLockWallpaperCallback(IWallpaperManagerCallback cb) { checkPermission(android.Manifest.permission.INTERNAL_SYSTEM_WINDOW);