Android Open Source - PrincePolo Notification Handler






From Project

Back to project page PrincePolo.

License

The source code is released under:

GNU General Public License

If you think the Android project PrincePolo 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 se.agile.model;
//  w  ww .  j  a  v a 2s .co m
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import se.agile.activities.MainActivity;
import se.agile.activities.MainActivity.VIEW;
import se.agile.asynctasks.RequestBranches;
import se.agile.asynctasks.RequestFullCommit;
import se.agile.asynctasks.RequestListener;
import se.agile.asynctasks.RequestListenerAdapter;
import se.agile.githubdata.Branch;
import se.agile.githubdata.Commit;
import se.agile.githubdata.File;
import se.agile.princepolo.R;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class NotificationHandler extends RequestListenerAdapter<ArrayList<Branch>>{
  private ScheduledFuture<?> future;
  private RequestBranches requestBranches;
  private static NotificationManager notificationManager;
  private static Context context;
  private String logTag = "PrincePolo";
  private Runnable runnable;
  private ScheduledExecutorService scheduler;
  
  private static ArrayList<NotificationListener> listenerList = new ArrayList<NotificationListener>();
  
  public NotificationHandler(Context contexts){
    context = contexts;
    final RequestListener<ArrayList<Branch>> listener = this;

    runnable = new Runnable() {
      public void run() {
        requestBranches = new RequestBranches(listener);
        requestBranches.execute();
      }
    };
    
  }
  
  public void start(){
    scheduler = Executors.newSingleThreadScheduledExecutor();
    future = scheduler.scheduleAtFixedRate(runnable, 0, 10, TimeUnit.SECONDS);
    // Time is set in the Settings fragment and the value is stored in the Preferences class
    // Integer.parseInt(Preferences.getTimeInterval())
  }
  
  public void stop(){
    if(future != null){
      requestBranches.cancel(false);
      future.cancel(false);
      scheduler.shutdown();
    }else{
      Log.e(logTag, "you haven't started before you cancel");
    }
  }

  @Override
  public void requestFinished(ArrayList<Branch> branches) {
    ArrayList<Branch> earlierBranches = TemporaryStorage.branchList;
    if(earlierBranches != null && branches != null){
      ArrayList<Branch> unselectedBrancherForNotification = Preferences.getUnselectedBranches();
      for(Branch branch : branches){
        int index = earlierBranches.indexOf(branch);
        if(index >= 0){
          //Branch exist
          
          //Does the user want the notification??
          boolean wantCommit = true;
          for(Branch unselectedBranch : unselectedBrancherForNotification){
            if(branch.equals(unselectedBranch)){
              wantCommit = false;
              break;
            }
          }
          if(wantCommit){
            Commit earlierCommit = earlierBranches.get(index).getLatestCommit();
            Commit latestCommit = branch.getLatestCommit();
            if(!earlierCommit.equals(latestCommit)){
//              Commits are different. A new commit have been received.
              getFullCommit(branch);
            }
          }
          
        }else{
          //New branch
          Log.d("PrincePolo", "New branch recieved");
          BranchNotification branchNotification = new BranchNotification(branch);
          TemporaryStorage.addNotification(branchNotification);
          fireNotification(branchNotification);
        }
      }
    }
    if(branches != null){
      TemporaryStorage.branchList = branches.size() > 0 ? branches : null;
    }
    
  }
  
  private void getFullCommit(Branch branch){
    final Commit simpleCommit = branch.getLatestCommit();
    RequestListener<Commit> listener = new RequestListenerAdapter<Commit>() {
      
      @Override
      public void requestFinished(Commit commit) {
        if(commit != null && commit.equals(simpleCommit)){
          CommitNotification commitNotificaiton = new CommitNotification(commit);
          ArrayList<File> workingFiles = TemporaryStorage.workingFiles;
          ArrayList<File> changedFiles = commit.getChangedFiles();
          TemporaryStorage.addNotification(commitNotificaiton );
          fireNotification(commitNotificaiton);
          for(File changedFile : changedFiles){
            for(File workingFile : workingFiles){
              if(changedFile.equals(workingFile)){
                ConflictNotification confNotif = new ConflictNotification(changedFile, commit);
                TemporaryStorage.addNotification(confNotif);
                fireNotification(confNotif);
              }
            }
          }
        }
      }
    };
    RequestFullCommit requestCommit = new RequestFullCommit(listener);
    requestCommit.execute(simpleCommit.getSha(),branch.getName());
  }

  private void fireNotification(Notification notification){
    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.setAction(VIEW.NOTIFICATIONS.getName());
    
    PendingIntent contentIntent = PendingIntent.getActivity(context, notification.getId(), resultIntent,PendingIntent.FLAG_CANCEL_CURRENT);
    
    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      long[] pattern = {500,500,500,500,500,500,500,500,500};
       
      NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
      builder.setContentIntent(contentIntent);
      builder.setSmallIcon(R.drawable.ic_launcher);
      builder.setContentTitle(notification.getContentTitle());
      builder.setContentText(notification.getContentText());
      builder.setSound(alarmSound);
      builder.setVibrate(pattern);
      builder.setAutoCancel(true);
      if(notification instanceof CommitNotification){
        CommitNotification commitNot = (CommitNotification) notification;
        builder.setWhen(commitNot.getData().getDate().getTime());
      }

      notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
      notificationManager.notify(notification.getId(), builder.build());
      for(NotificationListener listener : listenerList){
        listener.notificationRecieved();
      }
  }
  
  public static void viewedNotification(Notification notification){
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Log.d("PrincePolo", "cancel id: " + notification.getId());
    notificationManager.cancel(notification.getId());
  }
  
  public static boolean addNotificationListener(NotificationListener listener){
    return listenerList.add(listener);
  }
  
  public static boolean removeNotificationListener(NotificationListener listener){
    return listenerList.remove(listener);
  }
}




