VolumePref and RingtonePref

Quickly share your Android Code Snippets here...

VolumePref and RingtonePref

Postby r00t316 » Tue Jul 27, 2010 1:36 pm

Others like myself may want to use a VolumePref or RingtonePref in their app. Maybe because they are using a database to store settings or whatever...
Hopefully someone will find these useful.
(I did search the forum and did not find anything like this, if there is I apologize and remove thread.)

RingtonePref is subclasses RingtonePreference


in the res/xml/pref.xml
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <your.package.RingtonePref android:key="ringtone" android:title="Select Ringtone" android:ringtoneType="all" android:showDefault="false"/>
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


in your main activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. RingtonePref ringtonePref = (RingtonePref) findPreference("ringtone");
  2.  
  3. // grab your ringtone from your database or whatever
  4. // then set it
  5.  
  6. ringtonePref.setRingtone(Uri.parse(ringtoneString));
  7.  
  8.  
  9.  
  10.  
  11. // to retrieve it back once they select it
  12. ringtonePref.getRingtone();
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class RingtonePref extends RingtonePreference {
  2.        
  3.         private Uri mRingtone;
  4.        
  5.  
  6.         public RingtonePref(Context context, AttributeSet attrs) {
  7.                 super(context, attrs);
  8.         }
  9.  
  10.  
  11.         @Override
  12.         protected Uri onRestoreRingtone() {
  13.                 return mRingtone;
  14.         }
  15.  
  16.         @Override
  17.         protected void onSaveRingtone(Uri ringtoneUri) {
  18.                 setRingtone(ringtoneUri);
  19.         }
  20.        
  21.         public void setRingtone(Uri ringtoneUri) {
  22.                 mRingtone = ringtoneUri;
  23.         }
  24.        
  25.         public String getRingtone() {
  26.                 if (mRingtone != null) {
  27.                         return mRingtone.toString();
  28.                 }
  29.                 return null;
  30.                
  31.         }
  32. }
  33.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Last edited by r00t316 on Sat Jul 31, 2010 12:47 pm, edited 1 time in total.
r00t316
Developer
Developer
 
Posts: 29
Joined: Tue Jul 27, 2010 12:35 pm
Location: Florida

Top

Re: VolumePref and RingtonePref

Postby r00t316 » Tue Jul 27, 2010 1:45 pm

Forgot the volume pref....
This will play a ringtone if one is available as you slide the volume slider.
I based this off of VolumePreference but this one seems to work better for some reason?

in res/xml/pref.xml
Code: Select all
<your.package.VolumePref android:key="ring_volume" android:title="Ringer Volume"/>


in your main activity
Code: Select all
mRingVolume = (VolumePref) findPreference("ring_volume");

// set the value from your prefeneces or database if it already exits
mRingVolume.setVolume((int)profile.ringvolume);


// get the prefenence once set
mRingVolume.getVolume()



VolumePref
Code: Select all
public class VolumePref extends DialogPreference implements OnSeekBarChangeListener, Runnable {

   private Drawable mMyIcon;
   private SeekBar mSeekBar;
   private int iVolume;
   private AudioManager mAudioManager;
   private final int iStreamType = AudioManager.STREAM_RING;
   private Ringtone mRingtone;
   private Uri mRingtoneUri;
   private Handler mHandler = new Handler();
   private int iLastVolume;
   private int iOrigVolume;

   public VolumePref(Context context, AttributeSet attrs) {
      super(context, attrs);
      
      setDialogLayoutResource(R.layout.volumepref);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
       
        // Steal the XML dialogIcon attribute's value
        mMyIcon = getDialogIcon();
        setDialogIcon(mMyIcon);
       
        // initialize
        iVolume = -1;
        iLastVolume = -1;
        iOrigVolume = -1;
        mRingtone = null;
        mRingtoneUri = null;
       
        initalizeAudio();
   }

   private void initalizeAudio() {
      Context context = getContext();
       mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
       iOrigVolume = iVolume;
   }
   
   
    @Override
       protected void onDialogClosed(boolean positiveResult) {
           super.onDialogClosed(positiveResult);

           if (!positiveResult)
              iVolume = iOrigVolume;
           else
              iVolume = mSeekBar.getProgress();
          
           cleanup();
       }

       private void cleanup() {
          stop();
   }

      public void onActivityStop() {
           cleanup();
       }


   @Override
   protected void onBindDialogView(View view) {
      super.onBindDialogView(view);
      
      iOrigVolume = iVolume;
      mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
      mSeekBar.setMax(mAudioManager.getStreamMaxVolume(iStreamType));
      mSeekBar.setOnSeekBarChangeListener(this);
      mSeekBar.setProgress(iVolume);
      
      if (mRingtoneUri != null) {
         mRingtone = RingtoneManager.getRingtone(getContext(), mRingtoneUri);
      }
      else { // hopefully we can grab a default ringtone
         mRingtoneUri = android.provider.Settings.System.DEFAULT_RINGTONE_URI;
         mRingtone = RingtoneManager.getRingtone(getContext(), mRingtoneUri);
      }
      
      if (mRingtone != null)
         mRingtone.setStreamType(iStreamType);
         
   }
   
   protected static SeekBar getSeekBar(View dialogView) {
      return (SeekBar) dialogView.findViewById(R.id.seekbar);
   }   

   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
         boolean fromUser) {
   
      if (!fromUser)
         return;
      
      if (mRingtone != null && !mRingtone.isPlaying())
         sampleOn();

      postVolumeChange(progress);
   }

   private void postVolumeChange(int progress) {
      iLastVolume = progress;
      mHandler.removeCallbacks(null);
      mHandler.post(this);
      
   }

   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
      
   }

   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
      if (mRingtone != null && !mRingtone.isPlaying()) {
         sampleOn();
      }
   }
   
   public void setVolume(int volume) {
      iVolume = volume;
   }
   
   public int getVolume() {
         return iVolume;
   }
   
   public void setRingtone(Uri ringtoneUri) {
      mRingtoneUri = ringtoneUri;
   }

   @Override
   public void run() {
      mAudioManager.setStreamVolume(iStreamType, iLastVolume, 0);
   }
   
   public void stop() {
      sampleOff();
      mSeekBar.setOnSeekBarChangeListener(null);
   }
   private void sampleOff() {
      if (mRingtone != null )
         mRingtone.stop();
      
   }

   public void sampleOn() {
      sampleOff();
      if (mRingtone != null && !mRingtone.isPlaying())
         mRingtone.play();
   }

}
r00t316
Developer
Developer
 
Posts: 29
Joined: Tue Jul 27, 2010 12:35 pm
Location: Florida

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests