Skip to content

Commit 643d996

Browse files
committed
ANDROID: scheme start can be zipped
1 parent 8bbcfe1 commit 643d996

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

src/platform/android/src/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import java.io.BufferedOutputStream;
44
import java.io.BufferedReader;
55
import java.io.BufferedWriter;
6+
import java.io.ByteArrayInputStream;
67
import java.io.ByteArrayOutputStream;
78
import java.io.DataInputStream;
89
import java.io.File;
910
import java.io.FileNotFoundException;
1011
import java.io.FileReader;
1112
import java.io.FileWriter;
1213
import java.io.IOException;
14+
import java.io.InputStreamReader;
1315
import java.io.UnsupportedEncodingException;
1416
import java.net.ServerSocket;
1517
import java.net.Socket;
@@ -22,6 +24,7 @@
2224
import java.util.concurrent.ConcurrentLinkedQueue;
2325
import java.util.concurrent.ExecutorService;
2426
import java.util.concurrent.Executors;
27+
import java.util.zip.GZIPInputStream;
2528

2629
import android.annotation.TargetApi;
2730
import android.app.Activity;
@@ -35,6 +38,7 @@
3538
import android.net.Uri;
3639
import android.os.Build;
3740
import android.os.Bundle;
41+
import android.util.Base64;
3842
import android.util.Log;
3943
import android.view.InputDevice;
4044
import 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
}

src/platform/android/src/net/sourceforge/smallbasic/Sound.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.sourceforge.smallbasic;
22

33
import android.annotation.TargetApi;
4-
import android.content.Context;
54
import android.media.AudioFormat;
65
import android.media.AudioManager;
76
import android.media.AudioTrack;
@@ -17,33 +16,33 @@
1716
public class Sound {
1817
private static final String TAG = "smallbasic";
1918
static final int AUDIO_SAMPLE_RATE = 8000;
20-
private byte[] sound;
21-
private float volume;
22-
private int dur;
23-
private boolean silent;
19+
private byte[] _sound;
20+
private float _volume;
21+
private int _dur;
22+
private boolean _silent;
2423

2524
public Sound(int frq, int dur, float vol) {
26-
this.sound = generateTone(frq, dur);
27-
this.volume = vol;
28-
this.dur = dur;
29-
this.silent = false;
25+
this._sound = generateTone(frq, dur);
26+
this._volume = vol;
27+
this._dur = dur;
28+
this._silent = false;
3029
}
3130

3231
public boolean isSilent() {
33-
return silent;
32+
return _silent;
3433
}
3534

3635
public void play() {
37-
if (!silent) {
36+
if (!_silent) {
3837
try {
3938
AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
4039
AUDIO_SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO,
41-
AudioFormat.ENCODING_PCM_16BIT, sound.length, AudioTrack.MODE_STATIC);
42-
if (audioTrack.write(sound, 0, sound.length) == sound.length) {
43-
audioTrack.setStereoVolume(volume, volume);
40+
AudioFormat.ENCODING_PCM_16BIT, _sound.length, AudioTrack.MODE_STATIC);
41+
if (audioTrack.write(_sound, 0, _sound.length) == _sound.length) {
42+
audioTrack.setStereoVolume(_volume, _volume);
4443
playTrack(audioTrack);
4544
} else {
46-
Log.i(TAG, "Failed to write audio: " + sound.length);
45+
Log.i(TAG, "Failed to write audio: " + _sound.length);
4746
}
4847
} catch (Exception e) {
4948
Log.i(TAG, "play failed: ", e);
@@ -52,7 +51,7 @@ public void play() {
5251
}
5352

5453
public void setSilent(boolean silent) {
55-
this.silent = silent;
54+
this._silent = silent;
5655
}
5756

5857
/**
@@ -112,10 +111,10 @@ private final byte[] generateTone(int freqOfTone, int durationMillis) {
112111

113112
private void playTrack(AudioTrack audioTrack) throws InterruptedException {
114113
int frame;
115-
int frames = sound.length / 2;
114+
int frames = _sound.length / 2;
116115
audioTrack.play();
117116
do {
118-
Thread.sleep(dur / 2);
117+
Thread.sleep(_dur / 2);
119118
frame = audioTrack.getPlaybackHeadPosition();
120119
} while (frame < frames);
121120
audioTrack.release();

0 commit comments

Comments
 (0)