From http://www.jwz.org/xscreensaver/xscreensaver-5.39.tar.gz
[xscreensaver] / android / xscreensaver / src / org / jwz / xscreensaver / Wallpaper.java
1 /* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * xscreensaver, Copyright (c) 2016-2018 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.content.res.Configuration;
22 import android.service.wallpaper.WallpaperService;
23 import android.view.GestureDetector;
24 import android.view.SurfaceHolder;
25 import android.util.Log;
26 import java.lang.RuntimeException;
27 import java.lang.Thread;
28 import org.jwz.xscreensaver.jwxyz;
29 import android.graphics.PixelFormat;
30 import android.view.WindowManager;
31 import android.view.Display;
32 import android.graphics.Point;
33
34 public class Wallpaper extends WallpaperService
35 /*implements GestureDetector.OnGestureListener,
36              GestureDetector.OnDoubleTapListener, */ {
37
38   /* TODO: Input! */
39   private Engine engine;
40
41   @Override
42   public Engine onCreateEngine() {
43     // Log.d("xscreensaver", "tid = " + Thread.currentThread().getId());
44     engine = new XScreenSaverGLEngine();
45     return engine;
46   }
47
48   @Override
49   public void onConfigurationChanged(Configuration config) {
50       super.onConfigurationChanged(config);
51       Log.d("xscreensaver", "wallpaper onConfigurationChanged");
52       /*
53       WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
54       Display display = wm.getDefaultDisplay();
55       Point size = new Point();
56       display.getSize(size);
57       int width = size.x;
58       int height = size.y;
59       engine.onSurfaceChanged(engine.getSurfaceHolder(), PixelFormat.RGBA_8888, width, height);
60       */
61       
62   }
63
64   class XScreenSaverGLEngine extends Engine {
65
66     private boolean initTried = false;
67     private jwxyz jwxyz_obj;
68
69     @Override
70     public void onSurfaceCreated (SurfaceHolder holder) {
71       super.onSurfaceCreated(holder);
72
73       if (!initTried) {
74         initTried = true;
75       } else {
76         if (jwxyz_obj != null) {
77           jwxyz_obj.close();
78           jwxyz_obj = null;
79         }
80       }
81     }
82
83     @Override
84     public void onVisibilityChanged(final boolean visible) {
85       if (jwxyz_obj != null) {
86         if (visible)
87           jwxyz_obj.start();
88         else
89           jwxyz_obj.pause();
90       }
91     }
92
93     @Override
94     public void onSurfaceChanged (SurfaceHolder holder, int format,
95                                   int width, int height) {
96
97       super.onSurfaceChanged(holder, format, width, height);
98
99       if (width == 0 || height == 0) {
100         jwxyz_obj.close();
101         jwxyz_obj = null;
102       }
103
104       Log.d ("xscreensaver",
105              String.format("surfaceChanged: %dx%d", width, height));
106
107       if (jwxyz_obj == null) {
108         jwxyz_obj = new jwxyz (jwxyz.saverNameOf(Wallpaper.this),
109                                Wallpaper.this, null, width, height,
110                                holder.getSurface(), null);
111       } else {
112         jwxyz_obj.resize (width, height);
113       }
114
115       jwxyz_obj.start();
116     }
117
118     @Override
119     public void onSurfaceDestroyed (SurfaceHolder holder) {
120       super.onSurfaceDestroyed (holder);
121
122       if (jwxyz_obj != null) {
123         jwxyz_obj.close();
124         jwxyz_obj = null;
125       }
126     }
127   }
128 }