Search in sources :

Example 6 with RequiresPermission

use of androidx.annotation.RequiresPermission in project Signal-Android by WhisperSystems.

the class Util method getDeviceNumber.

@RequiresPermission(anyOf = { android.Manifest.permission.READ_PHONE_STATE, android.Manifest.permission.READ_SMS, android.Manifest.permission.READ_PHONE_NUMBERS })
@SuppressLint("MissingPermission")
public static Optional<Phonenumber.PhoneNumber> getDeviceNumber(Context context) {
    try {
        final String localNumber = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
        final Optional<String> countryIso = getSimCountryIso(context);
        if (TextUtils.isEmpty(localNumber))
            return Optional.absent();
        if (!countryIso.isPresent())
            return Optional.absent();
        return Optional.fromNullable(PhoneNumberUtil.getInstance().parse(localNumber, countryIso.get()));
    } catch (NumberParseException e) {
        Log.w(TAG, e);
        return Optional.absent();
    }
}
Also used : TelephonyManager(android.telephony.TelephonyManager) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) SpannableString(android.text.SpannableString) RequiresPermission(androidx.annotation.RequiresPermission) SuppressLint(android.annotation.SuppressLint)

Example 7 with RequiresPermission

use of androidx.annotation.RequiresPermission in project Signal-Android by WhisperSystems.

the class WallpaperImageSelectionActivity method getIntent.

@RequiresPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
public static Intent getIntent(@NonNull Context context, @Nullable RecipientId recipientId) {
    Intent intent = new Intent(context, WallpaperImageSelectionActivity.class);
    intent.putExtra(EXTRA_RECIPIENT_ID, recipientId);
    return intent;
}
Also used : Intent(android.content.Intent) RequiresPermission(androidx.annotation.RequiresPermission)

Example 8 with RequiresPermission

use of androidx.annotation.RequiresPermission in project LshUtils by SenhLinsh.

the class PowerHelperEx method keepScreenOn.

@Deprecated
@RequiresPermission(Manifest.permission.WAKE_LOCK)
public void keepScreenOn() {
    PowerManager powerManager = (PowerManager) getContext().getSystemService(Service.POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, getClass().getSimpleName());
    mWakeLock.setReferenceCounted(false);
    mWakeLock.acquire();
}
Also used : PowerManager(android.os.PowerManager) RequiresPermission(androidx.annotation.RequiresPermission)

Example 9 with RequiresPermission

use of androidx.annotation.RequiresPermission in project AndroidUtilCode by Blankj.

the class DangerousUtils method setMobileDataEnabled.

/**
 * Enable or disable mobile data.
 * <p>Must hold {@code android:sharedUserId="android.uid.system"},
 * {@code <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />}</p>
 *
 * @param enabled True to enabled, false otherwise.
 * @return {@code true}: success<br>{@code false}: fail
 */
@RequiresPermission(MODIFY_PHONE_STATE)
public static boolean setMobileDataEnabled(final boolean enabled) {
    try {
        TelephonyManager tm = (TelephonyManager) Utils.getApp().getSystemService(Context.TELEPHONY_SERVICE);
        if (tm == null)
            return false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            tm.setDataEnabled(enabled);
            return true;
        }
        Method setDataEnabledMethod = tm.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
        setDataEnabledMethod.invoke(tm, enabled);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
Also used : TelephonyManager(android.telephony.TelephonyManager) Method(java.lang.reflect.Method) RequiresPermission(androidx.annotation.RequiresPermission)

Example 10 with RequiresPermission

use of androidx.annotation.RequiresPermission in project AndroidUtilCode by Blankj.

the class DangerousUtils method sendSmsSilent.

/**
 * Send sms silently.
 * <p>Must hold {@code <uses-permission android:name="android.permission.SEND_SMS" />}</p>
 *
 * @param phoneNumber The phone number.
 * @param content     The content.
 */
@RequiresPermission(SEND_SMS)
public static void sendSmsSilent(final String phoneNumber, final String content) {
    if (TextUtils.isEmpty(content))
        return;
    PendingIntent sentIntent = PendingIntent.getBroadcast(Utils.getApp(), 0, new Intent("send"), 0);
    SmsManager smsManager = SmsManager.getDefault();
    if (content.length() >= 70) {
        List<String> ms = smsManager.divideMessage(content);
        for (String str : ms) {
            smsManager.sendTextMessage(phoneNumber, null, str, sentIntent, null);
        }
    } else {
        smsManager.sendTextMessage(phoneNumber, null, content, sentIntent, null);
    }
}
Also used : Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) SmsManager(android.telephony.SmsManager) RequiresPermission(androidx.annotation.RequiresPermission)

Aggregations

RequiresPermission (androidx.annotation.RequiresPermission)43 SuppressLint (android.annotation.SuppressLint)11 WifiManager (android.net.wifi.WifiManager)5 TelephonyManager (android.telephony.TelephonyManager)4 ActivityManager (android.app.ActivityManager)3 Intent (android.content.Intent)3 SurfaceTexture (android.graphics.SurfaceTexture)3 Vibrator (android.os.Vibrator)3 Size (com.google.android.gms.common.images.Size)3 TargetApi (android.annotation.TargetApi)2 ConnectivityManager (android.net.ConnectivityManager)2 NetworkInfo (android.net.NetworkInfo)2 Bundle (android.os.Bundle)2 PowerManager (android.os.PowerManager)2 SpannableString (android.text.SpannableString)2 Rational (android.util.Rational)2 Size (android.util.Size)2 SurfaceView (android.view.SurfaceView)2 CameraSelector (androidx.camera.core.CameraSelector)2 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)2