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:ch.epfl.sweng.team7.hikingapp.LoginActivity.java

/**
 * Check if we have the GET_ACCOUNTS permission and request it if we do not.
 *
 * @return true if we have the permission, false if we do not.
 *///from  w w  w  .  j  a  va 2  s .com
private boolean checkAccountsPermission() {
    final String perm = Manifest.permission.GET_ACCOUNTS;
    int permissionCheck = ContextCompat.checkSelfPermission(this, perm);
    if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
        // We have the permission
        return true;
    } else if (ActivityCompat.shouldShowRequestPermissionRationale(this, perm)) {
        // Need to show permission rationale, display a snackbar and then request
        // the permission again when the snackbar is dismissed.
        Snackbar.make(findViewById(R.id.login_layout), R.string.contacts_permission_rationale,
                Snackbar.LENGTH_INDEFINITE).setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Request the permission again.
                        ActivityCompat.requestPermissions(LoginActivity.this, new String[] { perm },
                                RC_PERM_GET_ACCOUNTS);
                    }
                }).show();
        return false;
    } else {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(this, new String[] { perm }, RC_PERM_GET_ACCOUNTS);
        return false;
    }
}

From source file:cc.metapro.openct.allclasses.AllClassesActivity.java

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_WRITE_STORAGE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            mPresenter.exportClasses();//from  w w w  .jav a2s  .  com
        } else {
            Toast.makeText(this, R.string.no_write_permission, Toast.LENGTH_LONG).show();
        }
    }
}

From source file:ca.uqac.florentinth.speakerauthentication.MainActivity.java

private void checkRecordAudioPermission() {
    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
        requestRecordAudioPermissions();
    } else {/*from   ww  w.  j ava 2  s .co  m*/
        submit();
    }
}

From source file:com.facebook.login.LoginClient.java

boolean checkInternetPermission() {
    if (checkedInternetPermission) {
        return true;
    }/*from  w  w  w  . j  a v  a  2 s . c o m*/

    int permissionCheck = checkPermission(Manifest.permission.INTERNET);
    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        Activity activity = getActivity();
        String errorType = activity.getString(R.string.com_facebook_internet_permission_error_title);
        String errorDescription = activity.getString(R.string.com_facebook_internet_permission_error_message);
        complete(Result.createErrorResult(pendingRequest, errorType, errorDescription));

        return false;
    }

    checkedInternetPermission = true;
    return true;
}

From source file:com.android.projectz.teamrocket.thebusapp.activities.SplashScreenActivity.java

private boolean checkAndRequestPermissions() {
    int internet = ContextCompat.checkSelfPermission(this, permissions[0]);
    int w_ext = ContextCompat.checkSelfPermission(this, permissions[1]);
    int account = ContextCompat.checkSelfPermission(this, permissions[2]);
    int r_phone = ContextCompat.checkSelfPermission(this, permissions[3]);
    List<String> listPermissionsNeeded = new ArrayList<>();
    Log.d(TAG, "internet: " + internet);
    Log.d(TAG, "w_ext: " + w_ext);
    Log.d(TAG, "account: " + account);
    Log.d(TAG, "r_phone: " + r_phone);

    if (internet != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(permissions[0]);
    }//from   www  .j a va 2 s  . c  om
    if (w_ext != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(permissions[1]);
    }
    if (account != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(permissions[2]);
    }
    if (r_phone != PackageManager.PERMISSION_GRANTED) {
        listPermissionsNeeded.add(permissions[3]);
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(this,
                listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                REQUEST_ID_MULTIPLE_PERMISSIONS);
        return false;
    }

    return true;
}

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

protected void checkAudioRecordPermission() {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO)) {

            // Show an expanation 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 {//from w  ww .j  a  va 2  s . c o  m

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.RECORD_AUDIO },
                    REQUEST_AUDIO_PERMISSIONS_ID);

        }
    }
}

From source file:key.secretkey.MainActivity.java

