Example usage for android.content.pm PackageManager PERMISSION_GRANTED

List of usage examples for android.content.pm PackageManager PERMISSION_GRANTED

Introduction

In this page you can find the example usage for android.content.pm PackageManager PERMISSION_GRANTED.

Prototype

int PERMISSION_GRANTED

To view the source code for android.content.pm PackageManager PERMISSION_GRANTED.

Click Source Link

Document

Permission check result: this is returned by #checkPermission if the permission has been granted to the given package.

Usage

From source file:io.selendroid.server.ServerInstrumentation.java

public List<CallLogEntry> readCallLog() throws PermissionDeniedException {
    if (getTargetContext().checkCallingOrSelfPermission(
            Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
        List<CallLogEntry> logs = new ArrayList<CallLogEntry>();
        Cursor managedCursor = getTargetContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
                null, null, null);/*from ww  w  .jav a  2  s .  c  o  m*/
        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        while (managedCursor.moveToNext()) {
            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            Date callDayTime = new Date(Long.valueOf(callDate));
            String callDuration = managedCursor.getString(duration);
            logs.add(new CallLogEntry(phNumber, Integer.parseInt(callDuration), callDayTime,
                    Integer.parseInt(callType)));
        }
        managedCursor.close();
        return logs;
    } else {
        throw new PermissionDeniedException(
                "Application under test does not have required READ_CALL_LOG permission for this feature.");
    }

}

From source file:ac.robinson.bettertogether.ConnectionSetupActivity.java

private boolean cameraPermissionGranted() {
    if (ContextCompat.checkSelfPermission(ConnectionSetupActivity.this,
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

        if (!mHasRequestedCameraPermission) {
            mHasRequestedCameraPermission = true;

            if (ActivityCompat.shouldShowRequestPermissionRationale(ConnectionSetupActivity.this,
                    Manifest.permission.CAMERA)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ConnectionSetupActivity.this);
                builder.setTitle(R.string.title_camera_access);
                builder.setMessage(R.string.hint_enable_camera_access);
                builder.setPositiveButton(R.string.hint_ask_again_permissions,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(ConnectionSetupActivity.this,
                                        new String[] { Manifest.permission.CAMERA }, CAMERA_PERMISSION_RESULT);
                            }// w w w .  jav  a2  s. c  o  m
                        });
                builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(ConnectionSetupActivity.this, R.string.error_accessing_camera,
                                Toast.LENGTH_LONG).show();
                        restartCaptureManager(); // reset capture UI and try again
                    }
                });
                builder.show();
            } else {
                ActivityCompat.requestPermissions(ConnectionSetupActivity.this,
                        new String[] { Manifest.permission.CAMERA }, CAMERA_PERMISSION_RESULT);
            }
        }
        return false;
    }
    return true;
}

From source file:ota.otaupdates.MainActivity.java

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
        @NonNull int[] grantResults) {
    switch (requestCode) {
    case PERMISSION_REQUEST_CODE:
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.d(getString(R.string.app_name), "Permission granted. Files can be saved");
        } else {/*ww w.j av  a  2s . co  m*/
            Log.e(getString(R.string.app_name), "Permission denied. The App won\'t work");
            finish();
        }
        break;
    }
}

From source file:carsharing.starter.automotive.iot.ibm.com.mobilestarterapp.Home.CarBrowse.java

private void getAccurateLocation2(final GoogleMap googleMap) {
    final FragmentActivity activity = getActivity();
    if (activity == null) {
        return;/*from w  w w  .ja v a 2s .  c  o m*/
    }
    final ConnectivityManager connectivityManager = (ConnectivityManager) activity
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            && (networkInfo != null && networkInfo.isConnected())) {
        if (simulatedLocation == null) {
            if (ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }

            locationManager = (LocationManager) activity.getApplicationContext()
                    .getSystemService(Context.LOCATION_SERVICE);

            final List<String> providers = locationManager.getProviders(true);
            Location finalLocation = null;

            for (String provider : providers) {
                if (ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(activity.getApplicationContext(),
                                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }

                final Location suggestedLocation = locationManager.getLastKnownLocation(provider);

                if (suggestedLocation == null) {
                    continue;
                }
                Log.i("Location Provider",
                        "provider=" + suggestedLocation.getProvider() + " time="
                                + new SimpleDateFormat("yyyy MMM dd HH:mm:ss")
                                        .format(suggestedLocation.getTime())
                                + " accuracy=" + suggestedLocation.getAccuracy() + " lat="
                                + suggestedLocation.getLatitude() + " lng=" + suggestedLocation.getLongitude());

                if (finalLocation == null) {
                    finalLocation = suggestedLocation;
                } else if (suggestedLocation.getTime() - finalLocation.getTime() > 1000) {
                    // drop more than 1000ms old data
                    finalLocation = suggestedLocation;
                } else if (suggestedLocation.getAccuracy() < finalLocation.getAccuracy()) {
                    // picks more acculate one
                    finalLocation = suggestedLocation;
                }
            }

            location = finalLocation;
        } else {
            location = simulatedLocation;
        }
        if (location != null) {
            final LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());

            mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));

            mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(14), 2000, null);

            getCars(location);
        } else {
            Log.e("Location Data", "Not Working!");

            //                Toast.makeText(getActivity().getApplicationContext(), "Please activate your location settings and restart the application!", Toast.LENGTH_LONG).show();
            getAccurateLocation(mMap);
        }
    } else {
        if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(activity.getApplicationContext(), "Please turn on your GPS", Toast.LENGTH_LONG)
                    .show();

            final Intent gpsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivityForResult(gpsIntent, GPS_INTENT);

            if (networkInfo == null) {
                networkIntentNeeded = true;
            }
        } else {
            if (networkInfo == null) {
                Toast.makeText(activity.getApplicationContext(), "Please turn on Mobile Data or WIFI",
                        Toast.LENGTH_LONG).show();

                final Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
                startActivityForResult(settingsIntent, SETTINGS_INTENT);
            }
        }
    }
}

