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();
}
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 "";
}
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;
}
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);
}
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);
}
Aggregations