33import java .io .BufferedOutputStream ;
44import java .io .BufferedReader ;
55import java .io .BufferedWriter ;
6+ import java .io .ByteArrayInputStream ;
67import java .io .ByteArrayOutputStream ;
78import java .io .DataInputStream ;
89import java .io .File ;
910import java .io .FileNotFoundException ;
1011import java .io .FileReader ;
1112import java .io .FileWriter ;
1213import java .io .IOException ;
14+ import java .io .InputStreamReader ;
1315import java .io .UnsupportedEncodingException ;
1416import java .net .ServerSocket ;
1517import java .net .Socket ;
2224import java .util .concurrent .ConcurrentLinkedQueue ;
2325import java .util .concurrent .ExecutorService ;
2426import java .util .concurrent .Executors ;
27+ import java .util .zip .GZIPInputStream ;
2528
2629import android .annotation .TargetApi ;
2730import android .app .Activity ;
3538import android .net .Uri ;
3639import android .os .Build ;
3740import android .os .Bundle ;
41+ import android .util .Base64 ;
3842import android .util .Log ;
3943import android .view .InputDevice ;
4044import android .view .View ;
@@ -51,10 +55,10 @@ public class MainActivity extends NativeActivity {
5155 private static final String WEB_BAS = "web.bas" ;
5256 private static final String SCHEME_BAS = "scheme.bas" ;
5357 private static final String SCHEME = "smallbasic://x/" ;
54- private String startupBas = null ;
55- private boolean untrusted = false ;
56- private ExecutorService audioExecutor = Executors .newSingleThreadExecutor ();
57- private Queue <Sound > sounds = new ConcurrentLinkedQueue <Sound >();
58+ private String _startupBas = null ;
59+ private boolean _untrusted = false ;
60+ private ExecutorService _audioExecutor = Executors .newSingleThreadExecutor ();
61+ private Queue <Sound > _sounds = new ConcurrentLinkedQueue <Sound >();
5862
5963 static {
6064 System .loadLibrary ("smallbasic" );
@@ -66,19 +70,19 @@ public class MainActivity extends NativeActivity {
6670
6771 public void clearSoundQueue () {
6872 Log .i (TAG , "clearSoundQueue" );
69- for (Sound sound : sounds ) {
73+ for (Sound sound : _sounds ) {
7074 sound .setSilent (true );
7175 }
7276 }
7377
7478 public boolean getSoundPlaying () {
75- boolean result = this .sounds .size () > 0 ;
79+ boolean result = this ._sounds .size () > 0 ;
7680 Log .i (TAG , "getSoundPlaying = " + result );
7781 return result ;
7882 }
7983
8084 public String getStartupBas () {
81- return this .startupBas ;
85+ return this ._startupBas ;
8286 }
8387
8488 public int getUnicodeChar (int keyCode , int metaState ) {
@@ -94,7 +98,7 @@ public int getUnicodeChar(int keyCode, int metaState) {
9498
9599 public boolean getUntrusted () {
96100 Log .i (TAG , "getUntrusted" );
97- return this .untrusted ;
101+ return this ._untrusted ;
98102 }
99103
100104 @ Override
@@ -131,12 +135,12 @@ public void playTone(int frq, int dur, int vol) {
131135 Log .i (TAG , "playTone: " + frq + " " + dur + " " + vol + " " + volume );
132136
133137 final Sound sound = new Sound (frq , dur , volume );
134- sounds .add (sound );
135- audioExecutor .execute (new Runnable () {
138+ _sounds .add (sound );
139+ _audioExecutor .execute (new Runnable () {
136140 @ Override
137141 public void run () {
138142 sound .play ();
139- sounds .remove (sound );
143+ _sounds .remove (sound );
140144 }
141145 });
142146 }
@@ -182,9 +186,9 @@ protected void onCreate(Bundle savedInstanceState) {
182186 execScheme (data );
183187 Log .i (TAG , "data=" + data );
184188 } else {
185- startupBas = uri .getPath ();
189+ _startupBas = uri .getPath ();
186190 }
187- Log .i (TAG , "startupBas=" + startupBas );
191+ Log .i (TAG , "startupBas=" + _startupBas );
188192 }
189193 try {
190194 Properties p = new Properties ();
@@ -239,9 +243,25 @@ private String execBuffer(final String buffer, final String name, boolean run) t
239243
240244 private void execScheme (final String data ) {
241245 try {
242- String bas = URLDecoder .decode (data .substring (SCHEME .length ()), "utf-8" );
243- startupBas = execBuffer (bas , SCHEME_BAS , false );
244- untrusted = true ;
246+ String input = data .substring (SCHEME .length ());
247+ byte [] decodedBytes = Base64 .decode (input , Base64 .DEFAULT );
248+ int magic = (decodedBytes [1 ] & 0xFF ) << 8 | decodedBytes [0 ];
249+ BufferedReader reader ;
250+ String bas ;
251+ if (magic == GZIPInputStream .GZIP_MAGIC ) {
252+ GZIPInputStream zipStream = new GZIPInputStream (new ByteArrayInputStream (decodedBytes ));
253+ reader = new BufferedReader (new InputStreamReader (zipStream ));
254+ StringBuilder out = new StringBuilder ();
255+ String s ;
256+ while ((s = reader .readLine ()) != null ) {
257+ out .append (s ).append ('\n' );
258+ }
259+ bas = out .toString ();
260+ } else {
261+ bas = URLDecoder .decode (input , "utf-8" );
262+ }
263+ _startupBas = execBuffer (bas , SCHEME_BAS , false );
264+ _untrusted = true ;
245265 } catch (IOException e ) {
246266 Log .i (TAG , "saveSchemeData failed: " , e );
247267 }
0 commit comments