• 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.util.DiskBasedLogger;
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             DiskBasedLogger.e(
49                     TAG,
50                     "Unexpected action or Android version!",
51                     context);
52             throw new IllegalStateException(
53                     "Unexpected broadcast action or unsupported Android version");
54         }
55 
56         PendingResult broadcastResult = goAsync();
57         new Thread(() -> {
58             Context appContext = context.getApplicationContext();
59             Context deviceProtectedContext = appContext.createDeviceProtectedStorageContext();
60 
61             if (context.getFileStreamPath(ROTATING_WALLPAPER_FILE_PATH).exists()) {
62                 moveFileToProtectedStorage(appContext, deviceProtectedContext);
63             }
64 
65             File wallpaperFile = deviceProtectedContext.getFileStreamPath(
66                     ROTATING_WALLPAPER_FILE_PATH);
67             if (wallpaperFile.exists()) {
68                 switchToStaticWallpaper(appContext, wallpaperFile);
69             }
70             broadcastResult.finish();
71         }).start();
72     }
73 
moveFileToProtectedStorage(Context context, Context deviceProtectedContext)74     private void moveFileToProtectedStorage(Context context, Context deviceProtectedContext) {
75         try {
76             FileMover.moveFileBetweenContexts(context, ROTATING_WALLPAPER_FILE_PATH,
77                     deviceProtectedContext, ROTATING_WALLPAPER_FILE_PATH);
78         } catch (Exception ex) {
79             DiskBasedLogger.e(
80                     TAG,
81                     "Failed to move rotating wallpaper file to device protected storage: "
82                             + ex.getMessage(),
83                     context);
84         }
85     }
86 
switchToStaticWallpaper(Context appContext, File wallpaperFile)87     private void switchToStaticWallpaper(Context appContext, File wallpaperFile) {
88         try {
89             Injector injector = InjectorProvider.getInjector();
90             WallpaperPreferences wallpaperPreferences = injector.getPreferences(appContext);
91             if (wallpaperPreferences.getWallpaperPresentationMode()
92                     != WallpaperPreferences.PRESENTATION_MODE_ROTATING) {
93                 return;
94             }
95             Bitmap bitmap = BitmapFactory.decodeFile(wallpaperFile.getAbsolutePath());
96 
97             injector.getWallpaperPersister(appContext).setWallpaperInRotation(bitmap,
98                     wallpaperPreferences.getHomeWallpaperAttributions(),
99                     wallpaperPreferences.getHomeWallpaperActionLabelRes(),
100                     wallpaperPreferences.getHomeWallpaperActionIconRes(),
101                     wallpaperPreferences.getHomeWallpaperActionUrl(),
102                     wallpaperPreferences.getHomeWallpaperCollectionId(),
103                     wallpaperPreferences.getHomeWallpaperRemoteId());
104             wallpaperFile.delete();
105 
106         } catch (Exception ex) {
107             DiskBasedLogger.e(TAG, "Unable to set static wallpaper", appContext);
108         }
109     }
110 }
111