blob: 3812429b5f054b756a772b347df2c1620b6ff317 [file] [log] [blame]
Adrian Roose381c162016-02-11 15:26:42 -08001/*
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
17package com.android.systemui.statusbar.phone;
18
19import android.app.ActivityManager;
20import android.app.IWallpaperManager;
21import android.app.IWallpaperManagerCallback;
22import android.app.WallpaperManager;
23import android.content.Context;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.os.Bundle;
27import android.os.Handler;
28import android.os.ParcelFileDescriptor;
29import android.os.RemoteException;
30import android.os.ServiceManager;
Vadim Tryshev12a30e82016-02-12 15:39:28 -080031import android.os.UserHandle;
Adrian Roose381c162016-02-11 15:26:42 -080032import android.util.Log;
33
34import libcore.io.IoUtils;
35
Vadim Tryshev12a30e82016-02-12 15:39:28 -080036import java.util.Objects;
37
Adrian Roose381c162016-02-11 15:26:42 -080038/**
39 * Manages the lockscreen wallpaper.
40 */
41public 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 Tryshev12a30e82016-02-12 15:39:28 -080048 private final WallpaperManager mWallpaperManager;
Adrian Roose381c162016-02-11 15:26:42 -080049 private final Handler mH;
50
51 private boolean mCached;
52 private Bitmap mCache;
Vadim Tryshev12a30e82016-02-12 15:39:28 -080053 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 Roose381c162016-02-11 15:26:42 -080057
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 Tryshev12a30e82016-02-12 15:39:28 -080064 mWallpaperManager = (WallpaperManager) ctx.getSystemService(Context.WALLPAPER_SERVICE);
65 mCurrentUserId = ActivityManager.getCurrentUser();
Adrian Roose381c162016-02-11 15:26:42 -080066
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 Tryshev12a30e82016-02-12 15:39:28 -080084 // 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;
Christopher Tateedf7d042016-03-29 18:24:25 -070088 ParcelFileDescriptor fd = mService.getWallpaper(null, WallpaperManager.FLAG_LOCK,
Vadim Tryshev12a30e82016-02-12 15:39:28 -080089 new Bundle(), lockWallpaperUserId);
Adrian Roose381c162016-02-11 15:26:42 -080090 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 Tryshev12a30e82016-02-12 15:39:28 -0800105 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 Tryshev12a30e82016-02-12 15:39:28 -0800108 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 Roose381c162016-02-11 15:26:42 -0800115 }
116 } catch (RemoteException e) {
117 Log.e(TAG, "System dead?" + e);
118 return null;
119 }
120 }
121
Vadim Tryshev12a30e82016-02-12 15:39:28 -0800122 public void setCurrentUser(int user) {
123 if (user != mCurrentUserId) {
Adrian Roose381c162016-02-11 15:26:42 -0800124 mCached = false;
Vadim Tryshev12a30e82016-02-12 15:39:28 -0800125 mCurrentUserId = user;
Adrian Roose381c162016-02-11 15:26:42 -0800126 }
127 }
128
Vadim Tryshev12a30e82016-02-12 15:39:28 -0800129 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 Roose381c162016-02-11 15:26:42 -0800139 @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}