• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.module;
17 
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapFactory;
23 
24 import com.android.wallpaper.compat.BuildCompat;
25 import com.android.wallpaper.util.DiskBasedLogger;
26 import com.android.wallpaper.util.FileMover;
27 
28 import java.io.File;
29 
30 /**
31  * Receiver to run when the app was updated or on first boot to switch from live rotating wallpaper
32  * to static rotating wallpaper.
33  */
34 public class RotationWallpaperUpdateReceiver extends BroadcastReceiver {
35 
36 
37     // Earlier versions of rotating wallpaper save the current rotation image as a file.
38     // We can infer from the extistance of this file whether or not user had rotating live wallpaper
39     private static final String ROTATING_WALLPAPER_FILE_PATH = "rotating_wallpaper.jpg";
40     private static final String TAG = "RotationWallpaperUpdateReceiver";
41 
42     @Override
onReceive(Context context, Intent intent)43     public void onReceive(Context context, Intent intent) {
44         // This receiver is a no-op on pre-N Android and should only respond to a
45         // MY_PACKAGE_REPLACED intent.
46         if (intent.getAction() == null
47                 || !(intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)
48                 || intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
49                 || !BuildCompat.isAtLeastN()) {
50             DiskBasedLogger.e(
51                     TAG,
52                     "Unexpected action or Android version!",
53                     context);
54             throw new IllegalStateException(
55                     "Unexpected broadcast action or unsupported Android version");
56         }
57 
58         PendingResult broadcastResult = goAsync();
59         new Thread(() -> {
60             Context appContext = context.getApplicationContext();
61             Context deviceProtectedContext = appContext.createDeviceProtectedStorageContext();
62 
63             if (context.getFileStreamPath(ROTATING_WALLPAPER_FILE_PATH).exists()) {
64                 moveFileToProtectedStorage(appContext, deviceProtectedContext);
65             }
66 
67             File wallpaperFile = deviceProtectedContext.getFileStreamPath(
68                     ROTATING_WALLPAPER_FILE_PATH);
69             if (wallpaperFile.exists()) {
70                 switchToStaticWallpaper(appContext, wallpaperFile);
71             }
72             broadcastResult.finish();
73         }).start();
74     }
75 
moveFileToProtectedStorage(Context context, Context deviceProtectedContext)76     private void moveFileToProtectedStorage(Context context, Context deviceProtectedContext) {
77         try {
78             FileMover.moveFileBetweenContexts(context, ROTATING_WALLPAPER_FILE_PATH,
79                     deviceProtectedContext, ROTATING_WALLPAPER_FILE_PATH);
80         } catch (Exception ex) {
81             DiskBasedLogger.e(
82                     TAG,
83                     "Failed to move rotating wallpaper file to device protected storage: "
84                             + ex.getMessage(),
85                     context);
86         }
87     }
88 
switchToStaticWallpaper(Context appContext, File wallpaperFile)89     private void switchToStaticWallpaper(Context appContext, File wallpaperFile) {
90         try {
91             Injector injector = InjectorProvider.getInjector();
92             WallpaperPreferences wallpaperPreferences = injector.getPreferences(appContext);
93             if (wallpaperPreferences.getWallpaperPresentationMode()
94                     != WallpaperPreferences.PRESENTATION_MODE_ROTATING) {
95                 return;
96             }
97             Bitmap bitmap = BitmapFactory.decodeFile(wallpaperFile.getAbsolutePath());
98 
99             injector.getWallpaperPersister(appContext).setWallpaperInRotation(bitmap,
100                     wallpaperPreferences.getHomeWallpaperAttributions(),
101                     wallpaperPreferences.getHomeWallpaperActionLabelRes(),
102                     wallpaperPreferences.getHomeWallpaperActionIconRes(),
103                     wallpaperPreferences.getHomeWallpaperActionUrl(),
104                     wallpaperPreferences.getHomeWallpaperCollectionId());
105             wallpaperFile.delete();
106 
107         } catch (Exception ex) {
108             DiskBasedLogger.e(TAG, "Unable to set static wallpaper", appContext);
109         }
110     }
111 }
112