package com.adobe.phonegap.push;

import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;

import com.google.android.gms.gcm.GcmPubSub;
import com.google.android.gms.iid.InstanceID;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import android.net.Uri;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;

public class RingtonePickerActivity extends Activity implements OnClickListener{

Ringtone rt;
RingtoneManager mRingtoneManager;
TextView text;
Button button1;
Cursor mcursor;
Intent Mringtone;
String title;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picker);

//the following appends the cursor with the cursor that is used when the ringtone picker pops up
mRingtoneManager = new RingtoneManager(this);
mcursor = mRingtoneManager.getCursor();
title = mRingtoneManager.EXTRA_RINGTONE_TITLE;

Cursor myCursor = myRingtoneManager.getCursor();


text = (TextView)findViewById(R.id.textadd);
button1 = (Button)findViewById(R.id.button01);
button1.setOnClickListener(this);


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Starts the intent or Activity of the ringtone manager, opens popup box
Mringtone = new Intent(mRingtoneManager.ACTION_RINGTONE_PICKER);

//specifies what type of tone we want, in this case "ringtone", can be notification if you want
Mringtone.putExtra(mRingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);

//gives the title of the RingtoneManager picker title
Mringtone.putExtra(mRingtoneManager.EXTRA_RINGTONE_TITLE, "This is the title Of Your Picker!");

//returns true shows the rest of the songs on the device in the default location
Mringtone.getBooleanExtra(mRingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM, true);

String uri = null;
//chooses and keeps the selected item as a uri
if ( uri != null ) {
Mringtone.putExtra(mRingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse( uri ));
} else {
Mringtone.putExtra(mRingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri)null);
}


startActivityForResult(Mringtone, 0);

}

protected void onActivityResult(int requestCode, int resultCode, Intent Mringtone) {
switch (resultCode) {
/*
*
*/
case RESULT_OK:
//sents the ringtone that is picked in the Ringtone Picker Dialog
Uri uri = Mringtone.getParcelableExtra(mRingtoneManager.EXTRA_RINGTONE_PICKED_URI);

//send the output of the selected to a string
String test = uri.toString();

//the program creates a "line break" when using the "\n" inside a string value
text.setText("\n " + test + "\n " + title);

//prints out the result in the console window
Log.i("Sample", "uri " + uri);

//this passed the ringtone selected from the user to a new method
play(uri);

//inserts another line break for more data, this times adds the cursor count on the selected item
text.append("\n " + mcursor.getCount());

//set default ringtone
try
{
RingtoneManager.setActualDefaultRingtoneUri(this, resultCode, uri);
}
catch (Exception localException)
{

}
break;


}

}


play(uri);


//this method captures the ringtone from the selection and plays it in the main activity
private void play(Uri uri) {
// TODO Auto-generated method stub
if (uri != null) {

//in order to play the ringtone, you need to create a new Ringtone with RingtoneManager and pass it to a variable
rt = mRingtoneManager.getRingtone(this, uri);
rt.play();

}
}

@@ -1028,16 +1028,250 @@
 title;%0A%0A
+@Override%0Aprotected void onDestroy() %7B%0A// TODO Auto-generated method stub%0Asuper.onDestroy();%0Aif (rt.isPlaying()) %7B%0Art.stop();%0A%7D else %7B%0A%0A%7D%0A%7D%0A%0A@Override%0Aprotected void onPause() %7B%0A// TODO Auto-generated method stub%0Asuper.onPause();%0A%0A%7D%0A%0A
 /** Call
@@ -1432,22 +1432,20 @@
 .layout.
-picker
+main
 );%0A%0A//th
@@ -1675,59 +1675,8 @@
 E;%0A%0A
-Cursor myCursor = myRingtoneManager.getCursor();%0A%0A%0A
 text
@@ -1797,16 +1797,433 @@
 (this);%0A
+%7D%0A%0A//adds a menu item from the res/menu/menu.xml%0A@Override%0Apublic boolean onCreateOptionsMenu(Menu menu) %7B%0AMenuInflater inflater = getMenuInflater();%0Ainflater.inflate(R.menu.menu, menu);%0Areturn true;%0A%7D%0A%0A//adds an action to the button click%0A@Override%0Apublic boolean onOptionsItemSelected(MenuItem item) %7B%0Aswitch (item.getItemId()) %7B%0Acase R.id.item1:%0Aif (rt.isPlaying()) %7B%0Art.stop();%0A%7D%0Afinish();%0Abreak;%0A%7D%0Areturn true;%0A%7D
 %0A%0A@Overr
@@ -2811,17 +2811,16 @@
 the song
-s
  on the 
@@ -3189,17 +3189,16 @@
 ll);%0A%7D%0A%0A
-%0A
 startAct
@@ -3228,16 +3228,17 @@
 e, 0);%0A%0A
+%0A
 %7D%0A%0Aprote
@@ -4168,21 +4168,8 @@
 %7D%0A%0A%0A
-play(uri);%0A%0A%0A
 //th
@@ -4509,12 +4509,16 @@
 lay();%0A%0A%7D%0A%7D%0A
+%0A%0A%7D%0A

Update RingtonePickerActivity.java