Advertisement
rachmadi

Get Duration

Feb 26th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.87 KB | None | 0 0
  1. package info.lina.netstatlog;
  2.  
  3. import android.Manifest;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.database.Cursor;
  8. import android.net.Uri;
  9. import android.support.v4.app.ActivityCompat;
  10. import android.support.v4.content.ContextCompat;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.os.Bundle;
  13. import android.view.Menu;
  14. import android.view.MenuItem;
  15. import android.view.View;
  16. import android.widget.Button;
  17.  
  18. import com.google.android.gms.appindexing.Action;
  19. import com.google.android.gms.appindexing.AppIndex;
  20. import com.google.android.gms.common.api.GoogleApiClient;
  21.  
  22. import java.text.ParseException;
  23. import java.text.SimpleDateFormat;
  24. import java.util.ArrayList;
  25. import java.util.Date;
  26. import java.util.HashMap;
  27. import java.util.concurrent.TimeUnit;
  28.  
  29. public class MainMenu extends AppCompatActivity {
  30.  
  31.     Button btnInetUp, btnInetDown, btnPowerUp, btnPowerDown, btnUpsUp, btnUpsDown, btnLog;
  32.     ArrayList<HashMap<String, String>> smsList;
  33.     ArrayList<String> arraylist_date = new ArrayList<String>();
  34.     ArrayList<String> arraylist_ups_up = new ArrayList<String>();
  35.     ArrayList<String> arraylist_pwr_up = new ArrayList<String>();
  36.     ArrayList<String> arraylist_inet_up = new ArrayList<String>();
  37.     ArrayList<String> arraylist_ups_down = new ArrayList<String>();
  38.     ArrayList<String> arraylist_pwr_down = new ArrayList<String>();
  39.     ArrayList<String> arraylist_inet_down = new ArrayList<String>();
  40.     ArrayList<String> arraylist_log = new ArrayList<String>();
  41.     DBHelper db;
  42.     Utility util = new Utility();
  43.     private ProgressDialog pDialog;
  44.  
  45.     public static final String SMS_COLUMN_CONTENT = "body";
  46.     public static final String SMS_CONTENT_INETUP = "inet up";
  47.     public static final String SMS_CONTENT_INETDOWN = "inet down";
  48.     public static final String SMS_CONTENT_UPSUP = "ups up";
  49.     public static final String SMS_CONTENT_UPSDOWN = "ups down";
  50.     public static final String SMS_CONTENT_POWERUP = "power up";
  51.     public static final String SMS_CONTENT_POWERDOWN = "power down";
  52.     /**
  53.      * ATTENTION: This was auto-generated to implement the App Indexing API.
  54.      * See https://g.co/AppIndexing/AndroidStudio for more information.
  55.      */
  56.     private GoogleApiClient client;
  57.  
  58.     @Override
  59.     protected void onCreate(Bundle savedInstanceState) {
  60.         super.onCreate(savedInstanceState);
  61.         setContentView(R.layout.activity_main_menu);
  62.  
  63.         btnInetUp = (Button) findViewById(R.id.btnInetUp);
  64.         btnInetDown = (Button) findViewById(R.id.btnInetDown);
  65.         btnPowerUp = (Button) findViewById(R.id.btnPowerUp);
  66.         btnPowerDown = (Button) findViewById(R.id.btnPowerDown);
  67.         btnUpsUp = (Button) findViewById(R.id.btnUpsUp);
  68.         btnUpsDown = (Button) findViewById(R.id.btnUpsDown);
  69.         btnLog = (Button)findViewById(R.id.btnLog);
  70.  
  71.         //set the listener when the buttons are clicked
  72.         btnInetUp.setOnClickListener(new View.OnClickListener() {
  73.             @Override
  74.             public void onClick(View v) {
  75.                 util.setTittle(getApplicationContext(), "Internet Up");
  76.                 //get data from database table and store it in arraylist
  77.                 arraylist_date = db.getInetUp(SMS_CONTENT_INETUP);
  78.                 //prepare intent to start Log activity
  79.                 Intent intent = new Intent(getApplicationContext(), Log.class);
  80.                 //prepare arraylist to pass to Log activity
  81.                 intent.putStringArrayListExtra("arraylist_date", arraylist_date);
  82.                 //start Log activity
  83.                 startActivity(intent);
  84.             }
  85.         });
  86.  
  87.         btnInetDown.setOnClickListener(new View.OnClickListener() {
  88.             @Override
  89.             public void onClick(View v) {
  90.                 util.setTittle(getApplicationContext(), "Internet Down");
  91.                 arraylist_date = db.getInetUp(SMS_CONTENT_INETDOWN);
  92.                 Intent intent = new Intent(getApplicationContext(), Log.class);
  93.  
  94.                 intent.putStringArrayListExtra("arraylist_date", arraylist_date);
  95.                 startActivity(intent);
  96.             }
  97.         });
  98.  
  99.         btnPowerUp.setOnClickListener(new View.OnClickListener() {
  100.             @Override
  101.             public void onClick(View v) {
  102.                 util.setTittle(getApplicationContext(), "Power Up");
  103.                 arraylist_date = db.getInetUp(SMS_CONTENT_POWERUP);
  104.                 Intent intent = new Intent(getApplicationContext(), Log.class);
  105.  
  106.                 intent.putStringArrayListExtra("arraylist_date", arraylist_date);
  107.                 startActivity(intent);
  108.             }
  109.         });
  110.  
  111.         btnPowerDown.setOnClickListener(new View.OnClickListener() {
  112.             @Override
  113.             public void onClick(View v) {
  114.                 util.setTittle(getApplicationContext(), "Power Down");
  115.                 arraylist_date = db.getInetUp(SMS_CONTENT_POWERDOWN);
  116.                 Intent intent = new Intent(getApplicationContext(), Log.class);
  117.  
  118.                 intent.putStringArrayListExtra("arraylist_date", arraylist_date);
  119.                 startActivity(intent);
  120.             }
  121.         });
  122.  
  123.         btnUpsUp.setOnClickListener(new View.OnClickListener() {
  124.             @Override
  125.             public void onClick(View v) {
  126.                 util.setTittle(getApplicationContext(), "UPS Up");
  127.                 arraylist_date = db.getInetUp(SMS_CONTENT_UPSUP);
  128.                 Intent intent = new Intent(getApplicationContext(), Log.class);
  129.  
  130.                 intent.putStringArrayListExtra("arraylist_date", arraylist_date);
  131.                 startActivity(intent);
  132.             }
  133.         });
  134.  
  135.         btnUpsDown.setOnClickListener(new View.OnClickListener() {
  136.             @Override
  137.             public void onClick(View v) {
  138.                 util.setTittle(getApplicationContext(), "UPS Down");
  139.                 arraylist_date = db.getInetUp(SMS_CONTENT_UPSDOWN);
  140.                 Intent intent = new Intent(getApplicationContext(), Log.class);
  141.  
  142.                 intent.putStringArrayListExtra("arraylist_date", arraylist_date);
  143.                 startActivity(intent);
  144.             }
  145.         });
  146.  
  147.         btnLog.setOnClickListener(new View.OnClickListener() {
  148.             @Override
  149.             public void onClick(View v) {
  150.                 util.setTittle(getApplicationContext(), "Log");
  151.                 arraylist_log.clear();
  152.                 arraylist_log = getLog();
  153.                 Intent intent = new Intent(getApplicationContext(), Log.class);
  154.                 System.out.println("arraylist_log: " + arraylist_log);
  155.  
  156.                 intent.putStringArrayListExtra("arraylist_date", arraylist_log);
  157.                 startActivity(intent);
  158.             }
  159.         });
  160.  
  161.         // ATTENTION: This was auto-generated to implement the App Indexing API.
  162.         // See https://g.co/AppIndexing/AndroidStudio for more information.
  163.         client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
  164.     }
  165.  
  166.     @Override
  167.     protected void onResume() {
  168.         super.onResume();
  169.         //check if number exist
  170.         Boolean status = util.isEmptyNumber(getApplicationContext());
  171.         if (!status) {
  172.             //if number not exist start Sender activity to give sender's number
  173.             startActivity(new Intent(getApplicationContext(), Sender.class));
  174.         } else {
  175.             //get sms from the phone sms table and store it it SQLite database table
  176.             getSMS();
  177. //            getLog();
  178.         }
  179.     }
  180.  
  181.     private void getSMS() {
  182.  
  183.         smsList = new ArrayList<HashMap<String, String>>();
  184.  
  185.         Uri uri = Uri.parse("content://sms/inbox");
  186.         Cursor c = getContentResolver().query(uri, null, null, null, null);
  187.         startManagingCursor(c);
  188.  
  189.         String sender_number = util.getNumber(getApplicationContext());
  190.  
  191.         // Read the sms data and store it in the list
  192.         if (c.moveToFirst()) {
  193.             for (int i = 0; i < c.getCount(); i++) {
  194.                 String number = c.getString(c.getColumnIndexOrThrow("address")).toString();
  195.                 String content = c.getString(c.getColumnIndexOrThrow("body")).toString();
  196.                 Date date = new Date(c.getLong(c.getColumnIndexOrThrow("date")));
  197.                 String formattedDtime = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").format(date);
  198.  
  199.                 if (number.equals(sender_number)) {
  200.                     System.out.println("SMS: " + number + ", " + content + ", " + formattedDtime);
  201.                     //create database
  202.                     db = new DBHelper(getApplication());
  203.                     //insert table
  204.                     System.out.println("Insert table: " + number + ", " + content + ", " + formattedDtime);
  205.                     db.insertSms(formattedDtime, number, content);
  206.                     if (content.contains("ups")){
  207.                         db.insertUps(formattedDtime, number, content);
  208.                     } else if(content.contains("inet")){
  209.                         db.insertUps(formattedDtime, number, content);
  210.                     } else if(content.contains("power")){
  211.                         db.insertPwr(formattedDtime, number, content);
  212.                     }
  213.                 }
  214.                 c.moveToNext();
  215.             }
  216.         }
  217.     }
  218.  
  219.     private ArrayList<String> getLog() {
  220.         String log;
  221.         //get all records
  222.         arraylist_ups_down = db.getInetUp(SMS_CONTENT_UPSDOWN);
  223.         arraylist_ups_up = db.getInetUp(SMS_CONTENT_UPSUP);
  224.         arraylist_pwr_down = db.getInetUp(SMS_CONTENT_POWERDOWN);
  225.         arraylist_pwr_up = db.getInetUp(SMS_CONTENT_POWERUP);
  226.         arraylist_inet_down = db.getInetUp(SMS_CONTENT_INETDOWN);
  227.         arraylist_inet_up = db.getInetUp(SMS_CONTENT_INETUP);
  228.         //count number of data
  229.         int ups_down = arraylist_ups_down.size();
  230.         int ups_up = arraylist_ups_up.size();
  231.         int pwr_down = arraylist_pwr_down.size();
  232.         int pwr_up = arraylist_pwr_up.size();
  233.         int inet_down = arraylist_inet_down.size();
  234.         int inet_up = arraylist_inet_up.size();
  235.         //get total records
  236.         if (ups_down < ups_up){
  237.             int total_ups = ups_down;
  238.         } else {
  239.             int total_ups = ups_up;
  240.         }
  241.         if (pwr_down < pwr_up){
  242.             int total_pwr = pwr_down;
  243.         } else {
  244.             int total_pwr = pwr_up;
  245.         }
  246.         if (inet_down < inet_up){
  247.             int total_inet = inet_down;
  248.         } else {
  249.             int total_inet = inet_up;
  250.         }
  251.  
  252.         if (ups_down!=0 && ups_up!=0){
  253.             System.out.println("ups_down, ups_up: " + ups_down + ", " +ups_up);
  254.             for (int i = 0; i < ups_down; i++){
  255.                 //get Log data and format them
  256.                 log = "UPS\n" +
  257.                         "Down: " + arraylist_ups_down.get(i) + "\n" +
  258.                         "Up     : " + arraylist_ups_up.get(i) + "\n" +
  259.                         "Down duration: " +getDuration(arraylist_ups_down.get(i), arraylist_ups_up.get(i));
  260.                 arraylist_log.add(log);
  261.             }
  262.         }
  263.         if (pwr_down!=0 && pwr_up!=0){
  264.             System.out.println("pwr_down, pwr_up: " + pwr_down + ", " + pwr_up);
  265.             for (int i = 0; i <pwr_down; i++){
  266.                 log = "Power\n" +
  267.                         "Down: " + arraylist_pwr_down.get(i) + "\n" +
  268.                         "Up     : " + arraylist_pwr_up.get(i) + "\n" +
  269.                         "Down duration: " + getDuration(arraylist_pwr_down.get(i), arraylist_pwr_up.get(i));
  270.                 arraylist_log.add(log);
  271.             }
  272.         }
  273.         if (inet_down!=0 && inet_up!=0){
  274.             System.out.println("inet_down, inet_up: " + inet_down + ", " + inet_up);
  275.             for (int i = 0; i <inet_down; i++) {
  276.                 log = "Internet\n" +
  277.                         "Down: " + arraylist_inet_down.get(i) + "\n" +
  278.                         "Up     : " + arraylist_inet_up.get(i) + "\n" +
  279.                         "Down duration: " + getDuration(arraylist_inet_down.get(i), arraylist_inet_up.get(i));
  280.                 arraylist_log.add(log);
  281.             }
  282.         }
  283.         return arraylist_log;
  284.     }
  285.  
  286.     private String getDuration(String down, String up){
  287.         String timeDuration = null;
  288.         SimpleDateFormat formattedDtime = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
  289.         try {
  290.             Date dateUp = formattedDtime.parse(up);
  291.             Date dateDown = formattedDtime.parse(down);
  292.             long duration = dateUp.getTime()-dateDown.getTime();
  293.  
  294.             long diffSeconds = duration / 1000 % 60;
  295.             System.out.println("diffSeconds: " + diffSeconds);
  296.             long diffMinutes = duration / (60 * 1000) % 60;
  297.             System.out.println("diffMinutes: " + diffMinutes);
  298.             long diffHours = duration / (60 * 60 * 1000);
  299.             System.out.println("diffHours: " + diffHours);
  300.             int diffInDays = (int) (duration) / (1000 * 60 * 60 * 24);
  301.             System.out.println("diffInDays: " + diffInDays);
  302.  
  303.             timeDuration = diffHours + "hrs, " + diffMinutes + "mins, " + diffSeconds + "secs";
  304.  
  305.         } catch (ParseException e){
  306.             e.printStackTrace();
  307.         }
  308.         return timeDuration;
  309.     }
  310.  
  311.     @Override
  312.     public boolean onCreateOptionsMenu(Menu menu) {
  313.         // Inflate the menu; this adds items to the action bar if it is present.
  314.         getMenuInflater().inflate(R.menu.menu_main, menu);
  315.         return true;
  316.     }
  317.  
  318.     @Override
  319.     public boolean onOptionsItemSelected(MenuItem item) {
  320.         // Handle action bar item clicks here. The action bar will
  321.         // automatically handle clicks on the Home/Up button, so long
  322.         // as you specify a parent activity in AndroidManifest.xml.
  323.         int id = item.getItemId();
  324.  
  325.         //noinspection SimplifiableIfStatement
  326.         if (id == R.id.sender_number) {
  327.             startActivity(new Intent(getApplicationContext(), Sender.class));
  328.             return true;
  329.         }
  330.         return super.onOptionsItemSelected(item);
  331.     }
  332.  
  333.     @Override
  334.     public void onStart() {
  335.         super.onStart();
  336.  
  337.         // ATTENTION: This was auto-generated to implement the App Indexing API.
  338.         // See https://g.co/AppIndexing/AndroidStudio for more information.
  339.         client.connect();
  340.         Action viewAction = Action.newAction(
  341.                 Action.TYPE_VIEW, // TODO: choose an action type.
  342.                 "MainMenu Page", // TODO: Define a title for the content shown.
  343.                 // TODO: If you have web page content that matches this app activity's content,
  344.                 // make sure this auto-generated web page URL is correct.
  345.                 // Otherwise, set the URL to null.
  346.                 Uri.parse("http://host/path"),
  347.                 // TODO: Make sure this auto-generated app URL is correct.
  348.                 Uri.parse("android-app://info.lina.netstatlog/http/host/path")
  349.         );
  350.         AppIndex.AppIndexApi.start(client, viewAction);
  351.     }
  352.  
  353.     @Override
  354.     public void onStop() {
  355.         super.onStop();
  356.  
  357.         // ATTENTION: This was auto-generated to implement the App Indexing API.
  358.         // See https://g.co/AppIndexing/AndroidStudio for more information.
  359.         Action viewAction = Action.newAction(
  360.                 Action.TYPE_VIEW, // TODO: choose an action type.
  361.                 "MainMenu Page", // TODO: Define a title for the content shown.
  362.                 // TODO: If you have web page content that matches this app activity's content,
  363.                 // make sure this auto-generated web page URL is correct.
  364.                 // Otherwise, set the URL to null.
  365.                 Uri.parse("http://host/path"),
  366.                 // TODO: Make sure this auto-generated app URL is correct.
  367.                 Uri.parse("android-app://info.lina.netstatlog/http/host/path")
  368.         );
  369.         AppIndex.AppIndexApi.end(client, viewAction);
  370.         client.disconnect();
  371.     }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement