Add dark mode

This commit is contained in:
Jannis Mattheis
2020-05-08 18:13:18 +02:00
parent 91be6fa72e
commit ee36b6aa8a
19 changed files with 205 additions and 27 deletions

View File

@@ -41,6 +41,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta5' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta5'
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0' implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.preference:preference:1.1.0-rc01'
testImplementation 'junit:junit:4.13' testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

View File

@@ -41,6 +41,10 @@
android:name=".log.LogsActivity" android:name=".log.LogsActivity"
android:label="@string/title_activity_logs" android:label="@string/title_activity_logs"
android:theme="@style/AppTheme.NoActionBar" /> android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".settings.SettingsActivity"
android:theme="@style/AppTheme.NoActionBar"
android:label="@string/title_activity_settings" />
<service android:name=".service.WebSocketService" /> <service android:name=".service.WebSocketService" />

View File

@@ -8,6 +8,7 @@ import android.os.Bundle;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;
import com.github.gotify.NotificationSupport; import com.github.gotify.NotificationSupport;
import com.github.gotify.R; import com.github.gotify.R;
import com.github.gotify.Settings; import com.github.gotify.Settings;
@@ -21,6 +22,7 @@ import com.github.gotify.log.UncaughtExceptionHandler;
import com.github.gotify.login.LoginActivity; import com.github.gotify.login.LoginActivity;
import com.github.gotify.messages.MessagesActivity; import com.github.gotify.messages.MessagesActivity;
import com.github.gotify.service.WebSocketService; import com.github.gotify.service.WebSocketService;
import com.github.gotify.settings.ThemeHelper;
import static com.github.gotify.api.Callback.callInUI; import static com.github.gotify.api.Callback.callInUI;
@@ -31,6 +33,13 @@ public class InitializationActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Log.init(this); Log.init(this);
String theme =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(
getString(R.string.setting_key_theme),
getString(R.string.theme_default));
ThemeHelper.setTheme(this, theme);
setContentView(R.layout.splash); setContentView(R.layout.splash);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

View File

@@ -14,6 +14,7 @@ import android.widget.ProgressBar;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ContextThemeWrapper;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.OnClick; import butterknife.OnClick;
@@ -135,7 +136,7 @@ public class LoginActivity extends AppCompatActivity {
} }
public void showHttpWarning() { public void showHttpWarning() {
new AlertDialog.Builder(this) new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppTheme_Dialog))
.setTitle(R.string.warning) .setTitle(R.string.warning)
.setCancelable(true) .setCancelable(true)
.setMessage(R.string.http_warning) .setMessage(R.string.http_warning)
@@ -273,7 +274,7 @@ public class LoginActivity extends AppCompatActivity {
EditText clientName = new EditText(this); EditText clientName = new EditText(this);
clientName.setText(Build.MODEL); clientName.setText(Build.MODEL);
new AlertDialog.Builder(this) new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppTheme_Dialog))
.setTitle(R.string.create_client_title) .setTitle(R.string.create_client_title)
.setMessage(R.string.create_client_message) .setMessage(R.string.create_client_message)
.setView(clientName) .setView(clientName)

View File

