store and install assets on android

This commit is contained in:
R4SAS 2018-06-27 23:20:02 +03:00 committed by R4SAS
parent fc4787da4e
commit db5b45222a
2 changed files with 284 additions and 209 deletions

View file

@ -37,6 +37,7 @@ android {
java.srcDirs = ['src'] java.srcDirs = ['src']
res.srcDirs = ['res'] res.srcDirs = ['res']
jniLibs.srcDirs = ['libs'] jniLibs.srcDirs = ['libs']
assets.srcDirs = ['assets']
} }
} }
signingConfigs { signingConfigs {

View file

@ -1,5 +1,10 @@
package org.purplei2p.i2pd; package org.purplei2p.i2pd;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Timer; import java.util.Timer;
@ -10,7 +15,9 @@ import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.content.res.AssetManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
@ -36,7 +43,7 @@ public class I2PDActivity extends Activity {
@Override @Override
public void run() { public void run() {
try { try {
if(textView==null)return; if(textView==null) return;
Throwable tr = daemon.getLastThrowable(); Throwable tr = daemon.getLastThrowable();
if(tr!=null) { if(tr!=null) {
textView.setText(throwableToString(tr)); textView.setText(throwableToString(tr));
@ -72,12 +79,18 @@ public class I2PDActivity extends Activity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// copy assets
copyAsset("certificates");
copyAsset("i2pd.conf");
copyAsset("subsciptions.txt");
copyAsset("tunnels.conf");
textView = new TextView(this); textView = new TextView(this);
setContentView(textView); setContentView(textView);
daemon.addStateChangeListener(daemonStateUpdatedListener); daemon.addStateChangeListener(daemonStateUpdatedListener);
daemonStateUpdatedListener.daemonStateUpdate(); daemonStateUpdatedListener.daemonStateUpdate();
//set the app be foreground // set the app be foreground
doBindService(); doBindService();
final Timer gracefulQuitTimer = getGracefulQuitTimer(); final Timer gracefulQuitTimer = getGracefulQuitTimer();
@ -119,7 +132,7 @@ public class I2PDActivity extends Activity {
return sw.toString(); return sw.toString();
} }
// private LocalService mBoundService; // private LocalService mBoundService;
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
@ -128,11 +141,11 @@ public class I2PDActivity extends Activity {
// interact with the service. Because we have bound to a explicit // interact with the service. Because we have bound to a explicit
// service that we know is running in our own process, we can // service that we know is running in our own process, we can
// cast its IBinder to a concrete class and directly access it. // cast its IBinder to a concrete class and directly access it.
// mBoundService = ((LocalService.LocalBinder)service).getService(); // mBoundService = ((LocalService.LocalBinder)service).getService();
// Tell the user about this for our demo. // Tell the user about this for our demo.
// Toast.makeText(Binding.this, R.string.local_service_connected, // Toast.makeText(Binding.this, R.string.local_service_connected,
// Toast.LENGTH_SHORT).show(); // Toast.LENGTH_SHORT).show();
} }
public void onServiceDisconnected(ComponentName className) { public void onServiceDisconnected(ComponentName className) {
@ -140,9 +153,9 @@ public class I2PDActivity extends Activity {
// unexpectedly disconnected -- that is, its process crashed. // unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never // Because it is running in our same process, we should never
// see this happen. // see this happen.
// mBoundService = null; // mBoundService = null;
// Toast.makeText(Binding.this, R.string.local_service_disconnected, // Toast.makeText(Binding.this, R.string.local_service_disconnected,
// Toast.LENGTH_SHORT).show(); // Toast.LENGTH_SHORT).show();
} }
}; };
@ -282,4 +295,65 @@ public class I2PDActivity extends Activity {
private static void setGracefulQuitTimer(Timer gracefulQuitTimer) { private static void setGracefulQuitTimer(Timer gracefulQuitTimer) {
I2PDActivity.gracefulQuitTimer = gracefulQuitTimer; I2PDActivity.gracefulQuitTimer = gracefulQuitTimer;
} }
/**
* Copy the asset at the specified path to this app's data directory. If the
* asset is a directory, its contents are also copied.
*
* @param path
* Path to asset, relative to app's assets directory.
*/
private void copyAsset(String path) {
AssetManager manager = getAssets();
// If we have a directory, we make it and recurse. If a file, we copy its
// contents.
try {
String[] contents = manager.list(path);
// The documentation suggests that list throws an IOException, but doesn't
// say under what conditions. It'd be nice if it did so when the path was
// to a file. That doesn't appear to be the case. If the returned array is
// null or has 0 length, we assume the path is to a file. This means empty
// directories will get turned into files.
if (contents == null || contents.length == 0)
throw new IOException();
// Make the directory.
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/i2pd/", path);
dir.mkdirs();
// Recurse on the contents.
for (String entry : contents) {
copyAsset(path + "/" + entry);
}
} catch (IOException e) {
copyFileAsset(path);
}
}
/**
* Copy the asset file specified by path to app's data directory. Assumes
* parent directories have already been created.
*
* @param path
* Path to asset, relative to app's assets directory.
*/
private void copyFileAsset(String path) {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/i2pd/", path);
try {
InputStream in = getAssets().open(path);
OutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int read = in.read(buffer);
while (read != -1) {
out.write(buffer, 0, read);
read = in.read(buffer);
}
out.close();
in.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
}
} }