Java Source Code List

se.agile.activities.AboutFragment.java
se.agile.activities.BranchFragment.java
se.agile.activities.BranchesFragment.java
se.agile.activities.CardFragment.java
se.agile.activities.CollaboratorsFragment.java
se.agile.activities.CommitFragment.java
se.agile.activities.ConflictFragment.java
se.agile.activities.ConnectToGitHubFragment.java
se.agile.activities.LoginActivity.java
se.agile.activities.MainActivity.java
se.agile.activities.NotificationFragmentSwitcher.java
se.agile.activities.NotificationsFragment.java
se.agile.activities.PersonalNotesFragment.java
se.agile.activities.PokerFragment.java
se.agile.activities.RepositoryOverviewFragment.java
se.agile.activities.SelectRepositoryFragment.java
se.agile.activities.SelectWorkingFilesActivity.java
se.agile.activities.SelectWorkingFilesFragment.java
se.agile.activities.SelectWorkingFilesOverviewFragment.java
se.agile.activities.SettingsFragment.java
se.agile.activities.SignOutFragment.java
se.agile.asynctasks.RequestAccessToken.java
se.agile.asynctasks.RequestAllCommitsForBranch.java
se.agile.asynctasks.RequestAllFilesForBranch.java
se.agile.asynctasks.RequestBranch.java
se.agile.asynctasks.RequestBranches.java
se.agile.asynctasks.RequestFiles.java
se.agile.asynctasks.RequestFullCommit.java
se.agile.asynctasks.RequestListenerAdapter.java
se.agile.asynctasks.RequestListener.java
se.agile.asynctasks.RequestRepositories.java
se.agile.asynctasks.RequestShortCommit.java
se.agile.asynctasks.RequestTask.java
se.agile.asynctasks.RequestUser.java
se.agile.controller.MainActivityController.java
se.agile.controller.settingsFragmentController.java
se.agile.githubdata.Branch.java
se.agile.githubdata.Commit.java
se.agile.githubdata.Directory.java
se.agile.githubdata.File.java
se.agile.githubdata.Folder.java
se.agile.githubdata.GitHubDataInterface.java
se.agile.githubdata.Repository.java
se.agile.githubdata.User.java
se.agile.model.BranchNotification.java
se.agile.model.BranchSelectionModel.java
se.agile.model.CommitNotification.java
se.agile.model.ConflictNotification.java
se.agile.model.CreateNotificationActivity.java
se.agile.model.DirectoryListArrayAdapter.java
se.agile.model.InteractiveArrayAdapter.java
se.agile.model.JSONParser.java
se.agile.model.NotificationDialog.java
se.agile.model.NotificationHandler.java
se.agile.model.NotificationListArrayAdapter.java
se.agile.model.NotificationListener.java
se.agile.model.Notification.java
se.agile.model.PreferenceListener.java
se.agile.model.Preferences.java
se.agile.model.TemporaryStorage.java
se.agile.model.Tuple.java
se.agile.model.WorkingFileListArrayAdapter.java
se.agile.navigator.NavDrawerItem.java
se.agile.navigator.NavDrawerListAdapter.java