Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.09 KB | None | 0 0
  1. package se.GameTimer;
  2.  
  3.  
  4. import java.util.*;
  5.  
  6. import se.GameTimer.Views.AddActivityView;
  7. import se.GameTimer.Views.EditActivitiesView;
  8. import se.GameTimer.Views.EditActivityView;
  9. import se.GameTimer.Views.LoginView;
  10. import se.GameTimer.Views.MainView;
  11. import se.GameTimer.Views.SetPassword;
  12. import se.GameTimer.database.SimpleDatabase;
  13. import se.GameTimer.login.Login;
  14. import android.app.Activity;
  15. import android.app.AlertDialog;
  16. import android.content.DialogInterface;
  17. import android.app.Notification;
  18. import android.app.NotificationManager;
  19. import android.app.PendingIntent;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.database.Cursor;
  23. import android.net.Uri;
  24. import android.os.Bundle;
  25.  
  26. import android.os.CountDownTimer;
  27. import android.os.Message;
  28. import android.widget.TextView;
  29. import android.os.Handler;
  30.  
  31. import android.util.Log;
  32. import android.view.Menu;
  33. import android.view.MenuInflater;
  34. import android.view.MenuItem;
  35. import android.view.View;
  36. import android.view.View.OnClickListener;
  37. import android.widget.AdapterView;
  38. import android.widget.ArrayAdapter;
  39. import android.widget.Button;
  40. import android.widget.Spinner;
  41. import android.widget.Toast;
  42. import android.widget.AdapterView.OnItemClickListener;
  43. import android.widget.AdapterView.OnItemSelectedListener;
  44.  
  45.  
  46. public class GameTimer extends Activity {
  47.  
  48. private Handler timerHandler;
  49.  
  50. private MainTimer mainTimer;
  51.  
  52. private Activities activities;
  53.  
  54. private SimpleDatabase db;
  55.  
  56. /**
  57. * Current chosen activity
  58. */
  59. private KidActivity currentActivity;
  60.  
  61. public final int DEFAULT_INITIAL_TIME = 1000;
  62. public KidActivity DEFAULT_INITIAL_ACTIVITY;
  63. private int totalTime;
  64.  
  65. private View currentView;
  66.  
  67. public static GameTimer ThisActivity;
  68.  
  69. /** Called when the activity is first created. */
  70. @Override
  71. public void onCreate(Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. ThisActivity = this;
  74. db = new SimpleDatabase(this);
  75. activities = new Activities(db);
  76. DEFAULT_INITIAL_ACTIVITY = activities.getActivities()[0];
  77. Login login = new Login(db);
  78. this.setInitialTime();
  79. this.setInitialActivity();
  80. if (login.isPasswordSet())
  81. this.setLoginView();
  82. else
  83. this.setPasswordView();
  84. }
  85.  
  86.  
  87. /**
  88. * Gets the MainTimer instance.
  89. */
  90. public MainTimer getMainTimer() {
  91. return this.mainTimer;
  92. }
  93.  
  94.  
  95. /**
  96. * Handle the onBackPressed events
  97. */
  98. @Override
  99. public void onBackPressed() {
  100. if (currentView instanceof AddActivityView ||
  101. currentView instanceof EditActivityView)
  102. setEditActivitiesView();
  103. else if (currentView instanceof EditActivitiesView){
  104. KidActivity temp = currentActivity;
  105. setMainView();
  106. ((MainView) currentView).getActivitySpinner().setSelection(activities.getPosition(temp, false));
  107. } else
  108. super.onBackPressed();
  109. }
  110.  
  111. /**
  112. * The menu that appears when you press menu-button
  113. */
  114. public boolean onCreateOptionsMenu(Menu menu) {
  115. MenuInflater inflater = getMenuInflater();
  116. inflater.inflate(R.menu.main_menu, menu);
  117. if (currentView instanceof LoginView || currentView instanceof SetPassword)
  118. return false;
  119. return true;
  120. }
  121.  
  122. /**
  123. * The handling of menu events
  124. */
  125. public boolean onOptionsItemSelected(MenuItem item) {
  126. // Handle item selection
  127. switch (item.getItemId()) {
  128. case R.id.edit_activities:
  129. setEditActivitiesView();
  130. return true;
  131. case R.id.set_time:
  132. // TODO - Pier, set to Set Time here!
  133. return true;
  134. default:
  135. return super.onOptionsItemSelected(item);
  136. }
  137. }
  138.  
  139. /**
  140. * Sets to the view were we see the activities as well as have the
  141. * option to add and edit them
  142. */
  143. public void setEditActivitiesView(){
  144. OnClickListener clickListener = new View.OnClickListener() {
  145. public void onClick(View v) {
  146. setAddActivityView();
  147. }
  148. };
  149. OnItemClickListener clickListener2 = new OnItemClickListener() {
  150. @Override
  151. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  152. long arg3) {
  153. if (currentActivity.getActivity().equals(activities.getActivities()[arg2].getActivity()) && mainTimer.isRunning()){
  154. AlertDialog ad = new AlertDialog.Builder(GameTimer.this).create();
  155. ad.setTitle("GameTimer");
  156. ad.setMessage("The activity is currently running. Please go back and stop it before changing it.");
  157. ad.setButton("OK", new DialogInterface.OnClickListener() {
  158. public void onClick(DialogInterface dialog, int which) {
  159. return;
  160. } });
  161. ad.show();
  162. } else
  163. setEditActivity(activities.getActivities()[arg2]);
  164. }
  165. };
  166. // TODO dfhfg
  167. EditActivitiesView eav = new EditActivitiesView(this);
  168. eav.setAddBtnListener(clickListener);
  169. eav.setListViewBtnListener(clickListener2);
  170. setContentView(eav);
  171. }
  172.  
  173. public void setEditActivity(KidActivity kd){
  174. EditActivityView aav = new EditActivityView(this);
  175. OnClickListener clickListener = new View.OnClickListener() {
  176. public void onClick(View v) {
  177. // Add activity here
  178. String actName = ((EditActivityView) currentView).getActivityName();
  179. int actWeight = ((EditActivityView) currentView).getActivityWeight();
  180. String oldActivityName = ((EditActivityView) currentView).getOldName();
  181. KidActivity kd2 = new KidActivity(actName,actWeight);
  182. if (actName == null || actName.trim() == ""){
  183. AlertDialog ad = new AlertDialog.Builder(GameTimer.this).create();
  184. ad.setTitle("GameTimer");
  185. ad.setMessage("You can not have an empty activity name!");
  186. ad.setButton("OK", new DialogInterface.OnClickListener() {
  187. public void onClick(DialogInterface dialog, int which) {
  188. return;
  189. } });
  190. ad.show();
  191. } else if (!actName.equals(oldActivityName) && activities.existsActivity(kd2)){
  192. AlertDialog ad = new AlertDialog.Builder(GameTimer.this).create();
  193. ad.setTitle("GameTimer");
  194. ad.setMessage("The activity name already exist!");
  195. ad.setButton("OK", new DialogInterface.OnClickListener() {
  196. public void onClick(DialogInterface dialog, int which) {
  197. return;
  198. } });
  199. ad.show();
  200. } else {
  201. Log.v("GameTimer", "Activity: " + actName + " was changed.");
  202. activities.changeActivity(oldActivityName, kd2);
  203. // If we changed the current activity
  204. if (currentActivity.getActivity().equals(oldActivityName))
  205. setCurrentActivity(kd2);
  206. setEditActivitiesView();
  207. }
  208. }
  209. };
  210. OnClickListener clickListener2 = new View.OnClickListener() {
  211. public void onClick(View v) {
  212. final String oldActivityName = ((EditActivityView) currentView).getOldName();
  213. AlertDialog.Builder builder = new AlertDialog.Builder(GameTimer.this);
  214. builder.setMessage("Are you sure you want to delete the activity "+ oldActivityName + "?")
  215. .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  216. public void onClick(DialogInterface dialog, int id) {
  217. activities.removeActivity(oldActivityName);
  218. setEditActivitiesView();
  219. }
  220. })
  221. .setNegativeButton("No", new DialogInterface.OnClickListener() {
  222. public void onClick(DialogInterface dialog, int id) {
  223. dialog.cancel();
  224. }
  225. });
  226. builder.create();
  227. builder.show();
  228. }
  229. };
  230. aav.setDeleteBtnListener(clickListener2);
  231. aav.setAddBtnListener(clickListener);
  232. aav.setNameText(kd.getActivity());
  233. aav.setSelectedActivity((int) kd.getWeight());
  234. setContentView(aav);
  235. }
  236.  
  237. public void setAddActivityView(){
  238. AddActivityView aav = new AddActivityView(this);
  239. OnClickListener clickListener = new View.OnClickListener() {
  240. public void onClick(View v) {
  241. // Add activity here
  242. String actName = ((AddActivityView) currentView).getActivityName();
  243. int actWeight = ((AddActivityView) currentView).getActivityWeight();
  244. if(!activities.addActivity(new KidActivity(actName,actWeight))){
  245. AlertDialog ad = new AlertDialog.Builder(GameTimer.this).create();
  246. ad.setTitle("GameTimer");
  247. ad.setMessage("Activity already exist!");
  248. ad.setButton("OK", new DialogInterface.OnClickListener() {
  249. public void onClick(DialogInterface dialog, int which) {
  250. return;
  251. } });
  252. ad.show();
  253. } else {
  254. Log.v("GameTimer", "Activity: " + actName + " was added.");
  255. setEditActivitiesView();
  256. }
  257. }
  258. };
  259. aav.setAddBtnListener(clickListener);
  260. setContentView(aav);
  261. }
  262.  
  263.  
  264.  
  265.  
  266. public void setMainView() {
  267. MainView v = new MainView(this,activities.getActivities(),new ChangeActivity());
  268. //currentActivity = activities.getActivities()[0];
  269. timerHandler = new tHandler();
  270. if (mainTimer == null)
  271. mainTimer = new MainTimer(timerHandler,this);
  272. OnClickListener btn_start_clickListener = new View.OnClickListener() {
  273. public void onClick(View v) {
  274. mainTimer.togglePause();
  275. if (mainTimer.isRunning()){
  276. ((MainView) currentView).getStartActivityButton().setText("Pause");
  277. ((MainView) currentView).getActivitySpinner().setEnabled(false);
  278. }else
  279. ((MainView) currentView).getStartActivityButton().setText("Start");
  280. }
  281. };
  282. OnClickListener btn_stop_clickListener = new View.OnClickListener() {
  283. public void onClick(View v) {
  284. mainTimer.stop();
  285. ((MainView) currentView).getStartActivityButton().setText("Start");
  286. ((MainView) currentView).setTimeLabel(mainTimer.getSeconds());
  287. ((MainView) currentView).getActivitySpinner().setEnabled(true);
  288. }
  289. };
  290. v.setStartActivityListener(btn_start_clickListener);
  291. v.setStopActivityListener(btn_stop_clickListener);
  292. setContentView(v);
  293. if (mainTimer.isRunning()){
  294. ((MainView) currentView).getStartActivityButton().setText("Pause");
  295. ((MainView) currentView).getActivitySpinner().setEnabled(false);
  296. }
  297. ((MainView) currentView).getActivitySpinner().setSelection(activities.getPosition(currentActivity, false));
  298. mainTimer.updateUI();
  299. }
  300.  
  301. public void setLoginView() {
  302. this.setContentView(new LoginView(this));
  303.  
  304. }
  305.  
  306. public void setPasswordView() {
  307. this.setContentView(new SetPassword(this));
  308. }
  309.  
  310.  
  311. /**
  312. * Override the setContentView method to store the view that is being set in the variable currentView.
  313. */
  314. @Override
  315. public void setContentView(View view) {
  316. super.setContentView(view);
  317. this.currentView = view;
  318. }
  319.  
  320.  
  321.  
  322. public KidActivity getCurrentActivity() {
  323. return currentActivity;
  324. }
  325.  
  326. public void setCurrentActivity(KidActivity ka){
  327. currentActivity = ka;
  328. }
  329.  
  330. public int getTotalTime() {
  331. return totalTime;
  332. }
  333.  
  334. public void setTotalTime(int totalTime) {
  335. this.totalTime = totalTime;
  336. }
  337.  
  338.  
  339.  
  340.  
  341. public void addTotalTime(int duration) {
  342. this.totalTime = totalTime + duration;
  343. }
  344.  
  345.  
  346.  
  347. public Activities getActivities() {
  348. return activities;
  349. }
  350.  
  351. final class tHandler extends Handler{
  352. @Override
  353. public void handleMessage(Message msg) {
  354. if (msg.what==-1){
  355.  
  356. // Dialog alarm
  357. GameTimer GT= new GameTimer();
  358. GT.TimeisUp();
  359.  
  360. } else {
  361. if (currentView instanceof MainView ){
  362. ((MainView) currentView).setTimeLabel(msg.what);
  363. }
  364. }
  365. }
  366. }
  367.  
  368.  
  369.  
  370. private class ChangeActivity implements OnItemSelectedListener{
  371. public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
  372. setCurrentActivity(activities.getActivities()[(int) (id)]);
  373. }
  374. public void onNothingSelected(AdapterView<?> parentView) {
  375. // your code here
  376. }
  377. }
  378.  
  379.  
  380. public void TimeisUp()
  381. {
  382. try
  383. {
  384. String ns = Context.NOTIFICATION_SERVICE;
  385. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  386.  
  387. int icon = R.drawable.icon;
  388. CharSequence tickerText = "Time is up!";
  389. long when = System.currentTimeMillis();
  390. Notification notification = new Notification(icon, tickerText, 200);
  391. Context context = getApplicationContext();
  392. CharSequence contentTitle = "My notification";
  393. CharSequence contentText = "Time Warning!";
  394. Intent notificationIntent = new Intent(this, GameTimer.class);
  395. PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
  396. notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
  397.  
  398. notification.ledARGB = 0xff00ff00;
  399. notification.ledOnMS = 300;
  400. notification.ledOffMS = 1000;
  401. notification.flags |= Notification.FLAG_SHOW_LIGHTS;
  402.  
  403. notification.defaults |= Notification.DEFAULT_SOUND;
  404. notification.vibrate = new long[] { 0, 300, 200, 300, 400, 300 };
  405.  
  406. final int HELLO_ID = 1;
  407. mNotificationManager.notify(HELLO_ID, notification);
  408. Toast.makeText(getBaseContext(), "Time is up!",Toast.LENGTH_LONG).show();
  409. }
  410. catch(Exception e){
  411. Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_LONG).show();
  412. }
  413.  
  414. }
  415.  
  416. @Override
  417. public void onDestroy() {
  418. super.onDestroy();
  419. this.updateInitialTime();
  420. this.updateInitialActivity();
  421. this.getDB().close();
  422. }
  423.  
  424. public boolean isCurrentBalanceTimeSet() {
  425. Cursor c = this.getDB().selectQuery("SELECT * FROM " +
  426. SimpleDatabase.getDatatable() +
  427. " WHERE name = 'currentBalanceTime'");
  428. return (c.getCount() > 0);
  429. }
  430.  
  431.  
  432. public void updateInitialTime() {
  433. this.getDB().executeQuery("UPDATE " + SimpleDatabase.getDatatable() +
  434. " SET value = '" + this.getTotalTime() + "' WHERE name='currentBalanceTime';");
  435. }
  436.  
  437. public void setInitialTime() {
  438. if (this.isCurrentBalanceTimeSet()) {
  439. Cursor c = this.getDB().selectQuery("SELECT value FROM " +
  440. SimpleDatabase.getDatatable() +
  441. " WHERE name = 'currentBalanceTime'");
  442. if (c.moveToFirst()) this.setTotalTime(c.getInt(0));
  443. else /* any problem */ this.setTotalTime(this.DEFAULT_INITIAL_TIME);
  444. } else {
  445. this.setTotalTime(this.DEFAULT_INITIAL_TIME);
  446. this.getDB().executeQuery("INSERT INTO " + SimpleDatabase.getDatatable() +
  447. " (name, value) VALUES ('currentBalanceTime', '" +
  448. this.DEFAULT_INITIAL_TIME + "');");
  449. }
  450.  
  451. }
  452.  
  453. public void updateInitialActivity() {
  454. this.getDB().executeQuery("UPDATE " + SimpleDatabase.getDatatable() +
  455. " SET value = '" + this.getCurrentActivity() + "' WHERE name='currentActivity';");
  456. }
  457.  
  458. public void setInitialActivity() {
  459. if (this.isCurrentActivitySet()) {
  460. Cursor c = this.getDB().selectQuery("SELECT value FROM " +
  461. SimpleDatabase.getDatatable() +
  462. " WHERE name = 'currentActivity'");
  463. if (c.moveToFirst())
  464. this.setCurrentActivity(activities.getActivity(c.getString(0)));
  465. else /* any problem */ this.setCurrentActivity(DEFAULT_INITIAL_ACTIVITY);
  466. } else {
  467. this.setCurrentActivity(DEFAULT_INITIAL_ACTIVITY);
  468. this.getDB().executeQuery("INSERT INTO " + SimpleDatabase.getDatatable() +
  469. " (name, value) VALUES ('currentActivity', '" +
  470. DEFAULT_INITIAL_ACTIVITY.getActivity() + "');");
  471. }
  472. }
  473.  
  474. public boolean isCurrentActivitySet() {
  475. Cursor c = this.getDB().selectQuery("SELECT * FROM " +
  476. SimpleDatabase.getDatatable() +
  477. " WHERE name = 'currentActivity'");
  478. return (c.getCount() > 0);
  479. }
  480.  
  481. public SimpleDatabase getDB() { return this.db;}
  482.  
  483.  
  484.  
  485. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement