Browse Source

Android: improved setting (#37)

The start sequence is optimized on Android.
Now, the game can launch directly after user saved the settings.
And the screen orientation is not changed when launching the setting page.
Yihua LOU 7 years ago
parent
commit
085c4c2d2f

+ 8 - 4
android/app/src/main/AndroidManifest.xml

@@ -23,16 +23,20 @@
         android:hardwareAccelerated="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name">
+        <activity
+            android:name=".MainActivity"
+            android:label="@string/app_name">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
         <activity
             android:name=".PalActivity"
             android:configChanges="keyboardHidden|orientation"
             android:label="@string/app_name"
             android:screenOrientation="landscape"
             android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
-            <intent-filter>
-                <action android:name="android.intent.action.MAIN" />
-                <category android:name="android.intent.category.LAUNCHER" />
-            </intent-filter>
         </activity>
         <activity
             android:name=".SettingsActivity"

+ 4 - 3
android/app/src/main/cpp/android_jni.cpp

@@ -53,12 +53,12 @@ static JNIEnv* getJNIEnv()
 }
 
 /*
- * Class:     io_github_sdlpal_PalActivity
+ * Class:     io_github_sdlpal_MainActivity
  * Method:    setAppPath
- * Signature: (Ljava/lang/String;Ljava/lang/String;)V
+ * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
  */
 EXTERN_C_LINKAGE
-JNIEXPORT void JNICALL Java_io_github_sdlpal_PalActivity_setAppPath(JNIEnv *env, jclass cls, jstring base_path, jstring data_path, jstring cache_path)
+JNIEXPORT void JNICALL Java_io_github_sdlpal_MainActivity_setAppPath(JNIEnv *env, jclass cls, jstring base_path, jstring data_path, jstring cache_path)
 {
     g_basepath = jstring_to_utf8(env, base_path);
     g_configpath = jstring_to_utf8(env, data_path);
@@ -299,4 +299,5 @@ UTIL_Platform_Quit(
 )
 {
     unlink((g_cachepath + "running").c_str());
+    exit(0);
 }

+ 51 - 0
android/app/src/main/java/io/github/sdlpal/MainActivity.java

@@ -0,0 +1,51 @@
+package io.github.sdlpal;
+
+import android.content.Intent;
+import android.os.*;
+import android.support.v7.app.AppCompatActivity;
+import java.io.*;
+
+public class MainActivity extends AppCompatActivity {
+    private static final String TAG = "sdlpal-debug";
+
+    public static native void setAppPath(String basepath, String datapath, String cachepath);
+
+    public static boolean crashed = false;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        System.loadLibrary("SDL2");
+        System.loadLibrary("main");
+
+        String dataPath = getApplicationContext().getFilesDir().getPath();
+        String cachePath = getApplicationContext().getCacheDir().getPath();
+        String sdcardState = Environment.getExternalStorageState();
+        if (sdcardState.equals(Environment.MEDIA_MOUNTED)){
+            setAppPath(Environment.getExternalStorageDirectory().getPath() + "/sdlpal/", dataPath, cachePath);
+        } else {
+            setAppPath("/sdcard/sdlpal/", dataPath, cachePath);
+        }
+
+        File runningFile = new File(cachePath + "/running");
+        crashed = runningFile.exists();
+
+        Intent intent;
+        if (SettingsActivity.loadConfigFile() || crashed) {
+            runningFile.delete();
+
+            intent = new Intent(this, SettingsActivity.class);
+        } else {
+            intent = new Intent(this, PalActivity.class);
+        }
+        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+        startActivity(intent);
+        finish();
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+    }
+}

+ 0 - 22
android/app/src/main/java/io/github/sdlpal/PalActivity.java

@@ -13,7 +13,6 @@ public class PalActivity extends SDLActivity {
     private static final String TAG = "sdlpal-debug";
     private static MediaPlayer mediaPlayer;
 
-    public static native void setAppPath(String basepath, String datapath, String cachepath);
     public static native void setScreenSize(int width, int height);
 
     public static boolean crashed = false;
@@ -36,30 +35,9 @@ public class PalActivity extends SDLActivity {
     public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);
 
-        String dataPath = getApplicationContext().getFilesDir().getPath();
-        String cachePath = getApplicationContext().getCacheDir().getPath();
-        String sdcardState = Environment.getExternalStorageState();
-        if (sdcardState.equals(Environment.MEDIA_MOUNTED)){
-            setAppPath(Environment.getExternalStorageDirectory().getPath() + "/sdlpal/", dataPath, cachePath);
-        } else {
-            setAppPath("/sdcard/sdlpal/", dataPath, cachePath);
-        }
-
         DisplayMetrics metrics = new DisplayMetrics();
         getWindowManager().getDefaultDisplay().getMetrics(metrics);
         setScreenSize(metrics.widthPixels, metrics.heightPixels);
-
-        File runningFile = new File(cachePath + "/running");
-        crashed = runningFile.exists();
-
-        if (SettingsActivity.loadConfigFile() || crashed) {
-            runningFile.delete();
-
-            Intent intent = new Intent(this, SettingsActivity.class);
-            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
-            startActivity(intent);
-            finish();
-        }
     }
 
     @Override

+ 6 - 1
android/app/src/main/java/io/github/sdlpal/SettingsActivity.java

@@ -1,6 +1,7 @@
 package io.github.sdlpal;
 
 import android.content.DialogInterface;
+import android.content.Intent;
 import android.os.Bundle;
 import android.os.Environment;
 import android.support.v7.app.AppCompatActivity;
@@ -126,7 +127,11 @@ public class SettingsActivity extends AppCompatActivity {
                 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialogInterface, int i) {
-                        System.exit(0);
+                        Intent intent = new Intent(mInstance, PalActivity.class);
+                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+                        startActivity(intent);
+                        finish();
+
                     }
                 });
                 builder.create().show();

+ 1 - 1
android/app/src/main/res/values/strings.xml

@@ -19,7 +19,7 @@
     <string name="action_default">Default</string>
     <string name="action_finish">Finish</string>
     <string name="action_restore">Reset</string>
-    <string name="msg_exit">Settings have been saved and the game will be started on next launch. You can use the main menu option inside the game to return to this page.</string>
+    <string name="msg_exit">Settings have been saved and the game will be started directly on next launch. You can use the main menu option inside the game to return to this page.</string>
     <string name="msg_crash">The program is abnormally terminated last time. The setting page has been launched for you. Please check if there are any incorrect settings.</string>
     <string name="msg_empty">The game resource folder must be specified!</string>
     <string name="label_loglevel">Logging level</string>