FileDocCategorySizeDatePackage
SoundAndDisplaySettings.javaAPI DocAndroid 1.5 API10957Wed May 06 22:42:48 BST 2009com.android.settings

SoundAndDisplaySettings

public class SoundAndDisplaySettings extends android.preference.PreferenceActivity implements Preference.OnPreferenceChangeListener

Fields Summary
private static final String
TAG
private static final int
FALLBACK_SCREEN_TIMEOUT_VALUE
If there is no setting in the provider, use this.
private static final String
KEY_SILENT
private static final String
KEY_VIBRATE
private static final String
KEY_SCREEN_TIMEOUT
private static final String
KEY_DTMF_TONE
private static final String
KEY_SOUND_EFFECTS
private static final String
KEY_ANIMATIONS
private static final String
KEY_ACCELEROMETER
private static final String
KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS
private android.preference.CheckBoxPreference
mSilent
private android.preference.CheckBoxPreference
mPlayMediaNotificationSounds
private android.os.IMountService
mMountService
private android.preference.CheckBoxPreference
mVibrate
private android.preference.CheckBoxPreference
mDtmfTone
private android.preference.CheckBoxPreference
mSoundEffects
private android.preference.CheckBoxPreference
mAnimations
private android.preference.CheckBoxPreference
mAccelerometer
private float[]
mAnimationScales
private android.media.AudioManager
mAudioManager
private android.view.IWindowManager
mWindowManager
private android.content.BroadcastReceiver
mReceiver
Constructors Summary
Methods Summary
protected voidonCreate(android.os.Bundle savedInstanceState)


    
        
        super.onCreate(savedInstanceState);
        ContentResolver resolver = getContentResolver();
        
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));

        mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
        
        addPreferencesFromResource(R.xml.sound_and_display_settings);
        
        mSilent = (CheckBoxPreference) findPreference(KEY_SILENT);
        mPlayMediaNotificationSounds = (CheckBoxPreference) findPreference(KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS);

        mVibrate = (CheckBoxPreference) findPreference(KEY_VIBRATE);
        mDtmfTone = (CheckBoxPreference) findPreference(KEY_DTMF_TONE);
        mDtmfTone.setPersistent(false);
        mDtmfTone.setChecked(Settings.System.getInt(resolver,
                Settings.System.DTMF_TONE_WHEN_DIALING, 1) != 0);
        mSoundEffects = (CheckBoxPreference) findPreference(KEY_SOUND_EFFECTS);
        mSoundEffects.setPersistent(false);
        mSoundEffects.setChecked(Settings.System.getInt(resolver,
                Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0);
        mAnimations = (CheckBoxPreference) findPreference(KEY_ANIMATIONS);
        mAnimations.setPersistent(false);
        mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
        mAccelerometer.setPersistent(false);
        
        ListPreference screenTimeoutPreference =
            (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
        screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt(
                resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE)));
        screenTimeoutPreference.setOnPreferenceChangeListener(this);
    
protected voidonPause()

        super.onPause();

        unregisterReceiver(mReceiver);
    
public booleanonPreferenceChange(android.preference.Preference preference, java.lang.Object objValue)

        if (KEY_SCREEN_TIMEOUT.equals(preference.getKey())) {
            int value = Integer.parseInt((String) objValue);
            try {
                Settings.System.putInt(getContentResolver(), 
                        SCREEN_OFF_TIMEOUT, value);
            } catch (NumberFormatException e) {
                Log.e(TAG, "could not persist screen timeout setting", e);
            }
        }
        
        return true;
    
