mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 21:37:17 +01:00
store and install assets on android
This commit is contained in:
parent
fc4787da4e
commit
db5b45222a
|
@ -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 {
|
||||||
|
|
|
@ -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;
|
||||||
|
@ -19,24 +26,24 @@ import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
public class I2PDActivity extends Activity {
|
public class I2PDActivity extends Activity {
|
||||||
private static final String TAG = "i2pdActvt";
|
private static final String TAG = "i2pdActvt";
|
||||||
public static final int GRACEFUL_DELAY_MILLIS = 10 * 60 * 1000;
|
public static final int GRACEFUL_DELAY_MILLIS = 10 * 60 * 1000;
|
||||||
|
|
||||||
private TextView textView;
|
private TextView textView;
|
||||||
|
|
||||||
private static final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
private static final DaemonSingleton daemon = DaemonSingleton.getInstance();
|
||||||
|
|
||||||
private final DaemonSingleton.StateUpdateListener daemonStateUpdatedListener =
|
private final DaemonSingleton.StateUpdateListener daemonStateUpdatedListener =
|
||||||
new DaemonSingleton.StateUpdateListener() {
|
new DaemonSingleton.StateUpdateListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void daemonStateUpdate() {
|
public void daemonStateUpdate() {
|
||||||
runOnUiThread(new Runnable(){
|
runOnUiThread(new Runnable(){
|
||||||
|
|
||||||
@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));
|
||||||
|
@ -44,242 +51,309 @@ public class I2PDActivity extends Activity {
|
||||||
}
|
}
|
||||||
DaemonSingleton.State state = daemon.getState();
|
DaemonSingleton.State state = daemon.getState();
|
||||||
textView.setText(
|
textView.setText(
|
||||||
String.valueOf(state)+
|
String.valueOf(state)+
|
||||||
(DaemonSingleton.State.startFailed.equals(state)?": "+daemon.getDaemonStartResult():"")+
|
(DaemonSingleton.State.startFailed.equals(state)?": "+daemon.getDaemonStartResult():"")+
|
||||||
(DaemonSingleton.State.gracefulShutdownInProgress.equals(state)?": "+formatGraceTimeRemaining()+" "+getText(R.string.remaining):"")
|
(DaemonSingleton.State.gracefulShutdownInProgress.equals(state)?": "+formatGraceTimeRemaining()+" "+getText(R.string.remaining):"")
|
||||||
);
|
);
|
||||||
} catch (Throwable tr) {
|
} catch (Throwable tr) {
|
||||||
Log.e(TAG,"error ignored",tr);
|
Log.e(TAG,"error ignored",tr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private static volatile long graceStartedMillis;
|
private static volatile long graceStartedMillis;
|
||||||
private static final Object graceStartedMillis_LOCK=new Object();
|
private static final Object graceStartedMillis_LOCK=new Object();
|
||||||
|
|
||||||
private static String formatGraceTimeRemaining() {
|
private static String formatGraceTimeRemaining() {
|
||||||
long remainingSeconds;
|
long remainingSeconds;
|
||||||
synchronized (graceStartedMillis_LOCK){
|
synchronized (graceStartedMillis_LOCK){
|
||||||
remainingSeconds=Math.round(Math.max(0,graceStartedMillis+GRACEFUL_DELAY_MILLIS-System.currentTimeMillis())/1000.0D);
|
remainingSeconds=Math.round(Math.max(0,graceStartedMillis+GRACEFUL_DELAY_MILLIS-System.currentTimeMillis())/1000.0D);
|
||||||
}
|
}
|
||||||
long remainingMinutes=(long)Math.floor(remainingSeconds/60.0D);
|
long remainingMinutes=(long)Math.floor(remainingSeconds/60.0D);
|
||||||
long remSec=remainingSeconds-remainingMinutes*60;
|
long remSec=remainingSeconds-remainingMinutes*60;
|
||||||
return remainingMinutes+":"+(remSec/10)+remSec%10;
|
return remainingMinutes+":"+(remSec/10)+remSec%10;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
textView = new TextView(this);
|
// copy assets
|
||||||
setContentView(textView);
|
copyAsset("certificates");
|
||||||
daemon.addStateChangeListener(daemonStateUpdatedListener);
|
copyAsset("i2pd.conf");
|
||||||
daemonStateUpdatedListener.daemonStateUpdate();
|
copyAsset("subsciptions.txt");
|
||||||
|
copyAsset("tunnels.conf");
|
||||||
//set the app be foreground
|
|
||||||
doBindService();
|
textView = new TextView(this);
|
||||||
|
setContentView(textView);
|
||||||
final Timer gracefulQuitTimer = getGracefulQuitTimer();
|
daemon.addStateChangeListener(daemonStateUpdatedListener);
|
||||||
if(gracefulQuitTimer!=null){
|
daemonStateUpdatedListener.daemonStateUpdate();
|
||||||
long gracefulStopAtMillis;
|
|
||||||
synchronized (graceStartedMillis_LOCK) {
|
// set the app be foreground
|
||||||
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS;
|
doBindService();
|
||||||
}
|
|
||||||
rescheduleGraceStop(gracefulQuitTimer, gracefulStopAtMillis);
|
final Timer gracefulQuitTimer = getGracefulQuitTimer();
|
||||||
}
|
if(gracefulQuitTimer!=null){
|
||||||
}
|
long gracefulStopAtMillis;
|
||||||
|
synchronized (graceStartedMillis_LOCK) {
|
||||||
@Override
|
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS;
|
||||||
|
}
|
||||||
|
rescheduleGraceStop(gracefulQuitTimer, gracefulStopAtMillis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
textView = null;
|
textView = null;
|
||||||
daemon.removeStateChangeListener(daemonStateUpdatedListener);
|
daemon.removeStateChangeListener(daemonStateUpdatedListener);
|
||||||
//cancelGracefulStop();
|
//cancelGracefulStop();
|
||||||
try{
|
try{
|
||||||
doUnbindService();
|
doUnbindService();
|
||||||
}catch(Throwable tr){
|
}catch(Throwable tr){
|
||||||
Log.e(TAG, "", tr);
|
Log.e(TAG, "", tr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void cancelGracefulStop() {
|
private static void cancelGracefulStop() {
|
||||||
Timer gracefulQuitTimer = getGracefulQuitTimer();
|
Timer gracefulQuitTimer = getGracefulQuitTimer();
|
||||||
if(gracefulQuitTimer!=null) {
|
if(gracefulQuitTimer!=null) {
|
||||||
gracefulQuitTimer.cancel();
|
gracefulQuitTimer.cancel();
|
||||||
setGracefulQuitTimer(null);
|
setGracefulQuitTimer(null);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private CharSequence throwableToString(Throwable tr) {
|
|
||||||
StringWriter sw = new StringWriter(8192);
|
|
||||||
PrintWriter pw = new PrintWriter(sw);
|
|
||||||
tr.printStackTrace(pw);
|
|
||||||
pw.close();
|
|
||||||
return sw.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// private LocalService mBoundService;
|
private CharSequence throwableToString(Throwable tr) {
|
||||||
|
StringWriter sw = new StringWriter(8192);
|
||||||
private ServiceConnection mConnection = new ServiceConnection() {
|
PrintWriter pw = new PrintWriter(sw);
|
||||||
public void onServiceConnected(ComponentName className, IBinder service) {
|
tr.printStackTrace(pw);
|
||||||
// This is called when the connection with the service has been
|
pw.close();
|
||||||
// established, giving us the service object we can use to
|
return sw.toString();
|
||||||
// interact with the service. Because we have bound to a explicit
|
}
|
||||||
// service that we know is running in our own process, we can
|
|
||||||
// cast its IBinder to a concrete class and directly access it.
|
// private LocalService mBoundService;
|
||||||
// mBoundService = ((LocalService.LocalBinder)service).getService();
|
|
||||||
|
private ServiceConnection mConnection = new ServiceConnection() {
|
||||||
// Tell the user about this for our demo.
|
public void onServiceConnected(ComponentName className, IBinder service) {
|
||||||
// Toast.makeText(Binding.this, R.string.local_service_connected,
|
// This is called when the connection with the service has been
|
||||||
// Toast.LENGTH_SHORT).show();
|
// established, giving us the service object we can use to
|
||||||
}
|
// interact with the service. Because we have bound to a explicit
|
||||||
|
// service that we know is running in our own process, we can
|
||||||
public void onServiceDisconnected(ComponentName className) {
|
// cast its IBinder to a concrete class and directly access it.
|
||||||
// This is called when the connection with the service has been
|
// mBoundService = ((LocalService.LocalBinder)service).getService();
|
||||||
// unexpectedly disconnected -- that is, its process crashed.
|
|
||||||
// Because it is running in our same process, we should never
|
// Tell the user about this for our demo.
|
||||||
// see this happen.
|
// Toast.makeText(Binding.this, R.string.local_service_connected,
|
||||||
// mBoundService = null;
|
// Toast.LENGTH_SHORT).show();
|
||||||
// Toast.makeText(Binding.this, R.string.local_service_disconnected,
|
}
|
||||||
// Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
public void onServiceDisconnected(ComponentName className) {
|
||||||
};
|
// This is called when the connection with the service has been
|
||||||
|
// unexpectedly disconnected -- that is, its process crashed.
|
||||||
|
// Because it is running in our same process, we should never
|
||||||
private static volatile boolean mIsBound;
|
// see this happen.
|
||||||
|
// mBoundService = null;
|
||||||
private void doBindService() {
|
// Toast.makeText(Binding.this, R.string.local_service_disconnected,
|
||||||
synchronized (I2PDActivity.class) {
|
// Toast.LENGTH_SHORT).show();
|
||||||
if (mIsBound) return;
|
}
|
||||||
// Establish a connection with the service. We use an explicit
|
};
|
||||||
// class name because we want a specific service implementation that
|
|
||||||
// we know will be running in our own process (and thus won't be
|
|
||||||
// supporting component replacement by other applications).
|
private static volatile boolean mIsBound;
|
||||||
bindService(new Intent(this, ForegroundService.class), mConnection, Context.BIND_AUTO_CREATE);
|
|
||||||
mIsBound = true;
|
private void doBindService() {
|
||||||
}
|
synchronized (I2PDActivity.class) {
|
||||||
}
|
if (mIsBound) return;
|
||||||
|
// Establish a connection with the service. We use an explicit
|
||||||
private void doUnbindService() {
|
// class name because we want a specific service implementation that
|
||||||
synchronized (I2PDActivity.class) {
|
// we know will be running in our own process (and thus won't be
|
||||||
if (mIsBound) {
|
// supporting component replacement by other applications).
|
||||||
// Detach our existing connection.
|
bindService(new Intent(this, ForegroundService.class), mConnection, Context.BIND_AUTO_CREATE);
|
||||||
unbindService(mConnection);
|
mIsBound = true;
|
||||||
mIsBound = false;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
private void doUnbindService() {
|
||||||
|
synchronized (I2PDActivity.class) {
|
||||||
|
if (mIsBound) {
|
||||||
|
// Detach our existing connection.
|
||||||
|
unbindService(mConnection);
|
||||||
|
mIsBound = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
// Inflate the menu; this adds items to the action bar if it is present.
|
// Inflate the menu; this adds items to the action bar if it is present.
|
||||||
getMenuInflater().inflate(R.menu.options_main, menu);
|
getMenuInflater().inflate(R.menu.options_main, menu);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
// Handle action bar item clicks here. The action bar will
|
// Handle action bar item clicks here. The action bar will
|
||||||
// automatically handle clicks on the Home/Up button, so long
|
// automatically handle clicks on the Home/Up button, so long
|
||||||
// as you specify a parent activity in AndroidManifest.xml.
|
// as you specify a parent activity in AndroidManifest.xml.
|
||||||
int id = item.getItemId();
|
int id = item.getItemId();
|
||||||
|
|
||||||
switch(id){
|
switch(id){
|
||||||
case R.id.action_stop:
|
case R.id.action_stop:
|
||||||
i2pdStop();
|
i2pdStop();
|
||||||
return true;
|
return true;
|
||||||
case R.id.action_graceful_stop:
|
case R.id.action_graceful_stop:
|
||||||
i2pdGracefulStop();
|
i2pdGracefulStop();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void i2pdStop() {
|
private void i2pdStop() {
|
||||||
cancelGracefulStop();
|
cancelGracefulStop();
|
||||||
new Thread(new Runnable(){
|
new Thread(new Runnable(){
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.d(TAG, "stopping");
|
Log.d(TAG, "stopping");
|
||||||
try{
|
try{
|
||||||
daemon.stopDaemon();
|
daemon.stopDaemon();
|
||||||
}catch (Throwable tr) {
|
}catch (Throwable tr) {
|
||||||
Log.e(TAG, "", tr);
|
Log.e(TAG, "", tr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
},"stop").start();
|
},"stop").start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static volatile Timer gracefulQuitTimer;
|
private static volatile Timer gracefulQuitTimer;
|
||||||
|
|
||||||
private void i2pdGracefulStop() {
|
private void i2pdGracefulStop() {
|
||||||
if(daemon.getState()==DaemonSingleton.State.stopped){
|
if(daemon.getState()==DaemonSingleton.State.stopped){
|
||||||
Toast.makeText(this, R.string.already_stopped,
|
Toast.makeText(this, R.string.already_stopped,
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(getGracefulQuitTimer()!=null){
|
if(getGracefulQuitTimer()!=null){
|
||||||
Toast.makeText(this, R.string.graceful_stop_is_already_in_progress,
|
Toast.makeText(this, R.string.graceful_stop_is_already_in_progress,
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Toast.makeText(this, R.string.graceful_stop_is_in_progress,
|
Toast.makeText(this, R.string.graceful_stop_is_in_progress,
|
||||||
Toast.LENGTH_SHORT).show();
|
Toast.LENGTH_SHORT).show();
|
||||||
new Thread(new Runnable(){
|
new Thread(new Runnable(){
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try{
|
try{
|
||||||
Log.d(TAG, "grac stopping");
|
Log.d(TAG, "grac stopping");
|
||||||
if(daemon.isStartedOkay()) {
|
if(daemon.isStartedOkay()) {
|
||||||
daemon.stopAcceptingTunnels();
|
daemon.stopAcceptingTunnels();
|
||||||
long gracefulStopAtMillis;
|
long gracefulStopAtMillis;
|
||||||
synchronized (graceStartedMillis_LOCK) {
|
synchronized (graceStartedMillis_LOCK) {
|
||||||
graceStartedMillis = System.currentTimeMillis();
|
graceStartedMillis = System.currentTimeMillis();
|
||||||
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS;
|
gracefulStopAtMillis = graceStartedMillis + GRACEFUL_DELAY_MILLIS;
|
||||||
}
|
}
|
||||||
rescheduleGraceStop(null,gracefulStopAtMillis);
|
rescheduleGraceStop(null,gracefulStopAtMillis);
|
||||||
}else{
|
}else{
|
||||||
i2pdStop();
|
i2pdStop();
|
||||||
}
|
}
|
||||||
} catch(Throwable tr) {
|
} catch(Throwable tr) {
|
||||||
Log.e(TAG,"",tr);
|
Log.e(TAG,"",tr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
},"gracInit").start();
|
},"gracInit").start();
|
||||||
}
|
|
||||||
|
|
||||||
private void rescheduleGraceStop(Timer gracefulQuitTimerOld, long gracefulStopAtMillis) {
|
|
||||||
if(gracefulQuitTimerOld!=null)gracefulQuitTimerOld.cancel();
|
|
||||||
final Timer gracefulQuitTimer = new Timer(true);
|
|
||||||
setGracefulQuitTimer(gracefulQuitTimer);
|
|
||||||
gracefulQuitTimer.schedule(new TimerTask(){
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
i2pdStop();
|
|
||||||
}
|
|
||||||
|
|
||||||
}, Math.max(0,gracefulStopAtMillis-System.currentTimeMillis()));
|
|
||||||
final TimerTask tickerTask = new TimerTask() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
daemonStateUpdatedListener.daemonStateUpdate();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
gracefulQuitTimer.scheduleAtFixedRate(tickerTask,0/*start delay*/,1000/*millis period*/);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Timer getGracefulQuitTimer() {
|
|
||||||
return gracefulQuitTimer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void rescheduleGraceStop(Timer gracefulQuitTimerOld, long gracefulStopAtMillis) {
|
||||||
|
if(gracefulQuitTimerOld!=null)gracefulQuitTimerOld.cancel();
|
||||||
|
final Timer gracefulQuitTimer = new Timer(true);
|
||||||
|
setGracefulQuitTimer(gracefulQuitTimer);
|
||||||
|
gracefulQuitTimer.schedule(new TimerTask(){
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
i2pdStop();
|
||||||
|
}
|
||||||
|
|
||||||
|
}, Math.max(0,gracefulStopAtMillis-System.currentTimeMillis()));
|
||||||
|
final TimerTask tickerTask = new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
daemonStateUpdatedListener.daemonStateUpdate();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
gracefulQuitTimer.scheduleAtFixedRate(tickerTask,0/*start delay*/,1000/*millis period*/);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Timer getGracefulQuitTimer() {
|
||||||
|
return gracefulQuitTimer;
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue