From 6b3e2d29787d5d87850d727c51fae2215d445338 Mon Sep 17 00:00:00 2001 From: DeaDshot14 Date: Tue, 10 Mar 2020 18:45:23 +0530 Subject: [PATCH] v1.0.2 released --- build.gradle | 2 +- library/build.gradle | 14 +---- library/publish.gradle | 2 +- .../com/andruid/magic/eezetensions/Context.kt | 58 ++++++++++++++++++- .../andruid/magic/eezetensions/Fragment.kt | 36 +++++++++--- .../com/andruid/magic/eezetensions/Intent.kt | 4 +- 6 files changed, 90 insertions(+), 26 deletions(-) diff --git a/build.gradle b/build.gradle index fb502b9..e7db1ba 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { - kotlin_version = "1.3.61" + kotlin_version = "1.3.70" dokka_version = "0.10.1" } diff --git a/library/build.gradle b/library/build.gradle index 7d956be..d7574f9 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -16,7 +16,7 @@ android { minSdkVersion 23 targetSdkVersion 29 versionCode 2 - versionName "1.0.1" + versionName "1.0.2" consumerProguardFiles "consumer-rules.pro" } @@ -44,18 +44,6 @@ android { outputFormat = 'html' outputDirectory = "$buildDir/dokka" } - - packagingOptions { - exclude 'META-INF/DEPENDENCIES' - exclude 'META-INF/LICENSE' - exclude 'META-INF/LICENSE.txt' - exclude 'META-INF/license.txt' - exclude 'META-INF/NOTICE' - exclude 'META-INF/NOTICE.txt' - exclude 'META-INF/notice.txt' - exclude 'META-INF/ASL2.0' - exclude("META-INF/*.kotlin_module") - } } dependencies { diff --git a/library/publish.gradle b/library/publish.gradle index 873e5cc..4b7bce4 100644 --- a/library/publish.gradle +++ b/library/publish.gradle @@ -1,7 +1,7 @@ ext { PUBLISH_GROUP_ID = 'com.andruid.magic' PUBLISH_ARTIFACT_ID = 'eezetensions' - PUBLISH_VERSION = '1.0.1' + PUBLISH_VERSION = '1.0.2' } apply from: 'https://raw.githubusercontent.com/blundell/release-android-library/master/android-release-aar.gradle' \ No newline at end of file diff --git a/library/src/main/java/com/andruid/magic/eezetensions/Context.kt b/library/src/main/java/com/andruid/magic/eezetensions/Context.kt index b275aab..73d43f3 100644 --- a/library/src/main/java/com/andruid/magic/eezetensions/Context.kt +++ b/library/src/main/java/com/andruid/magic/eezetensions/Context.kt @@ -5,12 +5,18 @@ package com.andruid.magic.eezetensions import android.Manifest import android.content.Context import android.content.Intent +import android.content.pm.PackageManager import android.net.ConnectivityManager import android.net.NetworkCapabilities import android.os.Build +import android.os.Handler +import android.os.Looper +import android.provider.Settings +import android.widget.Toast import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import androidx.annotation.RequiresPermission +import androidx.annotation.StringRes import androidx.core.content.ContextCompat import androidx.core.content.getSystemService @@ -55,4 +61,54 @@ fun Context.hasInternet(): Boolean { nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) } } ?: false -} \ No newline at end of file +} + +/** + * Extension function to get package version name for the application + * @return package version string + * @receiver context of the calling component + */ +fun Context.getPackageVersion(): String? { + return try { + val pInfo = packageManager.getPackageInfo(applicationContext.packageName, 0) + pInfo.versionName + } catch (e: PackageManager.NameNotFoundException) { + e.printStackTrace() + null + } +} + +/** + * Extension function to show toast from background threads + * @param msg toast message + * @param duration toast duration (default = [Toast.LENGTH_SHORT] + * @param fromBg toast from background (default = false) + * @receiver context of the calling component + */ +fun Context.toast(msg: String, duration: Int = Toast.LENGTH_SHORT, fromBg: Boolean = false) { + if (fromBg) { + Handler(Looper.getMainLooper()).post { + toast(msg, duration, false) + } + } else + Toast.makeText(this, msg, duration).show() +} + +/** + * Extension function to show toast from background threads + * @param msgRes string resource for toast message + * @param duration toast duration (default = [Toast.LENGTH_SHORT] + * @param fromBg toast from background (default = false) + * @receiver context of the calling component + */ +fun Context.toast(@StringRes msgRes: Int, duration: Int = Toast.LENGTH_SHORT, fromBg: Boolean = false) { + toast(getString(msgRes), duration, fromBg) +} + +/** + * Extension function to get device name set in settings + * @return user set device name + * @receiver context of the calling component + */ +fun Context.getUserDeviceName(): String = + Settings.Global.getString(contentResolver, "device_name") \ No newline at end of file diff --git a/library/src/main/java/com/andruid/magic/eezetensions/Fragment.kt b/library/src/main/java/com/andruid/magic/eezetensions/Fragment.kt index b6294f8..4ce6eca 100644 --- a/library/src/main/java/com/andruid/magic/eezetensions/Fragment.kt +++ b/library/src/main/java/com/andruid/magic/eezetensions/Fragment.kt @@ -3,16 +3,18 @@ package com.andruid.magic.eezetensions import android.content.Intent +import android.widget.Toast import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import androidx.annotation.RequiresPermission +import androidx.annotation.StringRes import androidx.fragment.app.Fragment /** * Extension function to load color from resource * @param color id of the color resource * @return color from color resource - * @receiver active fragment + * @receiver calling fragment */ fun Fragment.color(@ColorRes color: Int) = requireContext().color(color) @@ -20,23 +22,41 @@ fun Fragment.color(@ColorRes color: Int) = requireContext().color(color) * Extension function to load drawable from resource * @param res id of the drawable resource * @return drawable from drawable resource - * @receiver active fragment + * @receiver calling fragment */ fun Fragment.drawable(@DrawableRes res: Int) = requireContext().drawable(res) /** * Extension function to start foreground service above API 26 else start background service * @param intent to launch the service - * @receiver active fragment + * @receiver calling fragment */ fun Fragment.startFgOrBgService(intent: Intent) { requireContext().startFgOrBgService(intent) } /** - * Extension function to check whether device is connected to internet or not - * @return true/false - * @receiver active fragment + * Extension function to show toast from background threads + * @param msg toast message + * @param duration toast duration (default = [Toast.LENGTH_SHORT] + * @param fromBg toast from background (default = false) + * @receiver calling fragment */ -@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) -fun Fragment.hasInternet() = requireContext().hasInternet() \ No newline at end of file +fun Fragment.toast(msg: String, duration: Int = Toast.LENGTH_SHORT, fromBg: Boolean = false) { + requireContext().toast(msg, duration, fromBg) +} + +/** + * Extension function to show toast from background threads + * @param msgRes string resource for toast message + * @param duration toast duration (default = [Toast.LENGTH_SHORT] + * @param fromBg toast from background (default = false) + * @receiver calling fragment + */ +fun Fragment.toast( + @StringRes msgRes: Int, + duration: Int = Toast.LENGTH_SHORT, + fromBg: Boolean = false +) { + requireContext().toast(msgRes, duration, fromBg) +} \ No newline at end of file diff --git a/library/src/main/java/com/andruid/magic/eezetensions/Intent.kt b/library/src/main/java/com/andruid/magic/eezetensions/Intent.kt index b591df7..df411a7 100644 --- a/library/src/main/java/com/andruid/magic/eezetensions/Intent.kt +++ b/library/src/main/java/com/andruid/magic/eezetensions/Intent.kt @@ -8,12 +8,12 @@ import android.net.Uri import android.provider.Settings /** - * Get intent to show the settings screen of the app or permissions and other settings + * Get intent to show the settings screen of the app for permissions and other settings * @return intent to launch settings page * @receiver context of the component */ fun Context.buildSettingsIntent(): Intent { val uri = Uri.fromParts("package", packageName, null) return Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) - .setData(uri) + .setData(uri) } \ No newline at end of file