Advertisement
nigatigga

Countdown ish

Aug 21st, 2014
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.41 KB | None | 0 0
  1. package com.spicycurryman.getdisciplined10.app;
  2.  
  3. import android.app.ActionBar;
  4. import android.app.AlertDialog;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.content.pm.ActivityInfo;
  10. import android.graphics.Typeface;
  11. import android.os.Bundle;
  12. import android.os.CountDownTimer;
  13. import android.support.v7.app.ActionBarActivity;
  14. import android.text.Spannable;
  15. import android.text.SpannableString;
  16. import android.util.Log;
  17. import android.view.Menu;
  18. import android.view.MenuInflater;
  19. import android.view.View;
  20. import android.view.View.OnClickListener;
  21. import android.widget.Button;
  22. import android.widget.CheckBox;
  23. import android.widget.CompoundButton;
  24. import android.widget.SeekBar;
  25. import android.widget.SeekBar.OnSeekBarChangeListener;
  26. import android.widget.TextView;
  27.  
  28. import com.triggertrap.seekarc.SeekArc;
  29.  
  30. import java.util.Date;
  31.  
  32. public class MainActivity extends ActionBarActivity {
  33.     private SeekArc mSeekArc;
  34.     private SeekBar mRotation;
  35.     private SeekBar mStartAngle;
  36.     private SeekBar mSweepAngle;
  37.     private SeekBar mArcWidth;
  38.     private SeekBar mProgressWidth;
  39.     private CheckBox mRoundedEdges;
  40.     private CheckBox mTouchInside;
  41.     private CheckBox mClockwise;
  42.     private TextView mSeekArcProgress;
  43.     private TextView mSeekArcMinuteProgress;
  44.     private TextView mSeekArcSecondProgress;
  45.  
  46.     private Button block_button_text;
  47.     private Button start_timer_text;
  48.  
  49.     //will show the time
  50.     private TextView number_text;
  51.  
  52.     private TextView minute_text;
  53.     private TextView second_text;
  54.  
  55.  
  56.     private TextView little_hour_text;
  57.     private TextView little_minute_text;
  58.     private TextView little_second_text;
  59.  
  60.     private TextView little_hour_text2;
  61.     private TextView little_minute_text2;
  62.     private TextView little_second_text2;
  63.  
  64.     CountDownTimer countDownTimer;          // built in android class CountDownTimer
  65.     long totalTimeCountInMilliseconds;
  66.     long timeBlinkInMilliseconds;           // start time of start blinking
  67.     boolean blink;
  68.  
  69.  
  70.     // Consider showing drawable scrubber after pressing H M or S
  71.  
  72.  
  73.  
  74.     SharedPreferences timerPreferences;
  75.     Button block_button1;
  76.     Button start_timer;
  77.  
  78.     int hourint, minuteint,secondint;
  79.  
  80.     public boolean onCreateOptionsMenu(Menu menu){
  81.         MenuInflater inflater = getMenuInflater();
  82.         inflater.inflate(R.menu.main, menu);
  83.         return true;
  84.     }
  85.  
  86.     View previousView;
  87.  
  88.  
  89.     @Override
  90.     protected void onCreate(Bundle savedInstanceState) {
  91.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  92.         super.onCreate(savedInstanceState);
  93.         setContentView(R.layout.merge);
  94.  
  95.  
  96.         //set views
  97.  
  98.         start_timer = (Button) findViewById(R.id.start_button);
  99.         number_text = (TextView) findViewById(R.id.hour_progress_number);
  100.         minute_text = (TextView) findViewById(R.id.minute_progress_number);
  101.         second_text = (TextView) findViewById(R.id.second_progress_number);
  102.  
  103.  
  104.         //getReferenceOfViews ();                         // get all views
  105.         setActionListeners ();
  106.  
  107.         // This determine what the actual "countdown" time will be.
  108.         //totalTimeCountInMilliseconds = 60 * 1000;      // time count for 3 minutes = 180 seconds
  109.         //timeBlinkInMilliseconds = 30 * 1000;
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.         //Make sure you find out why it appears after a whole 1 second after the app appears
  117.         SpannableString s = new SpannableString("GetDisciplined");
  118.         s.setSpan(new TypefaceSpan(this, "roboto-lightitalic.ttf"), 0, s.length(),
  119.                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  120.  
  121. // Update the action bar title with the TypefaceSpan instance
  122.         ActionBar actionBar = getActionBar();
  123.         actionBar.setTitle(s);
  124.         // set the action bar in this activity as the home
  125.         actionBar.setHomeButtonEnabled(true);
  126.  
  127.  
  128.  
  129.  
  130.         //Listeners for the buttons
  131.         addListenerOnButton();
  132.  
  133.  
  134.  
  135.         //Editing Button Text
  136.  
  137.         block_button_text = (Button)findViewById(R.id.block_button);
  138.         block_button_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/robotocondensed-bold.ttf"));
  139.  
  140.         start_timer_text = (Button)findViewById(R.id.start_button);
  141.         start_timer_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/robotocondensed-bold.ttf"));
  142.  
  143.         number_text = (TextView)findViewById(R.id.hour_progress_number);
  144.         number_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  145.  
  146.         minute_text = (TextView)findViewById(R.id.minute_progress_number);
  147.         minute_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  148.  
  149.         second_text = (TextView)findViewById(R.id.second_progress_number);
  150.         second_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  151.  
  152.         little_hour_text = (TextView)findViewById(R.id.hourtext);
  153.         little_hour_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  154.  
  155.         little_minute_text = (TextView)findViewById(R.id.minutetext);
  156.         little_minute_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  157.  
  158.         little_second_text = (TextView)findViewById(R.id.secondtext);
  159.         little_second_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  160.  
  161.         little_hour_text2 = (TextView)findViewById(R.id.little_hour_text2);
  162.         little_hour_text2.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  163.  
  164.         little_minute_text2 = (TextView)findViewById(R.id.little_minute_text2);
  165.         little_minute_text2.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  166.  
  167.         little_second_text2 = (TextView)findViewById(R.id.little_second_text2);
  168.         little_second_text2.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-light.ttf"));
  169.  
  170.  
  171.  
  172.  
  173.         mSeekArc = (SeekArc) findViewById(R.id.seekArc);
  174.  
  175.  
  176. //Here is the actual "hour progress number" aka the TextView that changes as the scrubber is dragged around
  177.         mSeekArcProgress = (TextView) findViewById(R.id.hour_progress_number);
  178.  
  179.         mSeekArcMinuteProgress = (TextView) findViewById(R.id.minute_progress_number);
  180.  
  181.         mSeekArcSecondProgress = (TextView) findViewById(R.id.second_progress_number);
  182.  
  183.  
  184.  
  185.  
  186.         Date newDate = new Date(timerPreferences.getLong("time", 0));
  187.         long newmillis = newDate.getTime();
  188.  
  189.  
  190.  
  191.  
  192.  
  193.         //make textview selectable
  194.  
  195.         View.OnClickListener clickListener = new View.OnClickListener() {
  196.             @Override
  197.             public void onClick(View v) {
  198.                 TextView previousText = (TextView) previousView;
  199.                 TextView curText = (TextView) v;
  200.                 // If the clicked view is selected, do nothing
  201.                 if (curText.isSelected()) {
  202.                     //curText.setSelected(false);
  203.                     //curText.setTextColor(getResources().getColor(R.color.red_highlight));
  204.                 } else { // If this isn't selected, deselect  the previous one (if any)
  205.                     if (previousText != null && previousText.isSelected()) {
  206.                         previousText.setSelected(false);
  207.                         previousText.setTextColor(getResources().getColor(R.color.red_highlight));
  208.                     }
  209.                     curText.setSelected(true);
  210.                     curText.setTextColor(getResources().getColor(R.color.black));
  211.                     previousView = v;
  212.                 }
  213.  
  214.                 if(v.getId() == R.id.hourtext){
  215.                     //corresponding button logic should below here
  216.                     mSeekArc.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
  217.  
  218.                         @Override
  219.                         public void onStopTrackingTouch(SeekArc seekArc) {
  220.                         }
  221.                         @Override
  222.                         public void onStartTrackingTouch(SeekArc seekArc) {
  223.                         }
  224.  
  225.  
  226.                         //This sets the actual string for the hours
  227.                         @Override
  228.                         public void onProgressChanged(SeekArc seekArc, int progress,
  229.                                                       boolean fromUser) {
  230.  
  231.                             int progress_count = 0;
  232.  
  233.                             for (int i=0;i<24;i=i+1)
  234.                             {
  235.  
  236.                                 if (progress ==120) {
  237.                                     mSeekArcProgress.setText("24");
  238.  
  239.                                 }
  240.                                 else if (progress == progress_count)
  241.                                 {
  242.                                     mSeekArcProgress.setText(String.valueOf(String.format("%02d",i)));
  243.                                 }
  244.  
  245.                                 progress_count = progress_count + 5;
  246.  
  247.                             }
  248.                         }
  249.                     });
  250.                 } else if (v.getId() == R.id.minutetext) {
  251.                     //corresponding button logic should below here
  252.  
  253.                     mSeekArc.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
  254.  
  255.  
  256.                         @Override
  257.                         public void onStopTrackingTouch(SeekArc seekArc) {
  258.                         }
  259.                         @Override
  260.                         public void onStartTrackingTouch(SeekArc seekArc) {
  261.                         }
  262.  
  263.  
  264.                         //This sets the actual string for the minutes
  265.                         @Override
  266.                         public void onProgressChanged(SeekArc seekArc, int progress,
  267.                                                       boolean fromUser) {
  268.                             int progress_count = 0;
  269.  
  270.                             for (int i=0;i<120;i++)
  271.                             {
  272.  
  273.                                  if (progress ==120) {
  274.                                      mSeekArcMinuteProgress.setText("00");
  275.  
  276.                                  }
  277.                                 else if (progress == progress_count)
  278.                                 {
  279.                                     mSeekArcMinuteProgress.setText(String.valueOf(String.format("%02d",i)));
  280.                                 }
  281.  
  282.                                 progress_count = progress_count + 2;
  283.  
  284.  
  285.                             }
  286.  
  287.                         }
  288.                     });
  289.                 } else if (v.getId() == R.id.secondtext) {
  290.                     //corresponding button logic should below here
  291.                     mSeekArc.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
  292.  
  293.                         @Override
  294.                         public void onStopTrackingTouch(SeekArc seekArc) {
  295.                         }
  296.                         @Override
  297.                         public void onStartTrackingTouch(SeekArc seekArc) {
  298.                         }
  299.  
  300.  
  301.                         //This sets the actual string for the seconds
  302.                         @Override
  303.                         public void onProgressChanged(SeekArc seekArc, int progress,
  304.                                       boolean fromUser) {
  305.  
  306.  
  307.  
  308.                             // so ur setting it to HALF of what "progress" is = to
  309.  
  310.                             int progress_count = 0;
  311.  
  312.                             for (int i=0;i<60;i++)
  313.                             {
  314.  
  315.  
  316.                                 if (progress ==120) {
  317.                                     mSeekArcSecondProgress.setText("00");
  318.  
  319.                                 }
  320.                                 else if (progress == progress_count)
  321.                                 {
  322.                                     mSeekArcSecondProgress.setText(String.valueOf(String.format("%02d",i)));
  323.                                 }
  324.  
  325.                                 progress_count = progress_count + 2;
  326.  
  327.  
  328.                             }
  329.  
  330.                         }
  331.                     });
  332.                 }
  333.  
  334.             }
  335.         };
  336.         findViewById(R.id.hourtext).setOnClickListener(clickListener);
  337.         findViewById(R.id.minutetext).setOnClickListener(clickListener);
  338.         findViewById(R.id.secondtext).setOnClickListener(clickListener);
  339.  
  340.  
  341.         mRotation = (SeekBar) findViewById(R.id.rotation);
  342.         mStartAngle = (SeekBar) findViewById(R.id.startAngle);
  343.         mSweepAngle  = (SeekBar) findViewById(R.id.sweepAngle);
  344.         mArcWidth = (SeekBar) findViewById(R.id.arcWidth);
  345.         mProgressWidth = (SeekBar) findViewById(R.id.progressWidth);
  346.         mRoundedEdges = (CheckBox) findViewById(R.id.roundedEdges);
  347.         mTouchInside = (CheckBox) findViewById(R.id.touchInside);
  348.         mClockwise = (CheckBox) findViewById(R.id.clockwise);
  349.  
  350.        mRotation.setProgress(mSeekArc.getArcRotation());
  351.        mStartAngle.setProgress(mSeekArc.getStartAngle());
  352.        mSweepAngle.setProgress(mSeekArc.getSweepAngle());
  353.        mArcWidth.setProgress(mSeekArc.getArcWidth());
  354.        mProgressWidth.setProgress(mSeekArc.getProgressWidth());
  355.  
  356.  
  357.         mRotation.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  358.             @Override
  359.             public void onStopTrackingTouch(SeekBar arg0) {
  360.  
  361.             }
  362.             @Override
  363.             public void onStartTrackingTouch(SeekBar arg0) {
  364.             }
  365.  
  366.             @Override
  367.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  368.                 mSeekArc.setArcRotation(progress);
  369.                 mSeekArc.invalidate();
  370.             }
  371.         });
  372.  
  373.         mStartAngle.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  374.             @Override
  375.             public void onStopTrackingTouch(SeekBar arg0) {
  376.  
  377.             }
  378.             @Override
  379.             public void onStartTrackingTouch(SeekBar arg0) {
  380.             }
  381.  
  382.             @Override
  383.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  384.                 mSeekArc.setStartAngle(progress);
  385.                 mSeekArc.invalidate();
  386.             }
  387.         });
  388.  
  389.         mSweepAngle.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  390.             @Override
  391.             public void onStopTrackingTouch(SeekBar arg0) {
  392.  
  393.             }
  394.             @Override
  395.             public void onStartTrackingTouch(SeekBar arg0) {
  396.             }
  397.  
  398.             @Override
  399.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  400.                 mSeekArc.setSweepAngle(progress);
  401.                 mSeekArc.invalidate();
  402.             }
  403.         });
  404.  
  405.         mArcWidth.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  406.             @Override
  407.             public void onStopTrackingTouch(SeekBar arg0) {
  408.  
  409.             }
  410.             @Override
  411.             public void onStartTrackingTouch(SeekBar arg0) {
  412.             }
  413.  
  414.             @Override
  415.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  416.                 mSeekArc.setArcWidth(progress);
  417.                 mSeekArc.invalidate();
  418.             }
  419.         });
  420.  
  421.         mProgressWidth.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  422.             @Override
  423.             public void onStopTrackingTouch(SeekBar arg0) {
  424.  
  425.             }
  426.             @Override
  427.             public void onStartTrackingTouch(SeekBar arg0) {
  428.             }
  429.  
  430.             @Override
  431.             public void onProgressChanged(SeekBar view, int progress, boolean fromUser) {
  432.                 mSeekArc.setProgressWidth(progress);
  433.                 mSeekArc.invalidate();
  434.             }
  435.         });
  436.  
  437.         mRoundedEdges.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  438.             @Override
  439.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  440.                 mSeekArc.setRoundedEdges(isChecked);
  441.                 mSeekArc.invalidate();
  442.             }
  443.         });
  444.  
  445.         mTouchInside.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  446.             @Override
  447.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  448.                 mSeekArc.setTouchInSide(false);
  449.             }
  450.         });
  451.  
  452.         mClockwise.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  453.             @Override
  454.             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
  455.                 mSeekArc.setClockwise(isChecked);
  456.                 mSeekArc.invalidate();
  457.             }
  458.         });
  459.  
  460.     }
  461.  
  462.     // for the on click activity responses for each of the 3 buttons on the menu
  463.     public void addListenerOnButton() {
  464.  
  465.         final Context context = this;
  466.  
  467.         block_button1 = (Button) findViewById(R.id.block_button);
  468.  
  469.         block_button1.setOnClickListener(new OnClickListener() {
  470.  
  471.             @Override
  472.             public void onClick(View arg0) {
  473.  
  474.                 Intent intent = new Intent(context, BlockActivity.class);
  475.                 startActivity(intent);
  476.  
  477.             }
  478.  
  479.         });
  480.  
  481.     }
  482.  
  483.  
  484.     private void setActionListeners() {
  485.  
  486.  
  487.  
  488.  
  489.  
  490.         number_text = (TextView) findViewById(R.id.hour_progress_number);
  491.         minute_text = (TextView) findViewById(R.id.minute_progress_number);
  492.         second_text = (TextView) findViewById(R.id.second_progress_number);
  493.  
  494.  
  495.  
  496.  
  497.         start_timer.setOnClickListener(new View.OnClickListener() {
  498.  
  499.  
  500.             @Override
  501.             public void onClick(View view) {
  502.  
  503.                 new AlertDialog.Builder( MainActivity.this )
  504.                         .setMessage( "Are you sure you want to block the selected apps for the set amount of time?" )
  505.                         .setPositiveButton( "Yeah man!", new DialogInterface.OnClickListener() {
  506.                             public void onClick(DialogInterface dialog, int which) {
  507.                                 Log.d( "AlertDialog", "Positive" );
  508.  
  509.  
  510.  
  511.                                 Date currentime = new Date(System.currentTimeMillis());
  512.  
  513.                                 long millicurrentime = currentime.getTime();
  514.                                 SharedPreferences timerPreferences = getPreferences(MODE_APPEND);
  515.                                 SharedPreferences.Editor editor = timerPreferences.edit();
  516.                                 editor.putLong("time", currentime.getTime());
  517.                                 editor.commit();
  518.  
  519.  
  520.  
  521.                                 hourint = Integer.valueOf(number_text.getText().toString());
  522.  
  523.                                 minuteint = Integer.valueOf(minute_text.getText().toString());
  524.  
  525.                                 secondint = Integer.valueOf(second_text.getText().toString());
  526.  
  527.                                 Log.i("YourActivity", "Hours: " + hourint);
  528.  
  529.                                 Log.i("YourActivity", "Minutes: " + minuteint);
  530.  
  531.                                 Log.i("YourActivity", "Seconds: " + secondint);
  532.  
  533.                                 totalTimeCountInMilliseconds = ((hourint*60*60) +(minuteint*60) + (secondint)) * 1000;      // time count
  534.                                 timeBlinkInMilliseconds = 30*1000;
  535.  
  536.  
  537.                                 long milliendtime = millicurrentime+totalTimeCountInMilliseconds;
  538.  
  539.  
  540.                                 countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
  541.                                     // 500 means, onTick function will be called at every 500 milliseconds
  542.  
  543.                                     @Override
  544.                                     public void onTick(long leftTimeInMilliseconds) {
  545.  
  546.                                         long seconds = leftTimeInMilliseconds / 1000;
  547.                                         mSeekArc.setVisibility(View.INVISIBLE);
  548.                                         start_timer.setVisibility(View.INVISIBLE);
  549.                                         block_button1.setVisibility(View.INVISIBLE);
  550.  
  551.  
  552.  
  553.                                         if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
  554.                                             // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
  555.                                             // change the style of the textview .. giving a red alert style
  556.  
  557.                                             if ( blink ) {
  558.                                                 number_text.setVisibility(View.VISIBLE);
  559.                                                 minute_text.setVisibility(View.VISIBLE);
  560.                                                 second_text.setVisibility(View.VISIBLE);
  561.  
  562.  
  563.                                                 // if blink is true, textview will be visible
  564.                                             } else {
  565.                                                 number_text.setVisibility(View.INVISIBLE);
  566.                                                 minute_text.setVisibility(View.INVISIBLE);
  567.                                                 second_text.setVisibility(View.INVISIBLE);
  568.  
  569.  
  570.                                             }
  571.  
  572.                                             blink = !blink;         // toggle the value of blink
  573.                                         }
  574.  
  575.                                         second_text.setText(String.format("%02d", seconds % 60));
  576.                                         minute_text.setText(String.format("%02d", (seconds / 60) % 60));
  577.                                         number_text.setText(String.format("%02d", seconds / 3600));                     // format the textview to show the easily readable format
  578.                                     }
  579.  
  580.  
  581.                                     @Override
  582.                                     public void onFinish() {
  583.                                         // this function will be called when the timecount is finished
  584.                                         //textViewShowTime.setText("Time up!");
  585.                                         number_text.setVisibility(View.VISIBLE);
  586.                                         minute_text.setVisibility(View.VISIBLE);
  587.                                         second_text.setVisibility(View.VISIBLE);
  588.                                         mSeekArc.setVisibility(View.VISIBLE);
  589.                                         start_timer.setVisibility(View.VISIBLE);
  590.                                         block_button1.setVisibility(View.VISIBLE);
  591.  
  592.  
  593.                                     }
  594.  
  595.                                 }.start();
  596.                             }
  597.                         })
  598.                         .setNegativeButton("Nope!", new DialogInterface.OnClickListener() {
  599.                             public void onClick(DialogInterface dialog, int which) {
  600.                                 Log.d("AlertDialog", "Negative");
  601.                                 dialog.cancel();
  602.                             }
  603.                         })
  604.                         .show();
  605.  
  606.  
  607.  
  608.                // textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);
  609.  
  610.  
  611.             }
  612.         });
  613.     }
  614.     private void getReferenceOfViews() {
  615.  
  616.         start_timer = (Button) findViewById(R.id.start_button);
  617.         number_text = (TextView) findViewById(R.id.hour_progress_number);
  618.         minute_text = (TextView) findViewById(R.id.minute_progress_number);
  619.         second_text = (TextView) findViewById(R.id.second_progress_number);
  620.  
  621.  
  622.     }
  623.  
  624. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement