11package com .peacecorps .pcsa ;
22
3- import android .Manifest ;
4- import android .content .DialogInterface ;
53import android .content .Intent ;
64import android .content .IntentFilter ;
75import android .content .SharedPreferences ;
8- import android .content .pm .PackageManager ;
9- import android .net .Uri ;
10- import android .os .Build ;
116import android .os .Bundle ;
127import android .preference .PreferenceManager ;
13- import android .provider .Settings ;
14- import android .support .annotation .NonNull ;
15- import android .support .annotation .RequiresApi ;
168import android .support .v4 .app .Fragment ;
179import android .support .v4 .app .FragmentActivity ;
1810import android .support .v4 .app .FragmentManager ;
1911import android .support .v4 .view .GravityCompat ;
2012import android .support .v4 .widget .DrawerLayout ;
2113import android .support .v7 .app .ActionBarDrawerToggle ;
22- import android .support .v7 .app .AlertDialog ;
2314import android .support .v7 .app .AppCompatActivity ;
2415import android .support .v7 .widget .Toolbar ;
2516import android .util .Log ;
5950public class MainActivity extends AppCompatActivity {
6051
6152 private static final String TAG = MainActivity .class .getSimpleName ();
62- private static final int PERMISSION_CALLBACK = 100 ;
63- private static final int PERMISSION_SETTINGS = 101 ;
6453 public static boolean refreshList = false ;
6554 public static String FRAGMENT_TAG = MainActivityFragment .TAG ;
6655 SharedPreferences sharedPreferences ;
@@ -73,8 +62,40 @@ public class MainActivity extends AppCompatActivity {
7362 private FragmentManager fragmentManager = getSupportFragmentManager ();
7463 private int lastExpandedGroup = -1 ;
7564 private int lastChildExpandedGroup = -1 ;
76- private String [] runTimePermissions = new String []{Manifest .permission .ACCESS_FINE_LOCATION , Manifest .permission .SEND_SMS , Manifest .permission .CALL_PHONE , Manifest .permission .READ_CONTACTS };
77-
65+
66+ public static void swapFragmentIn (FragmentActivity activity , Fragment fragment , String TAG , boolean addToBackStack ) {
67+ FragmentManager fragmentManager = activity .getSupportFragmentManager ();
68+ // Insert the fragment by replacing any existing fragment
69+ FRAGMENT_TAG = TAG ;
70+ int animEnter = R .anim .fade_in ;
71+ int animExit = R .anim .fade_out ;
72+ int animPopEnter = R .anim .fade_in_back ;
73+ int animPopExit = R .anim .fade_out_back ;
74+
75+ if (addToBackStack ) {
76+ if (TAG .equals (ContactOtherStaff .TAG )) {
77+ animEnter = R .anim .fade_in ;
78+ animExit = R .anim .fade_out ;
79+ animPopEnter = R .anim .fade_in_back ;
80+ animPopExit = R .anim .fade_out_back ;
81+ }
82+ fragmentManager .beginTransaction ()
83+ .setCustomAnimations (animEnter , animExit , animPopEnter , animPopExit )
84+ .replace (R .id .fragment_container
85+ , fragment , TAG )
86+ .addToBackStack (TAG )
87+ .commit ();
88+ } else {
89+ animEnter = R .anim .fade_in ;
90+ animExit = R .anim .fade_out ;
91+ fragmentManager .beginTransaction ()
92+ .setCustomAnimations (animEnter , animExit , animPopEnter , animPopExit )
93+ .replace (R .id .fragment_container
94+ , fragment , TAG )
95+ .commit ();
96+ }
97+ }
98+
7899 @ Override
79100 protected void onCreate (Bundle savedInstanceState ) {
80101 super .onCreate (savedInstanceState );
@@ -269,41 +290,6 @@ public boolean onChildClick(ExpandableListView parent, View v, int groupPosition
269290 });
270291 }
271292
272-
273- public static void swapFragmentIn (FragmentActivity activity , Fragment fragment , String TAG , boolean addToBackStack )
274- {
275- FragmentManager fragmentManager = activity .getSupportFragmentManager ();
276- // Insert the fragment by replacing any existing fragment
277- FRAGMENT_TAG = TAG ;
278- int animEnter = R .anim .fade_in ;
279- int animExit = R .anim .fade_out ;
280- int animPopEnter = R .anim .fade_in_back ;
281- int animPopExit = R .anim .fade_out_back ;
282-
283- if (addToBackStack ){
284- if (TAG .equals (ContactOtherStaff .TAG )) {
285- animEnter = R .anim .fade_in ;
286- animExit = R .anim .fade_out ;
287- animPopEnter = R .anim .fade_in_back ;
288- animPopExit = R .anim .fade_out_back ;
289- }
290- fragmentManager .beginTransaction ()
291- .setCustomAnimations (animEnter , animExit , animPopEnter , animPopExit )
292- .replace (R .id .fragment_container
293- , fragment ,TAG )
294- .addToBackStack (TAG )
295- .commit ();
296- } else {
297- animEnter = R .anim .fade_in ;
298- animExit = R .anim .fade_out ;
299- fragmentManager .beginTransaction ()
300- .setCustomAnimations (animEnter , animExit , animPopEnter , animPopExit )
301- .replace (R .id .fragment_container
302- , fragment ,TAG )
303- .commit ();
304- }
305- }
306-
307293 /**
308294 * Populating the expandable list of the Navigation Drawer
309295 */
@@ -385,90 +371,4 @@ public boolean onOptionsItemSelected(MenuItem item) {
385371 }
386372 return true ;
387373 }
388-
389- @ Override
390- protected void onStart () {
391- super .onStart ();
392- if (Build .VERSION .SDK_INT > Build .VERSION_CODES .LOLLIPOP_MR1 ) {
393- requestRunTimePermissions ();
394- }
395- }
396-
397- @ RequiresApi (23 )
398- @ Override
399- public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
400- super .onRequestPermissionsResult (requestCode , permissions , grantResults );
401- if (requestCode == PERMISSION_CALLBACK ) {
402- boolean showRationale = false ;
403- for (int i = 0 , len = permissions .length ; i < len ; i ++) {
404- if (grantResults [i ] == PackageManager .PERMISSION_DENIED ) {
405- showRationale = showRationale || shouldShowRequestPermissionRationale (permissions [i ]);
406- }
407- }
408- if (showRationale ) {
409- requestPermissions (permissions , PERMISSION_CALLBACK );
410- } else if (!areAllRunTimePermissionsGranted (permissions )) {
411- AlertDialog .Builder builder = new AlertDialog .Builder (this );
412- builder .setCancelable (false );
413- builder .setTitle (getString (R .string .permission_dialog_title ));
414- builder .setMessage (getString (R .string .permission_dialog_message ));
415- builder .setPositiveButton (getString (R .string .permission_dialog_positive_button ), new DialogInterface .OnClickListener () {
416- @ Override
417- public void onClick (DialogInterface dialog , int which ) {
418- dialog .cancel ();
419- Intent intent = new Intent (Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
420- Uri uri = Uri .fromParts ("package" , getPackageName (), null );
421- intent .setData (uri );
422- startActivityForResult (intent , PERMISSION_SETTINGS );
423- }
424- });
425- builder .setNegativeButton (getString (R .string .permission_dialog_negative_button ), new DialogInterface .OnClickListener () {
426- @ Override
427- public void onClick (DialogInterface dialog , int which ) {
428- dialog .cancel ();
429- finish ();
430- }
431- });
432- builder .show ();
433- }
434- }
435- }
436-
437- @ Override
438- protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
439- super .onActivityResult (requestCode , resultCode , data );
440- if (requestCode == PERMISSION_SETTINGS ) {
441- if (Build .VERSION .SDK_INT > Build .VERSION_CODES .LOLLIPOP_MR1 ) {
442- if (!areAllRunTimePermissionsGranted (runTimePermissions )) {
443- Toast .makeText (this , getString (R .string .permission_toast ), Toast .LENGTH_SHORT ).show ();
444- finish ();
445- }
446- }
447- }
448- }
449-
450- /**
451- * Requests run time permissions.
452- */
453- @ RequiresApi (23 )
454- private void requestRunTimePermissions () {
455- if (!areAllRunTimePermissionsGranted (runTimePermissions )) {
456- requestPermissions (runTimePermissions , PERMISSION_CALLBACK );
457- }
458- }
459-
460- /**
461- * Checks if all the run time permissions are granted.
462- *
463- * @param permissions required permissions.
464- * @return true if all permissions are granted.
465- */
466- @ RequiresApi (23 )
467- private boolean areAllRunTimePermissionsGranted (String [] permissions ) {
468- boolean allPermissionsGranted = true ;
469- for (String permission : permissions ) {
470- allPermissionsGranted = allPermissionsGranted && checkSelfPermission (permission ) == PackageManager .PERMISSION_GRANTED ;
471- }
472- return allPermissionsGranted ;
473- }
474374}
0 commit comments