Skip to content

Commit

Permalink
Merge pull request #15 from norkator/feature/basic-dark-theme
Browse files Browse the repository at this point in the history
Implement very simple dark theme
  • Loading branch information
norkator authored Oct 8, 2020
2 parents a98f87d + 3914711 commit ac45d53
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 22 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/nitramite/apcupsdmonitor/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class Constants {
final static String EVENTS_LOCATION = "/var/log/apcupsd.events";

// Shared preferences
public final static String SP_USE_DARK_THEME = "SP_USE_DARK_THEME";

final static String SP_STATUS_COMMAND = "SP_STATUS_COMMAND";
final static String SP_EVENTS_LOCATION = "SP_EVENTS_LOCATION";
final static String SP_EVENTS_COLORING = "SP_EVENTS_COLORING";
Expand Down
21 changes: 18 additions & 3 deletions app/src/main/java/com/nitramite/apcupsdmonitor/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import com.nitramite.utils.ThemeUtils;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

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

@Override
protected void onCreate(Bundle savedInstanceState) {
// Set theme
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
if (ThemeUtils.Theme.isDarkThemeForced(getBaseContext())) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLACK);
} else if (ThemeUtils.Theme.isAutoTheme(getBaseContext())) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);

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

// Shared preferences
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
setAppActivityRunning(true);


// Floating action buttons
FloatingActionButton floatingAddUpsBtn = findViewById(R.id.floatingAddNewUpsBtn);
floatingAddUpsBtn.setOnClickListener(new View.OnClickListener() {
Expand Down
29 changes: 12 additions & 17 deletions app/src/main/java/com/nitramite/apcupsdmonitor/Widget.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.nitramite.apcupsdmonitor;

import android.app.ActivityManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -80,6 +81,9 @@ private ArrayList<UPS> getUpsData(DatabaseHelper databaseHelper) {


private Bitmap createUpsViewBitmap(Context context, ArrayList<UPS> upsArrayList) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
boolean useDarkTheme = sharedPreferences.getBoolean(Constants.SP_USE_DARK_THEME, false);

LayoutInflater inflater = LayoutInflater.from(context);

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


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

if (useDarkTheme) {
chargePB.setBackgroundColor(ContextCompat.getColor(context, R.color.widget_background));
percentageTv.setTextColor(ContextCompat.getColor(context, R.color.whiteColor));
} else {
chargePB.setBackgroundColor(ContextCompat.getColor(context, R.color.whiteColor));
}

percentageTv.setText(upsArrayList.get(i).getBatteryChargeLevelInteger() + "%");

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


// Check if service is running
private boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (manager != null) {
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
}
return false;
}


} // End of class
60 changes: 60 additions & 0 deletions app/src/main/java/com/nitramite/utils/ThemeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.nitramite.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.util.Log;

import com.nitramite.apcupsdmonitor.Constants;

import static android.content.res.Configuration.UI_MODE_NIGHT_YES;


public class ThemeUtils {

@SuppressWarnings("HardCodedStringLiteral")
public enum Theme {
BASIC(1),
DARK(2),
AUTO(3);

private int num;

Theme(int num) {
this.num = num;
}

public static Theme getCurrentTheme(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
int theme = preferences.getBoolean(Constants.SP_USE_DARK_THEME, false) ? 2 : 1;
for (Theme b : Theme.values()) {
if (b.num == theme) {

return b;
}
}
return BASIC;
}

public static boolean isDarkThemeForced(Context context) {
Theme currentTheme = getCurrentTheme(context);
return currentTheme == DARK;
}

public static boolean isDarkTheme(Context context) {
Theme currentTheme = getCurrentTheme(context);
int nightModeFlags =
context.getResources().getConfiguration().uiMode &
Configuration.UI_MODE_NIGHT_MASK;
Log.i("TU", "Nightmode: " + nightModeFlags);
return (nightModeFlags == UI_MODE_NIGHT_YES) || currentTheme == DARK;
}

public static boolean isAutoTheme(Context context) {
Theme currentTheme = getCurrentTheme(context);
return currentTheme == AUTO;
}
}

}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_ups_viewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/whiteColor"
android:background="?attr/eventsTextBackgroundColor"
android:padding="2dp"
android:text="@string/events" />

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
tools:ignore="UselessParent">

<ImageView
android:id="@+id/upsStatusImage"
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/res/values-night-v21/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<resources>

<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:navigationBarColor">@color/black</item>
<item name="android:statusBarColor">@color/black</item>
<item name="android:textColor">@color/color_white</item>
<item name="android:textColorSecondary">@color/color_white</item>
<item name="android:windowBackground">@color/background_darker</item>
<item name="android:itemBackground">@color/background_darker</item>
<item name="android:actionMenuTextColor">@color/color_white</item>
<item name="eventsTextBackgroundColor">@color/black</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
<string name="prefs_notification_service_summary">Проверять статусы ИБП в фоновом режиме</string>
<string name="prefs_service_refresh_interval">Интервал обновления</string>
<string name="prefs_service_refresh_interval_summary">Загружать информацию о статусе каждые X минут. Ноль означает минимально возможное значение пять минут.</string>
<string name="prefs_dark_theme_title">Темная тема</string>
<string name="prefs_dark_theme_description">После изменения перезапустите приложение</string>

<!-- Status service -->
<string name="warning">Предупреждение</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<attr name="eventsTextBackgroundColor" format="reference|color"/>

<declare-styleable name="CustomGauge">
<attr name="gaugePointSize" format="integer" />
<attr name="gaugePointStartColor" format="color" />
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<color name="whiteColor">#ffffff</color>
<color name="prefsEditTextColor">#000000</color>
<color name="materialGray">#546e7a</color>
<color name="black">#374046</color>
<color name="color_white">#f9f9f9</color>
<color name="background_darker">#303030</color>
<color name="widget_background">#292929</color>

<!-- Bootstrap like color -->
<color name="bootStrapPrimary">#347bb7</color>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
<string name="prefs_notification_service_summary">Check UPS statuses in the background</string>
<string name="prefs_service_refresh_interval">Refresh interval</string>
<string name="prefs_service_refresh_interval_summary">Load status information every X minutes. Zero means smallest possible value five minutes.</string>
<string name="prefs_dark_theme_title">Dark theme</string>
<string name="prefs_dark_theme_description">After change, please restart the application</string>

<!-- Status service -->
<string name="warning">Warning</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
<item name="eventsTextBackgroundColor">@color/whiteColor</item>
</style>

<!-- Preferences theme -->
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@

<PreferenceCategory android:title="Tweaks">

<SwitchPreference
android:defaultValue="false"
android:key="SP_USE_DARK_THEME"
android:summary="@string/prefs_dark_theme_description"
android:title="@string/prefs_dark_theme_title" />

<SwitchPreference
android:defaultValue="true"
android:key="SP_EVENTS_COLORING"
Expand Down

0 comments on commit ac45d53

Please sign in to comment.