summaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorJavier <dev.git@javispedro.com>2021-03-26 20:09:25 +0100
committerJavier <dev.git@javispedro.com>2021-03-26 20:09:25 +0100
commit6b2dd5b290850301472a7097c84f76972b43b507 (patch)
tree670952beacb900aa15d4b9b3b367805cd18993c5 /app/src/main
parent558f29b84406b0d4b9493503f0ca02b35bfed9cf (diff)
downloadrempe-6b2dd5b290850301472a7097c84f76972b43b507.tar.gz
rempe-6b2dd5b290850301472a7097c84f76972b43b507.zip
Add new Preferences activity and allow choosing temperature units
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/AndroidManifest.xml6
-rw-r--r--app/src/main/java/com/javispedro/rempe/DeviceViewHolder.java23
-rw-r--r--app/src/main/java/com/javispedro/rempe/MainActivity.java2
-rw-r--r--app/src/main/java/com/javispedro/rempe/Preferences.java4
-rw-r--r--app/src/main/java/com/javispedro/rempe/SettingsActivity.java33
-rw-r--r--app/src/main/res/layout/settings_activity.xml9
-rw-r--r--app/src/main/res/values/arrays.xml13
-rw-r--r--app/src/main/res/values/dimens.xml1
-rw-r--r--app/src/main/res/values/strings.xml6
-rw-r--r--app/src/main/res/xml/root_preferences.xml11
10 files changed, 103 insertions, 5 deletions
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 @@
<application
android:allowBackup="true"
+ android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
- android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Rempe">
<activity
+ android:name=".SettingsActivity"
+ android:label="@string/settings_title"></activity>
+ <activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.Rempe.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
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 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+
+ <FrameLayout
+ android:id="@+id/settings"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent" />
+</LinearLayout> \ 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 @@
+<resources>
+ <string-array name="settings_unit_entries">
+ <item>Celsius</item>
+ <item>Fahrenheit</item>
+ <item>Kelvin</item>
+ </string-array>
+
+ <string-array name="settings_unit_values">
+ <item>celsius</item>
+ <item>fahrenheit</item>
+ <item>kelvin</item>
+ </string-array>
+</resources> \ 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 @@
<resources>
<dimen name="fab_margin">16dp</dimen>
- <dimen name="text_margin">16dp</dimen>
</resources> \ 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 @@
<string name="temperature_nothing">—</string>
<string name="temperature_celsius">%.1f °C</string>
+ <string name="temperature_fahrenheit">%.0f °F</string>
+ <string name="temperature_kelvin">%.1f °K</string>
<string name="last_24h">Last 24h:</string>
@@ -40,4 +42,8 @@
<string name="connection_result_unrecognized">Unrecognized device</string>
<string name="connection_result_unknown">Unknown failure</string>
+ <!-- Settings -->
+ <string name="settings_title">Settings</string>
+ <string name="settings_unit">Units</string>
+
</resources> \ 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 @@
+<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
+
+ <ListPreference
+ app:key="units"
+ app:title="@string/settings_unit"
+ app:entries="@array/settings_unit_entries"
+ app:entryValues="@array/settings_unit_values"
+ app:defaultValue="celsius"
+ app:useSimpleSummaryProvider="true" />
+
+</PreferenceScreen> \ No newline at end of file