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); deactivateService(instance);
return false; return false;
} else { } else {
activateService(instance); prepareAndActivateService(instance);
return true; return true;
} }
} }
public static boolean activateService(Context context) { public static boolean prepareAndActivateService(Context context) {
Intent intent = VpnService.prepare(context); Intent intent = VpnService.prepare(context);
if (intent != null) { if (intent != null) {
return false; return false;
} else { } else {
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary()); activateService(context);
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
context.startService(Daedalus.getServiceIntent(context).setAction(DaedalusVpnService.ACTION_ACTIVATE));
return true; 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) { public static void deactivateService(Context context) {
context.startService(getServiceIntent(context).setAction(DaedalusVpnService.ACTION_DEACTIVATE)); context.startService(getServiceIntent(context).setAction(DaedalusVpnService.ACTION_DEACTIVATE));
context.stopService(getServiceIntent(context)); 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) { public void onActivityResult(int request, int result, Intent data) {
if (result == Activity.RESULT_OK) { if (result == Activity.RESULT_OK) {
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary()); Daedalus.activateService(Daedalus.getInstance());
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary());
Daedalus.getInstance().startService(Daedalus.getServiceIntent(getApplicationContext()).setAction(DaedalusVpnService.ACTION_ACTIVATE));
updateMainButton(R.string.button_text_deactivate); updateMainButton(R.string.button_text_deactivate);
Daedalus.updateShortcut(getApplicationContext()); Daedalus.updateShortcut(getApplicationContext());
} }

View File

@ -66,6 +66,7 @@
<string name="settings_dont_build_doh_cache">不缓存 DoH DNS 的 IP地址</string> <string name="settings_dont_build_doh_cache">不缓存 DoH DNS 的 IP地址</string>
<string name="settings_dark_theme">使用暗主题</string> <string name="settings_dark_theme">使用暗主题</string>
<string name="settings_dns_query_method">DNS 查询方式</string> <string name="settings_dns_query_method">DNS 查询方式</string>
<string name="settings_foreground">运行前台服务</string>
<string name="settings_rule_name">规则名称</string> <string name="settings_rule_name">规则名称</string>
<string name="settings_rule_type">规则类型</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_built_in">内置</string>
<string name="settings_rule_import_external">外部</string> <string name="settings_rule_import_external">外部</string>
<string name="settings_dns_query_method">DNS 查询方式</string> <string name="settings_dns_query_method">DNS 查询方式</string>
<string name="settings_foreground">運行前臺服務</string>
<string name="settings_server_name">伺服器名稱</string> <string name="settings_server_name">伺服器名稱</string>
<string name="settings_server_address">伺服器網址</string> <string name="settings_server_address">伺服器網址</string>

View File

@ -65,6 +65,7 @@
<string name="settings_debug_output">Debug Output</string> <string name="settings_debug_output">Debug Output</string>
<string name="settings_dark_theme">Use Dark Theme</string> <string name="settings_dark_theme">Use Dark Theme</string>
<string name="settings_dns_query_method">DNS Query Method</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_tcp">TCP</string>
<string name="settings_dns_udp">UDP</string> <string name="settings_dns_udp">UDP</string>

View File

@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-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">
<EditTextPreference <EditTextPreference
android:key="ruleName" android:key="ruleName"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
android:singleLine="true" android:singleLine="true"
android:title="@string/settings_rule_name"/> android:title="@string/settings_rule_name"/>
<PreferenceCategory <PreferenceCategory
app:iconSpaceReserved="false"
android:key="rulSettings" android:key="rulSettings"
android:title="@string/settings_rule"> android:title="@string/settings_rule">
<ListPreference <ListPreference
@ -27,7 +30,9 @@
android:key="ruleSync" android:key="ruleSync"
android:title="@string/settings_rule_sync"/> android:title="@string/settings_rule_sync"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
app:iconSpaceReserved="false"
android:key="Import" android:key="Import"
android:title="@string/settings_rule_import"> android:title="@string/settings_rule_import">
<ListPreference <ListPreference
@ -37,4 +42,4 @@
android:key="ruleImportExternal" android:key="ruleImportExternal"
android:title="@string/settings_rule_import_external"/> android:title="@string/settings_rule_import_external"/>
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </androidx.preference.PreferenceScreen>

View File

@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-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">
<EditTextPreference <EditTextPreference
android:key="serverName" android:key="serverName"
android:selectAllOnFocus="true" android:selectAllOnFocus="true"
android:singleLine="true" android:singleLine="true"
android:title="@string/settings_server_name"/> android:title="@string/settings_server_name"/>
<PreferenceCategory <PreferenceCategory
app:iconSpaceReserved="false"
android:key="serverSettings" android:key="serverSettings"
android:title="@string/settings_server"> android:title="@string/settings_server">
<EditTextPreference <EditTextPreference
@ -20,4 +23,4 @@
android:numeric="integer" android:numeric="integer"
android:title="@string/settings_server_port"/> android:title="@string/settings_server_port"/>
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </androidx.preference.PreferenceScreen>

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-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 <PreferenceCategory
app:iconSpaceReserved="false"
android:key="settingsServer" android:key="settingsServer"
android:title="@string/settings_server"> android:title="@string/settings_server">
@ -23,7 +24,9 @@
android:singleLine="true" android:singleLine="true"
android:title="@string/settings_dns_test_servers"/> android:title="@string/settings_dns_test_servers"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
app:iconSpaceReserved="false"
android:key="settings_system" android:key="settings_system"
android:title="@string/settings_system"> android:title="@string/settings_system">
@ -39,6 +42,10 @@
android:key="settings_dark_theme" android:key="settings_dark_theme"
android:title="@string/settings_dark_theme" android:title="@string/settings_dark_theme"
android:defaultValue="false"/> android:defaultValue="false"/>
<SwitchPreference
android:key="settings_foreground"
android:title="@string/settings_foreground"
android:defaultValue="false"/>
<EditTextPreference <EditTextPreference
android:key="settings_log_size" android:key="settings_log_size"
android:selectAllOnFocus="false" android:selectAllOnFocus="false"
@ -49,25 +56,27 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
android:key="settings_app_filter" app:iconSpaceReserved="false"
android:title="@string/settings_app_filter"> android:key="settings_app_filter"
android:title="@string/settings_app_filter">
<SwitchPreference <SwitchPreference
android:key="settings_app_filter_switch" android:key="settings_app_filter_switch"
android:title="@string/settings_app_filter" android:title="@string/settings_app_filter"
android:defaultValue="false"/> android:defaultValue="false"/>
<SwitchPreference <SwitchPreference
android:key="settings_app_filter_mode_switch" android:key="settings_app_filter_mode_switch"
android:title="@string/settings_app_filter_mode" android:title="@string/settings_app_filter_mode"
android:defaultValue="false" android:defaultValue="false"
android:enabled="false"/> android:enabled="false"/>
<org.itxtech.daedalus.widget.ClickPreference <org.itxtech.daedalus.widget.ClickPreference
android:key="settings_app_filter_list" android:key="settings_app_filter_list"
android:title="@string/settings_app_filter_list" android:title="@string/settings_app_filter_list"
android:summary="@string/settings_app_filter_list_summary" android:summary="@string/settings_app_filter_list_summary"
android:enabled="false"/> android:enabled="false"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
app:iconSpaceReserved="false"
android:key="settings_advanced" android:key="settings_advanced"
android:title="@string/settings_advanced"> android:title="@string/settings_advanced">
<SwitchPreference <SwitchPreference
@ -100,6 +109,7 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory <PreferenceCategory
app:iconSpaceReserved="false"
android:key="settings_help" android:key="settings_help"
android:title="@string/settings_help_and_support"> android:title="@string/settings_help_and_support">
<org.itxtech.daedalus.widget.ClickPreference <org.itxtech.daedalus.widget.ClickPreference
@ -118,4 +128,4 @@
android:title="@string/settings_manual" android:title="@string/settings_manual"
android:summary="@string/settings_manual_summary"/> android:summary="@string/settings_manual_summary"/>
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </androidx.preference.PreferenceScreen>