-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
421 lines (315 loc) · 12.8 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package com.example.work.timetracker3;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.DropBoxManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.os.SystemClock;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import java.util.ArrayList;
import static android.R.color.white;
public class MainActivity extends AppCompatActivity implements OnClickListener {
Button goingToBed;
Button working;
Button relaxing;
Button eating;
Button napping;
Button other;
long workingTime = 0;
long relaxingTime = 0;
long eatingTime = 0;
long nappingTime = 0;
long otherTime = 0;
String category = "";
//to go to the next window after going to bed
ViewFlipper viewFlipper;
RelativeLayout relativeLayout;
LinearLayout switchLayout;
Display display;
long startTime;
//for the pie chart. we will add data into this later
private PieChart mChart;
ArrayList<Entry> yValues = new ArrayList<Entry>();
ArrayList<String> xValues = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
goingToBed = (Button) findViewById(R.id.goingToBed);
working = (Button) findViewById(R.id.working);
relaxing = (Button) findViewById(R.id.relaxing);
eating = (Button) findViewById(R.id.eating);
napping = (Button) findViewById(R.id.napping);
other = (Button) findViewById(R.id.other);
goingToBed.setOnClickListener(this);
working.setOnClickListener(this);
relaxing.setOnClickListener(this);
eating.setOnClickListener(this);
napping.setOnClickListener(this);
other.setOnClickListener(this);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
switchLayout = (LinearLayout) findViewById(R.id.switchLayout);
viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
display = getWindowManager().getDefaultDisplay();
//we need to get the time of the preference if there are any
long time = getTime();
category=getCategory();
workingTime = getCatTime("working");
relaxingTime = getCatTime("relaxing");
eatingTime= getCatTime("eating");
nappingTime = getCatTime("napping");
otherTime = getCatTime("other");
//if the preference is not set, make the start time (and the prefernce, the elpased time
if(time==0){
startTime= SystemClock.elapsedRealtime();
saveTime(startTime);
}
//if the preference is already set set the start time to that preference
else{
startTime = time;
}
//make the pie chart
//mChart = (PieChart)findViewById(R.id.mChart);
mChart = new PieChart(this);
mChart.setUsePercentValues(true);
mChart.setDescription("Shows the percentage of time doing certain activities");
//this enables hole and configuration
mChart.setDrawHoleEnabled(true);
mChart.setHoleRadius(7);
mChart.setTransparentCircleRadius(10);
//enable rotation of chart by touch
mChart.setRotationAngle(0);
mChart.setRotationEnabled(true);
//add data
}
//save the time
public void saveCatTime(long time, String categor){
//we want to save the startTime in case the app closes or crashes
SharedPreferences sharedPref = getSharedPreferences(categor, Context.MODE_PRIVATE);
//need to make an editor for the preferences
SharedPreferences.Editor editor = sharedPref.edit();
//putting a key and value for the preferences
editor.putLong(categor,time);
//apply
editor.apply();
}
//save the time
public void saveTime(long time){
//we want to save the startTime in case the app closes or crashes
SharedPreferences sharedPref = getSharedPreferences("time", Context.MODE_PRIVATE);
//need to make an editor for the preferences
SharedPreferences.Editor editor = sharedPref.edit();
//putting a key and value for the preferences
editor.putLong("time",time);
//apply
editor.apply();
}
//return the time that app was started
public long getCatTime(String categor){
SharedPreferences sharedPref = getSharedPreferences(categor, Context.MODE_PRIVATE);
long time = sharedPref.getLong(categor, 0);
return time;
}
//return the time that app was started
public long getTime(){
SharedPreferences sharedPref = getSharedPreferences("time", Context.MODE_PRIVATE);
long time = sharedPref.getLong("time", 0);
return time;
}
//save our current cateogry
public void saveCategory(String incomingCategory){
//we want to save the startTime in case the app closes or crashes
SharedPreferences sharedPref = getSharedPreferences("category", Context.MODE_PRIVATE);
//need to make an editor for the preferences
SharedPreferences.Editor editor = sharedPref.edit();
//putting a key and value for the preferences
editor.putString("category", incomingCategory);
//apply
editor.apply();
}
//give back the category we have saved
public String getCategory(){
SharedPreferences sharedPref = getSharedPreferences("category", Context.MODE_PRIVATE);
String savedCategory = sharedPref.getString("category", "");
return savedCategory;
}
private void addData(){
//create the chart
PieDataSet dataSet =new PieDataSet(yValues,"Shares");
dataSet.setSliceSpace(3); //space between the slices
dataSet.setSelectionShift(5); //sets the distance the slice is shifted away from the center
//add colors
ArrayList<Integer> colors = new ArrayList<Integer>();
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);
for (int c : ColorTemplate.PASTEL_COLORS) {
colors.add(c);
}
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
//instantiate pie chart
PieData data = new PieData(xValues,dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);
mChart.setData(data);
//undo all highlists for some reason
mChart.highlightValues(null);
//update the pie chart
mChart.invalidate();
}
private void goingToBed() {
//assing the last time
assignTime("");
//reset the time and category for the next time the app opens
saveTime(0);
saveCategory("");
//counter for how many categories we need to add
int counter = 0;
//create the textFlipper to show the statistics for the day
TextView tv1 = (TextView) findViewById(R.id.textView);
viewFlipper.showNext();
//calculate the total time
double totalTime = (double) (otherTime + nappingTime + relaxingTime + workingTime + eatingTime);
tv1.setText(Double.toString(totalTime));
float calculatedTime;
TextView viewer;
//setContentView(R.layout.layout);
//set the text and the pie chart, and if the time isnt equal to 0 add it to the chart
viewer = (TextView) findViewById(R.id.eating2);
calculatedTime = (float) Math.round(eatingTime / totalTime * 10000)/100;
viewer.setText("Eating: " + Float.toString(calculatedTime));
if(calculatedTime != 0) {
xValues.add("Eating");
yValues.add(new Entry( (calculatedTime), counter++));
}
viewer = (TextView) findViewById(R.id.napping2);
calculatedTime = (float) Math.round(nappingTime / totalTime * 10000)/100;
viewer.setText("Napping: " + Float.toString( calculatedTime));
if(calculatedTime != 0) {
xValues.add("Napping");
yValues.add(new Entry((calculatedTime), counter++));
}
viewer = (TextView) findViewById(R.id.other2);
calculatedTime = (float) Math.round(otherTime / totalTime * 10000)/100;
viewer.setText("Other: " + Float.toString( calculatedTime));
if(calculatedTime!=0) {
xValues.add("Other");
yValues.add(new Entry( (calculatedTime), counter++));
}
viewer = (TextView) findViewById(R.id.relaxing2);
calculatedTime = (float) Math.round(relaxingTime / totalTime * 10000)/100;
viewer.setText("Relaxing: " + Float.toString( calculatedTime));
if(calculatedTime!=0) {
xValues.add("Relaxing");
yValues.add(new Entry(calculatedTime, counter++));
}
viewer = (TextView) findViewById(R.id.working2);
calculatedTime = (float) Math.round(workingTime / totalTime * 10000)/100;
viewer.setText("Working: " + Float.toString( calculatedTime));
if(calculatedTime!=0) {
xValues.add("Working");
yValues.add(new Entry(calculatedTime, counter++));
}
viewer = (TextView) findViewById(R.id.total);
viewer.setText("Total Time: " + Double.toString(Math.round((otherTime + nappingTime + relaxingTime + workingTime + eatingTime)*100)/100));
//add chart data
addData();
//customize legends
Legend l = mChart.getLegend();
l.setEnabled(false);
//add the chart to the layout
switchLayout.addView(mChart);
ViewGroup.LayoutParams params = mChart.getLayoutParams();
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
//reset the time for all of the categories
saveCatTime(0,"eating");
saveCatTime(0,"napping");
saveCatTime(0,"other");
saveCatTime(0,"relaxing");
saveCatTime(0,"working");
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.eating:
assignTime("eating");
break;
case R.id.napping:
assignTime("napping");
break;
case R.id.other:
assignTime("other");
break;
case R.id.relaxing:
assignTime("relaxing");
break;
case R.id.working:
assignTime("working");
break;
case R.id.goingToBed:
goingToBed();
break;
}
}
public void assignTime(String nextCat){
switch(category){
case "napping":
nappingTime+=((SystemClock.elapsedRealtime() - startTime) / 1000);
saveCatTime(nappingTime,"napping");
break;
case "eating":
eatingTime+=((SystemClock.elapsedRealtime() - startTime) / 1000);
saveCatTime(eatingTime,"eating");
break;
case "other":
otherTime+=((SystemClock.elapsedRealtime() - startTime) / 1000);
saveCatTime(otherTime,"other");
break;
case "relaxing":
relaxingTime+=((SystemClock.elapsedRealtime() - startTime) / 1000);
saveCatTime(relaxingTime,"relaxing");
break;
case "working":
workingTime+=((SystemClock.elapsedRealtime() - startTime) / 1000);
saveCatTime(workingTime,"working");
break;
default:
break;
}
//set the category
category=nextCat;
saveCategory(nextCat);
//set the time
startTime = SystemClock.elapsedRealtime();
saveTime(startTime);
}
//committed
}