Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.02 KB | None | 0 0
  1. package com.ftpsentinel;
  2.  
  3. import java.io.File;
  4.  
  5. import com.ftpsentinel.Utils;
  6.  
  7. import android.app.Notification;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.widget.Toast;
  14. import android.preference.CheckBoxPreference;
  15. import android.preference.EditTextPreference;
  16. import android.preference.Preference;
  17. import android.preference.Preference.OnPreferenceClickListener;
  18. import android.preference.PreferenceActivity;
  19.  
  20. public class FtpsentinelActivity extends PreferenceActivity {
  21.    
  22.     public boolean getWelcomeMsg = false;
  23.     public EditTextPreference hostEdit;
  24.     public EditTextPreference usernameEdit;
  25.     public EditTextPreference passwordEdit;
  26.     public EditTextPreference portEdit;
  27.     public CheckBoxPreference welcomeCheck;
  28.     public EditTextPreference timerButton;
  29.     public Preference connectButton;
  30.     public Preference fileListButton;
  31.     public Preference manualCheckButton;
  32.     public Preference logoutButton;
  33.     public Ftp newFtp;
  34.    
  35.     Utils utils = new Utils();
  36.    
  37.     public String dateFile;
  38.     public final String fileList = "/sdcard/.ftpsentinelList";
  39.     public final String newFileList = "/sdcard/.ftpsentinelList.new";
  40.     public int timer;
  41.     public CheckService checkService;
  42.     public boolean haveUserRead;
  43.    
  44.     @Override
  45.     public void onCreate(Bundle savedInstanceState) {
  46.         super.onCreate(savedInstanceState);
  47.         addPreferencesFromResource(R.layout.layout);
  48.        
  49.         dateFile = utils.getFileDate(fileList);
  50.        
  51.         hostEdit = (EditTextPreference) findPreference("host");
  52.         usernameEdit = (EditTextPreference) findPreference("username");
  53.         passwordEdit = (EditTextPreference) findPreference("password");
  54.         portEdit = (EditTextPreference) findPreference("port");
  55.        
  56.         welcomeCheck = (CheckBoxPreference) findPreference("welcomeCheck");
  57.        
  58.         timerButton = (EditTextPreference) findPreference("timerButton");
  59.        
  60.         connectButton = (Preference) findPreference("connectButton");
  61.         connectButton.setOnPreferenceClickListener(connectListener);
  62.        
  63.         fileListButton = (Preference) findPreference("getListButton");
  64.         fileListButton.setSummary("Last update on: " + dateFile);
  65.         fileListButton.setOnPreferenceClickListener(fileListListener);
  66.        
  67.         manualCheckButton = (Preference) findPreference("manualCheckButton");
  68.         manualCheckButton.setOnPreferenceClickListener(manualCheckListener);
  69.        
  70.         logoutButton = (Preference) findPreference("disconnectButton");
  71.         logoutButton.setOnPreferenceClickListener(logoutListener);
  72.     }
  73.  
  74.     OnPreferenceClickListener connectListener = new OnPreferenceClickListener() {
  75.        
  76.         private String hostname;
  77.         private String username;
  78.         private String password;
  79.         private int port;
  80.  
  81.         public boolean onPreferenceClick(Preference preference) {
  82.             hostname = hostEdit.getText();
  83.             username = usernameEdit.getText();
  84.             password = passwordEdit.getText();
  85.             port = Integer.parseInt(portEdit.getText());
  86.            
  87.             if(welcomeCheck.isChecked()) {
  88.                 getWelcomeMsg = true;
  89.             }
  90.            
  91.             String IP = !utils.isIPAddress(hostname) ? utils.GetIP(hostname) : hostname;
  92.            
  93.             newFtp = new Ftp(username, password, IP, port);
  94.             newFtp.connect(getWelcomeMsg);
  95.                        
  96.             if(newFtp.getWelcomeMessage() != null) {
  97.                 Toast.makeText(getBaseContext(), newFtp.getWelcomeMessage(), 100000).show();
  98.             }
  99.            
  100.             checkService = new CheckService(newFtp, utils, newFileList, fileList, hostEdit.getText(), Integer.parseInt(timerButton.getText()));
  101.             checkService.startCheckService();
  102.             return true;
  103.         }
  104.  
  105.     };
  106.    
  107.     OnPreferenceClickListener fileListListener = new OnPreferenceClickListener() {
  108.  
  109.         public boolean onPreferenceClick(Preference preference) {
  110.             newFtp.getFileList(fileList);
  111.             return true;
  112.         }
  113.        
  114.     };
  115.    
  116.     OnPreferenceClickListener manualCheckListener = new OnPreferenceClickListener() {
  117.        
  118.         public boolean onPreferenceClick(Preference preference) {
  119.             if(!newFtp.checkUpdates(utils, newFileList, fileList)) {
  120.                 Log.i("LOL", "<<< FILE CHANGED! >>>");
  121.                 //checkService.showNotification("Some files changed", "Some files changed", "file changed on " + hostEdit.getText(), 1);
  122.             }
  123.             if(!(new File(newFileList)).delete()) {
  124.                 Toast.makeText(getBaseContext(), "!!! IOExcpection !!! unable to delete new file list", 5000).show();
  125.             }
  126.             return true;
  127.         }
  128.        
  129.     };
  130.    
  131.     OnPreferenceClickListener logoutListener = new OnPreferenceClickListener() {
  132.        
  133.         public boolean onPreferenceClick(Preference preference) {
  134.             newFtp.disconnect();
  135.             checkService.stopCheckService();
  136.             Toast.makeText(getBaseContext(), "Disconnected", 2000).show();
  137.             return true;
  138.         }
  139.        
  140.     };
  141.    
  142.     public void hello() { Log.i("LOL", "<<< HELLO! >>>"); }
  143.  
  144.     public void checkForUpdates() {
  145.         if(!newFtp.checkUpdates(utils, newFileList, fileList)) {
  146.             Log.i("LOL", "<<< CHANGED FILES >>>");
  147.             //showNotification("Some files changed", "Some files changed", "file changed on" + hostEdit.getText(), 1);
  148.         } else {
  149.             Log.i("LOL", "<<< NOT CHANGED FILES >>>");
  150.         }
  151.         if(!(new File(newFileList)).delete()) {
  152.             Toast.makeText(getBaseContext(), "!!! IOExcpection !!! unable to delete new file list", 5000).show();
  153.         }
  154.     }
  155.    
  156.     void showNotification(String title, String brief, String message, int ID) {
  157.         if(!haveUserRead) {
  158.             NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  159.             Notification notification = new Notification(R.drawable.icon, title, System.currentTimeMillis());
  160.        
  161.             Intent notificationIntent = new Intent(FtpsentinelActivity.this, FtpsentinelActivity.class);
  162.             PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
  163.            
  164.             notification.setLatestEventInfo(getBaseContext(), brief, message, contentIntent);
  165.             notificationManager.notify(ID, notification);
  166.            
  167.             haveUserRead = true;
  168.         }
  169.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement