Skip to content

Commit

Permalink
Update system theme
Browse files Browse the repository at this point in the history
  • Loading branch information
billthefarmer committed Jun 11, 2023
1 parent f977e64 commit c126b39
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 38 deletions.
34 changes: 29 additions & 5 deletions src/main/java/org/billthefarmer/scope/HelpActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
Expand All @@ -41,8 +42,6 @@
// HelpActivity
public class HelpActivity extends Activity
{
private static final String PREF_DARK = "pref_dark";

// On create
@Override
@SuppressWarnings("deprecation")
Expand All @@ -54,11 +53,36 @@ public void onCreate(Bundle savedInstanceState)
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);

boolean dark =
preferences.getBoolean(PREF_DARK, false);
int theme =
Integer.parseInt(preferences.getString(MainActivity.PREF_THEME, "0"));

Configuration config = getResources().getConfiguration();
int night = config.uiMode & Configuration.UI_MODE_NIGHT_MASK;

if (!dark)
switch (theme)
{
case MainActivity.LIGHT:
setTheme(R.style.AppTheme);
break;

case MainActivity.DARK:
setTheme(R.style.AppDarkTheme);
break;

case MainActivity.SYSTEM:
switch (night)
{
case Configuration.UI_MODE_NIGHT_NO:
setTheme(R.style.AppTheme);
break;

case Configuration.UI_MODE_NIGHT_YES:
setTheme(R.style.AppDarkTheme);
break;
}
break;
}

setContentView(R.layout.help);

TextView view = findViewById(R.id.help);
Expand Down
55 changes: 43 additions & 12 deletions src/main/java/org/billthefarmer/scope/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
Expand All @@ -55,13 +56,14 @@
// MainActivity
public class MainActivity extends Activity
{
private static final String PREF_BRIGHT = "pref_bright";
private static final String PREF_DARK = "pref_dark";
private static final String PREF_INPUT = "pref_input";
private static final String PREF_SCREEN = "pref_screen";
private static final String PREF_SINGLE = "pref_single";
private static final String PREF_STORAGE = "pref_storage";
private static final String PREF_TIMEBASE = "pref_timebase";
public static final String PREF_ABOUT = "pref_about";
public static final String PREF_THEME = "pref_theme";
public static final String PREF_INPUT = "pref_input";
public static final String PREF_SCREEN = "pref_screen";
public static final String PREF_BRIGHT = "pref_bright";
public static final String PREF_SINGLE = "pref_single";
public static final String PREF_STORAGE = "pref_storage";
public static final String PREF_TIMEBASE = "pref_timebase";

private static final String TAG = "Scope";

Expand Down Expand Up @@ -100,6 +102,10 @@ public class MainActivity extends Activity

public static final int VERSION_CODE_S_V2 = 32;

public static final int LIGHT = 0;
public static final int DARK = 1;
public static final int SYSTEM = 2;

protected static final int SIZE = 20;
protected static final int DEFAULT_TIMEBASE = 3;
protected static final float SMALL_SCALE = 200;
Expand All @@ -118,7 +124,8 @@ public class MainActivity extends Activity
private Audio audio;
private Toast toast;

private boolean dark;
// private boolean dark;
private int theme;

// On create
@Override
Expand All @@ -129,8 +136,32 @@ protected void onCreate(Bundle savedInstanceState)
// Get preferences
getPreferences();

if (!dark)
Configuration config = getResources().getConfiguration();
int night = config.uiMode & Configuration.UI_MODE_NIGHT_MASK;

switch (theme)
{
case LIGHT:
setTheme(R.style.AppTheme);
break;

case DARK:
setTheme(R.style.AppDarkTheme);
break;

case SYSTEM:
switch (night)
{
case Configuration.UI_MODE_NIGHT_NO:
setTheme(R.style.AppTheme);
break;

case Configuration.UI_MODE_NIGHT_YES:
setTheme(R.style.AppDarkTheme);
break;
}
break;
}

setContentView(R.layout.activity_main);

Expand Down Expand Up @@ -588,12 +619,12 @@ protected void onResume()
{
super.onResume();

boolean theme = dark;
int last = theme;

// Get preferences
getPreferences();

if (theme != dark && Build.VERSION.SDK_INT != Build.VERSION_CODES.M)
if (last != theme && Build.VERSION.SDK_INT != Build.VERSION_CODES.M)
recreate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
Expand Down Expand Up @@ -677,7 +708,7 @@ void getPreferences()
else
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

dark = preferences.getBoolean(PREF_DARK, false);
theme = Integer.parseInt(preferences.getString(PREF_THEME, "0"));
}

// Save preferences
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/org/billthefarmer/scope/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.app.ActionBar;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MenuItem;
Expand All @@ -46,11 +47,35 @@ protected void onCreate(Bundle savedInstanceState)
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);

boolean dark =
preferences.getBoolean(KEY_PREF_DARK, false);
int theme =
Integer.parseInt(preferences.getString(MainActivity.PREF_THEME, "0"));

