OSDN Git Service

Fix Issue 5547: Apollo will not play MP3 files when using Root Explorer
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / activities / PlayExternal.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.andrew.apollo.activities;
18
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;
32
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;
37
38 import java.io.File;
39 import java.net.URLDecoder;
40
41 /**
42  * An activity that lets external browsers launching music inside Apollo
43  */
44 public class PlayExternal extends FragmentActivity
45     implements ServiceConnection, DialogInterface.OnCancelListener {
46
47     private static final String TAG = "PlayExternal";
48
49     private ServiceToken mToken;
50     private Uri mUri;
51
52     @Override
53     public void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55
56         // Get the external file to play
57         Intent intent = getIntent();
58         if (intent == null) {
59             finish();
60             return;
61         }
62         mUri = intent.getData();
63         if (mUri == null) {
64             finish();
65             return;
66         }
67     }
68
69     @Override
70     public void onServiceConnected(ComponentName name, IBinder obj) {
71         MusicUtils.mService = IApolloService.Stub.asInterface(obj);
72         play(this.mUri);
73     }
74
75     @Override
76     public void onServiceDisconnected(ComponentName name) {
77         MusicUtils.mService = null;
78     }
79
80     @Override
81     protected void onStart() {
82         // Bind to Service
83         mToken = MusicUtils.bindToService(this, this);
84         super.onStart();
85     }
86
87     @Override
88     protected void onStop() {
89         // Unbind
90         if (MusicUtils.mService != null)
91             MusicUtils.unbindFromService(mToken);
92         super.onStop();
93     }
94
95     private void play(Uri uri) {
96         try {
97             final String file = URLDecoder.decode( uri.toString(), "UTF-8");
98             final String name = new File(file).getName();
99
100             // Try to resolve the file to a media id
101             final long id = MusicUtils.mService.getIdFromPath(file);
102             if( id == -1 ) {
103                 // Open the stream, But we will not have album information
104                 openFile(file);
105             }
106             else {
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));
112
113                 DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
114                     @Override
115                     public void onClick(DialogInterface dialog, int which) {
116                         try {
117                             switch (which) {
118                                 case DialogInterface.BUTTON_POSITIVE:
119                                     playOrEnqueuFile(file, id, false);
120                                     break;
121
122                                 case DialogInterface.BUTTON_NEUTRAL:
123                                     playOrEnqueuFile(file, id, true);
124                                     break;
125
126                                 case DialogInterface.BUTTON_NEGATIVE:
127                                     break;
128
129                                 default:
130                                     break;
131                             }
132                         } finally {
133                             finish();
134                         }
135                     }
136                 };
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);
140
141                 Dialog dialog = builder.create();
142                 dialog.setOnCancelListener(this);
143                 dialog.show();
144             }
145
146         } catch (Exception e) {
147             Toast.makeText(
148                     getApplicationContext(),
149                     R.string.play_external_error,
150                     Toast.LENGTH_SHORT);
151             Log.e(TAG, String.format("Failed to play external file: ", uri.toString()), e);
152             try {
153                 Thread.sleep(1000L);
154             }catch (Exception e2) {}
155             finish();
156         }
157
158     }
159
160     @Override
161     public void onCancel(DialogInterface dialog) {
162         finish();
163     }
164
165     private void playOrEnqueuFile(String file, long id, boolean enqueue) {
166         final long[] list = new long[] {id};
167         if (!enqueue) {
168             //Remove the actual queue
169             MusicUtils.removeAllTracks();
170             MusicUtils.playAll(getApplicationContext(), list, 0);
171         }
172         else {
173             MusicUtils.addToCurrentPlaylist(getApplicationContext(), list);
174         }
175
176         // Show now playing
177         Intent intent = new Intent(this, AudioPlayerHolder.class);
178         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
179         startActivity(intent);
180     }
181
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();
187
188         // Show now playing
189         Intent nowPlayingIntent = new Intent(this, AudioPlayerHolder.class);
190         startActivity(nowPlayingIntent);
191     }
192
193 }