Browse Source

temporarily hackfix failure to save game on some phones (one of the most frequently reported issues at google play). Not tested.

Wei Mingzhi 1 year ago
parent
commit
96721fa1f1

+ 21 - 0
android/app/src/main/cpp/android_jni.cpp

@@ -16,6 +16,8 @@
 #include "util.h"
 #include "generated.h"
 
+#include <unistd.h>
+#include <sys/stat.h>
 #include <string>
 
 static std::string g_basepath, g_configpath, g_cachepath, g_midipath;
@@ -204,6 +206,25 @@ JNIEXPORT jboolean JNICALL Java_com_sdlpal_sdlpal_SettingsActivity_checkResource
     )) ? JNI_FALSE : JNI_TRUE;
 }
 
+/*
+ * Class:     com_sdlpal_sdlpal_SettingsActivity
+ * Method:    isDirWritable
+ * Signature: (Ljava/lang/String;)Z
+ */
+EXTERN_C_LINKAGE
+JNIEXPORT jboolean JNICALL Java_com_sdlpal_sdlpal_SettingsActivity_isDirWritable(JNIEnv *env, jclass cls, jstring path)
+{
+    std::string str_path = jstring_to_utf8(env, path);
+    mkdir(str_path.c_str(), 0755);
+    str_path += "/test";
+    FILE *fp = fopen(str_path.c_str(), "wb");
+    if (fp == NULL) {
+        return JNI_FALSE;
+    }
+    fclose(fp);
+    unlink(str_path.c_str());
+    return JNI_TRUE;
+}
 
 EXTERN_C_LINKAGE
 void* JNI_mediaplayer_load(const char *filename)

+ 1 - 1
android/app/src/main/java/com/sdlpal/sdlpal/PalActivity.java

@@ -40,7 +40,7 @@ public class PalActivity extends SDLActivity {
     }
 
     @Override
-    public void onCreate(Bundle savedInstanceState) {  
+    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
         DisplayMetrics metrics = new DisplayMetrics();

+ 12 - 1
android/app/src/main/java/com/sdlpal/sdlpal/SettingsActivity.java

@@ -39,6 +39,7 @@ public class SettingsActivity extends AppCompatActivity {
     public static native boolean setConfigString(String item, String value);
     public static native boolean checkResourceFiles(String path, String msgfile);
     public static native String getGitRevision();
+    public static native boolean isDirWritable(String path);
 
     private static final String KeepAspectRatio = "KeepAspectRatio";
     private static final String AspectRatio = "AspectRatio";
@@ -367,7 +368,17 @@ public class SettingsActivity extends AppCompatActivity {
         setConfigInt(ResampleQuality, ((SeekBar)findViewById(R.id.sbQuality)).getProgress());
 
         setConfigString(GamePath, ((EditText)findViewById(R.id.edFolder)).getText().toString());
-        setConfigString(SavePath, ((EditText)findViewById(R.id.edFolder)).getText().toString());
+        if (isDirWritable(((EditText)findViewById(R.id.edFolder)).getText().toString())) {
+            setConfigString(SavePath, ((EditText)findViewById(R.id.edFolder)).getText().toString());
+        } else {
+            String savePath = Environment.getExternalStorageDirectory().getPath() + "/sdlpal/";
+            if (isDirWritable(savePath)) {
+                setConfigString(SavePath, savePath);
+            } else {
+                savePath = getApplicationContext().getFilesDir().getPath();
+                setConfigString(SavePath, savePath);
+            }
+        }
         setConfigString(MessageFileName, ((SwitchCompat)findViewById(R.id.swMsgFile)).isChecked() ? ((EditText)findViewById(R.id.edMsgFile)).getText().toString() : null);
         setConfigString(FontFileName, ((SwitchCompat)findViewById(R.id.swFontFile)).isChecked() ? ((EditText)findViewById(R.id.edFontFile)).getText().toString() : null);
         setConfigString(LogFileName, ((SwitchCompat)findViewById(R.id.swLogFile)).isChecked() ? ((EditText)findViewById(R.id.edLogFile)).getText().toString() : null);