Search in sources :

Example 36 with RequiresPermission

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

the class NetworkUtils method getSSID.

/**
 * Return the ssid.
 *
 * @return the ssid.
 */
@RequiresPermission(ACCESS_WIFI_STATE)
public static String getSSID() {
    WifiManager wm = (WifiManager) Utils.getApp().getApplicationContext().getSystemService(WIFI_SERVICE);
    if (wm == null)
        return "";
    WifiInfo wi = wm.getConnectionInfo();
    if (wi == null)
        return "";
    String ssid = wi.getSSID();
    if (TextUtils.isEmpty(ssid)) {
        return "";
    }
    if (ssid.length() > 2 && ssid.charAt(0) == '"' && ssid.charAt(ssid.length() - 1) == '"') {
        return ssid.substring(1, ssid.length() - 1);
    }
    return ssid;
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiInfo(android.net.wifi.WifiInfo) RequiresPermission(androidx.annotation.RequiresPermission)

Example 37 with RequiresPermission

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

the class NetworkUtils method startScanWifiIfEnabled.

@RequiresPermission(allOf = { ACCESS_WIFI_STATE, CHANGE_WIFI_STATE })
private static void startScanWifiIfEnabled() {
    if (!getWifiEnabled())
        return;
    @SuppressLint("WifiManagerLeak") WifiManager wm = (WifiManager) Utils.getApp().getSystemService(WIFI_SERVICE);
    // noinspection ConstantConditions
    wm.startScan();
}
Also used : WifiManager(android.net.wifi.WifiManager) SuppressLint(android.annotation.SuppressLint) RequiresPermission(androidx.annotation.RequiresPermission)

Example 38 with RequiresPermission

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

the class ProcessUtils method killAllBackgroundProcesses.

/**
 * Kill all background processes.
 * <p>Must hold {@code <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />}</p>
 *
 * @return background processes were killed
 */
@RequiresPermission(KILL_BACKGROUND_PROCESSES)
public static Set<String> killAllBackgroundProcesses() {
    ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> info = am.getRunningAppProcesses();
    Set<String> set = new HashSet<>();
    if (info == null)
        return set;
    for (ActivityManager.RunningAppProcessInfo aInfo : info) {
        for (String pkg : aInfo.pkgList) {
            am.killBackgroundProcesses(pkg);
            set.add(pkg);
        }
    }
    info = am.getRunningAppProcesses();
    for (ActivityManager.RunningAppProcessInfo aInfo : info) {
        for (String pkg : aInfo.pkgList) {
            set.remove(pkg);
        }
    }
    return set;
}
Also used : ActivityManager(android.app.ActivityManager) HashSet(java.util.HashSet) RequiresPermission(androidx.annotation.RequiresPermission)

Example 39 with RequiresPermission

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

the class VibrateUtils method vibrate.

/**
 * Vibrate.
 * <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
 *
 * @param pattern An array of longs of times for which to turn the vibrator on or off.
 * @param repeat  The index into pattern at which to repeat, or -1 if you don't want to repeat.
 */
@RequiresPermission(VIBRATE)
public static void vibrate(final long[] pattern, final int repeat) {
    Vibrator vibrator = getVibrator();
    if (vibrator == null)
        return;
    vibrator.vibrate(pattern, repeat);
}
Also used : Vibrator(android.os.Vibrator) RequiresPermission(androidx.annotation.RequiresPermission)

Example 40 with RequiresPermission

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

the class NetworkUtils method setWifiEnabled.

/**
 * Enable or disable wifi.
 * <p>Must hold {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />}</p>
 *
 * @param enabled True to enabled, false otherwise.
 */
@RequiresPermission(CHANGE_WIFI_STATE)
public static void setWifiEnabled(final boolean enabled) {
    @SuppressLint("WifiManagerLeak") WifiManager manager = (WifiManager) Utils.getApp().getSystemService(WIFI_SERVICE);
    if (manager == null)
        return;
    if (enabled == manager.isWifiEnabled())
        return;
    manager.setWifiEnabled(enabled);
}
Also used : WifiManager(android.net.wifi.WifiManager) SuppressLint(android.annotation.SuppressLint) 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