Android Open Source - EnklawaPlayer Download Service






From Project

Back to project page EnklawaPlayer.

License

The source code is released under:

GNU General Public License

If you think the Android project EnklawaPlayer 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

package macbury.pod.services;
/*  w  w  w . j a v a  2 s .com*/
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import java.util.ArrayList;

import macbury.pod.db.models.EpisodeFile;
import macbury.pod.managers.App;
import macbury.pod.managers.download.DownloadEpisode;
import macbury.pod.managers.download.DownloadManager;
import macbury.pod.managers.download.DownloadManagerListener;

public class DownloadService extends Service implements DownloadManagerListener {
  private static final String TAG             = "DownloadService";
  private static final String WAKE_LOCK_TAG   = "DownloadService";
  private static final int NOTIFICATION_ID    = 123;
  private static final String WIFI_LOCK_TAG   = "DownloadService";
  private PowerManager powerManager;
  private PowerManager.WakeLock wakeLock;
  private App app;
  private static DownloadManager downloadManager;
  private NotificationManager mNotificationManager;
  private WifiManager.WifiLock wifiLock;

  public DownloadService() {
  }

  @Override
  public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
  }

  @Override
  public void onCreate() {
    super.onCreate();
    this.wifiLock             = ((WifiManager) getSystemService(Context.WIFI_SERVICE)) .createWifiLock(WifiManager.WIFI_MODE_FULL, WIFI_LOCK_TAG);
    this.mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    this.app                  = App.current();
    this.powerManager         = (PowerManager) getSystemService(POWER_SERVICE);
    this.wakeLock             = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
    this.downloadManager      = new DownloadManager(this, this);
    this.wakeLock.acquire();
    this.wifiLock.acquire();
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    wakeLock.release();
    wifiLock.release();
    downloadManager.cancelAll();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (app.intents.haveCancelExtra(intent)) {
      if (app.intents.haveEpisode(intent)) {
        Log.i(TAG, "Have episode id in intent, Canceling it!");
        downloadManager.cancelEpisodeById(app.intents.getEpisodeId(intent));
      } else {
        Log.i(TAG, "Cancel all downloads!");
        downloadManager.cancelAll();
      }
    } else {
      Log.i(TAG, "Pushing all pending episodes");
      downloadManager.push(new ArrayList<EpisodeFile>(app.db.episodeFiles.pending()));
    }
    return super.onStartCommand(intent, flags, startId);
  }


  private void sendStatusFor(DownloadEpisode download) {
    sendBroadcast(app.intents.downloadStatus(download));
  }

  private void updateNotification(DownloadEpisode download) {
    Notification notification = app.notifications.downloadEpisode(download.getEpisodeFile().episode, download.progress, downloadManager.size());
    startForeground(NOTIFICATION_ID, notification);
  }

  @Override
  public void onDownloadStart(DownloadEpisode download) {
    updateNotification(download);
    sendStatusFor(download);
  }

  @Override
  public void onDownloadProgress(DownloadEpisode download) {
    updateNotification(download);
    sendStatusFor(download);
  }

  @Override
  public void onDownloadFail(DownloadEpisode download, Exception e) {
    updateNotification(download);
    sendStatusFor(download);
  }

  @Override
  public void onDownloadSuccess(final DownloadEpisode download) {
    updateNotification(download);
    sendStatusFor(download);
    App.current().db.queue.createFromEpisode(download.getEpisode());
    Ion.with(App.current()).load(download.getEpisode().image).asBitmap().setCallback(new FutureCallback<Bitmap>() {
      @Override
      public void onCompleted(Exception e, Bitmap result) {
        mNotificationManager.notify(download.getEpisode().id, app.notifications.downloadedEpisode(result, download.getEpisode()));
      }
    });
  }

  @Override
  public void onDownloadManagerFinishedAll() {
    stopSelf();
  }

}




Java Source Code List

macbury.pod.ApplicationTest.java
macbury.pod.activities.DonateActivity.java
macbury.pod.activities.ProgramEpisodesActivity.java
macbury.pod.activities.SettingsActivity.java
macbury.pod.activities.ext.AccentFragmentActivity.java
macbury.pod.activities.main.MainActivityActionBarToggle.java
macbury.pod.activities.main.MainActivity.java
macbury.pod.activities.main.NavigationController.java
macbury.pod.activities.main.NavigationListener.java
macbury.pod.activities.player.PlayerActivity.java
macbury.pod.activities.player.RadioActivity.java
macbury.pod.adapters.EpisodesAdapterListener.java
macbury.pod.adapters.EpisodesAdapter.java
macbury.pod.adapters.ProgramsAdapter.java
macbury.pod.api.APICategory.java
macbury.pod.api.APIEpisode.java
macbury.pod.api.APIProgram.java
macbury.pod.api.APIResponse.java
macbury.pod.api.APIThread.java
macbury.pod.db.DatabaseCRUDListener.java
macbury.pod.db.models.EnqueueEpisode.java
macbury.pod.db.models.EpisodeFile.java
macbury.pod.db.models.Episode.java
macbury.pod.db.models.ForumThread.java
macbury.pod.db.models.Program.java
macbury.pod.db.scopes.AbstractScope.java
macbury.pod.db.scopes.EnqueueEpisodeScope.java
macbury.pod.db.scopes.EpisodeFilesScope.java
macbury.pod.db.scopes.EpisodesScope.java
macbury.pod.db.scopes.ProgramsScope.java
macbury.pod.db.scopes.ThreadScope.java
macbury.pod.dialogs.EpisodeAboutDialog.java
macbury.pod.dialogs.ProgramAboutDialog.java
macbury.pod.extensions.Converter.java
macbury.pod.extensions.DateDeserializer.java
macbury.pod.extensions.SleepTimer.java
macbury.pod.fragments.SettingsFragment.java
macbury.pod.fragments.main.AbstractEpisodesFragment.java
macbury.pod.fragments.main.AllProgramsFragment.java
macbury.pod.fragments.main.EnklawaBaseAbstractListFragment.java
macbury.pod.fragments.main.episodes.DownloadedEpisodesFragment.java
macbury.pod.fragments.main.episodes.NewestEpisodesFragment.java
macbury.pod.fragments.main.episodes.PlaylistFragment.java
macbury.pod.fragments.main.episodes.ProgramEpisodesFragment.java
macbury.pod.fragments.player.ExternalPlayerFragment.java
macbury.pod.fragments.player.PlayerArtworkAndInfoFragment.java
macbury.pod.fragments.player.PlayerControllerFragment.java
macbury.pod.managers.AlarmsManager.java
macbury.pod.managers.App.java
macbury.pod.managers.BroadcastsManager.java
macbury.pod.managers.DatabaseManager.java
macbury.pod.managers.IntentManager.java
macbury.pod.managers.NotificationsManager.java
macbury.pod.managers.ServiceManager.java
macbury.pod.managers.SettingsManager.java
macbury.pod.managers.StorageManager.java
macbury.pod.managers.download.DownloadEpisode.java
macbury.pod.managers.download.DownloadManagerListener.java
macbury.pod.managers.download.DownloadManager.java
macbury.pod.managers.player.PlaybackStatus.java
macbury.pod.managers.player.PlayerManagerListener.java
macbury.pod.managers.player.PlayerManager.java
macbury.pod.managers.player.sources.AbstractMediaSource.java
macbury.pod.managers.player.sources.EpisodeMediaSource.java
macbury.pod.managers.player.sources.RadioMediaSource.java
macbury.pod.navigation_drawer.NavAdapter.java
macbury.pod.navigation_drawer.NavBaseItem.java
macbury.pod.navigation_drawer.NavDivider.java
macbury.pod.navigation_drawer.items.AllProgramsNavItem.java
macbury.pod.navigation_drawer.items.DownloadedEpisodesNavItem.java
macbury.pod.navigation_drawer.items.FavoriteProgramNavItem.java
macbury.pod.navigation_drawer.items.ForumNavItem.java
macbury.pod.navigation_drawer.items.NavItemFragment.java
macbury.pod.navigation_drawer.items.NewestEpisodesNavItem.java
macbury.pod.navigation_drawer.items.PlayQueueNavItem.java
macbury.pod.receivers.BootReceiver.java
macbury.pod.receivers.MediaButtonReceiver.java
macbury.pod.services.DownloadService.java
macbury.pod.services.PlayerService.java
macbury.pod.services.SyncPodService.java
macbury.pod.tutorial.MainActivityTutorial.java
macbury.pod.views.TappableFrameLayout.java