Browse Source

Autostart button(switch) (#19)

* Enable/Disable button; Toast on webconsole if not enabled; i2pdpath;on boot autostart;getDataPath/iniEditor preinit for setting

* settings autostart switch

Co-authored-by: R4SAS <r4sas@users.noreply.github.com>
pull/58/head
wipedlifepotato 4 years ago committed by GitHub
parent
commit
17148b5511
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/src/main/AndroidManifest.xml
  2. 2
      app/src/main/java/org/purplei2p/i2pd/DaemonWrapper.java
  3. 3
      app/src/main/java/org/purplei2p/i2pd/I2PDActivity.java
  4. 108
      app/src/main/java/org/purplei2p/i2pd/SettingsActivity.java
  5. 20
      app/src/main/java/org/purplei2p/i2pd/receivers/BootUpReceiver.java
  6. 62
      app/src/main/res/layout/activity_settings.xml
  7. 3
      app/src/main/res/menu/options_main.xml
  8. 17
      app/src/main/res/values-ru/strings.xml
  9. 11
      app/src/main/res/values/strings.xml

6
app/src/main/AndroidManifest.xml

@ -21,7 +21,10 @@ @@ -21,7 +21,10 @@
android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
android:usesCleartextTraffic="true">
<receiver android:enabled="true" android:name=".receivers.BootUpReceiver"
<receiver
android:enabled="true"
android:name="org.purplei2p.i2pd.receivers.BootUpReceiver"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
@ -33,6 +36,7 @@ @@ -33,6 +36,7 @@
android:value="true" />
<activity android:name=".WebConsoleActivity" />
<activity android:name=".SettingsActivity" />
<receiver
android:name=".NetworkStateChangeReceiver"

2
app/src/main/java/org/purplei2p/i2pd/DaemonWrapper.java

@ -144,7 +144,7 @@ public class DaemonWrapper { @@ -144,7 +144,7 @@ public class DaemonWrapper {
return daemonStartResult;
}
public String getDataDir() { // for settings iniEditor
public static String getDataDir() { // for settings iniEditor
return I2PD_JNI.getDataDir();
}

3
app/src/main/java/org/purplei2p/i2pd/I2PDActivity.java

@ -299,6 +299,9 @@ public class I2PDActivity extends Activity { @@ -299,6 +299,9 @@ public class I2PDActivity extends Activity {
else
Toast.makeText(this,"I2Pd not was started!", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);

108
app/src/main/java/org/purplei2p/i2pd/SettingsActivity.java

@ -0,0 +1,108 @@ @@ -0,0 +1,108 @@
package org.purplei2p.i2pd;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import java.io.File;
import java.util.List;
import org.purplei2p.i2pd.iniedotr.IniEditor;
public class SettingsActivity extends Activity {
protected IniEditor iniedit = new IniEditor();
private String dataDir = DaemonWrapper.getDataDir();//for inieditor
private String TAG = "i2pdSrvcSettings";
private File cacheDir;
public static String onBootFileName="/onBoot"; // just file, empty, if exist the do autostart, if not then no.
//https://gist.github.com/chandruark/3165a5ee3452f2b9ec7736cf1b4c5ea6
private void addAutoStartupswitch() {
try {
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER .toLowerCase();
switch (manufacturer){
case "xiaomi":
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
break;
case "oppo":
intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
break;
case "vivo":
intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
break;
case "Letv":
intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
break;
case "Honor":
intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
break;
case "oneplus":
intent.setComponent(new ComponentName("com.oneplus.security", "com.oneplus.security.chainlaunch.view.ChainLaunchAppListActivity"));
break;
}
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0) {
startActivity(intent);
}
} catch (Exception e) {
Log.e("exceptionAutostarti2pd" , String.valueOf(e));
}
}
//@Override
private void requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName())
);
startActivityForResult(intent, 232);
} else {
//Permission Granted-System will work
}
}
}
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
cacheDir = getApplicationContext().getCacheDir();
setContentView(R.layout.activity_settings);
Switch autostart_switch = findViewById(R.id.autostart_enable);
File onBoot= new File( cacheDir.getAbsolutePath()+onBootFileName);
autostart_switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked will be
// true if the switch is in the On position
if(isChecked){
if( !onBoot.exists() ) {
requestPermission();
addAutoStartupswitch();
try {
if( !onBoot.createNewFile()) Log.d(TAG, "Cant create new wile on: "+onBoot.getAbsolutePath());
} catch (Exception e) {
Log.d(TAG, "error: " + e.toString());
}
}
}else{
if( onBoot.exists() )
onBoot.delete();
}
}
});
if(onBoot.exists()) autostart_switch.setChecked(true);
}
}

