Guest User

Untitled

a guest
Jan 9th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.30 KB | None | 0 0
  1. public class InicioActivity extends AppCompatActivity {
  2.  
  3. public int count=0;
  4. int tempInt = 0;
  5.  
  6.  
  7. private FirebaseUser mFirebaseUser;
  8. private FirebaseAuth firebaseUser;
  9. private Button mButtonTrabajador;
  10. private Button mButtonCliente;
  11. private static final int RC_LOCATION_CONTACTS_PERM = 99;
  12. private static final int RC_LOCATION_CONTACTS_PERME = 122;
  13.  
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_inicio);
  19. mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
  20.  
  21. CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
  22. .setDefaultFontPath("fonts/RobotoLight.ttf")
  23. .setFontAttrId(R.attr.fontPath)
  24. .build()
  25. );
  26.  
  27. String permission = "android.permission.ACCESS_COARSE_LOCATION";
  28. final int res = this.checkCallingOrSelfPermission(permission);
  29. String permission2 = "android.permission.ACCESS_FINE_LOCATION";
  30. final int res2 = this.checkCallingOrSelfPermission(permission2);
  31.  
  32. mButtonCliente = (Button) findViewById(R.id.buttonIrARegistroCliente);
  33. mButtonTrabajador = (Button) findViewById(R.id.buttonIrARegistroTrabajador);
  34.  
  35.  
  36.  
  37. mButtonCliente.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View view) {
  40.  
  41. if(res == PackageManager.PERMISSION_GRANTED && res2 == PackageManager.PERMISSION_GRANTED){
  42. Intent intent = new Intent(InicioActivity.this, RegisterActivity.class);
  43. //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  44. startActivity(intent);
  45. }else{
  46.  
  47. final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Light_Dialog).create();
  48. alertDialog.setTitle("Los permisos tienen que ser activados o no podra continuar, desea actualizarlos?");
  49. alertDialog.setCanceledOnTouchOutside(false);
  50. alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ir a configuracion", new DialogInterface.OnClickListener() {
  51. @Override
  52. public void onClick(final DialogInterface dialog, int which) {
  53. Intent intent = new Intent();
  54. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  55. Uri uri = Uri.fromParts("package", InicioActivity.this.getPackageName(), null);
  56. intent.setData(uri);
  57. startActivity(intent);
  58. }
  59. });
  60. alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
  61. @Override
  62. public void onClick(DialogInterface dialogInterface, int i) {
  63. alertDialog.dismiss();
  64. }
  65. });
  66.  
  67. }
  68.  
  69. }
  70. });
  71.  
  72.  
  73. mButtonTrabajador.setOnClickListener(new View.OnClickListener() {
  74. @Override
  75. public void onClick(View view) {
  76.  
  77. if(res == PackageManager.PERMISSION_GRANTED && res2 == PackageManager.PERMISSION_GRANTED) {
  78. Intent intent = new Intent(InicioActivity.this, RegisterActivityWorkers.class);
  79. //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  80. startActivity(intent);
  81. }else{
  82.  
  83. final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Light_Dialog).create();
  84. alertDialog.setTitle("Los permisos tienen que ser activados o no podra continuar, desea actualizarlos?");
  85. alertDialog.setCanceledOnTouchOutside(false);
  86. alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ir a configuracion", new DialogInterface.OnClickListener() {
  87. @Override
  88. public void onClick(final DialogInterface dialog, int which) {
  89. Intent intent = new Intent();
  90. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  91. Uri uri = Uri.fromParts("package", InicioActivity.this.getPackageName(), null);
  92. intent.setData(uri);
  93. startActivity(intent);
  94. }
  95. });
  96. alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
  97. @Override
  98. public void onClick(DialogInterface dialogInterface, int i) {
  99. alertDialog.dismiss();
  100. }
  101. });
  102.  
  103.  
  104. }
  105. }
  106. });
  107.  
  108.  
  109.  
  110. RequestPermissions();
  111. }
  112.  
  113.  
  114.  
  115. @Override
  116. protected void attachBaseContext(Context newBase) {
  117. super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
  118. }
  119.  
  120. private void RequestPermissions(){
  121. if (ContextCompat.checkSelfPermission(getApplicationContext(),
  122. Manifest.permission.ACCESS_COARSE_LOCATION)
  123. != PackageManager.PERMISSION_GRANTED) {
  124.  
  125. // Should we show an explanation?
  126. if (ActivityCompat.shouldShowRequestPermissionRationale(InicioActivity.this,
  127. Manifest.permission.ACCESS_COARSE_LOCATION)) {
  128.  
  129. // Show an explanation to the user *asynchronously* -- don't block
  130. // this thread waiting for the user's response! After the user
  131. // sees the explanation, try again to request the permission.
  132.  
  133. } else {
  134.  
  135. // No explanation needed, we can request the permission.
  136.  
  137. ActivityCompat.requestPermissions(InicioActivity.this,
  138. new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
  139. RC_LOCATION_CONTACTS_PERM);
  140.  
  141. // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
  142. // app-defined int constant. The callback method gets the
  143. // result of the request.
  144. }
  145. }
  146.  
  147. if (ContextCompat.checkSelfPermission(getApplicationContext(),
  148. Manifest.permission.ACCESS_FINE_LOCATION)
  149. != PackageManager.PERMISSION_GRANTED) {
  150.  
  151. // Should we show an explanation?
  152. if (ActivityCompat.shouldShowRequestPermissionRationale(InicioActivity.this,
  153. Manifest.permission.ACCESS_FINE_LOCATION)) {
  154.  
  155. // Show an explanation to the user *asynchronously* -- don't block
  156. // this thread waiting for the user's response! After the user
  157. // sees the explanation, try again to request the permission.
  158.  
  159. } else {
  160.  
  161. // No explanation needed, we can request the permission.
  162.  
  163. ActivityCompat.requestPermissions(InicioActivity.this,
  164. new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
  165. RC_LOCATION_CONTACTS_PERME);
  166.  
  167. // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
  168. // app-defined int constant. The callback method gets the
  169. // result of the request.
  170. }
  171. }
  172. }
  173.  
  174. @Override
  175. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  176. switch (requestCode) {
  177. case RC_LOCATION_CONTACTS_PERM: {
  178. // If request is cancelled, the result arrays are empty.
  179. if (grantResults.length > 0
  180. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  181.  
  182.  
  183.  
  184.  
  185.  
  186. } else {
  187.  
  188.  
  189. }
  190. return;
  191.  
  192.  
  193.  
  194. }
  195. }
  196. }
  197.  
  198.  
  199.  
  200.  
  201. }
  202.  
  203. public void checkPermission() {
  204.  
  205. Log.e(TAG, " inside checkSelfPermission()");
  206.  
  207.  
  208. resultCode = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS);
  209. System.out.println(TAG + " resultCode at start :" + String.valueOf(resultCode));
  210.  
  211. if (resultCode != PackageManager.PERMISSION_GRANTED) {
  212.  
  213.  
  214. //This block only will get executed after you denied first
  215. //Here you can show why You need , then try again
  216. if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_CONTACTS)) {
  217. Log.w(TAG, "---You disabled previously, I have to show it again ----");
  218.  
  219.  
  220. new AlertDialog.Builder(context).setCancelable(false).setTitle("Hi Dumb User")
  221. .setMessage("You denied Permission ! before !! Which is requiredn" +
  222. "If you still deny ,then go to Setting ").
  223. setPositiveButton("TRY AGAIN ", new DialogInterface.OnClickListener() {
  224. @Override
  225. public void onClick(DialogInterface dialogInterface, int i) {
  226.  
  227. //request Again
  228.  
  229. requestPermission();
  230.  
  231.  
  232. }
  233. }).setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {
  234. @Override
  235. public void onClick(DialogInterface dialogInterface, int i) {
  236.  
  237. dialogInterface.dismiss();
  238.  
  239. }
  240. }).show();
  241.  
  242. } else {
  243.  
  244. Log.w(TAG, " --else --");
  245. requestPermission();//ask
  246. }
  247. }
  248.  
  249. }
  250.  
  251. alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
  252. @Override
  253. public void onClick(DialogInterface dialogInterface, int i) {
  254. dialogInterface.dismiss();
  255. }
  256. });
  257.  
  258. alertDialog.show();
Add Comment
Please, Sign In to add comment