1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 01:09:47 +01:00

Huami: Toggle phone silent mode from band

This commit is contained in:
José Rebelo 2023-12-11 10:50:31 +00:00
parent 8f2924c873
commit 4dbf9bb8ac
40 changed files with 329 additions and 0 deletions

View File

@ -376,6 +376,8 @@ public class DeviceSettingsPreferenceConst {
public static final String PREF_VITALITY_SCORE_7_DAY = "pref_vitality_score_7_day";
public static final String PREF_VITALITY_SCORE_DAILY = "pref_vitality_score_daily";
public static final String PREF_PHONE_SILENT_MODE = "phone_silent_mode";
public static final String PREF_HOURLY_CHIME_ENABLE = "hourly_chime_enable";
public static final String PREF_HOURLY_CHIME_START = "hourly_chime_start";
public static final String PREF_HOURLY_CHIME_END = "hourly_chime_end";

View File

@ -16,7 +16,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.devicesettings;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.text.InputType;
@ -55,6 +57,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.CannedMessagesSpec;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.getContext;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.*;
import static nodomain.freeyourgadget.gadgetbridge.devices.huami.HuamiConst.PREF_CONTROL_CENTER_SORTABLE;
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_DEVICE_ACTION_FELL_SLEEP_BROADCAST;
@ -636,6 +639,15 @@ public class DeviceSpecificSettingsFragment extends AbstractPreferenceFragment i
});
}
final Preference phoneSilentMode = findPreference(PREF_PHONE_SILENT_MODE);
if (phoneSilentMode != null) {
phoneSilentMode.setOnPreferenceChangeListener((preference, newVal) -> {
final AudioManager audioManager = (AudioManager) requireContext().getSystemService(Context.AUDIO_SERVICE);
GBApplication.deviceService(device).onChangePhoneSilentMode(audioManager.getRingerMode());
return true;
});
}
final String alwaysOnDisplayState = prefs.getString(PREF_ALWAYS_ON_DISPLAY_MODE, PREF_ALWAYS_ON_DISPLAY_OFF);
boolean alwaysOnDisplayScheduled = alwaysOnDisplayState.equals(PREF_ALWAYS_ON_DISPLAY_SCHEDULED);
boolean alwaysOnDisplayOff = alwaysOnDisplayState.equals(PREF_ALWAYS_ON_DISPLAY_OFF);

View File

@ -0,0 +1,29 @@
/* Copyright (C) 2022 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.deviceevents;
public class GBDeviceEventSilentMode extends GBDeviceEvent {
private final boolean enabled;
public GBDeviceEventSilentMode(final boolean enabled) {
this.enabled = enabled;
}
public boolean isEnabled() {
return enabled;
}
}

View File

@ -75,6 +75,8 @@ public interface EventHandler {
*/
void onSetPhoneVolume(final float volume);
void onChangePhoneSilentMode(int ringerMode);
void onSetNavigationInfo(NavigationInfoSpec navigationInfoSpec);
void onEnableRealtimeSteps(boolean enable);

View File

