summaryrefslogtreecommitdiffstats
path: root/src/com/android/wallpaper/module/DefaultWallpaperRefresher.java
blob: d85e04efbb84063125de3095e649d87dd4f006cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.wallpaper.module;

import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import com.android.wallpaper.R;
import com.android.wallpaper.asset.BitmapUtils;
import com.android.wallpaper.compat.BuildCompat;
import com.android.wallpaper.compat.WallpaperManagerCompat;
import com.android.wallpaper.model.WallpaperMetadata;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Default implementation of {@link WallpaperRefresher} which refreshes wallpaper metadata
 * asynchronously.
 */
@SuppressLint("ServiceCast")
public class DefaultWallpaperRefresher implements WallpaperRefresher {
    private static final String TAG = "DefaultWPRefresher";

    private final Context mAppContext;
    private final WallpaperPreferences mWallpaperPreferences;
    private final WallpaperManager mWallpaperManager;

    /**
     * @param context The application's context.
     */
    public DefaultWallpaperRefresher(Context context) {
        mAppContext = context.getApplicationContext();

        Injector injector = InjectorProvider.getInjector();
        mWallpaperPreferences = injector.getPreferences(mAppContext);

        // Retrieve WallpaperManager using Context#getSystemService instead of
        // WallpaperManager#getInstance so it can be mocked out in test.
        mWallpaperManager = (WallpaperManager) context.getSystemService(Context.WALLPAPER_SERVICE);
    }

    @Override
    public void refresh(RefreshListener listener) {
        GetWallpaperMetadataAsyncTask task = new GetWallpaperMetadataAsyncTask(listener);
        task.execute();
    }

