Android Open Source - pandoroid Pandoid Player






From Project

Back to project page pandoroid.

License

The source code is released under:

GNU General Public License

If you think the Android project pandoroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Pandoroid Radio - open source pandora.com client for android
 * Copyright (C) 2011  Andrew Regner <andrew@aregner.com>
 * /*from w  w w  .ja  v  a2  s .c  o m*/
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.aregner.android.pandoid;

import com.aregner.pandora.Song;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class PandoidPlayer extends Activity {

  public static final int REQUIRE_SELECT_STATION = 0x10;
  public static final int REQUIRE_LOGIN_CREDS = 0x20;
  public static final String RATING_BAN = "ban";
  public static final String RATING_LOVE = "love";
  public static final String RATING_NONE = null;

  private static ProgressDialog waiting;
  private PandoraRadioService pandora;
  private SharedPreferences prefs;
  private ImageDownloader imageDownloader = new ImageDownloader();

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player);

    if(PandoraRadioService.getInstance(false) == null) {
      // handle for the preferences for us to use everywhere
      prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

      // look for what we need to continue with pandora auth
      String username = prefs.getString("pandora_username", null);
      String password = prefs.getString("pandora_password", null);

      if(username == null || password == null) {
        // bring them to the login screen so they can enter what we need
        startActivityForResult(new Intent(getApplicationContext(), PandoidLogin.class), REQUIRE_LOGIN_CREDS);
      }
    }
    else {
      pandora = PandoraRadioService.getInstance(false);
      updateForNewSong(pandora.getCurrentSong());
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.player_menu, menu);
    return true;
  }

  @Override
  protected void onStart() {
    super.onStart();
  }

  @Override
  protected void onResume() {
    super.onResume();
    // The activity has become visible (it is now "resumed").
    serviceSetup();
  }
  
  private void serviceSetup() {
    if(pandora == null || !(pandora instanceof PandoraRadioService)) {
      (new InitialSetupTask()).execute();
    }
  }

  protected void updateForNewSong(Song song) {
    TextView top = (TextView) findViewById(R.id.player_topText);
    TextView bottom = (TextView) findViewById(R.id.player_bottomText);
    ImageView image = (ImageView) findViewById(R.id.player_image);

    top.setText(String.format("%s by %s", song.getTitle(), song.getArtist()));
    imageDownloader.download(song.getAlbumCoverUrl(), image);
    bottom.setText(String.format("%s", song.getAlbum()));
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUIRE_SELECT_STATION && resultCode == RESULT_OK) {
      pandora.setCurrentStationId(data.getLongExtra("stationId", -1));
      (new PlayStationTask()).execute();
    }
    else if(requestCode == REQUIRE_LOGIN_CREDS && resultCode == RESULT_OK) {
      serviceSetup();
    }
  }

  public void controlButtonPressed(View button) {
    switch(button.getId()) {

    case R.id.player_ban:
      pandora.rate(RATING_BAN);
      Toast.makeText(getApplicationContext(), getString(R.string.baned_song), Toast.LENGTH_SHORT).show();
      if(prefs.getBoolean("behave_nextOnBan", true)) {
        updateForNewSong(pandora.next());
      }
      break;

    case R.id.player_love:
      pandora.rate(RATING_LOVE);
      Toast.makeText(getApplicationContext(), getString(R.string.loved_song), Toast.LENGTH_SHORT).show();
      break;

    case R.id.player_pause:
      pandora.pause();
      break;

    case R.id.player_next:
      updateForNewSong(pandora.next());
      break;
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.menu_stations:
      startActivityForResult(new Intent(getApplicationContext(), PandoidStationSelect.class), REQUIRE_SELECT_STATION);
      return true;

    case R.id.menu_logout:
      pandora.signOut();
      PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit()
        .putString("pandora_username", null)
        .putString("pandora_password", null)
        .commit();
      startActivityForResult(new Intent(getApplicationContext(), PandoidLogin.class), REQUIRE_LOGIN_CREDS);
      return true;

    case R.id.menu_settings:
      startActivity(new Intent(getApplicationContext(), PandoidSettings.class));
      return true;

    default:
      return super.onOptionsItemSelected(item);
    }
  }

  /** Signs in the user and loads their initial data
   *     -> brings them toward a station               */
  private class InitialSetupTask extends AsyncTask<Void, Void, Boolean> {
    @Override
    protected void onPreExecute() {
      waiting = ProgressDialog.show(PandoidPlayer.this, "",  getString(R.string.signing_in));
    }

    @Override
    protected Boolean doInBackground(Void... arg) {
      PandoraRadioService.createPandoraRadioService(getApplicationContext());
      pandora = PandoraRadioService.getInstance(true);
      
      String username = prefs.getString("pandora_username", null);
      String password = prefs.getString("pandora_password", null);
      
      try {
        pandora.signIn(username, password);
      } catch(Exception ex) {
        ex.printStackTrace();
      }
      return pandora.isAlive();
    }

    @Override
    protected void onPostExecute(Boolean result) {

      dismissWaiting();

      if(result.booleanValue()) {

        if(!pandora.isPlaying()) {

          if(pandora.isPlayable()) {
            // play it or resume playback or something smart like that
            (new PlayStationTask()).execute();
          }
          else {
            // ask them to select a station
            startActivityForResult(new Intent(getApplicationContext(), PandoidStationSelect.class), REQUIRE_SELECT_STATION);
          }
        }
      }
      else {
        // failed to sign in for some reason
        Toast.makeText(getApplicationContext(), getString(R.string.signin_failed), Toast.LENGTH_SHORT).show();
        startActivityForResult(new Intent(getApplicationContext(), PandoidLogin.class), REQUIRE_LOGIN_CREDS);
      }
    }
  }

  /** Prepares a selected station to be played */
  private class PlayStationTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
      waiting = ProgressDialog.show(PandoidPlayer.this, "",  getString(R.string.loading, pandora.getCurrentStation().getName()));
    }

    @Override
    protected Void doInBackground(Void... arg0) {
      pandora.setListener(OnCompletionListener.class, new OnCompletionListener() {
        public void onCompletion(MediaPlayer mp) {
          updateForNewSong(pandora.next());
        }
      });
      pandora.setListener(OnPreparedListener.class, new OnPreparedListener() {
        public void onPrepared(MediaPlayer mp) {
        }
      });
      pandora.prepare();

      return null;
    }

    @Override
    protected void onPostExecute(Void result) {
      updateForNewSong(pandora.play());
      dismissWaiting();
    }
  }

  public static void dismissWaiting() {
    if(waiting != null && waiting.isShowing()) {
      waiting.dismiss();
    }
  }

  @Override
  protected void onPause() {
    super.onPause();
    // Another activity is taking focus (this activity is about to be "paused").
  }

  @Override
  protected void onStop() {
    super.onStop();
  }
}




