Advertisement
nigatigga

Untitled

Sep 16th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 34.93 KB | None | 0 0
  1. package com.spicycurryman.getdisciplined10.app;
  2.  
  3. import android.app.ActionBar;
  4. import android.app.AlarmManager;
  5. import android.app.AlertDialog;
  6. import android.app.Notification;
  7. import android.app.NotificationManager;
  8. import android.app.PendingIntent;
  9. import android.app.admin.DevicePolicyManager;
  10. import android.content.ComponentName;
  11. import android.content.Context;
  12. import android.content.DialogInterface;
  13. import android.content.Intent;
  14. import android.content.SharedPreferences;
  15. import android.content.pm.ActivityInfo;
  16. import android.graphics.Typeface;
  17. import android.os.Bundle;
  18. import android.os.CountDownTimer;
  19. import android.support.v7.app.ActionBarActivity;
  20. import android.text.Spannable;
  21. import android.text.SpannableString;
  22. import android.util.Log;
  23. import android.view.Menu;
  24. import android.view.MenuInflater;
  25. import android.view.View;
  26. import android.view.View.OnClickListener;
  27. import android.widget.CheckBox;
  28. import android.widget.CompoundButton;
  29. import android.widget.SeekBar;
  30. import android.widget.SeekBar.OnSeekBarChangeListener;
  31. import android.widget.TextView;
  32. import android.widget.Toast;
  33.  
  34. import com.ibc.android.demo.appslist.app.HeartBeat;
  35. import com.triggertrap.seekarc.SeekArc;
  36.  
  37. import java.util.ArrayList;
  38. import java.util.Date;
  39. import java.util.Map;
  40.  
  41. public class MainActivity extends ActionBarActivity {
  42.     private SeekArc mSeekArc;
  43.     private SeekBar mRotation;
  44.     private SeekBar mStartAngle;
  45.     private SeekBar mSweepAngle;
  46.     private SeekBar mArcWidth;
  47.     private SeekBar mProgressWidth;
  48.     private CheckBox mRoundedEdges;
  49.     private CheckBox mTouchInside;
  50.     private CheckBox mClockwise;
  51.     private TextView mSeekArcProgress;
  52.     private TextView mSeekArcMinuteProgress;
  53.  
  54.  
  55.  
  56.     //will show the time
  57.     private TextView number_text;
  58.  
  59.     private TextView minute_text;
  60.  
  61.     private TextView little_hour_text2;
  62.     private TextView little_minute_text2;
  63.  
  64.  
  65.  
  66.     CountDownTimer countDownTimer;          // built in android class CountDownTimer
  67.     long totalTimeCountInMilliseconds;
  68.     long timeBlinkInMilliseconds;           // start time of start blinking
  69.     boolean blink;
  70.  
  71.  
  72.     SharedPreferences startimerPreferences;
  73.     SharedPreferences endTimerPreferences;
  74.     SharedPreferences endservice;
  75.  
  76.  
  77.     ArrayList<String> packagezList;
  78.     SharedPreferences sharedPrefsapp;
  79.     Map<String, ?> allEntries;
  80.  
  81.  
  82.     long timerstarted; //this is when the user hit start timer.
  83.     long timerends; //this is the time when the time when the timer will end;
  84.     long reopened; //this is when time when the user reopens the application;
  85.  
  86.  
  87.  
  88.     long newtotalTimeCountInMilliseconds;
  89.  
  90.  
  91.     // Consider showing drawable scrubber after pressing H M or S
  92.  
  93.  
  94.  
  95.  
  96.     CircleButton block_button1;
  97.     CircleButton start_timer;
  98.  
  99.  
  100.     int hourint, minuteint;
  101.  
  102.  
  103.     private static final int REQUEST_CODE = 0;
  104.     private DevicePolicyManager mDPM;
  105.     private ComponentName mAdminName;
  106.  
  107.     public boolean onCreateOptionsMenu(Menu menu){
  108.         MenuInflater inflater = getMenuInflater();
  109.         inflater.inflate(R.menu.main, menu);
  110.         return true;
  111.     }
  112.  
  113.  
  114.     @Override
  115.     protected void onResume()
  116.     {
  117.         super.onResume();
  118.  
  119.         try
  120.         {
  121.             // Initiate DevicePolicyManager.
  122.             mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
  123.             // Set DeviceAdminDemo Receiver for active the component with different option
  124.             mAdminName = new ComponentName(this, DeviceAdmin.class);
  125.  
  126.             if (!mDPM.isAdminActive(mAdminName)) {
  127.                 // try to become active
  128.                 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  129.                 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
  130.                 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click the Activate button to keep your selected apps locked for your set amount of time when you start the timer.\n " +
  131.                         "\nIt's time to GetDisciplined! ;)");
  132.                 startActivityForResult(intent, REQUEST_CODE);
  133.             }
  134.             else
  135.             {
  136.                 // Already is a device administrator, can do security operations now.
  137.                 //mDPM.lockNow();
  138.             }
  139.         } catch (Exception e)
  140.         {
  141.             e.printStackTrace();
  142.         }
  143.     }
  144.  
  145.  
  146.  
  147.     @Override
  148.     protected void onCreate(Bundle savedInstanceState) {
  149.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  150.         super.onCreate(savedInstanceState);
  151.         setTheme(R.style.Theme_Light_appalled);
  152.  
  153.         setContentView(R.layout.merge);
  154.  
  155.  
  156.  
  157.         try
  158.         {
  159.             // Initiate DevicePolicyManager.
  160.             mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
  161.             // Set DeviceAdminDemo Receiver for active the component with different option
  162.             mAdminName = new ComponentName(this, DeviceAdmin.class);
  163.  
  164.             if (!mDPM.isAdminActive(mAdminName)) {
  165.                 // try to become active
  166.                 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  167.                 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
  168.                 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click the Activate button to keep your selected apps locked for your set amount of time when you start the timer.\n " +
  169.                         "\nIt's time to GetDisciplined! ;)");
  170.                 startActivityForResult(intent, REQUEST_CODE);
  171.             }
  172.             else
  173.             {
  174.                 // Already is a device administrator, can do security operations now.
  175.                 //mDPM.lockNow();
  176.             }
  177.         } catch (Exception e)
  178.         {
  179.             e.printStackTrace();
  180.         }
  181.  
  182.  
  183.         // here is where the service is started.
  184.         //startService(new Intent(this, HeartBeat.class));
  185.  
  186. /*
  187.         Intent iHeartBeatService = new Intent(this, HeartBeat.class);
  188.         PendingIntent piHeartBeatService = PendingIntent.getService(this, 0, iHeartBeatService, PendingIntent.FLAG_UPDATE_CURRENT);
  189.         AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  190.         alarmManager.cancel(piHeartBeatService);
  191.         alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 300000, piHeartBeatService);
  192. */
  193.  
  194.  
  195.         Intent ishintent = new Intent(this, HeartBeat.class);
  196.         PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
  197.         AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  198.         alarm.cancel(pintent);
  199.         alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),150000, pintent);
  200.         getApplicationContext().startService(ishintent);
  201.  
  202.  
  203.  
  204.  
  205.  
  206.         //set views
  207.  
  208.         start_timer = (CircleButton) findViewById(R.id.start_button);
  209.         number_text = (TextView) findViewById(R.id.hour_progress_number);
  210.         minute_text = (TextView) findViewById(R.id.minute_progress_number);
  211.  
  212.  
  213.         //getReferenceOfViews ();                         // get all views
  214.         setActionListeners ();
  215.  
  216.         // This determine what the actual "countdown" time will be.
  217.         //totalTimeCountInMilliseconds = 60 * 1000;      // time count for 3 minutes = 180 seconds
  218.         //timeBlinkInMilliseconds = 30 * 1000;
  219.  
  220.         //Make sure you find out why it appears after a whole 1 second after the app appears
  221.         SpannableString s = new SpannableString("GetDisciplined");
  222.         s.setSpan(new TypefaceSpan(this, "miso.otf"), 0, s.length(),
  223.                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  224.  
  225. // Update the action bar title with the TypefaceSpan instance
  226.         ActionBar actionBar = getActionBar();
  227.         actionBar.setTitle(s);
  228.         // set the action bar in this activity as the home
  229.         actionBar.setHomeButtonEnabled(true);
  230.  
  231.         //Listeners for the buttons
  232.         addListenerOnButton();
  233.  
  234.  
  235.  
  236.         //Editing Button Text
  237.  
  238.  
  239.  
  240.         number_text = (TextView)findViewById(R.id.hour_progress_number);
  241.         number_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-thin.ttf"));
  242.  
  243.         minute_text = (TextView)findViewById(R.id.minute_progress_number);
  244.         minute_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-thin.ttf"));
  245.  
  246.  
  247.         little_hour_text2 = (TextView)findViewById(R.id.little_hour_text2);
  248.         little_hour_text2.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/miso.otf"));
  249.  
  250.         little_minute_text2 = (TextView)findViewById(R.id.little_minute_text2);
  251.         little_minute_text2.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/miso.otf"));
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.         mSeekArc = (SeekArc) findViewById(R.id.seekArc);
  264.  
  265.  
  266. //Here is the actual "hour progress number" aka the TextView that changes as the scrubber is dragged around
  267.         mSeekArcProgress = (TextView) findViewById(R.id.hour_progress_number);
  268.  
  269.         mSeekArcMinuteProgress = (TextView) findViewById(R.id.minute_progress_number);
  270.  
  271.  
  272.         startimerPreferences = getPreferences(MODE_APPEND);
  273.  
  274.         Date startDate = new Date(startimerPreferences.getLong("time", 0));
  275.         timerstarted = startDate.getTime();
  276.         Log.e("This is the start time!!!!!:  ", timerstarted + "");
  277.  
  278.  
  279.         endTimerPreferences = getPreferences(MODE_APPEND);
  280.         Date endDate = new Date(endTimerPreferences.getLong("endtime", 0));
  281.         timerends = endDate.getTime();
  282.         Log.e("This is the end time!!!!!:  ", timerends + "");
  283.  
  284.  
  285.         Date openagain = new Date(System.currentTimeMillis());
  286.         reopened = openagain.getTime();
  287.  
  288.  
  289.         //make textview selectable
  290.  
  291.         View.OnClickListener clickListener = new View.OnClickListener() {
  292.             @Override
  293.             public void onClick(View v) {
  294.  
  295.                 if((v.getId() == R.id.hour_progress_number)){
  296.                     number_text.setTextColor(getResources().getColor(R.color.black));
  297.                     little_hour_text2.setTextColor(getResources().getColor(R.color.black));
  298.  
  299.  
  300.  
  301.  
  302.  
  303.                     minute_text.setTextColor(getResources().getColor(R.color.red_highlight));
  304.                     little_minute_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.                     //corresponding button logic should below here
  312.                     mSeekArc.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
  313.  
  314.                         @Override
  315.                         public void onStopTrackingTouch(SeekArc seekArc) {
  316.                         }
  317.                         @Override
  318.                         public void onStartTrackingTouch(SeekArc seekArc) {
  319.                             number_text.setTextColor(getResources().getColor(R.color.black));
  320.                         }
  321.  
  322.  
  323.                         //This sets the actual string for the hours
  324.                         @Override
  325.                         public void onProgressChanged(SeekArc seekArc, int progress,
  326.                                                       boolean fromUser) {
  327.  
  328.                             int progress_count = 0;
  329.  
  330.                             for (int i=0;i<24;i=i+1)
  331.                             {
  332.  
  333.                                 if (progress ==120) {
  334.                                     mSeekArcProgress.setText("24");
  335.  
  336.                                 }
  337.                                 else if (progress == progress_count)
  338.                                 {
  339.                                     mSeekArcProgress.setText(String.valueOf(String.format("%02d",i)));
  340.                                 }
  341.  
  342.                                 progress_count = progress_count + 5;
  343.  
  344.                             }
  345.                         }
  346.                     });
  347.                 } else if((v.getId() == R.id.minute_progress_number)){
  348.  
  349.                     minute_text.setTextColor(getResources().getColor(R.color.black));
  350.                     little_minute_text2.setTextColor(getResources().getColor(R.color.black));
  351.  
  352.  
  353.  
  354.                     number_text.setTextColor(getResources().getColor(R.color.red_highlight));
  355.                     little_hour_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  356.  
  357.  
  358.  
  359.  
  360.  
  361.                     //corresponding button logic should below here
  362.  
  363.                     mSeekArc.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
  364.  
  365.  
  366.                         @Override
  367.                         public void onStopTrackingTouch(SeekArc seekArc) {
  368.                         }
  369.                         @Override
  370.                         public void onStartTrackingTouch(SeekArc seekArc) {
  371.                             minute_text.setTextColor(getResources().getColor(R.color.black));
  372.                         }
  373.  
  374.  
  375.                         //This sets the actual string for the minutes
  376.                         @Override
  377.                         public void onProgressChanged(SeekArc seekArc, int progress,
  378.                                                       boolean fromUser) {
  379.                             int progress_count = 0;
  380.  
  381.                             for (int i=0;i<120;i++)
  382.                             {
  383.  
  384.                                 if (progress ==120) {
  385.                                     mSeekArcMinuteProgress.setText("00");
  386.  
  387.  
  388.  
  389.  
  390.                                 }
  391.                                 else if (progress == progress_count)
  392.                                 {
  393.                                     mSeekArcMinuteProgress.setText(String.valueOf(String.format("%02d",i)));
  394.                                 }
  395.  
  396.                                 progress_count = progress_count + 2;
  397.  
  398.  
  399.                             }
  400.  
  401.                         }
  402.                     });
  403.                 }
  404.  
  405.             }
  406.         };
  407.  
  408.         findViewById(R.id.hour_progress_number).setOnClickListener(clickListener);
  409.         findViewById(R.id.minute_progress_number).setOnClickListener(clickListener);
  410.  
  411.  
  412.         findViewById(R.id.minute_progress_number).performClick();
  413.  
  414.  
  415.  
  416.         mRotation = (SeekBar) findViewById(R.id.rotation);
  417.         mStartAngle = (SeekBar) findViewById(R.id.startAngle);
  418.         mSweepAngle  = (SeekBar) findViewById(R.id.sweepAngle);
  419.         mArcWidth = (SeekBar) findViewById(R.id.arcWidth);
  420.         mProgressWidth = (SeekBar) findViewById(R.id.progressWidth);
  421.         mRoundedEdges = (CheckBox) findViewById(R.id.roundedEdges);
  422.         mTouchInside = (CheckBox) findViewById(R.id.touchInside);
  423.         mClockwise = (CheckBox) findViewById(R.id.clockwise);
  424.  
  425.         mRotation.setProgress(mSeekArc.getArcRotation());
  426.         mStartAngle.setProgress(mSeekArc.getStartAngle());
  427.         mSweepAngle.setProgress(mSeekArc.getSweepAngle());
  428.         mArcWidth.setProgress(mSeekArc.getArcWidth());
  429.         mProgressWidth.setProgress(mSeekArc.getProgressWidth());
  430.  
  431.  
  432.         mRotation.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  433.             @Override
  434.             public void onStopTrackingTouch(SeekBar arg0) {
  435.  
  436.             }
  437.             @Override
  438.             public void onStartTrackingTouch(SeekBar arg0) {
  439.             }
  440.  
  441.             @Override
  442.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  443.                 mSeekArc.setArcRotation(progress);
  444.                 mSeekArc.invalidate();
  445.             }
  446.         });
  447.  
  448.         mStartAngle.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  449.             @Override
  450.             public void onStopTrackingTouch(SeekBar arg0) {
  451.  
  452.             }
  453.             @Override
  454.             public void onStartTrackingTouch(SeekBar arg0) {
  455.             }
  456.  
  457.             @Override
  458.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  459.                 mSeekArc.setStartAngle(progress);
  460.                 mSeekArc.invalidate();
  461.             }
  462.         });
  463.  
  464.         mSweepAngle.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  465.             @Override
  466.             public void onStopTrackingTouch(SeekBar arg0) {
  467.  
  468.             }
  469.             @Override
  470.             public void onStartTrackingTouch(SeekBar arg0) {
  471.             }
  472.  
  473.             @Override
  474.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  475.                 mSeekArc.setSweepAngle(progress);
  476.                 mSeekArc.invalidate();
  477.             }
  478.         });
  479.  
  480.         mArcWidth.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  481.             @Override
  482.             public void onStopTrackingTouch(SeekBar arg0) {
  483.  
  484.             }
  485.             @Override
  486.             public void onStartTrackingTouch(SeekBar arg0) {
  487.             }
  488.  
  489.             @Override
  490.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  491.                 mSeekArc.setArcWidth(progress);
  492.                 mSeekArc.invalidate();
  493.             }
  494.         });
  495.  
  496.         mProgressWidth.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  497.             @Override
  498.             public void onStopTrackingTouch(SeekBar arg0) {
  499.  
  500.             }
  501.             @Override
  502.             public void onStartTrackingTouch(SeekBar arg0) {
  503.             }
  504.  
  505.             @Override
  506.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  507.                 mSeekArc.setProgressWidth(progress);
  508.                 mSeekArc.invalidate();
  509.             }
  510.         });
  511.  
  512.         mRoundedEdges.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  513.             @Override
  514.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  515.                 mSeekArc.setRoundedEdges(isChecked);
  516.                 mSeekArc.invalidate();
  517.             }
  518.         });
  519.  
  520.         mTouchInside.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  521.             @Override
  522.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  523.                 mSeekArc.setTouchInSide(false);
  524.             }
  525.         });
  526.  
  527.         mClockwise.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  528.             @Override
  529.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  530.                 mSeekArc.setClockwise(isChecked);
  531.                 mSeekArc.invalidate();
  532.             }
  533.         });
  534.  
  535.  
  536.         if(timerstarted > 0)
  537.         {
  538.             if(reopened <timerends){
  539.                 //start countdown timer with new time.
  540.                 //set countdowntime to timerends-reopen.
  541.  
  542.                 newtotalTimeCountInMilliseconds = timerends-reopened;
  543.  
  544.  
  545.                 number_text.setTextColor(getResources().getColor(R.color.red_highlight));
  546.                 little_hour_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  547.  
  548.  
  549.                 minute_text.setTextColor(getResources().getColor(R.color.red_highlight));
  550.                 little_minute_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  551.  
  552.  
  553.  
  554.  
  555.  
  556.                 // yo
  557.  
  558.                 number_text.setClickable(false);
  559.  
  560.                 minute_text.setClickable(false);
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.                 countDownTimer = new CountDownTimer(newtotalTimeCountInMilliseconds, 500) {
  568.                     // 500 means, onTick function will be called at every 500 milliseconds
  569.  
  570.                     @Override
  571.                     public void onTick(long leftTimeInMilliseconds) {
  572.  
  573.                         long seconds = leftTimeInMilliseconds / 1000;
  574.                         mSeekArc.setVisibility(View.INVISIBLE);
  575.                         start_timer.setVisibility(View.INVISIBLE);
  576.                         //block_button1.setVisibility(View.INVISIBLE);
  577.  
  578.  
  579.                         if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
  580.                             // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
  581.                             // change the style of the textview .. giving a red alert style
  582.  
  583.                             if (blink) {
  584.                                 number_text.setVisibility(View.VISIBLE);
  585.                                 minute_text.setVisibility(View.VISIBLE);
  586.  
  587.  
  588.                                 // if blink is true, textview will be visible
  589.                             } else {
  590.                                 number_text.setVisibility(View.INVISIBLE);
  591.                                 minute_text.setVisibility(View.INVISIBLE);
  592.  
  593.  
  594.                             }
  595.  
  596.                             blink = !blink;         // toggle the value of blink
  597.                         }
  598.  
  599.                         minute_text.setText(String.format("%02d", (seconds / 60) % 60));
  600.                         number_text.setText(String.format("%02d", seconds / 3600));                     // format the textview to show the easily readable format
  601.                     }
  602.  
  603.  
  604.                     @Override
  605.                     public void onFinish() {
  606.                         // this function will be called when the timecount is finished
  607.                         //textViewShowTime.setText("Time up!");
  608.                         number_text.setVisibility(View.VISIBLE);
  609.                         minute_text.setVisibility(View.VISIBLE);
  610.                         mSeekArc.setVisibility(View.VISIBLE);
  611.                         start_timer.setVisibility(View.VISIBLE);
  612.                         //block_button1.setVisibility(View.VISIBLE);
  613.  
  614.  
  615.  
  616.  
  617.                         number_text.setTextColor(getResources().getColor(R.color.red_highlight));
  618.  
  619.                         minute_text.setTextColor(getResources().getColor(R.color.red_highlight));
  620.  
  621.                         little_hour_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  622.  
  623.                         little_minute_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.                         number_text.setClickable(true);
  633.  
  634.                         minute_text.setClickable(true);
  635.  
  636.  
  637.  
  638.  
  639.  
  640.                     }
  641.  
  642.                 }.start();
  643.  
  644.  
  645.  
  646.             }
  647.         }
  648.  
  649.     }
  650.  
  651.  
  652.  
  653.  
  654.  
  655.     private static final long TIME_INTERVAL = 2000; // # milliseconds, desired time passed between two back presses.
  656.     private long mBackPressed;
  657.  
  658.     @Override
  659.     public void onBackPressed()
  660.     {
  661.         if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis())
  662.         {
  663.             super.onBackPressed();
  664.             return;
  665.         }
  666.         else { Toast.makeText(getBaseContext(), "Tap back button again to exit", Toast.LENGTH_SHORT).show(); }
  667.  
  668.         mBackPressed = System.currentTimeMillis();
  669.     }
  670.  
  671.     // for the on click activity responses for each of the 3 buttons on the menu
  672.     public void addListenerOnButton() {
  673.  
  674.         final Context context = this;
  675.  
  676.         block_button1 = (CircleButton) findViewById(R.id.block_button);
  677.  
  678.         block_button1.setOnClickListener(new OnClickListener() {
  679.  
  680.             @Override
  681.             public void onClick(View arg0) {
  682.  
  683.                 Intent intent = new Intent(context, InstalledAppActivity.class);
  684.                 startActivity(intent);
  685.  
  686.             }
  687.  
  688.         });
  689.  
  690.     }
  691.  
  692.  
  693.     private void setActionListeners() {
  694.  
  695.         number_text = (TextView) findViewById(R.id.hour_progress_number);
  696.         minute_text = (TextView) findViewById(R.id.minute_progress_number);
  697.  
  698.         start_timer.setOnClickListener(new View.OnClickListener() {
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.             @Override
  707.             public void onClick(View view) {
  708.  
  709.  
  710.  
  711.  
  712.  
  713.                 AlertDialog.Builder zeroerror = new AlertDialog.Builder(MainActivity.this)
  714.                         .setMessage("Dude, you didn't set a time! :P")
  715.                         .setNegativeButton("Whoops! My bad!", new DialogInterface.OnClickListener() {
  716.                             public void onClick(DialogInterface dialog, int which) {
  717.                                 Log.d("AlertDialog", "Negative");
  718.                                 dialog.cancel();
  719.                             }
  720.                         });
  721.  
  722.  
  723.                 AlertDialog alertzero = zeroerror.create();
  724.  
  725.  
  726.                 AlertDialog.Builder noapp = new AlertDialog.Builder(MainActivity.this)
  727.                         .setMessage("Hey silly! You didn't select any apps to block!")
  728.                         .setNegativeButton("Oh, silly me!", new DialogInterface.OnClickListener() {
  729.                             public void onClick(DialogInterface dialog, int which) {
  730.                                 Log.d("AlertDialog", "Negative");
  731.                                 dialog.cancel();
  732.                             }
  733.                         });
  734.  
  735.  
  736.                 AlertDialog zeroapp = noapp.create();
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.                 AlertDialog.Builder timeset = new AlertDialog.Builder(MainActivity.this)
  744.  
  745.  
  746.                         .setMessage("Are you sure you want to block the selected apps for the set amount of time?")
  747.                         .setPositiveButton("Yeah man!", new DialogInterface.OnClickListener() {
  748.  
  749.  
  750.  
  751.                             public void onClick(DialogInterface dialog, int which) {
  752.  
  753.                                 number_text.setTextColor(getResources().getColor(R.color.red_highlight));
  754.  
  755.                                 minute_text.setTextColor(getResources().getColor(R.color.red_highlight));
  756.  
  757.                                 little_hour_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  758.  
  759.                                 little_minute_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.                                 // yo
  767.  
  768.                                 number_text.setClickable(false);
  769.  
  770.                                 minute_text.setClickable(false);
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.                                 Log.d("AlertDialog", "Positive");
  778.  
  779.                                 hourint = Integer.valueOf(number_text.getText().toString());
  780.  
  781.                                 minuteint = Integer.valueOf(minute_text.getText().toString());
  782.  
  783.  
  784.                                 Log.i("YourActivity", "Hours: " + hourint);
  785.  
  786.                                 Log.i("YourActivity", "Minutes: " + minuteint);
  787.  
  788.  
  789.                                 //Make sure it stays alive no matter what until stopservice is called when the timer runs out
  790.  
  791.  
  792.                                 //Intent intent = new Intent(getApplicationContext(), HeartBeat.class);
  793.                                 //startService(intent);
  794.  
  795.                                 Date currenttime = new Date(System.currentTimeMillis());
  796.  
  797.                                 timerstarted = currenttime.getTime();
  798.                                 Log.e("This is the current time:  ", timerstarted + "");
  799.                                 startimerPreferences = getPreferences(MODE_APPEND);
  800.                                 SharedPreferences.Editor starteditor = startimerPreferences.edit();
  801.                                 starteditor.putLong("time", timerstarted);
  802.                                 starteditor.apply();
  803.  
  804.  
  805.                                 Date endtime = new Date(System.currentTimeMillis());
  806.  
  807.                                 //timerends = endtime.getTime() + (((hourint * 60 * 60) + (minuteint * 60) + (secondint)) * 1000);
  808.  
  809.                                 if ((((hourint * 60 * 60) + (minuteint * 60)  ) * 1000) > 0) {
  810.                                     timerends = endtime.getTime() + (((hourint * 60 * 60) + (minuteint * 60) ) * 1000);
  811.  
  812.  
  813.                                 } else {
  814.                                     timerends = 0;
  815.                                 }
  816.  
  817.  
  818.                                 Log.e("This is the end time:  ", timerends + "");
  819.                                 endTimerPreferences = getPreferences(MODE_APPEND);
  820.                                 SharedPreferences.Editor endeditor = endTimerPreferences.edit();
  821.                                 endeditor.putLong("endtime", timerends);
  822.                                 endeditor.apply();
  823.  
  824.                                 endservice = getApplicationContext().getSharedPreferences("endservice", Context.MODE_PRIVATE);
  825.                                 SharedPreferences.Editor serviceeditor = endservice.edit();
  826.                                 serviceeditor.putLong("endservice", timerstarted + (((hourint * 60 * 60) + (minuteint * 60) ) * 1000));
  827.                                 Log.e("Check out this time:  ", timerends + "");
  828.  
  829.                                 serviceeditor.apply();
  830.  
  831.  
  832.                                 totalTimeCountInMilliseconds = (((hourint * 60 * 60) + (minuteint * 60) ) * 1000);      // time count
  833.                                 timeBlinkInMilliseconds = 30 * 1000;
  834.  
  835.                                 countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
  836.                                     // 500 means, onTick function will be called at every 500 milliseconds
  837.  
  838.                                     @Override
  839.                                     public void onTick(long leftTimeInMilliseconds) {
  840.  
  841.                                         long seconds = leftTimeInMilliseconds / 1000;
  842.                                         mSeekArc.setVisibility(View.INVISIBLE);
  843.                                         start_timer.setVisibility(View.INVISIBLE);
  844.                                         //block_button1.setVisibility(View.INVISIBLE);
  845.  
  846.  
  847.                                         if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {
  848.                                             // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
  849.                                             // change the style of the textview .. giving a red alert style
  850.  
  851.                                             if (blink) {
  852.                                                 number_text.setVisibility(View.VISIBLE);
  853.                                                 minute_text.setVisibility(View.VISIBLE);
  854.  
  855.  
  856.                                                 // if blink is true, textview will be visible
  857.                                             } else {
  858.                                                 number_text.setVisibility(View.INVISIBLE);
  859.                                                 minute_text.setVisibility(View.INVISIBLE);
  860.  
  861.  
  862.                                             }
  863.  
  864.                                             blink = !blink;         // toggle the value of blink
  865.                                         }
  866.  
  867.                                         minute_text.setText(String.format("%02d", (seconds / 60) % 60));
  868.                                         number_text.setText(String.format("%02d", seconds / 3600));                     // format the textview to show the easily readable format
  869.                                     }
  870.  
  871.  
  872.                                     @Override
  873.                                     public void onFinish() {
  874.                                         // this function will be called when the timecount is finished
  875.                                         //textViewShowTime.setText("Time up!");
  876.                                         number_text.setVisibility(View.VISIBLE);
  877.                                         minute_text.setVisibility(View.VISIBLE);
  878.                                         mSeekArc.setVisibility(View.VISIBLE);
  879.                                         start_timer.setVisibility(View.VISIBLE);
  880.                                         //block_button1.setVisibility(View.VISIBLE);
  881.  
  882.  
  883.  
  884.  
  885.  
  886.  
  887.                                         number_text.setTextColor(getResources().getColor(R.color.red_highlight));
  888.  
  889.                                         minute_text.setTextColor(getResources().getColor(R.color.red_highlight));
  890.  
  891.                                         little_hour_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  892.  
  893.                                         little_minute_text2.setTextColor(getResources().getColor(R.color.red_highlight));
  894.  
  895.  
  896.  
  897.  
  898.  
  899.  
  900.  
  901.                                         number_text.setClickable(true);
  902.  
  903.                                         minute_text.setClickable(true);
  904.  
  905.  
  906.  
  907.  
  908.  
  909.                                         //Make sure it stops for good.
  910.  
  911.                                         //Intent intent = new Intent(getApplicationContext(), HeartBeat.class);
  912.                                         //stopService(intent);
  913.  
  914.  
  915.                                         NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  916.                                         Notification myNotification = new Notification(R.drawable.ic_launcher, "Time's up!", System.currentTimeMillis());
  917.                                         Context context = getApplicationContext();
  918.                                         String notificationTitle = "Apps are now unlocked!";
  919.                                         String notificationText = "Great job being productive! ;)";
  920.                                         Intent myIntent = new Intent(MainActivity.this, MainActivity.class);
  921.                                         PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,   myIntent, Intent.FILL_IN_ACTION);
  922.                                         myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
  923.                                         myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
  924.                                         notificationManager.notify(1, myNotification);
  925.  
  926.  
  927.                                     }
  928.  
  929.                                 }.start();
  930.                             }
  931.                         });
  932.                 timeset.setNegativeButton("Nope!", new DialogInterface.OnClickListener() {
  933.                     public void onClick(DialogInterface dialog, int which) {
  934.                         Log.d("AlertDialog", "Negative");
  935.                         dialog.cancel();
  936.                     }
  937.                 });
  938.  
  939.  
  940.                 AlertDialog timerright = timeset.create();
  941.  
  942.                 hourint = Integer.valueOf(number_text.getText().toString());
  943.  
  944.                 minuteint = Integer.valueOf(minute_text.getText().toString());
  945.  
  946.  
  947.  
  948.  
  949.                 sharedPrefsapp = getApplicationContext().getSharedPreferences("appdb", Context.MODE_PRIVATE);
  950.                 allEntries= null;
  951.                 allEntries = sharedPrefsapp.getAll();
  952.                 packagezList= null;
  953.  
  954.                 packagezList = new ArrayList<String>();
  955.  
  956.                 for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
  957.                     packagezList.add(entry.getKey());
  958.  
  959.                 }
  960.  
  961.  
  962.  
  963.                 if((((hourint * 60 * 60) + (minuteint * 60)  ) * 1000) == 0)
  964.                 {
  965.                     alertzero.show();
  966.                 }
  967.  
  968.                 else if (packagezList.size() ==0){
  969.                     noapp.show();
  970.                 }
  971.  
  972.  
  973.  
  974.                 else
  975.                 {
  976.                     timerright.show();
  977.                 }
  978.  
  979.                 // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);
  980.             }
  981.  
  982.  
  983.         });
  984.     }
  985.  
  986.  
  987. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement