2 * Copyright (C) 2012 The CyanogenMod Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.andrew.apollo.activities;
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.ComponentName;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.ServiceConnection;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.os.IBinder;
28 import android.os.RemoteException;
29 import android.support.v4.app.FragmentActivity;
30 import android.util.Log;
31 import android.widget.Toast;
33 import com.andrew.apollo.IApolloService;
34 import com.andrew.apollo.R;
35 import com.andrew.apollo.service.ServiceToken;
36 import com.andrew.apollo.utils.MusicUtils;
39 import java.net.URLDecoder;
42 * An activity that lets external browsers launching music inside Apollo
44 public class PlayExternal extends FragmentActivity
45 implements ServiceConnection, DialogInterface.OnCancelListener {
47 private static final String TAG = "PlayExternal";
49 private ServiceToken mToken;
53 public void onCreate(Bundle savedInstanceState) {
54 super.onCreate(savedInstanceState);
56 // Get the external file to play
57 Intent intent = getIntent();
62 mUri = intent.getData();
70 public void onServiceConnected(ComponentName name, IBinder obj) {
71 MusicUtils.mService = IApolloService.Stub.asInterface(obj);
76 public void onServiceDisconnected(ComponentName name) {
77 MusicUtils.mService = null;
81 protected void onStart() {
83 mToken = MusicUtils.bindToService(this, this);
88 protected void onStop() {
90 if (MusicUtils.mService != null)
91 MusicUtils.unbindFromService(mToken);
95 private void play(Uri uri) {
97 final String file = URLDecoder.decode( uri.toString(), "UTF-8");
98 final String name = new File(file).getName();
100 // Try to resolve the file to a media id
101 final long id = MusicUtils.mService.getIdFromPath(file);
103 // Open the stream, But we will not have album information
107 // Show a dialog asking the user for play or queue the song
108 AlertDialog.Builder builder =
109 new AlertDialog.Builder(this, R.style.Theme_Light_Translucent_Dialog);
110 builder.setTitle(R.string.app_name);
111 builder.setMessage(getString(R.string.play_external_question_msg, name));
113 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
115 public void onClick(DialogInterface dialog, int which) {
118 case DialogInterface.BUTTON_POSITIVE:
119 playOrEnqueuFile(file, id, false);
122 case DialogInterface.BUTTON_NEUTRAL:
123 playOrEnqueuFile(file, id, true);
126 case DialogInterface.BUTTON_NEGATIVE:
137 builder.setPositiveButton(R.string.play_external_question_button_play, listener);
138 builder.setNeutralButton(R.string.play_external_question_button_queue, listener);
139 builder.setNegativeButton(R.string.play_external_question_button_cancel, listener);
141 Dialog dialog = builder.create();
142 dialog.setOnCancelListener(this);
146 } catch (Exception e) {
148 getApplicationContext(),
149 R.string.play_external_error,
151 Log.e(TAG, String.format("Failed to play external file: ", uri.toString()), e);
154 }catch (Exception e2) {}
161 public void onCancel(DialogInterface dialog) {
165 private void playOrEnqueuFile(String file, long id, boolean enqueue) {
166 final long[] list = new long[] {id};
168 //Remove the actual queue
169 MusicUtils.removeAllTracks();
170 MusicUtils.playAll(getApplicationContext(), list, 0);
173 MusicUtils.addToCurrentPlaylist(getApplicationContext(), list);
177 Intent intent = new Intent(this, AudioPlayerHolder.class);
178 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
179 startActivity(intent);
182 private void openFile(String file) throws RemoteException {
183 // Stop, load and play
184 MusicUtils.mService.stop();
185 MusicUtils.mService.openFile(file);
186 MusicUtils.mService.play();
189 Intent nowPlayingIntent = new Intent(this, AudioPlayerHolder.class);
190 startActivity(nowPlayingIntent);