<commit_before>package edu.chalmers.dat255.group09.Alarmed.utils;

import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.SeekBar;
import edu.chalmers.dat255.group09.Alarmed.R;

/**
 * Helper class which takes care of everything to do with vibration and audio
 * while creating an alarm
 * 
 * @author <NAME>
 */
public class AudioHelper {
	private Context context;
	private Intent intent;
	private MediaPlayer mediaPlayer;
	private AudioManager audioMan;

	private View volumeView;
	private AlertDialog volumeDialog, alarmToneDialog;
	private Map<String, String> alarmTones;

	/**
	 * Constructor for the AudioHelper object
	 * 
	 * @param c
	 *            Context holding the helper
	 * @param i
	 *            Intent containing information about the alarm
	 */
	public AudioHelper(Context c, Intent i) {
		context = c;
		intent = i;

		audioMan = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		mediaPlayer = new MediaPlayer();

		initSettings();
		setupAlarmToneMap();
		createVolumeDialog();
		createAlarmToneDialog();
	}

	private void initSettings() {
		intent.putExtra("vibration", true);
		intent.putExtra("volume",
				audioMan.getStreamMaxVolume(AudioManager.STREAM_ALARM));
		setInitialAlarmTone();
	}

	/**
	 * Since the alarm tone dialog isn't guaranteed to spawn during the lifetime
	 * of the CreateAlarm activity, this method sets the default value unless
	 * one already exists in the intent (in edit mode)
	 */
	private void setInitialAlarmTone() {
		String previousTone = intent.getStringExtra("toneuri");
		if (previousTone == null) {
			Uri tone = RingtoneManager.getActualDefaultRingtoneUri(context,
					RingtoneManager.TYPE_ALARM);
			if (tone == null) {
				tone = RingtoneManager.getActualDefaultRingtoneUri(context,
						RingtoneManager.TYPE_RINGTONE);
			}
			intent.putExtra("toneuri", tone.toString());
		}
	}

	/**
	 * Sets up a map of alarm tone URIs to their human readable titles
	 */
	private void setupAlarmToneMap() {
		RingtoneManager ringMan = new RingtoneManager(context);

		Cursor cur = ringMan.getCursor();

		int tonesAvailable = cur.getCount();
		if (tonesAvailable == 0) {
			alarmTones = new HashMap<String, String>();<commit_msg>AudioHelper: all sounds now available in alarm tone selector<commit_after>package edu.chalmers.dat255.group09.Alarmed.utils;

import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.SeekBar;
import edu.chalmers.dat255.group09.Alarmed.R;

/**
 * Helper class which takes care of everything to do with vibration and audio
 * while creating an alarm
 * 
 * @author <NAME>
 */
public class AudioHelper {
	private Context context;
	private Intent intent;
	private MediaPlayer mediaPlayer;
	private AudioManager audioMan;

	private View volumeView;
	private AlertDialog volumeDialog, alarmToneDialog;
	private Map<String, String> alarmTones;

	/**
	 * Constructor for the AudioHelper object
	 * 
	 * @param c
	 *            Context holding the helper
	 * @param i
	 *            Intent containing information about the alarm
	 */
	public AudioHelper(Context c, Intent i) {
		context = c;
		intent = i;

		audioMan = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		mediaPlayer = new MediaPlayer();

		initSettings();
		setupAlarmToneMap();
		createVolumeDialog();
		createAlarmToneDialog();
	}

	private void initSettings() {
		intent.putExtra("vibration", true);
		intent.putExtra("volume",
				audioMan.getStreamMaxVolume(AudioManager.STREAM_ALARM));
		setInitialAlarmTone();
	}

	/**
	 * Since the alarm tone dialog isn't guaranteed to spawn during the lifetime
	 * of the CreateAlarm activity, this method sets the default value unless
	 * one already exists in the intent (in edit mode)
	 */
	private void setInitialAlarmTone() {
		String previousTone = intent.getStringExtra("toneuri");
		if (previousTone == null) {
			Uri tone = RingtoneManager.getActualDefaultRingtoneUri(context,
					RingtoneManager.TYPE_ALARM);
			if (tone == null) {
				tone = RingtoneManager.getActualDefaultRingtoneUri(context,
						RingtoneManager.TYPE_RINGTONE);
			}
			intent.putExtra("toneuri", tone.toString());
		}
	}

	/**
	 * Sets up a map of alarm tone URIs to their human readable titles
	 */
	private void setupAlarmToneMap() {
		RingtoneManager ringMan = new RingtoneManager(context);
		ringMan.setType(RingtoneManager.TYPE_ALL);

		Cursor cur = ringMan.getCursor();

		int tonesAvailable = cur.getCount();
		if (tonesAvailable == 0) {
			alarmTones = new HashMap<String, String>();
{'fraction_non_alphanumeric': 0.09550760523523169, 'fraction_numerical': 0.0038910505836575876, 'mean_word_length': 4.5224609375, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3018757', 'n_tokens_mistral': 1834, 'n_tokens_neox': 1624, 'n_words': 456}