/****CODE EMPRUNT****/
/* Les lignes suivantes proviennent du projet open source */
/* Android-Password-Store sous license GPL 3.0 de l'auteur Zeapo */
/* Ce sont principalement des mthodes servant a effectuer des oprations sur les mot de passe */

@Override//from   w  w  w.j  a v  a 2 s. c  o m
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
    case REQUEST_EXTERNAL_STORAGE: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            checkLocalRepository();
        }
    }
    }
}

From source file:eu.codeplumbers.cosi.services.CosiContactService.java

private void readContactDatabase() {
    //Fetches the complete call log in descending order. i.e recent calls appears first.
    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        // // TODO: 11/11/16 remove before release
        new Delete().from(ContactDataPoint.class).execute();
        new Delete().from(Contact.class).execute();

        if (cur.getCount() > 0) {
            int cnt = 0, total = cur.getCount();
            while (cur.moveToNext()) {
                EventBus.getDefault().post(new ContactSyncEvent(SYNC_MESSAGE,
                        "Reading local contacts " + (cnt + 1) + "/" + total + "..."));

                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photo = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                System.out.println("name : " + name + ", ID : " + id);

                //Contact contact = Contact.getByName(name);

                //if (contact == null) {
                Contact contact = new Contact();
                contact.setN(name);//  w w  w .j  a  v  a  2s  .c o  m
                contact.setRevision(DateUtils.formatDate(new Date().getTime()));
                contact.setRemoteId("");
                contact.setTags(new JSONArray().toString());

                contact.setFn(name);
                if (photo != null) {
                    contact.setPhotoBase64(FileUtils.uriToBase64(this, photo));
                } else {
                    contact.setPhotoBase64("");
                }
                //} else {
                if ((contact.getPhotoBase64() == null || contact.getPhotoBase64().equalsIgnoreCase(""))
                        && photo != null) {
                    contact.setPhotoBase64(FileUtils.uriToBase64(this, photo));
                }
                //}

                contact.setSystemId(id);
                contact.setDeviceId(contact.getRemoteId().isEmpty() ? Device.registeredDevice().getLogin()
                        : contact.getDeviceId());
                contact.save();

                if (Integer.parseInt(
                        cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    // get the phone number
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id },
                            null);
                    while (pCur.moveToNext()) {
                        String phone = pCur
                                .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        System.out.println("phone " + phone);
                        if (!phone.isEmpty()) {
                            ContactDataPoint contactDataPoint = ContactDataPoint.getByValue(contact, phone);
                            if (contactDataPoint == null) {
                                contactDataPoint = new ContactDataPoint();
                                contactDataPoint.setPref(false);
                                contactDataPoint.setType("tel");
                                contactDataPoint.setValue(phone);
                                contactDataPoint.setName("tel");
                                contactDataPoint.setContact(contact);
                                contactDataPoint.save();
                            }
                        }
                    }
                    pCur.close();

                }
                // get email and type

                Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
                while (emailCur.moveToNext()) {
                    // This would allow you get several email addresses
                    // if the email addresses were stored in an array
                    String email = emailCur
                            .getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    String emailType = emailCur
                            .getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                    System.out.println("Email " + email + " Email Type : " + emailType);

                    if (!email.isEmpty()) {
                        ContactDataPoint contactDataPoint = ContactDataPoint.getByValue(contact, email);
                        if (contactDataPoint == null) {
                            contactDataPoint = new ContactDataPoint();
                            contactDataPoint.setPref(false);
                            contactDataPoint.setType(emailType);
                            contactDataPoint.setValue(email);
                            contactDataPoint.setName("email");
                            contactDataPoint.setContact(contact);
                            contactDataPoint.save();
                        }
                    }
                }
                emailCur.close();

                // Get note.......
                String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
                        + ContactsContract.Data.MIMETYPE + " = ?";
                String[] noteWhereParams = new String[] { id,
                        ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE };
                Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams,
                        null);
                if (noteCur.moveToFirst()) {
                    String note = noteCur
                            .getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                    System.out.println("Note " + note);

                    if (!note.isEmpty()) {
                        ContactDataPoint contactDataPoint = ContactDataPoint.getByValue(contact, note);
                        if (contactDataPoint == null) {
                            contactDataPoint = new ContactDataPoint();
                            contactDataPoint.setPref(false);
                            contactDataPoint.setType("note");
                            contactDataPoint.setValue(note);
                            contactDataPoint.setName("note");
                            contactDataPoint.setContact(contact);
                            contactDataPoint.save();
                        }
                    }
                }
                noteCur.close();

                //Get Postal Address....

                String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
                        + ContactsContract.Data.MIMETYPE + " = ?";
                String[] addrWhereParams = new String[] { id,
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
                Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
                while (addrCur.moveToNext()) {
                    String poBox = addrCur.getString(
                            addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                    String street = addrCur.getString(
                            addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                    String city = addrCur.getString(
                            addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                    String state = addrCur.getString(
                            addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                    String postalCode = addrCur.getString(
                            addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                    String country = addrCur.getString(
                            addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                    String type = addrCur.getString(
                            addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

                }
                addrCur.close();

                // Get Instant Messenger.........
                String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE
                        + " = ?";
                String[] imWhereParams = new String[] { id,
                        ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE };
                Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI, null, imWhere, imWhereParams, null);
                if (imCur.moveToFirst()) {
                    String imName = imCur
                            .getString(imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                    String imType;
                    imType = imCur.getString(imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));

                    if (!imName.isEmpty()) {
                        ContactDataPoint contactDataPoint = ContactDataPoint.getByValue(contact, imName);
                        if (contactDataPoint == null) {
                            contactDataPoint = new ContactDataPoint();
                            contactDataPoint.setPref(false);
                            contactDataPoint.setType(imType);
                            contactDataPoint.setValue(imName);
                            contactDataPoint.setName("chat");
                            contactDataPoint.setContact(contact);
                            contactDataPoint.save();
                        }
                    }

                }
                imCur.close();

                // Get Organizations
                String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
                        + ContactsContract.Data.MIMETYPE + " = ?";
                String[] orgWhereParams = new String[] { id,
                        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE };
                Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, null, orgWhere, orgWhereParams,
                        null);
                if (orgCur.moveToFirst()) {
                    String orgName = orgCur.getString(
                            orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
                    String title = orgCur.getString(
                            orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
                }
                orgCur.close();

                cnt++;
            }
        }

    } else {
        EventBus.getDefault()
                .post(new ContactSyncEvent(SERVICE_ERROR, getString(R.string.permission_denied_contacts)));
    }
}

From source file:co.codecrunch.musicplayerlite.activities.MusicPlayerBaseActivity.java

@Override
protected void onResume() {
    super.onResume();
    addObserver();/*from w w  w  .  j a  v  a 2s .c o m*/
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
            loadAlreadyPlayng();
        else
            (new PermissionHelper(MusicPlayerBaseActivity.this)).checkPermission();
    } else
        loadAlreadyPlayng();
}

From source file:org.cocos2dx.lib.CCUtils.java

public static String getMacAddress() {
    String mac = "";
    Context ctx = Cocos2dxActivity.getContext();

    // first, try to get mac from wifi manager
    if (ctx.checkCallingPermission(permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED) {
        WifiManager wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        mac = info.getMacAddress();//from   ww  w.j  a  v  a 2  s .c o  m
    }

    // if failed, try from network interface api
    if (TextUtils.isEmpty(mac)) {
        if (Build.VERSION.SDK_INT >= 9) {
            try {
                NetworkInterface ne = NetworkInterface
                        .getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
                byte[] b = ne.getHardwareAddress();
                mac = byte2Hex(b);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // if failed, use fake
    if (TextUtils.isEmpty(mac)) {
        mac = "00:00:00:00:00:00";
    }

    // return
    return mac;
}