Advertisement
nigatigga

Untitled

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