From http://www.jwz.org/xscreensaver/xscreensaver-5.38.tar.gz
[xscreensaver] / android / xscreensaver / src / org / jwz / xscreensaver / XScreenSaverWallpaper.java
1 /* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * xscreensaver, Copyright (c) 2016-2017 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  *
12  * The superclass of every saver's Wallpaper.
13  *
14  * Each Wallpaper needs a distinct subclass in order to show up in the list.
15  * We know which saver we are running by the subclass name; we know which
16  * API to use by how the subclass calls super().
17  */
18
19 package org.jwz.xscreensaver;
20
21 import android.service.wallpaper.WallpaperService;
22 import android.view.GestureDetector;
23 import android.view.SurfaceHolder;
24 import android.util.Log;
25 import java.lang.RuntimeException;
26 import java.lang.Thread;
27 import org.jwz.xscreensaver.jwxyz;
28
29 public class XScreenSaverWallpaper extends WallpaperService
30 /*implements GestureDetector.OnGestureListener,
31              GestureDetector.OnDoubleTapListener, */ {
32
33   /* TODO: Input! */
34
35   @Override
36   public Engine onCreateEngine() {
37     // Log.d("xscreensaver", "tid = " + Thread.currentThread().getId());
38     return new XScreenSaverGLEngine();
39   }
40
41   class XScreenSaverGLEngine extends Engine {
42
43     private boolean initTried = false;
44     private jwxyz jwxyz_obj;
45
46     @Override
47     public void onSurfaceCreated (SurfaceHolder holder) {
48       super.onSurfaceCreated(holder);
49
50       if (!initTried) {
51         initTried = true;
52       } else {
53         if (jwxyz_obj != null) {
54           jwxyz_obj.close();
55           jwxyz_obj = null;
56         }
57       }
58     }
59
60     @Override
61     public void onVisibilityChanged(final boolean visible) {
62       if (jwxyz_obj != null) {
63         if (visible)
64           jwxyz_obj.start();
65         else
66           jwxyz_obj.pause();
67       }
68     }
69
70     @Override
71     public void onSurfaceChanged (SurfaceHolder holder, int format,
72                                   int width, int height) {
73
74       super.onSurfaceChanged(holder, format, width, height);
75
76       if (width == 0 || height == 0) {
77         jwxyz_obj.close();
78         jwxyz_obj = null;
79       }
80
81       Log.d ("xscreensaver",
82              String.format("surfaceChanged: %dx%d", width, height));
83
84       if (jwxyz_obj == null) {
85         jwxyz_obj = new jwxyz (jwxyz.saverNameOf(XScreenSaverWallpaper.this),
86                                XScreenSaverWallpaper.this, null,
87                                width, height, holder.getSurface(), null);
88       } else {
89         jwxyz_obj.resize (width, height);
90       }
91
92       jwxyz_obj.start();
93     }
94
95     @Override
96     public void onSurfaceDestroyed (SurfaceHolder holder) {
97       super.onSurfaceDestroyed (holder);
98
99       if (jwxyz_obj != null) {
100         jwxyz_obj.close();
101         jwxyz_obj = null;
102       }
103     }
104   }
105 }