2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-10 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
28 import android
.app
.Activity
;
29 import android
.app
.AlertDialog
;
30 import android
.content
.DialogInterface
;
31 import android
.os
.Bundle
;
32 import android
.content
.Context
;
33 import android
.view
.ViewGroup
;
34 import android
.graphics
.Paint
;
35 import android
.graphics
.Canvas
;
36 import android
.graphics
.Path
;
37 import android
.graphics
.Bitmap
;
38 import android
.graphics
.Matrix
;
39 import android
.graphics
.RectF
;
40 import android
.graphics
.Rect
;
41 import android
.text
.ClipboardManager
;
42 import com
.juce
.ComponentPeerView
;
43 import java
.io
.BufferedInputStream
;
44 import java
.io
.IOException
;
45 import java
.io
.InputStream
;
46 import java
.io
.OutputStream
;
48 import java
.net
.HttpURLConnection
;
50 //==============================================================================
51 public final class JuceAppActivity
extends Activity
53 //==============================================================================
56 System
.loadLibrary ("juce_jni");
60 public final void onCreate (Bundle savedInstanceState
)
62 super.onCreate (savedInstanceState
);
64 viewHolder
= new ViewHolder (this);
65 setContentView (viewHolder
);
69 protected final void onDestroy()
75 private void callAppLauncher()
77 launchApp (getApplicationInfo().publicSourceDir
,
78 getApplicationInfo().dataDir
);
81 //==============================================================================
82 public native void launchApp (String appFile
, String appDataDir
);
83 public native void quitApp();
84 public native void setScreenSize (int screenWidth
, int screenHeight
);
86 //==============================================================================
87 public static final void printToConsole (String s
)
89 android
.util
.Log
.i ("Juce", s
);
92 //==============================================================================
93 public native void deliverMessage (long value
);
94 private android
.os
.Handler messageHandler
= new android
.os
.Handler();
96 public final void postMessage (long value
)
98 messageHandler
.post (new MessageCallback (value
));
101 final class MessageCallback
implements Runnable
103 public MessageCallback (long value_
)
108 public final void run()
110 deliverMessage (value
);
116 //==============================================================================
117 private ViewHolder viewHolder
;
119 public final ComponentPeerView
createNewView (boolean opaque
)
121 ComponentPeerView v
= new ComponentPeerView (this, opaque
);
122 viewHolder
.addView (v
);
126 public final void deleteView (ComponentPeerView view
)
128 viewHolder
.removeView (view
);
131 final class ViewHolder
extends ViewGroup
133 public ViewHolder (Context context
)
136 setDescendantFocusability (ViewGroup
.FOCUS_AFTER_DESCENDANTS
);
137 setFocusable (false);
140 protected final void onLayout (boolean changed
, int left
, int top
, int right
, int bottom
)
142 setScreenSize (getWidth(), getHeight());
146 isFirstResize
= false;
151 private boolean isFirstResize
= true;
154 public final void excludeClipRegion (android
.graphics
.Canvas canvas
, float left
, float top
, float right
, float bottom
)
156 canvas
.clipRect (left
, top
, right
, bottom
, android
.graphics
.Region
.Op
.DIFFERENCE
);
159 //==============================================================================
160 public final String
getClipboardContent()
162 ClipboardManager clipboard
= (ClipboardManager
) getSystemService (CLIPBOARD_SERVICE
);
163 return clipboard
.getText().toString();
166 public final void setClipboardContent (String newText
)
168 ClipboardManager clipboard
= (ClipboardManager
) getSystemService (CLIPBOARD_SERVICE
);
169 clipboard
.setText (newText
);
172 //==============================================================================
173 public final void showMessageBox (String title
, String message
, final long callback
)
175 AlertDialog
.Builder builder
= new AlertDialog
.Builder (this);
176 builder
.setTitle (title
)
177 .setMessage (message
)
178 .setCancelable (true)
179 .setPositiveButton ("OK", new DialogInterface
.OnClickListener()
181 public void onClick (DialogInterface dialog
, int id
)
184 JuceAppActivity
.this.alertDismissed (callback
, 0);
188 builder
.create().show();
191 public final void showOkCancelBox (String title
, String message
, final long callback
)
193 AlertDialog
.Builder builder
= new AlertDialog
.Builder (this);
194 builder
.setTitle (title
)
195 .setMessage (message
)
196 .setCancelable (true)
197 .setPositiveButton ("OK", new DialogInterface
.OnClickListener()
199 public void onClick (DialogInterface dialog
, int id
)
202 JuceAppActivity
.this.alertDismissed (callback
, 1);
205 .setNegativeButton ("Cancel", new DialogInterface
.OnClickListener()
207 public void onClick (DialogInterface dialog
, int id
)
210 JuceAppActivity
.this.alertDismissed (callback
, 0);
214 builder
.create().show();
217 public final void showYesNoCancelBox (String title
, String message
, final long callback
)
219 AlertDialog
.Builder builder
= new AlertDialog
.Builder (this);
220 builder
.setTitle (title
)
221 .setMessage (message
)
222 .setCancelable (true)
223 .setPositiveButton ("Yes", new DialogInterface
.OnClickListener()
225 public void onClick (DialogInterface dialog
, int id
)
228 JuceAppActivity
.this.alertDismissed (callback
, 1);
231 .setNegativeButton ("No", new DialogInterface
.OnClickListener()
233 public void onClick (DialogInterface dialog
, int id
)
236 JuceAppActivity
.this.alertDismissed (callback
, 2);
239 .setNeutralButton ("Cancel", new DialogInterface
.OnClickListener()
241 public void onClick (DialogInterface dialog
, int id
)
244 JuceAppActivity
.this.alertDismissed (callback
, 0);
248 builder
.create().show();
251 public native void alertDismissed (long callback
, int id
);
253 //==============================================================================
254 public final int[] renderGlyph (char glyph
, Paint paint
, Matrix matrix
, Rect bounds
)
257 paint
.getTextPath (String
.valueOf (glyph
), 0, 1, 0.0f
, 0.0f
, p
);
259 RectF boundsF
= new RectF();
260 p
.computeBounds (boundsF
, true);
261 matrix
.mapRect (boundsF
);
263 boundsF
.roundOut (bounds
);
267 final int w
= bounds
.width();
268 final int h
= bounds
.height();
270 Bitmap bm
= Bitmap
.createBitmap (w
, h
, Bitmap
.Config
.ARGB_8888
);
272 Canvas c
= new Canvas (bm
);
273 matrix
.postTranslate (-bounds
.left
, -bounds
.top
);
274 c
.setMatrix (matrix
);
275 c
.drawPath (p
, paint
);
277 int sizeNeeded
= w
* h
;
278 if (cachedRenderArray
.length
< sizeNeeded
)
279 cachedRenderArray
= new int [sizeNeeded
];
281 bm
.getPixels (cachedRenderArray
, 0, w
, 0, 0, w
, h
);
283 return cachedRenderArray
;
286 private int[] cachedRenderArray
= new int [256];
288 //==============================================================================
289 public static class HTTPStream
291 public HTTPStream (HttpURLConnection connection_
) throws IOException
293 connection
= connection_
;
294 inputStream
= new BufferedInputStream (connection
.getInputStream());
297 public final void release()
303 catch (IOException e
)
306 connection
.disconnect();
309 public final int read (byte[] buffer
, int numBytes
)
315 num
= inputStream
.read (buffer
, 0, numBytes
);
317 catch (IOException e
)
326 public final long getPosition()
331 public final long getTotalLength()
336 public final boolean isExhausted()
341 public final boolean setPosition (long newPos
)
346 private HttpURLConnection connection
;
347 private InputStream inputStream
;
348 private long position
;
351 public static final HTTPStream
createHTTPStream (String address
, boolean isPost
, byte[] postData
,
352 String headers
, int timeOutMs
, java
.lang
.StringBuffer responseHeaders
)
356 HttpURLConnection connection
= (HttpURLConnection
) (new URL (address
).openConnection());
358 if (connection
!= null)
364 connection
.setConnectTimeout (timeOutMs
);
365 connection
.setDoOutput (true);
366 connection
.setChunkedStreamingMode (0);
368 OutputStream out
= connection
.getOutputStream();
369 out
.write (postData
);
373 return new HTTPStream (connection
);
377 connection
.disconnect();