<commit_before>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package ti.modules.titanium.media;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiLifecycle.OnLifecycleEvent;
import org.appcelerator.titanium.TiLifecycle.OnWindowFocusChangedEvent;
import org.appcelerator.titanium.util.TiConvert;

import ti.modules.titanium.filesystem.FileProxy;
import android.app.Activity;
import android.media.AudioManager;

@Kroll.proxy(creatableInModule=MediaModule.class, propertyAccessors={
	TiC.PROPERTY_VOLUME
})
public class AudioPlayerProxy extends KrollProxy
	implements OnLifecycleEvent, OnWindowFocusChangedEvent
{
	private static final String TAG = "AudioPlayerProxy";

	@Kroll.constant public static final int STATE_BUFFERING = TiSound.STATE_BUFFERING;
	@Kroll.constant public static final int STATE_INITIALIZED = TiSound.STATE_INITIALIZED;
	@Kroll.constant public static final int STATE_PAUSED = TiSound.STATE_PAUSED;
	@Kroll.constant public static final int STATE_PLAYING = TiSound.STATE_PLAYING;
	@Kroll.constant public static final int STATE_STARTING = TiSound.STATE_STARTING;
	@Kroll.constant public static final int STATE_STOPPED = TiSound.STATE_STOPPED;
	@Kroll.constant public static final int STATE_STOPPING = TiSound.STATE_STOPPING;
	@Kroll.constant public static final int STATE_WAITING_FOR_DATA = TiSound.STATE_WAITING_FOR_DATA;
	@Kroll.constant public static final int STATE_WAITING_FOR_QUEUE = TiSound.STATE_WAITING_FOR_QUEUE;

	@Kroll.constant public static final int STREAM_ALARM = AudioManager.STREAM_ALARM;
	@Kroll.constant public static final int STREAM_DTMF = AudioManager.STREAM_DTMF;
	@Kroll.constant public static final int STREAM_MUSIC = AudioManager.STREAM_MUSIC;
	@Kroll.constant public static final int STREAM_NOTIFICATION = AudioManager.STREAM_NOTIFICATION;
	@Kroll.constant public static final int STREAM_RING = AudioManager.STREAM_RING;
	@Kroll.constant public static final int STREAM_SYSTEM = AudioManager.STREAM_SYSTEM;
	@Kroll.constant public static final int STREAM_VOICE_CALL = AudioManager.STREAM_VOICE_CALL;

	protected TiSound snd;
	private boolean windowFocused;
	private boolean resumeInOnWindowFocusChanged;

	public AudioPlayerProxy()
	{
		super();

		// TODO - we shouldnt need this as this proxy is created only from the runtime - double check
		// TODO this needs to happen post-set
		//((TiBaseActivity)getActivity()).addOnLifecycleEventListener(this);

		defaultValues.put(TiC.PROPERTY_VOLUME, 1.0f);
		defaultValues.put(TiC.PROPERTY_TIME,0);
		defaultValues.put(TiC.PROPERTY_AUDIO_STREAM_TYPE, AudioManager.STREAM_MUSIC);
	}

	@Override
	protected void initActivity(Activity activity) {
		super.initActivity(activity);
		((TiBaseActivity) getActivity()).addOnLifecycleEventListener(this);
		((TiBaseActivity) getActivity()).addOnWindowFocusChangedEventListener(this);
	}

	@Override
	public void handleCreationDict(KrollDict options) {
		super.handleCreationDict(options);
		if (options.containsKey(TiC.PROPERTY_URL)) {
			setProperty(TiC.PROPERTY_URL, resolveUrl(null, TiConvert.toString(options, TiC.PROPERTY_URL)));
		} else if (options.containsKey(TiC.PROPERTY_SOUND)) {
			FileProxy fp = (FileProxy) options.get(TiC.PROPERTY_SOUND);
			if (fp != null) {
				String url = fp.getNativePath();
				setProperty(TiC.PROPERTY_URL, url);
			}
		}
		if (options.containsKey(TiC.PROPERTY_ALLOW_BACKGROUND)) {
			setProperty(TiC.PROPERTY_ALLOW_BACKGROUND, options.get(TiC.PROPERTY_ALLOW_BACKGROUND));
		}
		if (options.containsKey(TiC.PROPERTY_AUDIO_FOCUS)) {
			boolean audioFocus = TiConvert.toBoolean(options.get(TiC.PROPERTY_AUDIO_FOCUS));
			setProperty(TiC.PROPERTY_AUDIO_FOCUS, audioFocus);
			TiSound.audioFocus = audioFocus;
		}
		Log.i(TAG, "Creating audio player proxy for url: " + TiConvert.toString(getProperty(TiC.PROPERTY_URL)),
			Log.DEBUG_MODE);
	}


	@Kroll.getProperty @Kroll.method
	public String getUrl() {
		return TiConvert.toString(getProperty(TiC.PROPERTY_URL));
	}

	@Kroll.setProperty @Kroll.method
	public void setUrl(String url) {
		if (url != null) {
			setProperty(TiC.PROPERTY_URL, resolveUrl(null, TiConvert.toString(url)));
		}
	}

	@Kroll.getProperty @Kroll.method
	public int getDuration() {
		TiSound s = getSound();
		if (s != null) {
			return s.getDuration();
		}
		return 0;
	}

  @Kroll.method @Kroll.getProperty
	public int getAudioStreamType() {
		return TiConvert.toInt(getProperty(TiC.PROPERTY_AUDIO_STREAM_TYPE));
	}

	@Kroll.method @Kroll.setProperty
	public void setAudioStreamType(int val) {
    setProperty(TiC.PROPERTY_AUDIO_STREAM_TYPE, val);
	}

	@Kroll.getProperty @Kroll.method
	public boolean isPlaying() {
		TiSound s = getSound();
		if (s != null) {
			return s.isPlaying();
		}
		return false;
	}<commit_msg>Update AudioPlayerProxy.java<commit_after>/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2017 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */
package ti.modules.titanium.media;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiBaseActivity;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiLifecycle.OnLifecycleEvent;
import org.appcelerator.titanium.TiLifecycle.OnWindowFocusChangedEvent;
import org.appcelerator.titanium.util.TiConvert;

import ti.modules.titanium.filesystem.FileProxy;
import android.app.Activity;
import android.media.AudioManager;

@Kroll.proxy(creatableInModule=MediaModule.class, propertyAccessors={
	TiC.PROPERTY_VOLUME
})
public class AudioPlayerProxy extends KrollProxy
	implements OnLifecycleEvent, OnWindowFocusChangedEvent
{
	private static final String TAG = "AudioPlayerProxy";

	@Kroll.constant public static final int STATE_BUFFERING = TiSound.STATE_BUFFERING;
	@Kroll.constant public static final int STATE_INITIALIZED = TiSound.STATE_INITIALIZED;
	@Kroll.constant public static final int STATE_PAUSED = TiSound.STATE_PAUSED;
	@Kroll.constant public static final int STATE_PLAYING = TiSound.STATE_PLAYING;
	@Kroll.constant public static final int STATE_STARTING = TiSound.STATE_STARTING;
	@Kroll.constant public static final int STATE_STOPPED = TiSound.STATE_STOPPED;
	@Kroll.constant public static final int STATE_STOPPING = TiSound.STATE_STOPPING;
	@Kroll.constant public static final int STATE_WAITING_FOR_DATA = TiSound.STATE_WAITING_FOR_DATA;
	@Kroll.constant public static final int STATE_WAITING_FOR_QUEUE = TiSound.STATE_WAITING_FOR_QUEUE;

	@Kroll.constant public static final int STREAM_ALARM = AudioManager.STREAM_ALARM;
	@Kroll.constant public static final int STREAM_DTMF = AudioManager.STREAM_DTMF;
	@Kroll.constant public static final int STREAM_MUSIC = AudioManager.STREAM_MUSIC;
	@Kroll.constant public static final int STREAM_NOTIFICATION = AudioManager.STREAM_NOTIFICATION;
	@Kroll.constant public static final int STREAM_RING = AudioManager.STREAM_RING;
	@Kroll.constant public static final int STREAM_SYSTEM = AudioManager.STREAM_SYSTEM;
	@Kroll.constant public static final int STREAM_VOICE_CALL = AudioManager.STREAM_VOICE_CALL;

	protected TiSound snd;
	private boolean windowFocused;
	private boolean resumeInOnWindowFocusChanged;

	public AudioPlayerProxy()
	{
		super();

		// TODO - we shouldnt need this as this proxy is created only from the runtime - double check
		// TODO this needs to happen post-set
		//((TiBaseActivity)getActivity()).addOnLifecycleEventListener(this);

		defaultValues.put(TiC.PROPERTY_VOLUME, 1.0f);
		defaultValues.put(TiC.PROPERTY_TIME,0);
		defaultValues.put(TiC.PROPERTY_AUDIO_STREAM_TYPE, AudioManager.STREAM_MUSIC);
	}

	@Override
	protected void initActivity(Activity activity) {
		super.initActivity(activity);
		((TiBaseActivity) getActivity()).addOnLifecycleEventListener(this);
		((TiBaseActivity) getActivity()).addOnWindowFocusChangedEventListener(this);
	}

	@Override
	public void handleCreationDict(KrollDict options) {
		super.handleCreationDict(options);
		if (options.containsKey(TiC.PROPERTY_URL)) {
			setProperty(TiC.PROPERTY_URL, resolveUrl(null, TiConvert.toString(options, TiC.PROPERTY_URL)));
		} else if (options.containsKey(TiC.PROPERTY_SOUND)) {
			FileProxy fp = (FileProxy) options.get(TiC.PROPERTY_SOUND);
			if (fp != null) {
				String url = fp.getNativePath();
				setProperty(TiC.PROPERTY_URL, url);
			}
		}
		if (options.containsKey(TiC.PROPERTY_ALLOW_BACKGROUND)) {
			setProperty(TiC.PROPERTY_ALLOW_BACKGROUND, options.get(TiC.PROPERTY_ALLOW_BACKGROUND));
		}
		if (options.containsKey(TiC.PROPERTY_AUDIO_FOCUS)) {
			boolean audioFocus = TiConvert.toBoolean(options.get(TiC.PROPERTY_AUDIO_FOCUS));
			setProperty(TiC.PROPERTY_AUDIO_FOCUS, audioFocus);
			TiSound.audioFocus = audioFocus;
		}
		Log.i(TAG, "Creating audio player proxy for url: " + TiConvert.toString(getProperty(TiC.PROPERTY_URL)),
			Log.DEBUG_MODE);
	}


	@Kroll.getProperty @Kroll.method
	public String getUrl() {
		return TiConvert.toString(getProperty(TiC.PROPERTY_URL));
	}

	@Kroll.setProperty @Kroll.method
	public void setUrl(String url) {
		if (url != null) {
			setProperty(TiC.PROPERTY_URL, resolveUrl(null, TiConvert.toString(url)));
		}
	}

	@Kroll.getProperty @Kroll.method
	public int getDuration() {
		TiSound s = getSound();
		if (s != null) {
			return s.getDuration();
		}
		return 0;
	}

	@Kroll.method @Kroll.getProperty
	public int getAudioStreamType() {
		return TiConvert.toInt(getProperty(TiC.PROPERTY_AUDIO_STREAM_TYPE));
	}

	@Kroll.method @Kroll.setProperty
	public void setAudioStreamType(int val) {
		setProperty(TiC.PROPERTY_AUDIO_STREAM_TYPE, val);
	}

	@Kroll.getProperty @Kroll.method
	public boolean isPlaying() {
		TiSound s = getSound();
		if (s != null) {
			return s.isPlaying();
		}
		return false;
	}
{'fraction_non_alphanumeric': 0.09418040737148399, 'fraction_numerical': 0.0023278370514064014, 'mean_word_length': 5.783552631578948, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5046793', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3348, 'n_words': 701}