Advertisement
Guest User

OutwittersNotificationManager

a guest
Mar 24th, 2015
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.31 KB | None | 0 0
  1. package com.onemanleft.outwitters;
  2.  
  3. import android.app.Notification;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.graphics.Bitmap;
  9. import android.graphics.drawable.BitmapDrawable;
  10.  
  11. import com.onemanleft.outwitters.MainActivity;
  12. import com.onemanleft.outwitters.R;
  13.  
  14. public class OutwittersNotificationManager {
  15.  
  16.     private static final int NOTIFICATION_ID = 1;
  17.  
  18.     public static void notifyNewTurn(Context context) {
  19.         // Call this when the player gets a new turn
  20.         getNotificationManager(context).notify(
  21.                 NOTIFICATION_ID,
  22.                 makeNotification(context).build()
  23.         );
  24.     }
  25.  
  26.     public static void updateTurnNotification(Context context) {
  27.         // Call this when the player finishes a turn
  28.         if (Game.hasPendingGame()) {
  29.             getNotificationManager(context).notify(
  30.                     NOTIFICATION_ID,
  31.                     makeNotification(context).setOnlyAlertOnce(true).build()
  32.             );
  33.         }
  34.         else{
  35.             getNotificationManager(context).cancel(NOTIFICATION_ID);
  36.         }
  37.     }
  38.  
  39.     public static NotificationManager getNotificationManager(Context context) {
  40.         return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  41.     }
  42.  
  43.     public static Notification.Builder makeNotification (Context context) {
  44.         return new Notification.Builder(context)
  45.                 .setLargeIcon(getLargeIcon(context))
  46.                 .setContentTitle("Outwitters")
  47.                 .setSmallIcon(R.id.notificationIcon) // Or something, I don't know how Unity does drawables,
  48.                                                      // but I get the suspicion that it doesn't
  49.                 .setContentText(getContentText())
  50.                 .setWhen(getWhen())
  51.                 .setNumber(getNumberOfTurnsAvailable())
  52.                 .setContentIntent(PendingIntent.getActivity(
  53.                         context,
  54.                         0,
  55.                         new Intent(context, MainActivity.class) //I don't know how Unity does Activities either
  56.                                 /*
  57.                                  * If you really want to get fancy, you can make the intent
  58.                                  * navigate to the current turn
  59.                                  *
  60.                                  * This probably isn't exactly how you'd do this, but there's enough
  61.                                  * documentation and Stack Overflow threads to make it work
  62.                                  */
  63.                                 .putExtra("GAME_ID_EXTRA", Long.MIN_VALUE /* some way of identifying the game */),
  64.                                 /*
  65.                                  * You'd also want to handle the intent by overriding
  66.                                  * onNewIntent(Intent intent) in MainActivity
  67.                                  * and have it take care of said fancy navigation
  68.                                  *
  69.                                  * This might not be possible with the way Unity does Activities,
  70.                                  * but again, StackOverflow can probably cobble something together
  71.                                  * if nothing else
  72.                                  */
  73.                         PendingIntent.FLAG_UPDATE_CURRENT));
  74.     }
  75.  
  76.     public static Bitmap getLargeIcon (Context context){
  77.         return ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();
  78.         // Or something, I don't know how Unity does drawables
  79.         // Also, ignore that this method is deprecated as of API 22,
  80.         // Google's API's are unbelievably stupid sometimes
  81.     }
  82.  
  83.     public static String getContentText() {
  84.         Game g = Game.getMostRecentGame();
  85.         String message = "Your turn with " + g.opponentName;
  86.         if (g.isMultiplayer) message += " and " + g.secondOpponentName;
  87.         return message;
  88.     }
  89.  
  90.     public static long getWhen(){
  91.         return 0; // It's a really old game -- like REALLY old
  92.                   //
  93.                   // It's so old that if you cut its life into equal halves,
  94.                   // both of them can legally drink in the United States
  95.     }
  96.  
  97.     public static int getNumberOfTurnsAvailable(){
  98.         return Integer.MAX_VALUE; // They have a lot of games going on right now.
  99.     }
  100.  
  101.     public static class Game{
  102.         // For demonstration purposes only
  103.  
  104.         // Saying this is an oversimplification of what actually happens is an understatement,
  105.         // but you get the idea and have probably already implemented this successfully
  106.  
  107.         // If you have not successfully implemented these methods and fields, then Alex
  108.         // is most likely a wizard and should put on trial for abuse of arcane magic
  109.  
  110.         public boolean isMultiplayer;
  111.         public String opponentName;
  112.         public String secondOpponentName;
  113.  
  114.         public static Game getMostRecentGame(){
  115.             return null; // This is probably already in Outwitters' code somewhere
  116.                          // Although hopefully not this exact method
  117.         }
  118.  
  119.         public static boolean hasPendingGame(){
  120.             return getMostRecentGame() != null; // Or something
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement