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

Commit f139f92b authored by Annie Chin's avatar Annie Chin
Browse files

Add custom ringtones for timers.

Bug: 18366475

Add an option in the SettingsActivity to change timer ringtone.
In order to keep currently satisfied users happy, default to using
the current timer_expire.ogg ringtone. If the user opens the
timer ringtone preference, they'll be able to pick from
RingtoneManager's alarm selection. This also means that they will
never be able to get timer_expire back unless they clear their
SharedPreferences, because timer_expire is not one of
RingtoneManager's provided ringtones.

Before the user selects a RingtoneManager ringtone, leave the
summary for the timer ringtone preference blank so the user will
have less of an impression that timer_expire is a selectable option.

Change-Id: I513514d9add3e67e22d539d54eeea236677e5874
parent 5a4cc253
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -808,6 +808,11 @@
         translation. -->
    <string name="description_direction_both">Swipe left to snooze or right to dismiss</string>

    <!-- Header in the preferences settings for the section pertaining to timers. -->
    <string name="timer_settings">Timers</string>
    <!-- Description for timer ringtone setting. -->
    <string name="timer_ringtone_title">Timer ringtone</string>

    <!-- Notification title when timer is stopped. -->
    <string name="timer_stopped">Timer stopped</string>
    <!-- Notification title when multiple timers are stopped. -->
+9 −0
Original line number Diff line number Diff line
@@ -71,4 +71,13 @@
            android:entries="@array/week_start_entries"
            android:entryValues="@array/week_start_values" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/timer_settings">
        <RingtonePreference
            android:key="timer_ringtone"
            android:title="@string/timer_ringtone_title"
            android:ringtoneType="alarm" />
    </PreferenceCategory>

</PreferenceScreen>
+2 −2
Original line number Diff line number Diff line
@@ -476,7 +476,7 @@ public abstract class AlarmClockFragment extends DeskClockFragment implements

        // If the user chose an external ringtone and has not yet granted the permission to read
        // external storage, ask them for that permission now.
        if (!AlarmUtils.hasPermissionToDisplayRingtoneTitle(getActivity(), uri)) {
        if (!Utils.hasPermissionToDisplayRingtoneTitle(getActivity(), uri)) {
            final String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE};
            requestPermissions(perms, REQUEST_CODE_PERMISSIONS);
        }