@@ -22,6 +22,7 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat; import androidx.core.graphics.drawable.DrawableCompat;
@@ -61,6 +62,7 @@ import com.github.gotify.messages.provider.MessageState;
import com.github.gotify.messages.provider.MessageWithImage; import com.github.gotify.messages.provider.MessageWithImage;
import com.github.gotify.picasso.PicassoDataRequestHandler; import com.github.gotify.picasso.PicassoDataRequestHandler;
import com.github.gotify.service.WebSocketService; import com.github.gotify.service.WebSocketService;
import com.github.gotify.settings.SettingsActivity;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.BaseTransientBottomBar; import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
@@ -309,7 +311,7 @@ public class MessagesActivity extends AppCompatActivity
startLoading(); startLoading();
toolbar.setSubtitle(""); toolbar.setSubtitle("");
} else if (id == R.id.logout) { } else if (id == R.id.logout) {
new AlertDialog.Builder(this) new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppTheme_Dialog))
.setTitle(R.string.logout) .setTitle(R.string.logout)
.setMessage(getString(R.string.logout_confirm)) .setMessage(getString(R.string.logout_confirm))
.setPositiveButton(R.string.yes, this::doLogout) .setPositiveButton(R.string.yes, this::doLogout)
@@ -317,6 +319,8 @@ public class MessagesActivity extends AppCompatActivity
.show(); .show();
} else if (id == R.id.nav_logs) { } else if (id == R.id.nav_logs) {
startActivity(new Intent(this, LogsActivity.class)); startActivity(new Intent(this, LogsActivity.class));
} else if (id == R.id.settings) {
startActivity(new Intent(this, SettingsActivity.class));
} }
drawer.closeDrawer(GravityCompat.START); drawer.closeDrawer(GravityCompat.START);
@@ -326,6 +330,7 @@ public class MessagesActivity extends AppCompatActivity
public void doLogout(DialogInterface dialog, int which) { public void doLogout(DialogInterface dialog, int which) {
setContentView(R.layout.splash); setContentView(R.layout.splash);
new DeleteClientAndNavigateToLogin().execute(); new DeleteClientAndNavigateToLogin().execute();
finish();
} }
private void startLoading() { private void startLoading() {

View File

@@ -0,0 +1,56 @@
package com.github.gotify.settings;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import com.github.gotify.R;
public class SettingsActivity extends AppCompatActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
setSupportActionBar(findViewById(R.id.toolbar));
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (getString(R.string.setting_key_theme).equals(key)) {
ThemeHelper.setTheme(
this, sharedPreferences.getString(key, getString(R.string.theme_default)));
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}

View File

@@ -0,0 +1,27 @@
package com.github.gotify.settings;
import android.content.Context;
import android.os.Build;
import androidx.appcompat.app.AppCompatDelegate;
import com.github.gotify.R;
public final class ThemeHelper {
private ThemeHelper() {}
public static void setTheme(Context context, String newTheme) {
AppCompatDelegate.setDefaultNightMode(ofKey(context, newTheme));
}
private static int ofKey(Context context, String newTheme) {
if (context.getString(R.string.theme_dark).equals(newTheme)) {
return AppCompatDelegate.MODE_NIGHT_YES;
}
if (context.getString(R.string.theme_light).equals(newTheme)) {
return AppCompatDelegate.MODE_NIGHT_NO;
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
return AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
}
return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
}
}

View File

@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#dfe3e4"
android:endColor="#ecf0f1"
android:angle="0"/>
<corners
android:radius="0dp"/>
</shape>

View File

@@ -1,4 +1,6 @@
<vector android:height="24dp" android:viewportHeight="20" <vector android:height="24dp" android:viewportHeight="20"
android:viewportWidth="20" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> android:viewportWidth="20" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M15.95,10.78c0.03,-0.25 0.05,-0.51 0.05,-0.78s-0.02,-0.53 -0.06,-0.78l1.69,-1.32c0.15,-0.12 0.19,-0.34 0.1,-0.51l-1.6,-2.77c-0.1,-0.18 -0.31,-0.24 -0.49,-0.18l-1.99,0.8c-0.42,-0.32 -0.86,-0.58 -1.35,-0.78L12,2.34c-0.03,-0.2 -0.2,-0.34 -0.4,-0.34H8.4c-0.2,0 -0.36,0.14 -0.39,0.34l-0.3,2.12c-0.49,0.2 -0.94,0.47 -1.35,0.78l-1.99,-0.8c-0.18,-0.07 -0.39,0 -0.49,0.18l-1.6,2.77c-0.1,0.18 -0.06,0.39 0.1,0.51l1.69,1.32c-0.04,0.25 -0.07,0.52 -0.07,0.78s0.02,0.53 0.06,0.78L2.37,12.1c-0.15,0.12 -0.19,0.34 -0.1,0.51l1.6,2.77c0.1,0.18 0.31,0.24 0.49,0.18l1.99,-0.8c0.42,0.32 0.86,0.58 1.35,0.78l0.3,2.12c0.04,0.2 0.2,0.34 0.4,0.34h3.2c0.2,0 0.37,-0.14 0.39,-0.34l0.3,-2.12c0.49,-0.2 0.94,-0.47 1.35,-0.78l1.99,0.8c0.18,0.07 0.39,0 0.49,-0.18l1.6,-2.77c0.1,-0.18 0.06,-0.39 -0.1,-0.51l-1.67,-1.32zM10,13c-1.65,0 -3,-1.35 -3,-3s1.35,-3 3,-3 3,1.35 3,3 -1.35,3 -3,3z"/> <path
android:fillColor="@color/icons"
android:pathData="M15.95,10.78c0.03,-0.25 0.05,-0.51 0.05,-0.78s-0.02,-0.53 -0.06,-0.78l1.69,-1.32c0.15,-0.12 0.19,-0.34 0.1,-0.51l-1.6,-2.77c-0.1,-0.18 -0.31,-0.24 -0.49,-0.18l-1.99,0.8c-0.42,-0.32 -0.86,-0.58 -1.35,-0.78L12,2.34c-0.03,-0.2 -0.2,-0.34 -0.4,-0.34H8.4c-0.2,0 -0.36,0.14 -0.39,0.34l-0.3,2.12c-0.49,0.2 -0.94,0.47 -1.35,0.78l-1.99,-0.8c-0.18,-0.07 -0.39,0 -0.49,0.18l-1.6,2.77c-0.1,0.18 -0.06,0.39 0.1,0.51l1.69,1.32c-0.04,0.25 -0.07,0.52 -0.07,0.78s0.02,0.53 0.06,0.78L2.37,12.1c-0.15,0.12 -0.19,0.34 -0.1,0.51l1.6,2.77c0.1,0.18 0.31,0.24 0.49,0.18l1.99,-0.8c0.42,0.32 0.86,0.58 1.35,0.78l0.3,2.12c0.04,0.2 0.2,0.34 0.4,0.34h3.2c0.2,0 0.37,-0.14 0.39,-0.34l0.3,-2.12c0.49,-0.2 0.94,-0.47 1.35,-0.78l1.99,0.8c0.18,0.07 0.39,0 0.49,-0.18l1.6,-2.77c0.1,-0.18 0.06,-0.39 -0.1,-0.51l-1.67,-1.32zM10,13c-1.65,0 -3,-1.35 -3,-3s1.35,-3 3,-3 3,1.35 3,3 -1.35,3 -3,3z"/>
</vector> </vector>

View File

@@ -6,22 +6,22 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:fillViewport="true"> android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@drawable/gradient"
android:orientation="vertical" android:orientation="vertical"
tools:context=".login.LoginActivity" tools:context=".login.LoginActivity"
tools:layout_editor_absoluteY="25dp"> tools:layout_editor_absoluteY="25dp">
<Button <Button
android:id="@+id/open_logs" android:id="@+id/open_logs"
android:layout_width="50dp" android:layout_width="70dp"
android:layout_height="40dp" android:layout_height="40dp"
android:background="@null" android:insetTop="0dp"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:text="@string/logs" android:text="@string/logs"
app:backgroundTint="#00FFFFFF"
android:textColor="@color/colorNavPrimary"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />

View File

@@ -91,6 +91,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="start" android:layout_gravity="start"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
app:theme="@style/AppTheme.Nav"
app:headerLayout="@layout/nav_header_drawer" app:headerLayout="@layout/nav_header_drawer"
app:menu="@menu/messages_menu" /> app:menu="@menu/messages_menu" />

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<include
layout="@layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.drawerlayout.widget.DrawerLayout>

View File

@@ -16,15 +16,20 @@
</group> </group>
<group android:checkableBehavior="single"> <group android:checkableBehavior="single">
<item <item
android:icon="@drawable/ic_power_setting" android:icon="@drawable/ic_settings"
android:orderInCategory="2" android:orderInCategory="2"
android:id="@+id/logout" android:id="@+id/settings"
android:title="@string/logout" /> android:title="@string/title_activity_settings" />
<item <item
android:orderInCategory="2" android:orderInCategory="2"
android:icon="@drawable/ic_bug_report" android:icon="@drawable/ic_bug_report"
android:id="@+id/nav_logs" android:id="@+id/nav_logs"
android:title="@string/logs" /> android:title="@string/logs" />
<item
android:icon="@drawable/ic_power_setting"
android:orderInCategory="2"
android:id="@+id/logout"
android:title="@string/logout" />
</group> </group>
</menu> </menu>

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorNavPrimary">#000000</color>
<color name="colorPrimaryDark">#3867d6</color>
<color name="colorAccent">#1c49b4</color>
<color name="icons">#434343</color>
<color name="swipeBackground">#E74C3C</color>
<color name="swipeIcon">#FFFFFF</color>
</resources>

View File

@@ -0,0 +1,10 @@
<resources>
<string-array name="mode">
<item>@string/theme_light</item>
<item>@string/theme_dark</item>
<item>@string/theme_default</item>
</string-array>
<string name="theme_light">Light</string>
<string name="theme_dark">Dark</string>
<string name="theme_default">System Default</string>
</resources>

View File

@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<color name="colorPrimary">#3F51B5</color> <color name="colorPrimary">#3F51B5</color>
<color name="colorNavPrimary">#FFFFFF</color>
<color name="colorPrimaryDark">#3867d6</color> <color name="colorPrimaryDark">#3867d6</color>
<color name="colorAccent">#1c49b4</color> <color name="colorAccent">#1c49b4</color>
<color name="icons">#434343</color> <color name="icons">#797979</color>
<color name="swipeBackground">#E74C3C</color> <color name="swipeBackground">#E74C3C</color>
<color name="swipeIcon">#FFFFFF</color> <color name="swipeIcon">#FFFFFF</color>
</resources> </resources>

View File

@@ -64,4 +64,8 @@
<string name="websocket_no_network">Waiting for network</string> <string name="websocket_no_network">Waiting for network</string>
<string name="connection">%s@%s</string> <string name="connection">%s@%s</string>
<string name="no_messages_yet">There are no messages, yet.\nSend a message to Gotify\nand it will appear here.</string> <string name="no_messages_yet">There are no messages, yet.\nSend a message to Gotify\nand it will appear here.</string>
<string name="title_activity_settings">Settings</string>
<string name="settings_appearance">Appearance</string>
<string name="setting_theme">Theme</string>
<string name="setting_key_theme">theme</string>
</resources> </resources>

View File

@@ -1,20 +1,27 @@
<resources> <resources>
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<!-- 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>
<item name="colorAccent">@color/colorAccent</item> <item name="colorAccent">@color/colorAccent</item>
</style> </style>
<style name="AppTheme.Nav">
<item name="colorPrimary">@color/colorNavPrimary</item>
</style>
<style name="AppTheme.NoActionBar"> <style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item> <item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item> <item name="windowNoTitle">true</item>
</style> </style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> <style name="AppTheme.PopupOverlay" parent="AppTheme" />
<style name="AppTheme.Dialog" parent="AppTheme" >
<item name="colorPrimary">@color/colorNavPrimary</item>
</style>
</resources> </resources>

View File

@@ -0,0 +1,13 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/settings_appearance" >
<ListPreference
android:defaultValue="@string/theme_default"
android:entries="@array/mode"
android:entryValues="@array/mode"
android:key="@string/setting_key_theme"
android:title="@string/setting_theme" />
</PreferenceCategory>
</PreferenceScreen>