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

Commit 506f13b6 authored by Glenn Maynard's avatar Glenn Maynard
Browse files

Allow setting which sounds are muted by silent mode.

All sound categories are supported, but not all are actually shown in
the menu; I've only shown the ones where it seems meaningful to silence
them.

I was undecided where to put this in the sound menu.  It most naturally
goes under "Silent mode", but I pushed it near the bottom of the section
since the other sound settings are much more frequently used.

There are significantly different uses of "silent mode":

- I'm asleep.  I don't want notifications or calls ringing and waking me
  up, but I do want alarms to wake me up.  (Android almost does this by
  default--it still plays music in silent mode, which is annoying with
  the "android randomly plays classical music" bug.)
- I'm in a place where I don't want my device making noise; for example,
  watching a movie.  Nothing should play, including music and alarms.

This doesn't make it easy to switch between uses, but it's a start.  Note
that "silent mode silences alarms" is also available in the clock app, buried
very deep in options; these use the same setting and will stay in sync.
parent 597ea9a9
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -235,6 +235,14 @@
            </intent-filter>
        </activity>

        <activity android:name="SilentChannels" android:label="@string/silent_channels_label">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="com.android.settings.SILENT_CHANNELS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name="DockSettings"
                android:label="@string/dock_settings_title"
                android:clearTaskOnLaunch="true"
+34 −0
Original line number Diff line number Diff line
@@ -950,6 +950,40 @@
    <string name="audio_record_proc_title">Noise cancellation</string>
    <!-- Sound settings screen, setting option summary text -->
    <string name="audio_record_proc_summary">Suppress background noise when speaking or recording.</string>
    <!-- Sound settings screen, setting screen option name to enter menu to choose what silent mode affects -->
    <string name="silent_mode_settings_title">Silent mode settings</string>
    <!-- Sound settings screen, setting option summary text -->
    <string name="silent_mode_settings_summary">Select which sounds are muted in silent mode.</string>

    <!-- Sound and alerts settings, silent mode submenu -->
    <!-- Screen title -->
    <string name="silent_channels_label">Select sounds muted in silent mode</string>
    <string name="silent_mode_settings_reset_to_default_title">Reset to defaults</string>
    <string name="silent_mode_settings_reset_to_default_summary"></string>
    <string name="silent_mode_settings_mute_all_title">Mute all</string>
    <string name="silent_mode_settings_mute_all_summary"></string>

    <string name="silent_mode_settings_voice_call_title">Voice calls</string>
    <string name="silent_mode_settings_system_title">System sounds</string>
    <string name="silent_mode_settings_ring_title">Call rings</string>
    <string name="silent_mode_settings_music_title">Music</string>
    <string name="silent_mode_settings_alarm_title">Alarms</string>
    <string name="silent_mode_settings_notification_title">Notifications</string>
    <string name="silent_mode_settings_bluetooth_title">Bluetooth output</string>
    <string name="silent_mode_settings_system_enforced_title">System-enforced</string>
    <string name="silent_mode_settings_dtmf_title">DTMF tones</string>
    <string name="silent_mode_settings_tts_title">Text-to-speech</string>

    <string name="silent_mode_settings_voice_call_summary">Don\'t hear active calls in silent mode.</string>
    <string name="silent_mode_settings_system_summary">Silent mode silences system sounds.</string>
    <string name="silent_mode_settings_ring_summary">Don\'t ring for incoming calls in silent mode.</string>
    <string name="silent_mode_settings_music_summary">Don\'t play music in silent mode.</string>
    <string name="silent_mode_settings_alarm_summary">Don\'t play alarms in silent mode.</string>
    <string name="silent_mode_settings_notification_summary">Don\'t play notifications in silent mode.</string>
    <string name="silent_mode_settings_bluetooth_summary">Don\'t play to bluetooth in silent mode.</string>
    <string name="silent_mode_settings_system_enforced_summary">Don\'t play required sounds in silent mode.</string>
    <string name="silent_mode_settings_dtmf_summary">Don\'t play dialing sounds in silent mode.</string>
    <string name="silent_mode_settings_tts_summary">Don\'t play text-to-speech in silent mode.</string>

    <!-- Dock settings title, top level -->
    <string name="dock_settings">Dock</string>
