Add dark theme

Thanks to an anonymous contributor
This commit is contained in:
xynngh 2020-07-24 23:11:33 +04:00
parent f701f18792
commit 5bc11077d7
9 changed files with 100 additions and 2 deletions

View File

@ -5,6 +5,8 @@ import android.app.Application;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import androidx.appcompat.app.AppCompatDelegate;
import dummydomain.yetanothercallblocker.data.Config; import dummydomain.yetanothercallblocker.data.Config;
import dummydomain.yetanothercallblocker.utils.DebuggingUtils; import dummydomain.yetanothercallblocker.utils.DebuggingUtils;
@ -23,6 +25,10 @@ public class App extends Application {
return settings; return settings;
} }
public static void setUiMode(int uiMode) {
AppCompatDelegate.setDefaultNightMode(uiMode);
}
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
@ -37,6 +43,8 @@ public class App extends Application {
settings.init(); settings.init();
Config.init(getDeviceProtectedStorageContext(), settings); Config.init(getDeviceProtectedStorageContext(), settings);
setUiMode(settings.getUiMode());
} }
private Context getDeviceProtectedStorageContext() { private Context getDeviceProtectedStorageContext() {

View File

@ -3,6 +3,7 @@ package dummydomain.yetanothercallblocker;
import android.content.Context; import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import dummydomain.yetanothercallblocker.data.CountryHelper; import dummydomain.yetanothercallblocker.data.CountryHelper;
@ -12,6 +13,7 @@ public class Settings extends GenericSettings {
public static final String PREF_INCOMING_CALL_NOTIFICATIONS = "incomingCallNotifications"; public static final String PREF_INCOMING_CALL_NOTIFICATIONS = "incomingCallNotifications";
public static final String PREF_BLOCK_CALLS = "blockCalls"; public static final String PREF_BLOCK_CALLS = "blockCalls";
public static final String PREF_USE_CONTACTS = "useContacts"; public static final String PREF_USE_CONTACTS = "useContacts";
public static final String PREF_UI_MODE = "uiMode";
public static final String PREF_NUMBER_OF_RECENT_CALLS = "numberOfRecentCalls"; public static final String PREF_NUMBER_OF_RECENT_CALLS = "numberOfRecentCalls";
public static final String PREF_NOTIFICATIONS_KNOWN = "showNotificationsForKnownCallers"; public static final String PREF_NOTIFICATIONS_KNOWN = "showNotificationsForKnownCallers";
public static final String PREF_NOTIFICATIONS_UNKNOWN = "showNotificationsForUnknownCallers"; public static final String PREF_NOTIFICATIONS_UNKNOWN = "showNotificationsForUnknownCallers";
@ -86,6 +88,14 @@ public class Settings extends GenericSettings {
setBoolean(PREF_USE_CONTACTS, use); setBoolean(PREF_USE_CONTACTS, use);
} }
public int getUiMode() {
return getInt(PREF_UI_MODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
public void setUiMode(int mode) {
setInt(PREF_UI_MODE, mode);
}
public int getNumberOfRecentCalls() { public int getNumberOfRecentCalls() {
return getInt(PREF_NUMBER_OF_RECENT_CALLS, 20); return getInt(PREF_NUMBER_OF_RECENT_CALLS, 20);
} }

View File

@ -26,6 +26,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import dummydomain.yetanothercallblocker.preference.IntListPreference;
import dummydomain.yetanothercallblocker.utils.DebuggingUtils; import dummydomain.yetanothercallblocker.utils.DebuggingUtils;
import dummydomain.yetanothercallblocker.work.UpdateScheduler; import dummydomain.yetanothercallblocker.work.UpdateScheduler;
@ -190,6 +191,12 @@ public class SettingsActivity extends AppCompatActivity
return true; return true;
}); });
IntListPreference uiModePref = requireNonNull(findPreference(Settings.PREF_UI_MODE));
uiModePref.setOnPreferenceChangeListener((preference, newValue) -> {
App.setUiMode(Integer.parseInt((String) newValue));
return true;
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Preference category = requireNonNull(findPreference(PREF_CATEGORY_NOTIFICATIONS)); Preference category = requireNonNull(findPreference(PREF_CATEGORY_NOTIFICATIONS));
category.setVisible(false); category.setVisible(false);

View File

@ -0,0 +1,45 @@
package dummydomain.yetanothercallblocker.preference;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import androidx.preference.ListPreference;
public class IntListPreference extends ListPreference {
public IntListPreference(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public IntListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public IntListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IntListPreference(Context context) {
super(context);
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getInt(index, 0);
}
@Override
protected void onSetInitialValue(Object defaultValue) {
int defaultInt = defaultValue != null ? (int) defaultValue : 0;
setValue(String.valueOf(getPersistedInt(defaultInt)));
}
@Override
protected boolean persistString(String value) {
return persistInt(!TextUtils.isEmpty(value) ? Integer.parseInt(value) : 0);
}
}

View File

@ -77,6 +77,10 @@
<string name="notification_background_operation">Выполняется процесс в фоне…</string> <string name="notification_background_operation">Выполняется процесс в фоне…</string>
<string name="use_contacts">Отображать имена контактов</string> <string name="use_contacts">Отображать имена контактов</string>
<string name="use_contacts_summary">Номера из телефонной книги никогда не блокируются, и имя контакта отображается рядом/вместо номера</string> <string name="use_contacts_summary">Номера из телефонной книги никогда не блокируются, и имя контакта отображается рядом/вместо номера</string>
<string name="ui_mode">Тема приложения</string>
<string name="ui_mode_day">Светлая</string>
<string name="ui_mode_night">Тёмная</string>
<string name="ui_mode_auto">Определяется системой</string>
<string name="number_of_recent_calls">Кол-во недавних вызовов</string> <string name="number_of_recent_calls">Кол-во недавних вызовов</string>
<string name="number_of_recent_calls_summary">Количество недавних вызовов на основном экране</string> <string name="number_of_recent_calls_summary">Количество недавних вызовов на основном экране</string>
<string name="notification_incoming_call_contact">Контакт</string> <string name="notification_incoming_call_contact">Контакт</string>

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ui_mode_entries">
<item>@string/ui_mode_day</item>
<item>@string/ui_mode_night</item>
<item>@string/ui_mode_auto</item>
</string-array>
<string-array name="ui_mode_entry_values">
<item>1</item>
<item>2</item>
<item>-1</item>
</string-array>
</resources>

View File

@ -98,6 +98,10 @@
<string name="auto_updates_summary">Automatically receive daily DB updates (these are incremental/delta updates, so they consume very little traffic)</string> <string name="auto_updates_summary">Automatically receive daily DB updates (these are incremental/delta updates, so they consume very little traffic)</string>
<string name="use_contacts">Use contacts</string> <string name="use_contacts">Use contacts</string>
<string name="use_contacts_summary">Numbers present in the phone book are never blocked and the contact name is displayed next to/instead of a number throughout the app</string> <string name="use_contacts_summary">Numbers present in the phone book are never blocked and the contact name is displayed next to/instead of a number throughout the app</string>
<string name="ui_mode">UI theme</string>
<string name="ui_mode_day">Light</string>
<string name="ui_mode_night">Dark</string>
<string name="ui_mode_auto">Follow system</string>
<string name="number_of_recent_calls">Number of recent calls</string> <string name="number_of_recent_calls">Number of recent calls</string>
<string name="number_of_recent_calls_summary">The number of recent calls to display on the main screen</string> <string name="number_of_recent_calls_summary">The number of recent calls to display on the main screen</string>

View File

@ -1,7 +1,7 @@
<resources> <resources>
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. --> <!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
@ -16,7 +16,7 @@
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.DayNight" />
<style name="DialogBackgroundTheme" parent="AppTheme.NoActionBar"> <style name="DialogBackgroundTheme" parent="AppTheme.NoActionBar">
<item name="android:windowIsTranslucent">true</item> <item name="android:windowIsTranslucent">true</item>

View File

@ -15,6 +15,13 @@
app:key="useContacts" app:key="useContacts"
app:summary="@string/use_contacts_summary" app:summary="@string/use_contacts_summary"
app:title="@string/use_contacts" /> app:title="@string/use_contacts" />
<dummydomain.yetanothercallblocker.preference.IntListPreference
app:defaultValue="-1"
app:entries="@array/ui_mode_entries"
app:entryValues="@array/ui_mode_entry_values"
app:key="uiMode"
app:title="@string/ui_mode"
app:useSimpleSummaryProvider="true" />
<dummydomain.yetanothercallblocker.preference.IntEditTextPreference <dummydomain.yetanothercallblocker.preference.IntEditTextPreference
app:defaultValue="20" app:defaultValue="20"
app:key="numberOfRecentCalls" app:key="numberOfRecentCalls"