Skip to content

Commit

Permalink
Try to fix #50 (#76)
Browse files Browse the repository at this point in the history
* Fix foreground notification
* Trying to fix app quit
* Remove unsupported UI items
* Remove misleading exceptions
* Set Unix exec bit on build scripts
* Revamp processAssets point to be launched
* Corrected logic of daemon status events
* Fix double I2PActivity.onCreate call hangup
* Fix exception processing @ processAssets
  • Loading branch information
nonlin-lin-chaos-order-etc-etal authored Jul 21, 2024
1 parent 8dba5a0 commit 988940f
Show file tree
Hide file tree
Showing 18 changed files with 505 additions and 156 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export ANDROID_HOME=/opt/android-sdk
export ANDROID_NDK_HOME=$ANDROID_HOME/ndk/23.2.8568313

pushd app/jni
./build_boost.sh
./build_openssl.sh
./build_miniupnpc.sh
pushd binary/jni
export BUILD_SO=1
./build_debug.sh
popd

gradle clean assembleDebug
Expand Down
10 changes: 8 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {

externalNativeBuild {
ndkBuild {
arguments "NDK_MODULE_PATH:=${rootProject.projectDir}/app/jni"
arguments "NDK_MODULE_PATH:=${rootProject.projectDir}/binary/jni"
arguments "-j${Runtime.getRuntime().availableProcessors()}"
}
}
Expand Down Expand Up @@ -80,7 +80,13 @@ android {

externalNativeBuild {
ndkBuild {
path "${rootProject.projectDir}/app/jni/Android.mk"
path "${rootProject.projectDir}/binary/jni/Android.mk"
}
}

sourceSets {
main {
jniLibs.srcDir file("${rootProject.projectDir}/binary/libs")
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:name=".appscope.App"
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/org/purplei2p/i2pd/AbstractProcess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.purplei2p.i2pd;

public interface AbstractProcess {
/** @param tr can be null
*/
void kill(Throwable tr);
}
172 changes: 91 additions & 81 deletions app/src/main/java/org/purplei2p/i2pd/DaemonWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -67,25 +68,25 @@ private synchronized void setState(State newState) {
public synchronized void stopAcceptingTunnels() {
if (isStartedOkay()) {
setState(State.gracefulShutdownInProgress);
I2PD_JNI.stopAcceptingTunnels();
//I2PD_JNI.stopAcceptingTunnels();
}
}

public synchronized void startAcceptingTunnels() {
if (isStartedOkay()) {
setState(State.startedOkay);
I2PD_JNI.startAcceptingTunnels();
//I2PD_JNI.startAcceptingTunnels();
}
}

public synchronized void reloadTunnelsConfigs() {
if (isStartedOkay()) {
I2PD_JNI.reloadTunnelsConfigs();
//I2PD_JNI.reloadTunnelsConfigs();
}
}

public int getTransitTunnelsCount() {
return I2PD_JNI.getTransitTunnelsCount();
return 0;//I2PD_JNI.getTransitTunnelsCount();
}

public enum State {
Expand Down Expand Up @@ -117,11 +118,11 @@ public State getState() {
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); //need to start when storage permissions to the datadir exist
}

private Throwable lastThrowable;
Expand All @@ -147,11 +148,13 @@ public String getDaemonStartResult() {
}

public static String getDataDir() { // for settings iniEditor
return I2PD_JNI.getDataDir();
return i2pdDataDir;
}

private static String i2pdDataDir;

public void changeDataDir(String dataDir, Boolean updateAssets) {
I2PD_JNI.setDataDir(dataDir);
i2pdDataDir=dataDir;
if (updateAssets) processAssets();
//ToDo: move old dir to new dir?
}
Expand All @@ -160,44 +163,44 @@ public boolean isStartedOkay() {
return getState().isStartedOkay();
}

public synchronized void stopDaemon() {
public synchronized void stopDaemon(final Throwable throwable) {
if (isStartedOkay()) {
try {
I2PD_JNI.stopDaemon();
I2pdApi.stopDaemon(throwable);
} catch (Throwable tr) {
Log.e(TAG, "", tr);
}
if (throwable != null) lastThrowable = throwable;
setState(State.stopped);
}
}
public synchronized void startDaemon() {
if( getState() != State.stopped && getState() != State.starting ) return;
new Thread(() -> {
try {
processAssets();
I2PD_JNI.loadLibraries();
//registerNetworkCallback();
} catch (Throwable tr) {
lastThrowable = tr;
setState(State.startFailed);
return;
}
try {
synchronized (DaemonWrapper.this) {
I2PD_JNI.setDataDir(i2pdpath); // (Environment.getExternalStorageDirectory().getAbsolutePath() + "/i2pd");

Log.i(TAG, "setting webconsole language to " + appLocale);
I2PD_JNI.setLanguage(appLocale);
public synchronized void startDaemonIfStopped(Context ctx) {
if (getState() == State.startedOkay) return;
new Thread(() -> {
synchronized(DaemonWrapper.this) {
if (getState() == State.startedOkay) return;
try {
processAssets();
//registerNetworkCallback();
} catch (Throwable tr) {
lastThrowable = tr;
setState(State.startFailed);
return;
}
try {
String locale = getAppLocale();
Log.i(TAG, "setting webconsole language to " + locale);

daemonStartResult = I2PD_JNI.startDaemon();
daemonStartResult = I2pdApi.startDaemon(ctx, i2pdpath, locale, DaemonWrapper.this);
if ("ok".equals(daemonStartResult)) {
setState(State.startedOkay);
} else
setState(State.startFailed);
} catch (Throwable tr) {
lastThrowable = tr;
setState(State.startFailed);
}
} catch (Throwable tr) {
lastThrowable = tr;
setState(State.startFailed);
}
}, "i2pdDaemonStart").start();
}
Expand All @@ -207,72 +210,76 @@ private void processAssets() {
String versionName = BuildConfig.VERSION_NAME; // here will be app version, like 2.XX.XX
StringBuilder text = new StringBuilder();
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 {
if (holderFile.exists()) {
try { // if holder file exists, read assets version string
FileReader fileReader = new FileReader(holderFile);

try {
String line;
BufferedReader br = new BufferedReader(fileReader);

while ((line = br.readLine()) != null) {
text.append(line);
try {
String line;

while ((line = br.readLine()) != null) {
text.append(line);
}
} finally {
try {
br.close();
} catch (IOException e) {
Log.e(TAG, "", e);
}
}
}finally {
} finally {
try {
br.close();
fileReader.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);
}
} catch (IOException e) {
Log.e(TAG, "", e);
}
}

// if version differs from current app version or null, try to delete certificates folder
if (!text.toString().contains(versionName)) {
try {
boolean deleteResult = holderFile.delete();
if (!deleteResult)
Log.e(TAG, "holderFile.delete() returned " + deleteResult + ", absolute path='" + holderFile.getAbsolutePath() + "'");
File certPath = new File(i2pdpath, "certificates");
deleteRecursive(certPath);

// copy assets. If processed file exists, it won't be overwritten
copyAsset("addressbook");
copyAsset("certificates");
copyAsset("tunnels.d");
copyAsset("i2pd.conf");
copyAsset("subscriptions.txt");
copyAsset("tunnels.conf");

// update holder file about successful copying
FileWriter writer = new FileWriter(holderFile);
// if version differs from current app version or null, try to delete certificates folder
if (!text.toString().contains(versionName)) {
try {
writer.append(versionName);
} finally {
boolean deleteResult = holderFile.delete();
if (!deleteResult)
Log.e(TAG, "holderFile.delete() returned " + deleteResult + ", absolute path='" + holderFile.getAbsolutePath() + "'");
File certPath = new File(i2pdpath, "certificates");
deleteRecursive(certPath);

// copy assets. If processed file exists, it won't be overwritten
copyAsset("addressbook");
copyAsset("certificates");
copyAsset("tunnels.d");
copyAsset("i2pd.conf");
copyAsset("subscriptions.txt");
copyAsset("tunnels.conf");

// update holder file about successful copying
FileWriter writer = new FileWriter(holderFile);
try {
writer.close();
} catch (IOException e) {
Log.e(TAG,"on writer close", e);
writer.append(versionName);
} finally {
try {
writer.close();
} catch (IOException e) {
Log.e(TAG, "on writer close", e);
}
}
} catch (Throwable tr) {
Log.e(TAG, "on assets copying", tr);
}
}
catch (Throwable tr)
{
Log.e(TAG,"on assets copying", tr);
}catch(Throwable tr) {
if(tr.getMessage().contains("Permission denied")) {
Log.e(TAG, "Permission denied on assets copying", tr);
}
throw new RuntimeException(tr);
}
}

Expand Down Expand Up @@ -372,15 +379,18 @@ private static final class NetworkStateCallbackImpl extends ConnectivityManager.
@Override
public void onAvailable(Network network) {
super.onAvailable(network);
I2PD_JNI.onNetworkStateChanged(true);
//I2PD_JNI.onNetworkStateChanged(true);
Log.d(TAG, "NetworkCallback.onAvailable");
}

@Override
public void onLost(Network network) {
super.onLost(network);
I2PD_JNI.onNetworkStateChanged(false);
//I2PD_JNI.onNetworkStateChanged(false);
Log.d(TAG, " NetworkCallback.onLost");
}
}
private String getAppLocale() {
return Locale.getDefault().getDisplayLanguage(Locale.ENGLISH).toLowerCase(); // lower-case system language (like "english")
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/org/purplei2p/i2pd/ForegroundService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
public class ForegroundService extends Service {
private static final String TAG = "FgService";
private volatile boolean shown;

public static ForegroundService getInstance() {
return instance;
}

private static ForegroundService instance;
private static volatile DaemonWrapper daemon;
private static final Object initDeinitLock = new Object();
Expand Down Expand Up @@ -92,6 +97,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {

@Override
public void onDestroy() {
stop();
}

public void stop() {
cancelNotification();
deinitCheck();
instance = null;
Expand Down
Loading

0 comments on commit 988940f

Please sign in to comment.