From 5616b6017d42dda1d8115d6e42da3fa89a39123b Mon Sep 17 00:00:00 2001 From: mittorn Date: Fri, 16 Sep 2016 16:01:54 +0000 Subject: [PATCH] Update java launcher --- android/build-manual.sh | 2 +- .../src/in/celest/xash3d/InstallReceiver.java | 89 ++++++++++++++++++- .../in/celest/xash3d/LauncherActivity.java | 57 ------------ 3 files changed, 88 insertions(+), 60 deletions(-) diff --git a/android/build-manual.sh b/android/build-manual.sh index db7f32a4..c1d3fa50 100644 --- a/android/build-manual.sh +++ b/android/build-manual.sh @@ -7,7 +7,7 @@ mkdir bin mkdir bin/classes mkdir assets/ $AAPT package -M AndroidManifest.xml -m -S res -I $ANDROID_JAR -$JAVA_HOME/bin/javac -d bin/classes -s bin/classes -cp $ANDROID_JAR src/in/celest/xash3d/* +$JAVA_HOME/bin/javac -d bin/classes -s bin/classes -cp $ANDROID_JAR src/in/celest/xash3d/*.java $DX --dex --output=bin/classes.dex bin/classes/ $AAPT package -f -M AndroidManifest.xml -S res -I $ANDROID_JAR -F bin/$NAME.apk.unaligned python2 makepak.py pak/ assets/extras.pak diff --git a/android/src/in/celest/xash3d/InstallReceiver.java b/android/src/in/celest/xash3d/InstallReceiver.java index 7aee5cf3..9d61ea00 100644 --- a/android/src/in/celest/xash3d/InstallReceiver.java +++ b/android/src/in/celest/xash3d/InstallReceiver.java @@ -3,12 +3,97 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; +import android.content.Intent; +import java.io.FileOutputStream; +import java.io.File; +import java.io.InputStream; +import java.lang.reflect.Method; +import android.content.SharedPreferences; +import android.util.Log; public class InstallReceiver extends BroadcastReceiver { private static final String TAG = "MOD_LAUNCHER"; @Override public void onReceive(Context context, Intent arg1) { - Log.d( TAG, "Install received, extracting PAK" ); - LauncherActivity.extractPAK( context, true ); + String pkgname = arg1.getData().getEncodedSchemeSpecificPart(); + Log.d( TAG, "Install received, package " + pkgname ); + if( context.getPackageName().equals(pkgname) ) + extractPAK(context, true); } + public static SharedPreferences mPref = null; + private static final int PAK_VERSION = 2; + private static int chmod(String path, int mode) { + int ret = -1; + try + { + ret = Runtime.getRuntime().exec("chmod " + Integer.toOctalString(mode) + " " + path).waitFor(); + Log.d(TAG, "chmod " + Integer.toOctalString(mode) + " " + path + ": " + ret ); + } + catch(Exception e) + { + ret = -1; + Log.d(TAG, "chmod: Runtime not worked: " + e.toString() ); + } + try + { + Class fileUtils = Class.forName("android.os.FileUtils"); + Method setPermissions = fileUtils.getMethod("setPermissions", + String.class, int.class, int.class, int.class); + ret = (Integer) setPermissions.invoke(null, path, + mode, -1, -1); + } + catch(Exception e) + { + ret = -1; + Log.d(TAG, "chmod: FileUtils not worked: " + e.toString() ); + } + return ret; + } + + private static void extractFile(Context context, String path) { + try + { + InputStream is = null; + FileOutputStream os = null; + is = context.getAssets().open(path); + File out = new File(context.getFilesDir().getPath()+'/'+path); + out.getParentFile().mkdirs(); + chmod( out.getParent(), 0777 ); + os = new FileOutputStream(out); + byte[] buffer = new byte[1024]; + int length; + while ((length = is.read(buffer)) > 0) { + os.write(buffer, 0, length); + } + os.close(); + is.close(); + chmod( context.getFilesDir().getPath()+'/'+path, 0777 ); + } catch( Exception e ) + { + Log.e( TAG, "Failed to extract file:" + e.toString() ); + e.printStackTrace(); + } + + } + + public static synchronized void extractPAK(Context context, Boolean force) { + InputStream is = null; + FileOutputStream os = null; + try { + if( mPref == null ) + mPref = context.getSharedPreferences("mod", 0); + synchronized( mPref ) + { + if( mPref.getInt( "pakversion", 0 ) == PAK_VERSION && !force ) + return; + extractFile(context, "extras.pak"); + SharedPreferences.Editor editor = mPref.edit(); + editor.putInt( "pakversion", PAK_VERSION ); + editor.commit(); + } + } catch( Exception e ) + { + Log.e( TAG, "Failed to extract PAK:" + e.toString() ); + } + } } diff --git a/android/src/in/celest/xash3d/LauncherActivity.java b/android/src/in/celest/xash3d/LauncherActivity.java index 2271a7c1..73da73a8 100644 --- a/android/src/in/celest/xash3d/LauncherActivity.java +++ b/android/src/in/celest/xash3d/LauncherActivity.java @@ -16,10 +16,6 @@ import android.widget.TextView; import android.content.ComponentName; import android.content.pm.PackageManager; import android.content.SharedPreferences; -import java.lang.reflect.Method; -import java.io.FileOutputStream; -import java.io.File; -import java.io.InputStream; import android.content.Context; import android.util.Log; @@ -81,59 +77,6 @@ public class LauncherActivity extends Activity { intent.putExtra("pakfile", getFilesDir().getAbsolutePath() + "/extras.pak" ); startActivity(intent); } - private static int chmod(String path, int mode) { - int ret = -1; - try - { - ret = Runtime.getRuntime().exec("chmod " + Integer.toOctalString(mode) + " " + path).waitFor(); - Log.d(TAG, "chmod " + Integer.toOctalString(mode) + " " + path + ": " + ret ); - } - catch(Exception e) - { - ret = -1; - Log.d(TAG, "chmod: Runtime not worked: " + e.toString() ); - } - try - { - Class fileUtils = Class.forName("android.os.FileUtils"); - Method setPermissions = fileUtils.getMethod("setPermissions", - String.class, int.class, int.class, int.class); - ret = (Integer) setPermissions.invoke(null, path, - mode, -1, -1); - } - catch(Exception e) - { - ret = -1; - Log.d(TAG, "chmod: FileUtils not worked: " + e.toString() ); - } - return ret; - } - - private static void extractFile(Context context, String path) { - try - { - InputStream is = null; - FileOutputStream os = null; - is = context.getAssets().open(path); - File out = new File(context.getFilesDir().getPath()+'/'+path); - out.getParentFile().mkdirs(); - chmod( out.getParent(), 0777 ); - os = new FileOutputStream(out); - byte[] buffer = new byte[1024]; - int length; - while ((length = is.read(buffer)) > 0) { - os.write(buffer, 0, length); - } - os.close(); - is.close(); - chmod( context.getFilesDir().getPath()+'/'+path, 0777 ); - } catch( Exception e ) - { - Log.e( TAG, "Failed to extract file:" + e.toString() ); - e.printStackTrace(); - } - - } public static void extractPAK(Context context, Boolean force) { if(isExtracting)