1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 02:25:50 +01:00

Migrate more settings to per-device settings

Also add icons.
This commit is contained in:
Andreas Shimokawa 2019-05-22 10:49:59 +02:00
parent 9bfef4cf4f
commit 038f101a12
15 changed files with 181 additions and 126 deletions

View File

@ -54,7 +54,6 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.PeriodicExporter;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActivity;
import nodomain.freeyourgadget.gadgetbridge.devices.zetime.ZeTimePreferenceActivity;
import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
@ -66,11 +65,6 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_DATEFORMAT;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_ENABLE_TEXT_NOTIFICATIONS;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE_END;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE_OFF;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE_SCHEDULED;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI3_NIGHT_MODE_START;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_HEIGHT_CM;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_SLEEP_DURATION;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_STEPS_GOAL;
@ -379,60 +373,6 @@ public class SettingsActivity extends AbstractSettingsActivity {
}
});
String nightModeState = prefs.getString(MiBandConst.PREF_MI3_NIGHT_MODE, PREF_MI3_NIGHT_MODE_OFF);
boolean nightModeScheduled = nightModeState.equals(PREF_MI3_NIGHT_MODE_SCHEDULED);
final Preference nightModeStart = findPreference(PREF_MI3_NIGHT_MODE_START);
nightModeStart.setEnabled(nightModeScheduled);
nightModeStart.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(PREF_MI3_NIGHT_MODE_START);
}
});
return true;
}
});
final Preference nightModeEnd = findPreference(PREF_MI3_NIGHT_MODE_END);
nightModeEnd.setEnabled(nightModeScheduled);
nightModeEnd.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(PREF_MI3_NIGHT_MODE_END);
}
});
return true;
}
});
final Preference nightMode = findPreference(PREF_MI3_NIGHT_MODE);
nightMode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
final boolean scheduled = PREF_MI3_NIGHT_MODE_SCHEDULED.equals(newVal.toString());
nightModeStart.setEnabled(scheduled);
nightModeEnd.setEnabled(scheduled);
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(PREF_MI3_NIGHT_MODE);
}
});
return true;
}
});
// Get all receivers of Media Buttons
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);

View File

