Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package io.github.boapps.eSzivacs.Utils;
  2.  
  3. import android.app.Activity;
  4. import android.app.NotificationManager;
  5. import android.app.PendingIntent;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.graphics.Color;
  10. import android.net.ConnectivityManager;
  11. import android.net.NetworkInfo;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.support.v4.app.NotificationCompat;
  15. import android.util.Log;
  16. import android.widget.Toast;
  17.  
  18. import com.google.android.gms.gcm.GcmNetworkManager;
  19. import com.google.android.gms.gcm.GcmTaskService;
  20. import com.google.android.gms.gcm.OneoffTask;
  21. import com.google.android.gms.gcm.PeriodicTask;
  22. import com.google.android.gms.gcm.Task;
  23. import com.google.android.gms.gcm.TaskParams;
  24.  
  25. import java.io.IOException;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28.  
  29. import io.github.boapps.eSzivacs.Activities.EvaluationListActivity;
  30. import io.github.boapps.eSzivacs.Datas.Evaluation;
  31. import io.github.boapps.eSzivacs.R;
  32.  
  33. public class BackgroundTaskService extends GcmTaskService {
  34.  
  35.     public static final String GCM_ONEOFF_TAG = "oneoff|[0,0]";
  36.     public static final String GCM_REPEAT_TAG = "repeat|[7200,1800]";
  37.     private static final String TAG = BackgroundTaskService.class.getSimpleName();
  38.  
  39.     public static void scheduleRepeat(Context context) {
  40.         //in this method, single Repeating task is scheduled (the target service that will be called is BackgroundTaskService.class)
  41.         try {
  42.             PeriodicTask periodic = new PeriodicTask.Builder()
  43.                     //specify target service - must extend GcmTaskService
  44.                     .setService(BackgroundTaskService.class)
  45.                     //repeat every 60 seconds
  46.                     .setPeriod(10 * 60) //10 mins
  47. //                .setPeriod(60) //1 hour
  48. //              specify how much earlier the task can be executed (in seconds)
  49.                     .setFlex(5 * 60) //5 mins
  50.                     //tag that is unique to this task (can be used to cancel task)
  51.                     .setTag(GCM_REPEAT_TAG)
  52.                     //whether the task persists after device reboot
  53.                     .setPersisted(true)
  54.                     //if another task with same tag is already scheduled, replace it with this task
  55.                     .setUpdateCurrent(true)
  56.                     //set required network state, this line is optional
  57.                     .setRequiredNetwork(Task.NETWORK_STATE_ANY)
  58.                     //request that charging must be connected, this line is optional
  59.                     .setRequiresCharging(false)
  60.                     .build();
  61.             GcmNetworkManager.getInstance(context).schedule(periodic);
  62.             Log.v(TAG, "repeating task scheduled");
  63.         } catch (Exception e) {
  64.             Log.e(TAG, "scheduling failed");
  65.             e.printStackTrace();
  66.         }
  67.     }
  68.  
  69.     @Override
  70.     public void onInitializeTasks() {
  71.         //called when app is updated to a new version, reinstalled etc.
  72.         //you have to schedule your repeating tasks again
  73.         super.onInitializeTasks();
  74.     }
  75.  
  76.     @Override
  77.     public int onRunTask(TaskParams taskParams) {
  78.         Bundle extras = taskParams.getExtras();
  79.  
  80.         Handler h = new Handler(getMainLooper());
  81.         Log.v(TAG, "onRunTask");
  82.         if (taskParams.getTag().equals(GCM_ONEOFF_TAG)) {
  83.             h.post(new Runnable() {
  84.                 @Override
  85.                 public void run() {
  86.                     Toast.makeText(BackgroundTaskService.this, "ONEOFF executed", Toast.LENGTH_SHORT).show();
  87.                 }
  88.             });
  89.         } else if (taskParams.getTag().equals(GCM_REPEAT_TAG)) {
  90.             h.post(new Runnable() {
  91.                 @Override
  92.                 public void run() {
  93.                     if (isOnline()) {
  94.                         AccountManager accountManager = new AccountManager(getApplicationContext());
  95.                         String usr = accountManager.getSelectedAccount();
  96.                         SharedPreferences sharedPreferences = getApplication().getSharedPreferences(usr, Activity.MODE_PRIVATE);
  97.                         String psw = sharedPreferences.getString("pw", "");
  98.                         String schoolCode = sharedPreferences.getString("schoolCode", "");
  99.                         String schoolUrl = sharedPreferences.getString("schoolUrl", "");
  100.  
  101.                         DataLoader dloader = new DataLoader(getApplicationContext());
  102.                         dloader.setLogin(usr, psw, schoolUrl, schoolCode);
  103.                         try {
  104.                             dloader.doLogin();
  105.                         } catch (IOException e) {
  106.                             e.printStackTrace();
  107.                         }
  108.  
  109.                         ArrayList<Evaluation> evaluations = dloader.getNewEvaluations();
  110.  
  111.                         for (Evaluation evaluation : evaluations) {
  112.                             System.out.println(evaluation.getSubject() + evaluation.getNumericValue());
  113.                             String CHANNEL_ID = "jegyek";
  114.                             NotificationCompat.Builder mBuilder =
  115.                                     new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
  116.                                             .setSmallIcon(R.drawable.ic_school_black_24px)
  117.                                             .setContentTitle(evaluation.getSubject() + " " + evaluation.getNumericValue())
  118.                                             .setColor(Color.parseColor("#bf360c"))
  119.                                             .setContentText(evaluation.getTheme());
  120.                             NotificationManager mNotificationManager =
  121.                                     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  122.                             int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
  123.                             Intent intent = new Intent(getApplicationContext(), EvaluationListActivity.class);
  124.                             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
  125.                                     | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  126.  
  127.                             PendingIntent pendingIntentintent = PendingIntent.getActivity(getApplicationContext(), 0,
  128.                                     intent, 0);
  129.                             mBuilder.setContentIntent(pendingIntentintent);
  130.  
  131.                             mNotificationManager.notify(m, mBuilder.build());
  132.  
  133.                         }
  134.                     }
  135.  
  136.  
  137.                 }
  138.             });
  139.         }
  140.         return GcmNetworkManager.RESULT_SUCCESS;
  141.     }
  142.  
  143.     public boolean isOnline() {
  144.         ConnectivityManager cm =
  145.                 (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
  146.         NetworkInfo netInfo = cm.getActiveNetworkInfo();
  147.         return netInfo != null && netInfo.isConnectedOrConnecting();
  148.     }
  149.  
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement