Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. package com.ftpsentinel;
  2.  
  3. import java.io.File;
  4. import java.util.Timer;
  5. import java.util.TimerTask;
  6.  
  7. import android.app.Notification;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.app.Service;
  11. import android.content.Intent;
  12. import android.os.Handler;
  13. import android.os.IBinder;
  14. import android.os.Message;
  15. import android.util.Log;
  16. import android.widget.Toast;
  17.  
  18. public class CheckService extends Service {
  19.  
  20. private Ftp newFtp;
  21. private Utils utils;
  22. private String newFileList, fileList;
  23. private Timer timer;
  24. private int interval;
  25. private String hostname;
  26. public boolean haveUserRead;
  27. public NotificationManager notificationManager;
  28. public FtpsentinelActivity ftps;
  29.  
  30. CheckService(Ftp newFtp, Utils utils, String newFileList, String fileList, String hostname, int interval) {
  31. this.newFtp = newFtp;
  32. this.utils = utils;
  33. this.newFileList = newFileList;
  34. this.fileList = fileList;
  35. this.hostname = hostname;
  36. this.interval = interval;
  37. }
  38.  
  39. @Override
  40. public IBinder onBind(Intent intent) {
  41. return null;
  42. }
  43.  
  44. @Override
  45. public void onCreate() {
  46. super.onCreate();
  47. startCheckService();
  48. }
  49.  
  50. public void checkForUpdates() {
  51. if(!newFtp.checkUpdates(utils, newFileList, fileList)) {
  52. Log.i("LOL", "<<< CHANGED FILES >>>");
  53. showNotification("Some files changed", "Some files changed", "file changed on" + hostname, 1);
  54. } else {
  55. Log.i("LOL", "<<< NOT CHANGED FILES >>>");
  56. }
  57. if(!(new File(newFileList)).delete()) {
  58. Toast.makeText(getBaseContext(), "!!! IOExcpection !!! unable to delete new file list", 5000).show();
  59. }
  60. }
  61.  
  62. public void startCheckService() {
  63.  
  64. timer = new Timer();
  65.  
  66. final Handler update = new Handler() {
  67. public void dispatchMessage (Message msg) {
  68. super.dispatchMessage(msg);
  69. Log.i("LOL", "<<< CHECKING >>>");
  70. newFtp.checkUpdates(utils, newFileList, fileList);
  71. }
  72. };
  73.  
  74. timer.scheduleAtFixedRate(new TimerTask() {
  75. public void run() {
  76. try {
  77. Log.i("LOL", "<<< RUN >>>");
  78. update.sendEmptyMessage(0);
  79. } catch (Exception e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }, 0, interval);
  84. }
  85.  
  86. public void stopCheckService() {
  87. if(timer != null) {
  88. timer.cancel();
  89. }
  90. }
  91.  
  92. void showNotification(String title, String brief, String message, int ID) {
  93. if(!haveUserRead) {
  94. Notification notification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
  95.  
  96. Intent notificationIntent = new Intent(this, FtpsentinelActivity.class);
  97. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
  98.  
  99. notification.setLatestEventInfo(getBaseContext(), brief, message, contentIntent);
  100. notificationManager.notify(ID, notification);
  101.  
  102. haveUserRead = true;
  103. }
  104. }
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement