Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package com.android.systemui.statusbar.phone; |
| 18 | |
| 19 | import android.app.ActivityManager; |
| 20 | import android.app.IWallpaperManager; |
| 21 | import android.app.IWallpaperManagerCallback; |
| 22 | import android.app.WallpaperManager; |
| 23 | import android.content.Context; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.Handler; |
| 28 | import android.os.ParcelFileDescriptor; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 31 | import android.os.UserHandle; |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | |
| 34 | import libcore.io.IoUtils; |
| 35 | |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 36 | import java.util.Objects; |
| 37 | |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Manages the lockscreen wallpaper. |
| 40 | */ |
| 41 | public class LockscreenWallpaper extends IWallpaperManagerCallback.Stub implements Runnable { |
| 42 | |
| 43 | private static final String TAG = "LockscreenWallpaper"; |
| 44 | |
| 45 | private final Context mContext; |
| 46 | private final PhoneStatusBar mBar; |
| 47 | private final IWallpaperManager mService; |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 48 | private final WallpaperManager mWallpaperManager; |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 49 | private final Handler mH; |
| 50 | |
| 51 | private boolean mCached; |
| 52 | private Bitmap mCache; |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 53 | private int mCurrentUserId; |
| 54 | // The user selected in the UI, or null if no user is selected or UI doesn't support selecting |
| 55 | // users. |
| 56 | private UserHandle mSelectedUser; |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 57 | |
| 58 | public LockscreenWallpaper(Context ctx, PhoneStatusBar bar, Handler h) { |
| 59 | mContext = ctx; |
| 60 | mBar = bar; |
| 61 | mH = h; |
| 62 | mService = IWallpaperManager.Stub.asInterface( |
| 63 | ServiceManager.getService(Context.WALLPAPER_SERVICE)); |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 64 | mWallpaperManager = (WallpaperManager) ctx.getSystemService(Context.WALLPAPER_SERVICE); |
| 65 | mCurrentUserId = ActivityManager.getCurrentUser(); |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 66 | |
| 67 | try { |
| 68 | mService.setLockWallpaperCallback(this); |
| 69 | } catch (RemoteException e) { |
| 70 | Log.e(TAG, "System dead?" + e); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public Bitmap getBitmap() { |
| 75 | try { |
| 76 | if (mCached) { |
| 77 | return mCache; |
| 78 | } |
| 79 | if (!mService.isWallpaperSupported(mContext.getOpPackageName())) { |
| 80 | mCached = true; |
| 81 | mCache = null; |
| 82 | return null; |
| 83 | } |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 84 | // Prefer the selected user (when specified) over the current user for the FLAG_SET_LOCK |
| 85 | // wallpaper. |
| 86 | final int lockWallpaperUserId = |
| 87 | mSelectedUser != null ? mSelectedUser.getIdentifier() : mCurrentUserId; |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 88 | ParcelFileDescriptor fd = mService.getWallpaper(null, WallpaperManager.FLAG_SET_LOCK, |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 89 | new Bundle(), lockWallpaperUserId); |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 90 | if (fd != null) { |
| 91 | try { |
| 92 | BitmapFactory.Options options = new BitmapFactory.Options(); |
| 93 | mCache = BitmapFactory.decodeFileDescriptor( |
| 94 | fd.getFileDescriptor(), null, options); |
| 95 | mCached = true; |
| 96 | return mCache; |
| 97 | } catch (OutOfMemoryError e) { |
| 98 | Log.w(TAG, "Can't decode file", e); |
| 99 | return null; |
| 100 | } finally { |
| 101 | IoUtils.closeQuietly(fd); |
| 102 | } |
| 103 | } else { |
| 104 | mCached = true; |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 105 | if (mSelectedUser != null && mSelectedUser.getIdentifier() != mCurrentUserId) { |
| 106 | // When selected user is different from the current user, show the selected |
| 107 | // user's static wallpaper. |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 108 | mCache = mWallpaperManager.getBitmapAsUser(mSelectedUser.getIdentifier()); |
| 109 | } else { |
| 110 | // When there is no selected user, or it's same as the current user, show the |
| 111 | // system (possibly dynamic) wallpaper for the selected user. |
| 112 | mCache = null; |
| 113 | } |
| 114 | return mCache; |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 115 | } |
| 116 | } catch (RemoteException e) { |
| 117 | Log.e(TAG, "System dead?" + e); |
| 118 | return null; |
| 119 | } |
| 120 | } |
| 121 | |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 122 | public void setCurrentUser(int user) { |
| 123 | if (user != mCurrentUserId) { |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 124 | mCached = false; |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 125 | mCurrentUserId = user; |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Vadim Tryshev | 12a30e8 | 2016-02-12 15:39:28 -0800 | [diff] [blame] | 129 | public void setSelectedUser(UserHandle selectedUser) { |
| 130 | if (Objects.equals(selectedUser, mSelectedUser)) { |
| 131 | return; |
| 132 | } |
| 133 | mSelectedUser = selectedUser; |
| 134 | |
| 135 | mH.removeCallbacks(this); |
| 136 | mH.post(this); |
| 137 | } |
| 138 | |
Adrian Roos | e381c16 | 2016-02-11 15:26:42 -0800 | [diff] [blame] | 139 | @Override |
| 140 | public void onWallpaperChanged() { |
| 141 | // Called on Binder thread. |
| 142 | mH.removeCallbacks(this); |
| 143 | mH.post(this); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void run() { |
| 148 | // Called in response to onWallpaperChanged on the main thread. |
| 149 | mCached = false; |
| 150 | mCache = null; |
| 151 | getBitmap(); |
| 152 | mBar.updateMediaMetaData(true /* metaDataChanged */, true /* allowEnterAnimation */); |
| 153 | } |
| 154 | } |