20
app/src/main/java/org/purplei2p/i2pd/receivers/BootUpReceiver.java

@ -4,16 +4,28 @@ package org.purplei2p.i2pd.receivers; @@ -4,16 +4,28 @@ package org.purplei2p.i2pd.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
//import org.purplei2p.i2pd.ForegroundService;
//ToDo:* fix^^^ change to service, not on window on start.
import org.purplei2p.i2pd.I2PDPermsAskerActivity;
import java.io.File;
import static org.purplei2p.i2pd.SettingsActivity.onBootFileName;
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);
File onBoot =
new File(
context.getApplicationContext().getCacheDir().getAbsolutePath()
+ onBootFileName);
if(onBoot.exists()) {
Intent i = new Intent(context, I2PDPermsAskerActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}

62
app/src/main/res/layout/activity_settings.xml

@ -0,0 +1,62 @@ @@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/settings_section0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoText="true"
android:clickable="false"
android:cursorVisible="false"
android:text="@string/settings_section0"
android:textColor="#282528"
android:textStyle="bold|italic"
android:verticalScrollbarPosition="defaultPosition" />
<Switch
android:id="@+id/autostart_enable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/autostart_enabled" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<!--
<TextView
android:id="@+id/settings_section2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoText="true"
android:clickable="false"
android:cursorVisible="false"
android:text="@string/settings_section_tunnels"
android:textColor="#282528"
android:textStyle="bold|italic"
android:verticalScrollbarPosition="defaultPosition" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/add_tunnel" />
<Button
android:id="@+id/add_tunnel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_tunnel_button" />
</LinearLayout>
-->
</LinearLayout>

3
app/src/main/res/menu/options_main.xml

@ -23,6 +23,9 @@ @@ -23,6 +23,9 @@
android:id="@+id/action_graceful_stop"
android:orderInCategory="98"
android:title="@string/action_graceful_stop" />
<item
android:id="@+id/action_settings"
android:title="@string/action_settings" />
<item
android:id="@+id/action_stop"
android:orderInCategory="99"

17
app/src/main/res/values-ru/strings.xml

@ -1,12 +1,16 @@ @@ -1,12 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">i2pd</string>
<string name="app_description">Invisible Internet Protocol:</string>
<string name="app_description2">Сеть без границ</string>
<string name="action_start">Запустить</string>
<string name="action_stop">Остановить</string>
<string name="action_graceful_stop">Корректная остановка</string>
<string name="action_cancel_graceful_stop">Отменить корректную остановку</string>
<string name="action_reload_tunnels_config">Перезагрузить туннели</string>
<string name="action_start_webview">Открыть Веб Консоль</string>
<string name="action_settings">Настройки</string>
<string name="graceful_stop_is_already_in_progress">Корректная остановка уже запущена</string>
<string name="graceful_stop_is_in_progress">Корректная остановка запущена</string>
@ -37,7 +41,14 @@ @@ -37,7 +41,14 @@
<string name="shutdown_canceled">Плановая остановка отменена</string>
<string name="tunnels_reloading">Перезагрузка конфигурации туннелей...</string>
<string name="app_description">Invisible Internet Protocol:</string>
<string name="action_start">Запустить</string>
<string name="app_description2">Сеть без границ</string>
<string name="autostart_enabled">Автозагрузка при запуске телефона</string>
<string name="settings_section0">Основные настройки</string>
<string name="settings_section_tunnels">Туннели</string>
<string name="add_tunnel_button">Добавить</string>
<string name="add_tunnel">Добавление туннелей</string>
<string name="del_tunnel_button">Удалить</string>
</resources>

11
app/src/main/res/values/strings.xml

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
<string name="action_cancel_graceful_stop">Cancel Graceful Stop</string>
<string name="action_reload_tunnels_config">Reload tunnels</string>
<string name="action_start_webview">Open Web Console</string>
<string name="action_settings">Settings</string>
<string name="graceful_stop_is_already_in_progress">Graceful stop is already in progress</string>
<string name="graceful_stop_is_in_progress">Graceful stop is in progress</string>
@ -41,4 +42,14 @@ @@ -41,4 +42,14 @@
<string name="shutdown_canceled">Planned shutdown canceled</string>
<string name="tunnels_reloading">Reloading tunnels config...</string>
<string name="settings_section0">Common settings</string>
<string name="settings_section_tunnels">Tunnels</string>
<string name="autostart_enabled">AutoStart on boot</string>
<string name="add_tunnel_button">Add tunnel</string>
<string name="add_tunnel">Tunnels adding</string>
<string name="del_tunnel_button">Del tunnel</string>
</resources>

Loading…
Cancel
Save