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

Commit 3101fcf7 authored by Dylan Phan's avatar Dylan Phan
Browse files

Implement custom ringtone picker for timers and alarms.

Bug: 23996371
Change-Id: I6515da60f2811d7fd03b8a41d9bd724dc959fd0f
parent 94ac3347
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -76,6 +76,9 @@
    <!-- Setting labels on Set alarm screen: Select alarm ringtone  -->
    <string name="alert">Alarm Ringtone</string>

    <!-- Title of default ringtone played when an alarm triggers. -->
    <string name="default_alarm_ringtone_title">Default alarm sound</string>

    <!-- Title of default ringtone played when a timer expires. -->
    <string name="default_timer_ringtone_title">Timer Expired</string>

+2 −3
Original line number Diff line number Diff line
@@ -85,10 +85,9 @@

    <PreferenceCategory
        android:title="@string/timer_settings">
        <com.android.deskclock.settings.TimerRingtonePreference
        <Preference
            android:key="timer_ringtone"
            android:title="@string/timer_ringtone_title"
            android:ringtoneType="alarm" />
            android:title="@string/timer_ringtone_title" />

        <com.android.deskclock.settings.CrescendoLengthDialog
            android:key="timer_crescendo_duration"
+14 −36
Original line number Diff line number Diff line
@@ -16,12 +16,10 @@

package com.android.deskclock;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
@@ -173,6 +171,20 @@ public final class AlarmClockFragment extends DeskClockFragment implements
        mAlarmUpdateHandler.asyncUpdateAlarm(alarm, false, true);
    }

    public void setRingtone(Uri ringtoneUri) {
        // Update the default ringtone for future new alarms.
        DataModel.getDataModel().setDefaultAlarmRingtoneUri(ringtoneUri);

        final Alarm alarm = mAlarmTimeClickHandler.getSelectedAlarm();
        if (alarm == null) {
            LogUtils.e("Could not get selected alarm to set ringtone");
            return;
        }
        alarm.alert = ringtoneUri;
        // Save the change to alarm.
        mAlarmUpdateHandler.asyncUpdateAlarm(alarm, false /* popToast */, true /* minorUpdate */);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return Alarm.getAlarmsCursorLoader(getActivity());
@@ -219,40 +231,6 @@ public final class AlarmClockFragment extends DeskClockFragment implements
        mAlarmTimeAdapter.swapCursor(null);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }

        switch (requestCode) {
            case R.id.request_code_ringtone:
                // Extract the selected ringtone uri.
                Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                if (uri == null) {
                    uri = Alarm.NO_RINGTONE_URI;
                }

                // Update the default ringtone for future new alarms.
                DataModel.getDataModel().setDefaultAlarmRingtoneUri(uri);

                // Set the ringtone uri on the alarm.
                final Alarm alarm = mAlarmTimeClickHandler.getSelectedAlarm();
                if (alarm == null) {
                    LogUtils.e("Could not get selected alarm to set ringtone");
                    return;
                }
                alarm.alert = uri;

                // Save the change to alarm.
                mAlarmUpdateHandler.asyncUpdateAlarm(alarm, false /* popToast */,
                        true /* minorUpdate */);
                break;
            default:
                LogUtils.w("Unhandled request code in onActivityResult: " + requestCode);
        }
    }

    @Override
    public void setSmoothScrollStableId(long stableId) {
        mScrollToAlarmId = stableId;
+4 −3
Original line number Diff line number Diff line
@@ -70,7 +70,7 @@ public final class AsyncRingtonePlayer {

    /**
     * @param crescendoPrefKey the key to the user preference that defines the crescendo behavior
     *                         associated with this ringtone player
     *                         associated with this ringtone player, or null to ignore crescendo
     */
    public AsyncRingtonePlayer(Context context, String crescendoPrefKey) {
        mContext = context;
@@ -212,10 +212,11 @@ public final class AsyncRingtonePlayer {
    }

    /**
     * @return {@code true} iff the crescendo duration is more than 0 seconds
     * Returns true if the crescendo preference was given and the duration is more than
     * 0 seconds.
     */
    private boolean isCrescendoEnabled(Context context) {
        return getCrescendoDurationMillis(context) > 0;
        return mCrescendoPrefKey != null && getCrescendoDurationMillis(context) > 0;
    }

    /**
+18 −5
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.VisibleForTesting;
@@ -65,7 +66,8 @@ import java.util.Set;
 */
public class DeskClock extends BaseActivity
        implements LabelDialogFragment.TimerLabelDialogHandler,
        LabelDialogFragment.AlarmLabelDialogHandler {
        LabelDialogFragment.AlarmLabelDialogHandler,
        RingtonePickerDialogFragment.RingtoneSelectionListener {

    private static final String TAG = "DeskClock";

@@ -439,27 +441,38 @@ public class DeskClock extends BaseActivity
    }

    /**
     * Called by the LabelDialogFormat class after the dialog is finished. *
     * Called by the LabelDialogFragment class after the dialog is finished.
     */
    @Override
    public void onDialogLabelSet(TimerObj timer, String label, String tag) {
        Fragment frag = getFragmentManager().findFragmentByTag(tag);
        final Fragment frag = getFragmentManager().findFragmentByTag(tag);
        if (frag instanceof TimerFragment) {
            ((TimerFragment) frag).setLabel(timer, label);
        }
    }

    /**
     * Called by the LabelDialogFormat class after the dialog is finished. *
     * Called by the LabelDialogFragment class after the dialog is finished.
     */
    @Override
    public void onDialogLabelSet(Alarm alarm, String label, String tag) {
        Fragment frag = getFragmentManager().findFragmentByTag(tag);
        final Fragment frag = getFragmentManager().findFragmentByTag(tag);
        if (frag instanceof AlarmClockFragment) {
            ((AlarmClockFragment) frag).setLabel(alarm, label);
        }
    }

    /**
     * Called by the RingtonePickerDialogFragment class after the dialog is finished.
     */
    @Override
    public void onRingtoneSelected(Uri ringtoneUri, String fragmentTag) {
        final Fragment frag = getFragmentManager().findFragmentByTag(fragmentTag);
        if (frag instanceof AlarmClockFragment) {
            ((AlarmClockFragment) frag).setRingtone(ringtoneUri);
        }
    }

    public int getSelectedTab() {
        return mSelectedTab;
    }
Loading