@ -9,6 +9,7 @@ import androidx.preference.PreferenceFragmentCompat;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst;
import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.XTimePreference;
import nodomain.freeyourgadget.gadgetbridge.util.XTimePreferenceFragment;
@ -18,6 +19,11 @@ import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF
import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF_DISCONNECT_NOTIFICATION_START;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_DO_NOT_DISTURB_OFF;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MI2_DO_NOT_DISTURB_SCHEDULED;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_NIGHT_MODE;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_NIGHT_MODE_END;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_NIGHT_MODE_OFF;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_NIGHT_MODE_SCHEDULED;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_NIGHT_MODE_START;
import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_SWIPE_UNLOCK;
public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat {
@ -153,6 +159,66 @@ public class DeviceSpecificSettingsFragment extends PreferenceFragmentCompat {
});
}
String nightModeState = prefs.getString(MiBandConst.PREF_NIGHT_MODE, PREF_NIGHT_MODE_OFF);
boolean nightModeScheduled = nightModeState.equals(PREF_NIGHT_MODE_SCHEDULED);
final Preference nightModeStart = findPreference(PREF_NIGHT_MODE_START);
if (nightModeStart != null) {
nightModeStart.setEnabled(nightModeScheduled);
nightModeStart.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(PREF_NIGHT_MODE_START);
}
});
return true;
}
});
}
final Preference nightModeEnd = findPreference(PREF_NIGHT_MODE_END);
if (nightModeEnd != null) {
nightModeEnd.setEnabled(nightModeScheduled);
nightModeEnd.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(PREF_NIGHT_MODE_END);
}
});
return true;
}
});
}
final Preference nightMode = findPreference(PREF_NIGHT_MODE);
if (nightMode != null) {
nightMode.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newVal) {
final boolean scheduled = PREF_NIGHT_MODE_SCHEDULED.equals(newVal.toString());
nightModeStart.setEnabled(scheduled);
nightModeEnd.setEnabled(scheduled);
invokeLater(new Runnable() {
@Override
public void run() {
GBApplication.deviceService().onSendConfiguration(PREF_NIGHT_MODE);
}
});
return true;
}
});
}
final Preference swipeUnlock = findPreference(PREF_SWIPE_UNLOCK);
if (swipeUnlock != null) {
swipeUnlock.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

View File

@ -184,12 +184,12 @@ public abstract class HuamiCoordinator extends AbstractDeviceCoordinator {
return DisconnectNotificationSetting.OFF;
}
public static Date getDisconnectNotificationStart() {
return getTimePreference(HuamiConst.PREF_DISCONNECT_NOTIFICATION_START, "00:00");
public static Date getDisconnectNotificationStart(String deviceAddress) {
return getTimePreference(HuamiConst.PREF_DISCONNECT_NOTIFICATION_START, "00:00", deviceAddress);
}
public static Date getDisconnectNotificationEnd() {
return getTimePreference(HuamiConst.PREF_DISCONNECT_NOTIFICATION_END, "00:00");
public static Date getDisconnectNotificationEnd(String deviceAddress) {
return getTimePreference(HuamiConst.PREF_DISCONNECT_NOTIFICATION_END, "00:00", deviceAddress);
}
public static Set<String> getDisplayItems(String deviceAddress) {
@ -251,8 +251,14 @@ public abstract class HuamiCoordinator extends AbstractDeviceCoordinator {
return prefs.getBoolean(MiBandConst.PREF_SWIPE_UNLOCK, false);
}
public static Date getTimePreference(String key, String defaultValue) {
Prefs prefs = GBApplication.getPrefs();
protected static Date getTimePreference(String key, String defaultValue, String deviceAddress) {
Prefs prefs;
if (deviceAddress == null) {
prefs = GBApplication.getPrefs();
} else {
prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
}
String time = prefs.getString(key, defaultValue);
DateFormat df = new SimpleDateFormat("HH:mm");
@ -265,6 +271,10 @@ public abstract class HuamiCoordinator extends AbstractDeviceCoordinator {
return new Date();
}
protected static Date getTimePreference(String key, String defaultValue) {
return getTimePreference(key, defaultValue, null);
}
public static MiBandConst.DistanceUnit getDistanceUnit() {
Prefs prefs = GBApplication.getPrefs();
String unit = prefs.getString(SettingsActivity.PREF_MEASUREMENT_SYSTEM, GBApplication.getContext().getString(R.string.p_unit_metric));

View File

@ -85,24 +85,25 @@ public class MiBand3Coordinator extends HuamiCoordinator {
}
public static String getNightMode() {
Prefs prefs = GBApplication.getPrefs();
public static String getNightMode(String deviceAddress) {
Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
return prefs.getString(MiBandConst.PREF_MI3_NIGHT_MODE, MiBandConst.PREF_MI3_NIGHT_MODE_OFF);
return prefs.getString(MiBandConst.PREF_NIGHT_MODE, MiBandConst.PREF_NIGHT_MODE_OFF);
}
public static Date getNightModeStart() {
return getTimePreference( MiBandConst.PREF_MI3_NIGHT_MODE_START, "16:00");
public static Date getNightModeStart(String deviceAddress) {
return getTimePreference(MiBandConst.PREF_NIGHT_MODE_START, "16:00", deviceAddress);
}
public static Date getNightModeEnd() {
return getTimePreference(MiBandConst.PREF_MI3_NIGHT_MODE_END, "07:00");
public static Date getNightModeEnd(String deviceAddress) {
return getTimePreference(MiBandConst.PREF_NIGHT_MODE_END, "07:00", deviceAddress);
}
@Override
public int[] getSupportedDeviceSpecificSettings(GBDevice device) {
return new int[]{
R.xml.devicesettings_miband3,
R.xml.devicesettings_nightmode,
R.xml.devicesettings_swipeunlock,
R.xml.devicesettings_pairingkey
};

View File

@ -66,12 +66,12 @@ public final class MiBandConst {
public static final String PREF_MIBAND_SETUP_BT_PAIRING = "mi_setup_bt_pairing";
public static final String PREF_SWIPE_UNLOCK = "swipe_unlock";
public static final String PREF_MI3_NIGHT_MODE = "mi3_night_mode";
public static final String PREF_MI3_NIGHT_MODE_START = "mi3_night_mode_start";
public static final String PREF_MI3_NIGHT_MODE_END = "mi3_night_mode_end";
public static final String PREF_MI3_NIGHT_MODE_OFF = "off";
public static final String PREF_MI3_NIGHT_MODE_SUNSET = "sunset";
public static final String PREF_MI3_NIGHT_MODE_SCHEDULED = "scheduled";
public static final String PREF_NIGHT_MODE = "night_mode";
public static final String PREF_NIGHT_MODE_START = "night_mode_start";
public static final String PREF_NIGHT_MODE_END = "night_mode_end";
public static final String PREF_NIGHT_MODE_OFF = "off";
public static final String PREF_NIGHT_MODE_SUNSET = "sunset";
public static final String PREF_NIGHT_MODE_SCHEDULED = "scheduled";
public static final String ORIGIN_INCOMING_CALL = "incoming_call";
public static final String ORIGIN_ALARM_CLOCK = "alarm_clock";

View File

@ -1793,12 +1793,12 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
Calendar calendar = GregorianCalendar.getInstance();
Date start = HuamiCoordinator.getDisconnectNotificationStart();
Date start = HuamiCoordinator.getDisconnectNotificationStart(gbDevice.getAddress());
calendar.setTime(start);
cmd[4] = (byte) calendar.get(Calendar.HOUR_OF_DAY);
cmd[5] = (byte) calendar.get(Calendar.MINUTE);
Date end = HuamiCoordinator.getDisconnectNotificationEnd();
Date end = HuamiCoordinator.getDisconnectNotificationEnd(gbDevice.getAddress());
calendar.setTime(end);
cmd[6] = (byte) calendar.get(Calendar.HOUR_OF_DAY);
cmd[7] = (byte) calendar.get(Calendar.MINUTE);

View File

@ -106,9 +106,9 @@ public class MiBand3Support extends AmazfitBipSupport {
try {
builder = performInitialized("Sending configuration for option: " + config);
switch (config) {
case MiBandConst.PREF_MI3_NIGHT_MODE:
case MiBandConst.PREF_MI3_NIGHT_MODE_START:
case MiBandConst.PREF_MI3_NIGHT_MODE_END:
case MiBandConst.PREF_NIGHT_MODE:
case MiBandConst.PREF_NIGHT_MODE_START:
case MiBandConst.PREF_NIGHT_MODE_END:
setNightMode(builder);
break;
default:
@ -122,27 +122,27 @@ public class MiBand3Support extends AmazfitBipSupport {
}
private MiBand3Support setNightMode(TransactionBuilder builder) {
String nightMode = MiBand3Coordinator.getNightMode();
String nightMode = MiBand3Coordinator.getNightMode(gbDevice.getAddress());
LOG.info("Setting night mode to " + nightMode);
switch (nightMode) {
case MiBandConst.PREF_MI3_NIGHT_MODE_SUNSET:
case MiBandConst.PREF_NIGHT_MODE_SUNSET:
builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), MiBand3Service.COMMAND_NIGHT_MODE_SUNSET);
break;
case MiBandConst.PREF_MI3_NIGHT_MODE_OFF:
case MiBandConst.PREF_NIGHT_MODE_OFF:
builder.write(getCharacteristic(HuamiService.UUID_CHARACTERISTIC_3_CONFIGURATION), MiBand3Service.COMMAND_NIGHT_MODE_OFF);
break;
case MiBandConst.PREF_MI3_NIGHT_MODE_SCHEDULED:
case MiBandConst.PREF_NIGHT_MODE_SCHEDULED:
byte[] cmd = MiBand3Service.COMMAND_NIGHT_MODE_SCHEDULED.clone();
Calendar calendar = GregorianCalendar.getInstance();
Date start = MiBand3Coordinator.getNightModeStart();
Date start = MiBand3Coordinator.getNightModeStart(gbDevice.getAddress());
calendar.setTime(start);
cmd[2] = (byte) calendar.get(Calendar.HOUR_OF_DAY);
cmd[3] = (byte) calendar.get(Calendar.MINUTE);
Date end = MiBand3Coordinator.getNightModeEnd();
Date end = MiBand3Coordinator.getNightModeEnd(gbDevice.getAddress());
calendar.setTime(end);
cmd[4] = (byte) calendar.get(Calendar.HOUR_OF_DAY);
cmd[5] = (byte) calendar.get(Calendar.MINUTE);

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#7E7E7E"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M10,2c-1.82,0 -3.53,0.5 -5,1.35C7.99,5.08 10,8.3 10,12s-2.01,6.92 -5,8.65C6.47,21.5 8.18,22 10,22c5.52,0 10,-4.48 10,-10S15.52,2 10,2z" />
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#7E7E7E"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,17c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2zM18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6h1.9c0,-1.71 1.39,-3.1 3.1,-3.1 1.71,0 3.1,1.39 3.1,3.1v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM18,20L6,20L6,10h12v10z"/>
</vector>

View File

@ -1,5 +1,10 @@
<vector android:height="24dp" android:tint="#7E7E7E"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z"/>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#7E7E7E"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z" />
</vector>

View File

@ -0,0 +1,14 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#7E7E7E"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillAlpha=".3"
android:fillColor="#FF000000"
android:pathData="M22,8V2L2,22h16V8z" />
<path
android:fillColor="#FF000000"
android:pathData="M20,22h2v-2h-2v2zM20,10v8h2v-8h-2z" />
</vector>

View File

@ -3,6 +3,7 @@
<PreferenceScreen
android:key="screen_disconnect_nofifiction"
android:icon="@drawable/ic_signal_cellular_connected_no_internet_0_bar"
android:persistent="false"
android:title="@string/prefs_disconnect_notification">

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen
android:icon="@drawable/ic_brightness_2"
android:key="screen_night_mode"
android:persistent="false"
android:summary="@string/mi3_prefs_night_mode_summary"
android:title="@string/mi3_prefs_night_mode">
<!-- workaround for missing toolbar -->
<PreferenceCategory android:title="@string/mi3_prefs_night_mode" />
<ListPreference
android:defaultValue="@string/p_off"
android:entries="@array/mi3_night_mode"
android:entryValues="@array/mi3_night_mode_values"
android:key="night_mode"
android:summary="%s"
android:title="@string/mi3_prefs_night_mode" />
<nodomain.freeyourgadget.gadgetbridge.util.XTimePreference
android:defaultValue="16:00"
android:key="night_mode_start"
android:title="@string/mi2_prefs_do_not_disturb_start" />
<nodomain.freeyourgadget.gadgetbridge.util.XTimePreference
android:defaultValue="7:00"
android:key="night_mode_end"
android:title="@string/mi2_prefs_do_not_disturb_end" />
</PreferenceScreen>
</androidx.preference.PreferenceScreen>

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
<SwitchPreference
android:defaultValue="false"
android:icon="@drawable/ic_lock_open"
android:key="swipe_unlock"
android:layout="@layout/preference_checkbox"
android:summary="@string/mi3_prefs_band_screen_unlock_summary"

View File

@ -290,37 +290,6 @@
android:summary="%s" />
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_device_miband2"
android:title="@string/preferences_miband3_settings">
<PreferenceScreen
android:persistent="false"
android:title="@string/mi3_prefs_night_mode"
android:summary="@string/mi3_prefs_night_mode_summary">
<!-- workaround for missing toolbar -->
<PreferenceCategory android:title="@string/mi3_prefs_night_mode" />
<ListPreference
android:defaultValue="@string/p_off"
android:entries="@array/mi3_night_mode"
android:entryValues="@array/mi3_night_mode_values"
android:key="mi3_night_mode"
android:summary="%s"
android:title="@string/mi3_prefs_night_mode" />
<nodomain.freeyourgadget.gadgetbridge.util.TimePreference
android:defaultValue="16:00"
android:key="mi3_night_mode_start"
android:title="@string/mi2_prefs_do_not_disturb_start" />
<nodomain.freeyourgadget.gadgetbridge.util.TimePreference
android:defaultValue="7:00"
android:key="mi3_night_mode_end"
android:title="@string/mi2_prefs_do_not_disturb_end" />
</PreferenceScreen>
</PreferenceScreen>
<PreferenceScreen
android:icon="@drawable/ic_device_pebble"
android:key="pref_key_pebble"