1
1
/*
2
2
* @file MainActivity.java
3
+ * @author Holger Mueller
3
4
*
4
5
* Main activity of JobLogTimer
5
6
*
9
10
package de .euhm .jlt ;
10
11
11
12
import android .Manifest ;
13
+ import android .annotation .SuppressLint ;
12
14
import android .annotation .TargetApi ;
13
15
import android .app .AlertDialog ;
14
16
import android .content .BroadcastReceiver ;
27
29
import android .view .MenuItem ;
28
30
import android .view .MotionEvent ;
29
31
import android .view .View ;
32
+ import android .widget .CheckBox ;
30
33
import android .widget .ImageView ;
31
34
import android .widget .TextView ;
32
35
import android .widget .Toast ;
33
36
34
37
import androidx .annotation .NonNull ;
38
+ import androidx .appcompat .app .ActionBar ;
35
39
import androidx .appcompat .app .ActionBarDrawerToggle ;
36
40
import androidx .appcompat .app .AppCompatActivity ;
37
41
import androidx .appcompat .widget .Toolbar ;
@@ -164,7 +168,9 @@ public void onCreate(Bundle savedInstanceState) {
164
168
registerReceiver (alarmReceiver , new IntentFilter (Constants .RECEIVER_MAX_WORK_ALARM ));
165
169
166
170
// set path and name of backup database used by db export and import
167
- mBackupDbPath =
171
+ // TODO: set to Android/data/de.euhm.jlt/files to avoid permission requests
172
+ //mBackupDbPath = getExternalFilesDir(null).getAbsolutePath() + File.separatorChar + JobLogContract.DATABASE_NAME;
173
+ mBackupDbPath =
168
174
Environment .getExternalStorageDirectory ().getAbsolutePath () +
169
175
File .separatorChar + getResources ().getString (R .string .app_name ) +
170
176
File .separatorChar + JobLogContract .DATABASE_NAME ;
@@ -174,7 +180,7 @@ public void onCreate(Bundle savedInstanceState) {
174
180
// initialize preferences with default settings
175
181
PreferenceManager .setDefaultValues (this , R .xml .preferences , false );
176
182
} else {
177
- if (mTimes == null ) mTimes = new Times (0 , 0 , 0 );
183
+ if (mTimes == null ) mTimes = new Times (0 , 0 , 0 , false );
178
184
mTimes .loadInstanceState (savedInstanceState );
179
185
if (mTimes .getId () == 0 && mTimes .getTimeStart () == 0 && mTimes .getTimeEnd () == 0 ) {
180
186
// no data loaded, remove mTimes
@@ -194,7 +200,7 @@ public void onCreate(Bundle savedInstanceState) {
194
200
public void onClick (View v ) {
195
201
// prepare to add a new SQL row
196
202
long currentTime = TimeUtil .getCurrentTimeInMillis ();
197
- Times times = new Times (-1 , currentTime , currentTime );
203
+ Times times = new Times (-1 , currentTime , currentTime , false );
198
204
199
205
// prepare to edit entry
200
206
EditTimesFragment frag = new EditTimesFragment ();
@@ -212,7 +218,10 @@ public void onClick(View v) {
212
218
setSupportActionBar (supportToolbar );
213
219
214
220
// Specify that the Home/Up button should not be enabled, since there is no hierarchical parent.
215
- getSupportActionBar ().setHomeButtonEnabled (false );
221
+ ActionBar actionBar = getSupportActionBar ();
222
+ if (actionBar != null ) {
223
+ actionBar .setHomeButtonEnabled (false );
224
+ }
216
225
217
226
// Setup the ViewPager.
218
227
mViewPager = findViewById (R .id .pager );
@@ -269,6 +278,7 @@ public void onTabReselected(TabLayout.Tab tab) {
269
278
Tab tab = tabLayout .newTab ();
270
279
tab .setCustomView (R .layout .custom_tab_title );
271
280
View tab_view = tab .getCustomView ();
281
+ assert tab_view != null ;
272
282
TextView tab_title = tab_view .findViewById (R .id .title );
273
283
ImageView img = tab_view .findViewById (R .id .icon );
274
284
tab_title .setText (mAppSectionsPagerAdapter .getPageTitle (i ));
@@ -298,7 +308,7 @@ public void onDrawerStateChanged(int newState) {
298
308
}
299
309
300
310
301
- /* ******************************************************************
311
+ /* ******************************************************************
302
312
* FragmentPagerAdapter to handle tabs
303
313
* ****************************************************************** */
304
314
@@ -308,14 +318,15 @@ public void onDrawerStateChanged(int newState) {
308
318
*/
309
319
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
310
320
311
- private Context mContext ;
321
+ private final Context mContext ;
312
322
313
323
public AppSectionsPagerAdapter (FragmentManager fm , Context context ) {
314
324
super (fm );
315
325
mContext = context ;
316
326
}
317
327
318
328
@ Override
329
+ @ NonNull
319
330
public Fragment getItem (int position ) {
320
331
switch (position ) {
321
332
case 0 : // Main tab
@@ -389,14 +400,11 @@ public boolean onPrepareOptionsMenu(Menu menu) {
389
400
menu .findItem (R .id .action_end ).setVisible (false );
390
401
}
391
402
// show special menu entries only on view tab
392
- if (mViewPager .getCurrentItem () == 1 ) {
393
- menu .findItem (R .id .action_filter ).setVisible (true );
394
- } else {
395
- menu .findItem (R .id .action_filter ).setVisible (false );
396
- }
403
+ menu .findItem (R .id .action_filter ).setVisible (mViewPager .getCurrentItem () == 1 );
397
404
return super .onPrepareOptionsMenu (menu );
398
405
}
399
406
407
+ @ SuppressLint ("NonConstantResourceId" )
400
408
@ Override
401
409
public boolean onOptionsItemSelected (MenuItem item ) {
402
410
// Handle action bar item clicks here. The action bar will
@@ -406,9 +414,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
406
414
// Handle presses on the action bar items
407
415
switch (id ) {
408
416
case R .id .action_start :
409
- // make the broadcast Intent explicit by specifying the receiver class
410
- sendBroadcast (new Intent (Constants .RECEIVER_START_STOP , null , this , StartStopReceiver .class ));
411
- return true ;
412
417
case R .id .action_end :
413
418
// make the broadcast Intent explicit by specifying the receiver class
414
419
sendBroadcast (new Intent (Constants .RECEIVER_START_STOP , null , this , StartStopReceiver .class ));
@@ -449,6 +454,7 @@ public boolean onKeyUp(int keycode, KeyEvent event) {
449
454
* Navigation drawer menu stuff (onNavigationItemSelected)
450
455
* ****************************************************************** */
451
456
457
+ @ SuppressLint ("NonConstantResourceId" )
452
458
@ TargetApi (Build .VERSION_CODES .JELLY_BEAN )
453
459
@ Override
454
460
public boolean onNavigationItemSelected (@ NonNull MenuItem item ) {
@@ -461,7 +467,7 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
461
467
case R .id .nav_item_add_times :
462
468
// prepare to add a new SQL row
463
469
long currentTime = TimeUtil .getCurrentTimeInMillis ();
464
- Times times = new Times (-1 , currentTime , currentTime );
470
+ Times times = new Times (-1 , currentTime , currentTime , false );
465
471
466
472
// prepare to edit entry
467
473
EditTimesFragment frag = new EditTimesFragment ();
@@ -476,7 +482,7 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
476
482
// backup internal database to external file
477
483
// check if we have permission to write external storage
478
484
if (ContextCompat .checkSelfPermission (this ,
479
- Manifest .permission .WRITE_EXTERNAL_STORAGE ) !=
485
+ Manifest .permission .WRITE_EXTERNAL_STORAGE ) !=
480
486
PackageManager .PERMISSION_GRANTED ) {
481
487
// permission is not granted, ask for permission and wait for
482
488
// callback method onRequestPermissionsResult gets the result of the request.
@@ -511,7 +517,7 @@ public void onClick(DialogInterface dialog, int id) {
511
517
} else {
512
518
// check if we have permission to read from external storage
513
519
if (ContextCompat .checkSelfPermission (MainActivity .this ,
514
- Manifest .permission .READ_EXTERNAL_STORAGE ) !=
520
+ Manifest .permission .READ_EXTERNAL_STORAGE ) !=
515
521
PackageManager .PERMISSION_GRANTED ) {
516
522
// permission is not granted, ask for permission and wait for
517
523
// callback method onRequestPermissionsResult gets the result of the request.
@@ -693,6 +699,24 @@ public void onFinishFilterFragment(int id, int month, int year) {
693
699
}
694
700
}
695
701
702
+ public void onCheckboxClicked (View view ) {
703
+ // Check which checkbox was clicked
704
+ switch (view .getId ()) {
705
+ case R .id .homeoffice_cb :
706
+ // save the Home Office check box into mTW dataset
707
+ if (mTW .getWorkStarted ()) {
708
+ mTW .setHomeOffice (((CheckBox ) view ).isChecked ());
709
+ mMainSectionFragment .updateTimesView ();
710
+ } else {
711
+ ((CheckBox ) view ).toggle ();
712
+ Toast .makeText (this , R .string .work_not_started , Toast .LENGTH_LONG ).show ();
713
+ }
714
+ break ;
715
+ default :
716
+ throw new IllegalStateException ("Unexpected value: " + view .getId ());
717
+ }
718
+ }
719
+
696
720
public void showDatePickerDialog (View v ) {
697
721
if (mTW .getWorkStarted ()) {
698
722
// do not set mTW.timeStart to current time, otherwise we can not cancel ...
@@ -732,6 +756,7 @@ public void showTimePickerDialog_EndTime(View v) {
732
756
}
733
757
}
734
758
759
+ @ SuppressLint ("NonConstantResourceId" )
735
760
@ Override
736
761
public void onFinishTimePickerFragment (Calendar cal , int titleId ) {
737
762
// set time depending on titleId
@@ -756,6 +781,7 @@ public void onFinishTimePickerFragment(Calendar cal, int titleId) {
756
781
AlarmUtils .setAlarms (this , mTW );
757
782
}
758
783
784
+ @ SuppressLint ("NonConstantResourceId" )
759
785
@ Override
760
786
public void onFinishDatePickerFragment (Calendar cal , int titleId ) {
761
787
// set time depending on titleId
@@ -771,6 +797,8 @@ public void onFinishDatePickerFragment(Calendar cal, int titleId) {
771
797
mTW .setCalEnd (calTimeEnd );
772
798
}
773
799
break ;
800
+ default :
801
+ throw new IllegalStateException ("Unexpected value: " + titleId );
774
802
}
775
803
// update view and alarms ...
776
804
mMainSectionFragment .updateTimesView ();
@@ -782,6 +810,7 @@ public void onFinishDatePickerFragment(Calendar cal, int titleId) {
782
810
public void onRequestPermissionsResult (int requestCode ,
783
811
@ NonNull String [] permissions ,
784
812
@ NonNull int [] grantResults ) {
813
+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
785
814
// Handle the permissions request response.
786
815
switch (requestCode ) {
787
816
case Constants .PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE :
@@ -800,12 +829,12 @@ public void onRequestPermissionsResult(int requestCode,
800
829
mViewSectionFragment .importDatabase (mBackupDbPath );
801
830
}
802
831
break ;
803
- }
832
+ }
804
833
}
805
834
806
835
807
836
@ Override
808
- public void onSaveInstanceState (Bundle savedInstanceState ) {
837
+ public void onSaveInstanceState (@ NonNull Bundle savedInstanceState ) {
809
838
// Always call the superclass so it can save the view hierarchy state
810
839
super .onSaveInstanceState (savedInstanceState );
811
840
if (mTimes != null ) {
0 commit comments