From 6b2dd5b290850301472a7097c84f76972b43b507 Mon Sep 17 00:00:00 2001 From: Javier Date: Fri, 26 Mar 2021 20:09:25 +0100 Subject: Add new Preferences activity and allow choosing temperature units --- app/src/main/AndroidManifest.xml | 6 ++-- .../com/javispedro/rempe/DeviceViewHolder.java | 23 +++++++++++++-- .../java/com/javispedro/rempe/MainActivity.java | 2 ++ .../java/com/javispedro/rempe/Preferences.java | 4 +++ .../com/javispedro/rempe/SettingsActivity.java | 33 ++++++++++++++++++++++ app/src/main/res/layout/settings_activity.xml | 9 ++++++ app/src/main/res/values/arrays.xml | 13 +++++++++ app/src/main/res/values/dimens.xml | 1 - app/src/main/res/values/strings.xml | 6 ++++ app/src/main/res/xml/root_preferences.xml | 11 ++++++++ 10 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/javispedro/rempe/SettingsActivity.java create mode 100644 app/src/main/res/layout/settings_activity.xml create mode 100644 app/src/main/res/values/arrays.xml create mode 100644 app/src/main/res/xml/root_preferences.xml (limited to 'app/src/main') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 18651ae..cc27544 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,18 +4,20 @@ + - diff --git a/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java b/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java index 2e2a441..4d1c99a 100644 --- a/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java +++ b/app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java @@ -1,11 +1,13 @@ package com.javispedro.rempe; import android.content.Context; +import android.content.SharedPreferences; import android.view.View; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; +import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.RecyclerView; import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult; @@ -18,6 +20,8 @@ public class DeviceViewHolder extends RecyclerView.ViewHolder implements Device. private final View mView; private final Context mContext; + private final SharedPreferences mPrefs; + private final TextView mTemperatureView; private final TextView mMinMaxTemperatureLabel; private final TextView mMinTemperatureView; @@ -29,12 +33,17 @@ public class DeviceViewHolder extends RecyclerView.ViewHolder implements Device. private final TextView mSignalLabel; private final ProgressBar mSignalBar; + private final static BigDecimal C2F_MULT = new BigDecimal("1.8"); + private final static BigDecimal C2F_ADD = new BigDecimal("32"); + private final static BigDecimal C2K_ADD = new BigDecimal("273.15"); + private Device mDevice; public DeviceViewHolder(View view) { super(view); mView = view; mContext = view.getContext(); + mPrefs = PreferenceManager.getDefaultSharedPreferences(mContext); mNameView = view.findViewById(R.id.nameView); mStatusView = view.findViewById(R.id.statusView); mStatusImageView = view.findViewById(R.id.statusImageView); @@ -82,8 +91,18 @@ public class DeviceViewHolder extends RecyclerView.ViewHolder implements Device. mSignalBar.setProgress(0); } - private String formatTemperature(BigDecimal temp) { - return mContext.getString(R.string.temperature_celsius, temp); + private String formatTemperature(BigDecimal tempC) { + switch (Preferences.getUnits(mPrefs)) { + default: + case "celsius": + return mContext.getString(R.string.temperature_celsius, tempC); + case "fahrenheit": + BigDecimal tempF = (tempC.multiply(C2F_MULT)).add(C2F_ADD); + return mContext.getString(R.string.temperature_fahrenheit, tempF); + case "kelvin": + BigDecimal tempK = (tempC.add(C2K_ADD)); + return mContext.getString(R.string.temperature_kelvin, tempK); + } } @Override diff --git a/app/src/main/java/com/javispedro/rempe/MainActivity.java b/app/src/main/java/com/javispedro/rempe/MainActivity.java index 48fa5fd..77caa96 100644 --- a/app/src/main/java/com/javispedro/rempe/MainActivity.java +++ b/app/src/main/java/com/javispedro/rempe/MainActivity.java @@ -1,5 +1,6 @@ package com.javispedro.rempe; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; @@ -104,6 +105,7 @@ public class MainActivity extends AppCompatActivity { switch (id) { case R.id.action_settings: + startActivity(new Intent(this, SettingsActivity.class)); return true; } diff --git a/app/src/main/java/com/javispedro/rempe/Preferences.java b/app/src/main/java/com/javispedro/rempe/Preferences.java index 41c943c..d82b004 100644 --- a/app/src/main/java/com/javispedro/rempe/Preferences.java +++ b/app/src/main/java/com/javispedro/rempe/Preferences.java @@ -40,4 +40,8 @@ class Preferences { editor.putString(PREFS_DEVICES, pref); editor.apply(); } + + public static String getUnits(SharedPreferences prefs) { + return prefs.getString("units", "celsius"); + } } diff --git a/app/src/main/java/com/javispedro/rempe/SettingsActivity.java b/app/src/main/java/com/javispedro/rempe/SettingsActivity.java new file mode 100644 index 0000000..f660c70 --- /dev/null +++ b/app/src/main/java/com/javispedro/rempe/SettingsActivity.java @@ -0,0 +1,33 @@ +package com.javispedro.rempe; + +import android.os.Bundle; + +import androidx.appcompat.app.ActionBar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.preference.PreferenceFragmentCompat; + +public class SettingsActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.settings_activity); + if (savedInstanceState == null) { + getSupportFragmentManager() + .beginTransaction() + .replace(R.id.settings, new SettingsFragment()) + .commit(); + } + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + } + } + + public static class SettingsFragment extends PreferenceFragmentCompat { + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + setPreferencesFromResource(R.xml.root_preferences, rootKey); + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/settings_activity.xml b/app/src/main/res/layout/settings_activity.xml new file mode 100644 index 0000000..de6591a --- /dev/null +++ b/app/src/main/res/layout/settings_activity.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 0000000..f82d234 --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,13 @@ + + + Celsius + Fahrenheit + Kelvin + + + + celsius + fahrenheit + kelvin + + \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index cf00d3f..125df87 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,4 +1,3 @@ 16dp - 16dp \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c90f66a..df21b28 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -14,6 +14,8 @@ %.1f °C + %.0f °F + %.1f °K Last 24h: @@ -40,4 +42,8 @@ Unrecognized device Unknown failure + + Settings + Units + \ No newline at end of file diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml new file mode 100644 index 0000000..c360335 --- /dev/null +++ b/app/src/main/res/xml/root_preferences.xml @@ -0,0 +1,11 @@ + + + + + \ No newline at end of file -- cgit v1.2.3