summaryrefslogtreecommitdiffstats
path: root/src/com/android/wallpaper/module/RotationWallpaperUpdateReceiver.java
blob: 4fbc20d138c4f3bb1936231a1a025e78e4ae39af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.wallpaper.module;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.android.wallpaper.compat.BuildCompat;
import com.android.wallpaper.util.DiskBasedLogger;
import com.android.wallpaper.util.FileMover;

import java.io.File;

/**
 * Receiver to run when the app was updated or on first boot to switch from live rotating wallpaper
 * to static rotating wallpaper.
 */
public class RotationWallpaperUpdateReceiver extends BroadcastReceiver {


    // Earlier versions of rotating wallpaper save the current rotation image as a file.
    // We can infer from the extistance of this file whether or not user had rotating live wallpaper
    private static final String ROTATING_WALLPAPER_FILE_PATH = "rotating_wallpaper.jpg";
    private static final String TAG = "RotationWallpaperUpdateReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        // This receiver is a no-op on pre-N Android and should only respond to a
        // MY_PACKAGE_REPLACED intent.
        if (intent.getAction() == null
                || !(intent.getAction().equals(Intent.ACTION_MY_PACKAGE_REPLACED)
                || intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
                || !BuildCompat.isAtLeastN()) {
            DiskBasedLogger.e(
                    TAG,
                    "Unexpected action or Android version!",
                    context);
            throw new IllegalStateException(
                    "Unexpected broadcast action or unsupported Android version");
        }

        PendingResult broadcastResult = goAsync();
        new Thread(() -> {
            Context appContext = context.getApplicationContext();
            Context deviceProtectedContext = appContext.createDeviceProtectedStorageContext();

            if (context.getFileStreamPath(ROTATING_WALLPAPER_FILE_PATH).exists()) {
                moveFileToProtectedStorage(appContext, deviceProtectedContext);
            }

            File wallpaperFile = deviceProtectedContext.getFileStreamPath(
                    ROTATING_WALLPAPER_FILE_PATH);
            if (wallpaperFile.exists()) {
                switchToStaticWallpaper(appContext, wallpaperFile);
            }
            broadcastResult.finish();
        }).start();
    }

    private void moveFileToProtectedStorage(Context context, Context deviceProtectedContext) {
        try {
            FileMover.moveFileBetweenContexts(context, ROTATING_WALLPAPER_FILE_PATH,
                    deviceProtectedContext, ROTATING_WALLPAPER_FILE_PATH);
        } catch (Exception ex) {
            DiskBasedLogger.e(
                    TAG,
                    "Failed to move rotating wallpaper file to device protected storage: "
                            + ex.getMessage(),
                    context);
        }
    }

    private void switchToStaticWallpaper(Context appContext, File wallpaperFile) {
        try {
            Injector injector = InjectorProvider.getInjector();
            WallpaperPreferences wallpaperPreferences = injector.getPreferences(appContext);
            if (wallpaperPreferences.getWallpaperPresentationMode()
                    != WallpaperPreferences.PRESENTATION_MODE_ROTATING) {
                return;
            }
            Bitmap bitmap = BitmapFactory.decodeFile(wallpaperFile.getAbsolutePath());

            injector.getWallpaperPersister(appContext).setWallpaperInRotation(bitmap,
                    wallpaperPreferences.getHomeWallpaperAttributions(),
                    wallpaperPreferences.getHomeWallpaperActionLabelRes(),
                    wallpaperPreferences.getHomeWallpaperActionIconRes(),
                    wallpaperPreferences.getHomeWallpaperActionUrl(),
                    wallpaperPreferences.getHomeWallpaperCollectionId());
            wallpaperFile.delete();

        } catch (Exception ex) {
            DiskBasedLogger.e(TAG, "Unable to set static wallpaper", appContext);
        }
    }
}