Java Source Code List

com.aregner.android.pandoid.ImageDownloader.java
com.aregner.android.pandoid.PandoidLogin.java
com.aregner.android.pandoid.PandoidPlayer.java
com.aregner.android.pandoid.PandoidSettings.java
com.aregner.android.pandoid.PandoidStationSelect.java
com.aregner.android.pandoid.PandoraDB.java
com.aregner.android.pandoid.PandoraRadioService.java
com.aregner.pandora.Blowfish.java
com.aregner.pandora.PandoraKeys.java
com.aregner.pandora.PandoraRadio.java
com.aregner.pandora.Song.java
com.aregner.pandora.Station.java
com.aregner.pandora.XmlRpc.java
org.xmlrpc.android.Base64Coder.java
org.xmlrpc.android.IXMLRPCSerializer.java
org.xmlrpc.android.MethodCall.java
org.xmlrpc.android.Tag.java
org.xmlrpc.android.XMLRPCClient.java
org.xmlrpc.android.XMLRPCCommon.java
org.xmlrpc.android.XMLRPCException.java
org.xmlrpc.android.XMLRPCFault.java
org.xmlrpc.android.XMLRPCSerializable.java
org.xmlrpc.android.XMLRPCSerializer.java
org.xmlrpc.android.XMLRPCServer.java