summaryrefslogtreecommitdiffstats
path: root/android/xscreensaver/src/org/jwz/xscreensaver/Daydream.java
blob: 372af95ce40dbd9d7482488e626e079ec1d28877 (plain) (blame)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* -*- Mode: java; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * xscreensaver, Copyright (c) 2016-2017 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 *
 * The superclass of every saver's Daydream.
 *
 * Each Daydream needs a distinct subclass in order to show up in the list.
 * We know which saver we are running by the subclass name; we know which
 * API to use by how the subclass calls super().
 */

package org.jwz.xscreensaver;

import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.KeyEvent;
import android.service.dreams.DreamService;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Message;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

public class Daydream extends DreamService {

  private class SaverView extends SurfaceView
    implements SurfaceHolder.Callback {

    private boolean initTried = false;
    private jwxyz jwxyz_obj;

    private GestureDetector detector;

    private Runnable on_quit = new Runnable() {
      @Override
      public void run() {
        finish();  // Exit the Daydream
      }
    };

    SaverView () {
      super (Daydream.this);
      getHolder().addCallback(this);
    }

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

      if (width == 0 || height == 0) {
        detector = null;
        jwxyz_obj.close();
        jwxyz_obj = null;
      }

      Log.d ("xscreensaver",
             String.format("surfaceChanged: %dx%d", width, height));

      /*
      double r = 0;

      Display d = view.getDisplay();

      if (d != null) {
        switch (d.getRotation()) {
        case Surface.ROTATION_90:  r = 90;  break;
        case Surface.ROTATION_180: r = 180; break;
        case Surface.ROTATION_270: r = 270; break;
        }
      }
      */

      if (jwxyz_obj == null) {
        jwxyz_obj = new jwxyz (jwxyz.saverNameOf (Daydream.this),
                               Daydream.this, screenshot, width, height,
                               holder.getSurface(), on_quit);
        detector = new GestureDetector (Daydream.this, jwxyz_obj);
      } else {
        jwxyz_obj.resize (width, height);
      }

      jwxyz_obj.start();
    }

    @Override
    public void surfaceCreated (SurfaceHolder holder) {
      if (!initTried) {
        initTried = true;
      } else {
        if (jwxyz_obj != null) {
          jwxyz_obj.close();
          jwxyz_obj = null;
        }
      }
    }

    @Override
    public void surfaceDestroyed (SurfaceHolder holder) {
      if (jwxyz_obj != null) {
        jwxyz_obj.close();
        jwxyz_obj = null;
      }
    }

    @Override
    public boolean onTouchEvent (MotionEvent event) {
      detector.onTouchEvent (event);
      if (event.getAction() == MotionEvent.ACTION_UP)
        jwxyz_obj.dragEnded (event);
      return true;
    }

    @Override
    public boolean onKeyDown (int keyCode, KeyEvent event) {
      // In the emulator, this doesn't receive keyboard arrow keys, PgUp, etc.
      // Some other keys like "Home" are interpreted before we get here, and
      // function keys do weird shit.

      // TODO: Does this still work? And is the above still true?

      if (view.jwxyz_obj != null)
        view.jwxyz_obj.sendKeyEvent (event);
      return true;
    }
  }

  private SaverView view;
  Bitmap screenshot;

  private void LOG (String fmt, Object... args) {
    Log.d ("xscreensaver",
           this.getClass().getSimpleName() + ": " +
           String.format (fmt, args));
  }

  protected Daydream () {
    super();
  }

  // Called when jwxyz_abort() is called, or other exceptions are thrown.
  //
/*
  @Override
  public void uncaughtException (Thread thread, Throwable ex) {

    renderer = null;
    String err = ex.toString();
    LOG ("Caught exception: %s", err);

    this.finish();  // Exit the Daydream

    final AlertDialog.Builder b = new AlertDialog.Builder(this);
    b.setMessage (err);
    b.setCancelable (false);
    b.setPositiveButton ("Bummer",
                         new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface d, int id) {
                           }
                         });

    // #### This isn't working:
    // "Attempted to add window with non-application token"
    // "Unable to add window -- token null is not for an application"
    // I think I need to get an "Activity" to run it on somehow?

    new Handler (Looper.getMainLooper()).post (new Runnable() {
        public void run() {
          AlertDialog alert = b.create();
          alert.setTitle (this.getClass().getSimpleName() + " crashed");
          alert.setIcon(android.R.drawable.ic_dialog_alert);
          alert.show();
        }
      });

    old_handler.uncaughtException (thread, ex);
  }
*/


  @Override
  public void onAttachedToWindow() {
    super.onAttachedToWindow();

    setInteractive (true);
    setFullscreen (true);
    saveScreenshot();

    view = new SaverView ();
    setContentView (view);
  }

  public void onDreamingStarted() {
    super.onDreamingStarted();
    // view.jwxyz_obj.start();
  }

  public void onDreamingStopped() {
    super.onDreamingStopped();
    view.jwxyz_obj.pause();
  }

  public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    try {
      if (view.jwxyz_obj != null)
        view.jwxyz_obj.pause();
    } catch (Exception exc) {
      // Fun fact: Android swallows exceptions coming from here, then crashes
      // elsewhere.
      LOG ("onDetachedFromWindow: %s", exc.toString());
      throw exc;
    }
  }


  // At startup, before we have blanked the screen, save a screenshot
  // for later use by the hacks.
  //
  private void saveScreenshot() {
    View view = getWindow().getDecorView().getRootView();
    if (view == null) {
      LOG ("unable to get root view for screenshot");
    } else {

      // This doesn't work:
  /*
      boolean was = view.isDrawingCacheEnabled();
      if (!was) view.setDrawingCacheEnabled (true);
      view.buildDrawingCache();
      screenshot = view.getDrawingCache();
      if (!was) view.setDrawingCacheEnabled (false);
      if (screenshot == null) {
        LOG ("unable to get screenshot bitmap from %s", view.toString());
      } else {
        screenshot = Bitmap.createBitmap (screenshot);
      }
   */

      // This doesn't work either: width and height are both -1...

      int w = view.getLayoutParams().width;
      int h = view.getLayoutParams().height;
      if (w <= 0 || h <= 0) {
        LOG ("unable to get root view for screenshot");
      } else {
        screenshot = Bitmap.createBitmap (w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas (screenshot);
        view.layout (0, 0, w, h);
        view.draw (c);
      }
    }
  }
}