Java Code Examples for androidx.core.content.PermissionChecker#PERMISSION_GRANTED

The following examples show how to use androidx.core.content.PermissionChecker#PERMISSION_GRANTED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: public_func.java    From dingtalk-sms with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static String get_contact_name(Context context, String phone_number) {
    String contact_name = null;
    if (checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PermissionChecker.PERMISSION_GRANTED) {
        try {
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone_number));
            String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
            Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    String cursor_name = cursor.getString(0);
                    if (!cursor_name.isEmpty())
                        contact_name = cursor_name;
                }
                cursor.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    return contact_name;
}
 
Example 2
Source File: public_func.java    From telegram-sms with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static void send_fallback_sms(Context context, String content, int sub_id) {
    final String TAG = "send_fallback_sms";
    if (androidx.core.content.PermissionChecker.checkSelfPermission(context, Manifest.permission.SEND_SMS) != PermissionChecker.PERMISSION_GRANTED) {
        Log.d(TAG, "No permission.");
        return;
    }
    SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
    String trust_number = sharedPreferences.getString("trusted_phone_number", null);
    if (trust_number == null) {
        Log.i(TAG, "The trusted number is empty.");
        return;
    }
    if (!sharedPreferences.getBoolean("fallback_sms", false)) {
        Log.i(TAG, "Did not open the SMS to fall back.");
        return;
    }
    android.telephony.SmsManager sms_manager;
    if (sub_id == -1) {
        sms_manager = SmsManager.getDefault();
    } else {
        sms_manager = SmsManager.getSmsManagerForSubscriptionId(sub_id);
    }
    ArrayList<String> divideContents = sms_manager.divideMessage(content);
    sms_manager.sendMultipartTextMessage(trust_number, null, divideContents, null, null);

}
 
Example 3
Source File: DexterActivity.java    From Dexter with Apache License 2.0 6 votes vote down vote up
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions,
    int[] grantResults) {
  Collection<String> grantedPermissions = new LinkedList<>();
  Collection<String> deniedPermissions = new LinkedList<>();

  if (isTargetSdkUnderAndroidM()) {
    deniedPermissions.addAll(Arrays.asList(permissions));
  } else {
    for (int i = 0; i < permissions.length; i++) {
      String permission = permissions[i];
      switch (grantResults[i]) {
        case PermissionChecker.PERMISSION_DENIED:
        case PermissionChecker.PERMISSION_DENIED_APP_OP:
          deniedPermissions.add(permission);
          break;
        case PermissionChecker.PERMISSION_GRANTED:
          grantedPermissions.add(permission);
          break;
        default:
      }
    }
  }

  Dexter.onPermissionsRequested(grantedPermissions, deniedPermissions);
}
 
Example 4
Source File: DexterInstance.java    From Dexter with Apache License 2.0 6 votes vote down vote up
private PermissionStates getPermissionStates(Collection<String> pendingPermissions) {
  PermissionStates permissionStates = new PermissionStates();

  for (String permission : pendingPermissions) {
    int permissionState = checkSelfPermission(activity, permission);

    switch (permissionState) {
      case PermissionChecker.PERMISSION_DENIED_APP_OP:
        permissionStates.addImpossibleToGrantPermission(permission);
        break;
      case PermissionChecker.PERMISSION_DENIED:
        permissionStates.addDeniedPermission(permission);
        break;
      case PermissionChecker.PERMISSION_GRANTED:
      default:
        permissionStates.addGrantedPermission(permission);
        break;
    }
  }

  return permissionStates;
}
 
Example 5
Source File: Game.java    From remixed-dungeon with GNU General Public License v3.0 6 votes vote down vote up
public void doPermissionsRequest(@NotNull InterstitialPoint returnTo, String[] permissions) {
    boolean havePermissions = true;
    for (String permission : permissions) {
        int checkResult = ActivityCompat.checkSelfPermission(this, permission);
        if (checkResult != PermissionChecker.PERMISSION_GRANTED) {
            havePermissions = false;
            break;
        }
    }
    if (!havePermissions) {
        int code = 0;
        permissionsPoint = returnTo;
        ActivityCompat.requestPermissions(this, permissions, code);
    } else {
        returnTo.returnToWork(true);
    }
}
 