+111 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 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.
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
        android:key="silent_mode_settings">

        <Preference
                android:key="reset_to_default"
                android:title="@string/silent_mode_settings_reset_to_default_title"
                android:summary="@string/silent_mode_settings_reset_to_default_summary"
                android:persistent="false" />

        <Preference
                android:key="mute_all"
                android:title="@string/silent_mode_settings_mute_all_title"
                android:summary="@string/silent_mode_settings_mute_all_summary"
                android:persistent="false" />

        <!-- Don't allow disabling the voice call channel.  It doesn't make sense to do
             that and it'll probably just confuse people. -->
        <!--
        <CheckBoxPreference
                android:key="stream_voice_call"
                android:title="@string/silent_mode_settings_voice_call_title"
                android:summary="@string/silent_mode_settings_voice_call_summary"
                android:persistent="false" />
        -->
        <CheckBoxPreference
                android:key="stream_system"
                android:title="@string/silent_mode_settings_system_title"
                android:summary="@string/silent_mode_settings_system_summary"
                android:persistent="false" />

        <CheckBoxPreference
                android:key="stream_ring"
                android:title="@string/silent_mode_settings_ring_title"
                android:summary="@string/silent_mode_settings_ring_summary"
                android:persistent="false" />

        <CheckBoxPreference
                android:key="stream_music"
                android:title="@string/silent_mode_settings_music_title"
                android:summary="@string/silent_mode_settings_music_summary"
                android:persistent="false" />

        <CheckBoxPreference
                android:key="stream_alarm"
                android:title="@string/silent_mode_settings_alarm_title"
                android:summary="@string/silent_mode_settings_alarm_summary"
                android:persistent="false" />

        <CheckBoxPreference
                android:key="stream_notification"
                android:title="@string/silent_mode_settings_notification_title"
                android:summary="@string/silent_mode_settings_notification_summary"
                android:persistent="false" />

        <!-- I'm not sure exactly what this silences.  Let's hide it until it's tested
             to make sure it has any value.  It's pointless to silence a bluetooth headset. -->
        <!--
        <CheckBoxPreference
                android:key="stream_bluetooth"
                android:title="@string/silent_mode_settings_bluetooth_title"
                android:summary="@string/silent_mode_settings_bluetooth_summary"
                android:persistent="false" />
        -->

        <!-- Don't show the "system enforced" channel; users don't know what that means,
             and if we want to allow silencing the camera sound, that should be done in
             the camera settings.  Maybe we should tie this one to SYSTEM. -->
        <!--
        <CheckBoxPreference
                android:key="stream_system_enforced"
                android:title="@string/silent_mode_settings_system_enforced_title"
                android:summary="@string/silent_mode_settings_system_enforced_summary"
                android:persistent="false" />
        -->

        <!-- Hide the DTMF toggle.  DTMF sounds seem to be silenced regardless of this (they
             must be going over a different channel). -->
        <!--
        <CheckBoxPreference
                android:key="stream_dtmf"
                android:title="@string/silent_mode_settings_dtmf_title"
                android:summary="@string/silent_mode_settings_dtmf_summary"
                android:persistent="false" />
        -->

        <!-- I havn't tested this one. -->
        <!--
        <CheckBoxPreference
                android:key="stream_tts"
                android:title="@string/silent_mode_settings_tts_title"
                android:summary="@string/silent_mode_settings_tts_summary"
                android:persistent="false" />
        -->
</PreferenceScreen>
+10 −0
Original line number Diff line number Diff line
@@ -29,6 +29,16 @@
                android:order="1"
                android:disableDependentsState="true" />

        <PreferenceScreen
                android:title="@string/silent_mode_settings_title"
                android:summary="@string/silent_mode_settings_summary"
                android:order="10">
                <intent
                        android:action="android.intent.action.MAIN"
                        android:targetPackage="com.android.settings"
                        android:targetClass="com.android.settings.SilentChannels" />
        </PreferenceScreen>

        <com.android.settings.RingerVolumePreference
                android:key="ring_volume"
                android:title="@string/ring_volume_title"
+209 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 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.settings;

import android.content.ContentResolver;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.util.Log;

