Java tutorial
/* * 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 net.dian1.player.activity; import java.util.ArrayList; import org.json.JSONException; import net.dian1.player.Dian1Application; import net.dian1.player.adapter.AlbumGridAdapter; import net.dian1.player.api.Album; import net.dian1.player.api.Artist; import net.dian1.player.api.JamendoGet2Api; import net.dian1.player.api.Music; import net.dian1.player.api.WSError; import net.dian1.player.api.impl.JamendoGet2ApiImpl; import net.dian1.player.dialog.AddToPlaylistDialog; import net.dian1.player.dialog.ArtistLoadingDialog; import net.dian1.player.dialog.LoadingDialog; import net.dian1.player.widget.ArtistBar; import net.dian1.player.R; import android.app.Activity; import android.content.Intent; import android.gesture.GestureOverlayView; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.GridView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; /** * Artist View - discography, webpage & donate buttons * * @author Lukasz Wisniewski */ public class ArtistActivity extends BaseActivity { private ArtistBar mArtistBar; private AlbumGridAdapter mAlbumGridAdapter; private GridView mAlbumGridView; private Button mDonateButton; private Button mWebpageButton; private GestureOverlayView mGestureOverlayView; private Artist mArtist; /** * Launch this Activity from the outside * * @param c context from which Activity should be started * @param artistName Artist to be presented */ public static void launch(Activity c, String artistName) { new ArtistLoadingDialog(c, R.string.artist_loading, R.string.artist_fail).execute(artistName); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.artist); mArtistBar = (ArtistBar) findViewById(R.id.ArtistBar); mAlbumGridView = (GridView) findViewById(R.id.AlbumGridView); mArtistBar.setDescription(R.string.discography); mArtist = (Artist) getIntent().getSerializableExtra("artist"); mAlbumGridAdapter = new AlbumGridAdapter(this); mArtistBar.setArtist(mArtist); loadAlbums(); mAlbumGridView.setOnItemClickListener(mOnItemClickListener); mAlbumGridView.setOnItemLongClickListener(mOnItemLongClickListener); mDonateButton = (Button) findViewById(R.id.DonateButton); mDonateButton.setOnClickListener(mDonateClick); mWebpageButton = (Button) findViewById(R.id.WebpageButton); mWebpageButton.setOnClickListener(mWebpageClick); Toast.makeText(ArtistActivity.this, R.string.long_press_playlist, Toast.LENGTH_SHORT).show(); mGestureOverlayView = (GestureOverlayView) findViewById(R.id.gestures); mGestureOverlayView.addOnGesturePerformedListener(Dian1Application.getInstance().getPlayerGestureHandler()); } @Override protected void onResume() { super.onResume(); boolean gesturesEnabled = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("gestures", true); mGestureOverlayView.setEnabled(gesturesEnabled); } @SuppressWarnings("unchecked") private void loadAlbums() { // JamendoGet2Api server = new JamendoGet2ApiImpl(); // Album[] albums = server.searchForAlbumsByArtist(artistName); ArrayList<Album> albums = (ArrayList<Album>) getIntent().getSerializableExtra("albums"); mAlbumGridAdapter.setList(albums); mAlbumGridView.setAdapter(mAlbumGridAdapter); } /** * Album grid OnItemClickListener, launches Album Activity */ private OnItemClickListener mOnItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapter, View view, int position, long time) { Album album = (Album) mAlbumGridAdapter.getItem(position); //AlbumActivity.launch(ArtistActivity.this, album); } }; /** * Long press adds album to the current playlist */ private OnItemLongClickListener mOnItemLongClickListener = new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long time) { Album album = (Album) mAlbumGridView.getAdapter().getItem(position); new AddToPlaylistLoadingDialog(ArtistActivity.this, R.string.adding_to_playlist, R.string.adding_to_playlist_fail).execute(album); return true; } }; /** * Donation link */ private OnClickListener mDonateClick = new OnClickListener() { @Override public void onClick(View v) { Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mArtist.getUrl() + "/donate")); startActivity(myIntent); } }; /** * Artist's webpage */ private OnClickListener mWebpageClick = new OnClickListener() { @Override public void onClick(View v) { Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mArtist.getUrl())); startActivity(myIntent); } }; /** * Loading progress dialog adding asynchronously * tracks from album to playlist * * @author Lukasz Wisniewski */ private class AddToPlaylistLoadingDialog extends LoadingDialog<Album, Music[]> { private Album mAlbum; public AddToPlaylistLoadingDialog(Activity activity, int loadingMsg, int failMsg) { super(activity, loadingMsg, failMsg); } @Override public Music[] doInBackground(Album... params) { mAlbum = params[0]; JamendoGet2Api service = new JamendoGet2ApiImpl(); Music[] musics = null; try { musics = service.getAlbumTracks(mAlbum, Dian1Application.getInstance().getStreamEncoding()); } catch (JSONException e) { e.printStackTrace(); } catch (WSError e) { publishProgress(e); } return musics; } @Override public void doStuffWithResult(Music[] musics) { AddToPlaylistDialog dialog = new AddToPlaylistDialog(ArtistActivity.this); mAlbum.setMusics(musics); dialog.setPlaylistAlbum(mAlbum); dialog.show(); } } }