Skip to content

Commit 86f3815

Browse files
committed
ANDROID: remove hardcoded references to /sdcard path
1 parent c75299d commit 86f3815

File tree

4 files changed

+63
-35
lines changed

4 files changed

+63
-35
lines changed

src/common/brun.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ void exec_setup_predefined_variables() {
269269
setsysvar_num(SYSVAR_MAXINT, VAR_MAX_INT);
270270

271271
#if defined(_ANDROID)
272-
if (getenv("EXTERNAL_STORAGE")) {
273-
strlcpy(homedir, getenv("EXTERNAL_STORAGE"), sizeof(homedir));
272+
if (getenv("HOME_DIR")) {
273+
strlcpy(homedir, getenv("HOME_DIR"), sizeof(homedir));
274274
}
275275
#else
276276
#if defined(_Win32)

src/platform/android/app/src/main/assets/main.bas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,15 @@ sub loadFileList(path, byref basList)
249249
dirwalk path, "", use walker(x)
250250

251251
if (path = "/" && len(basList) == 0 && !is_sdl) then
252-
ext_storage = env("EXTERNAL_STORAGE")
252+
ext_storage = env("EXTERNAL_DIR")
253253
if (len(ext_storage) > 0) then
254254
emptyNode.name = mid(ext_storage, 2)
255255
emptyNode.dir = true
256256
emptyNode.size = ""
257257
emptyNode.mtime = 0
258258
basList << emptyNode
259259
endif
260-
int_storage = env("INTERNAL_STORAGE")
260+
int_storage = env("INTERNAL_DIR")
261261
if (len(int_storage) > 0 && int_storage != ext_storage) then
262262
emptyNode.name = mid(int_storage, 2)
263263
emptyNode.dir = true

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

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.os.Bundle;
2424
import android.os.Environment;
2525
import android.os.Handler;
26+
import android.support.annotation.NonNull;
2627
import android.support.v4.app.ActivityCompat;
2728
import android.support.v4.content.ContextCompat;
2829
import android.util.Base64;
@@ -104,6 +105,7 @@ public class MainActivity extends NativeActivity {
104105
public static native void onUnicodeChar(int ch);
105106
public static native boolean optionSelected(int index);
106107
public static native void runFile(String fileName);
108+
public static native void setenv(String name, String value);
107109

108110
public void addShortcut(final byte[] pathBytes) {
109111
final String path = getString(pathBytes);
@@ -168,7 +170,6 @@ public void onClick(DialogInterface dialog, int which) {
168170
mutex.acquire();
169171
} catch (InterruptedException e) {
170172
Log.i(TAG, "ask failed: ", e);
171-
e.printStackTrace();
172173
}
173174
Log.i(TAG, "ask result=" + result);
174175
return result.value;
@@ -229,7 +230,6 @@ public void run() {
229230
mutex.acquire();
230231
} catch (InterruptedException e) {
231232
Log.i(TAG, "getClipboardText failed: ", e);
232-
e.printStackTrace();
233233
}
234234
byte[] result;
235235
try {
@@ -241,26 +241,6 @@ public void run() {
241241
return result;
242242
}
243243

244-
public String getExternalStorage() {
245-
String result;
246-
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
247-
if (isPublicStorage(path)) {
248-
File sb = new File(path, FOLDER_NAME);
249-
if ((sb.isDirectory() && sb.canWrite()) || sb.mkdirs()) {
250-
result = path + "/" + FOLDER_NAME;
251-
} else {
252-
result = path;
253-
}
254-
} else {
255-
result = getInternalStorage();
256-
}
257-
return result;
258-
}
259-
260-
public String getInternalStorage() {
261-
return getFilesDir().getAbsolutePath();
262-
}
263-
264244
public String getIpAddress() {
265245
String result = "";
266246
try {
@@ -384,6 +364,16 @@ public boolean onPrepareOptionsMenu(Menu menu) {
384364
return super.onPrepareOptionsMenu(menu);
385365
}
386366

367+
@Override
368+
public void onRequestPermissionsResult(int requestCode,
369+
@NonNull String[] permissions,
370+
@NonNull int[] grantResults) {
371+
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
372+
if (requestCode == REQUEST_STORAGE_PERMISSION && grantResults[0] != PackageManager.PERMISSION_DENIED) {
373+
setupStorageEnvironment(true);
374+
}
375+
}
376+
387377
public void optionsBox(final String[] items) {
388378
this._options = items;
389379
runOnUiThread(new Runnable() {
@@ -628,13 +618,16 @@ private String buildTokenForm() {
628618

629619
private void checkFilePermission() {
630620
if (!permitted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
621+
setupStorageEnvironment(false);
631622
Runnable handler = new Runnable() {
632623
@Override
633624
public void run() {
634625
checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_STORAGE_PERMISSION);
635626
}
636627
};
637628
new Handler().postDelayed(handler, 250);
629+
} else {
630+
setupStorageEnvironment(true);
638631
}
639632
}
640633

@@ -700,6 +693,27 @@ private void execStream(final String line, DataInputStream inputStream) throws I
700693
runFile(outputFile.getAbsolutePath());
701694
}
702695

696+
private String getExternalStorage() {
697+
String result;
698+
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
699+
if (isPublicStorage(path)) {
700+
File sb = new File(path, FOLDER_NAME);
701+
if ((sb.isDirectory() && sb.canWrite()) || sb.mkdirs()) {
702+
result = path + "/" + FOLDER_NAME;
703+
} else {
704+
result = path;
705+
}
706+
} else {
707+
result = getInternalStorage();
708+
}
709+
return result;
710+
}
711+
712+
private String getInternalStorage() {
713+
Log.d(TAG, "internal = " + getFilesDir().getAbsolutePath());
714+
return getFilesDir().getAbsolutePath();
715+
}
716+
703717
private Map<String, String> getPostData(DataInputStream inputStream, final String line) throws IOException {
704718
int length = 0;
705719
final String lengthHeader = "content-length: ";
@@ -737,7 +751,6 @@ private String getString(final byte[] promptBytes) {
737751
return new String(promptBytes, CP1252);
738752
} catch (UnsupportedEncodingException e) {
739753
Log.i(TAG, "getString failed: ", e);
740-
e.printStackTrace();
741754
return "";
742755
}
743756
}
@@ -748,7 +761,7 @@ private boolean isPublicStorage(String dir) {
748761
result = false;
749762
} else {
750763
File file = new File(dir);
751-
result = file.isDirectory() && file.canWrite();
764+
result = file.isDirectory() && file.canRead() && file.canWrite();
752765
}
753766
return result;
754767
}
@@ -887,6 +900,13 @@ private void sendResponse(Socket socket, String content) throws IOException {
887900
out.close();
888901
}
889902

903+
private void setupStorageEnvironment(boolean external) {
904+
if (external) {
905+
setenv("EXTERNAL_DIR", getExternalStorage());
906+
}
907+
setenv("INTERNAL_DIR", getInternalStorage());
908+
}
909+
890910
private void startServer(final int socketNum, final String token) {
891911
Thread socketThread = new Thread(new Runnable() {
892912
public void run() {

src/platform/android/jni/runtime.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ extern "C" JNIEXPORT void JNICALL Java_net_sourceforge_smallbasic_MainActivity_r
147147
env->ReleaseStringUTFChars(path, fileName);
148148
}
149149

150+
extern "C" JNIEXPORT void JNICALL Java_net_sourceforge_smallbasic_MainActivity_setenv
151+
(JNIEnv *env, jclass jclazz, jstring nameString, jstring valueString) {
152+
const char *name = env->GetStringUTFChars(nameString, JNI_FALSE);
153+
const char *value = env->GetStringUTFChars(valueString, JNI_FALSE);
154+
setenv(name, value, 1);
155+
env->ReleaseStringUTFChars(nameString, name);
156+
env->ReleaseStringUTFChars(valueString, value);
157+
}
158+
150159
extern "C" JNIEXPORT void JNICALL Java_net_sourceforge_smallbasic_MainActivity_onResize
151160
(JNIEnv *env, jclass jclazz, jint width, jint height) {
152161
if (runtime != NULL && !runtime->isClosing() && runtime->isActive() && os_graphics) {
@@ -487,14 +496,13 @@ void Runtime::loadConfig() {
487496
_output->setFontSize(fontSize);
488497
_initialFontSize = _output->getFontSize();
489498

490-
String storage = getString("getInternalStorage");
491-
if (!storage.empty()) {
492-
setenv("INTERNAL_STORAGE", storage.c_str(), 1);
499+
const char *storage = getenv("EXTERNAL_DIR");
500+
if (!storage) {
501+
storage = getenv("INTERNAL_DIR");
493502
}
494-
storage = getString("getExternalStorage");
495-
if (!storage.empty()) {
496-
setenv("EXTERNAL_STORAGE", storage.c_str(), 1);
497-
chdir(storage.c_str());
503+
if (storage) {
504+
setenv("HOME_DIR", storage, 1);
505+
chdir(storage);
498506
}
499507
if (loadSettings(settings)) {
500508
String *s = settings.get(FONT_SCALE_KEY);

0 commit comments

Comments
 (0)