1
1
package com .easyfitness ;
2
2
3
+ import android .Manifest ;
3
4
import android .app .AlertDialog ;
4
5
import android .content .DialogInterface ;
5
6
import android .content .Intent ;
6
7
import android .content .SharedPreferences ;
8
+ import android .content .pm .PackageManager ;
7
9
import android .content .res .Configuration ;
8
10
import android .os .Bundle ;
9
11
import android .os .Environment ;
10
12
import android .preference .PreferenceManager ;
13
+ import android .support .v4 .app .ActivityCompat ;
11
14
import android .support .v4 .app .Fragment ;
12
15
import android .support .v4 .app .FragmentManager ;
13
16
import android .support .v4 .app .FragmentTransaction ;
17
+ import android .support .v4 .content .ContextCompat ;
14
18
import android .support .v4 .widget .DrawerLayout ;
15
19
import android .support .v7 .app .ActionBarDrawerToggle ;
16
20
import android .support .v7 .app .AppCompatActivity ;
@@ -210,7 +214,7 @@ public void onCreate(Bundle savedInstanceState) {
210
214
211
215
dataList .add (drawerTitleItem );
212
216
dataList .add (new DrawerItem (this .getResources ().getString (R .string .menu_Workout ), R .drawable .ic_barbell , true ));
213
- dataList .add (new DrawerItem (this .getResources ().getString (R .string .CardioMenuLabel ), R .drawable .ic_running , true ));
217
+ // dataList.add(new DrawerItem(this.getResources().getString(R.string.CardioMenuLabel), R.drawable.ic_running, true));
214
218
dataList .add (new DrawerItem (this .getResources ().getString (R .string .MachinesLabel ), R .drawable .ic_machine , true ));
215
219
dataList .add (new DrawerItem (this .getResources ().getString (R .string .weightMenuLabel ), R .drawable .ic_scale , true ));
216
220
dataList .add (new DrawerItem (this .getResources ().getString (R .string .bodytracking ), R .drawable .ic_measuring_tape , true ));
@@ -412,6 +416,52 @@ public void onClick(DialogInterface dialog, int which) {
412
416
413
417
};
414
418
419
+ private final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1001 ;
420
+
421
+ private void exportDatabase () {
422
+ // Here, thisActivity is the current activity
423
+ if (ContextCompat .checkSelfPermission (this ,
424
+ Manifest .permission .WRITE_EXTERNAL_STORAGE )
425
+ != PackageManager .PERMISSION_GRANTED ) {
426
+ // No explanation needed; request the permission
427
+ ActivityCompat .requestPermissions (this ,
428
+ new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE },
429
+ MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE );
430
+ } else {
431
+ // Afficher une boite de dialogue pour confirmer
432
+ AlertDialog .Builder exportDbBuilder = new AlertDialog .Builder (this );
433
+
434
+ exportDbBuilder .setTitle (getActivity ().getResources ().getText (R .string .export_database ));
435
+ exportDbBuilder .setMessage (getActivity ().getResources ().getText (R .string .export_question ) + " " + getCurrentProfil ().getName () + "?" );
436
+
437
+ // Si oui, supprimer la base de donnee et refaire un Start.
438
+ exportDbBuilder .setPositiveButton (getActivity ().getResources ().getText (R .string .global_yes ), new DialogInterface .OnClickListener () {
439
+ public void onClick (DialogInterface dialog , int which ) {
440
+ CVSManager cvsMan = new CVSManager (getActivity ().getBaseContext ());
441
+ if (cvsMan .exportDatabase (getCurrentProfil ())) {
442
+ KToast .successToast (getActivity (), getCurrentProfil ().getName () + ": " + getActivity ().getResources ().getText (R .string .export_success ), Gravity .BOTTOM , KToast .LENGTH_LONG );
443
+ } else {
444
+ KToast .errorToast (getActivity (), getCurrentProfil ().getName () + ": " + getActivity ().getResources ().getText (R .string .export_failed ), Gravity .BOTTOM , KToast .LENGTH_LONG );
445
+ }
446
+
447
+ // Do nothing but close the dialog
448
+ dialog .dismiss ();
449
+ }
450
+ });
451
+
452
+ exportDbBuilder .setNegativeButton (getActivity ().getResources ().getText (R .string .global_no ), new DialogInterface .OnClickListener () {
453
+
454
+ @ Override
455
+ public void onClick (DialogInterface dialog , int which ) {
456
+ // Do nothing
457
+ dialog .dismiss ();
458
+ }
459
+ });
460
+
461
+ AlertDialog exportDbDialog = exportDbBuilder .create ();
462
+ exportDbDialog .show ();
463
+ }
464
+ }
415
465
416
466
@ Override
417
467
public boolean onOptionsItemSelected (MenuItem item ) {
@@ -424,39 +474,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
424
474
// Handle presses on the action bar items
425
475
switch (item .getItemId ()) {
426
476
case R .id .export_database :
427
- // Afficher une boite de dialogue pour confirmer
428
- AlertDialog .Builder exportDbBuilder = new AlertDialog .Builder (this );
429
-
430
- exportDbBuilder .setTitle (getActivity ().getResources ().getText (R .string .export_database ));
431
- exportDbBuilder .setMessage (getActivity ().getResources ().getText (R .string .export_question ) + " " + getCurrentProfil ().getName () + "?" );
432
-
433
- // Si oui, supprimer la base de donnee et refaire un Start.
434
- exportDbBuilder .setPositiveButton (getActivity ().getResources ().getText (R .string .global_yes ), new DialogInterface .OnClickListener () {
435
- public void onClick (DialogInterface dialog , int which ) {
436
- CVSManager cvsMan = new CVSManager (getActivity ().getBaseContext ());
437
- if (cvsMan .exportDatabase (getCurrentProfil ())) {
438
- KToast .successToast (getActivity (), getCurrentProfil ().getName () + ": " + getActivity ().getResources ().getText (R .string .export_success ), Gravity .BOTTOM , KToast .LENGTH_LONG );
439
- } else {
440
- KToast .errorToast (getActivity (), getCurrentProfil ().getName () + ": " + getActivity ().getResources ().getText (R .string .export_failed ), Gravity .BOTTOM , KToast .LENGTH_LONG );
441
- }
442
-
443
- // Do nothing but close the dialog
444
- dialog .dismiss ();
445
- }
446
- });
447
-
448
- exportDbBuilder .setNegativeButton (getActivity ().getResources ().getText (R .string .global_no ), new DialogInterface .OnClickListener () {
449
-
450
- @ Override
451
- public void onClick (DialogInterface dialog , int which ) {
452
- // Do nothing
453
- dialog .dismiss ();
454
- }
455
- });
456
-
457
- AlertDialog exportDbDialog = exportDbBuilder .create ();
458
- exportDbDialog .show ();
459
-
477
+ exportDatabase ();
460
478
return true ;
461
479
case R .id .import_database :
462
480
// Create DirectoryChooserDialog and register a callback
@@ -536,6 +554,27 @@ public void onClick(DialogInterface dialog, int which) {
536
554
}
537
555
}
538
556
557
+ @ Override
558
+ public void onRequestPermissionsResult (int requestCode ,
559
+ String [] permissions , int [] grantResults ) {
560
+ switch (requestCode ) {
561
+ case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE : {
562
+ // If request is cancelled, the result arrays are empty.
563
+ if (grantResults .length > 0
564
+ && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
565
+ KToast .infoToast (this , getString (R .string .access_granted ), Gravity .BOTTOM , KToast .LENGTH_SHORT );
566
+ exportDatabase ();
567
+ } else {
568
+ KToast .infoToast (this , getString (R .string .another_time_maybe ), Gravity .BOTTOM , KToast .LENGTH_SHORT );
569
+
570
+ }
571
+
572
+
573
+ }
574
+ }
575
+ }
576
+
577
+
539
578
public boolean CreateNewProfil () {
540
579
AlertDialog .Builder newProfilBuilder = new AlertDialog .Builder (this );
541
580
@@ -885,26 +924,22 @@ public void onItemClick(AdapterView parent, View view, int position, long id) {
885
924
setTitle (getResources ().getText (R .string .menu_Workout ));
886
925
break ;
887
926
case 2 :
888
- showFragment (CARDIO );
889
- setTitle (getResources ().getText (R .string .CardioLabel ));
890
- break ;
891
- case 3 :
892
927
showFragment (MACHINES );
893
928
setTitle (getResources ().getText (R .string .MachinesLabel ));
894
929
break ;
895
- case 4 :
930
+ case 3 :
896
931
showFragment (WEIGHT );
897
932
setTitle (getResources ().getText (R .string .weightMenuLabel ));
898
933
break ;
899
- case 5 :
934
+ case 4 :
900
935
showFragment (BODYTRACKING );
901
936
setTitle (getResources ().getText (R .string .bodytracking ));
902
937
break ;
903
- case 6 :
938
+ case 5 :
904
939
showFragment (SETTINGS );
905
940
setTitle (getResources ().getText (R .string .SettingLabel ));
906
941
break ;
907
- case 7 :
942
+ case 6 :
908
943
showFragment (ABOUT );
909
944
setTitle (getResources ().getText (R .string .AboutLabel ));
910
945
break ;
0 commit comments