// Path: app/src/main/java/com/philliphsu/clock2/ringtone/playback/RingtoneLoop.java
// public final class RingtoneLoop {
// 
//     private final Context mContext;
//     private final AudioManager mAudioManager;
//     private final Uri mUri;
// 
//     private MediaPlayer mMediaPlayer;
// 
//     public RingtoneLoop(Context context, Uri uri) {
//         mContext = context;
//         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//         mUri = uri;
//     }
// 
//     public void play() {
//         try {
//             mMediaPlayer = new MediaPlayer();
//             mMediaPlayer.setDataSource(mContext, mUri);
//             if (mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
//                 // "Must call this method before prepare() or prepareAsync() in order
//                 // for the target stream type to become effective thereafter."
//                 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
//                 mMediaPlayer.setLooping(true);
//                 // There is prepare() and prepareAsync().
//                 // "For files, it is OK to call prepare(), which blocks until
//                 // MediaPlayer is ready for playback."
//                 mMediaPlayer.prepare();
//                 mMediaPlayer.start();
//             }
//         } catch (SecurityException | IOException e) {
//             destroyLocalPlayer();
//         }
//     }
// 
//     public void stop() {
//         if (mMediaPlayer != null) {
//             destroyLocalPlayer();
//         }
//     }
// 
//     private void destroyLocalPlayer() {
//         if (mMediaPlayer != null) {
//             mMediaPlayer.reset();
//             mMediaPlayer.release();
//             mMediaPlayer = null;
//         }
//     }
// 
// }


import android.content.DialogInterface;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import com.philliphsu.clock2.R;
import com.philliphsu.clock2.ringtone.playback.RingtoneLoop;
/*
 * Copyright 2017 Phillip Hsu
 *
 * This file is part of ClockPlus.
 *
 * ClockPlus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ClockPlus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ClockPlus.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.philliphsu.clock2.dialogs;



/**
 * Created by Phillip Hsu on 9/3/2016.
 * <p></p>
 * An alternative to the system's ringtone picker dialog. The differences are:
 * (1) this dialog matches the current theme,
 * (2) the selected ringtone URI is delivered via the {@link OnRingtoneSelectedListener
 * OnRingtoneSelectedListener} callback.
 * <p></p>
 * TODO: If a ringtone was playing and the configuration changes, the ringtone is destroyed.
 * Restore the playing ringtone (seamlessly, without the stutter that comes from restarting).
 * Setting setRetainInstance(true) in onCreate() made our app crash (error said attempted to
 * access closed Cursor).
 * We might need to play the ringtone from a Service instead, so we won't have to worry about
 * the ringtone being destroyed on rotation.
 */
public class RingtonePickerDialog extends BaseAlertDialogFragment {
    private static final String TAG = "RingtonePickerDialog";
    private static final String KEY_RINGTONE_URI = "key_ringtone_uri";

    private RingtoneManager mRingtoneManager;
    private OnRingtoneSelectedListener mOnRingtoneSelectedListener;
    private Uri mRingtoneUri;
// Path: app/src/main/java/com/philliphsu/clock2/ringtone/playback/RingtoneLoop.java
// public final class RingtoneLoop {
// 
//     private final Context mContext;
//     private final AudioManager mAudioManager;
//     private final Uri mUri;
// 
//     private MediaPlayer mMediaPlayer;
// 
//     public RingtoneLoop(Context context, Uri uri) {
//         mContext = context;
//         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//         mUri = uri;
//     }
// 
//     public void play() {
//         try {
//             mMediaPlayer = new MediaPlayer();
//             mMediaPlayer.setDataSource(mContext, mUri);
//             if (mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
//                 // "Must call this method before prepare() or prepareAsync() in order
//                 // for the target stream type to become effective thereafter."
//                 mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
//                 mMediaPlayer.setLooping(true);
//                 // There is prepare() and prepareAsync().
//                 // "For files, it is OK to call prepare(), which blocks until
//                 // MediaPlayer is ready for playback."
//                 mMediaPlayer.prepare();
//                 mMediaPlayer.start();
//             }
//         } catch (SecurityException | IOException e) {
//             destroyLocalPlayer();
//         }
//     }
// 
//     public void stop() {
//         if (mMediaPlayer != null) {
//             destroyLocalPlayer();
//         }
//     }
// 
//     private void destroyLocalPlayer() {
//         if (mMediaPlayer != null) {
//             mMediaPlayer.reset();
//             mMediaPlayer.release();
//             mMediaPlayer = null;
//         }
//     }
// 
// }


// Path: app/src/main/java/com/philliphsu/clock2/dialogs/RingtonePickerDialog.java
import android.content.DialogInterface;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import com.philliphsu.clock2.R;
import com.philliphsu.clock2.ringtone.playback.RingtoneLoop;

/*
 * Copyright 2017 Phillip Hsu
 *
 * This file is part of ClockPlus.
 *
 * ClockPlus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ClockPlus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ClockPlus.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.philliphsu.clock2.dialogs;



/**
 * Created by Phillip Hsu on 9/3/2016.
 * <p></p>
 * An alternative to the system's ringtone picker dialog. The differences are:
 * (1) this dialog matches the current theme,
 * (2) the selected ringtone URI is delivered via the {@link OnRingtoneSelectedListener
 * OnRingtoneSelectedListener} callback.
 * <p></p>
 * TODO: If a ringtone was playing and the configuration changes, the ringtone is destroyed.
 * Restore the playing ringtone (seamlessly, without the stutter that comes from restarting).
 * Setting setRetainInstance(true) in onCreate() made our app crash (error said attempted to
 * access closed Cursor).
 * We might need to play the ringtone from a Service instead, so we won't have to worry about
 * the ringtone being destroyed on rotation.
 */
public class RingtonePickerDialog extends BaseAlertDialogFragment {
    private static final String TAG = "RingtonePickerDialog";
    private static final String KEY_RINGTONE_URI = "key_ringtone_uri";

    private RingtoneManager mRingtoneManager;
    private OnRingtoneSelectedListener mOnRingtoneSelectedListener;
    private Uri mRingtoneUri;
    private RingtoneLoop mRingtone;