Merge pull request #146 from TouchInstinct/added-extensions
Added device utils extensions
This commit is contained in:
commit
dfdee90a97
|
|
@ -39,7 +39,7 @@ abstract class MviViewModel<NavArgs : Parcelable, Action : ViewAction, State : V
|
|||
internal val state = Transformations.distinctUntilChanged(_state)
|
||||
|
||||
protected val currentState: State
|
||||
get() = state.value ?: initialState
|
||||
get() = _state.value ?: initialState
|
||||
|
||||
abstract fun dispatchAction(action: Action)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
package ru.touchin.roboswag.components.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.drawable.Drawable
|
||||
import androidx.annotation.ColorRes
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.core.content.ContextCompat
|
||||
|
||||
fun Context.getColorSimple(@ColorRes id: Int): Int = ContextCompat.getColor(this, id)
|
||||
|
||||
fun Context.getDrawableSimple(@DrawableRes id: Int): Drawable? = ContextCompat.getDrawable(this, id)
|
||||
|
|
@ -122,7 +122,7 @@ object UiUtils {
|
|||
* @param activity Activity of action bar;
|
||||
* @return Height of action bar.
|
||||
*/
|
||||
fun getActionBarHeight(activity: Activity): Int = 56.px
|
||||
fun getActionBarHeight(): Int = 56.px
|
||||
|
||||
/**
|
||||
* Returns status bar (on top where system info is) common height.
|
||||
|
|
|
|||
|
|
@ -1,154 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Touch Instinct
|
||||
*
|
||||
* This file is part of RoboSwag library.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package ru.touchin.templates;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Process;
|
||||
import androidx.annotation.NonNull;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
/**
|
||||
* Utility class that is providing common methods related to android device.
|
||||
*/
|
||||
public final class DeviceUtils {
|
||||
|
||||
/**
|
||||
* Detects active network type.
|
||||
*
|
||||
* @param context Application context
|
||||
* @return Active network type {@link NetworkType}
|
||||
*/
|
||||
@NonNull
|
||||
public static NetworkType getNetworkType(@NonNull final Context context) {
|
||||
if (context.checkPermission(Manifest.permission.ACCESS_NETWORK_STATE, Process.myPid(), Process.myUid())
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
return NetworkType.UNKNOWN;
|
||||
}
|
||||
final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (cm == null) {
|
||||
return NetworkType.UNKNOWN;
|
||||
}
|
||||
@SuppressLint("MissingPermission") final NetworkInfo info = cm.getActiveNetworkInfo();
|
||||
if (info == null || !info.isConnected()) {
|
||||
return NetworkType.NONE;
|
||||
}
|
||||
switch (info.getType()) {
|
||||
case ConnectivityManager.TYPE_WIFI:
|
||||
return NetworkType.WI_FI;
|
||||
case ConnectivityManager.TYPE_MOBILE:
|
||||
return getMobileNetworkType(info);
|
||||
default:
|
||||
return NetworkType.UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detects if some network connected.
|
||||
*
|
||||
* @param context Application context
|
||||
* @return true if network connected, false otherwise.
|
||||
*/
|
||||
public static boolean isNetworkConnected(@NonNull final Context context) {
|
||||
return getNetworkType(context) != NetworkType.NONE;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static NetworkType getMobileNetworkType(@NonNull final NetworkInfo info) {
|
||||
switch (info.getSubtype()) {
|
||||
case TelephonyManager.NETWORK_TYPE_GPRS:
|
||||
case TelephonyManager.NETWORK_TYPE_EDGE:
|
||||
case TelephonyManager.NETWORK_TYPE_CDMA:
|
||||
case TelephonyManager.NETWORK_TYPE_1xRTT:
|
||||
case TelephonyManager.NETWORK_TYPE_IDEN:
|
||||
return NetworkType.MOBILE_2G;
|
||||
case TelephonyManager.NETWORK_TYPE_UMTS:
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_0:
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_A:
|
||||
case TelephonyManager.NETWORK_TYPE_HSDPA:
|
||||
case TelephonyManager.NETWORK_TYPE_HSUPA:
|
||||
case TelephonyManager.NETWORK_TYPE_HSPA:
|
||||
case TelephonyManager.NETWORK_TYPE_EVDO_B:
|
||||
case TelephonyManager.NETWORK_TYPE_EHRPD:
|
||||
case TelephonyManager.NETWORK_TYPE_HSPAP:
|
||||
return NetworkType.MOBILE_3G;
|
||||
case TelephonyManager.NETWORK_TYPE_LTE:
|
||||
case 19: // NETWORK_TYPE_LTE_CA is hide
|
||||
return NetworkType.MOBILE_LTE;
|
||||
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
|
||||
default:
|
||||
return NetworkType.UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
private DeviceUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Available network types.
|
||||
*/
|
||||
public enum NetworkType {
|
||||
/**
|
||||
* Mobile 2G network.
|
||||
*/
|
||||
MOBILE_2G("2g"),
|
||||
/**
|
||||
* Mobile 3G network.
|
||||
*/
|
||||
MOBILE_3G("3g"),
|
||||
/**
|
||||
* Mobile LTE network.
|
||||
*/
|
||||
MOBILE_LTE("lte"),
|
||||
/**
|
||||
* Wi-Fi network.
|
||||
*/
|
||||
WI_FI("Wi-Fi"),
|
||||
/**
|
||||
* Unknown network type.
|
||||
*/
|
||||
UNKNOWN("unknown"),
|
||||
/**
|
||||
* No network.
|
||||
*/
|
||||
NONE("none");
|
||||
|
||||
@NonNull
|
||||
private final String name;
|
||||
|
||||
NetworkType(@NonNull final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Network type readable name.
|
||||
*/
|
||||
@NonNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Touch Instinct
|
||||
*
|
||||
* This file is part of RoboSwag library.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
package ru.touchin.templates
|
||||
|
||||
import android.content.Context
|
||||
import android.net.NetworkInfo
|
||||
import android.telephony.TelephonyManager
|
||||
|
||||
/**
|
||||
* Utility class that is providing common methods related to android device.
|
||||
*/
|
||||
object DeviceUtils {
|
||||
/**
|
||||
* Detects active network type.
|
||||
*
|
||||
* @param context Application context
|
||||
* @return Active network type [NetworkType]
|
||||
*/
|
||||
@Deprecated(
|
||||
"Use extension instead",
|
||||
ReplaceWith("context.getNetworkType()")
|
||||
)
|
||||
fun getNetworkType(context: Context) = context.getNetworkType()
|
||||
|
||||
/**
|
||||
* Detects if some network connected.
|
||||
*
|
||||
* @param context Application context
|
||||
* @return true if network connected, false otherwise.
|
||||
*/
|
||||
@Deprecated(
|
||||
"Use extension instead",
|
||||
ReplaceWith("context.isNetworkConnected()")
|
||||
)
|
||||
fun isNetworkConnected(context: Context) = context.isNetworkConnected()
|
||||
|
||||
|
||||
fun getMobileNetworkType(info: NetworkInfo): NetworkType =
|
||||
when (info.subtype) {
|
||||
TelephonyManager.NETWORK_TYPE_GPRS,
|
||||
TelephonyManager.NETWORK_TYPE_EDGE,
|
||||
TelephonyManager.NETWORK_TYPE_CDMA,
|
||||
TelephonyManager.NETWORK_TYPE_1xRTT,
|
||||
TelephonyManager.NETWORK_TYPE_IDEN -> NetworkType.MOBILE_2G
|
||||
TelephonyManager.NETWORK_TYPE_UMTS,
|
||||
TelephonyManager.NETWORK_TYPE_EVDO_0,
|
||||
TelephonyManager.NETWORK_TYPE_EVDO_A,
|
||||
TelephonyManager.NETWORK_TYPE_HSDPA,
|
||||
TelephonyManager.NETWORK_TYPE_HSUPA,
|
||||
TelephonyManager.NETWORK_TYPE_HSPA,
|
||||
TelephonyManager.NETWORK_TYPE_EVDO_B,
|
||||
TelephonyManager.NETWORK_TYPE_EHRPD,
|
||||
TelephonyManager.NETWORK_TYPE_HSPAP -> NetworkType.MOBILE_3G
|
||||
TelephonyManager.NETWORK_TYPE_LTE,
|
||||
19 -> NetworkType.MOBILE_LTE
|
||||
TelephonyManager.NETWORK_TYPE_UNKNOWN -> NetworkType.UNKNOWN
|
||||
else -> NetworkType.UNKNOWN
|
||||
}
|
||||
|
||||
/**
|
||||
* Available network types.
|
||||
*/
|
||||
enum class NetworkType(val networkName: String) {
|
||||
/**
|
||||
* Mobile 2G network.
|
||||
*/
|
||||
MOBILE_2G("2g"),
|
||||
|
||||
/**
|
||||
* Mobile 3G network.
|
||||
*/
|
||||
MOBILE_3G("3g"),
|
||||
|
||||
/**
|
||||
* Mobile LTE network.
|
||||
*/
|
||||
MOBILE_LTE("lte"),
|
||||
|
||||
/**
|
||||
* Wi-Fi network.
|
||||
*/
|
||||
WI_FI("Wi-Fi"),
|
||||
|
||||
/**
|
||||
* Unknown network type.
|
||||
*/
|
||||
UNKNOWN("unknown"),
|
||||
|
||||
/**
|
||||
* No network.
|
||||
*/
|
||||
NONE("none");
|
||||
|
||||
/**
|
||||
* @return Network type readable name.
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
package ru.touchin.templates
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.hardware.biometrics.BiometricManager
|
||||
import android.net.ConnectivityManager
|
||||
import android.os.Build
|
||||
import android.os.Process
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.annotation.RequiresPermission
|
||||
import androidx.core.hardware.fingerprint.FingerprintManagerCompat
|
||||
import ru.touchin.templates.DeviceUtils.NetworkType
|
||||
import ru.touchin.templates.DeviceUtils.getMobileNetworkType
|
||||
|
||||
fun Context.isNetworkConnected(): Boolean = getNetworkType() != NetworkType.NONE
|
||||
|
||||
fun Context.getNetworkType(): NetworkType {
|
||||
if (checkPermission(Manifest.permission.ACCESS_NETWORK_STATE, Process.myPid(), Process.myUid())
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
return NetworkType.UNKNOWN
|
||||
}
|
||||
val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager?
|
||||
?: return NetworkType.UNKNOWN
|
||||
@SuppressLint("MissingPermission") val info = cm.activeNetworkInfo
|
||||
return if (info == null || !info.isConnected) {
|
||||
NetworkType.NONE
|
||||
} else when (info.type) {
|
||||
ConnectivityManager.TYPE_WIFI -> NetworkType.WI_FI
|
||||
ConnectivityManager.TYPE_MOBILE -> getMobileNetworkType(info)
|
||||
else -> NetworkType.UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
@Suppress("InlinedApi")
|
||||
@RequiresPermission(anyOf = [Manifest.permission.USE_FINGERPRINT, Manifest.permission.USE_BIOMETRIC])
|
||||
fun Context.canAuthenticateWithBiometrics(): Boolean = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
||||
val fingerprintManagerCompat = FingerprintManagerCompat.from(this)
|
||||
fingerprintManagerCompat.hasEnrolledFingerprints() && fingerprintManagerCompat.isHardwareDetected
|
||||
} else {
|
||||
getSystemService(BiometricManager::class.java)?.let { biometricManager ->
|
||||
biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS
|
||||
} ?: false
|
||||
}
|
||||
Loading…
Reference in New Issue