Example 6
Source File: RecordPermissionHelper.java    From LingoRecorder with Apache License 2.0 5 votes vote down vote up
public boolean checkRecordPermission() {
    if (PermissionChecker.checkSelfPermission(activity,
            Manifest.permission.WRITE_EXTERNAL_STORAGE) != PermissionChecker.PERMISSION_GRANTED
            || PermissionChecker.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO)
            != PermissionChecker.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (activity.shouldShowRequestPermissionRationale(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE) ||
                    activity.shouldShowRequestPermissionRationale(
                            Manifest.permission.RECORD_AUDIO)) {
                new AlertDialog.Builder(activity)
                        .setTitle(R.string.check_permission_title)
                        .setMessage(R.string.check_permission_content)
                        .setCancelable(false)
                        .setPositiveButton(R.string.confirm,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                            activity.requestPermissions(new String[]{
                                                            Manifest.permission
                                                                    .WRITE_EXTERNAL_STORAGE,
                                                            Manifest.permission.RECORD_AUDIO},
                                                    REQUEST_CODE_PERMISSION);
                                        }
                                    }
                                }).show();
            } else {
                activity.requestPermissions(
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.RECORD_AUDIO}, REQUEST_CODE_PERMISSION);
            }
        }
        return false;
    }
    return true;
}
 