From source file:app.axe.imooc.zxing.app.CaptureActivity.java

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!hasSurface) {
        hasSurface = true;/*from w  w w.  j  a va  2s.c o m*/
        if (ContextCompat.checkSelfPermission(this,
                android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.CAMERA },
                    MY_PERMISSIONS_REQUEST_CAMERA);
        } else {
            initCamera(surfaceHolder);
        }
    }
}

From source file:com.theonespy.util.Util.java

public static boolean checkPermission(String permission) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return MyApplication.getAppContext()
                .checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
    } else {/*  ww  w .j a v a  2s . co m*/
        return true;
    }
}

From source file:com.amazon.android.tv.tenfoot.ui.fragments.ContentSearchFragment.java

private boolean hasPermission(final String permission) {

    final Context context = getActivity();
    return PackageManager.PERMISSION_GRANTED == context.getPackageManager().checkPermission(permission,
            context.getPackageName());//from www. j a v  a2s. c o m
}

From source file:ai.api.sample.MainActivity.java

public synchronized void createCalendar(CalendarBuilderObject calendarObj) {
    Calendar startTimeMillis = Calendar.getInstance();
    Calendar endTimeMillis = Calendar.getInstance();
    startTimeMillis.set(calendarObj.getYear().intValue(), calendarObj.getMonth().intValue() - 1,
            calendarObj.getDay().intValue(), calendarObj.getHour().intValue(),
            calendarObj.getMinute().intValue());
    endTimeMillis.set(calendarObj.getYear().intValue(), calendarObj.getMonth().intValue() - 1,
            calendarObj.getDay().intValue(), calendarObj.getHour().intValue(),
            calendarObj.getMinute().intValue() + 30);
    final ContentValues event = new ContentValues();
    //event.put(CalendarContract.Reminders.DTSTART, startTimeMillis.getTimeInMillis());
    event.put(CalendarContract.Events.CALENDAR_ID, 1);
    event.put(CalendarContract.Events.TITLE, calendarObj.getTaskTitle());
    event.put(CalendarContract.Events.DESCRIPTION, calendarObj.getTaskTodo());
    event.put(CalendarContract.Events.EVENT_LOCATION, "");
    event.put(CalendarContract.Events.DTSTART, startTimeMillis.getTimeInMillis());
    event.put(CalendarContract.Events.DTEND, endTimeMillis.getTimeInMillis());
    event.put(CalendarContract.Events.ALL_DAY, 0); // 0 for false, 1 for true
    event.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true
    String timeZone = TimeZone.getDefault().getID();
    event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);

    Uri baseUri;//from www  .j  av a 2 s . c o  m
    baseUri = Uri.parse("content://com.android.calendar/events");
    //getApplicationContext();
    baseUri = getApplicationContext().getContentResolver().insert(baseUri, event);

    long eventID = Long.parseLong(baseUri.getLastPathSegment());
    ContentValues reminders = new ContentValues();
    reminders.put(CalendarContract.Reminders.EVENT_ID, eventID);
    reminders.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
    reminders.put(CalendarContract.Reminders.MINUTES, 10);

    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Uri uri2 = getApplicationContext().getContentResolver().insert(CalendarContract.Reminders.CONTENT_URI,
            reminders);

}

From source file:co.taqat.call.assistant.AssistantActivity.java

private void launchEchoCancellerCalibration(boolean sendEcCalibrationResult) {
    int recordAudio = getPackageManager().checkPermission(Manifest.permission.RECORD_AUDIO, getPackageName());
    Log.i("[Permission] Record audio permission is "
            + (recordAudio == PackageManager.PERMISSION_GRANTED ? "granted" : "denied"));

    if (recordAudio == PackageManager.PERMISSION_GRANTED) {
        EchoCancellerCalibrationFragment fragment = new EchoCancellerCalibrationFragment();
        fragment.enableEcCalibrationResultSending(sendEcCalibrationResult);
        changeFragment(fragment);//  ww  w . j av  a2s. c  o m
        currentFragment = AssistantFragmentsEnum.ECHO_CANCELLER_CALIBRATION;
        back.setVisibility(View.VISIBLE);
        cancel.setEnabled(false);
    } else {
        checkAndRequestAudioPermission();
    }
}

From source file:au.com.dektech.dektalk.MainActivity.java

private boolean havePermissions(ArrayList<String> permissions) {
    boolean allgranted = true;
    ListIterator<String> it = permissions.listIterator();
    while (it.hasNext()) {
        if (ActivityCompat.checkSelfPermission(this, it.next()) != PackageManager.PERMISSION_GRANTED) {
            allgranted = false;/*from   w  w w . j  a v  a  2  s  .  c  o m*/
        } else {
            // permission granted, remove it from permissions
            it.remove();
        }
    }
    return allgranted;
}