Skip to content

Commit

Permalink
server: add foreground service option. close #112
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Oct 10, 2019
1 parent d207a41 commit 99cc306
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 28 deletions.
21 changes: 16 additions & 5 deletions app/src/main/java/org/itxtech/daedalus/Daedalus.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,23 +199,34 @@ public static boolean switchService() {
deactivateService(instance);
return false;
} else {
activateService(instance);
prepareAndActivateService(instance);
return true;
}
}

public static boolean activateService(Context context) {
public static boolean prepareAndActivateService(Context context) {
Intent intent = VpnService.prepare(context);
if (intent != null) {
return false;
} else {
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary());
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
context.startService(Daedalus.getServiceIntent(context).setAction(DaedalusVpnService.ACTION_ACTIVATE));
activateService(context);
return true;
}
}

public static void activateService(Context context) {
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary());
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
if (getInstance().prefs.getBoolean("settings_foreground", false)
&& Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
Logger.info("Starting foreground service");
context.startForegroundService(Daedalus.getServiceIntent(context).setAction(DaedalusVpnService.ACTION_ACTIVATE));
} else {
Logger.info("Starting background service");
context.startService(Daedalus.getServiceIntent(context).setAction(DaedalusVpnService.ACTION_ACTIVATE));
}
}

public static void deactivateService(Context context) {
context.startService(getServiceIntent(context).setAction(DaedalusVpnService.ACTION_DEACTIVATE));
context.stopService(getServiceIntent(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ public void activateService() {

public void onActivityResult(int request, int result, Intent data) {
if (result == Activity.RESULT_OK) {
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary());
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
Daedalus.getInstance().startService(Daedalus.getServiceIntent(getApplicationContext()).setAction(DaedalusVpnService.ACTION_ACTIVATE));
Daedalus.activateService(Daedalus.getInstance());
updateMainButton(R.string.button_text_deactivate);
Daedalus.updateShortcut(getApplicationContext());
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<string name="settings_dont_build_doh_cache">不缓存 DoH DNS 的 IP地址</string>
<string name="settings_dark_theme">使用暗主题</string>
<string name="settings_dns_query_method">DNS 查询方式</string>
<string name="settings_foreground">运行前台服务</string>

<string name="settings_rule_name">规则名称</string>
<string name="settings_rule_type">规则类型</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<string name="settings_rule_import_built_in">内置</string>
<string name="settings_rule_import_external">外部</string>
<string name="settings_dns_query_method">DNS 查询方式</string>
<string name="settings_foreground">運行前臺服務</string>

<string name="settings_server_name">伺服器名稱</string>
<string name="settings_server_address">伺服器網址</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<string name="settings_debug_output">Debug Output</string>
<string name="settings_dark_theme">Use Dark Theme</string>
<string name="settings_dns_query_method">DNS Query Method</string>
<string name="settings_foreground">Run service in Foreground</string>

<string name="settings_dns_tcp">TCP</string>
<string name="settings_dns_udp">UDP</string>
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/res/xml/perf_rule.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditTextPreference
android:key="ruleName"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_rule_name"/>

<PreferenceCategory
app:iconSpaceReserved="false"
android:key="rulSettings"
android:title="@string/settings_rule">
<ListPreference
Expand All @@ -27,7 +30,9 @@
android:key="ruleSync"
android:title="@string/settings_rule_sync"/>
</PreferenceCategory>

<PreferenceCategory
app:iconSpaceReserved="false"
android:key="Import"
android:title="@string/settings_rule_import">
<ListPreference
Expand All @@ -37,4 +42,4 @@
android:key="ruleImportExternal"
android:title="@string/settings_rule_import_external"/>
</PreferenceCategory>
</PreferenceScreen>
</androidx.preference.PreferenceScreen>
7 changes: 5 additions & 2 deletions app/src/main/res/xml/perf_server.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<EditTextPreference
android:key="serverName"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/settings_server_name"/>

<PreferenceCategory
app:iconSpaceReserved="false"
android:key="serverSettings"
android:title="@string/settings_server">
<EditTextPreference
Expand All @@ -20,4 +23,4 @@
android:numeric="integer"
android:title="@string/settings_server_port"/>
</PreferenceCategory>
</PreferenceScreen>
</androidx.preference.PreferenceScreen>
42 changes: 26 additions & 16 deletions app/src/main/res/xml/perf_settings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:iconSpaceReserved="false"
android:key="settingsServer"
android:title="@string/settings_server">

Expand All @@ -23,7 +24,9 @@
android:singleLine="true"
android:title="@string/settings_dns_test_servers"/>
</PreferenceCategory>

<PreferenceCategory
app:iconSpaceReserved="false"
android:key="settings_system"
android:title="@string/settings_system">

Expand All @@ -39,6 +42,10 @@
android:key="settings_dark_theme"
android:title="@string/settings_dark_theme"
android:defaultValue="false"/>
<SwitchPreference
android:key="settings_foreground"
android:title="@string/settings_foreground"
android:defaultValue="false"/>
<EditTextPreference
android:key="settings_log_size"
android:selectAllOnFocus="false"
Expand All @@ -49,25 +56,27 @@
</PreferenceCategory>

<PreferenceCategory
android:key="settings_app_filter"
android:title="@string/settings_app_filter">
app:iconSpaceReserved="false"
android:key="settings_app_filter"
android:title="@string/settings_app_filter">
<SwitchPreference
android:key="settings_app_filter_switch"
android:title="@string/settings_app_filter"
android:defaultValue="false"/>
android:key="settings_app_filter_switch"
android:title="@string/settings_app_filter"
android:defaultValue="false"/>
<SwitchPreference
android:key="settings_app_filter_mode_switch"
android:title="@string/settings_app_filter_mode"
android:defaultValue="false"
android:enabled="false"/>
android:key="settings_app_filter_mode_switch"
android:title="@string/settings_app_filter_mode"
android:defaultValue="false"
android:enabled="false"/>
<org.itxtech.daedalus.widget.ClickPreference
android:key="settings_app_filter_list"
android:title="@string/settings_app_filter_list"
android:summary="@string/settings_app_filter_list_summary"
android:enabled="false"/>
android:key="settings_app_filter_list"
android:title="@string/settings_app_filter_list"
android:summary="@string/settings_app_filter_list_summary"
android:enabled="false"/>
</PreferenceCategory>

<PreferenceCategory
app:iconSpaceReserved="false"
android:key="settings_advanced"
android:title="@string/settings_advanced">
<SwitchPreference
Expand Down Expand Up @@ -100,6 +109,7 @@
</PreferenceCategory>

<PreferenceCategory
app:iconSpaceReserved="false"
android:key="settings_help"
android:title="@string/settings_help_and_support">
<org.itxtech.daedalus.widget.ClickPreference
Expand All @@ -118,4 +128,4 @@
android:title="@string/settings_manual"
android:summary="@string/settings_manual_summary"/>
</PreferenceCategory>
</PreferenceScreen>
</androidx.preference.PreferenceScreen>

0 comments on commit 99cc306

Please sign in to comment.