Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ftpsentinel;
- import java.io.File;
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.util.Log;
- import android.widget.Toast;
- public class CheckService extends Service {
- private Ftp newFtp;
- private Utils utils;
- private String newFileList, fileList;
- private Timer timer;
- private int interval;
- private String hostname;
- public boolean haveUserRead;
- public NotificationManager notificationManager;
- public FtpsentinelActivity ftps;
- CheckService(Ftp newFtp, Utils utils, String newFileList, String fileList, String hostname, int interval) {
- this.newFtp = newFtp;
- this.utils = utils;
- this.newFileList = newFileList;
- this.fileList = fileList;
- this.hostname = hostname;
- this.interval = interval;
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- startCheckService();
- }
- public void checkForUpdates() {
- if(!newFtp.checkUpdates(utils, newFileList, fileList)) {
- Log.i("LOL", "<<< CHANGED FILES >>>");
- showNotification("Some files changed", "Some files changed", "file changed on" + hostname, 1);
- } else {
- Log.i("LOL", "<<< NOT CHANGED FILES >>>");
- }
- if(!(new File(newFileList)).delete()) {
- Toast.makeText(getBaseContext(), "!!! IOExcpection !!! unable to delete new file list", 5000).show();
- }
- }
- public void startCheckService() {
- timer = new Timer();
- final Handler update = new Handler() {
- public void dispatchMessage (Message msg) {
- super.dispatchMessage(msg);
- Log.i("LOL", "<<< CHECKING >>>");
- newFtp.checkUpdates(utils, newFileList, fileList);
- }
- };
- timer.scheduleAtFixedRate(new TimerTask() {
- public void run() {
- try {
- Log.i("LOL", "<<< RUN >>>");
- update.sendEmptyMessage(0);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }, 0, interval);
- }
- public void stopCheckService() {
- if(timer != null) {
- timer.cancel();
- }
- }
- void showNotification(String title, String brief, String message, int ID) {
- if(!haveUserRead) {
- Notification notification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
- Intent notificationIntent = new Intent(this, FtpsentinelActivity.class);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
- notification.setLatestEventInfo(getBaseContext(), brief, message, contentIntent);
- notificationManager.notify(ID, notification);
- haveUserRead = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement