Android: Add simple launcher

This adds a minimal launcher activity for the android app, which does
the followings:

- Opens a file chooser to let user select a zip file
- Extracts the zip into the app's internal storage
- Launches the SDL activity
This commit is contained in:
kichikuou
2019-12-31 21:17:39 +09:00
parent e3c812da3a
commit 358ea7b1a9
6 changed files with 146 additions and 9 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ android {
compileSdkVersion 26
defaultConfig {
if (buildAsApplication) {
applicationId "org.libsdl.app"
applicationId "io.github.kichikuou.system3"
}
minSdkVersion 16
targetSdkVersion 26
+11 -7
View File
@@ -3,7 +3,7 @@
com.gamemaker.game
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.libsdl.app"
package="io.github.kichikuou.system3"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto">
@@ -51,17 +51,21 @@
<!-- Example of setting SDL hints from AndroidManifest.xml:
<meta-data android:name="SDL_ENV.SDL_ACCELEROMETER_AS_JOYSTICK" android:value="0"/>
-->
<activity android:name="SDLActivity"
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameActivity"
android:label="@string/app_name"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance"
android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Drop file event -->
<!--
<intent-filter>
@@ -0,0 +1,35 @@
/* Copyright (C) 2019 <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package io.github.kichikuou.system3;
import org.libsdl.app.SDLActivity;
public class GameActivity extends SDLActivity {
@Override
protected String[] getLibraries() {
return new String[] {
"hidapi",
"SDL2",
// "SDL2_image",
// "SDL2_mixer",
// "SDL2_net",
// "SDL2_ttf",
"system3"
};
}
}
@@ -0,0 +1,91 @@
/* Copyright (C) 2019 <KichikuouChrome@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package io.github.kichikuou.system3;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class LauncherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("application/zip");
startActivityForResult(Intent.createChooser(i, "Choose a file"), 0);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK)
extractFiles(data.getData(), getApplicationContext().getFilesDir());
startGame();
}
boolean extractFiles(Uri zipUri, File outDir) {
try {
InputStream input = getContentResolver().openInputStream(zipUri);
if (input == null)
return false;
ZipInputStream zip;
if (Build.VERSION.SDK_INT >= 24)
zip = new ZipInputStream(new BufferedInputStream(input), Charset.forName("Shift_JIS"));
else
zip = new ZipInputStream(new BufferedInputStream(input));
byte[] buffer = new byte[4096];
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
File entryName = new File(zipEntry.getName());
if (zipEntry.isDirectory())
continue;
Log.d("Extracting", entryName.toString());
File outFile = new File(outDir, entryName.getName());
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outFile));
int size;
while ((size = zip.read(buffer)) > 0) {
output.write(buffer, 0, size);
}
output.close();
}
zip.close();
return true;
} catch (IOException e) {
Log.e("launcher", "Error while extracting zip", e);
return false;
}
}
void startGame() {
Intent i = new Intent();
i.setClass(getApplicationContext(), GameActivity.class);
startActivity(i);
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">Game</string>
<string name="app_name">system3</string>
</resources>
+7
View File
@@ -1,6 +1,7 @@
#include "common.h"
#include "fileio.h"
#include "sys/nact.h"
#include <unistd.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
@@ -14,6 +15,12 @@ SDL_Window* g_window;
int main(int argc, char *argv[])
{
#ifdef __ANDROID__
// Chdir to the internal storage path where the launcher should have placed
// game files.
chdir(SDL_AndroidGetInternalStoragePath());
#endif
SDL_Init(SDL_INIT_VIDEO);
// ウィンドウ表示