@ -13,6 +13,7 @@ import java.util.Set;
@@ -13,6 +13,7 @@ import java.util.Set;
import java.util.Locale ;
import android.annotation.TargetApi ;
import android.content.Context ;
import android.content.res.AssetManager ;
import android.net.ConnectivityManager ;
import android.net.Network ;
@ -32,7 +33,9 @@ public class DaemonWrapper {
@@ -32,7 +33,9 @@ public class DaemonWrapper {
private String i2pdpath = Environment . getExternalStorageDirectory ( ) . getAbsolutePath ( ) + "/i2pd" ;
private boolean assetsCopied ;
private static final String appLocale = Locale . getDefault ( ) . getDisplayLanguage ( Locale . ENGLISH ) . toLowerCase ( ) ; // lower-case system language (like "english")
private String getAppLocale ( ) {
return Locale . getDefault ( ) . getDisplayLanguage ( Locale . ENGLISH ) . toLowerCase ( ) ; // lower-case system language (like "english")
}
public interface StateUpdateListener {
void daemonStateUpdate ( State oldValue , State newValue ) ;
@ -67,25 +70,25 @@ public class DaemonWrapper {
@@ -67,25 +70,25 @@ public class DaemonWrapper {
public synchronized void stopAcceptingTunnels ( ) {
if ( isStartedOkay ( ) ) {
setState ( State . gracefulShutdownInProgress ) ;
I2PD_JNI . stopAcceptingTunnels ( ) ;
I2pdApi . stopAcceptingTunnels ( ) ;
}
}
public synchronized void startAcceptingTunnels ( ) {
if ( isStartedOkay ( ) ) {
setState ( State . startedOkay ) ;
I2PD_JNI . startAcceptingTunnels ( ) ;
I2pdApi . startAcceptingTunnels ( ) ;
}
}
public synchronized void reloadTunnelsConfigs ( ) {
if ( isStartedOkay ( ) ) {
I2PD_JNI . reloadTunnelsConfigs ( ) ;
I2pdApi . reloadTunnelsConfigs ( ) ;
}
}
public int getTransitTunnelsCount ( ) {
return I2PD_JNI . getTransitTunnelsCount ( ) ;
return I2pdApi . getTransitTunnelsCount ( ) ;
}
public enum State {
@ -117,11 +120,11 @@ public class DaemonWrapper {
@@ -117,11 +120,11 @@ public class DaemonWrapper {
return state ;
}
public DaemonWrapper ( AssetManager assetManager , ConnectivityManager connectivityManager ) {
public DaemonWrapper ( Context ctx , AssetManager assetManager , ConnectivityManager connectivityManager ) {
this . assetManager = assetManager ;
this . connectivityManager = connectivityManager ;
setState ( State . starting ) ;
startDaemon ( ) ;
startDaemon ( ctx ) ;
}
private Throwable lastThrowable ;
@ -146,16 +149,6 @@ public class DaemonWrapper {
@@ -146,16 +149,6 @@ public class DaemonWrapper {
return daemonStartResult ;
}
public static String getDataDir ( ) { // for settings iniEditor
return I2PD_JNI . getDataDir ( ) ;
}
public void changeDataDir ( String dataDir , Boolean updateAssets ) {
I2PD_JNI . setDataDir ( dataDir ) ;
if ( updateAssets ) processAssets ( ) ;
//ToDo: move old dir to new dir?
}
public boolean isStartedOkay ( ) {
return getState ( ) . isStartedOkay ( ) ;
}
@ -163,19 +156,18 @@ public class DaemonWrapper {
@@ -163,19 +156,18 @@ public class DaemonWrapper {
public synchronized void stopDaemon ( ) {
if ( isStartedOkay ( ) ) {
try {
I2PD_JNI . stopDaemon ( ) ;
I2pdApi . stopDaemon ( ) ;
} catch ( Throwable tr ) {
Log . e ( TAG , "" , tr ) ;
}
setState ( State . stopped ) ;
}
}
public synchronized void startDaemon ( ) {
public synchronized void startDaemon ( Context ctx ) {
if ( getState ( ) ! = State . stopped & & getState ( ) ! = State . starting ) return ;
new Thread ( ( ) - > {
try {
processAssets ( ) ;
I2PD_JNI . loadLibraries ( ) ;
//registerNetworkCallback();
} catch ( Throwable tr ) {
lastThrowable = tr ;
@ -184,12 +176,10 @@ public class DaemonWrapper {
@@ -184,12 +176,10 @@ public class DaemonWrapper {
}
try {
synchronized ( DaemonWrapper . this ) {
I2PD_JNI . setDataDir ( i2pdpath ) ; // (Environment.getExternalStorageDirectory().getAbsolutePath() + "/i2pd");
Log . i ( TAG , "setting webconsole language to " + appLocale ) ;
I2PD_JNI . setLanguage ( appLocale ) ;
String locale = getAppLocale ( ) ;
Log . i ( TAG , "setting webconsole language to " + locale ) ;
daemonStartResult = I2PD_JNI . startDaemon ( ) ;
daemonStartResult = I2pdApi . startDaemon ( ctx , i2pdpath , locale ) ;
if ( "ok" . equals ( daemonStartResult ) ) {
setState ( State . startedOkay ) ;
} else
@ -209,31 +199,13 @@ public class DaemonWrapper {
@@ -209,31 +199,13 @@ public class DaemonWrapper {
Log . d ( TAG , "checking assets" ) ;
if ( holderFile . exists ( ) ) {
try { // if holder file exists, read assets version string
FileReader fileReader = new FileReader ( holderFile ) ;
try {
BufferedReader br = new BufferedReader ( fileReader ) ;
try {
try ( FileReader fileReader = new FileReader ( holderFile ) ) { // if holder file exists, read assets version string
try ( BufferedReader br = new BufferedReader ( fileReader ) ) {
String line ;
while ( ( line = br . readLine ( ) ) ! = null ) {
text . append ( line ) ;
}
} finally {
try {
br . close ( ) ;
} catch ( IOException e ) {
Log . e ( TAG , "" , e ) ;
}
}
} finally {
try {
fileReader . close ( ) ;
} catch ( IOException e ) {
Log . e ( TAG , "" , e ) ;
}
}
} catch ( IOException e ) {
Log . e ( TAG , "" , e ) ;
@ -258,15 +230,9 @@ public class DaemonWrapper {
@@ -258,15 +230,9 @@ public class DaemonWrapper {
copyAsset ( "tunnels.conf" ) ;
// update holder file about successful copying
FileWriter writer = new FileWriter ( holderFile ) ;
try {
;
try ( FileWriter writer = new FileWriter ( holderFile ) ) {
writer . append ( versionName ) ;
} finally {
try {
writer . close ( ) ;
} catch ( IOException e ) {
Log . e ( TAG , "on writer close" , e ) ;
}
}
}
catch ( Throwable tr )
@ -372,14 +338,14 @@ public class DaemonWrapper {
@@ -372,14 +338,14 @@ public class DaemonWrapper {
@Override
public void onAvailable ( Network network ) {
super . onAvailable ( network ) ;
I2PD_JNI . onNetworkStateChanged ( true ) ;
I2pdApi . onNetworkStateChanged ( true ) ;
Log . d ( TAG , "NetworkCallback.onAvailable" ) ;
}
@Override
public void onLost ( Network network ) {
super . onLost ( network ) ;
I2PD_JNI . onNetworkStateChanged ( false ) ;
I2pdApi . onNetworkStateChanged ( false ) ;
Log . d ( TAG , " NetworkCallback.onLost" ) ;
}
}