Implement dark theme

Closes #46
This commit is contained in:
emanuele-f 2021-03-30 12:24:12 +02:00
parent 62d1de0183
commit 049af45f42
12 changed files with 88 additions and 27 deletions

View File

@ -15,6 +15,7 @@
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"

View File

@ -0,0 +1,28 @@
package com.emanuelef.remote_capture;
import android.app.Application;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
import com.emanuelef.remote_capture.model.Prefs;
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String theme = prefs.getString(Prefs.PREF_APP_THEME, "");
if("".equals(theme)) {
if(Utils.isTv(this)) {
// Use the dark theme by default on Android TV
theme = "dark";
prefs.edit().putString(Prefs.PREF_APP_THEME, theme).apply();
} else
theme = "system";
}
Utils.setAppTheme(theme);
}
}

View File

@ -56,6 +56,7 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.SearchView;
import androidx.preference.PreferenceManager;
@ -156,6 +157,15 @@ public class Utils {
return config;
}
public static void setAppTheme(String theme) {
if(theme.equals("light"))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
else if(theme.equals("dark"))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
public static String proto2str(int proto) {
switch(proto) {
case 6: return "TCP";

View File

@ -34,6 +34,7 @@ import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.SwitchPreference;
import com.emanuelef.remote_capture.Utils;
import com.emanuelef.remote_capture.model.Prefs;
import com.emanuelef.remote_capture.R;
@ -176,6 +177,14 @@ public class SettingsActivity extends BaseActivity {
return false;
});
DropDownPreference appTheme = findPreference(Prefs.PREF_APP_THEME);
appTheme.setOnPreferenceChangeListener((preference, newValue) -> {
Utils.setAppTheme(newValue.toString());
return true;
});
}
}
}

View File

@ -21,6 +21,8 @@ package com.emanuelef.remote_capture.model;
import android.content.SharedPreferences;
import com.emanuelef.remote_capture.Utils;
public class Prefs {
public static final String DUMP_HTTP_SERVER = "http_server";
public static final String DUMP_UDP_EXPORTER = "udp_exporter";
@ -37,6 +39,7 @@ public class Prefs {
public static final String DEFAULT_DUMP_MODE = DUMP_HTTP_SERVER;
public static final String PREF_IPV6_ENABLED = "ipv6_enabled";
public static final String PREF_APP_LANGUAGE = "app_language";
public static final String PREF_APP_THEME = "app_theme";
public enum DumpMode {
NONE,

View File

@ -1,25 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="oval" >
<gradient
android:angle="360"
android:startColor="#543456"
android:endColor="#f800f0"
android:type="linear" />
<size android:width="24dp"
android:height="24dp"/>
</shape>
</item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="2.1"
android:thickness="2dp"
android:useLevel="false" >
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape android:shape="oval" >
<solid android:color="#fff" />
</shape>
</item>
</layer-list>
<gradient
android:angle="360"
android:startColor="#7E4382"
android:endColor="#f800f0"
android:type="linear" />
</shape>

View File

@ -33,6 +33,7 @@
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/colorTab"
app:tabMaxWidth="0dp"
app:tabGravity="fill" />

View File

@ -6,14 +6,12 @@
<item>pcap_file</item>
<item>udp_exporter</item>
</string-array>
<string-array name="pcap_dump_modes_labels">
<item>@string/no_dump</item>
<item>@string/http_server</item>
<item>@string/pcap_file</item>
<item>@string/udp_exporter</item>
</string-array>
<string-array name="pcap_dump_modes_descriptions">
<item>@string/no_dump_info</item>
<item>@string/http_server_info</item>
@ -25,9 +23,19 @@
<item>system</item>
<item>english</item>
</string-array>
<string-array name="app_languages_labels">
<item>@string/lang_system_default</item>
<item>@string/lang_english</item>
</string-array>
<string-array name="app_theme">
<item>system</item>
<item>light</item>
<item>dark</item>
</string-array>
<string-array name="app_theme_labels">
<item>@string/lang_system_default</item>
<item>@string/theme_light</item>
<item>@string/theme_dark</item>
</string-array>
</resources>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorTab">#7A5EBC</color>
<color name="colorPrimary">#512da8</color>
<color name="colorPrimaryDark">#361e73</color>
<color name="colorAccent">#D81B60</color>

View File

@ -118,5 +118,8 @@
<string name="lang_system_default">System default</string>
<string name="no_activity_file_selection">No app found to handle file selection</string>
<string name="file_saved_with_name">File %1$s successfully saved</string>
<string name="theme_light">Light</string>
<string name="theme_dark">Dark</string>
<string name="app_theme">Theme</string>
</resources>

View File

@ -1,6 +1,6 @@
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

View File

@ -82,6 +82,15 @@
app:summary="@string/enable_ipv6_summary"
app:defaultValue="false" />
<DropDownPreference
app:key="app_theme"
app:title="@string/app_theme"
android:entries="@array/app_theme_labels"
android:entryValues="@array/app_theme"
app:iconSpaceReserved="false"
app:defaultValue="system"
app:useSimpleSummaryProvider="true"/>
<DropDownPreference
app:key="app_language"
app:title="@string/app_language"