@@ -1100,7 +1100,7 @@ public abstract class AlarmClockFragment extends DeskClockFragment implements
            if (title == null) {
                // If the user cannot read the ringtone file, insert our own name rather than the
                // ugly one returned by Ringtone.getTitle().
                if (!AlarmUtils.hasPermissionToDisplayRingtoneTitle(mContext, uri)) {
                if (!Utils.hasPermissionToDisplayRingtoneTitle(mContext, uri)) {
                    title = getString(R.string.custom_ringtone);
                } else {
                    // This is slow because a media player is created during Ringtone object creation.
+0 −27
Original line number Diff line number Diff line
@@ -16,15 +16,12 @@

package com.android.deskclock;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.VisibleForTesting;
import android.text.format.DateFormat;
@@ -120,30 +117,6 @@ public class AlarmUtils {
        timePickerFragment.show(manager, FRAG_TAG_TIME_PICKER);
    }

    /**
     * @return {@code true} iff the user has granted permission to read the ringtone at the given
     *      uri or no permission is required to read the ringtone
     */
    public static boolean hasPermissionToDisplayRingtoneTitle(Context context, Uri ringtoneUri) {
        final PackageManager pm = context.getPackageManager();
        final String packageName = context.getPackageName();

        // If no ringtone is specified, return true.
        if (ringtoneUri == null || ringtoneUri == Alarm.NO_RINGTONE_URI) {
            return true;
        }

        // If the permission is already granted, return true.
        if (pm.checkPermission(Manifest.permission.READ_EXTERNAL_STORAGE, packageName)
                == PackageManager.PERMISSION_GRANTED) {
            return true;
        }

        // If the ringtone is internal, return true;
        // external ringtones require the permission to see their title
        return ringtoneUri.toString().startsWith("content://media/internal/");
    }

    /**
     * format "Alarm set for 2 days, 7 hours, and 53 minutes from now."
     */
+59 −26
Original line number Diff line number Diff line
@@ -16,16 +16,19 @@

package com.android.deskclock;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.RingtonePreference;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
@@ -44,6 +47,7 @@ public class SettingsActivity extends BaseActivity {

    public static final String KEY_ALARM_SNOOZE = "snooze_duration";
    public static final String KEY_ALARM_VOLUME = "volume_setting";
    public static final String KEY_TIMER_RINGTONE = "timer_ringtone";
    public static final String KEY_VOLUME_BEHAVIOR = "volume_button_setting";
    public static final String KEY_AUTO_SILENCE = "auto_silence";
    public static final String KEY_CLOCK_STYLE = "clock_style";
@@ -56,6 +60,8 @@ public class SettingsActivity extends BaseActivity {
    public static final String VOLUME_BEHAVIOR_SNOOZE = "1";
    public static final String VOLUME_BEHAVIOR_DISMISS = "2";

    private static final int REQUEST_CODE_PERMISSIONS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -120,32 +126,55 @@ public class SettingsActivity extends BaseActivity {

        @Override
        public boolean onPreferenceChange(Preference pref, Object newValue) {
            if (KEY_AUTO_SILENCE.equals(pref.getKey())) {
            final int idx;
            switch (pref.getKey()) {
                case KEY_AUTO_SILENCE:
                    final ListPreference autoSilencePref = (ListPreference) pref;
                    String delay = (String) newValue;
                    updateAutoSnoozeSummary(autoSilencePref, delay);
            } else if (KEY_CLOCK_STYLE.equals(pref.getKey())) {
                    break;
                case KEY_CLOCK_STYLE:
                    final ListPreference clockStylePref = (ListPreference) pref;
                final int idx = clockStylePref.findIndexOfValue((String) newValue);
                    idx = clockStylePref.findIndexOfValue((String) newValue);
                    clockStylePref.setSummary(clockStylePref.getEntries()[idx]);
            } else if (KEY_HOME_TZ.equals(pref.getKey())) {
                    break;
                case KEY_HOME_TZ:
                    final ListPreference homeTimezonePref = (ListPreference) pref;
                final int idx = homeTimezonePref.findIndexOfValue((String) newValue);
                    idx = homeTimezonePref.findIndexOfValue((String) newValue);
                    homeTimezonePref.setSummary(homeTimezonePref.getEntries()[idx]);
                    notifyHomeTimeZoneChanged();
            } else if (KEY_AUTO_HOME_CLOCK.equals(pref.getKey())) {
                    break;
                case KEY_AUTO_HOME_CLOCK:
                    final boolean autoHomeClockEnabled = ((CheckBoxPreference) pref).isChecked();
                    final Preference homeTimeZonePref = findPreference(KEY_HOME_TZ);
                    homeTimeZonePref.setEnabled(!autoHomeClockEnabled);
                    notifyHomeTimeZoneChanged();
            } else if (KEY_VOLUME_BUTTONS.equals(pref.getKey())) {
                    break;
                case KEY_VOLUME_BUTTONS:
                    final ListPreference volumeButtonsPref = (ListPreference) pref;
                    final int index = volumeButtonsPref.findIndexOfValue((String) newValue);
                    volumeButtonsPref.setSummary(volumeButtonsPref.getEntries()[index]);
            } else if (KEY_WEEK_START.equals(pref.getKey())) {
                final ListPreference weekStartPref = (ListPreference) findPreference(KEY_WEEK_START);
                final int idx = weekStartPref.findIndexOfValue((String) newValue);
                    break;
                case KEY_WEEK_START:
                    final ListPreference weekStartPref = (ListPreference)
                        findPreference(KEY_WEEK_START);
                    idx = weekStartPref.findIndexOfValue((String) newValue);
                    weekStartPref.setSummary(weekStartPref.getEntries()[idx]);
                    break;
                case KEY_TIMER_RINGTONE:
                    final RingtonePreference timerRingtonePref = (RingtonePreference)
                            findPreference(KEY_TIMER_RINGTONE);
                    final Uri uri = Uri.parse((String) newValue);
                    Utils.setTimerRingtoneUri(getActivity(), uri);

                    // If the user chose an external ringtone and has not yet granted the permission to read
                    // external storage, ask them for that permission now.
                    if (!Utils.hasPermissionToDisplayRingtoneTitle(getActivity(), uri)) {
                        final String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE};
                        requestPermissions(perms, REQUEST_CODE_PERMISSIONS);
                    }
                    timerRingtonePref.setSummary(Utils.getTimerRingtoneName(getActivity()));
                    break;
            }
            // Set result so DeskClock knows to refresh itself
            getActivity().setResult(RESULT_OK);
@@ -242,6 +271,10 @@ public class SettingsActivity extends BaseActivity {
            weekStartPref.setValueIndex(idx);
            weekStartPref.setSummary(weekStartPref.getEntries()[idx]);
            weekStartPref.setOnPreferenceChangeListener(this);

            final Preference ringtonePref = findPreference(KEY_TIMER_RINGTONE);
            ringtonePref.setSummary(Utils.getTimerRingtoneName(getActivity()));
            ringtonePref.setOnPreferenceChangeListener(this);
        }

        private void updateAutoSnoozeSummary(ListPreference listPref, String delay) {
Loading