aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java
blob: 2c537096d33bdbd4857b3cf5ec56b2bf9f3ed90b (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (C) 2014 The CyanogenMod 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 org.cyanogenmod.wallpapers.photophase;

import android.app.ActivityManager;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ViewConfiguration;

import org.cyanogenmod.wallpapers.photophase.GLESWallpaperService.GLESEngineListener;
import org.cyanogenmod.wallpapers.photophase.preferences.PreferencesProvider;

import java.util.ArrayList;
import java.util.List;


/**
 * The PhotoPhase Live Wallpaper service.
 */
public class PhotoPhaseWallpaper
    extends GLES20WallpaperService implements GLESEngineListener {

    private static final String TAG = "PhotoPhaseWallpaper";

    private static final boolean DEBUG = false;

    private List<PhotoPhaseRenderer> mRenderers;
    private PhotoPhaseWallpaperEngine mEngine;

    private boolean mPreserveEGLContext;

    // List of the current top activities. Tap should be ignored when this acitivities are
    // in the foreground
    static final String[] TOP_ACTIVITIES = {"com.android.internal.app.ChooserActivity"};

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate() {
        if (DEBUG) Log.d(TAG, "onCreate");
        super.onCreate();

        // Load the configuration
        mPreserveEGLContext = getResources().getBoolean(R.bool.config_preserve_egl_context);
        mRenderers = new ArrayList<PhotoPhaseRenderer>();

        // Instance the application
        PreferencesProvider.reload(this);
        Colors.register(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDestroy() {
        if (DEBUG) Log.d(TAG, "onDestroy");
        super.onDestroy();
        for (final PhotoPhaseRenderer renderer : mRenderers) {
            // Destroy the instance in the GLThread
            renderer.mDispatcher.dispatch(new Runnable() {
                @Override
                public void run() {
                    renderer.onGLContextDestroy();
                }
            });
            renderer.onDestroy();
        }
        mRenderers.clear();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Engine onCreateEngine() {
        checkPermission();
        mEngine = new PhotoPhaseWallpaperEngine(this);
        return mEngine;
    }

    private void checkPermission() {
        if (!RequestPermissionsActivity.hasRequestedPermissions(this)) {
            startActivity(new Intent(Intent.ACTION_MAIN)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .setClass(this, RequestPermissionsActivity.class));
        }
    }

    /**
     * A wallpaper engine implementation using GLES.
     */
    class PhotoPhaseWallpaperEngine extends GLES20WallpaperService.GLES20Engine {

        private final Handler mHandler;
        final ActivityManager mActivityManager;

        /**
         * Constructor of <code>PhotoPhaseWallpaperEngine<code>
         *
         * @param wallpaper The wallpaper service reference
         */
        PhotoPhaseWallpaperEngine(PhotoPhaseWallpaper wallpaper) {
            super();
            mHandler = new Handler();
            mActivityManager = (ActivityManager)getApplication().getSystemService(ACTIVITY_SERVICE);
            setOffsetNotificationsEnabled(false);
            setTouchEventsEnabled(false);
            setGLESEngineListener(wallpaper);
            setWallpaperGLSurfaceView(new PhotoPhaseWallpaperGLSurfaceView(wallpaper));
            setPauseOnPreview(true);
        }

        /**
         * Out custom GLSurfaceView class to let us access all events stuff.
         */
        class PhotoPhaseWallpaperGLSurfaceView extends WallpaperGLSurfaceView {
            /**
             * The constructor of <code>PhotoPhaseWallpaperGLSurfaceView</code>.
             *
             * @param context The current context
             */
            public PhotoPhaseWallpaperGLSurfaceView(Context context) {
                super(context);
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Bundle onCommand(final String action, final int x, final int y, final int z,
                final Bundle extras, final boolean resultRequested) {
            if (action.compareTo(WallpaperManager.COMMAND_TAP) == 0) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // Only if the wallpaper is visible after a long press and
                        // not in preview mode
                        if (isVisible() && !isPreview()) {
                            List<ActivityManager.RunningTaskInfo> taskInfo =
                                                    mActivityManager.getRunningTasks(1);
                            String topActivity = taskInfo.get(0).topActivity.getClassName();
                            for (String activity : TOP_ACTIVITIES) {
                                if (activity.compareTo(topActivity) == 0) {
                                    // Ignore tap event
                                    return;
                                }
                            }

                            // Pass the x and y position to the renderer
                            ((PhotoPhaseRenderer)getRenderer()).onTouch(x, y);
                        }
                    }
                }, ViewConfiguration.getLongPressTimeout() + 100L);
            }
            return super.onCommand(action, x, y, z, extras, resultRequested);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.i(TAG, "onLowMemory");
        for (PhotoPhaseRenderer renderer : mRenderers) {
            // Pause the wallpaper and destroy the cached textures
            renderer.onPause();
            renderer.onLowMemory();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onInitializeEGLView(GLSurfaceView view) {
        if (DEBUG) Log.d(TAG, "onInitializeEGLView");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDestroyEGLView(GLSurfaceView view, Renderer renderer) {
        if (DEBUG) Log.d(TAG, "onDestroyEGLView" + renderer);
        mRenderers.remove(renderer);
        ((PhotoPhaseRenderer)renderer).onPause();
        ((PhotoPhaseRenderer)renderer).onDestroy();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onEGLViewInitialized(GLSurfaceView view) {
        view.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        view.setPreserveEGLContextOnPause(mPreserveEGLContext);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onPause(Renderer renderer) {
        if (DEBUG) Log.d(TAG, "onPause: " + renderer);
        ((PhotoPhaseRenderer)renderer).onPause();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onResume(Renderer renderer) {
        if (DEBUG) Log.d(TAG, "onResume: " + renderer);
        ((PhotoPhaseRenderer)renderer).onResume();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Renderer getNewRenderer(GLSurfaceView view) {
        if (DEBUG) Log.d(TAG, "getNewRenderer()");
        PhotoPhaseRenderer renderer = new PhotoPhaseRenderer(this,
                new GLESSurfaceDispatcher(view), mEngine.isPreview());
        renderer.onCreate();
        mRenderers.add(renderer);
        if (DEBUG) Log.d(TAG, "renderer" + renderer);
        return renderer;
    }
}