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