Android Open Source - PhoneProfiles_Eclipse Volume Dialog Preference






From Project

Back to project page PhoneProfiles_Eclipse.

License

The source code is released under:

Apache License

If you think the Android project PhoneProfiles_Eclipse listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright CMW Mobile.com, 2010./*  www.  j  ava2s  .  c  o m*/
 */
package sk.henrichg.phoneprofiles;

import android.content.Context;
import android.content.DialogInterface;

import android.content.res.TypedArray;

import android.media.AudioManager;
import android.preference.DialogPreference;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.CompoundButton;

/**
 * The SeekBarDialogPreference class is a DialogPreference based and provides a
 * seekbar preference.
 * @author Casper Wakkers
 */
public class VolumeDialogPreference extends
    DialogPreference implements SeekBar.OnSeekBarChangeListener, CompoundButton.OnCheckedChangeListener {
  // Layout widgets.

  Context _context;
  private SeekBar seekBar = null;
  private TextView valueText = null;
  private CheckBox noChangeChBox = null; 
  private CheckBox defaultProfileChBox = null; 
  
  private AudioManager audioManager = null;

  // Custom xml attributes.
  private String volumeType = null;
  private int noChange = 0;
  private int defaultProfile = 0;
  private int disableDefaultProfile = 0;
  
  private int maximumValue = 7;
  private int minimumValue = 0;
  private int defaultValue = 0;
  private int defaultRingerMode = 0;
  private int stepSize = 1;

  private String sValue = "0|1";
  private int value = 0;

  /**
   * The SeekBarDialogPreference constructor.
   * @param context of this preference.
   * @param attrs custom xml attributes.
   */
  public VolumeDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    _context = context;
    
    TypedArray typedArray = context.obtainStyledAttributes(attrs,
      R.styleable.VolumeDialogPreference);

    volumeType = typedArray.getString(
      R.styleable.VolumeDialogPreference_volumeType);
    noChange = typedArray.getInteger(
      R.styleable.VolumeDialogPreference_vNoChange, 1);
    defaultProfile = typedArray.getInteger(
        R.styleable.VolumeDialogPreference_vDefaultProfile, 0);
    disableDefaultProfile = typedArray.getInteger(
        R.styleable.VolumeDialogPreference_vDisableDefaultProfile, 0);

    audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    // zistima maximalnu hodnotu z audio managera
    if (volumeType.equalsIgnoreCase("RINGTONE")) 
      maximumValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
    else
    if (volumeType.equalsIgnoreCase("NOTIFICATION")) 
      maximumValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
    else
    if (volumeType.equalsIgnoreCase("MEDIA")) 
      maximumValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    else
    if (volumeType.equalsIgnoreCase("ALARM")) 
      maximumValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
    else
    if (volumeType.equalsIgnoreCase("SYSTEM")) 
      maximumValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
    else
    if (volumeType.equalsIgnoreCase("VOICE")) 
      maximumValue = audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
    // zistime default hodnotu z audio managera
    if (volumeType.equalsIgnoreCase("RINGTONE")) 
      defaultValue = audioManager.getStreamVolume(AudioManager.STREAM_RING);
    else
    if (volumeType.equalsIgnoreCase("NOTIFICATION")) 
      defaultValue = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
    else
    if (volumeType.equalsIgnoreCase("MEDIA")) 
      defaultValue = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    else
    if (volumeType.equalsIgnoreCase("ALARM")) 
      defaultValue = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
    else
    if (volumeType.equalsIgnoreCase("SYSTEM")) 
      defaultValue = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
    else
    if (volumeType.equalsIgnoreCase("VOICE")) 
      defaultValue = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
    defaultRingerMode = audioManager.getRingerMode();
    
    typedArray.recycle();
  }
  
  /**
   * {@inheritDoc}
   */
  protected View onCreateDialogView() {
    LayoutInflater layoutInflater = LayoutInflater.from(getContext());

    View view = layoutInflater.inflate(
      R.layout.activity_volume_pref_dialog, null);

    seekBar = (SeekBar)view.findViewById(R.id.volumePrefDialogSeekbar);
    valueText = (TextView)view.findViewById(R.id.volumePrefDialogValueText);
    noChangeChBox = (CheckBox)view.findViewById(R.id.volumePrefDialogNoChange);
    defaultProfileChBox = (CheckBox)view.findViewById(R.id.volumePrefDialogDefaultProfile);

    seekBar.setOnSeekBarChangeListener(this);
    seekBar.setKeyProgressIncrement(stepSize);
    seekBar.setMax(maximumValue - minimumValue);
    
    getValueVDP();

    seekBar.setProgress(value);

    noChangeChBox.setOnCheckedChangeListener(this);
    noChangeChBox.setChecked((noChange == 1));

    defaultProfileChBox.setOnCheckedChangeListener(this);
    defaultProfileChBox.setChecked((defaultProfile == 1));
    defaultProfileChBox.setEnabled(disableDefaultProfile == 0);

    if (noChange == 1)
      defaultProfileChBox.setChecked(false);
    if (defaultProfile == 1)
      noChangeChBox.setChecked(false);
    
    valueText.setEnabled((noChange == 0) && (defaultProfile == 0));
    seekBar.setEnabled((noChange == 0) && (defaultProfile == 0));
    
    return view;
  }
  /**
   * {@inheritDoc}
   */
  public void onProgressChanged(SeekBar seek, int newValue,
      boolean fromTouch) {
    // Round the value to the closest integer value.
    if (stepSize >= 1) {
      value = Math.round(newValue/stepSize)*stepSize;
    }
    else {
      value = newValue;
    }

    // Set the valueText text.
    valueText.setText(String.valueOf(value + minimumValue));
    
    callChangeListener(value);
  }

  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    //Log.d("VolumeDialogPreference.onCheckedChanged", Boolean.toString(isChecked));
    
    if (buttonView.getId() == R.id.volumePrefDialogNoChange)
    {
      noChange = (isChecked)? 1 : 0;

      valueText.setEnabled((noChange == 0) && (defaultProfile == 0));
      seekBar.setEnabled((noChange == 0) && (defaultProfile == 0));
      if (isChecked)
        defaultProfileChBox.setChecked(false);
    }

    if (buttonView.getId() == R.id.volumePrefDialogDefaultProfile)
    {
      defaultProfile = (isChecked)? 1 : 0;

      valueText.setEnabled((noChange == 0) && (defaultProfile == 0));
      seekBar.setEnabled((noChange == 0) && (defaultProfile == 0));
      if (isChecked)
        noChangeChBox.setChecked(false);
    }
    
    callChangeListener(noChange);
  }
  
  /**
   * {@inheritDoc}
   */
  public void onStartTrackingTouch(SeekBar seek) {
  }
  /**
   * {@inheritDoc}
   */
  public void onStopTrackingTouch(SeekBar seek) {
    if (volumeType.equalsIgnoreCase("RINGTONE")) 
      audioManager.setStreamVolume(AudioManager.STREAM_RING, value, AudioManager.FLAG_PLAY_SOUND);
    else
    if (volumeType.equalsIgnoreCase("NOTIFICATION")) 
      audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, value, AudioManager.FLAG_PLAY_SOUND);
    else
    if (volumeType.equalsIgnoreCase("MEDIA")) 
      audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, AudioManager.FLAG_PLAY_SOUND);
    else
    if (volumeType.equalsIgnoreCase("ALARM")) 
      audioManager.setStreamVolume(AudioManager.STREAM_ALARM, value, AudioManager.FLAG_PLAY_SOUND);
    else
    if (volumeType.equalsIgnoreCase("SYSTEM")) 
      audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, value, AudioManager.FLAG_PLAY_SOUND);
    else
    if (volumeType.equalsIgnoreCase("VOICE")) 
      audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, value, AudioManager.FLAG_PLAY_SOUND);
  }
  /**
   * {@inheritDoc}
   */
  public void onClick(DialogInterface dialog, int which) {
    // if the positive button is clicked, we persist the value.
    if (which == DialogInterface.BUTTON_POSITIVE) {
      if (shouldPersist()) {
        //Log.d("VolumeDialogPreference.onClick","xxxx");
        persistString(Integer.toString(value + minimumValue)
            + "|" + Integer.toString(noChange)
            + "|" + Integer.toString(defaultProfile));
        setSummaryVDP();
      }
    }

    super.onClick(dialog, which);
  }

  @Override
  protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
  {
    if (restoreValue) {
      // restore state
      getValueVDP();
    }
    else {
      // set state
      value = 0;
      noChange = 1;
      defaultProfile = 0;
      persistString(Integer.toString(value + minimumValue)
          + "|" + Integer.toString(noChange)
          + "|" + Integer.toString(defaultProfile));      
    }
    setSummaryVDP();
  }
  
  private void getValueVDP()
  {
    // Get the persistent value and correct it for the minimum value.
    sValue = getPersistedString(sValue);
    
    String[] splits = sValue.split("\\|");
    try {
      value = Integer.parseInt(splits[0]);
      if (value == -1)
      {
        value = defaultValue;
      }
    } catch (Exception e) {
      value = 0;
    }
    value = value - minimumValue;
    try {
      noChange = Integer.parseInt(splits[1]);
    } catch (Exception e) {
      noChange = 1;
    }
    try {
      defaultProfile = Integer.parseInt(splits[2]);
    } catch (Exception e) {
      defaultProfile = 0;
    }
    
    // You're never know...
    if (value < 0) {
      value = 0;
    }
  }
  
  private void setSummaryVDP()
  {
    String prefVolumeDataSummary;
    if (noChange == 1)
      prefVolumeDataSummary = _context.getResources().getString(R.string.preference_profile_no_change);
    else
    if (defaultProfile == 1)
      prefVolumeDataSummary = _context.getResources().getString(R.string.preference_profile_default_profile);
    else
      prefVolumeDataSummary = String.valueOf(value) + " / " + String.valueOf(maximumValue);
    setSummary(prefVolumeDataSummary);
  }
  
  @Override
  public void onDismiss(DialogInterface dialog)
  {
    audioManager.setRingerMode(defaultRingerMode);
    if (volumeType.equalsIgnoreCase("RINGTONE")) 
      audioManager.setStreamVolume(AudioManager.STREAM_RING, defaultValue, 0);
    else
    if (volumeType.equalsIgnoreCase("NOTIFICATION")) 
      audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, defaultValue, 0);
    else
    if (volumeType.equalsIgnoreCase("MEDIA")) 
      audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, defaultValue, 0);
    else
    if (volumeType.equalsIgnoreCase("ALARM")) 
      audioManager.setStreamVolume(AudioManager.STREAM_ALARM, defaultValue, 0);
    else
    if (volumeType.equalsIgnoreCase("SYSTEM")) 
      audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, defaultValue, 0);
    else
    if (volumeType.equalsIgnoreCase("VOICE")) 
      audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, defaultValue, 0);
    audioManager.setRingerMode(defaultRingerMode);
  }
  
}




