mirror of
https://github.com/PurpleI2P/i2pd-android.git
synced 2024-12-06 19:27:28 +01:00
Enable/Disable button; Toast on webconsole if not enabled; i2pdpath;on boot autostart;getDataPath/iniEditor preinit for setting
This commit is contained in:
committed by
R4SAS
parent
55cde8e11c
commit
fd04d7070d
@@ -24,6 +24,7 @@ import android.util.Log;
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
public class DaemonWrapper {
|
||||
|
||||
private static final String TAG = "i2pd";
|
||||
private final AssetManager assetManager;
|
||||
private final ConnectivityManager connectivityManager;
|
||||
@@ -118,31 +119,7 @@ public class DaemonWrapper {
|
||||
this.assetManager = assetManager;
|
||||
this.connectivityManager = connectivityManager;
|
||||
setState(State.starting);
|
||||
new Thread(() -> {
|
||||
try {
|
||||
processAssets();
|
||||
I2PD_JNI.loadLibraries();
|
||||
setState(State.jniLibraryLoaded);
|
||||
registerNetworkCallback();
|
||||
} catch (Throwable tr) {
|
||||
lastThrowable = tr;
|
||||
setState(State.startFailed);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
synchronized (DaemonWrapper.this) {
|
||||
I2PD_JNI.setDataDir(Environment.getExternalStorageDirectory().getAbsolutePath() + "/i2pd");
|
||||
daemonStartResult = I2PD_JNI.startDaemon();
|
||||
if ("ok".equals(daemonStartResult)) {
|
||||
setState(State.startedOkay);
|
||||
} else
|
||||
setState(State.startFailed);
|
||||
}
|
||||
} catch (Throwable tr) {
|
||||
lastThrowable = tr;
|
||||
setState(State.startFailed);
|
||||
}
|
||||
}, "i2pdDaemonStart").start();
|
||||
startDaemon();
|
||||
}
|
||||
|
||||
private Throwable lastThrowable;
|
||||
@@ -167,6 +144,16 @@ public class DaemonWrapper {
|
||||
return daemonStartResult;
|
||||
}
|
||||
|
||||
public 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();
|
||||
}
|
||||
@@ -182,6 +169,34 @@ public class DaemonWrapper {
|
||||
setState(State.stopped);
|
||||
}
|
||||
}
|
||||
public synchronized void startDaemon() {
|
||||
if( getState() != State.stopped && getState() != State.starting ) return;
|
||||
new Thread(() -> {
|
||||
try {
|
||||
processAssets();
|
||||
I2PD_JNI.loadLibraries();
|
||||
setState(State.jniLibraryLoaded);
|
||||
registerNetworkCallback();
|
||||
} catch (Throwable tr) {
|
||||
lastThrowable = tr;
|
||||
setState(State.startFailed);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
synchronized (DaemonWrapper.this) {
|
||||
I2PD_JNI.setDataDir(i2pdpath);//(Environment.getExternalStorageDirectory().getAbsolutePath() + "/i2pd");
|
||||
daemonStartResult = I2PD_JNI.startDaemon();
|
||||
if ("ok".equals(daemonStartResult)) {
|
||||
setState(State.startedOkay);
|
||||
} else
|
||||
setState(State.startFailed);
|
||||
}
|
||||
} catch (Throwable tr) {
|
||||
lastThrowable = tr;
|
||||
setState(State.startFailed);
|
||||
}
|
||||
}, "i2pdDaemonStart").start();
|
||||
}
|
||||
|
||||
private void processAssets() {
|
||||
if (!assetsCopied) {
|
||||
|
||||
@@ -27,6 +27,8 @@ import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -35,12 +37,17 @@ import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import static android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS;
|
||||
import static org.purplei2p.i2pd.DaemonWrapper.State.startedOkay;
|
||||
import static org.purplei2p.i2pd.DaemonWrapper.State.starting;
|
||||
import static org.purplei2p.i2pd.DaemonWrapper.State.stopped;
|
||||
|
||||
public class I2PDActivity extends Activity {
|
||||
private static final String TAG = "i2pdActvt";
|
||||
private static final int MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
|
||||
public static final int GRACEFUL_DELAY_MILLIS = 10 * 60 * 1000;
|
||||
public static final String PACKAGE_URI_SCHEME = "package:";
|
||||
private Button enableButton;
|
||||
private Button disableButton;
|
||||
|
||||
private TextView textView;
|
||||
|
||||
@@ -64,6 +71,9 @@ public class I2PDActivity extends Activity {
|
||||
return;
|
||||
}
|
||||
DaemonWrapper.State state = daemon.getState();
|
||||
if(state == startedOkay){
|
||||
disableButton.setVisibility(View.VISIBLE);
|
||||
}
|
||||
String startResultStr = DaemonWrapper.State.startFailed.equals(state) ? String.format(": %s", daemon.getDaemonStartResult()) : "";
|
||||
String graceStr = DaemonWrapper.State.gracefulShutdownInProgress.equals(state) ? String.format(": %s %s", formatGraceTimeRemaining(), getText(R.string.remaining)) : "";
|
||||
textView.setText(String.format("%s%s%s", getText(state.getStatusStringResourceId()), startResultStr, graceStr));
|
||||
@@ -92,9 +102,9 @@ public class I2PDActivity extends Activity {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
enableButton = findViewById(R.id.enableButton);
|
||||
disableButton = findViewById(R.id.disableButton);
|
||||
textView = (TextView) findViewById(R.id.textView);
|
||||
|
||||
if (daemon == null) {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
daemon = new DaemonWrapper(getAssets(), connectivityManager);
|
||||
@@ -103,7 +113,25 @@ public class I2PDActivity extends Activity {
|
||||
|
||||
daemon.addStateChangeListener(daemonStateUpdatedListener);
|
||||
daemonStateUpdatedListener.daemonStateUpdate(DaemonWrapper.State.uninitialized, daemon.getState());
|
||||
|
||||
enableButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if( daemon.getState() != startedOkay && daemon.getState() != starting ){
|
||||
daemon.stopDaemon();
|
||||
daemon.startDaemon();
|
||||
disableButton.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
});
|
||||
disableButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if( daemon.getState() != stopped ){
|
||||
daemon.stopDaemon();
|
||||
disableButton.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
}
|
||||
});
|
||||
// request permissions
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
||||
@@ -266,7 +294,10 @@ public class I2PDActivity extends Activity {
|
||||
return true;
|
||||
|
||||
case R.id.action_start_webview:
|
||||
startActivity(new Intent(getApplicationContext(), WebConsoleActivity.class));
|
||||
if( daemon.getState() == startedOkay)
|
||||
startActivity(new Intent(getApplicationContext(), WebConsoleActivity.class));
|
||||
else
|
||||
Toast.makeText(this,"I2Pd not was started!", Toast.LENGTH_SHORT).show();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -30,4 +30,6 @@ public class I2PD_JNI {
|
||||
//System.loadLibrary("c++_shared");
|
||||
System.loadLibrary("i2pd");
|
||||
}
|
||||
|
||||
public static native String getDataDir();
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
package org.purplei2p.i2pd.receivers;
|
||||
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import org.purplei2p.i2pd.I2PDPermsAskerActivity;
|
||||
|
||||
public class BootUpReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
/* todo: disable the autostart? */
|
||||
Intent i = new Intent(context, I2PDPermsAskerActivity.class);
|
||||
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(i);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user