Unverified Commit ae8d3098 authored by Vitalii Koshura's avatar Vitalii Koshura Committed by GitHub

Merge pull request #4003 from Isira-Seneviratne/Use_extensions

[Android] Use more extensions.
parents 4c4c39bd aba2026e
......@@ -23,7 +23,6 @@ import android.app.Service
import android.content.*
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.os.IBinder
import android.os.RemoteException
......@@ -35,6 +34,7 @@ import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toUri
import androidx.core.os.bundleOf
import androidx.fragment.app.replace
import androidx.lifecycle.lifecycleScope
......@@ -261,8 +261,8 @@ class BOINCActivity : AppCompatActivity() {
ft.replace<ProjectsFragment>(R.id.frame_container)
fragmentChanges = true
}
R.string.menu_help -> startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://boinc.berkeley.edu/wiki/BOINC_Help")))
R.string.menu_report_issue -> startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://boinc.berkeley.edu/trac/wiki/ReportBugs")))
R.string.menu_help -> startActivity(Intent(Intent.ACTION_VIEW, "https://boinc.berkeley.edu/wiki/BOINC_Help".toUri()))
R.string.menu_report_issue -> startActivity(Intent(Intent.ACTION_VIEW, "https://boinc.berkeley.edu/trac/wiki/ReportBugs".toUri()))
R.string.menu_about -> {
val dialog = Dialog(this).apply {
requestWindowFeature(Window.FEATURE_NO_TITLE)
......
......@@ -24,9 +24,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Bitmap
import android.graphics.Point
import android.net.Uri
import android.os.Bundle
import android.os.RemoteException
import android.text.SpannableString
......@@ -35,6 +33,8 @@ import android.util.Log
import android.view.*
import android.widget.Button
import android.widget.TextView
import androidx.core.graphics.scale
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import edu.berkeley.boinc.databinding.ProjectDetailsLayoutBinding
......@@ -251,7 +251,7 @@ class ProjectDetailsFragment : Fragment() {
content.setSpan(UnderlineSpan(), 0, content.length, 0)
binding.projectUrl.text = content
binding.projectUrl.setOnClickListener {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(project!!.masterURL)))
startActivity(Intent(Intent.ACTION_VIEW, project!!.masterURL.toUri()))
}
// set general area
......@@ -364,9 +364,7 @@ class ProjectDetailsFragment : Fragment() {
val slideshowBinding = ProjectDetailsSlideshowImageLayoutBinding.inflate(layoutInflater)
var bitmap = image.image!!
if (scaleImages(bitmap.height, bitmap.width)) {
bitmap = Bitmap.createScaledBitmap(image.image!!,
image.image!!.width * 2,
image.image!!.height * 2, false)
bitmap = bitmap.scale(bitmap.width * 2, bitmap.height * 2, filter = false)
}
slideshowBinding.slideshowImage.setImageBitmap(bitmap)
binding.slideshowHook.addView(slideshowBinding.slideshowImage)
......
......@@ -23,11 +23,11 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Bundle
import android.os.RemoteException
import android.util.Log
import android.view.*
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import edu.berkeley.boinc.adapter.ProjectControlsListAdapter
......@@ -393,7 +393,7 @@ class ProjectsFragment : Fragment() {
dialog.show()
} else if (operation == VISIT_WEBSITE) { // command does not require confirmation and is not RPC based
dialogControls!!.dismiss()
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(data.id)))
startActivity(Intent(Intent.ACTION_VIEW, data.id!!.toUri()))
} else { // command does not require confirmation, but is RPC based
lifecycleScope.launch {
performProjectOperationAsync(data, operation)
......
......@@ -24,9 +24,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import edu.berkeley.boinc.databinding.EventLogClientListItemLayoutBinding
import edu.berkeley.boinc.rpc.Message
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import edu.berkeley.boinc.utils.secondsToLocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
......@@ -54,16 +52,13 @@ class ClientLogRecyclerViewAdapter(private val messages: List<Message>) :
}
}
fun getDateTimeString(position: Int): String? {
val instant = Instant.ofEpochSecond(messages[position].timestamp)
return dateTimeFormatter.format(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()))
}
fun getDateTimeString(position: Int): String = dateTimeFormatter.format(messages[position].timestamp.secondsToLocalDateTime())
fun getMessage(position: Int) = messages[position].body
fun getProject(position: Int) = messages[position].project
inner class ViewHolder(binding: EventLogClientListItemLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
class ViewHolder(binding: EventLogClientListItemLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
val date = binding.msgsDate
val project = binding.msgsProject
val message = binding.msgsMessage
......
......@@ -24,7 +24,7 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.net.toUri
import androidx.core.text.HtmlCompat
import androidx.core.text.parseAsHtml
import androidx.recyclerview.widget.RecyclerView
import edu.berkeley.boinc.BOINCActivity
import edu.berkeley.boinc.NoticesFragment
......@@ -32,9 +32,7 @@ import edu.berkeley.boinc.R
import edu.berkeley.boinc.databinding.NoticesLayoutListItemBinding
import edu.berkeley.boinc.rpc.Notice
import edu.berkeley.boinc.utils.Logging
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import edu.berkeley.boinc.utils.secondsToLocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
......@@ -77,11 +75,9 @@ class NoticesRecyclerViewAdapter(
holder.projectName.text = listItem.projectName
holder.title.text = listItem.title
holder.content.text = HtmlCompat.fromHtml(listItem.description, HtmlCompat.FROM_HTML_MODE_LEGACY)
holder.content.text = listItem.description.parseAsHtml()
val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(
listItem.createTime.toLong()), ZoneId.systemDefault())
holder.time.text = dateTimeFormatter.format(localDateTime)
holder.time.text = dateTimeFormatter.format(listItem.createTime.toLong().secondsToLocalDateTime())
}
private fun getIcon(position: Int): Bitmap? {
......
......@@ -35,9 +35,6 @@ import edu.berkeley.boinc.databinding.TasksLayoutListItemBinding
import edu.berkeley.boinc.rpc.RpcClient
import edu.berkeley.boinc.utils.*
import java.text.NumberFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import kotlin.math.roundToInt
......@@ -46,6 +43,7 @@ class TaskRecyclerViewAdapter(
private val fragment: TasksFragment,
private val taskList: List<TasksFragment.TaskData>
) : RecyclerView.Adapter<TaskRecyclerViewAdapter.ViewHolder>() {
private val dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
private val elapsedTimeStringBuilder = StringBuilder()
private val percentNumberFormat = NumberFormat.getPercentInstance().apply { minimumFractionDigits = 3 }
......@@ -138,10 +136,7 @@ class TaskRecyclerViewAdapter(
holder.time.text = DateUtils.formatElapsedTime(elapsedTimeStringBuilder, elapsedTime)
// set deadline
val deadlineDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(
item.result.reportDeadline), ZoneId.systemDefault())
val deadline = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.format(deadlineDateTime)
val deadline = dateTimeFormatter.format(item.result.reportDeadline.secondsToLocalDateTime())
holder.deadline.text = deadline
// set application friendly name
if (result.app != null) {
......@@ -253,7 +248,7 @@ class TaskRecyclerViewAdapter(
}
}
inner class ViewHolder(binding: TasksLayoutListItemBinding) : RecyclerView.ViewHolder(binding.root) {
class ViewHolder(binding: TasksLayoutListItemBinding) : RecyclerView.ViewHolder(binding.root) {
val root = binding.root
val projectIcon = binding.projectIcon
val header = binding.taskHeader
......
......@@ -19,13 +19,14 @@
package edu.berkeley.boinc.attach.glide
import android.graphics.Bitmap
import androidx.core.graphics.scale
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import java.security.MessageDigest
class ScaleBitmapBy2 : BitmapTransformation() {
override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap {
return Bitmap.createScaledBitmap(toTransform, toTransform.width * 2, toTransform.height * 2, false)
return toTransform.scale(toTransform.width * 2, toTransform.height * 2, filter = false)
}
override fun equals(other: Any?) = other is ScaleBitmapBy2
......
......@@ -35,6 +35,9 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import java.io.IOException
import java.io.Reader
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
val ConnectivityManager.isOnline: Boolean
get() {
......@@ -105,3 +108,8 @@ fun Context.translateRPCReason(reason: Int) = when (reason) {
RPC_REASON_PROJECT_REQ -> resources.getString(R.string.rpcreason_projectreq)
else -> resources.getString(R.string.rpcreason_unknown)
}
@Suppress("NOTHING_TO_INLINE")
inline fun Long.secondsToLocalDateTime(
zoneId: ZoneId = ZoneId.systemDefault()
): LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(this), zoneId)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment