Android Open Source - EnklawaPlayer Sync Pod 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 ww .  ja v  a  2  s  .co m
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import java.util.ArrayList;
import java.util.List;

import macbury.pod.api.APIProgram;
import macbury.pod.api.APIResponse;
import macbury.pod.api.APIThread;
import macbury.pod.db.DatabaseCRUDListener;
import macbury.pod.db.models.Episode;
import macbury.pod.db.models.EpisodeFile;
import macbury.pod.db.models.ForumThread;
import macbury.pod.db.models.Program;
import macbury.pod.db.scopes.EpisodesScope;
import macbury.pod.db.scopes.ProgramsScope;
import macbury.pod.db.scopes.ThreadScope;
import macbury.pod.managers.App;

public class SyncPodService extends Service implements FutureCallback<APIResponse> {
  private static final String TAG = "SyncPodService";
  private static final String WAKE_LOCK_TAG = "SyncPodWakeLock";
  private static final int SYNC_POD_NOTIFICATION_ID = 666;
  private App app;
  private PowerManager powerManager;
  private PowerManager.WakeLock wakeLock;

  private static boolean running = false;

  public SyncPodService() {
  }

  public static boolean isRunning() {
    return running;
  }

  @Override
  public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "Destroy");
    wakeLock.release();
    Ion.getDefault(this).cancelAll(this);
    running = false;
    app.broadcasts.podSync();
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Log.d(TAG, "Create");
    running           = true;
    this.app          = (App)getApplication();
    this.powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    this.wakeLock     = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG);
    this.wakeLock.acquire();
    this.findOldEpisodesAndCleanThem();
    app.broadcasts.podSync();
    syncPod();
  }

  private void findOldEpisodesAndCleanThem() {
    List<EpisodeFile> episodes = app.db.episodeFiles.downloaded();
    for (EpisodeFile episodeFile : episodes) {
      if (episodeFile.isOld()) {
        Log.d(TAG, "Removing old episode: "+ episodeFile.episode.name);
        app.db.episodeFiles.destroy(episodeFile);
      }
    }
  }

  private void syncPod() {
    //startForeground(SYNC_POD_NOTIFICATION_ID, this.app.notifications.syncPod());
    Log.i(TAG, "Syncing pod: "+app.settings.getApiEndpoint());
    Ion.with(this).load(app.settings.getApiEndpoint()).noCache().as(new TypeToken<APIResponse>() {}).setCallback(this);
  }

  @Override
  public void onCompleted(Exception e, APIResponse result) {
    if (e == null) {
      Log.d(TAG, "Downloaded pod data!");
      App.current().settings.setRadioURI(result.radio);
      SyncApiResponseWithDB syncDB = new SyncApiResponseWithDB();
      syncDB.execute(result);
    } else {
      Log.e(TAG, "Error while downloading pod data!");
      e.printStackTrace();
      showErrorSyncNotification(e);
      stopSelf();
    }
  }

  private void showErrorSyncNotification(Exception e) {
    //app.notifications.manager.notify(1, app.notifications.syncPodError(e));
  }

  private void showNotificationsForNewEpisodesOrDownloadThem(ArrayList<Episode> newEpisodes) {
    for(Episode episode : newEpisodes) {
      if (episode.program.isFavorite()) {
        app.db.episodeFiles.createFromEpisode(episode);// TODO check if can do
        Log.i(TAG, "Show notification!!!!");
      }
      Log.i(TAG, "New episode: " + episode.name);
    }
    // else show summary
    // start download service
    app.services.downloadPendingEpisodes();
  }

  private class SyncApiResponseWithDB extends AsyncTask<APIResponse, Integer, Boolean> implements DatabaseCRUDListener<Episode> {
    private ArrayList<Episode> newEpisodes;


    private void syncProgramsAndEpisodes(ArrayList<APIProgram> resultPrograms) {
      ProgramsScope programs = app.db.programs;
      EpisodesScope episodes = app.db.episodes;
      episodes.addListener(this);
      for (APIProgram apiProgram : resultPrograms) {
        if (programs.updateFromApi(apiProgram)) {
          Program program = programs.find(apiProgram);
          Log.v(TAG, "Program: " +program.name);
          for(Episode episode : episodes.findEpisodesByApiProgramAndReturnNew(program, apiProgram.episodes)) {
            episode.program = program;
            if (episodes.update(episode)) {
              Log.v(TAG, "Episode: " + episode.name);
            } else {
              Log.e(TAG, "Could not save episode: " + episode.name);
            }
          }
        } else {
          Log.e(TAG, "Could not save program: " + apiProgram.name);
        }
      }

      episodes.removeListener(this);
    }

    private void syncThreads(ArrayList<APIThread> resultThreads) {
      ThreadScope threads = app.db.threads;
      for (APIThread apiThread : resultThreads) {
        ForumThread thread = threads.find(apiThread);
        if (thread == null) {
          thread = threads.buildFromApi(apiThread);
          threads.update(thread);
        }
      }
    }

    @Override
    protected Boolean doInBackground(APIResponse... params) {
      newEpisodes            = new ArrayList<Episode>();
      APIResponse result     = params[0];
      //syncThreads(result.forum);
      syncProgramsAndEpisodes(result.programs);

      return true;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
      super.onPostExecute(aBoolean);
      SyncPodService.this.showNotificationsForNewEpisodesOrDownloadThem(newEpisodes);
      stopSelf();
    }

    @Override
    public void afterCreate(Episode episode) {
      if (episode.isFresh()) {
        newEpisodes.add(episode);
      }
    }

    @Override
    public void afterDestroy(Episode object) {

    }

    @Override
    public void afterSave(Episode object) {

    }
  }

}




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