Migrated the following files in util module to Kotlin

- DeviceInfoUtil
- ExecutorUtils
- FragmentUtils
This commit is contained in:
Saifuddin 2024-11-16 21:27:57 +05:30
parent e30c0fd693
commit f63d054c3f
5 changed files with 73 additions and 74 deletions

View file

@ -13,7 +13,7 @@ object CommonsDateUtil {
/**
* Gets SimpleDateFormat for short date pattern.
* @return simpledateformat
* @return simpleDateFormat
*/
@JvmStatic
fun getIso8601DateFormatShort(): SimpleDateFormat {
@ -24,7 +24,7 @@ object CommonsDateUtil {
/**
* Gets SimpleDateFormat for date pattern returned by Media object.
* @return simpledateformat
* @return simpleDateFormat
*/
@JvmStatic
fun getMediaSimpleDateFormat(): SimpleDateFormat {

View file

@ -42,7 +42,10 @@ object DateUtil {
@JvmStatic
@Synchronized
fun getDateStringWithSkeletonPattern(date: Date, pattern: String): String {
return getCachedDateFormat(getBestDateTimePattern(Locale.getDefault(), pattern), Locale.getDefault(), false).format(date)
return getCachedDateFormat(
getBestDateTimePattern(Locale.getDefault(), pattern),
Locale.getDefault(), false
).format(date)
}
@JvmStatic

View file

@ -1,91 +1,80 @@
package fr.free.nrw.commons.utils;
package fr.free.nrw.commons.utils
import android.content.Context;
import android.os.Build;
import java.util.HashMap;
import java.util.Map;
import fr.free.nrw.commons.utils.model.ConnectionType;
import fr.free.nrw.commons.utils.model.NetworkConnectionType;
import static fr.free.nrw.commons.utils.model.ConnectionType.CELLULAR;
import static fr.free.nrw.commons.utils.model.ConnectionType.CELLULAR_3G;
import static fr.free.nrw.commons.utils.model.ConnectionType.CELLULAR_4G;
import static fr.free.nrw.commons.utils.model.ConnectionType.NO_INTERNET;
import static fr.free.nrw.commons.utils.model.ConnectionType.WIFI_NETWORK;
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.FOUR_G;
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.THREE_G;
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.TWO_G;
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.UNKNOWN;
import static fr.free.nrw.commons.utils.model.NetworkConnectionType.WIFI;
import android.content.Context
import android.os.Build
import fr.free.nrw.commons.utils.model.ConnectionType
import fr.free.nrw.commons.utils.model.NetworkConnectionType
/**
* Util class to get any information about the user's device
* Ensure that any sensitive information like IMEI is not fetched/shared without user's consent
*/
public class DeviceInfoUtil {
private static final Map<NetworkConnectionType, ConnectionType> TYPE_MAPPING = new HashMap<>();
static {
TYPE_MAPPING.put(TWO_G, CELLULAR);
TYPE_MAPPING.put(THREE_G, CELLULAR_3G);
TYPE_MAPPING.put(FOUR_G, CELLULAR_4G);
TYPE_MAPPING.put(WIFI, WIFI_NETWORK);
TYPE_MAPPING.put(UNKNOWN, CELLULAR);
}
object DeviceInfoUtil {
private val TYPE_MAPPING = mapOf(
NetworkConnectionType.TWO_G to ConnectionType.CELLULAR,
NetworkConnectionType.THREE_G to ConnectionType.CELLULAR_3G,
NetworkConnectionType.FOUR_G to ConnectionType.CELLULAR_4G,
NetworkConnectionType.WIFI to ConnectionType.WIFI_NETWORK,
NetworkConnectionType.UNKNOWN to ConnectionType.CELLULAR
)
/**
* Get network connection type
* @param context
* @return wifi/cellular-4g/cellular-3g/cellular-2g/no-internet
*/
public static ConnectionType getConnectionType(Context context) {
if (!NetworkUtils.isInternetConnectionEstablished(context)) {
return NO_INTERNET;
@JvmStatic
fun getConnectionType(context: Context): ConnectionType {
return if (!NetworkUtils.isInternetConnectionEstablished(context)) {
ConnectionType.NO_INTERNET
} else {
val networkType = NetworkUtils.getNetworkType(context)
TYPE_MAPPING[networkType] ?: ConnectionType.CELLULAR
}
NetworkConnectionType networkType = NetworkUtils.getNetworkType(context);
ConnectionType deviceNetworkType = TYPE_MAPPING.get(networkType);
return deviceNetworkType == null ? CELLULAR : deviceNetworkType;
}
/**
* Get Device manufacturer
* @return
*/
public static String getDeviceManufacturer() {
return Build.MANUFACTURER;
@JvmStatic
fun getDeviceManufacturer(): String {
return Build.MANUFACTURER
}
/**
* Get Device model name
* @return
*/
public static String getDeviceModel() {
return Build.MODEL;
@JvmStatic
fun getDeviceModel(): String {
return Build.MODEL
}
/**
* Get Android version. Eg. 4.4.2
* @return
*/
public static String getAndroidVersion() {
return Build.VERSION.RELEASE;
@JvmStatic
fun getAndroidVersion(): String {
return Build.VERSION.RELEASE
}
/**
* Get API Level. Eg. 26
* @return
*/
public static String getAPILevel() {
return Build.VERSION.SDK;
@JvmStatic
fun getAPILevel(): String {
return Build.VERSION.SDK
}
/**
* Get Device.
* @return
*/
public static String getDevice() {
return Build.DEVICE;
@JvmStatic
fun getDevice(): String {
return Build.DEVICE
}
}

View file

@ -1,31 +1,33 @@
package fr.free.nrw.commons.utils;
package fr.free.nrw.commons.utils
import android.os.Handler;
import android.os.Looper;
import android.os.Handler
import android.os.Looper
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Executor
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
public class ExecutorUtils {
object ExecutorUtils {
private static final Executor uiExecutor = command -> {
@JvmStatic
private val uiExecutor: Executor = Executor { command ->
if (Looper.myLooper() == Looper.getMainLooper()) {
command.run();
command.run()
} else {
new Handler(Looper.getMainLooper()).post(command);
Handler(Looper.getMainLooper()).post(command)
}
};
public static Executor uiExecutor() {
return uiExecutor;
}
private static final ExecutorService executor = Executors.newFixedThreadPool(3);
public static ExecutorService get() {
return executor;
@JvmStatic
fun uiExecutor(): Executor {
return uiExecutor
}
@JvmStatic
private val executor: ExecutorService = Executors.newFixedThreadPool(3)
@JvmStatic
fun get(): ExecutorService {
return executor
}
}

View file

@ -1,15 +1,20 @@
package fr.free.nrw.commons.utils;
package fr.free.nrw.commons.utils
import androidx.fragment.app.Fragment;
import androidx.fragment.app.Fragment
public class FragmentUtils {
object FragmentUtils {
/**
* Utility function to check whether the fragment UI is still active or not
* @param fragment
* @return
* @return Boolean
*/
public static boolean isFragmentUIActive(Fragment fragment) {
return fragment!=null && fragment.getActivity() != null && fragment.isAdded() && !fragment.isDetached() && !fragment.isRemoving();
@JvmStatic
fun isFragmentUIActive(fragment: Fragment?): Boolean {
return fragment != null &&
fragment.activity != null &&
fragment.isAdded &&
!fragment.isDetached &&
!fragment.isRemoving
}
}