summaryrefslogtreecommitdiffstats
path: root/src/com/android/wallpaper/util/WallpaperSurfaceCallback.java
blob: e0430516926e690b8a8678c3b0aec5da65159111 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2020 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.util;

import static android.view.View.MeasureSpec.EXACTLY;
import static android.view.View.MeasureSpec.makeMeasureSpec;

import android.content.Context;
import android.service.wallpaper.WallpaperService;
import android.view.Surface;
import android.view.SurfaceControlViewHost;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.android.wallpaper.module.Injector;
import com.android.wallpaper.module.InjectorProvider;
import com.android.wallpaper.module.PackageStatusNotifier;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Default implementation of {@link SurfaceHolder.Callback} to render a static wallpaper when the
 * surface has been created.
 */
public class WallpaperSurfaceCallback implements SurfaceHolder.Callback {

    /**
     * Listener used to be notified when this surface is created
     */
    public interface SurfaceListener {
        /**
         * Called when {@link WallpaperSurfaceCallback#surfaceCreated(SurfaceHolder)} is called.
         */
        void onSurfaceCreated();
    }

    private Surface mLastSurface;
    private SurfaceControlViewHost mHost;
    // Home workspace surface is behind the app window, and so must the home image wallpaper like
    // the live wallpaper. This view is rendered on here for home image wallpaper.
    private ImageView mHomeImageWallpaper;
    private final Context mContext;
    private final View mContainerView;
    private final SurfaceView mWallpaperSurface;
    @Nullable
    private final SurfaceListener mListener;
    @Nullable
    private final Future<Integer> mPlaceholderColor;
    private boolean mSurfaceCreated;

    private PackageStatusNotifier.Listener mAppStatusListener;
    private PackageStatusNotifier mPackageStatusNotifier;

    public WallpaperSurfaceCallback(Context context, View containerView,
            SurfaceView wallpaperSurface, @Nullable Future<Integer> placeholderColor,
            @Nullable SurfaceListener listener) {
        mContext = context.getApplicationContext();
        mContainerView = containerView;
        mWallpaperSurface = wallpaperSurface;
        mListener = listener;

        // Notify WallpaperSurface to reset image wallpaper when encountered live wallpaper's
        // package been changed in background.
        Injector injector = InjectorProvider.getInjector();
        mPackageStatusNotifier = injector.getPackageStatusNotifier(context);
        mAppStatusListener = (packageName, status) -> {
            if (status != PackageStatusNotifier.PackageStatus.REMOVED) {
                resetHomeImageWallpaper();
            }
        };
        mPackageStatusNotifier.addListener(mAppStatusListener,
                WallpaperService.SERVICE_INTERFACE);
        mPlaceholderColor = placeholderColor;
    }

    public WallpaperSurfaceCallback(Context context, View containerView,
            SurfaceView wallpaperSurface, @Nullable SurfaceListener listener) {
        this(context, containerView, wallpaperSurface, null, listener);
    }

    public WallpaperSurfaceCallback(Context context, View containerView,
            SurfaceView wallpaperSurface) {
        this(context, containerView, wallpaperSurface, null);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mLastSurface != holder.getSurface()) {
            mLastSurface = holder.getSurface();
            setupSurfaceWallpaper(/* forceClean= */ true);
        }
        if (mListener != null) {
            mListener.onSurfaceCreated();
        }
        mSurfaceCreated = true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mSurfaceCreated = false;
    }

    /**
     * Call to release resources and app status listener.
     */
    public void cleanUp() {
        releaseHost();
        if (mHomeImageWallpaper != null) {
            mHomeImageWallpaper.setImageDrawable(null);
        }
        mPackageStatusNotifier.removeListener(mAppStatusListener);
    }

    private void releaseHost() {
        if (mHost != null) {
            mHost.release();
            mHost = null;
        }
    }

    /**
     * Reset existing image wallpaper by creating a new ImageView for SurfaceControlViewHost
     * if surface state is not created.
     */
    private void resetHomeImageWallpaper() {
        if (mSurfaceCreated) {
            return;
        }

        if (mHost != null) {
            setupSurfaceWallpaper(/* forceClean= */ false);
        }
    }

    private void setupSurfaceWallpaper(boolean forceClean) {
        mHomeImageWallpaper = new ImageView(mContext);
        Integer placeholder = null;
        if (mPlaceholderColor != null && mPlaceholderColor.isDone()) {
            try {
                placeholder = mPlaceholderColor.get();
            } catch (InterruptedException | ExecutionException e) {
                // Ignore
            }
        }
        mHomeImageWallpaper.setBackgroundColor((placeholder != null) ? placeholder
                : ResourceUtils.getColorAttr(mContext, android.R.attr.colorSecondary));
        mHomeImageWallpaper.measure(makeMeasureSpec(mContainerView.getWidth(), EXACTLY),
                makeMeasureSpec(mContainerView.getHeight(), EXACTLY));
        mHomeImageWallpaper.layout(0, 0, mContainerView.getWidth(),
                mContainerView.getHeight());
        if (forceClean) {
            releaseHost();
            mHost = new SurfaceControlViewHost(mContext,
                    mContainerView.getDisplay(), mWallpaperSurface.getHostToken());
        }
        mHost.setView(mHomeImageWallpaper, mHomeImageWallpaper.getWidth(),
                mHomeImageWallpaper.getHeight());
        mWallpaperSurface.setChildSurfacePackage(mHost.getSurfacePackage());
    }

    @Nullable
    public ImageView getHomeImageWallpaper() {
        return mHomeImageWallpaper;
    }
}