package cn.edu.nju.software.androidsymplayer;
import cn.edu.nju.software.androidsymplayer.R;
import cn.edu.nju.software.androidsymplayer.musicservice.IProgress;
import cn.edu.nju.software.androidsymplayer.musicservice.MusicService;
import cn.edu.nju.software.androidsymplayer.musicservice.SensorService;
import cn.edu.nju.software.sym.data.Song;
import cn.edu.nju.software.symplayercontroller.SymController;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.webkit.URLUtil;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class player extends Activity implements OnTouchListener,
OnGestureListener {
String t = "Player";
String strMusicFolder = "/sdcard/symPlayer";
private static final int DIALOG_ERROR = 1;
final static String PREF = "NOW_PLAYING";
final static String URI_KEY = "uri";
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private static final int SHUFFLE_LIST_STATE = 1;
private static final int REPEAT_LIST_STATE = 2;
private static final int ONE_LIST_STATE = 3;
private static final int ALBUM_LIST_STATE = 4;
private ListView audioList, menuList, classifyList, artistList, albumList,
playList, latestList, artistSongList, albumSongList,
defaultPlaylist;
private TextView title, artist, album, duration, lyric, searchResult,
resultArtist, resultTitle, resultURL;
private ImageButton forward, backward, pauseorstart, stop, btn_nowPlaying,
btn_playlist, btn_lyric, btn_online, playmode;
private EditText searchTitle, searchArtist;
private Button btn_search, btn_searchPlay, btn_searchDownload;
private SeekBar durationBar;
private SymController controller;
private ContentResolver resolver;
private OnTouchListener listener;
private String errorDes;
private UpdateReceiver doUpdate;
private int barProgress = 0;
private int songTime = 0;
private GestureDetector mGestureDetector;
private ViewFlipper mViewFlipper;
private ScrollView scrollView;
private SharedPreferences sp;
private FrameLayout frameLayout;
private NotificationManager myNotiManager;
private IProgress ProgressService;
private Thread thread;
private boolean isFirst = true;
private int listStatus = REPEAT_LIST_STATE;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
ProgressService = (IProgress) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
ProgressService = null;
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (!bIfExsitMusicFolder(strMusicFolder)) {
Toast.makeText(player.this, "can not create the folder",
Toast.LENGTH_SHORT);
}
initUIComponent();
myNotiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
resolver = getContentResolver();
controller = new SymController(resolver, this);
mGestureDetector = new GestureDetector(this);
mViewFlipper.setLongClickable(true);
mViewFlipper.setOnTouchListener(this);
artistSongList = new ListView(this);
frameLayout.addView(artistSongList);
artistSongList.setVisibility(View.INVISIBLE);
artistSongList.setBackgroundColor(0);
artistSongList.setCacheColorHint(0);
defaultPlaylist = new ListView(this);
frameLayout.addView(defaultPlaylist);
defaultPlaylist.setVisibility(View.INVISIBLE);
defaultPlaylist.setBackgroundColor(0);
defaultPlaylist.setCacheColorHint(0);
// defaultPlaylist = audioList;
albumSongList = new ListView(this);
frameLayout.addView(albumSongList);
albumSongList.setVisibility(View.INVISIBLE);
albumSongList.setBackgroundColor(0);
albumSongList.setCacheColorHint(0);
if (!controller.buildAllList()) {
error("all");
}
updateList();
sp = getSharedPreferences(PREF, 0);
String uriString = sp.getString(URI_KEY, null);
if (uriString != null) {
controller.setReadyToPlaying(Uri.parse(uriString));
controller.setCurrentSong();
controller.setListState(SymController.ALLLIST);
title.setText("" + controller.getCurrentSong().getTitle());
artist.setText("" + controller.getCurrentSong().getArtist());
album.setText("" + controller.getCurrentSong().getAlbum());
duration.setText(" " + controller.getDuration());
controller.setAlbumPlayList(controller.getCurrentSong().getAlbum());
}
menuList.setAdapter(new SimpleAdapter(this, getData(0),
android.R.layout.simple_list_item_2, new String[] { "title",
"info" }, new int[] { android.R.id.text1,
android.R.id.text2 }));
classifyList.setAdapter(new SimpleAdapter(this, getData(1),
android.R.layout.simple_list_item_2, new String[] { "title",
"info" }, new int[] { android.R.id.text1,
android.R.id.text2 }));
playList.setAdapter(new SimpleAdapter(this, getData(2),
android.R.layout.simple_list_item_2, new String[] { "title",
"info" }, new int[] { android.R.id.text1,
android.R.id.text2 }));
menuList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long id) {
// TODO Auto-generated method stub
menuList.setVisibility(View.INVISIBLE);
if (pos == 0) {
audioList.setVisibility(View.VISIBLE);
} else if (pos == 1) {
classifyList.setVisibility(View.VISIBLE);
} else if (pos == 2) {
playList.setVisibility(View.VISIBLE);
} else {
updateLatestList();
latestList.setVisibility(View.VISIBLE);
}
}
});
classifyList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
classifyList.setVisibility(View.INVISIBLE);
if (pos == 0) {
artistList.setVisibility(View.VISIBLE);
} else {
albumList.setVisibility(View.VISIBLE);
}
}
});
playList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
playList.setVisibility(View.INVISIBLE);
defaultPlaylist.setVisibility(View.VISIBLE);
}
});
OnItemClickListener songItemListener = new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
// TODO Auto-generated method stub
playmode.setEnabled(true);
if (parent.equals(audioList) || parent.equals(defaultPlaylist))
controller.setListState(SymController.ALLLIST);
else if (parent.equals(artistSongList))
controller.setListState(SymController.ARTISTLIST);
else if (parent.equals(albumSongList))
controller.setListState(SymController.ALBUMLIST);
else if (parent.equals(latestList))
controller.setListState(SymController.LATESTLIST);
controller.playMusic(pos, id);
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_right_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_right_out);
mViewFlipper.setDisplayedChild(0);
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_left_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_left_out);
btn_playlist.setBackgroundColor(0);
btn_nowPlaying
.setBackgroundResource(R.drawable.img_buttom_bg_press);
if (listStatus == REPEAT_LIST_STATE)
controller.setListState(SymController.ALLLIST);
else if (listStatus == ONE_LIST_STATE) {
controller.setOnePlayList();
controller.setListState(SymController.ONELIST);
} else if (listStatus == ALBUM_LIST_STATE) {
controller.setListState(SymController.ALBUMLIST);
} else {
controller.setListState(SymController.SHUFFLElIST);
}
}
};
audioList.setOnItemClickListener(songItemListener);
defaultPlaylist.setOnItemClickListener(songItemListener);
artistSongList.setOnItemClickListener(songItemListener);
albumSongList.setOnItemClickListener(songItemListener);
latestList.setOnItemClickListener(songItemListener);
artistList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
// Log.d("player",""+artistList.getItemAtPosition(pos).toString().);
HashMap<String, Object> map = (HashMap<String, Object>) artistList
.getItemAtPosition(pos);
String artistname = (String) map.get("ItemTitle");
controller.setArtistPlayList(artistname);
updateArtistList();
artistList.setVisibility(View.INVISIBLE);
artistSongList.setVisibility(View.VISIBLE);
}
});
albumList.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
// Log.d("player",""+artistList.getItemAtPosition(pos).toString().);
String albumname = controller.getAlbumList().get(pos);
controller.setAlbumPlayList(albumname);
updateAlbumList();
albumList.setVisibility(View.INVISIBLE);
albumSongList.setVisibility(View.VISIBLE);
}
});
menuList.setOnTouchListener(this);
audioList.setOnTouchListener(this);
classifyList.setOnTouchListener(this);
playList.setOnTouchListener(this);
latestList.setOnTouchListener(this);
artistList.setOnTouchListener(this);
albumList.setOnTouchListener(this);
scrollView.setOnTouchListener(this);
artistSongList.setOnTouchListener(this);
albumSongList.setOnTouchListener(this);
defaultPlaylist.setOnTouchListener(this);
latestList.setOnTouchListener(this);
listener = new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if (controller.getReadyToPlaying() == null) {
return false;
}
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.setBackgroundResource(R.drawable.img_buttom_bg_press); // pressed
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
arg0.setBackgroundColor(0);
switch (arg0.getId()) {
case R.id.playorpause:
controller.playOrPause();
break;
case R.id.stop:
controller.stop();
break;
case R.id.playforward:
controller.playforward();
break;
case R.id.playbackward:
controller.playbackward();
break;
default:
break;
}
}
return false;
}
/*
* public void onClick(View view) {
*
* if (controller.getReadyToPlaying() == null) {
* return; } switch (view.getId()) { case R.id.playorpause:
* controller.playOrPause(); break; case R.id.stop:
* controller.stop(); break; case R.id.playforward:
* controller.playforward(); break; case R.id.playbackward:
* controller.playbackward(); break; default: break; }
*
* }
*/
};
forward.setOnTouchListener(listener);
backward.setOnTouchListener(listener);
pauseorstart.setOnTouchListener(listener);
stop.setOnTouchListener(listener);
durationBar
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
barProgress = durationBar.getProgress();
controller.setProgress(barProgress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// TODO Auto-generated method stub
}
});
btn_nowPlaying.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.setBackgroundResource(R.drawable.img_buttom_bg_press); // pressed
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
btn_lyric.setBackgroundColor(0);
btn_online.setBackgroundColor(0);
btn_playlist.setBackgroundColor(0);
if (mViewFlipper.getDisplayedChild() != 0) {
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_right_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_right_out);
mViewFlipper.setDisplayedChild(0);
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_left_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_left_out);
}
}
return false;
}
});
btn_playlist.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
menuList.setAdapter(new SimpleAdapter(player.this, getData(0),
android.R.layout.simple_list_item_2, new String[] {
"title", "info" }, new int[] {
android.R.id.text1, android.R.id.text2 }));
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.setBackgroundResource(R.drawable.img_buttom_bg_press); // pressed
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
btn_nowPlaying.setBackgroundColor(0);
btn_online.setBackgroundColor(0);
btn_lyric.setBackgroundColor(0);
int index = mViewFlipper.getDisplayedChild();
if (index > 1) {
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_right_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_right_out);
mViewFlipper.setDisplayedChild(1);
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_left_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_left_out);
index--;
} else if (index < 1) {
mViewFlipper.setDisplayedChild(1);
}
}
return false;
}
});
btn_lyric.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.setBackgroundResource(R.drawable.img_buttom_bg_press); // pressed
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
btn_nowPlaying.setBackgroundColor(0);
btn_online.setBackgroundColor(0);
btn_playlist.setBackgroundColor(0);
int index = mViewFlipper.getDisplayedChild();
if (index > 2) {
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_right_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_right_out);
mViewFlipper.setDisplayedChild(2);
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_left_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_left_out);
index--;
} else if (index < 2) {
mViewFlipper.setDisplayedChild(2);
}
}
return false;
}
});
btn_online.setOnTouchListener(new Button.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.setBackgroundResource(R.drawable.img_buttom_bg_press); // pressed
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
btn_nowPlaying.setBackgroundColor(0);
btn_lyric.setBackgroundColor(0);
btn_playlist.setBackgroundColor(0);
if (mViewFlipper.getDisplayedChild() != 3)
mViewFlipper.setDisplayedChild(3);
}
return false;
}
});
playmode.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
arg0.setBackgroundResource(R.drawable.img_top_bt_bg_press); // pressed
} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
if (listStatus == REPEAT_LIST_STATE) {
arg0.setBackgroundColor(0);
arg0
.setBackgroundResource(R.drawable.icon_playmode_repeat_single);
listStatus = ONE_LIST_STATE;
controller.setOnePlayList();
controller.setListState(SymController.ONELIST);
Toast.makeText(player.this, "", Toast.LENGTH_SHORT)
.show();
} else if (listStatus == ONE_LIST_STATE) {
arg0
.setBackgroundResource(R.drawable.icon_playmode_shuffle);
listStatus = SHUFFLE_LIST_STATE;
controller.setListState(SymController.SHUFFLElIST);
Toast.makeText(player.this, "", Toast.LENGTH_SHORT)
.show();
} else if (listStatus == SHUFFLE_LIST_STATE) {
arg0
.setBackgroundResource(R.drawable.icon_playmode_repeat);
listStatus = ALBUM_LIST_STATE;
controller.setListState(SymController.ALBUMLIST);
Toast.makeText(player.this, "", Toast.LENGTH_SHORT)
.show();
} else {
arg0
.setBackgroundResource(R.drawable.icon_playmode_normal);
listStatus = REPEAT_LIST_STATE;
controller.setListState(SymController.ALLLIST);
Toast.makeText(player.this, "", Toast.LENGTH_SHORT)
.show();
}
}
return false;
}
});
btn_search.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String title = searchTitle.getText().toString();
String artist = searchArtist.getText().toString();
if(title==null || artist==null){
Toast.makeText(player.this, "", Toast.LENGTH_SHORT).show();
return;
}
String s1 = URLEncoder.encode(title, "GB2312");
String s2 = URLEncoder.encode(artist, "GB2312");
String queryString = "http://box.zhangmen.baidu.com/x?op=12&count=1&title="
+ s1 + "$$" + s2 + "$$$$";
URL url = new URL(queryString.replace(" ", "%20"));
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
BaiduMusicHandler handler = new BaiduMusicHandler();
xr.setContentHandler(handler);
xr.parse(new InputSource(url.openStream()));
String musicurl = handler.getMusicURL();
if (musicurl != null) {
searchResult.setText("");
resultArtist.setText("" + artist);
resultTitle.setText("" + title);
resultURL.setText("URL:" + musicurl);
btn_searchPlay.setVisibility(View.VISIBLE);
btn_searchDownload.setVisibility(View.VISIBLE);
} else {
searchResult.setText("");
resultArtist.setText("");
resultTitle.setText("");
resultURL.setText("");
}
} catch (Exception e) {
Log.e("error", e.toString());
}
}
});
btn_searchPlay.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (resultURL.getText().toString() != null) {
controller.setReadyToPlaying(Uri.parse(resultURL.getText()
.toString().substring(4)));
controller.setCurrentSong(resultArtist.getText().toString()
.substring(3), resultTitle.getText().toString()
.substring(3));
mViewFlipper.setDisplayedChild(0);
btn_online.setBackgroundColor(0);
btn_nowPlaying
.setBackgroundResource(R.drawable.img_buttom_bg_press);
controller.setListState(SymController.ONLINElIST);
playmode.setEnabled(false);
controller.play();
} else
Toast.makeText(player.this, "",
Toast.LENGTH_SHORT).show();
}
});
btn_searchDownload.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String strURL = resultURL.getText().toString().substring(4);
getFile(strURL);
}
});
// BarThread.start();
doUpdate = new UpdateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(MusicService.UPDATE_STATUS);
filter.addAction(SensorService.UPDATE_STATUS);
registerReceiver(doUpdate, filter);
startService(new Intent(this, MusicService.class));
startService(new Intent(this, SensorService.class));
this
.bindService(
new Intent(
"cn.edu.nju.software.androidsymplayer.musicservice.MusicService"),
this.serviceConnection, BIND_AUTO_CREATE);
thread = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
barProgress = ProgressService.getProgress();
durationBar.setProgress(barProgress);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mViewFlipper.getDisplayedChild() == 1) {
if (audioList.getVisibility() == View.VISIBLE) {
Log.d("player", "audioList");
audioList.setVisibility(View.INVISIBLE);
menuList.setVisibility(View.VISIBLE);
return true;
} else if (classifyList.getVisibility() == View.VISIBLE) {
classifyList.setVisibility(View.INVISIBLE);
menuList.setVisibility(View.VISIBLE);
return true;
} else if (playList.getVisibility() == View.VISIBLE) {
playList.setVisibility(View.INVISIBLE);
menuList.setVisibility(View.VISIBLE);
return true;
} else if (latestList.getVisibility() == View.VISIBLE) {
latestList.setVisibility(View.INVISIBLE);
menuList.setVisibility(View.VISIBLE);
return true;
} else if (artistList.getVisibility() == View.VISIBLE) {
artistList.setVisibility(View.INVISIBLE);
classifyList.setVisibility(View.VISIBLE);
return true;
} else if (albumList.getVisibility() == View.VISIBLE) {
albumList.setVisibility(View.INVISIBLE);
classifyList.setVisibility(View.VISIBLE);
return true;
} else if (artistSongList.getVisibility() == View.VISIBLE) {
artistSongList.setVisibility(View.INVISIBLE);
artistList.setVisibility(View.VISIBLE);
return true;
} else if (albumSongList.getVisibility() == View.VISIBLE) {
albumSongList.setVisibility(View.INVISIBLE);
albumList.setVisibility(View.VISIBLE);
return true;
} else if (defaultPlaylist.getVisibility() == View.VISIBLE) {
Log.d("player", "defaultPlist");
defaultPlaylist.setVisibility(View.INVISIBLE);
playList.setVisibility(View.VISIBLE);
return true;
} else if (latestList.getVisibility() == View.VISIBLE) {
latestList.setVisibility(View.INVISIBLE);
menuList.setVisibility(View.VISIBLE);
return true;
} else {
return super.onKeyDown(keyCode, event);
}
} else {
return super.onKeyDown(keyCode, event);
}
} else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
mViewFlipper.setDisplayedChild(3);
btn_nowPlaying.setBackgroundColor(0);
btn_lyric.setBackgroundColor(0);
btn_playlist.setBackgroundColor(0);
btn_online.setBackgroundResource(R.drawable.img_buttom_bg_press);
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
private List<HashMap<String, Object>> getData(int type) {
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
switch (type) {
case 0:
map = new HashMap<String, Object>();
map.put("title", "");
map.put("info", "" + controller.getAllList().getPlaylist().size()
+ "");
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "");
map.put("info", "");
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "");
map.put("info", "1");
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "");
map.put("info", ""
+ controller.getLatestPlayList().getPlaylist().size()
+ "");
list.add(map);
break;
case 1:
map = new HashMap<String, Object>();
map.put("title", "");
map.put("info", "" + controller.getArtistList().size() + "");
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "");
map.put("info", "" + controller.getAlbumList().size() + "");
list.add(map);
break;
case 2:
map = new HashMap<String, Object>();
map.put("title", "");
map.put("info", "" + controller.getAllList().getPlaylist().size()
+ "");
list.add(map);
break;
default:
break;
}
return list;
}
private void initUIComponent() {
// TODO Auto-generated method stub
title = (TextView) findViewById(R.id.songtitle);
artist = (TextView) findViewById(R.id.songartist);
album = (TextView) findViewById(R.id.songalbum);
duration = (TextView) findViewById(R.id.songduration);
lyric = (TextView) findViewById(R.id.lyric);
forward = (ImageButton) findViewById(R.id.playforward);
backward = (ImageButton) findViewById(R.id.playbackward);
pauseorstart = (ImageButton) findViewById(R.id.playorpause);
stop = (ImageButton) findViewById(R.id.stop);
durationBar = (SeekBar) findViewById(R.id.progress);
audioList = (ListView) findViewById(R.id.lst_audio);
scrollView = (ScrollView) findViewById(R.id.scroll);
menuList = (ListView) findViewById(R.id.lst_menu);
classifyList = (ListView) findViewById(R.id.lst_clssify);
artistList = (ListView) findViewById(R.id.lst_artist);
albumList = (ListView) findViewById(R.id.lst_album);
playList = (ListView) findViewById(R.id.lst_play);
latestList = (ListView) findViewById(R.id.lst_latest);
frameLayout = (FrameLayout) findViewById(R.id.playlistView);
btn_nowPlaying = (ImageButton) findViewById(R.id.id_nowPlaying);
btn_playlist = (ImageButton) findViewById(R.id.id_playlist);
btn_lyric = (ImageButton) findViewById(R.id.id_lyric);
btn_online = (ImageButton) findViewById(R.id.id_oline);
mViewFlipper = (ViewFlipper) findViewById(R.id.details);
playmode = (ImageButton) findViewById(R.id.playmode);
btn_search = (Button) findViewById(R.id.searchButton);
searchTitle = (EditText) findViewById(R.id.searchTitle);
searchArtist = (EditText) findViewById(R.id.searchArtist);
searchResult = (TextView) findViewById(R.id.searchResult);
resultArtist = (TextView) findViewById(R.id.resultArtist);
resultTitle = (TextView) findViewById(R.id.resultTitle);
resultURL = (TextView) findViewById(R.id.resultURL);
btn_searchPlay = (Button) findViewById(R.id.resultPlay);
btn_searchDownload = (Button) findViewById(R.id.resultDownload);
}
public void error(String errorDes) {
// TODO Auto-generated method stub
this.errorDes = errorDes;
showDialog(DIALOG_ERROR);
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_ERROR:
((AlertDialog) dialog).setMessage(errorDes);
default:
return;
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ERROR:
return (new AlertDialog.Builder(this).setIcon(
android.R.drawable.ic_dialog_info).setTitle("")
.setMessage(errorDes).setPositiveButton("", null)
.create());
default:
return null;
}
}
public void updateArtistList() {
ArrayAdapter aritistAdapter = new MyListAdapter(this,
R.layout.listview, controller.getArtistPlayList().getPlaylist());
artistSongList.setAdapter(aritistAdapter);
}
public void updateAlbumList() {
ArrayAdapter albumAdapter = new MyListAdapter(this, R.layout.listview,
controller.getAlbumPlayList().getPlaylist());
albumSongList.setAdapter(albumAdapter);
}
public void updateLatestList() {
ArrayAdapter latestAdapter = new MyListAdapter(this, R.layout.listview,
controller.getLatestPlayList().getPlaylist());
latestList.setAdapter(latestAdapter);
}
public void updateList() {
ArrayAdapter allAdapter = new MyListAdapter(this, R.layout.listview,
controller.getAllList().getPlaylist());
ArrayList<HashMap<String, Object>> artistListItem = new ArrayList<HashMap<String, Object>>();
ArrayList<HashMap<String, Object>> albumListItem = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < controller.getArtistList().size(); i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemTitle", controller.getArtistList().get(i));
map.put("ItmeNumber", ""
+ controller.getArtistSongSize(controller.getArtistList()
.get(i)) + "");
artistListItem.add(map);
}
SimpleAdapter artistListItemAdapter = new SimpleAdapter(this,
artistListItem,//
android.R.layout.simple_list_item_2, new String[] {
"ItemTitle", "ItmeNumber" }, new int[] {
android.R.id.text1, android.R.id.text2 });
ArrayAdapter albumListItemAdapter = new ArtistListAdapter(this,
R.layout.artistlist, controller.getAlbumList());
albumList.setAdapter(albumListItemAdapter);
artistList.setAdapter(artistListItemAdapter);
audioList.setAdapter(allAdapter);
defaultPlaylist.setAdapter(allAdapter);
}
public class MyListAdapter extends ArrayAdapter {
ArrayList<Song> info;
public MyListAdapter(Context context, int layout, ArrayList<?> arraylist) {
super(context, layout, arraylist);
this.info = (ArrayList<Song>) arraylist;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = (View) convertView;
ViewWrapperList wrapper = null;
if (view == null) {
LayoutInflater inflater = getLayoutInflater();
view = inflater.inflate(R.layout.listview, parent, false);
wrapper = new ViewWrapperList(view);
view.setTag(wrapper);
} else {
wrapper = (ViewWrapperList) view.getTag();
}
Bitmap b = controller.getArt(controller.getAllList().getPlaylist()
.indexOf(info.get(position)), info.get(position).getID());
if (b == null) {
wrapper.getImage().setBackgroundResource(R.drawable.unknow);
} else {
BitmapDrawable background = new BitmapDrawable(b);
wrapper.getImage().setBackgroundDrawable(background);
}
wrapper.getText1().setText("" + info.get(position).getTitle());
wrapper.getText2().setText("" + info.get(position).getArtist());
wrapper.getText3().setText("" + info.get(position).getAlbum());
return view;
}
}
public class ArtistListAdapter extends ArrayAdapter {
ArrayList<String> info;
public ArtistListAdapter(Context context, int layout,
ArrayList<?> arraylist) {
super(context, layout, arraylist);
this.info = (ArrayList<String>) arraylist;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = (View) convertView;
ArtistListWrapper wrapper = null;
if (view == null) {
LayoutInflater inflater = getLayoutInflater();
view = inflater.inflate(R.layout.artistlist, parent, false);
wrapper = new ArtistListWrapper(view);
view.setTag(wrapper);
} else {
wrapper = (ArtistListWrapper) view.getTag();
}
Bitmap b = controller.getArt(info.get(position));
if (b == null) {
wrapper.getImage().setBackgroundResource(R.drawable.unknow);
} else {
BitmapDrawable background = new BitmapDrawable(b);
wrapper.getImage().setBackgroundDrawable(background);
}
wrapper.getText1().setText(info.get(position));
wrapper.getText2().setText(
"" + controller.getAlbumSongSize(info.get(position))
+ "");
return view;
}
}
/* */
@Override
public void onDestroy() {
if (controller.getStatus() == MusicService.STATUS_STOPPED) {
stopService(new Intent(this, MusicService.class));
}
unregisterReceiver(doUpdate);
// ===================
if (controller.getListState() != SymController.ONLINElIST) {
SharedPreferences.Editor editor = sp.edit();
editor.putString(URI_KEY, controller.getCurrentSong().getUri()
.toString());
editor.commit();
}
// ===================
// this.unbindService(serviceConnection);
super.onDestroy();
this.unbindService(serviceConnection);
}
public class UpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
controller.setStatus(intent.getIntExtra("status", -1));
switch (controller.getStatus()) {
case MusicService.STATUS_PLAYING:
pauseorstart.setImageResource(R.drawable.img_playback_bt_pause);
updateTrackInfo();
setNotiType(R.drawable.icon_notification_play, ""
+ controller.getCurrentSong().getTitle());
controller.setAlbumPlayList(controller.getCurrentSong()
.getAlbum());
updateArt();
songTime = ProgressService.getDuration();
durationBar.setMax(songTime);
if (isFirst) {
thread.start();
isFirst = false;
}
break;
case MusicService.STATUS_PAUSED:
pauseorstart.setImageResource(R.drawable.img_playback_bt_play);
setNotiType(R.drawable.icon_notification_pause, ":"
+ controller.getCurrentSong().getTitle());
break;
case MusicService.STATUS_STOPPED:
pauseorstart.setImageResource(R.drawable.img_playback_bt_play);
myNotiManager.cancelAll();
break;
/*
* case MusicService.UPDATE_BAR: barProgress =
* intent.getIntExtra("barProgress", -1); updateBar(); break;
*/
case MusicService.STATUS_REQ_NEXT:
controller.playforward();
updateTrackInfo();
updateArt();
break;
case MusicService.UPDATE_LYRIC:
updateLyric();
break;
case SensorService.SENSOR_BACKWARD:
controller.playbackward();
updateTrackInfo();
updateArt();
// Log.d("123", "debug01");
break;
case SensorService.SENSOR_FORWARD:
controller.playforward();
updateTrackInfo();
updateArt();
// Log.d("123", "debug02");
break;
/*
* case MusicService.UPDATE_BAR:
* barProgress=intent.getIntExtra("barProgress", -1); updateBar();
* break;
*/
default:
break;
}
}
private void updateLyric() {
// TODO Auto-generated method stub
if (controller.getCurrentSong() != null
&& controller.getCurrentSong().getLyric() != null)
lyric
.setText(controller.getCurrentSong().getLyric()
.getLyric());
}
/* */
public void updateTrackInfo() {
if (controller.getListState() != SymController.ONLINElIST) {
title.setText("" + controller.getCurrentSong().getTitle());
artist.setText("" + controller.getCurrentSong().getArtist());
album.setText("" + controller.getCurrentSong().getAlbum());
duration.setText(" " + controller.getDuration());
} else {
title.setText("" + searchTitle.getText().toString());
artist.setText("" + searchArtist.getText().toString());
album.setText("");
int time = ProgressService.getDuration();
int minutes = time / 1000 / 60;
int seconds = time / 1000 % 60;
duration.setText(" " + minutes + ":" + seconds);
}
}
private void setNotiType(int iconId, String text) {
/*
* IntentNotification Activity
*/
Intent notifyIntent = new Intent(player.this, NotifyActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent appIntent = PendingIntent.getActivity(player.this, 0,
notifyIntent, 0);
Notification myNoti = new Notification();
myNoti.icon = iconId;
myNoti.tickerText = text;
myNoti.defaults = Notification.DEFAULT_SOUND;
myNoti.setLatestEventInfo(player.this, "simple Player", text,
appIntent);
myNotiManager.notify(0, myNoti);
}
public void updateArt() {
Bitmap b = controller.getArt(controller.getAllList().getPlaylist()
.indexOf(controller.getCurrentSong()), controller
.getCurrentSong().getID());
if (b == null) {
mViewFlipper.setBackgroundResource(R.drawable.bg);
} else {
BitmapDrawable background = new BitmapDrawable(b);
mViewFlipper.setBackgroundDrawable(background);
}
}
/*
* public void updateBar(){ durationBar.setMax(controller.getTime());
* durationBar.setProgress(barProgress); }
*/
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
Log.d("hello", "...OnFling");
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {// move to
// left
int index = mViewFlipper.getDisplayedChild();
mViewFlipper.showNext();
switch (index) {
case 0:
btn_nowPlaying.setBackgroundColor(0);
btn_playlist
.setBackgroundResource(R.drawable.img_buttom_bg_press);
menuList.setAdapter(new SimpleAdapter(this, getData(0),
android.R.layout.simple_list_item_2, new String[] {
"title", "info" }, new int[] {
android.R.id.text1, android.R.id.text2 }));
break;
case 1:
btn_playlist.setBackgroundColor(0);
btn_lyric.setBackgroundResource(R.drawable.img_buttom_bg_press);
break;
case 2:
btn_lyric.setBackgroundColor(0);
btn_online
.setBackgroundResource(R.drawable.img_buttom_bg_press);
break;
case 3:
btn_online.setBackgroundColor(0);
btn_nowPlaying
.setBackgroundResource(R.drawable.img_buttom_bg_press);
break;
}
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
int index = mViewFlipper.getDisplayedChild();
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_right_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_right_out);
mViewFlipper.showPrevious();
mViewFlipper.setInAnimation(getApplicationContext(),
R.anim.push_left_in);
mViewFlipper.setOutAnimation(getApplicationContext(),
R.anim.push_left_out);
switch (index) {
case 0:
btn_nowPlaying.setBackgroundColor(0);
btn_online
.setBackgroundResource(R.drawable.img_buttom_bg_press);
break;
case 1:
btn_playlist.setBackgroundColor(0);
btn_nowPlaying
.setBackgroundResource(R.drawable.img_buttom_bg_press);
break;
case 2:
btn_lyric.setBackgroundColor(0);
btn_playlist
.setBackgroundResource(R.drawable.img_buttom_bg_press);
break;
case 3:
btn_online.setBackgroundColor(0);
btn_lyric.setBackgroundResource(R.drawable.img_buttom_bg_press);
break;
}
} else {
return false;
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onStop() {
super.onStop();
}
public boolean bIfExsitMusicFolder(String strFolder) {
boolean bReturn = false;
File f = new File(strFolder);
if (!f.exists()) {
if (f.mkdirs()) {
bReturn = true;
} else {
bReturn = false;
}
} else {
bReturn = true;
}
return bReturn;
}
public void getFile(final String strPath) {
Toast.makeText(player.this, "~", Toast.LENGTH_LONG).show();
Runnable r = new Runnable() {
public void run() {
try {
getDataSource(strPath);
} catch (Exception e) {
Log.e("wrong", e.getMessage(), e);
}
}
};
new Thread(r).start();
}
public void getDataSource(String strPath) throws Exception {
if (!URLUtil.isNetworkUrl(strPath)) {
Toast.makeText(player.this, "URL", Toast.LENGTH_SHORT).show();
} else {
URL myURL = new URL(strPath);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
if (is == null) {
throw new RuntimeException("stream is null");
}
File myTempFile = new File("/sdcard/symPlayer", resultTitle
.getText().toString().substring(3)
+ ".mp3");
FileOutputStream fos = new FileOutputStream(myTempFile);
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0) {
break;
}
fos.write(buf, 0, numread);
} while (true);
try {
is.close();
} catch (Exception ex) {
Log.e("wrong", "error: " + ex.getMessage(), ex);
}
}
}
}
|