Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class InicioActivity extends AppCompatActivity {
- public int count=0;
- int tempInt = 0;
- private FirebaseUser mFirebaseUser;
- private FirebaseAuth firebaseUser;
- private Button mButtonTrabajador;
- private Button mButtonCliente;
- private static final int RC_LOCATION_CONTACTS_PERM = 99;
- private static final int RC_LOCATION_CONTACTS_PERME = 122;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_inicio);
- mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
- CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
- .setDefaultFontPath("fonts/RobotoLight.ttf")
- .setFontAttrId(R.attr.fontPath)
- .build()
- );
- String permission = "android.permission.ACCESS_COARSE_LOCATION";
- final int res = this.checkCallingOrSelfPermission(permission);
- String permission2 = "android.permission.ACCESS_FINE_LOCATION";
- final int res2 = this.checkCallingOrSelfPermission(permission2);
- mButtonCliente = (Button) findViewById(R.id.buttonIrARegistroCliente);
- mButtonTrabajador = (Button) findViewById(R.id.buttonIrARegistroTrabajador);
- mButtonCliente.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(res == PackageManager.PERMISSION_GRANTED && res2 == PackageManager.PERMISSION_GRANTED){
- Intent intent = new Intent(InicioActivity.this, RegisterActivity.class);
- //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- }else{
- final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Light_Dialog).create();
- alertDialog.setTitle("Los permisos tienen que ser activados o no podra continuar, desea actualizarlos?");
- alertDialog.setCanceledOnTouchOutside(false);
- alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ir a configuracion", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(final DialogInterface dialog, int which) {
- Intent intent = new Intent();
- intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- Uri uri = Uri.fromParts("package", InicioActivity.this.getPackageName(), null);
- intent.setData(uri);
- startActivity(intent);
- }
- });
- alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- alertDialog.dismiss();
- }
- });
- }
- }
- });
- mButtonTrabajador.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if(res == PackageManager.PERMISSION_GRANTED && res2 == PackageManager.PERMISSION_GRANTED) {
- Intent intent = new Intent(InicioActivity.this, RegisterActivityWorkers.class);
- //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- startActivity(intent);
- }else{
- final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext(), R.style.Theme_AppCompat_Light_Dialog).create();
- alertDialog.setTitle("Los permisos tienen que ser activados o no podra continuar, desea actualizarlos?");
- alertDialog.setCanceledOnTouchOutside(false);
- alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ir a configuracion", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(final DialogInterface dialog, int which) {
- Intent intent = new Intent();
- intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- Uri uri = Uri.fromParts("package", InicioActivity.this.getPackageName(), null);
- intent.setData(uri);
- startActivity(intent);
- }
- });
- alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- alertDialog.dismiss();
- }
- });
- }
- }
- });
- RequestPermissions();
- }
- @Override
- protected void attachBaseContext(Context newBase) {
- super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
- }
- private void RequestPermissions(){
- if (ContextCompat.checkSelfPermission(getApplicationContext(),
- Manifest.permission.ACCESS_COARSE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- // Should we show an explanation?
- if (ActivityCompat.shouldShowRequestPermissionRationale(InicioActivity.this,
- Manifest.permission.ACCESS_COARSE_LOCATION)) {
- // Show an explanation to the user *asynchronously* -- don't block
- // this thread waiting for the user's response! After the user
- // sees the explanation, try again to request the permission.
- } else {
- // No explanation needed, we can request the permission.
- ActivityCompat.requestPermissions(InicioActivity.this,
- new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
- RC_LOCATION_CONTACTS_PERM);
- // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
- // app-defined int constant. The callback method gets the
- // result of the request.
- }
- }
- if (ContextCompat.checkSelfPermission(getApplicationContext(),
- Manifest.permission.ACCESS_FINE_LOCATION)
- != PackageManager.PERMISSION_GRANTED) {
- // Should we show an explanation?
- if (ActivityCompat.shouldShowRequestPermissionRationale(InicioActivity.this,
- Manifest.permission.ACCESS_FINE_LOCATION)) {
- // Show an explanation to the user *asynchronously* -- don't block
- // this thread waiting for the user's response! After the user
- // sees the explanation, try again to request the permission.
- } else {
- // No explanation needed, we can request the permission.
- ActivityCompat.requestPermissions(InicioActivity.this,
- new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
- RC_LOCATION_CONTACTS_PERME);
- // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
- // app-defined int constant. The callback method gets the
- // result of the request.
- }
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
- switch (requestCode) {
- case RC_LOCATION_CONTACTS_PERM: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- } else {
- }
- return;
- }
- }
- }
- }
- public void checkPermission() {
- Log.e(TAG, " inside checkSelfPermission()");
- resultCode = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS);
- System.out.println(TAG + " resultCode at start :" + String.valueOf(resultCode));
- if (resultCode != PackageManager.PERMISSION_GRANTED) {
- //This block only will get executed after you denied first
- //Here you can show why You need , then try again
- if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_CONTACTS)) {
- Log.w(TAG, "---You disabled previously, I have to show it again ----");
- new AlertDialog.Builder(context).setCancelable(false).setTitle("Hi Dumb User")
- .setMessage("You denied Permission ! before !! Which is requiredn" +
- "If you still deny ,then go to Setting ").
- setPositiveButton("TRY AGAIN ", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- //request Again
- requestPermission();
- }
- }).setNeutralButton("DISMISS", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- }).show();
- } else {
- Log.w(TAG, " --else --");
- requestPermission();//ask
- }
- }
- }
- alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancelar", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- dialogInterface.dismiss();
- }
- });
- alertDialog.show();
Add Comment
Please, Sign In to add comment