public static boolean hasPermissions(Context context, final String... permissions) { for (final String permission : permissions) { if (!hasPermission(context, permission)) { return false; } } return true; }
public static boolean hasPhonePermissions(Context context) { return hasPermission(context, PHONE); }
public static boolean hasLocationPermissions(Context context) { return hasPermission(context, LOCATION); }
public static boolean hasContactsPermissions(Context context) { return hasPermission(context, CONTACTS); }
private void undemoteContactWithId(Context context, long id) { // If the contact is not demoted, this will not do anything. Otherwise, it will // restore it to an unpinned position. If it was a frequently called contact, it will // show up once again show up on the favorites screen. if (PermissionsUtil.hasPermission(context, WRITE_CONTACTS)) { try { PinnedPositions.undemote(context.getContentResolver(), id); } catch (SecurityException e) { // Just in case } } }
@Override public void onReceive(final Context context, Intent intent) { if (!PermissionsUtil.hasPermission(context, READ_CONTACTS) || !PermissionsUtil.hasPermission(context, WRITE_CONTACTS)) { return; } if (intent != null && Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { final String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); if (TextUtils.isEmpty(number)) { return; } new Thread() { @Override public void run() { final long id = getContactIdFromPhoneNumber(context, number); if (id != NO_CONTACT_FOUND) { undemoteContactWithId(context, id); } } }.start(); } }
@Override @Nullable public List<NewCall> query(int type) { if (!PermissionsUtil.hasPermission(mContext, Manifest.permission.READ_CALL_LOG)) { Log.w(TAG, "No READ_CALL_LOG permission, returning null for calls lookup."); return null; } final String selection = String.format("%s = 1 AND %s = ?", Calls.NEW, Calls.TYPE); final String[] selectionArgs = new String[]{ Integer.toString(type) }; try (Cursor cursor = mContentResolver.query(Calls.CONTENT_URI_WITH_VOICEMAIL, PROJECTION, selection, selectionArgs, Calls.DEFAULT_SORT_ORDER)) { if (cursor == null) { return null; } List<NewCall> newCalls = new ArrayList<>(); while (cursor.moveToNext()) { newCalls.add(createNewCallsFromCursor(cursor)); } return newCalls; } catch (RuntimeException e) { Log.w(TAG, "Exception when querying Contacts Provider for calls lookup"); return null; } }
@Override @Nullable public String query(@Nullable String number) { if (!PermissionsUtil.hasPermission(mContext, Manifest.permission.READ_CONTACTS)) { Log.w(TAG, "No READ_CONTACTS permission, returning null for name lookup."); return null; } try (Cursor cursor = mContentResolver.query( Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)), PROJECTION, null, null, null)) { if (cursor == null || !cursor.moveToFirst()) { return null; } return cursor.getString(DISPLAY_NAME_COLUMN_INDEX); } catch (RuntimeException e) { Log.w(TAG, "Exception when querying Contacts Provider for name lookup"); return null; } } }
public void onResume() { if (PermissionsUtil.hasPermission(mContext, android.Manifest.permission.READ_CONTACTS)) { mContactInfoCache.start(); } mContactsPreferences.refreshValue(ContactsPreferences.DISPLAY_ORDER_KEY); }
private long getContactIdFromPhoneNumber(Context context, String number) { if (!PermissionsUtil.hasPermission(context, READ_CONTACTS)) { return NO_CONTACT_FOUND; } final Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); final Cursor cursor; try { cursor = context.getContentResolver().query(contactUri, new String[] { PhoneLookup._ID}, null, null, null); } catch (SecurityException e) { // Just in case return NO_CONTACT_FOUND; } if (cursor == null) { return NO_CONTACT_FOUND; } try { if (cursor.moveToFirst()) { final long id = cursor.getLong(0); return id; } else { return NO_CONTACT_FOUND; } } finally { cursor.close(); } } }
private void loadPhotosInBackground() { if (!PermissionsUtil.hasPermission(mContext, android.Manifest.permission.READ_CONTACTS)) { return; } obtainPhotoIdsAndUrisToLoad(mPhotoIds, mPhotoIdsAsStrings, mPhotoUris); loadThumbnails(false); loadUriBasedPhotos(); requestPreloading(); }
@Override public void onEmptyViewActionButtonClicked() { final Activity activity = getActivity(); if (activity == null) { return; } if (!PermissionsUtil.hasPermission(activity, READ_CONTACTS)) { FragmentCompat.requestPermissions(this, new String[] {READ_CONTACTS}, READ_CONTACTS_PERMISSION_REQUEST_CODE); } else { // Switch tabs ((HostInterface) activity).showAllContactsTab(); } }
@Override public void onEmptyViewActionButtonClicked() { final Activity activity = getActivity(); if (activity == null) { return; } if (!PermissionsUtil.hasPermission(activity, READ_CALL_LOG)) { FragmentCompat.requestPermissions(this, new String[] {READ_CALL_LOG}, READ_CALL_LOG_PERMISSION_REQUEST_CODE); } else if (!mIsCallLogActivity) { // Show dialpad if we are not in the call log activity. ((HostInterface) activity).showDialpad(); } }
@Override public void onEmptyViewActionButtonClicked() { final Activity activity = getActivity(); if (activity == null) { return; } if (!PermissionsUtil.hasPermission(activity, READ_CONTACTS)) { FragmentCompat.requestPermissions(this, new String[] {READ_CONTACTS}, READ_CONTACTS_PERMISSION_REQUEST_CODE); } else { // Add new contact DialerUtils.startActivityWithErrorToast(activity, IntentUtil.getNewContactIntent(), R.string.add_contact_not_available); } }
/** * Determines if one of the call capable phone accounts defined supports calling with a subject * specified. * * @param context The context. * @return {@code true} if one of the call capable phone accounts supports calling with a * subject specified, {@code false} otherwise. */ public static boolean isCallWithSubjectSupported(Context context) { if (!PermissionsUtil.hasPermission(context, android.Manifest.permission.READ_PHONE_STATE) || !CompatUtils.isCallSubjectCompatible()) { return false; } TelecomManager telecommMgr = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); if (telecommMgr == null) { return false; } List<PhoneAccountHandle> accountHandles = telecommMgr.getCallCapablePhoneAccounts(); for (PhoneAccountHandle accountHandle : accountHandles) { PhoneAccount account = telecommMgr.getPhoneAccount(accountHandle); if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_SUBJECT)) { return true; } } return false; } }
if (!PermissionsUtil.hasPermission(context, READ_CALL_LOG)) { mEmptyListView.setDescription(R.string.permission_no_calllog); mEmptyListView.setActionLabel(R.string.permission_single_turn_on);
@Override protected void startLoading() { if (PermissionsUtil.hasPermission(getActivity(), READ_CONTACTS)) { super.startLoading(); mEmptyListView.setDescription(R.string.all_contacts_empty); mEmptyListView.setActionLabel(R.string.all_contacts_empty_add_contact_action); } else { mEmptyListView.setDescription(R.string.permission_no_contacts); mEmptyListView.setActionLabel(R.string.permission_single_turn_on); mEmptyListView.setVisibility(View.VISIBLE); } }
@Override public void onResume() { super.onResume(); final boolean hasReadCallLogPermission = PermissionsUtil.hasPermission(getActivity(), READ_CALL_LOG); if (!mHasReadCallLogPermission && hasReadCallLogPermission) { // We didn't have the permission before, and now we do. Force a refresh of the call log. // Note that this code path always happens on a fresh start, but mRefreshDataRequired // is already true in that case anyway. mRefreshDataRequired = true; updateEmptyMessage(mCallTypeFilter); } mHasReadCallLogPermission = hasReadCallLogPermission; refreshData(); mAdapter.onResume(); rescheduleDisplayUpdate(); }
@Override protected void setupEmptyView() { if (mEmptyView != null && getActivity() != null) { final int imageResource; final int actionLabelResource; final int descriptionResource; final OnEmptyViewActionButtonClickedListener listener; if (!PermissionsUtil.hasPermission(getActivity(), READ_CONTACTS)) { imageResource = R.drawable.empty_contacts; actionLabelResource = R.string.permission_single_turn_on; descriptionResource = R.string.permission_no_search; listener = this; mPermissionToRequest = READ_CONTACTS; } else { imageResource = EmptyContentView.NO_IMAGE; actionLabelResource = EmptyContentView.NO_LABEL; descriptionResource = EmptyContentView.NO_LABEL; listener = null; mPermissionToRequest = null; } mEmptyView.setImage(imageResource); mEmptyView.setActionLabel(actionLabelResource); mEmptyView.setDescription(descriptionResource); if (listener != null) { mEmptyView.setActionClickedListener(listener); } } }
@Override protected void setupEmptyView() { if (mEmptyView != null && getActivity() != null) { if (!PermissionsUtil.hasPermission(getActivity(), CALL_PHONE)) { mEmptyView.setImage(R.drawable.empty_contacts); mEmptyView.setActionLabel(R.string.permission_single_turn_on); mEmptyView.setDescription(R.string.permission_place_call); mEmptyView.setActionClickedListener(this); } else { mEmptyView.setImage(EmptyContentView.NO_IMAGE); mEmptyView.setActionLabel(EmptyContentView.NO_LABEL); mEmptyView.setDescription(EmptyContentView.NO_LABEL); } } }