Java Source Code List

sk.henrichg.phoneprofiles.ActivateProfileActivity.java
sk.henrichg.phoneprofiles.ActivateProfileHelper.java
sk.henrichg.phoneprofiles.ActivateProfileListAdapter.java
sk.henrichg.phoneprofiles.ActivateProfileListFragment.java
sk.henrichg.phoneprofiles.ApplicationsCache.java
sk.henrichg.phoneprofiles.ApplicationsPreferenceAdapter.java
sk.henrichg.phoneprofiles.ApplicationsPreferenceDialog.java
sk.henrichg.phoneprofiles.ApplicationsPreference.java
sk.henrichg.phoneprofiles.BackgroundActivateProfileActivity.java
sk.henrichg.phoneprofiles.BitmapManipulator.java
sk.henrichg.phoneprofiles.BootUpReceiver.java
sk.henrichg.phoneprofiles.BrightnessDialogPreference.java
sk.henrichg.phoneprofiles.BrightnessView.java
sk.henrichg.phoneprofiles.DashClockBroadcastReceiver.java
sk.henrichg.phoneprofiles.DataWrapper.java
sk.henrichg.phoneprofiles.DatabaseHandler.java
sk.henrichg.phoneprofiles.EditorProfileListAdapter.java
sk.henrichg.phoneprofiles.EditorProfileListFragment.java
sk.henrichg.phoneprofiles.EditorProfilesActivity.java
sk.henrichg.phoneprofiles.ExecuteRadioProfilePrefsService.java
sk.henrichg.phoneprofiles.ExecuteVolumeProfilePrefsService.java
sk.henrichg.phoneprofiles.FirstStartService.java
sk.henrichg.phoneprofiles.GUIData.java
sk.henrichg.phoneprofiles.GlobalData.java
sk.henrichg.phoneprofiles.IconWidgetProvider.java
sk.henrichg.phoneprofiles.ImageViewPreferenceAdapter.java
sk.henrichg.phoneprofiles.ImageViewPreferenceDialog.java
sk.henrichg.phoneprofiles.ImageViewPreference.java
sk.henrichg.phoneprofiles.KeyguardService.java
sk.henrichg.phoneprofiles.Keyguard.java
sk.henrichg.phoneprofiles.LocaleChangedReceiver.java
sk.henrichg.phoneprofiles.NumberPickerPreference.java
sk.henrichg.phoneprofiles.OneRowWidgetProvider.java
sk.henrichg.phoneprofiles.PackageReplacedReceiver.java
sk.henrichg.phoneprofiles.PhoneCallBroadcastReceiver.java
sk.henrichg.phoneprofiles.PhoneCallReceiver.java
sk.henrichg.phoneprofiles.PhoneProfilesDashClockExtension.java
sk.henrichg.phoneprofiles.PhoneProfilesHelper.java
sk.henrichg.phoneprofiles.PhoneProfilesPreferencesActivity.java
sk.henrichg.phoneprofiles.PhoneProfilesPreferencesFragment.java
sk.henrichg.phoneprofiles.ProfileDurationAlarmBroadcastReceiver.java
sk.henrichg.phoneprofiles.ProfileListWidgetFactory.java
sk.henrichg.phoneprofiles.ProfileListWidgetProvider.java
sk.henrichg.phoneprofiles.ProfileListWidgetService.java
sk.henrichg.phoneprofiles.ProfilePreferenceAdapter.java
sk.henrichg.phoneprofiles.ProfilePreferenceDialog.java
sk.henrichg.phoneprofiles.ProfilePreference.java
sk.henrichg.phoneprofiles.ProfilePreferencesFragmentActivity.java
sk.henrichg.phoneprofiles.ProfilePreferencesFragment.java
sk.henrichg.phoneprofiles.ProfilePreferencesIndicator.java
sk.henrichg.phoneprofiles.Profile.java
sk.henrichg.phoneprofiles.ReceiversService.java
sk.henrichg.phoneprofiles.RefreshGUIBroadcastReceiver.java
sk.henrichg.phoneprofiles.RemoteExportDataActivity.java
sk.henrichg.phoneprofiles.RemoveBrightnessViewBroadcastReceiver.java
sk.henrichg.phoneprofiles.ScreenOnOffBroadcastReceiver.java
sk.henrichg.phoneprofiles.ShortcutCreatorActivity.java
sk.henrichg.phoneprofiles.ShortcutCreatorListFragment.java
sk.henrichg.phoneprofiles.ShortcutProfileListAdapter.java
sk.henrichg.phoneprofiles.UpgradePPHelperActivity.java
sk.henrichg.phoneprofiles.VolumeDialogPreference.java