Example 7
Source File: BlankFragment.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == 100) {
        if (grantResults[0] == PermissionChecker.PERMISSION_GRANTED) {
            getLoaderManager().initLoader(0, null, this);
        } else {
            Toast.makeText(getContext(), "Permission denied !", Toast.LENGTH_SHORT).show();
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
 
Example 8
Source File: DataStorageActivity.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == 100) {
        if (grantResults[0] == PermissionChecker.PERMISSION_GRANTED) {
            readContacts();
        } else {
            Toast.makeText(mContext, "Permission denied !", Toast.LENGTH_SHORT).show();
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
 
Example 9
Source File: PermissionUtils.java    From PermissionsDispatcher with Apache License 2.0 5 votes vote down vote up
/**
 * Checks all given permissions have been granted.
 *
 * @param grantResults results
 * @return returns true if all permissions have been granted.
 */
public static boolean verifyPermissions(int... grantResults) {
    if (grantResults.length == 0) {
        return false;
    }
    for (int result : grantResults) {
        if (result != PermissionChecker.PERMISSION_GRANTED) {
            return false;
        }
    }
    return true;
}
 
Example 10
Source File: DexterInstance.java    From Dexter with Apache License 2.0 5 votes vote down vote up
private boolean isEveryPermissionGranted(Collection<String> permissions, Context context) {
  for (String permission : permissions) {
    int permissionState = androidPermissionService.checkSelfPermission(context, permission);
    if (permissionState != PermissionChecker.PERMISSION_GRANTED) {
      return false;
    }
  }
  return true;
}
 
Example 11
Source File: public_func.java    From telegram-sms with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static void send_sms(Context context, String send_to, String content, int slot, int sub_id) {
    if (androidx.core.content.PermissionChecker.checkSelfPermission(context, Manifest.permission.SEND_SMS) != PermissionChecker.PERMISSION_GRANTED) {
        Log.d("send_sms", "No permission.");
        return;
    }
    if (!is_phone_number(send_to)) {
        write_log(context, "[" + send_to + "] is an illegal phone number");
        return;
    }
    SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
    String bot_token = sharedPreferences.getString("bot_token", "");
    String chat_id = sharedPreferences.getString("chat_id", "");
    String request_uri = public_func.get_url(bot_token, "sendMessage");
    message_json request_body = new message_json();
    request_body.chat_id = chat_id;
    android.telephony.SmsManager sms_manager;
    if (sub_id == -1) {
        sms_manager = SmsManager.getDefault();
    } else {
        sms_manager = SmsManager.getSmsManagerForSubscriptionId(sub_id);
    }
    String dual_sim = get_dual_sim_card_display(context, slot, sharedPreferences.getBoolean("display_dual_sim_display_name", false));
    String send_content = "[" + dual_sim + context.getString(R.string.send_sms_head) + "]" + "\n" + context.getString(R.string.to) + send_to + "\n" + context.getString(R.string.content) + content;
    String message_id = "-1";
    request_body.text = send_content + "\n" + context.getString(R.string.status) + context.getString(R.string.sending);
    String request_body_raw = new Gson().toJson(request_body);
    RequestBody body = RequestBody.create(request_body_raw, public_func.JSON);
    OkHttpClient okhttp_client = public_func.get_okhttp_obj(sharedPreferences.getBoolean("doh_switch", true), Paper.book().read("proxy_config", new proxy_config()));
    Request request = new Request.Builder().url(request_uri).method("POST", body).build();
    Call call = okhttp_client.newCall(request);
    try {
        Response response = call.execute();
        if (response.code() != 200 || response.body() == null) {
            throw new IOException(String.valueOf(response.code()));
        }
        message_id = get_message_id(Objects.requireNonNull(response.body()).string());
    } catch (IOException e) {
        e.printStackTrace();
        public_func.write_log(context, "failed to send message:" + e.getMessage());
    }
    ArrayList<String> divideContents = sms_manager.divideMessage(content);
    ArrayList<PendingIntent> send_receiver_list = new ArrayList<>();
    IntentFilter filter = new IntentFilter("send_sms");
    BroadcastReceiver receiver = new sms_send_receiver();
    context.getApplicationContext().registerReceiver(receiver, filter);
    Intent sent_intent = new Intent("send_sms");
    sent_intent.putExtra("message_id", message_id);
    sent_intent.putExtra("message_text", send_content);
    sent_intent.putExtra("sub_id", sms_manager.getSubscriptionId());
    PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, sent_intent, PendingIntent.FLAG_CANCEL_CURRENT);
    send_receiver_list.add(sentIntent);
    sms_manager.sendMultipartTextMessage(send_to, null, divideContents, send_receiver_list, null);
}
 
Example 12
Source File: OpenFilePlugin.java    From open_file with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private boolean hasPermission(String permission) {
    return ContextCompat.checkSelfPermission(activity, permission) == PermissionChecker.PERMISSION_GRANTED;
}
 
Example 13
Source File: Util.java    From android-notification-log with MIT License 4 votes vote down vote up
public static boolean hasPermission(Context context, String permission) {
	return PermissionChecker.checkSelfPermission(context, permission) == PermissionChecker.PERMISSION_GRANTED;
}
 
Example 14
Source File: PermissionUtils.java    From PermissionsDispatcher with Apache License 2.0 3 votes vote down vote up
/**
 * Determine context has access to the given permission.
 * <p>
 * This is a workaround for RuntimeException of Parcel#readException.
 * For more detail, check this issue https://github.com/hotchemi/PermissionsDispatcher/issues/107
 *
 * @param context    context
 * @param permission permission
 * @return true if context has access to the given permission, false otherwise.
 * @see #hasSelfPermissions(Context, String...)
 */
private static boolean hasSelfPermission(Context context, String permission) {
    try {
        return PermissionChecker.checkSelfPermission(context, permission) == PermissionChecker.PERMISSION_GRANTED;
    } catch (RuntimeException t) {
        return false;
    }
}
 
Example 15
Source File: PermissionUtils.java    From DevUtils with Apache License 2.0 2 votes vote down vote up
/**
 * 判断是否授予了权限
 * @param context    {@link Context}
 * @param permission 待判断权限
 * @return {@code true} yes, {@code false} no
 */
private static boolean isGranted(final Context context, final String permission) {
    if (context == null || permission == null) return false;
    // SDK 版本小于 23 则表示直接通过 || 检查是否通过权限
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.M || PermissionChecker.PERMISSION_GRANTED == PermissionChecker.checkSelfPermission(context, permission);
}