public class SilentChannels extends PreferenceActivity implements
        Preference.OnPreferenceChangeListener {
    private static final String TAG = "SilentChannels";

    private Preference mResetToDefault;
    private Preference mMuteAll;

    private CheckBoxPreference mStreamVoiceCall; // STREAM_VOICE_CALL
    private CheckBoxPreference mStreamSystem; // STREAM_SYSTEM
    private CheckBoxPreference mStreamRing; // STREAM_RING
    private CheckBoxPreference mStreamMusic; // STREAM_MUSIC
    private CheckBoxPreference mStreamAlarm; // STREAM_ALARM
    private CheckBoxPreference mStreamNotification; // STREAM_NOTIFICATION
    private CheckBoxPreference mStreamBluetooth; // STREAM_BLUETOOTH_SCO
    private CheckBoxPreference mStreamSystemEnforced; // STREAM_SYSTEM_ENFORCED
    private CheckBoxPreference mStreamDTMF; // STREAM_DTMF
    private CheckBoxPreference mStreamTTS; // STREAM_TTS

    private CheckBoxPreference[] mAllStreamPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.silent_mode_settings);

        mResetToDefault = findPreference("reset_to_default");
        mMuteAll = findPreference("mute_all");

        /* Note that some of these may be null, if the preference is disabled. */
        mStreamVoiceCall = (CheckBoxPreference) findPreference("stream_voice_call");
        mStreamSystem = (CheckBoxPreference) findPreference("stream_system");
        mStreamRing = (CheckBoxPreference) findPreference("stream_ring");
        mStreamMusic = (CheckBoxPreference) findPreference("stream_music");
        mStreamAlarm = (CheckBoxPreference) findPreference("stream_alarm");
        mStreamNotification = (CheckBoxPreference) findPreference("stream_notification");
        mStreamBluetooth = (CheckBoxPreference) findPreference("stream_bluetooth");
        mStreamSystemEnforced = (CheckBoxPreference) findPreference("stream_system_enforced");
        mStreamDTMF = (CheckBoxPreference) findPreference("stream_dtmf");
        mStreamTTS = (CheckBoxPreference) findPreference("stream_tts");

        CheckBoxPreference allPreferences[] = {
            mStreamVoiceCall, mStreamSystem, mStreamRing, mStreamMusic, mStreamAlarm,
            mStreamNotification, mStreamBluetooth, mStreamSystemEnforced, mStreamDTMF, mStreamTTS
        };
        mAllStreamPrefs = allPreferences;

        updateState();
    }

    @Override
    protected void onResume() {
        super.onResume();

        updateState();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private int getPreferenceMask(Preference preference) {
        if (preference == mStreamVoiceCall)      return 1 << AudioManager.STREAM_VOICE_CALL;
        if (preference == mStreamSystem)         return 1 << AudioManager.STREAM_SYSTEM;
        if (preference == mStreamRing)           return 1 << AudioManager.STREAM_RING;
        if (preference == mStreamMusic)          return 1 << AudioManager.STREAM_MUSIC;
        if (preference == mStreamAlarm)          return 1 << AudioManager.STREAM_ALARM;
        if (preference == mStreamNotification)   return 1 << AudioManager.STREAM_NOTIFICATION;
        if (preference == mStreamBluetooth)      return 1 << AudioManager.STREAM_BLUETOOTH_SCO;
        if (preference == mStreamSystemEnforced) return 1 << AudioManager.STREAM_SYSTEM_ENFORCED;
        if (preference == mStreamDTMF)           return 1 << AudioManager.STREAM_DTMF;
        if (preference == mStreamTTS)            return 1 << AudioManager.STREAM_TTS;
        Log.w(TAG, "Unexpected preference given to getPreferenceMask");
        return 0;
    }

    private void setAllOn() {
        /* Set all streams which are displayed in the menu to silenced.  Don't change
         * any streams which we're not displaying in the menu. */
        int silentModeStreams = Settings.System.getInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
        for (int i = 0; i < mAllStreamPrefs.length; i++) {
            CheckBoxPreference pref = mAllStreamPrefs[i];
            if (pref == null)
                continue;

            silentModeStreams |= getPreferenceMask(pref);
        }

        Settings.System.putInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, silentModeStreams);
        updateState();
    }

    private void resetToDefault() {
        /*
         * This matches the default set in
         *   frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
         *
         * There's no way to query the default properly.
         *
         * Don't change any streams which we're not displaying in the menu.
         */
        final int defaultStreams =
              (1 << AudioManager.STREAM_RING)
            | (1 << AudioManager.STREAM_NOTIFICATION)
            | (1 << AudioManager.STREAM_SYSTEM)
            | (1 << AudioManager.STREAM_SYSTEM_ENFORCED);

        int silentModeStreams = Settings.System.getInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
        for (int i = 0; i < mAllStreamPrefs.length; i++) {
            CheckBoxPreference pref = mAllStreamPrefs[i];
            if (pref == null)
                continue;

            int bit = getPreferenceMask(pref);
            if ((defaultStreams & bit) != 0)
                silentModeStreams |= bit;
            else
                silentModeStreams &= ~bit;
        }

        Settings.System.putInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, silentModeStreams);
        updateState();
    }

    private void updateState() {
        final int silentModeStreams = Settings.System.getInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
        for (int i = 0; i < mAllStreamPrefs.length; i++) {
            CheckBoxPreference pref = mAllStreamPrefs[i];
            if (pref == null)
                continue;

            boolean silenced = (silentModeStreams & getPreferenceMask(pref)) != 0;
            pref.setChecked(silenced);
        }
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (preference == mResetToDefault)
        {
            resetToDefault();
            return true;
        }

        if (preference == mMuteAll)
        {
            setAllOn();
            return true;
        }

        for (int i = 0; i < mAllStreamPrefs.length; i++) {
            CheckBoxPreference pref = mAllStreamPrefs[i];
            if (pref == null || pref != preference)
                continue;

            final int mask = getPreferenceMask(pref);
            int silentModeStreams = Settings.System.getInt(getContentResolver(),
                    Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
            if (pref.isChecked())
                silentModeStreams |= mask;
            else
                silentModeStreams &= ~mask;

            Settings.System.putInt(getContentResolver(),
                    Settings.System.MODE_RINGER_STREAMS_AFFECTED, silentModeStreams);
            return true;
        }

        return true;
    }

    public boolean onPreferenceChange(Preference preference, Object newValue) {
        return true;
    }
}
Loading