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

View file

@ -42,7 +42,10 @@ object DateUtil {
@JvmStatic @JvmStatic
@Synchronized @Synchronized
fun getDateStringWithSkeletonPattern(date: Date, pattern: String): String { 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 @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.content.Context
import android.os.Build; import android.os.Build
import fr.free.nrw.commons.utils.model.ConnectionType
import java.util.HashMap; import fr.free.nrw.commons.utils.model.NetworkConnectionType
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;
/** /**
* Util class to get any information about the user's device * 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 * Ensure that any sensitive information like IMEI is not fetched/shared without user's consent
*/ */
public class DeviceInfoUtil { object DeviceInfoUtil {
private static final Map<NetworkConnectionType, ConnectionType> TYPE_MAPPING = new HashMap<>(); private val TYPE_MAPPING = mapOf(
NetworkConnectionType.TWO_G to ConnectionType.CELLULAR,
static { NetworkConnectionType.THREE_G to ConnectionType.CELLULAR_3G,
TYPE_MAPPING.put(TWO_G, CELLULAR); NetworkConnectionType.FOUR_G to ConnectionType.CELLULAR_4G,
TYPE_MAPPING.put(THREE_G, CELLULAR_3G); NetworkConnectionType.WIFI to ConnectionType.WIFI_NETWORK,
TYPE_MAPPING.put(FOUR_G, CELLULAR_4G); NetworkConnectionType.UNKNOWN to ConnectionType.CELLULAR
TYPE_MAPPING.put(WIFI, WIFI_NETWORK); )
TYPE_MAPPING.put(UNKNOWN, CELLULAR);
}
/** /**
* Get network connection type * Get network connection type
* @param context * @param context
* @return wifi/cellular-4g/cellular-3g/cellular-2g/no-internet * @return wifi/cellular-4g/cellular-3g/cellular-2g/no-internet
*/ */
public static ConnectionType getConnectionType(Context context) { @JvmStatic
if (!NetworkUtils.isInternetConnectionEstablished(context)) { fun getConnectionType(context: Context): ConnectionType {
return NO_INTERNET; 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 * Get Device manufacturer
* @return * @return
*/ */
public static String getDeviceManufacturer() { @JvmStatic
return Build.MANUFACTURER; fun getDeviceManufacturer(): String {
return Build.MANUFACTURER
} }
/** /**
* Get Device model name * Get Device model name
* @return * @return
*/ */
public static String getDeviceModel() { @JvmStatic
return Build.MODEL; fun getDeviceModel(): String {
return Build.MODEL
} }
/** /**
* Get Android version. Eg. 4.4.2 * Get Android version. Eg. 4.4.2
* @return * @return
*/ */
public static String getAndroidVersion() { @JvmStatic
return Build.VERSION.RELEASE; fun getAndroidVersion(): String {
return Build.VERSION.RELEASE
} }
/** /**
* Get API Level. Eg. 26 * Get API Level. Eg. 26
* @return * @return
*/ */
public static String getAPILevel() { @JvmStatic
return Build.VERSION.SDK; fun getAPILevel(): String {
return Build.VERSION.SDK
} }
/** /**
* Get Device. * Get Device.
* @return * @return
*/ */
public static String getDevice() { @JvmStatic
return Build.DEVICE; 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.Handler
import android.os.Looper; import android.os.Looper
import java.util.concurrent.Executor; import java.util.concurrent.Executor
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors; 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()) { if (Looper.myLooper() == Looper.getMainLooper()) {
command.run(); command.run()
} else { } else {
new Handler(Looper.getMainLooper()).post(command); Handler(Looper.getMainLooper()).post(command)
} }
};
public static Executor uiExecutor() {
return uiExecutor;
} }
@JvmStatic
private static final ExecutorService executor = Executors.newFixedThreadPool(3); fun uiExecutor(): Executor {
return uiExecutor
public static ExecutorService get() {
return executor;
} }
@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 * Utility function to check whether the fragment UI is still active or not
* @param fragment * @param fragment
* @return * @return Boolean
*/ */
public static boolean isFragmentUIActive(Fragment fragment) { @JvmStatic
return fragment!=null && fragment.getActivity() != null && fragment.isAdded() && !fragment.isDetached() && !fragment.isRemoving(); fun isFragmentUIActive(fragment: Fragment?): Boolean {
return fragment != null &&
fragment.activity != null &&
fragment.isAdded &&
!fragment.isDetached &&
!fragment.isRemoving
} }
} }