@ -109,6 +109,7 @@ public class AmazfitBand5Coordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -82,6 +82,7 @@ public class AmazfitBipCoordinator extends HuamiCoordinator {
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_buttonactions_with_longpress,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -96,6 +96,7 @@ public class AmazfitBipSCoordinator extends HuamiCoordinator {
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -119,6 +119,7 @@ public class AmazfitBipUCoordinator extends HuamiCoordinator {
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -110,6 +110,7 @@ public class AmazfitBipUProCoordinator extends HuamiCoordinator {
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -90,6 +90,7 @@ public class AmazfitCorCoordinator extends HuamiCoordinator {
R.xml.devicesettings_reserve_reminders_calendar,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -92,6 +92,7 @@ public class AmazfitCor2Coordinator extends HuamiCoordinator {
R.xml.devicesettings_reserve_reminders_calendar,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -90,6 +90,7 @@ public class AmazfitGTRCoordinator extends HuamiCoordinator {
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_buttonactions_with_longpress,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -90,6 +90,7 @@ public class AmazfitGTRLiteCoordinator extends HuamiCoordinator {
R.xml.devicesettings_reserve_reminders_calendar,
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -96,6 +96,7 @@ public class AmazfitGTR2Coordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -96,6 +96,7 @@ public class AmazfitGTR2eCoordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -90,6 +90,7 @@ public class AmazfitGTSCoordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -96,6 +96,7 @@ public class AmazfitGTS2Coordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -65,6 +65,7 @@ public class AmazfitGTS2MiniCoordinator extends AmazfitGTS2Coordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -96,6 +96,7 @@ public class AmazfitGTS2eCoordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -98,6 +98,7 @@ public class AmazfitNeoCoordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -90,6 +90,7 @@ public class AmazfitTRexCoordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -90,6 +90,7 @@ public class AmazfitTRexProCoordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -91,6 +91,7 @@ public class AmazfitVergeLCoordinator extends HuamiCoordinator {
R.xml.devicesettings_disconnectnotification,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_buttonactions_with_longpress,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -93,6 +93,7 @@ public class AmazfitXCoordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -107,6 +107,7 @@ public class MiBand3Coordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,
R.xml.devicesettings_transliteration

View File

@ -118,6 +118,7 @@ public class MiBand5Coordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -107,6 +107,7 @@ public class MiBand6Coordinator extends HuamiCoordinator {
R.xml.devicesettings_reserve_reminders_calendar,
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -89,6 +89,7 @@ public class ZeppECoordinator extends HuamiCoordinator {
R.xml.devicesettings_expose_hr_thirdparty,
R.xml.devicesettings_bt_connected_advertisement,
R.xml.devicesettings_device_actions,
R.xml.devicesettings_phone_silent_mode,
R.xml.devicesettings_high_mtu,
R.xml.devicesettings_overwrite_settings_on_connection,
R.xml.devicesettings_huami2021_fetch_operation_time_unit,

View File

@ -0,0 +1,48 @@
/* Copyright (C) 2023 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.externalevents;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
public class SilentModeReceiver extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(SilentModeReceiver.class);
@Override
public void onReceive(final Context context, final Intent intent) {
if (intent.getAction() == null) {
return;
}
if (!AudioManager.RINGER_MODE_CHANGED_ACTION.equals(intent.getAction())) {
LOG.warn("Unexpected action {}", intent.getAction());
return;
}
final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final int ringerMode = audioManager.getRingerMode();
GBApplication.deviceService().onChangePhoneSilentMode(ringerMode);
}
}

View File

@ -262,6 +262,13 @@ public class GBDeviceService implements DeviceService {
invokeService(intent);
}
@Override
public void onChangePhoneSilentMode(int ringerMode) {
Intent intent = createIntent().setAction(ACTION_SET_PHONE_SILENT_MODE)
.putExtra(EXTRA_PHONE_RINGER_MODE, ringerMode);
invokeService(intent);
}
@Override
public void onSetReminders(ArrayList<? extends Reminder> reminders) {
Intent intent = createIntent().setAction(ACTION_SET_REMINDERS)

View File

@ -38,6 +38,7 @@ public interface DeviceService extends EventHandler {
String ACTION_SETMUSICINFO = PREFIX + ".action.setmusicinfo";
String ACTION_SETMUSICSTATE = PREFIX + ".action.setmusicstate";
String ACTION_SET_PHONE_VOLUME = PREFIX + ".action.set_phone_volume";
String ACTION_SET_PHONE_SILENT_MODE = PREFIX + ".action.set_phone_silent_mode";
String ACTION_SETNAVIGATIONINFO = PREFIX + ".action.setnavigationinfo";
String ACTION_REQUEST_DEVICEINFO = PREFIX + ".action.request_deviceinfo";
String ACTION_REQUEST_APPINFO = PREFIX + ".action.request_appinfo";
@ -114,6 +115,7 @@ public interface DeviceService extends EventHandler {
String EXTRA_MUSIC_POSITION = "music_position";
String EXTRA_MUSIC_RATE = "music_rate";
String EXTRA_PHONE_VOLUME = "phone_volume";
String EXTRA_PHONE_RINGER_MODE = "ringer_mode";
String EXTRA_NAVIGATION_INSTRUCTION = "navigation_instruction";
String EXTRA_NAVIGATION_DISTANCE_TO_TURN = "navigation_distance_to_turn";
String EXTRA_NAVIGATION_NEXT_ACTION = "navigation_next_action";

View File

@ -70,6 +70,7 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventDisplayMes
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFmFrequency;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepStateDetection;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSilentMode;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventUpdateDeviceInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventLEDColor;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
@ -105,6 +106,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.receivers.GBMusicControlRece
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.PendingIntentUtils;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import nodomain.freeyourgadget.gadgetbridge.util.SilentMode;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID;
@ -212,6 +214,8 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
handleGBDeviceEvent((GBDeviceEventUpdatePreferences) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventUpdateDeviceState) {
handleGBDeviceEvent((GBDeviceEventUpdateDeviceState) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventSilentMode) {
handleGBDeviceEvent((GBDeviceEventSilentMode) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventFmFrequency) {
handleGBDeviceEvent((GBDeviceEventFmFrequency) deviceEvent);
} else if (deviceEvent instanceof GBDeviceEventWearState) {
@ -221,6 +225,12 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
}
}
private void handleGBDeviceEvent(GBDeviceEventSilentMode deviceEvent) {
LOG.info("Got GBDeviceEventSilentMode: enabled = {}", deviceEvent.isEnabled());
SilentMode.setPhoneSilentMode(getDevice().getAddress(), deviceEvent.isEnabled());
}
private void handleGBDeviceEvent(final GBDeviceEventFindPhone deviceEvent) {
final Context context = getContext();
LOG.info("Got GBDeviceEventFindPhone: {}", deviceEvent.event);
@ -793,6 +803,15 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
}
/**
* Called when the phone's interruption filter or ringer mode is changed.
* @param ringerMode as per {@link android.media.AudioManager#getRingerMode()}
*/
@Override
public void onChangePhoneSilentMode(int ringerMode) {
}
/**
* If the device can receive the GPS location from the phone, this method
* can be overridden and implemented by the device support class.

View File

@ -33,6 +33,7 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Location;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
@ -73,6 +74,7 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.OsmandEventReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.PebbleReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.PhoneCallReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.SilentModeReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver;
import nodomain.freeyourgadget.gadgetbridge.externalevents.TinyWeatherForecastGermanyReceiver;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
@ -233,6 +235,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
private BluetoothConnectReceiver mBlueToothConnectReceiver = null;
private BluetoothPairingRequestReceiver mBlueToothPairingRequestReceiver = null;
private AlarmClockReceiver mAlarmClockReceiver = null;
private SilentModeReceiver mSilentModeReceiver = null;
private GBAutoFetchReceiver mGBAutoFetchReceiver = null;
private AutoConnectIntervalReceiver mAutoConnectInvervalReceiver = null;
@ -776,6 +779,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
float phoneVolume = intent.getFloatExtra(EXTRA_PHONE_VOLUME, 0);
deviceSupport.onSetPhoneVolume(phoneVolume);
break;
case ACTION_SET_PHONE_SILENT_MODE:
final int ringerMode = intent.getIntExtra(EXTRA_PHONE_RINGER_MODE, -1);
deviceSupport.onChangePhoneSilentMode(ringerMode);
break;
case ACTION_SETMUSICSTATE:
MusicStateSpec stateSpec = new MusicStateSpec();
stateSpec.shuffle = intent.getByteExtra(EXTRA_MUSIC_SHUFFLE, (byte) 0);
@ -1156,6 +1163,13 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
registerReceiver(mAlarmClockReceiver, filter);
}
if (mSilentModeReceiver == null) {
mSilentModeReceiver = new SilentModeReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(mSilentModeReceiver, filter);
}
if (mOsmandAidlHelper == null && features.supportsNavigation()) {
mOsmandAidlHelper = new OsmandEventReceiver(this.getApplication());
}
@ -1226,6 +1240,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
unregisterReceiver(mAlarmClockReceiver);
mAlarmClockReceiver = null;
}
if (mSilentModeReceiver != null) {
unregisterReceiver(mSilentModeReceiver);
mSilentModeReceiver = null;
}
if (mCMWeatherReceiver != null) {
unregisterReceiver(mCMWeatherReceiver);
mCMWeatherReceiver = null;

View File

@ -224,6 +224,14 @@ public class ServiceDeviceSupport implements DeviceSupport {
delegate.onSetPhoneVolume(volume);
}
@Override
public void onChangePhoneSilentMode(final int ringerMode) {
if (checkBusy("change phone silent mode")) {
return;
}
delegate.onChangePhoneSilentMode(ringerMode);
}
@Override
public void onSetNavigationInfo(NavigationInfoSpec navigationInfoSpec) {
if (checkBusy("set navigation info")) {

View File

@ -30,6 +30,7 @@ public class HuamiDeviceEvent {
public static final byte BUTTON_PRESSED_LONG = 0x0b;
public static final byte TICK_30MIN = 0x0e; // unsure
public static final byte FIND_PHONE_STOP = 0x0f;
public static final byte SILENT_MODE = 0x10;
public static final byte MTU_REQUEST = 0x16;
public static final byte WORKOUT_STARTING = 0x14;
public static final byte ALARM_CHANGED = 0x1a;

View File

@ -87,6 +87,7 @@ import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventFindPhone;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventNotificationControl;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSleepStateDetection;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventSilentMode;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventVersionInfo;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventWearState;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
@ -141,6 +142,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.Fet
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.operations.HuamiFetchDebugLogsOperation;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huami.zeppos.services.ZeppOsCannedMessagesService;
import nodomain.freeyourgadget.gadgetbridge.util.MediaManager;
import nodomain.freeyourgadget.gadgetbridge.util.SilentMode;
import nodomain.freeyourgadget.gadgetbridge.util.calendar.CalendarEvent;
import nodomain.freeyourgadget.gadgetbridge.util.calendar.CalendarManager;
import nodomain.freeyourgadget.gadgetbridge.model.CallSpec;
@ -1893,6 +1895,12 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
findPhoneEvent.event = GBDeviceEventFindPhone.Event.STOP;
evaluateGBDeviceEvent(findPhoneEvent);
break;
case HuamiDeviceEvent.SILENT_MODE:
final boolean silentModeEnabled = value[1] == 1;
LOG.info("silent mode = {}", silentModeEnabled);
sendPhoneSilentMode(silentModeEnabled);
evaluateGBDeviceEvent(new GBDeviceEventSilentMode(silentModeEnabled));
break;
case HuamiDeviceEvent.MUSIC_CONTROL:
LOG.info("got music control");
GBDeviceEventMusicControl deviceEventMusicControl = new GBDeviceEventMusicControl();
@ -2156,6 +2164,28 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
}
}
private void sendPhoneSilentMode(final TransactionBuilder builder) {
final boolean silentMode = SilentMode.isPhoneInSilenceMode(getDevice().getAddress());
sendPhoneSilentMode(builder, silentMode);
}
private void sendPhoneSilentMode(final boolean enabled) {
try {
final TransactionBuilder builder = performInitialized("send phone silent mode");
sendPhoneSilentMode(builder, enabled);
builder.queue(getQueue());
} catch (final Exception ex) {
LOG.error("Error while sending phone silent mode", ex);
}
}
private void sendPhoneSilentMode(final TransactionBuilder builder, final boolean enabled) {
final byte[] cmd = {ENDPOINT_DISPLAY, 0x19, 0x00, (byte) (enabled ? 0x01 : 0x00)};
writeToConfiguration(builder, cmd);
}
protected void processDeviceEvent(int deviceEvent){
LOG.debug("Handling device event: " + deviceEvent);
GBDeviceEvent event;
@ -4161,6 +4191,7 @@ public abstract class HuamiSupport extends AbstractBTLEDeviceSupport implements
}
requestAlarms(builder);
sendPhoneSilentMode(builder);
}
public abstract HuamiFWHelper createFWHelper(Uri uri, Context context) throws IOException;

View File

@ -0,0 +1,99 @@
/* Copyright (C) 2023 José Rebelo
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.util;
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.getContext;
import android.content.Context;
import android.media.AudioManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
public class SilentMode {
private static final Logger LOG = LoggerFactory.getLogger(SilentMode.class);
public enum RingerMode {
NORMAL(AudioManager.RINGER_MODE_NORMAL),
VIBRATE(AudioManager.RINGER_MODE_VIBRATE),
SILENT(AudioManager.RINGER_MODE_SILENT),
UNKNOWN(-1);
private final int code;
RingerMode(final int code) {
this.code = code;
}
public int getCode() {
return code;
}
public static RingerMode fromCode(final int code) {
for (final RingerMode ringerMode : values()) {
if (ringerMode.code == code) {
return ringerMode;
}
}
return RingerMode.UNKNOWN;
}
}
public static void setPhoneSilentMode(final String deviceAddress, final boolean enabled) {
final RingerMode[] phoneSilentMode = getPhoneSilentMode(deviceAddress);
final RingerMode ringerMode = phoneSilentMode[enabled ? 1 : 0];
LOG.debug("Set phone silent mode = {} ({})", enabled, ringerMode);
setRingerMode(ringerMode);
}
public static boolean isPhoneInSilenceMode(final String deviceAddress) {
final AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
final RingerMode currentRingerMode = RingerMode.fromCode(audioManager.getRingerMode());
final RingerMode[] phoneSilentMode = getPhoneSilentMode(deviceAddress);
// Check if current mode "is more silent than" desired ringer mode
return currentRingerMode.getCode() < phoneSilentMode[0].getCode();
}
public static RingerMode[] getPhoneSilentMode(final String deviceAddress) {
final Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
final String phoneSilentModePref = prefs.getString(DeviceSettingsPreferenceConst.PREF_PHONE_SILENT_MODE, "normal_silent").toUpperCase(Locale.ROOT);
final String[] prefSplit = phoneSilentModePref.split("_");
return new RingerMode[]{
RingerMode.valueOf(prefSplit[0]),
RingerMode.valueOf(prefSplit[1])
};
}
public static void setRingerMode(final RingerMode mode) {
if (mode == RingerMode.UNKNOWN) {
LOG.warn("Unable to set unknown ringer mode");
return;
}
final AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(mode.getCode());
}
}

View File

@ -204,6 +204,17 @@
<item>@string/p_alarm_clock</item>
</string-array>
<string-array name="phone_silent_mode">
<item>@string/silent_mode_normal_vibrate</item>
<item>@string/silent_mode_normal_silent</item>
<item>@string/silent_mode_vibrate_silent</item>
</string-array>
<string-array name="phone_silent_mode_values">
<item>normal_vibrate</item>
<item>normal_silent</item>
<item>vibrate_silent</item>
</string-array>
<string-array name="pebble_activitytracker">
<item name="4">Pebble Health</item>
<item name="3">Misfit</item>

View File

@ -964,6 +964,10 @@
<string name="mi2_prefs_inactivity_warnings_dnd_summary">Disable inactivity warnings for a time interval</string>
<string name="mi2_prefs_heart_rate_monitoring">Heart Rate Monitoring</string>
<string name="mi2_prefs_heart_rate_monitoring_summary">Configure heart rate monitoring</string>
<string name="prefs_phone_silent_mode">Phone Silent Mode</string>
<string name="silent_mode_normal_vibrate">Normal / Vibrate</string>
<string name="silent_mode_normal_silent">Normal / Silent</string>
<string name="silent_mode_vibrate_silent">Vibrate / Silent</string>
<string name="prefs_always_on_display">Always On Display</string>
<string name="prefs_always_on_display_follow_watchface">Style follows Watchface</string>
<string name="prefs_always_on_display_style">Style</string>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ListPreference
android:defaultValue="normal_silent"
android:entries="@array/phone_silent_mode"
android:entryValues="@array/phone_silent_mode_values"
android:icon="@drawable/ic_volume_off"
android:key="phone_silent_mode"
android:title="@string/prefs_phone_silent_mode"
app:useSimpleSummaryProvider="true" />
</androidx.preference.PreferenceScreen>