if (!dark)
Configuration config = getResources().getConfiguration();
int night = config.uiMode & Configuration.UI_MODE_NIGHT_MASK;

switch (theme)
{
case MainActivity.LIGHT:
setTheme(R.style.AppTheme);
break;

case MainActivity.DARK:
setTheme(R.style.AppDarkTheme);
break;

case MainActivity.SYSTEM:
switch (night)
{
case Configuration.UI_MODE_NIGHT_NO:
setTheme(R.style.AppTheme);
break;

case Configuration.UI_MODE_NIGHT_YES:
setTheme(R.style.AppDarkTheme);
break;
}
break;
}

// Display the fragment as the main content.
getFragmentManager().beginTransaction()
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/org/billthefarmer/scope/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
public class SettingsFragment extends android.preference.PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener
{
private static final String KEY_PREF_INPUT = "pref_input";
private static final String KEY_PREF_DARK = "pref_dark";
private static final String KEY_PREF_ABOUT = "pref_about";

// onCreate
@Override
public void onCreate(Bundle savedInstanceState)
Expand All @@ -55,11 +51,11 @@ public void onCreate(Bundle savedInstanceState)
PreferenceManager.getDefaultSharedPreferences(getActivity());

ListPreference preference =
(ListPreference) findPreference(KEY_PREF_INPUT);
(ListPreference) findPreference(MainActivity.PREF_INPUT);
preference.setSummary(preference.getEntry());

// Get about summary
Preference about = findPreference(KEY_PREF_ABOUT);
Preference about = findPreference(MainActivity.PREF_ABOUT);

if (about != null)
{
Expand Down Expand Up @@ -112,15 +108,16 @@ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
public void onSharedPreferenceChanged(SharedPreferences preferences,
String key)
{
if (key.equals(KEY_PREF_INPUT))
if (key.equals(MainActivity.PREF_INPUT))
{
Preference preference = findPreference(key);

// Set summary to be the user-description for the selected
// value
preference.setSummary(((ListPreference) preference).getEntry());
}
if (key.equals(KEY_PREF_DARK))

if (key.equals(MainActivity.PREF_THEME))
{
if (Build.VERSION.SDK_INT != Build.VERSION_CODES.M)
getActivity().recreate();
Expand Down
12 changes: 12 additions & 0 deletions src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,16 @@
<item>9</item>
</string-array>

<string-array name="pref_theme_entries">
<item>Light</item>
<item>Dark</item>
<item>System</item>
</string-array>

<string-array name="pref_theme_entry_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>

</resources>
2 changes: 1 addition & 1 deletion src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<attr name="pref_fill" format="reference" />
<attr name="pref_hold" format="reference" />
<attr name="pref_screen" format="reference" />
<attr name="pref_dark" format="reference" />
<attr name="pref_theme" format="reference" />
<attr name="pref_about" format="reference" />

</declare-styleable>
Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/integers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<resources>

<integer name="def_input">0</integer>
<integer name="def_theme">0</integer>

</resources>
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<string name="pref_screen_summ">Keep screen on</string>

<string name="pref_theme">Theme</string>
<string translatable="false" name="pref_theme_summ">%s</string>

<string name="pref_dark">Dark Theme</string>
<string name="pref_dark_summ">Use Dark Theme</string>
Expand Down
4 changes: 2 additions & 2 deletions src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<item name="pref_fill">@drawable/ic_action_cloud</item>
<item name="pref_hold">@drawable/ic_action_upload</item>
<item name="pref_screen">@drawable/ic_action_brightness_low</item>
<item name="pref_dark">@drawable/ic_action_brightness_high</item>
<item name="pref_theme">@drawable/ic_action_brightness_high</item>
<item name="pref_about">@drawable/ic_action_about</item>

</style>
Expand Down Expand Up @@ -52,7 +52,7 @@
<item name="pref_fill">@drawable/ic_action_cloud_dark</item>
<item name="pref_hold">@drawable/ic_action_upload_dark</item>
<item name="pref_screen">@drawable/ic_action_brightness_low_dark</item>
<item name="pref_dark">@drawable/ic_action_brightness_high_dark</item>
<item name="pref_theme">@drawable/ic_action_brightness_high_dark</item>
<item name="pref_about">@drawable/ic_action_about_dark</item>

</style>
Expand Down
16 changes: 9 additions & 7 deletions src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@
android:key="pref_theme_category"
android:title="@string/pref_theme">

<CheckBoxPreference
android:defaultValue="false"
android:icon="?attr/pref_dark"
android:key="pref_dark"
android:persistent="true"
android:summary="@string/pref_dark_summ"
android:title="@string/pref_dark" />
<ListPreference
android:defaultValue="@integer/def_theme"
android:dialogIcon="?attr/pref_theme"
android:entries="@array/pref_theme_entries"
android:entryValues="@array/pref_theme_entry_values"
android:icon="?attr/pref_theme"
android:key="pref_theme"
android:summary="@string/pref_theme_summ"
android:title="@string/pref_theme" />

</PreferenceCategory>

Expand Down

0 comments on commit c126b39

Please sign in to comment.