server: add foreground service option. close #112

This commit is contained in:
PeratX 2019-10-10 10:59:10 +08:00
parent d207a41635
commit 99cc306ded
8 changed files with 58 additions and 28 deletions

View File

@ -199,23 +199,34 @@ public class Daedalus extends Application {
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));

View File

@ -166,9 +166,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
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());
}

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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
@ -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
@ -37,4 +42,4 @@
android:key="ruleImportExternal"
android:title="@string/settings_rule_import_external"/>
</PreferenceCategory>
</PreferenceScreen>
</androidx.preference.PreferenceScreen>

View File

@ -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
@ -20,4 +23,4 @@
android:numeric="integer"
android:title="@string/settings_server_port"/>
</PreferenceCategory>
</PreferenceScreen>
</androidx.preference.PreferenceScreen>

View File

@ -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">
@ -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">
@ -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"
@ -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
@ -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
@ -118,4 +128,4 @@
android:title="@string/settings_manual"
android:summary="@string/settings_manual_summary"/>
</PreferenceCategory>
</PreferenceScreen>
</androidx.preference.PreferenceScreen>