Search in sources :

Example 16 with RequiresPermission

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

the class VibrateUtils method cancel.

/**
 * Cancel vibrate.
 * <p>Must hold {@code <uses-permission android:name="android.permission.VIBRATE" />}</p>
 */
@RequiresPermission(VIBRATE)
public static void cancel() {
    Vibrator vibrator = getVibrator();
    if (vibrator == null)
        return;
    vibrator.cancel();
}
Also used : Vibrator(android.os.Vibrator) RequiresPermission(androidx.annotation.RequiresPermission)

Example 17 with RequiresPermission

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

the class NetworkUtils method getIPAddress.

/**
 * Return the ip address.
 * <p>Must hold {@code <uses-permission android:name="android.permission.INTERNET" />}</p>
 *
 * @param useIPv4 True to use ipv4, false otherwise.
 * @return the ip address
 */
@RequiresPermission(INTERNET)
public static String getIPAddress(final boolean useIPv4) {
    try {
        Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
        LinkedList<InetAddress> adds = new LinkedList<>();
        while (nis.hasMoreElements()) {
            NetworkInterface ni = nis.nextElement();
            // To prevent phone of xiaomi return "10.0.2.15"
            if (!ni.isUp() || ni.isLoopback())
                continue;
            Enumeration<InetAddress> addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
                adds.addFirst(addresses.nextElement());
            }
        }
        for (InetAddress add : adds) {
            if (!add.isLoopbackAddress()) {
                String hostAddress = add.getHostAddress();
                boolean isIPv4 = hostAddress.indexOf(':') < 0;
                if (useIPv4) {
                    if (isIPv4)
                        return hostAddress;
                } else {
                    if (!isIPv4) {
                        int index = hostAddress.indexOf('%');
                        return index < 0 ? hostAddress.toUpperCase() : hostAddress.substring(0, index).toUpperCase();
                    }
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    return "";
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress) LinkedList(java.util.LinkedList) SuppressLint(android.annotation.SuppressLint) RequiresPermission(androidx.annotation.RequiresPermission)

Example 18 with RequiresPermission

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

the class NetworkUtils method isEthernet.

/**
 * Return whether using ethernet.
 * <p>Must hold
 * {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />}</p>
 *
 * @return {@code true}: yes<br>{@code false}: no
 */
@RequiresPermission(ACCESS_NETWORK_STATE)
private static boolean isEthernet() {
    final ConnectivityManager cm = (ConnectivityManager) Utils.getApp().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null)
        return false;
    final NetworkInfo info = cm.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
    if (info == null)
        return false;
    NetworkInfo.State state = info.getState();
    if (null == state)
        return false;
    return state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING;
}
Also used : NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) RequiresPermission(androidx.annotation.RequiresPermission)

Example 19 with RequiresPermission

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

the class NetworkUtils method startScanWifi.

private static void startScanWifi() {
    sPreWifiScanResults = new WifiScanResults();
    sScanWifiTimer = new Timer();
    sScanWifiTimer.schedule(new TimerTask() {

        @RequiresPermission(allOf = { ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, ACCESS_COARSE_LOCATION })
        @Override
        public void run() {
            startScanWifiIfEnabled();
            WifiScanResults scanResults = getWifiScanResult();
            if (isSameScanResults(sPreWifiScanResults.allResults, scanResults.allResults)) {
                return;
            }
            sPreWifiScanResults = scanResults;
            UtilsBridge.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    for (Utils.Consumer<WifiScanResults> consumer : SCAN_RESULT_CONSUMERS) {
                        consumer.accept(sPreWifiScanResults);
                    }
                }
            });
        }
    }, 0, SCAN_PERIOD_MILLIS);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) TextUtils(android.text.TextUtils) RequiresPermission(androidx.annotation.RequiresPermission)

Example 20 with RequiresPermission

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

the class DeviceUtils 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)
private 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