Skip to content

Commit

Permalink
Remove Do Not Disturb code for AlarmVolumePreference
Browse files Browse the repository at this point in the history
- Todo solved: layout now displays in Android Studio
  • Loading branch information
BlackyHawky committed Nov 1, 2024
1 parent a9218ec commit a869b84
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
package com.best.deskclock.settings;

import static android.content.Context.AUDIO_SERVICE;
import static android.content.Context.NOTIFICATION_SERVICE;
import static android.media.AudioManager.STREAM_ALARM;

import android.annotation.TargetApi;
import android.app.NotificationManager;
import android.content.Context;
import android.database.ContentObserver;
import android.media.AudioManager;
Expand All @@ -23,14 +20,14 @@
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.SeekBarPreference;

import com.best.deskclock.R;
import com.best.deskclock.RingtonePreviewKlaxon;
import com.best.deskclock.data.DataModel;

public class AlarmVolumePreference extends Preference {
public class AlarmVolumePreference extends SeekBarPreference {

private static final long ALARM_PREVIEW_DURATION_MS = 5000;

Expand All @@ -45,8 +42,7 @@ public AlarmVolumePreference(Context context, AttributeSet attrs) {
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
super.onBindViewHolder(holder);

final Context context = getContext();
final AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
final AudioManager audioManager = (AudioManager) holder.itemView.getContext().getSystemService(AUDIO_SERVICE);

// Disable click feedback for this preference.
holder.itemView.setClickable(false);
Expand All @@ -61,8 +57,6 @@ public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
mSeekbar.setMax(maxVolume);
mSeekbar.setProgress(audioManager.getStreamVolume(STREAM_ALARM) - getMinVolume(audioManager));

onSeekbarChanged();

final ContentObserver volumeObserver = new ContentObserver(mSeekbar.getHandler()) {
@Override
public void onChange(boolean selfChange) {
Expand All @@ -74,13 +68,13 @@ public void onChange(boolean selfChange) {
mSeekbar.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(@NonNull View v) {
context.getContentResolver().registerContentObserver(Settings.System.CONTENT_URI,
v.getContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI,
true, volumeObserver);
}

@Override
public void onViewDetachedFromWindow(@NonNull View v) {
context.getContentResolver().unregisterContentObserver(volumeObserver);
v.getContext().getContentResolver().unregisterContentObserver(volumeObserver);
}
});

Expand All @@ -91,7 +85,6 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int newVolume = progress + getMinVolume(audioManager);
audioManager.setStreamVolume(STREAM_ALARM, newVolume, 0);
}
onSeekbarChanged();
}

@Override
Expand All @@ -102,33 +95,17 @@ public void onStartTrackingTouch(SeekBar seekBar) {
public void onStopTrackingTouch(SeekBar seekBar) {
if (!mPreviewPlaying) {
// If we are not currently playing, start.
RingtonePreviewKlaxon.start(context, DataModel.getDataModel().getAlarmRingtoneUriFromSettings());
RingtonePreviewKlaxon.start(seekBar.getContext(), DataModel.getDataModel().getAlarmRingtoneUriFromSettings());
mPreviewPlaying = true;
seekBar.postDelayed(() -> {
RingtonePreviewKlaxon.stop(context);
RingtonePreviewKlaxon.stop(seekBar.getContext());
mPreviewPlaying = false;
}, ALARM_PREVIEW_DURATION_MS);
}
}
});
}

private void onSeekbarChanged() {
mSeekbar.setEnabled(doesDoNotDisturbAllowAlarmPlayback());
}

private boolean doesDoNotDisturbAllowAlarmPlayback() {
return !(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) || doesDoNotDisturbAllowAlarmPlaybackNPlus();
}

@TargetApi(Build.VERSION_CODES.N)
private boolean doesDoNotDisturbAllowAlarmPlaybackNPlus() {
final NotificationManager notificationManager = (NotificationManager)
getContext().getSystemService(NOTIFICATION_SERVICE);
return notificationManager.getCurrentInterruptionFilter() !=
NotificationManager.INTERRUPTION_FILTER_NONE;
}

private int getMinVolume(AudioManager audioManager) {
return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) ? audioManager.getStreamMinVolume(STREAM_ALARM) : 0;
}
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/res/xml/settings_alarm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
SPDX-License-Identifier: GPL-3.0-only
-->

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/alarm_settings">

Expand Down Expand Up @@ -41,7 +40,6 @@
app:iconSpaceReserved="false"
app:singleLineTitle="false" />

<!-- Todo: to be reworked to display this settings view in Android Studio -->
<com.best.deskclock.settings.AlarmVolumePreference
android:key="key_volume_setting"
android:layout="@layout/settings_preference_seekbar_layout"
Expand Down

0 comments on commit a869b84

Please sign in to comment.