Move from java/ to kotlin/ directory

Android 12 dynamic color usage on login screen
This commit is contained in:
2021-08-31 02:18:29 +03:00
parent 2453e534ae
commit 1209c37e24
135 changed files with 140 additions and 57 deletions
@@ -0,0 +1,112 @@
package com.meloda.fast.util
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.TypedValue
import androidx.annotation.AttrRes
import com.meloda.fast.BuildConfig
import com.meloda.fast.common.AppGlobal
object AndroidUtils {
fun px(dp: Float): Float {
return dp * (AppGlobal.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
}
fun px(dp: Int) = px(dp.toFloat())
fun dp(px: Float): Float {
return px / (AppGlobal.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
}
fun dp(px: Int) = dp(px.toFloat())
fun isDarkTheme(): Boolean {
val currentNightMode =
AppGlobal.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return when (currentNightMode) {
Configuration.UI_MODE_NIGHT_YES -> true
else -> false
}
}
//TODO
fun hasConnection(): Boolean {
return false
// val network = AppGlobal.connectivityManager.activeNetwork ?: return false
// val activeNetwork =
// AppGlobal.connectivityManager.getNetworkCapabilities(network) ?: return false
//
// return when {
// activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
// activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
// activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
// activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true
// else -> false
// }
}
fun getDisplayWidth(): Int {
return AppGlobal.resources.displayMetrics.widthPixels
}
fun getDisplayHeight(): Int {
return AppGlobal.resources.displayMetrics.heightPixels
}
fun isDeveloperSettingsEnabled(context: Context) = Settings.Secure.getInt(
context.contentResolver,
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED,
0
) == 1
@Suppress("DEPRECATION")
fun isCanInstallUnknownApps(context: Context) =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Settings.Secure.getInt(
context.contentResolver,
Settings.Secure.INSTALL_NON_MARKET_APPS
) == 1
} else {
AppGlobal.packageManager.canRequestPackageInstalls()
}
fun openInstallUnknownAppsScreen(context: Context) {
context.startActivity(Intent().apply {
action = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Settings.ACTION_SECURITY_SETTINGS
} else {
data = Uri.parse("package:${BuildConfig.APPLICATION_ID}")
Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES
}
})
}
//TODO
fun copyText(label: String? = "", text: String) {
// AppGlobal.clipboardManager.setPrimaryClip(ClipData.newPlainText(label, text))
}
fun getThemeAttrColor(context: Context, @AttrRes resId: Int): Int {
val typedValue = TypedValue()
context.theme.resolveAttribute(resId, typedValue, true)
val colorRes = typedValue.resourceId
var color = -1
try {
color = context.resources.getColor(colorRes, context.theme)
} catch (e: Exception) {
}
return color
}
}
@@ -0,0 +1,59 @@
package com.meloda.fast.util
import java.util.stream.Collectors
object ArrayUtils {
@SafeVarargs
fun <T> asString(vararg array: T): String {
if (array.isEmpty()) {
return ""
}
val builder = StringBuilder(array.size * 12)
builder.append(array[0])
for (i in 1 until array.size) {
builder.append(',')
builder.append(array[i])
}
return builder.toString()
}
fun asString(array: IntArray): String {
if (array.isEmpty()) {
return ""
}
val builder = StringBuilder(array.size * 12)
builder.append(array[0])
for (i in 1 until array.size) {
builder.append(',')
builder.append(array[i])
}
return builder.toString()
}
fun <T> asString(arrayList: ArrayList<T>): String {
return ArrayList<String>().apply {
arrayList.forEach { add(it.toString()) }
}.stream().collect(Collectors.joining(","))
}
fun <T> asString(list: List<T>): String = asString(list.asArrayList())
fun <T> cut(arrayList: ArrayList<T>, offset: Int, count: Int): ArrayList<T> {
if (arrayList.isEmpty()) return arrayListOf()
var lastPosition = offset + count
if (lastPosition > arrayList.size) lastPosition = arrayList.size
return ArrayList(arrayList.subList(offset, lastPosition))
}
fun ByteArray?.isNullOrEmpty() = this == null || this.isEmpty()
fun <E> List<E>.asArrayList(): ArrayList<E> {
return ArrayList(this)
}
}
@@ -0,0 +1,30 @@
package com.meloda.fast.util
import android.content.Context
import android.graphics.Color
import androidx.annotation.ColorInt
import com.meloda.fast.R
object ColorUtils {
@ColorInt
fun getColorAccent(context: Context): Int {
return AndroidUtils.getThemeAttrColor(context, R.attr.colorAccent)
}
@ColorInt
fun getColorPrimary(context: Context): Int {
return AndroidUtils.getThemeAttrColor(context, R.attr.colorPrimary)
}
@JvmOverloads
fun darkenColor(color: Int, darkFactor: Float = 0.75f): Int {
var newColor = color
val hsv = FloatArray(3)
Color.colorToHSV(newColor, hsv)
hsv[2] *= darkFactor
newColor = Color.HSVToColor(hsv)
return newColor
}
}
@@ -0,0 +1,55 @@
package com.meloda.fast.util
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.widget.ImageView
object ImageUtils {
fun loadImage(image: String, imageView: ImageView, placeholder: Drawable?) {
if (image.isEmpty()) return
// if (imageView is SimpleDraweeView) {
// imageView.setImageURI(image)
// return
// }
//
// val picasso = Picasso.get()
// .load(image)
// .priority(Picasso.Priority.LOW)
// if (placeholder != null) picasso.placeholder(placeholder)
//
// picasso.into(imageView)
}
fun loadImage(image: String?, listener: OnLoadListener?) {
if (image.isNullOrEmpty()) return
// val picasso = Picasso.get()
// .load(image)
// .priority(Picasso.Priority.LOW)
//
// val target = object : Target {
// override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
//
// }
//
// override fun onBitmapFailed(e: Exception, errorDrawable: Drawable?) {
// listener?.onError(e)
// }
//
// override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
// listener?.onLoad(bitmap)
// }
// }
// picasso.into(target)
}
interface OnLoadListener {
fun onLoad(bitmap: Bitmap)
fun onError(e: Exception)
}
}
@@ -0,0 +1,16 @@
package com.meloda.fast.util
import android.view.View
import com.meloda.fast.common.AppGlobal
object KeyboardUtils {
fun hideKeyboardFrom(focusedView: View?) {
AppGlobal.inputMethodManager.hideSoftInputFromWindow(focusedView?.windowToken, 0)
}
fun showKeyboard(viewToFocus: View) {
AppGlobal.inputMethodManager.showSoftInput(viewToFocus, 0)
}
}
@@ -0,0 +1,15 @@
package com.meloda.fast.util
object TextUtils {
fun getFirstLetterFromString(string: String): String {
for (i in string.indices) {
val char = string[i]
if (char.isLetter()) return char.toString()
}
return ""
}
}
@@ -0,0 +1,17 @@
package com.meloda.fast.util
import java.util.*
object TimeUtils {
fun removeTime(date: Date): Long {
return Calendar.getInstance().apply {
time = date
this[Calendar.HOUR_OF_DAY] = 0
this[Calendar.MINUTE] = 0
this[Calendar.SECOND] = 0
this[Calendar.MILLISECOND] = 0
}.timeInMillis
}
}
@@ -0,0 +1,49 @@
package com.meloda.fast.util
import android.content.Context
import com.meloda.fast.util.ArrayUtils.isNullOrEmpty
import com.meloda.fast.R
import com.meloda.fast.io.BytesOutputStream
import java.io.ByteArrayInputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
object Utils {
fun getLocalizedThrowable(context: Context, t: Throwable): String {
return context.getString(R.string.error, t.message.toString())
}
fun serialize(source: Any?): ByteArray? {
try {
val bos = BytesOutputStream()
val out = ObjectOutputStream(bos)
out.writeObject(source)
out.close()
return bos.byteArray
} catch (e: IOException) {
e.printStackTrace()
}
return null
}
fun deserialize(source: ByteArray?): Any? {
if (source.isNullOrEmpty()) {
return null
}
try {
val bis = ByteArrayInputStream(source)
val `in` = ObjectInputStream(bis)
val o = `in`.readObject()
`in`.close()
return o
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
}
@@ -0,0 +1,336 @@
package com.meloda.fast.util
import android.content.Context
import android.graphics.drawable.Drawable
import androidx.annotation.WorkerThread
import androidx.core.content.ContextCompat
import com.meloda.fast.concurrent.TaskManager
import com.meloda.fast.extensions.ContextExtensions.color
import com.meloda.fast.extensions.ContextExtensions.drawable
import com.meloda.fast.extensions.DrawableExtensions.tint
import com.meloda.fast.extensions.StringExtensions.lowerCase
import com.meloda.fast.R
import com.meloda.fast.api.model.*
import com.meloda.fast.common.AppGlobal
import com.meloda.fast.api.OnResponseListener
import com.meloda.fast.api.util.VKUtil
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.abs
object VKUtils {
fun getUserOnline(user: VKUser): String {
val r = AppGlobal.resources
return if (user.isOnline) {
if (user.isOnlineMobile) {
r.getString(R.string.user_online_mobile)
} else {
r.getString(R.string.user_online)
}
} else {
if (user.lastSeen == 0) {
r.getString(R.string.user_last_seen_recently)
} else {
r.getString(
R.string.user_last_seen_at,
VKUtil.getLastSeenTime(user.lastSeen * 1000L)
)
}
}
}
fun getUserOnlineIcon(
context: Context,
conversation: VKConversation?,
peerUser: VKUser?
): Drawable? {
return if (conversation != null) {
if (conversation.isUser() && peerUser != null) {
if (!peerUser.isOnline) {
null
} else {
ContextCompat.getDrawable(
context,
if (peerUser.isOnlineMobile) R.drawable.ic_online_mobile else R.drawable.ic_online_pc
)
}
} else null
} else {
if (peerUser!!.isOnline) {
ContextCompat.getDrawable(
context,
if (peerUser.isOnlineMobile) R.drawable.ic_online_mobile else R.drawable.ic_online_pc
)
} else {
null
}
}
}
fun getUserOnlineIcon(context: Context, user: VKUser): Drawable? {
return getUserOnlineIcon(context, null, user)
}
// fun getAvatarPlaceholder(context: Context, dialogTitle: String): TextDrawable {
// return TextDrawable.builder().buildRound(
// if (dialogTitle.isEmpty()) "" else {
// TextUtils.getFirstLetterFromString(dialogTitle)
// },
// context.color(R.color.accent)
// )
// }
@Deprecated("")
@WorkerThread
fun searchUser(id: Int, onResponseListener: OnResponseListener<VKUser>? = null): VKUser? {
return if (VKUtil.isGroupId(id) || VKUtil.isChatId(id)) {
null
} else {
// MemoryCache.getUserById(id)?.let { return it }
//
// if (BuildConfig.DEBUG) {
// Log.d(VKUtil.TAG, "User with id $id not found")
// }
//
// TaskManager.loadUser(VKApiKeys.UPDATE_USER, id, onResponseListener)
return null
}
}
@Deprecated("")
@WorkerThread
fun searchGroup(id: Int, onResponseListener: OnResponseListener<VKGroup>? = null): VKGroup? {
return if (!VKUtil.isGroupId(id) || VKUtil.isChatId(id)) {
null
} else {
// MemoryCache.getGroupById(abs(id))?.let { return it }
//
// if (BuildConfig.DEBUG) {
// Log.d(VKUtil.TAG, "Group with id $id not found")
// }
//
// TaskManager.loadGroup(VKApiKeys.UPDATE_GROUP, abs(id), onResponseListener)
return null
}
}
fun getAttachmentText(context: Context, attachments: List<VKModel>): String {
val resId: Int
if (attachments.isNotEmpty()) {
if (attachments.size > 1) {
var oneType = true
val firstType = attachments[0].attachmentType
// val className = attachments[0].javaClass.simpleName
for (model in attachments) {
// if (model.javaClass.simpleName != className) {
if (model.attachmentType != firstType) {
oneType = false
break
}
}
return if (oneType) {
// val objectClass: Class<VKModel> = attachments[0].javaClass
resId = when (firstType) {
VKAttachments.Type.PHOTO -> {
R.string.message_attachment_photos
}
VKAttachments.Type.VIDEO -> {
R.string.message_attachment_videos
}
VKAttachments.Type.AUDIO -> {
R.string.message_attachment_audios
}
VKAttachments.Type.DOCUMENT -> {
R.string.message_attachment_docs
}
else -> -1
}
if (resId == -1) "Unknown attachments" else context.getString(
resId,
attachments.size
).toLowerCase(Locale.getDefault())
} else {
context.getString(R.string.message_attachments_many)
}
} else {
// val objectClass: Class<VKModel> = attachments[0].javaClass
val firstType = attachments[0].attachmentType
resId = when (firstType) {
VKAttachments.Type.PHOTO -> R.string.message_attachment_photo
VKAttachments.Type.AUDIO -> R.string.message_attachment_audio
VKAttachments.Type.VIDEO -> R.string.message_attachment_video
VKAttachments.Type.DOCUMENT -> R.string.message_attachment_doc
VKAttachments.Type.GRAFFITI -> R.string.message_attachment_graffiti
VKAttachments.Type.VOICE_MESSAGE -> R.string.message_attachment_voice
VKAttachments.Type.STICKER -> R.string.message_attachment_sticker
VKAttachments.Type.GIFT -> R.string.message_attachment_gift
VKAttachments.Type.LINK -> R.string.message_attachment_link
VKAttachments.Type.POLL -> R.string.message_attachment_poll
VKAttachments.Type.CALL -> R.string.message_attachment_call
VKAttachments.Type.WALL_POST -> R.string.message_attachment_wall_post
VKAttachments.Type.WALL_REPLY -> R.string.message_attachment_wall_reply
else -> return "Unknown"
}
}
} else {
return ""
}
return context.getString(resId)
}
fun getAttachmentDrawable(context: Context, attachments: List<VKModel>): Drawable? {
if (attachments.isEmpty() || attachments.size > 1) return null
val resId = when (attachments[0].attachmentType) {
VKAttachments.Type.PHOTO -> R.drawable.ic_message_attachment_camera
VKAttachments.Type.AUDIO -> R.drawable.ic_message_attachment_audio
VKAttachments.Type.VIDEO -> R.drawable.ic_message_attachment_video
VKAttachments.Type.DOCUMENT -> R.drawable.ic_message_attachment_doc
VKAttachments.Type.GRAFFITI -> R.drawable.ic_message_attachment_graffiti
VKAttachments.Type.VOICE_MESSAGE -> R.drawable.ic_message_attachment_audio_message
VKAttachments.Type.STICKER -> R.drawable.ic_message_attachment_sticker
VKAttachments.Type.GIFT -> R.drawable.ic_message_attachment_gift
VKAttachments.Type.LINK -> R.drawable.ic_message_attachment_link
VKAttachments.Type.POLL -> R.drawable.ic_message_attachment_poll
VKAttachments.Type.CALL -> R.drawable.ic_message_attachment_call
else -> null
}
resId?.let { return context.drawable(it).tint(context.color(R.color.accent)) }
return null
}
fun getFwdText(context: Context, forwardedMessages: List<VKMessage>): String {
return if (forwardedMessages.isNotEmpty()) {
if (forwardedMessages.size > 1) {
context.getString(R.string.message_fwd_many, forwardedMessages.size).lowerCase()
} else {
context.getString(R.string.message_fwd_one)
}
} else ""
}
@Deprecated("need to rewrite")
fun getActionText(
context: Context,
lastMessage: VKMessage,
onResponseListener: OnResponseListener<String>
) {
TaskManager.execute {
lastMessage.action?.let {
var result = ""
when (it.type) {
VKMessageAction.Type.CHAT_CREATE -> result = context.getString(
R.string.message_action_created_chat,
""
)
VKMessageAction.Type.INVITE_USER -> result =
if (lastMessage.fromId == lastMessage.action!!.memberId) {
context.getString(R.string.message_action_returned_to_chat, "")
} else {
""
// val invited = MemoryCache.getUserById(lastMessage.action!!.memberId)
// context.getString(R.string.message_action_invited_user, invited)
}
VKMessageAction.Type.INVITE_USER_BY_LINK -> result = context.getString(
R.string.message_action_invited_by_link,
""
)
VKMessageAction.Type.KICK_USER -> result =
if (lastMessage.fromId == lastMessage.action!!.memberId) {
context.getString(R.string.message_action_left_from_chat, "")
} else {
""
// val kicked = MemoryCache.getUserById(lastMessage.action!!.memberId)
// context.getString(R.string.message_action_kicked_user, kicked)
}
VKMessageAction.Type.PHOTO_REMOVE -> result = context.getString(
R.string.message_action_removed_photo,
""
)
VKMessageAction.Type.PHOTO_UPDATE -> result = context.getString(
R.string.message_action_updated_photo,
""
)
VKMessageAction.Type.PIN_MESSAGE -> result = context.getString(
R.string.message_action_pinned_message,
""
)
VKMessageAction.Type.UNPIN_MESSAGE -> result = context.getString(
R.string.message_action_unpinned_message,
""
)
VKMessageAction.Type.TITLE_UPDATE -> result = context.getString(
R.string.message_action_updated_title,
""
)
}
AppGlobal.post { onResponseListener.onResponse(result) }
}
}
}
fun getTime(context: Context, lastMessage: VKMessage): String {
val then = lastMessage.date * 1000L
val now = System.currentTimeMillis()
val change = abs(now - then)
val seconds = change / 1000
if (seconds == 0L) {
return context.getString(R.string.time_format_now)
}
val minutes = seconds / 60
if (minutes == 0L) {
return context.getString(R.string.time_format_second, seconds)
}
val hours = minutes / 60
if (hours == 0L) {
return context.getString(R.string.time_format_minute, minutes)
}
val days = hours / 24
if (days == 0L) {
return context.getString(R.string.time_format_hour, hours)
}
val months = days / 30
if (months == 0L) {
return context.getString(R.string.time_format_day, days)
}
val years = months / 12
if (years == 0L) {
return context.getString(R.string.time_format_month, months)
} else if (years > 0L) {
return context.getString(R.string.time_format_year, years)
}
return SimpleDateFormat("HH:mm", Locale.getDefault()).format(then)
}
}
@@ -0,0 +1,55 @@
package com.meloda.fast.util
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar
import com.meloda.fast.extensions.ContextExtensions.color
import com.meloda.fast.R
import com.meloda.fast.widget.CircleImageView
import com.meloda.fast.api.model.VKUser
import com.meloda.fast.api.util.VKUtil
object ViewUtils {
fun showErrorSnackbar(view: View, t: Throwable) {
Snackbar.make(
view,
Utils.getLocalizedThrowable(view.context, t),
Snackbar.LENGTH_LONG
).show()
}
fun showErrorToast(context: Context, t: Throwable) {
Toast.makeText(
context,
Utils.getLocalizedThrowable(context, t),
Toast.LENGTH_LONG
).show()
}
fun prepareNavigationHeader(view: View, user: VKUser) {
val profileName = view.findViewById<TextView>(R.id.headerName)
profileName.text = user.toString()
val profileStatus = view.findViewById<TextView>(R.id.headerStatus)
val statusText = if (TextUtils.isEmpty(user.status)) "@id${user.userId}" else user.status
profileStatus.text = statusText
val profileAvatar: CircleImageView = view.findViewById(R.id.headerAvatar)
if (AndroidUtils.hasConnection()) {
// Picasso.get().load(VKUtil.getUserPhoto(user)).into(profileAvatar)
} else {
profileAvatar.setImageDrawable(ColorDrawable(view.context.color(R.color.accent)))
}
}
}