public booleanonPreferenceTreeClick(android.preference.PreferenceScreen preferenceScreen, android.preference.Preference preference)


        if (preference == mSilent) {
            final boolean silent = mSilent.isChecked();
            mAudioManager.setRingerMode(silent ? AudioManager.RINGER_MODE_SILENT
                    : AudioManager.RINGER_MODE_NORMAL);
            updateState(false);
            
        } else if (preference == mPlayMediaNotificationSounds) {
            try {
                mMountService.setPlayNotificationSounds(mPlayMediaNotificationSounds.isChecked());
            } catch (RemoteException e) {
            }
        } else if (preference == mVibrate) {
            final boolean vibrate = mVibrate.isChecked();
            final boolean silent = mSilent.isChecked();
            
            if (silent) {
                mAudioManager.setRingerMode(vibrate ? AudioManager.RINGER_MODE_VIBRATE :
                    AudioManager.RINGER_MODE_SILENT);                
            } else {
                mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER,
                        vibrate ? AudioManager.VIBRATE_SETTING_ON
                                : AudioManager.VIBRATE_SETTING_OFF);
            }
            
        } else if (preference == mDtmfTone) {
            Settings.System.putInt(getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING,
                    mDtmfTone.isChecked() ? 1 : 0);
            
        } else if (preference == mSoundEffects) {
            if (mSoundEffects.isChecked()) {
                mAudioManager.loadSoundEffects();
            } else {
                mAudioManager.unloadSoundEffects();
            }
            Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED,
                    mSoundEffects.isChecked() ? 1 : 0);
            
        } else if (preference == mAnimations) {
            if (mAnimationScales.length > 0) {
                // Window animations are always on.
                mAnimationScales[0] = 1;
            }
            for (int i=1; i<mAnimationScales.length; i++) {
                mAnimationScales[i] = mAnimations.isChecked() ? 1 : 0;
            }
            try {
                mWindowManager.setAnimationScales(mAnimationScales);
            } catch (RemoteException e) {
            }
            
        } else if (preference == mAccelerometer) {
            Settings.System.putInt(getContentResolver(),
                    Settings.System.ACCELEROMETER_ROTATION,
                    mAccelerometer.isChecked() ? 1 : 0);
        }
        return true;
    
protected voidonResume()

        super.onResume();
        
        updateState(true);
        
        IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
        registerReceiver(mReceiver, filter);
    
private voidupdateState(boolean force)

        final int ringerMode = mAudioManager.getRingerMode();
        final boolean silentOrVibrateMode =
                ringerMode != AudioManager.RINGER_MODE_NORMAL;
        
        if (silentOrVibrateMode != mSilent.isChecked() || force) {
            mSilent.setChecked(silentOrVibrateMode);
        }

        try {
            mPlayMediaNotificationSounds.setChecked(mMountService.getPlayNotificationSounds());
        } catch (RemoteException e) {
        }
       
        boolean vibrateSetting;
        if (silentOrVibrateMode) {
            vibrateSetting = ringerMode == AudioManager.RINGER_MODE_VIBRATE;
        } else {
            vibrateSetting = mAudioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER)
                    == AudioManager.VIBRATE_SETTING_ON;            
        }
        if (vibrateSetting != mVibrate.isChecked() || force) {
            mVibrate.setChecked(vibrateSetting);
        }
        
        int silentModeStreams = Settings.System.getInt(getContentResolver(),
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
        boolean isAlarmInclSilentMode = (silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0; 
        mSilent.setSummary(isAlarmInclSilentMode ?
                R.string.silent_mode_incl_alarm_summary :
                R.string.silent_mode_summary);
        
        boolean animations = true;
        try {
            mAnimationScales = mWindowManager.getAnimationScales();
        } catch (RemoteException e) {
        }
        if (mAnimationScales != null) {
            // We will leave the window animations alone (always set),
            // and only use this to change the transition animations.
            for (int i=1; i<mAnimationScales.length; i++) {
                if (mAnimationScales[i] == 0) {
                    animations = false;
                    break;
                }
            }
        }
        if (animations != mAnimations.isChecked() || force) {
            mAnimations.setChecked(animations);
        }
        mAccelerometer.setChecked(Settings.System.getInt(
                getContentResolver(), 
                Settings.System.ACCELEROMETER_ROTATION, 0) != 0);