首页>代码>Android商业项目多功能媒体完整版APP源代码>/telecapoland-jamendo-android-6cd07fb/src/com/teleca/jamendo/activity/HomeActivity.java
/* * Copyright (C) 2009 Teleca Poland Sp. z o.o. <android@teleca.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.teleca.jamendo.activity; import java.util.ArrayList; import java.util.Hashtable; import org.json.JSONException; import com.teleca.jamendo.JamendoApplication; import com.teleca.jamendo.activity.BrowsePlaylistActivity.Mode; import com.teleca.jamendo.adapter.ImageAdapter; import com.teleca.jamendo.adapter.PurpleAdapter; import com.teleca.jamendo.adapter.PurpleEntry; import com.teleca.jamendo.adapter.PurpleListener; import com.teleca.jamendo.adapter.SeparatedListAdapter; import com.teleca.jamendo.api.Album; import com.teleca.jamendo.api.JamendoGet2Api; import com.teleca.jamendo.api.Playlist; import com.teleca.jamendo.api.PlaylistEntry; import com.teleca.jamendo.api.Track; import com.teleca.jamendo.api.WSError; import com.teleca.jamendo.api.impl.JamendoGet2ApiImpl; import com.teleca.jamendo.dialog.AboutDialog; import com.teleca.jamendo.dialog.LoadingDialog; import com.teleca.jamendo.widget.FailureBar; import com.teleca.jamendo.widget.OnAlbumClickListener; import com.teleca.jamendo.widget.ProgressBar; import com.teleca.jamendo.R; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.ListView; import android.widget.Toast; import android.widget.ViewFlipper; import android.widget.AdapterView.OnItemClickListener; /** * Home activity of the jamendo, central navigation place * * @author Lukasz Wisniewski */ public class HomeActivity extends Activity implements OnAlbumClickListener { private ViewFlipper mViewFlipper; private Gallery mGallery; private ProgressBar mProgressBar; private FailureBar mFailureBar; private ListView mHomeListView; private PurpleAdapter mBrowseJamendoPurpleAdapter; private PurpleAdapter mMyLibraryPurpleAdapter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); mHomeListView = (ListView)findViewById(R.id.HomeListView); mGallery = (Gallery)findViewById(R.id.Gallery); mProgressBar = (ProgressBar)findViewById(R.id.ProgressBar); mFailureBar = (FailureBar)findViewById(R.id.FailureBar); mViewFlipper = (ViewFlipper)findViewById(R.id.ViewFlipper); new NewsTask().execute((Void)null); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { // commented out, was causing "Wrong state class -- expecting View State" on view rotation // super.onRestoreInstanceState(savedInstanceState); } @Override public void onAlbumClicked(Album album) { PlayerActivity.launch(this, album); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.home, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onPrepareOptionsMenu(Menu menu) { if(JamendoApplication.getInstance().getPlayerEngineInterface() == null || JamendoApplication.getInstance().getPlayerEngineInterface().getPlaylist() == null){ menu.findItem(R.id.player_menu_item).setVisible(false); } else { menu.findItem(R.id.player_menu_item).setVisible(true); } return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.player_menu_item: PlayerActivity.launch(this, (Playlist)null); break; case R.id.about_menu_item: new AboutDialog(this).show(); break; case R.id.settings_menu_item: SettingsActivity.launch(this); break; default: } return super.onOptionsItemSelected(item); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // support for search key // TODO remove hardcoded '1' value if(keyCode == KeyEvent.KEYCODE_SEARCH){ mHomeListView.setSelection(1); PurpleEntry entry = (PurpleEntry) mHomeListView.getAdapter().getItem(1); entry.getListener().performAction(); } return super.onKeyDown(keyCode, event); } @Override protected void onPause() { super.onPause(); } @Override protected void onResume() { fillHomeListView(); super.onResume(); } /** * Fills ListView with clickable menu items */ private void fillHomeListView(){ mBrowseJamendoPurpleAdapter = new PurpleAdapter(this); mMyLibraryPurpleAdapter = new PurpleAdapter(this); ArrayList<PurpleEntry> browseListEntry = new ArrayList<PurpleEntry>(); ArrayList<PurpleEntry> libraryListEntry = new ArrayList<PurpleEntry>(); // BROWSE JAMENDO browseListEntry.add(new PurpleEntry(R.drawable.list_search, R.string.search, new PurpleListener(){ @Override public void performAction() { SearchActivity.launch(HomeActivity.this); } })); browseListEntry.add(new PurpleEntry(R.drawable.list_radio, R.string.radio, new PurpleListener(){ @Override public void performAction() { RadioActivity.launch(HomeActivity.this); } })); browseListEntry.add(new PurpleEntry(R.drawable.list_top, R.string.most_listened, new PurpleListener(){ @Override public void performAction() { new Top100Task(HomeActivity.this, R.string.loading_top100, R.string.top100_fail).execute(); } })); // MY LIBRARY libraryListEntry.add(new PurpleEntry(R.drawable.list_playlist, R.string.playlists, new PurpleListener(){ @Override public void performAction() { BrowsePlaylistActivity.launch(HomeActivity.this, Mode.Normal); } })); // check if we have personalized client then add starred albums final String userName = PreferenceManager.getDefaultSharedPreferences(this).getString("user_name", null); if(userName != null && userName.length() > 0){ libraryListEntry.add(new PurpleEntry(R.drawable.list_cd, R.string.albums, new PurpleListener(){ @Override public void performAction() { StarredAlbumsActivity.launch(HomeActivity.this, userName); } })); } /* following needs jamendo authorization (not documented yet on the wiki) * listEntry.add(new PurpleEntry(R.drawable.list_mail, "Inbox")); */ // show this list item only if the SD Card is present if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ libraryListEntry.add(new PurpleEntry(R.drawable.list_download, R.string.download, new PurpleListener(){ @Override public void performAction() { DownloadActivity.launch(HomeActivity.this); } })); } // listEntry.add(new PurpleEntry(R.drawable.list_star, R.string.favorites, new PurpleListener(){ // // @Override // public void performAction() { // Playlist playlist = new DatabaseImpl(HomeActivity.this).getFavorites(); // JamendroidApplication.getInstance().getPlayerEngine().openPlaylist(playlist); // PlaylistActivity.launch(HomeActivity.this, true); // } // // })); // attach list data to adapters mBrowseJamendoPurpleAdapter.setList(browseListEntry); mMyLibraryPurpleAdapter.setList(libraryListEntry); // separate adapters on one list SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this); separatedAdapter.addSection(getString(R.string.browse_jamendo), mBrowseJamendoPurpleAdapter); separatedAdapter.addSection(getString(R.string.my_library), mMyLibraryPurpleAdapter); mHomeListView.setAdapter(separatedAdapter); mHomeListView.setOnItemClickListener(mHomeItemClickListener); } /** * Launches menu actions */ private OnItemClickListener mHomeItemClickListener = new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> adapterView, View view, int index, long time) { PurpleListener listener = ((PurpleEntry)adapterView.getAdapter().getItem(index)).getListener(); if(listener != null){ listener.performAction(); } } }; /** * Executes news download, JamendoGet2Api.getPopularAlbumsWeek * * @author Lukasz Wisniewski */ private class NewsTask extends AsyncTask<Void, WSError, Album[]> { @Override public void onPreExecute() { mViewFlipper.setDisplayedChild(0); mProgressBar.setText(R.string.loading_news); super.onPreExecute(); } @Override public Album[] doInBackground(Void... params) { JamendoGet2Api server = new JamendoGet2ApiImpl(); Album[] albums = null; try { albums = server.getPopularAlbumsWeek(); } catch (JSONException e) { e.printStackTrace(); } catch (WSError e){ publishProgress(e); } return albums; } @Override public void onPostExecute(Album[] albums) { if(albums != null && albums.length > 0){ mViewFlipper.setDisplayedChild(1); ImageAdapter albumsAdapter = new ImageAdapter(HomeActivity.this); albumsAdapter.setList(albums); mGallery.setAdapter(albumsAdapter); mGallery.setOnItemClickListener(mGalleryListener); mGallery.setSelection(albums.length/2, true); // animate to center } else { mViewFlipper.setDisplayedChild(2); mFailureBar.setOnRetryListener(new OnClickListener(){ @Override public void onClick(View v) { new NewsTask().execute((Void)null); } }); mFailureBar.setText(R.string.connection_fail); } super.onPostExecute(albums); } @Override protected void onProgressUpdate(WSError... values) { Toast.makeText(HomeActivity.this, values[0].getMessage(), Toast.LENGTH_LONG).show(); super.onProgressUpdate(values); } } /** * Background task fetching top 100 tracks from the remote server * It's sort of ugly circumvention of Get2 API limitations<br> * 1 rss + 2 json requests * * @author Lukasz Wisniewski */ private class Top100Task extends LoadingDialog<Void, Playlist>{ public Top100Task(Activity activity, int loadingMsg, int failMsg) { super(activity, loadingMsg, failMsg); } @Override public Playlist doInBackground(Void... params) { JamendoGet2Api server = new JamendoGet2ApiImpl(); int[] id = null; try { id = server.getTop100Listened(); Album[] albums = server.getAlbumsByTracksId(id); Track[] tracks = server.getTracksByTracksId(id, JamendoApplication.getInstance().getStreamEncoding()); if(albums == null || tracks == null) return null; Hashtable<Integer, PlaylistEntry> hashtable = new Hashtable<Integer, PlaylistEntry>(); for(int i = 0; i < tracks.length; i++){ PlaylistEntry playlistEntry = new PlaylistEntry(); playlistEntry.setAlbum(albums[i]); playlistEntry.setTrack(tracks[i]); hashtable.put(tracks[i].getId(), playlistEntry); } // creating playlist in the correct order Playlist playlist = new Playlist(); for(int i =0; i < id.length; i++){ playlist.addPlaylistEntry(hashtable.get(id[i])); } return playlist; } catch (JSONException e) { e.printStackTrace(); } catch (WSError e) { publishProgress(e); } return null; } @Override public void doStuffWithResult(Playlist playlist) { if(playlist.size() <= 0){ failMsg(); return; } PlayerActivity.launch(HomeActivity.this, playlist); } } private OnItemClickListener mGalleryListener = new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Album album = (Album) adapterView.getItemAtPosition(position); PlayerActivity.launch(HomeActivity.this, album); } }; }
最近下载更多
haozhilang LV9
2023年11月2日
wzm123530 LV4
2023年5月30日
moomin709 LV24
2022年2月28日
木木不爱安妮酱 LV10
2022年1月7日
oopocia LV4
2021年5月26日
香菜 LV1
2021年5月11日
LogCat LV1
2021年3月26日
wcyzm999 LV4
2021年1月13日
123211234567 LV1
2021年1月8日
18781170695 LV6
2020年12月30日
最近浏览更多
微信网友_7041036943331328 LV3
10月8日
2636804923 LV6
6月20日
咩咩咩咩咩 LV2
6月17日
AIWU1234 LV2
6月9日
求学的熊猫 LV11
6月3日
lilitu LV6
5月30日
happySuperman LV2
5月29日
13543528515 LV8
5月21日
阿九11111 LV4
5月21日
MrDuan LV2
5月14日