Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 065a844b authored by Dylan Phan's avatar Dylan Phan Committed by Android (Google) Code Review
Browse files

Merge "Add alarm volume slider in preferences." into ub-deskclock-escargatoire

parents 3a7431e7 e84dc3ed
Loading
Loading
Loading
Loading
+60 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2015 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License
  -->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:clipToPadding="false"
    android:focusable="true"
    android:gravity="center_vertical"
    android:minHeight="?attr/listPreferredItemHeightSmall"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingTop="16dp">

    <TextView
        android:id="@android:id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceListItem"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="16dp">

        <ImageView
            android:id="@+id/alarm_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <SeekBar
            android:id="@+id/alarm_volume_slider"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>
+3 −2
Original line number Diff line number Diff line
@@ -57,9 +57,10 @@
            android:key="snooze_duration"
            android:title="@string/snooze_duration_title" />

        <Preference
        <com.android.deskclock.settings.AlarmVolumePreference
            android:key="volume_setting"
            android:title="@string/alarm_volume_title" />
            android:title="@string/alarm_volume_title"
            android:layout="@layout/alarm_volume_preference"/>

        <com.android.deskclock.settings.CrescendoLengthDialogPreference
            android:key="alarm_crescendo_duration"
+122 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.deskclock.settings;

import android.content.Context;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

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

public class AlarmVolumePreference extends Preference {

    private static final long ALARM_PREVIEW_DURATION_MS = 2000;

    private SeekBar mSeekbar;
    private ImageView mAlarmIcon;
    private boolean mPreviewPlaying;

    public AlarmVolumePreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        // Disable click feedback for this preference.
        holder.itemView.setClickable(false);

        final Context context = getContext();
        final AudioManager audioManager =
                (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        mSeekbar = (SeekBar) holder.findViewById(R.id.alarm_volume_slider);
        mSeekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM));
        mSeekbar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_ALARM));
        mAlarmIcon = (ImageView) holder.findViewById(R.id.alarm_icon);
        updateIcon();

        final ContentObserver volumeObserver = new ContentObserver(mSeekbar.getHandler()) {
            @Override
            public void onChange(boolean selfChange) {
                // Volume was changed elsewhere, update our slider.
                mSeekbar.setProgress(audioManager.getStreamVolume(
                        AudioManager.STREAM_ALARM));
            }
        };

        mSeekbar.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {
                context.getContentResolver().registerContentObserver(Settings.System.CONTENT_URI,
                        true, volumeObserver);
            }

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


        mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    audioManager.setStreamVolume(
                            AudioManager.STREAM_ALARM, seekBar.getProgress(), 0);
                }
                updateIcon();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (!mPreviewPlaying) {
                    RingtonePreviewKlaxon.start(
                            context, DataModel.getDataModel().getDefaultAlarmRingtoneUri());
                    mPreviewPlaying = true;

                    seekBar.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            RingtonePreviewKlaxon.stop(context);
                            mPreviewPlaying = false;
                        }
                    }, ALARM_PREVIEW_DURATION_MS);
                }
            }
        });
    }

    private void updateIcon() {
        mAlarmIcon.setImageResource(mSeekbar.getProgress() == 0 ?
                R.drawable.ic_alarm_off_24dp : R.drawable.ic_alarm_small_24dp);
    }
}
+0 −10
Original line number Diff line number Diff line
@@ -57,7 +57,6 @@ public final class SettingsActivity extends BaseActivity
        implements RingtonePickerDialogFragment.RingtoneSelectionListener {

    public static final String KEY_ALARM_SNOOZE = "snooze_duration";
    public static final String KEY_ALARM_VOLUME = "volume_setting";
    public static final String KEY_ALARM_CRESCENDO = "alarm_crescendo_duration";
    public static final String KEY_TIMER_CRESCENDO = "timer_crescendo_duration";
    public static final String KEY_TIMER_RINGTONE = "timer_ringtone";
@@ -225,12 +224,6 @@ public final class SettingsActivity extends BaseActivity
                    dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(dialogIntent);
                    return true;
                case KEY_ALARM_VOLUME:
                    final AudioManager audioManager =
                            (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
                    audioManager.adjustStreamVolume(AudioManager.STREAM_ALARM,
                            AudioManager.ADJUST_SAME, AudioManager.FLAG_SHOW_UI);
                    return true;
                case KEY_TIMER_RINGTONE:
                    final String dialogTitle = getString(R.string.timer_ringtone_title);
                    final String defaultTitle = getString(R.string.default_timer_ringtone_title);
@@ -316,9 +309,6 @@ public final class SettingsActivity extends BaseActivity
            volumeButtonsPref.setSummary(volumeButtonsPref.getEntry());
            volumeButtonsPref.setOnPreferenceChangeListener(this);

            final Preference volumePref = findPreference(KEY_ALARM_VOLUME);
            volumePref.setOnPreferenceClickListener(this);

            ((SnoozeLengthDialogPreference) findPreference(KEY_ALARM_SNOOZE)).updateSummary();
            ((CrescendoLengthDialogPreference) findPreference(KEY_ALARM_CRESCENDO)).updateSummary();
            ((CrescendoLengthDialogPreference) findPreference(KEY_TIMER_CRESCENDO)).updateSummary();