Skip to content

Commit ac45d53

Browse files
authored
Merge pull request #15 from norkator/feature/basic-dark-theme
Implement very simple dark theme
2 parents a98f87d + 3914711 commit ac45d53

File tree

13 files changed

+137
-22
lines changed

13 files changed

+137
-22
lines changed

app/src/main/java/com/nitramite/apcupsdmonitor/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class Constants {
1010
final static String EVENTS_LOCATION = "/var/log/apcupsd.events";
1111

1212
// Shared preferences
13+
public final static String SP_USE_DARK_THEME = "SP_USE_DARK_THEME";
14+
1315
final static String SP_STATUS_COMMAND = "SP_STATUS_COMMAND";
1416
final static String SP_EVENTS_LOCATION = "SP_EVENTS_LOCATION";
1517
final static String SP_EVENTS_COLORING = "SP_EVENTS_COLORING";

app/src/main/java/com/nitramite/apcupsdmonitor/MainMenu.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@
99
import android.content.DialogInterface;
1010
import android.content.Intent;
1111
import android.content.SharedPreferences;
12+
import android.graphics.Color;
1213
import android.net.Uri;
1314
import android.os.StrictMode;
1415
import android.preference.PreferenceManager;
1516
import android.os.Bundle;
1617
import android.view.MenuItem;
1718
import android.view.View;
19+
import android.view.Window;
20+
import android.view.WindowManager;
1821
import android.widget.AdapterView;
1922
import android.widget.ListView;
2023
import android.widget.ProgressBar;
2124
import android.widget.Toast;
2225

2326
import androidx.annotation.Nullable;
2427
import androidx.appcompat.app.AppCompatActivity;
28+
import androidx.appcompat.app.AppCompatDelegate;
29+
import com.nitramite.utils.ThemeUtils;
2530
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
2631

2732
import com.android.billingclient.api.AcknowledgePurchaseParams;
@@ -97,18 +102,28 @@ public void onDestroy() {
97102

98103
@Override
99104
protected void onCreate(Bundle savedInstanceState) {
105+
// Set theme
106+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
107+
if (ThemeUtils.Theme.isDarkThemeForced(getBaseContext())) {
108+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
109+
Window window = getWindow();
110+
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
111+
window.setStatusBarColor(Color.BLACK);
112+
} else if (ThemeUtils.Theme.isAutoTheme(getBaseContext())) {
113+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
114+
} else {
115+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
116+
}
117+
100118
super.onCreate(savedInstanceState);
101119
setContentView(R.layout.activity_main_menu);
102120

103121
// Override thread policy
104122
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
105123
StrictMode.setThreadPolicy(policy);
106124

107-
// Shared preferences
108-
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
109125
setAppActivityRunning(true);
110126

111-
112127
// Floating action buttons
113128
FloatingActionButton floatingAddUpsBtn = findViewById(R.id.floatingAddNewUpsBtn);
114129
floatingAddUpsBtn.setOnClickListener(new View.OnClickListener() {

app/src/main/java/com/nitramite/apcupsdmonitor/Widget.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.nitramite.apcupsdmonitor;
22

3-
import android.app.ActivityManager;
43
import android.app.PendingIntent;
54
import android.appwidget.AppWidgetManager;
65
import android.appwidget.AppWidgetProvider;
76
import android.content.ComponentName;
87
import android.content.Context;
98
import android.content.Intent;
9+
import android.content.SharedPreferences;
1010
import android.graphics.Bitmap;
1111
import android.graphics.Canvas;
1212
import android.graphics.Matrix;
13+
import android.preference.PreferenceManager;
1314
import android.util.Log;
1415
import android.view.LayoutInflater;
1516
import android.view.View;
@@ -80,6 +81,9 @@ private ArrayList<UPS> getUpsData(DatabaseHelper databaseHelper) {
8081

8182

8283
private Bitmap createUpsViewBitmap(Context context, ArrayList<UPS> upsArrayList) {
84+
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
85+
boolean useDarkTheme = sharedPreferences.getBoolean(Constants.SP_USE_DARK_THEME, false);
86+
8387
LayoutInflater inflater = LayoutInflater.from(context);
8488

8589
LinearLayout mainLinearLayout = new LinearLayout(context);
@@ -89,7 +93,6 @@ private Bitmap createUpsViewBitmap(Context context, ArrayList<UPS> upsArrayList)
8993
);
9094
mainLinearLayout.setLayoutParams(layoutParams);
9195

92-
9396
for (int i = 0; i < upsArrayList.size(); i++) {
9497
View inflatedLayout = inflater.inflate(R.layout.ups_item_widget, null, true);
9598
TextView upsName = inflatedLayout.findViewById(R.id.upsName);
@@ -108,6 +111,13 @@ private Bitmap createUpsViewBitmap(Context context, ArrayList<UPS> upsArrayList)
108111
CustomGauge chargePB = inflatedLayout.findViewById(R.id.chargePB);
109112
chargePB.setValue(upsArrayList.get(i).getBatteryChargeLevelInteger());
110113

114+
if (useDarkTheme) {
115+
chargePB.setBackgroundColor(ContextCompat.getColor(context, R.color.widget_background));
116+
percentageTv.setTextColor(ContextCompat.getColor(context, R.color.whiteColor));
117+
} else {
118+
chargePB.setBackgroundColor(ContextCompat.getColor(context, R.color.whiteColor));
119+
}
120+
111121
percentageTv.setText(upsArrayList.get(i).getBatteryChargeLevelInteger() + "%");
112122

113123
mainLinearLayout.addView(inflatedLayout);
@@ -140,19 +150,4 @@ private void setBitmap(RemoteViews views, int resId, Bitmap bitmap) {
140150
views.setImageViewBitmap(resId, proxy);
141151
}
142152

143-
144-
// Check if service is running
145-
private boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
146-
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
147-
if (manager != null) {
148-
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
149-
if (serviceClass.getName().equals(service.service.getClassName())) {
150-
return true;
151-
}
152-
}
153-
}
154-
return false;
155-
}
156-
157-
158153
} // End of class
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.nitramite.utils;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
import android.content.res.Configuration;
6+
import android.preference.PreferenceManager;
7+
import android.util.Log;
8+
9+
import com.nitramite.apcupsdmonitor.Constants;
10+
11+
import static android.content.res.Configuration.UI_MODE_NIGHT_YES;
12+
13+
14+
public class ThemeUtils {
15+
16+
@SuppressWarnings("HardCodedStringLiteral")
17+
public enum Theme {
18+
BASIC(1),
19+
DARK(2),
20+
AUTO(3);
21+
22+
private int num;
23+
24+
Theme(int num) {
25+
this.num = num;
26+
}
27+
28+
public static Theme getCurrentTheme(Context context) {
29+
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
30+
int theme = preferences.getBoolean(Constants.SP_USE_DARK_THEME, false) ? 2 : 1;
31+
for (Theme b : Theme.values()) {
32+
if (b.num == theme) {
33+
34+
return b;
35+
}
36+
}
37+
return BASIC;
38+
}
39+
40+
public static boolean isDarkThemeForced(Context context) {
41+
Theme currentTheme = getCurrentTheme(context);
42+
return currentTheme == DARK;
43+
}
44+
45+
public static boolean isDarkTheme(Context context) {
46+
Theme currentTheme = getCurrentTheme(context);
47+
int nightModeFlags =
48+
context.getResources().getConfiguration().uiMode &
49+
Configuration.UI_MODE_NIGHT_MASK;
50+
Log.i("TU", "Nightmode: " + nightModeFlags);
51+
return (nightModeFlags == UI_MODE_NIGHT_YES) || currentTheme == DARK;
52+
}
53+
54+
public static boolean isAutoTheme(Context context) {
55+
Theme currentTheme = getCurrentTheme(context);
56+
return currentTheme == AUTO;
57+
}
58+
}
59+
60+
}

app/src/main/res/layout/activity_ups_viewer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@
532532
<TextView
533533
android:layout_width="wrap_content"
534534
android:layout_height="wrap_content"
535-
android:background="@color/whiteColor"
535+
android:background="?attr/eventsTextBackgroundColor"
536536
android:padding="2dp"
537537
android:text="@string/events" />
538538

app/src/main/res/layout/widget.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
<LinearLayout
88
android:layout_width="match_parent"
9-
android:layout_height="wrap_content">
9+
android:layout_height="wrap_content"
10+
tools:ignore="UselessParent">
1011

1112
<ImageView
1213
android:id="@+id/upsStatusImage"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<resources>
2+
3+
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
4+
<item name="colorPrimary">@color/colorPrimary</item>
5+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
6+
<item name="colorAccent">@color/colorAccent</item>
7+
<item name="android:navigationBarColor">@color/black</item>
8+
<item name="android:statusBarColor">@color/black</item>
9+
<item name="android:textColor">@color/color_white</item>
10+
<item name="android:textColorSecondary">@color/color_white</item>
11+
<item name="android:windowBackground">@color/background_darker</item>
12+
<item name="android:itemBackground">@color/background_darker</item>
13+
<item name="android:actionMenuTextColor">@color/color_white</item>
14+
<item name="eventsTextBackgroundColor">@color/black</item>
15+
</style>
16+
17+
<style name="AppTheme.NoActionBar">
18+
<item name="windowActionBar">false</item>
19+
<item name="windowNoTitle">true</item>
20+
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
21+
<item name="android:statusBarColor">@android:color/transparent</item>
22+
</style>
23+
24+
</resources>

app/src/main/res/values-ru/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@
139139
<string name="prefs_notification_service_summary">Проверять статусы ИБП в фоновом режиме</string>
140140
<string name="prefs_service_refresh_interval">Интервал обновления</string>
141141
<string name="prefs_service_refresh_interval_summary">Загружать информацию о статусе каждые X минут. Ноль означает минимально возможное значение пять минут.</string>
142+
<string name="prefs_dark_theme_title">Темная тема</string>
143+
<string name="prefs_dark_theme_description">После изменения перезапустите приложение</string>
142144

143145
<!-- Status service -->
144146
<string name="warning">Предупреждение</string>

app/src/main/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3+
4+
<attr name="eventsTextBackgroundColor" format="reference|color"/>
5+
36
<declare-styleable name="CustomGauge">
47
<attr name="gaugePointSize" format="integer" />
58
<attr name="gaugePointStartColor" format="color" />

app/src/main/res/values/colors.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<color name="whiteColor">#ffffff</color>
99
<color name="prefsEditTextColor">#000000</color>
1010
<color name="materialGray">#546e7a</color>
11+
<color name="black">#374046</color>
12+
<color name="color_white">#f9f9f9</color>
13+
<color name="background_darker">#303030</color>
14+
<color name="widget_background">#292929</color>
1115

1216
<!-- Bootstrap like color -->
1317
<color name="bootStrapPrimary">#347bb7</color>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@
139139
<string name="prefs_notification_service_summary">Check UPS statuses in the background</string>
140140
<string name="prefs_service_refresh_interval">Refresh interval</string>
141141
<string name="prefs_service_refresh_interval_summary">Load status information every X minutes. Zero means smallest possible value five minutes.</string>
142+
<string name="prefs_dark_theme_title">Dark theme</string>
143+
<string name="prefs_dark_theme_description">After change, please restart the application</string>
142144

143145
<!-- Status service -->
144146
<string name="warning">Warning</string>

app/src/main/res/values/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
88
<item name="colorAccent">@color/colorAccent</item>
99
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
10+
<item name="eventsTextBackgroundColor">@color/whiteColor</item>
1011
</style>
1112

1213
<!-- Preferences theme -->

app/src/main/res/xml/preferences.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090

9191
<PreferenceCategory android:title="Tweaks">
9292

93+
<SwitchPreference
94+
android:defaultValue="false"
95+
android:key="SP_USE_DARK_THEME"
96+
android:summary="@string/prefs_dark_theme_description"
97+
android:title="@string/prefs_dark_theme_title" />
98+
9399
<SwitchPreference
94100
android:defaultValue="true"
95101
android:key="SP_EVENTS_COLORING"

0 commit comments

Comments
 (0)