add option to pause voice messages after taking device away from ear

This commit is contained in:
Christian Schneppe 2021-04-21 20:50:12 +02:00
parent c8c9b2ed44
commit 8e0495ced4
7 changed files with 26 additions and 4 deletions

View file

@ -49,6 +49,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
private final Sensor proximitySensor;
private final PendingItem<WeakReference<ImageButton>> pendingOnClickView = new PendingItem<>();
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private boolean isEarpieceBefore = false;
private final Handler handler = new Handler();
@ -162,7 +163,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
if (player.isPlaying()) {
viewHolder.progress.setEnabled(false);
player.pause();
releaseAudiFocus();
releaseAudioFocus();
messageAdapter.flagScreenOff();
releaseProximityWakeLock();
viewHolder.playPause.setImageResource(viewHolder.darkBackground ? R.drawable.ic_play_arrow_white_36dp : R.drawable.ic_play_arrow_black_36dp);
@ -180,7 +181,11 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
private void play(ViewHolder viewHolder, Message message, boolean earpiece, double progress) {
if (play(viewHolder, message, earpiece)) {
if (messageAdapter.autoPauseVoice() && (isEarpieceBefore && !earpiece)) {
playPauseCurrent(viewHolder);
}
AudioPlayer.player.seekTo((int) (AudioPlayer.player.getDuration() * progress));
isEarpieceBefore = earpiece;
}
}
@ -233,7 +238,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
if (AudioPlayer.player.isPlaying()) {
AudioPlayer.player.stop();
}
releaseAudiFocus();
releaseAudioFocus();
AudioPlayer.player.release();
messageAdapter.flagScreenOff();
releaseProximityWakeLock();
@ -379,7 +384,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
if (AudioPlayer.player.getAudioStreamType() != streamType) {
synchronized (AudioPlayer.LOCK) {
AudioPlayer.player.stop();
releaseAudiFocus();
releaseAudioFocus();
AudioPlayer.player.release();
AudioPlayer.player = null;
try {
@ -457,7 +462,7 @@ public class AudioPlayer implements View.OnClickListener, MediaPlayer.OnCompleti
}
}
private void releaseAudiFocus() {
private void releaseAudioFocus() {
AudioManager am = (AudioManager) messageAdapter.getActivity().getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
am.abandonAudioFocus(this);

View file

@ -1055,6 +1055,10 @@ public class XmppConnectionService extends Service {
}
}
public boolean pauseVoiceOnMoveFromEar() {
return getBooleanPreference(SettingsActivity.PAUSE_VOICE, R.bool.pause_voice);
}
public boolean easyDownloader() {
return getBooleanPreference(SettingsActivity.EASY_DOWNLOADER, R.bool.easy_downloader);
}

View file

@ -84,6 +84,7 @@ public class SettingsActivity extends XmppActivity implements
public static final String EASY_DOWNLOADER = "easy_downloader";
public static final String MIN_ANDROID_SDK21_SHOWN = "min_android_sdk21_shown";
public static final String INDIVIDUAL_NOTIFICATION_PREFIX = "individual_notification_set_";
public static final String PAUSE_VOICE = "pause_voice_on_move_from_ear";
public static final int REQUEST_CREATE_BACKUP = 0xbf8701;
Preference multiAccountPreference;

View file

@ -152,6 +152,10 @@ public class MessageAdapter extends ArrayAdapter<Message> {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
public boolean autoPauseVoice() {
return activity.xmppConnectionService.pauseVoiceOnMoveFromEar();
}
public void setVolumeControl(final int stream) {
activity.setVolumeControlStream(stream);
}

View file

@ -90,6 +90,7 @@
<integer name="high_video_res">1080</integer>
<bool name="easy_downloader">true</bool>
<bool name="hide_memory_warning">false</bool>
<bool name="pause_voice">false</bool>
<integer name="individual_notification">0</integer>
<string-array name="domains">

View file

@ -1139,4 +1139,6 @@
<string name="error_security_exception">The app you used to share this file did not provide enough permissions.</string>
<string name="unable_to_enable_video">Unable to enable video.</string>
<string name="delete_recording_dialog_message">Do you really want to cancel and delete your recording?</string>
<string name="pref_pause_voice_on_move_from_ear">Pause voice message automatically</string>
<string name="pref_pause_voice_on_move_from_ear_summary">Pause the current voice message when you move your device away from your ear.</string>
</resources>

View file

@ -492,6 +492,11 @@
android:key="remove_trusted_certificates"
android:summary="@string/pref_remove_trusted_certificates_summary"
android:title="@string/pref_remove_trusted_certificates_title" />
<CheckBoxPreference
android:defaultValue="@bool/pause_voice"
android:key="pause_voice_on_move_from_ear"
android:summary="@string/pref_pause_voice_on_move_from_ear_summary"
android:title="@string/pref_pause_voice_on_move_from_ear" />
</PreferenceCategory>
<PreferenceCategory
android:key="connection_options"