    /**
     * Retrieves the current wallpaper's thumbnail and metadata off the UI thread.
     */
    private class GetWallpaperMetadataAsyncTask extends
            AsyncTask<Void, Void, List<WallpaperMetadata>> {
        private final RefreshListener mListener;
        private final WallpaperManagerCompat mWallpaperManagerCompat;

        private long mCurrentHomeWallpaperHashCode;
        private long mCurrentLockWallpaperHashCode;
        private String mSystemWallpaperPackageName;

        @SuppressLint("ServiceCast")
        public GetWallpaperMetadataAsyncTask(RefreshListener listener) {
            mListener = listener;
            mWallpaperManagerCompat =
                    InjectorProvider.getInjector().getWallpaperManagerCompat(mAppContext);
        }

        @Override
        protected List<WallpaperMetadata> doInBackground(Void... unused) {
            List<WallpaperMetadata> wallpaperMetadatas = new ArrayList<>();

            if (!isHomeScreenMetadataCurrent() || isHomeScreenAttributionsEmpty()) {
                mWallpaperPreferences.clearHomeWallpaperMetadata();
                setFallbackHomeScreenWallpaperMetadata();
            }

            boolean isLockScreenWallpaperCurrentlySet = LockWallpaperStatusChecker
                    .isLockWallpaperSet(mAppContext);

            if (!BuildCompat.isAtLeastN() || !isLockScreenWallpaperCurrentlySet) {
                // Return only home metadata if pre-N device or lock screen wallpaper is not explicitly set.
                wallpaperMetadatas.add(new WallpaperMetadata(
                        mWallpaperPreferences.getHomeWallpaperAttributions(),
                        mWallpaperPreferences.getHomeWallpaperActionUrl(),
                        mWallpaperPreferences.getHomeWallpaperActionLabelRes(),
                        mWallpaperPreferences.getHomeWallpaperActionIconRes(),
                        mWallpaperPreferences.getHomeWallpaperCollectionId(),
                        mWallpaperPreferences.getHomeWallpaperBackingFileName(),
                        mWallpaperManager.getWallpaperInfo()));
                return wallpaperMetadatas;
            }

            if (!isLockScreenMetadataCurrent() || isLockScreenAttributionsEmpty()) {
                mWallpaperPreferences.clearLockWallpaperMetadata();
                setFallbackLockScreenWallpaperMetadata();
            }

            wallpaperMetadatas.add(new WallpaperMetadata(
                    mWallpaperPreferences.getHomeWallpaperAttributions(),
                    mWallpaperPreferences.getHomeWallpaperActionUrl(),
                    mWallpaperPreferences.getHomeWallpaperActionLabelRes(),
                    mWallpaperPreferences.getHomeWallpaperActionIconRes(),
                    mWallpaperPreferences.getHomeWallpaperCollectionId(),
                    mWallpaperPreferences.getHomeWallpaperBackingFileName(),
                    mWallpaperManager.getWallpaperInfo()));

            wallpaperMetadatas.add(new WallpaperMetadata(
                    mWallpaperPreferences.getLockWallpaperAttributions(),
                    mWallpaperPreferences.getLockWallpaperActionUrl(),
                    mWallpaperPreferences.getLockWallpaperActionLabelRes(),
                    mWallpaperPreferences.getLockWallpaperActionIconRes(),
                    mWallpaperPreferences.getLockWallpaperCollectionId(),
                    mWallpaperPreferences.getLockWallpaperBackingFileName(),
                    null /* wallpaperComponent */));

            return wallpaperMetadatas;
        }

        @Override
        protected void onPostExecute(List<WallpaperMetadata> metadatas) {
            if (metadatas.size() > 2) {
                Log.e(TAG, "Got more than 2 WallpaperMetadata objects - only home and (optionally) lock "
                        + "are permitted.");
                return;
            }

            mListener.onRefreshed(metadatas.get(0), metadatas.size() > 1 ? metadatas.get(1) : null,
                    mWallpaperPreferences.getWallpaperPresentationMode());
        }

        /**
         * Sets fallback wallpaper attributions to WallpaperPreferences when the saved metadata did not
         * match the system wallpaper. For live wallpapers, loads the label (title) but for image
         * wallpapers loads a generic title string.
         */
        private void setFallbackHomeScreenWallpaperMetadata() {
            android.app.WallpaperInfo wallpaperComponent = mWallpaperManager.getWallpaperInfo();
            if (wallpaperComponent == null) { // Image wallpaper
                mWallpaperPreferences.setHomeWallpaperAttributions(
                        Arrays.asList(mAppContext.getResources().getString(R.string.fallback_wallpaper_title)));

                // Set wallpaper ID if at least N or set a hash code if an earlier version of Android.
                if (BuildCompat.isAtLeastN()) {
                    mWallpaperPreferences.setHomeWallpaperManagerId(mWallpaperManagerCompat.getWallpaperId(
                            WallpaperManagerCompat.FLAG_SYSTEM));
                } else {
                    mWallpaperPreferences.setHomeWallpaperHashCode(getCurrentHomeWallpaperHashCode());
                }
            } else { // Live wallpaper
                mWallpaperPreferences.setHomeWallpaperAttributions(Arrays.asList(
                        wallpaperComponent.loadLabel(mAppContext.getPackageManager()).toString()));
                mWallpaperPreferences.setHomeWallpaperPackageName(mSystemWallpaperPackageName);
            }
            mWallpaperPreferences.setWallpaperPresentationMode(
                    WallpaperPreferences.PRESENTATION_MODE_STATIC);
        }

        /**
         * Sets fallback lock screen wallpaper attributions to WallpaperPreferences. This should be
         * called when the saved lock screen wallpaper metadata does not match the currently set lock
         * screen wallpaper.
         */
        private void setFallbackLockScreenWallpaperMetadata() {
            mWallpaperPreferences.setLockWallpaperAttributions(
                    Arrays.asList(mAppContext.getResources().getString(R.string.fallback_wallpaper_title)));
            mWallpaperPreferences.setLockWallpaperId(mWallpaperManagerCompat.getWallpaperId(
                    WallpaperManagerCompat.FLAG_LOCK));
        }

        /**
         * Returns whether the home screen metadata saved in WallpaperPreferences corresponds to the
         * current system wallpaper.
         */
        private boolean isHomeScreenMetadataCurrent() {
            return (mWallpaperManager.getWallpaperInfo() == null)
                    ? isHomeScreenImageWallpaperCurrent()
                    : isHomeScreenLiveWallpaperCurrent();
        }

        /**
         * Returns whether the home screen attributions saved in WallpaperPreferences is empty.
         */
        private boolean isHomeScreenAttributionsEmpty() {
            List<String> homeScreenAttributions = mWallpaperPreferences.getHomeWallpaperAttributions();
            return homeScreenAttributions.get(0) == null
                    && homeScreenAttributions.get(1) == null
                    && homeScreenAttributions.get(2) == null;
        }

        private long getCurrentHomeWallpaperHashCode() {
            if (mCurrentHomeWallpaperHashCode == 0) {
                    BitmapDrawable wallpaperDrawable = (BitmapDrawable) mWallpaperManagerCompat.getDrawable();
                    Bitmap wallpaperBitmap = wallpaperDrawable.getBitmap();
                    mCurrentHomeWallpaperHashCode = BitmapUtils.generateHashCode(wallpaperBitmap);

                    // Manually request that WallpaperManager loses its reference to the current wallpaper
                    // bitmap, which can occupy a large memory allocation for the lifetime of the app.
                    mWallpaperManager.forgetLoadedWallpaper();
            }
            return mCurrentHomeWallpaperHashCode;
        }

        private long getCurrentLockWallpaperHashCode() {
            if (mCurrentLockWallpaperHashCode == 0
                    && LockWallpaperStatusChecker.isLockWallpaperSet(mAppContext)) {
                Bitmap wallpaperBitmap = getLockWallpaperBitmap();
                mCurrentLockWallpaperHashCode = BitmapUtils.generateHashCode(wallpaperBitmap);
            }
            return mCurrentLockWallpaperHashCode;
        }

        /**
         * Returns the lock screen wallpaper currently set on the device as a Bitmap, or null if no
         * lock screen wallpaper is set.
         */
        private Bitmap getLockWallpaperBitmap() {
            Bitmap lockBitmap = null;

            ParcelFileDescriptor pfd = mWallpaperManagerCompat.getWallpaperFile(
                    WallpaperManagerCompat.FLAG_LOCK);
            // getWallpaperFile returns null if the lock screen isn't explicitly set, so need this
            // check.
            if (pfd != null) {
                InputStream fileStream = null;
                try {
                    fileStream = new FileInputStream(pfd.getFileDescriptor());
                    lockBitmap = BitmapFactory.decodeStream(fileStream);
                    pfd.close();
                    return lockBitmap;
                } catch (IOException e) {
                    Log.e(TAG, "IO exception when closing the file descriptor.");
                } finally {
                    if (fileStream != null) {
                        try {
                            fileStream.close();
                        } catch (IOException e) {
                            Log.e(TAG, "IO exception when closing input stream for lock screen WP.");
                        }
                    }
                }
            }

            return lockBitmap;
        }

        /**
         * Returns whether the image wallpaper set to the system matches the metadata in
         * WallpaperPreferences.
         */
        private boolean isHomeScreenImageWallpaperCurrent() {
            long savedBitmapHash = mWallpaperPreferences.getHomeWallpaperHashCode();

            // Use WallpaperManager IDs to check same-ness of image wallpaper on N+ versions of Android
            // only when there is no saved bitmap hash code (which could be leftover from a previous build
            // of the app that did not use wallpaper IDs).
            if (BuildCompat.isAtLeastN() && savedBitmapHash == 0) {
                return mWallpaperPreferences.getHomeWallpaperManagerId()
                        == mWallpaperManagerCompat.getWallpaperId(WallpaperManagerCompat.FLAG_SYSTEM);
            }

            return savedBitmapHash == getCurrentHomeWallpaperHashCode();
        }

        /**
         * Returns whether the live wallpaper set to the system's home screen matches the metadata in
         * WallpaperPreferences.
         */
        private boolean isHomeScreenLiveWallpaperCurrent() {
            mSystemWallpaperPackageName = mWallpaperManager.getWallpaperInfo().getPackageName();
            String homeWallpaperPackageName = mWallpaperPreferences.getHomeWallpaperPackageName();
            return mSystemWallpaperPackageName.equals(homeWallpaperPackageName);
        }

        /**
         * Returns whether the lock screen metadata saved in WallpaperPreferences corresponds to the
         * current lock screen wallpaper.
         */
        private boolean isLockScreenMetadataCurrent() {
            // Check for lock wallpaper image same-ness only when there is no stored lock wallpaper hash
            // code. Otherwise if there is a lock wallpaper hash code stored in
            // {@link WallpaperPreferences}, then check hash codes.
            long savedLockWallpaperHash = mWallpaperPreferences.getLockWallpaperHashCode();

            return (savedLockWallpaperHash == 0)
                    ? mWallpaperPreferences.getLockWallpaperId()
                    == mWallpaperManagerCompat.getWallpaperId(WallpaperManagerCompat.FLAG_LOCK)
                    : savedLockWallpaperHash == getCurrentLockWallpaperHashCode();
        }

        /**
         * Returns whether the lock screen attributions saved in WallpaperPreferences are empty.
         */
        private boolean isLockScreenAttributionsEmpty() {
            List<String> attributions = mWallpaperPreferences.getLockWallpaperAttributions();
            return attributions.get(0) == null
                    && attributions.get(1) == null
                    && attributions.get(2) == null;
        }
    }
}