Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. public class Utility {
  2.  
  3. public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
  4. public static final int MY_PERMISSIONS_PHONE_CALL = 123;
  5.  
  6. @TargetApi(Build.VERSION_CODES.M)
  7. public static boolean checkPermission(final Context context) {
  8. int currentAPIVersion = Build.VERSION.SDK_INT;
  9. if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
  10. if (ContextCompat.checkSelfPermission(context,
  11.  
  12. Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
  13. if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
  14.  
  15. // show a dialog
  16.  
  17. }).show();
  18. }
  19. else {
  20. ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
  21. }
  22. return false;
  23. }
  24.  
  25. if (ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
  26. if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.CALL_PHONE)) {
  27. Log.v("TAG", "Permission is granted");
  28. new AlertDialog.Builder(context).setMessage("You need to enable permissions to use this feature").setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
  29. @Override
  30. public void onClick(DialogInterface dialog, int which) {
  31. // navigate to settings
  32. ((Activity) context).startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
  33. }
  34. }).setNegativeButton("Go back", new DialogInterface.OnClickListener() {
  35. @Override
  36. public void onClick(DialogInterface dialog, int which) {
  37. // leave?
  38. ((Activity) context).onBackPressed();
  39. }
  40. }).show();
  41. }
  42. else {
  43. ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_PHONE_CALL);
  44. }
  45. return false;
  46. } else {
  47. return true;
  48. }
  49.  
  50. } else {
  51. return true;
  52. }
  53. }
  54. }
  55.  
  56. public class SecondAlertDialogFragment extends DialogFragment {
  57.  
  58.  
  59. @Override
  60. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  61. Bundle args) {
  62.  
  63. .......
  64.  
  65.  
  66. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  67.  
  68. @Override
  69. public void onItemClick(AdapterView<?> parent, View view,
  70. int position, long id) {
  71. // ListView Clicked item index
  72.  
  73. Bundle args = getArguments();
  74. args = getArguments();
  75. int itemPosition = args.getInt("position");
  76. switch (itemPosition) {
  77. case 0:
  78. phoneNumber = "12345";
  79. email="mail.com";
  80. break;
  81. case 1:
  82. phoneNumber = "1246";
  83. email="mail2.com";
  84. break;
  85. case 2:
  86. phoneNumber = "8780";
  87. email="mail3.com";
  88. break;
  89. }
  90.  
  91.  
  92. if (position == 0) {
  93. dismiss();
  94. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  95.  
  96. builder.setTitle("calling to " + phoneNumber);
  97.  
  98. builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
  99.  
  100. public void onClick(DialogInterface dialog, int which) {
  101. // Here I want to call the method but now sure how to do
  102. /*if (isPermissionGranted()) {
  103. call_action();
  104. }*/
  105. }
  106. });
  107. builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  108. @Override
  109. public void onClick(DialogInterface dialog, int which) {
  110. // Do nothing
  111. dialog.dismiss();
  112. }
  113. });
  114.  
  115. AlertDialog alert = builder.create();
  116. alert.show();
  117. }
  118.  
  119. }
  120. });
  121. return rootView;
  122. }
  123.  
  124. public void call_action(){
  125.  
  126. Intent callIntent = new Intent(Intent.ACTION_CALL);
  127. callIntent.setData(Uri.parse("tel:" + phoneNumber));
  128. startActivity(callIntent);
  129. }
  130.  
  131. @Override
  132. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  133. switch (requestCode) {
  134. case 1: {
  135. if (grantResults.length > 0
  136. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  137. Toast.makeText(getActivity().getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
  138. call_action();
  139. } else {
  140. Toast.makeText(getActivity().getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
  141. }
  142. return;
  143. }
  144.  
  145. // other 'case' lines to check for other
  146. // permissions this app might request
  147. }
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement