Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.wallpaper.backup; |
| 17 | |
| 18 | import android.annotation.SuppressLint; |
| 19 | import android.app.WallpaperManager; |
| 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Colin Cross | 1b7f208 | 2021-05-21 11:15:20 -0700 | [diff] [blame^] | 23 | import android.os.Build; |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 24 | |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 25 | import com.android.wallpaper.module.Injector; |
| 26 | import com.android.wallpaper.module.InjectorProvider; |
| 27 | import 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") |
| 38 | public 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 Cross | 1b7f208 | 2021-05-21 11:15:20 -0700 | [diff] [blame^] | 45 | || Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { |
Jon Miranda | 16ea1b1 | 2017-12-12 14:52:48 -0800 | [diff] [blame] | 46 | 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 | } |