blob: 0c16572fa891f11f4fa303716735b8b022a58d25 [file] [log] [blame]
Jon Miranda16ea1b12017-12-12 14:52:48 -08001/*
2 * Copyright (C) 2017 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 */
16package com.android.wallpaper.backup;
17
18import android.annotation.SuppressLint;
19import android.app.WallpaperManager;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
Colin Cross1b7f2082021-05-21 11:15:20 -070023import android.os.Build;
Jon Miranda16ea1b12017-12-12 14:52:48 -080024
Jon Miranda16ea1b12017-12-12 14:52:48 -080025import com.android.wallpaper.module.Injector;
26import com.android.wallpaper.module.InjectorProvider;
27import com.android.wallpaper.module.WallpaperPreferences;
28
29/**
30 * Generates hash codes for currently set static image wallpapers on N+ devices where they are
31 * missing because older versions of the app did not generate and set them.
32 * <p>
33 * Static image wallpaper hash codes are necessary on N+ devices for the purposes of backup &
34 * restore because N+ WallpaperManager integer IDs are local to physical devices and not backed up
35 * and restored on the framework side.
36 */
37@SuppressLint("ServiceCast")
38public class MissingHashCodeGenerator extends BroadcastReceiver {
39
40 @Override
41 public void onReceive(Context context, Intent intent) {
42 // This receiver is a no-op on pre-N Android and should only respond to a MY_PACKAGE_REPLACED
43 // intent.
44 if (!intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)
Colin Cross1b7f2082021-05-21 11:15:20 -070045 || Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
Jon Miranda16ea1b12017-12-12 14:52:48 -080046 return;
47 }
48
49 WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
50
51 // Make this receiver a no-op if running in the context of profile where wallpapers are not
52 // supported.
53 if (!wallpaperManager.isWallpaperSupported()) {
54 return;
55 }
56
57 Injector injector = InjectorProvider.getInjector();
58 WallpaperPreferences wallpaperPreferences = injector.getPreferences(context);
59 // Delegate the longer-running work of generating missing hash codes to a JobScheduler job if
60 // there's no hash codes saved.
61 if (wallpaperPreferences.getHomeWallpaperHashCode() != 0
62 && wallpaperPreferences.getLockWallpaperHashCode() != 0) {
63 return;
64 }
65
66 MissingHashCodeGeneratorJobService.schedule(context);
67 }
68}