add Remaining Account Chart Tile widget
This commit is contained in:
7 files changed
+500
-19
No files matched your search
@@ -99,6 +99,18 @@
|
||||
android:resource="@xml/widget_account_spotlight" />
|
||||
</receiver>
|
||||
|
||||
<receiver
|
||||
android:name=".widget.AccountChartTileWidgetProvider"
|
||||
android:exported="true"
|
||||
android:label="@string/widget_account_chart_tile_name">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
|
||||
</intent-filter>
|
||||
<meta-data
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/widget_account_chart_tile" />
|
||||
</receiver>
|
||||
|
||||
<activity
|
||||
android:name=".widget.AccountWidgetConfigureActivity"
|
||||
android:exported="true"
|
||||
|
||||
@@ -15,6 +15,7 @@ import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
@@ -32,6 +33,7 @@ import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
@@ -42,11 +44,13 @@ import net.rodakot.remaining.data.AccountSummary
|
||||
import net.rodakot.remaining.ui.components.ModernPanel
|
||||
import net.rodakot.remaining.ui.components.RemainingBackground
|
||||
import net.rodakot.remaining.ui.components.SectionHeader
|
||||
import net.rodakot.remaining.widget.AccountChartTileWidgetProvider
|
||||
import net.rodakot.remaining.widget.AccountSpotlightWidgetProvider
|
||||
import net.rodakot.remaining.widget.AccountWidgetConfigureActivity
|
||||
import net.rodakot.remaining.widget.EXTRA_ACCOUNT_WIDGET_PROVIDER
|
||||
import net.rodakot.remaining.widget.RemainingWidgetRenderer
|
||||
import net.rodakot.remaining.widget.RemainingWidgetStats
|
||||
import net.rodakot.remaining.widget.RemainingWidgetType
|
||||
import net.rodakot.remaining.widget.AccountSpotlightWidgetProvider
|
||||
import net.rodakot.remaining.widget.AccountWidgetConfigureActivity
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
@@ -109,6 +113,25 @@ fun WidgetGalleryScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
item {
|
||||
AccountChartTilePreviewCard(
|
||||
accounts = accounts,
|
||||
canPin = canPin,
|
||||
onPin = {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
AppWidgetManager.getInstance(context).requestPinAppWidget(
|
||||
ComponentName(context, AccountChartTileWidgetProvider::class.java),
|
||||
null,
|
||||
accountWidgetConfigureCallback(
|
||||
context,
|
||||
AccountChartTileWidgetProvider::class.java.name,
|
||||
ACCOUNT_CHART_TILE_CONFIGURE_REQUEST
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
item {
|
||||
AccountSpotlightPreviewCard(
|
||||
accounts = accounts,
|
||||
@@ -118,7 +141,11 @@ fun WidgetGalleryScreen(
|
||||
AppWidgetManager.getInstance(context).requestPinAppWidget(
|
||||
ComponentName(context, AccountSpotlightWidgetProvider::class.java),
|
||||
null,
|
||||
accountWidgetConfigureCallback(context)
|
||||
accountWidgetConfigureCallback(
|
||||
context,
|
||||
AccountSpotlightWidgetProvider::class.java.name,
|
||||
ACCOUNT_SPOTLIGHT_CONFIGURE_REQUEST
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -145,6 +172,57 @@ fun WidgetGalleryScreen(
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AccountChartTilePreviewCard(
|
||||
accounts: List<AccountSummary>,
|
||||
canPin: Boolean,
|
||||
onPin: () -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val data = remember(accounts) {
|
||||
RemainingWidgetStats.accountSpotlightPreview(accounts.firstOrNull())
|
||||
}
|
||||
val preview = remember(data) {
|
||||
RemainingWidgetRenderer.renderAccountChartTile(context, data, densityOverride = 1.5f)
|
||||
}
|
||||
ModernPanel {
|
||||
SectionHeader(
|
||||
title = "Account Chart Tile",
|
||||
subtitle = "A 1x1 selected-account widget with no visible text: quota ring, trend sparkline, risk pulse, and package bars."
|
||||
)
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(14.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Image(
|
||||
bitmap = preview.asImageBitmap(),
|
||||
contentDescription = "Account Chart Tile preview",
|
||||
modifier = Modifier.size(112.dp)
|
||||
)
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(10.dp),
|
||||
modifier = Modifier.weight(1.2f)
|
||||
) {
|
||||
Button(
|
||||
onClick = onPin,
|
||||
enabled = canPin,
|
||||
modifier = Modifier.fillMaxWidth()
|
||||
) {
|
||||
Text("Add and choose account")
|
||||
}
|
||||
Surface(shape = RoundedCornerShape(8.dp)) {
|
||||
Text(
|
||||
"1 x 1 / no text",
|
||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp),
|
||||
style = MaterialTheme.typography.labelMedium
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AccountSpotlightPreviewCard(
|
||||
accounts: List<AccountSummary>,
|
||||
@@ -239,9 +317,14 @@ private fun previewHeight(type: RemainingWidgetType) =
|
||||
else -> 156.dp
|
||||
}
|
||||
|
||||
private fun accountWidgetConfigureCallback(context: Context): PendingIntent {
|
||||
private fun accountWidgetConfigureCallback(
|
||||
context: Context,
|
||||
providerClassName: String,
|
||||
requestCode: Int
|
||||
): PendingIntent {
|
||||
val intent = Intent(context, AccountWidgetConfigureActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
putExtra(EXTRA_ACCOUNT_WIDGET_PROVIDER, providerClassName)
|
||||
}
|
||||
val mutabilityFlag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
PendingIntent.FLAG_MUTABLE
|
||||
@@ -250,10 +333,11 @@ private fun accountWidgetConfigureCallback(context: Context): PendingIntent {
|
||||
}
|
||||
return PendingIntent.getActivity(
|
||||
context,
|
||||
ACCOUNT_WIDGET_CONFIGURE_REQUEST,
|
||||
requestCode,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or mutabilityFlag
|
||||
)
|
||||
}
|
||||
|
||||
private const val ACCOUNT_WIDGET_CONFIGURE_REQUEST = 45_901
|
||||
private const val ACCOUNT_SPOTLIGHT_CONFIGURE_REQUEST = 45_901
|
||||
private const val ACCOUNT_CHART_TILE_CONFIGURE_REQUEST = 45_902
|
||||
@@ -13,10 +13,12 @@ import net.rodakot.remaining.ui.theme.RemainingTheme
|
||||
|
||||
class AccountWidgetConfigureActivity : ComponentActivity() {
|
||||
private var appWidgetId: Int = AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
private var providerClassName: String? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setResult(Activity.RESULT_CANCELED)
|
||||
providerClassName = intent?.getStringExtra(EXTRA_ACCOUNT_WIDGET_PROVIDER)
|
||||
appWidgetId = intent?.extras?.getInt(
|
||||
AppWidgetManager.EXTRA_APPWIDGET_ID,
|
||||
AppWidgetManager.INVALID_APPWIDGET_ID
|
||||
@@ -36,10 +38,11 @@ class AccountWidgetConfigureActivity : ComponentActivity() {
|
||||
onCancel = { finish() },
|
||||
onAccountSelected = { accountId ->
|
||||
AccountWidgetStore.save(this@AccountWidgetConfigureActivity, appWidgetId, accountId)
|
||||
RemainingWidgetUpdater.updateAccountSpotlights(
|
||||
RemainingWidgetUpdater.updateConfiguredAccountWidget(
|
||||
context = this@AccountWidgetConfigureActivity,
|
||||
appWidgetManager = AppWidgetManager.getInstance(this@AccountWidgetConfigureActivity),
|
||||
appWidgetIds = intArrayOf(appWidgetId)
|
||||
appWidgetId = appWidgetId,
|
||||
providerClassName = providerClassName
|
||||
)
|
||||
val result = Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
||||
setResult(Activity.RESULT_OK, result)
|
||||
@@ -51,3 +54,5 @@ class AccountWidgetConfigureActivity : ComponentActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const val EXTRA_ACCOUNT_WIDGET_PROVIDER = "net.rodakot.remaining.widget.EXTRA_ACCOUNT_WIDGET_PROVIDER"
|
||||
@@ -3,6 +3,7 @@ package net.rodakot.remaining.widget
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
@@ -80,6 +81,35 @@ class AccountSpotlightWidgetProvider : AppWidgetProvider() {
|
||||
}
|
||||
}
|
||||
|
||||
class AccountChartTileWidgetProvider : AppWidgetProvider() {
|
||||
override fun onUpdate(
|
||||
context: Context,
|
||||
appWidgetManager: AppWidgetManager,
|
||||
appWidgetIds: IntArray
|
||||
) {
|
||||
val pendingResult = goAsync()
|
||||
RemainingWidgetUpdater.updateAccountChartTiles(
|
||||
context = context,
|
||||
appWidgetManager = appWidgetManager,
|
||||
appWidgetIds = appWidgetIds,
|
||||
onFinished = pendingResult::finish
|
||||
)
|
||||
}
|
||||
|
||||
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
||||
AccountWidgetStore.remove(context, appWidgetIds)
|
||||
}
|
||||
|
||||
override fun onAppWidgetOptionsChanged(
|
||||
context: Context,
|
||||
appWidgetManager: AppWidgetManager,
|
||||
appWidgetId: Int,
|
||||
newOptions: android.os.Bundle
|
||||
) {
|
||||
onUpdate(context, appWidgetManager, intArrayOf(appWidgetId))
|
||||
}
|
||||
}
|
||||
|
||||
object RemainingWidgetUpdater {
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
@@ -91,11 +121,17 @@ object RemainingWidgetUpdater {
|
||||
update(context, appWidgetManager, ids, type)
|
||||
}
|
||||
}
|
||||
val accountIds = appWidgetManager.getAppWidgetIds(
|
||||
android.content.ComponentName(context, AccountSpotlightWidgetProvider::class.java)
|
||||
val spotlightIds = appWidgetManager.getAppWidgetIds(
|
||||
ComponentName(context, AccountSpotlightWidgetProvider::class.java)
|
||||
)
|
||||
if (accountIds.isNotEmpty()) {
|
||||
updateAccountSpotlights(context, appWidgetManager, accountIds)
|
||||
if (spotlightIds.isNotEmpty()) {
|
||||
updateAccountSpotlights(context, appWidgetManager, spotlightIds)
|
||||
}
|
||||
val tileIds = appWidgetManager.getAppWidgetIds(
|
||||
ComponentName(context, AccountChartTileWidgetProvider::class.java)
|
||||
)
|
||||
if (tileIds.isNotEmpty()) {
|
||||
updateAccountChartTiles(context, appWidgetManager, tileIds)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -140,12 +176,9 @@ object RemainingWidgetUpdater {
|
||||
onFinished: (() -> Unit)? = null
|
||||
) {
|
||||
scope.launch {
|
||||
val application = context.applicationContext as RemainingApplication
|
||||
appWidgetIds.forEach { widgetId ->
|
||||
runCatching {
|
||||
val accountId = AccountWidgetStore.get(context, widgetId)
|
||||
val detail = accountId?.let { application.container.accountRepository.getAccountWidgetDetail(it) }
|
||||
val data = RemainingWidgetStats.accountSpotlight(detail)
|
||||
val data = loadAccountData(context, widgetId)
|
||||
val bitmap = RemainingWidgetRenderer.renderAccountSpotlight(context, data, densityOverride = 1.6f)
|
||||
val views = RemoteViews(context.packageName, R.layout.remaining_widget_chart)
|
||||
views.setViewVisibility(R.id.remaining_widget_image, View.VISIBLE)
|
||||
@@ -158,7 +191,12 @@ object RemainingWidgetUpdater {
|
||||
views.setOnClickPendingIntent(
|
||||
R.id.remaining_widget_root,
|
||||
if (data.account == null) {
|
||||
openConfigureIntent(context, widgetId, mutable = false)
|
||||
openConfigureIntent(
|
||||
context,
|
||||
widgetId,
|
||||
mutable = false,
|
||||
providerClassName = AccountSpotlightWidgetProvider::class.java.name
|
||||
)
|
||||
} else {
|
||||
openAppIntent(context, widgetId)
|
||||
}
|
||||
@@ -171,7 +209,12 @@ object RemainingWidgetUpdater {
|
||||
views.setTextViewText(R.id.remaining_widget_fallback, "Choose account")
|
||||
views.setOnClickPendingIntent(
|
||||
R.id.remaining_widget_root,
|
||||
openConfigureIntent(context, widgetId, mutable = false)
|
||||
openConfigureIntent(
|
||||
context,
|
||||
widgetId,
|
||||
mutable = false,
|
||||
providerClassName = AccountSpotlightWidgetProvider::class.java.name
|
||||
)
|
||||
)
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
}
|
||||
@@ -180,12 +223,97 @@ object RemainingWidgetUpdater {
|
||||
}
|
||||
}
|
||||
|
||||
fun updateAccountChartTiles(
|
||||
context: Context,
|
||||
appWidgetManager: AppWidgetManager,
|
||||
appWidgetIds: IntArray,
|
||||
onFinished: (() -> Unit)? = null
|
||||
) {
|
||||
scope.launch {
|
||||
appWidgetIds.forEach { widgetId ->
|
||||
runCatching {
|
||||
val data = loadAccountData(context, widgetId)
|
||||
val bitmap = RemainingWidgetRenderer.renderAccountChartTile(context, data, densityOverride = 1.8f)
|
||||
val views = RemoteViews(context.packageName, R.layout.remaining_widget_chart)
|
||||
views.setViewVisibility(R.id.remaining_widget_image, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.remaining_widget_fallback, View.GONE)
|
||||
views.setImageViewBitmap(R.id.remaining_widget_image, bitmap)
|
||||
views.setContentDescription(
|
||||
R.id.remaining_widget_image,
|
||||
data.account?.let { "Account chart tile: ${it.name}" } ?: "Account chart tile"
|
||||
)
|
||||
views.setOnClickPendingIntent(
|
||||
R.id.remaining_widget_root,
|
||||
if (data.account == null) {
|
||||
openConfigureIntent(
|
||||
context,
|
||||
widgetId,
|
||||
mutable = false,
|
||||
providerClassName = AccountChartTileWidgetProvider::class.java.name
|
||||
)
|
||||
} else {
|
||||
openAppIntent(context, widgetId)
|
||||
}
|
||||
)
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
}.onFailure {
|
||||
val views = RemoteViews(context.packageName, R.layout.remaining_widget_chart)
|
||||
views.setViewVisibility(R.id.remaining_widget_image, View.VISIBLE)
|
||||
views.setViewVisibility(R.id.remaining_widget_fallback, View.GONE)
|
||||
runCatching {
|
||||
val bitmap = RemainingWidgetRenderer.renderAccountChartTile(
|
||||
context,
|
||||
RemainingWidgetStats.accountSpotlight(null),
|
||||
densityOverride = 1.8f
|
||||
)
|
||||
views.setImageViewBitmap(R.id.remaining_widget_image, bitmap)
|
||||
}
|
||||
views.setOnClickPendingIntent(
|
||||
R.id.remaining_widget_root,
|
||||
openConfigureIntent(
|
||||
context,
|
||||
widgetId,
|
||||
mutable = false,
|
||||
providerClassName = AccountChartTileWidgetProvider::class.java.name
|
||||
)
|
||||
)
|
||||
appWidgetManager.updateAppWidget(widgetId, views)
|
||||
}
|
||||
}
|
||||
onFinished?.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
fun updateConfiguredAccountWidget(
|
||||
context: Context,
|
||||
appWidgetManager: AppWidgetManager,
|
||||
appWidgetId: Int,
|
||||
providerClassName: String? = null
|
||||
) {
|
||||
val resolvedProvider = providerClassName ?: appWidgetManager.getAppWidgetInfo(appWidgetId)?.provider?.className
|
||||
when (resolvedProvider) {
|
||||
AccountChartTileWidgetProvider::class.java.name -> {
|
||||
updateAccountChartTiles(context, appWidgetManager, intArrayOf(appWidgetId))
|
||||
}
|
||||
else -> {
|
||||
updateAccountSpotlights(context, appWidgetManager, intArrayOf(appWidgetId))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadData(context: Context): WidgetDashboardData {
|
||||
val application = context.applicationContext as RemainingApplication
|
||||
val summaries = application.container.accountRepository.getAccountSummaries()
|
||||
return RemainingWidgetStats.fromSummaries(summaries)
|
||||
}
|
||||
|
||||
private suspend fun loadAccountData(context: Context, widgetId: Int): AccountSpotlightData {
|
||||
val application = context.applicationContext as RemainingApplication
|
||||
val accountId = AccountWidgetStore.get(context, widgetId)
|
||||
val detail = accountId?.let { application.container.accountRepository.getAccountWidgetDetail(it) }
|
||||
return RemainingWidgetStats.accountSpotlight(detail)
|
||||
}
|
||||
|
||||
private fun openAppIntent(context: Context, requestCode: Int): PendingIntent {
|
||||
val intent = Intent(context, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
@@ -201,11 +329,13 @@ object RemainingWidgetUpdater {
|
||||
fun openConfigureIntent(
|
||||
context: Context,
|
||||
requestCode: Int,
|
||||
mutable: Boolean
|
||||
mutable: Boolean,
|
||||
providerClassName: String? = null
|
||||
): PendingIntent {
|
||||
val intent = Intent(context, AccountWidgetConfigureActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, requestCode)
|
||||
providerClassName?.let { putExtra(EXTRA_ACCOUNT_WIDGET_PROVIDER, it) }
|
||||
}
|
||||
val mutabilityFlag = if (mutable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
PendingIntent.FLAG_MUTABLE
|
||||
|
||||
@@ -6,6 +6,7 @@ import android.graphics.Canvas
|
||||
import android.graphics.Color
|
||||
import android.graphics.LinearGradient
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Path
|
||||
import android.graphics.RectF
|
||||
import android.graphics.Shader
|
||||
import android.graphics.SweepGradient
|
||||
@@ -77,8 +78,241 @@ object RemainingWidgetRenderer {
|
||||
return bitmap
|
||||
}
|
||||
|
||||
fun renderAccountChartTile(
|
||||
context: Context,
|
||||
data: AccountSpotlightData,
|
||||
densityOverride: Float? = null
|
||||
): Bitmap {
|
||||
val density = densityOverride ?: context.resources.displayMetrics.density
|
||||
val bitmap = Bitmap.createBitmap(
|
||||
(ACCOUNT_CHART_TILE_WIDTH_DP * density).toInt(),
|
||||
(ACCOUNT_CHART_TILE_HEIGHT_DP * density).toInt(),
|
||||
Bitmap.Config.ARGB_8888
|
||||
)
|
||||
AccountChartTileCanvas(Canvas(bitmap), bitmap.width.toFloat(), bitmap.height.toFloat(), density, data)
|
||||
.draw()
|
||||
return bitmap
|
||||
}
|
||||
|
||||
const val ACCOUNT_SPOTLIGHT_WIDTH_DP = 360
|
||||
const val ACCOUNT_SPOTLIGHT_HEIGHT_DP = 230
|
||||
const val ACCOUNT_CHART_TILE_WIDTH_DP = 112
|
||||
const val ACCOUNT_CHART_TILE_HEIGHT_DP = 112
|
||||
}
|
||||
|
||||
private class AccountChartTileCanvas(
|
||||
private val canvas: Canvas,
|
||||
private val width: Float,
|
||||
private val height: Float,
|
||||
private val density: Float,
|
||||
private val data: AccountSpotlightData
|
||||
) {
|
||||
private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
||||
private val strokePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
||||
style = Paint.Style.STROKE
|
||||
strokeCap = Paint.Cap.ROUND
|
||||
}
|
||||
private val r = 22f.dp
|
||||
|
||||
fun draw() {
|
||||
drawBackground()
|
||||
val account = data.account
|
||||
if (account == null) {
|
||||
drawOuterRing(0.70f, PRIMARY)
|
||||
drawSparkline(listOf(0.28f, 0.54f, 0.40f, 0.66f, 0.58f, 0.76f), CYAN)
|
||||
drawSignalDots(PRIMARY, CYAN, AMBER)
|
||||
drawMiniBars(listOf(0.42f, 0.72f, 0.55f), PRIMARY)
|
||||
return
|
||||
}
|
||||
|
||||
val color = account.serviceType.widgetColor()
|
||||
val risk = riskColor(account.riskScore)
|
||||
val history = normalizedHistory(account)
|
||||
drawProviderGlow(color)
|
||||
drawOuterRing(account.progress ?: history.lastOrNull() ?: 0.56f, color)
|
||||
drawRiskNeedle(account.riskScore, risk)
|
||||
drawSparkline(history, color)
|
||||
drawMiniBars(packageProgress(account, history), color)
|
||||
drawSignalDots(color, risk, statusColor(account))
|
||||
}
|
||||
|
||||
private fun drawBackground() {
|
||||
fillPaint.shader = LinearGradient(
|
||||
0f,
|
||||
0f,
|
||||
width,
|
||||
height,
|
||||
intArrayOf(Color.rgb(4, 27, 24), Color.rgb(8, 18, 20), Color.rgb(7, 22, 30)),
|
||||
floatArrayOf(0f, 0.62f, 1f),
|
||||
Shader.TileMode.CLAMP
|
||||
)
|
||||
canvas.drawRoundRect(RectF(0f, 0f, width, height), r, r, fillPaint)
|
||||
fillPaint.shader = LinearGradient(
|
||||
0f,
|
||||
height,
|
||||
width,
|
||||
0f,
|
||||
Color.argb(68, 126, 232, 246),
|
||||
Color.TRANSPARENT,
|
||||
Shader.TileMode.CLAMP
|
||||
)
|
||||
canvas.drawRoundRect(RectF(1.5f.dp, 1.5f.dp, width - 1.5f.dp, height - 1.5f.dp), r, r, fillPaint)
|
||||
fillPaint.shader = null
|
||||
strokePaint.shader = null
|
||||
strokePaint.color = Color.argb(92, 142, 235, 255)
|
||||
strokePaint.strokeWidth = 1.2f.dp
|
||||
canvas.drawRoundRect(RectF(1.3f.dp, 1.3f.dp, width - 1.3f.dp, height - 1.3f.dp), r, r, strokePaint)
|
||||
}
|
||||
|
||||
private fun drawProviderGlow(color: Int) {
|
||||
fillPaint.shader = LinearGradient(
|
||||
0f,
|
||||
0f,
|
||||
width,
|
||||
height,
|
||||
Color.argb(60, Color.red(color), Color.green(color), Color.blue(color)),
|
||||
Color.TRANSPARENT,
|
||||
Shader.TileMode.CLAMP
|
||||
)
|
||||
canvas.drawRoundRect(RectF(5f.dp, 5f.dp, width - 5f.dp, height - 5f.dp), 18f.dp, 18f.dp, fillPaint)
|
||||
fillPaint.shader = null
|
||||
}
|
||||
|
||||
private fun drawOuterRing(progress: Float, color: Int) {
|
||||
val cx = width / 2f
|
||||
val cy = height / 2f
|
||||
val radius = min(width, height) * 0.34f
|
||||
val stroke = 8.5f.dp
|
||||
val arc = RectF(cx - radius, cy - radius, cx + radius, cy + radius)
|
||||
strokePaint.shader = null
|
||||
strokePaint.color = Color.argb(48, 255, 255, 255)
|
||||
strokePaint.strokeWidth = stroke
|
||||
canvas.drawArc(arc, 135f, 270f, false, strokePaint)
|
||||
strokePaint.shader = SweepGradient(cx, cy, intArrayOf(color, CYAN, color), null)
|
||||
canvas.drawArc(arc, 135f, 270f * progress.coerceIn(0.04f, 1f), false, strokePaint)
|
||||
strokePaint.shader = null
|
||||
}
|
||||
|
||||
private fun drawRiskNeedle(risk: Int, color: Int) {
|
||||
val cx = width / 2f
|
||||
val cy = height / 2f
|
||||
val radius = min(width, height) * 0.42f
|
||||
val angle = Math.toRadians((135f + 270f * (risk / 100f).coerceIn(0f, 1f)).toDouble())
|
||||
val innerX = cx + cos(angle).toFloat() * (radius - 10f.dp)
|
||||
val innerY = cy + sin(angle).toFloat() * (radius - 10f.dp)
|
||||
val outerX = cx + cos(angle).toFloat() * radius
|
||||
val outerY = cy + sin(angle).toFloat() * radius
|
||||
strokePaint.shader = null
|
||||
strokePaint.color = color
|
||||
strokePaint.strokeWidth = 3f.dp
|
||||
canvas.drawLine(innerX, innerY, outerX, outerY, strokePaint)
|
||||
drawDot(outerX, outerY, 3.8f.dp, color)
|
||||
}
|
||||
|
||||
private fun drawSparkline(values: List<Float>, color: Int) {
|
||||
val chart = RectF(25f.dp, 39f.dp, width - 25f.dp, height - 37f.dp)
|
||||
fillPaint.color = Color.argb(32, 255, 255, 255)
|
||||
canvas.drawRoundRect(chart, 11f.dp, 11f.dp, fillPaint)
|
||||
|
||||
val points = values.takeIf { it.size >= 2 } ?: listOf(0.35f, 0.52f, 0.45f, 0.68f)
|
||||
val linePath = Path()
|
||||
val fillPath = Path()
|
||||
points.forEachIndexed { index, value ->
|
||||
val x = chart.left + index * (chart.width() / (points.size - 1).coerceAtLeast(1))
|
||||
val y = chart.bottom - value.coerceIn(0.04f, 0.96f) * chart.height()
|
||||
if (index == 0) {
|
||||
linePath.moveTo(x, y)
|
||||
fillPath.moveTo(x, chart.bottom)
|
||||
fillPath.lineTo(x, y)
|
||||
} else {
|
||||
linePath.lineTo(x, y)
|
||||
fillPath.lineTo(x, y)
|
||||
}
|
||||
}
|
||||
fillPath.lineTo(chart.right, chart.bottom)
|
||||
fillPath.close()
|
||||
|
||||
fillPaint.shader = LinearGradient(
|
||||
chart.left,
|
||||
chart.top,
|
||||
chart.left,
|
||||
chart.bottom,
|
||||
Color.argb(72, Color.red(color), Color.green(color), Color.blue(color)),
|
||||
Color.TRANSPARENT,
|
||||
Shader.TileMode.CLAMP
|
||||
)
|
||||
canvas.drawPath(fillPath, fillPaint)
|
||||
fillPaint.shader = null
|
||||
|
||||
strokePaint.shader = LinearGradient(chart.left, chart.top, chart.right, chart.bottom, color, CYAN, Shader.TileMode.CLAMP)
|
||||
strokePaint.strokeWidth = 3.2f.dp
|
||||
canvas.drawPath(linePath, strokePaint)
|
||||
strokePaint.shader = null
|
||||
}
|
||||
|
||||
private fun drawMiniBars(values: List<Float>, color: Int) {
|
||||
val bars = values.take(4).ifEmpty { listOf(0.44f, 0.68f, 0.52f) }
|
||||
val gap = 3f.dp
|
||||
val x = 23f.dp
|
||||
val y = height - 22f.dp
|
||||
val w = (width - x * 2f - gap * (bars.size - 1)) / bars.size
|
||||
bars.forEachIndexed { index, value ->
|
||||
val left = x + index * (w + gap)
|
||||
val progress = value.coerceIn(0.06f, 1f)
|
||||
fillPaint.shader = null
|
||||
fillPaint.color = Color.argb(42, 255, 255, 255)
|
||||
canvas.drawRoundRect(RectF(left, y, left + w, y + 5f.dp), 3f.dp, 3f.dp, fillPaint)
|
||||
fillPaint.shader = LinearGradient(left, y, left + w, y, color, CYAN, Shader.TileMode.CLAMP)
|
||||
canvas.drawRoundRect(RectF(left, y, left + w * progress, y + 5f.dp), 3f.dp, 3f.dp, fillPaint)
|
||||
fillPaint.shader = null
|
||||
}
|
||||
}
|
||||
|
||||
private fun drawSignalDots(first: Int, second: Int, third: Int) {
|
||||
drawDot(17f.dp, 16f.dp, 5.4f.dp, first)
|
||||
drawDot(width - 17f.dp, 16f.dp, 5.4f.dp, second)
|
||||
drawDot(width - 17f.dp, height - 16f.dp, 4.4f.dp, third)
|
||||
}
|
||||
|
||||
private fun normalizedHistory(account: WidgetAccount): List<Float> {
|
||||
val values = data.history.mapNotNull { it.remainingBytes }
|
||||
if (values.size >= 2) {
|
||||
val minValue = values.min()
|
||||
val maxValue = values.max()
|
||||
val range = (maxValue - minValue).coerceAtLeast(1L)
|
||||
return values.map { ((it - minValue).toFloat() / range.toFloat()).coerceIn(0.08f, 0.96f) }
|
||||
}
|
||||
val base = account.progress ?: 0.54f
|
||||
return listOf(base * 0.62f, base * 0.86f, base * 0.74f, base * 1.12f, base * 0.96f)
|
||||
.map { it.coerceIn(0.08f, 0.96f) }
|
||||
}
|
||||
|
||||
private fun packageProgress(account: WidgetAccount, history: List<Float>): List<Float> =
|
||||
data.packages.take(4).map { it.progress ?: 0.14f }.ifEmpty {
|
||||
listOf(
|
||||
account.progress ?: history.lastOrNull() ?: 0.55f,
|
||||
history.getOrNull(history.lastIndex - 1) ?: 0.48f,
|
||||
history.firstOrNull() ?: 0.38f
|
||||
)
|
||||
}
|
||||
|
||||
private fun statusColor(account: WidgetAccount): Int =
|
||||
when {
|
||||
!account.enabled -> Color.rgb(126, 137, 132)
|
||||
account.status == AccountStatus.REFRESHING -> CYAN
|
||||
account.status != AccountStatus.ACTIVE -> ERROR
|
||||
account.riskScore >= 55 -> riskColor(account.riskScore)
|
||||
else -> PRIMARY
|
||||
}
|
||||
|
||||
private fun drawDot(cx: Float, cy: Float, radius: Float, color: Int) {
|
||||
fillPaint.shader = null
|
||||
fillPaint.color = color
|
||||
canvas.drawCircle(cx, cy, radius, fillPaint)
|
||||
}
|
||||
|
||||
private val Float.dp: Float
|
||||
get() = this * density
|
||||
}
|
||||
|
||||
private class AccountSpotlightCanvas(
|
||||
|
||||
@@ -17,4 +17,6 @@
|
||||
<string name="widget_compact_signal_description">Compact chart strip for small home-screen spaces.</string>
|
||||
<string name="widget_account_spotlight_name">Remaining Account Spotlight</string>
|
||||
<string name="widget_account_spotlight_description">Single-account quota ring, trend chart, package bars, and refresh health.</string>
|
||||
<string name="widget_account_chart_tile_name">Remaining Account Chart Tile</string>
|
||||
<string name="widget_account_chart_tile_description">One-cell selected-account widget with only visual quota, trend, risk, and package signals.</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:configure="net.rodakot.remaining.widget.AccountWidgetConfigureActivity"
|
||||
android:description="@string/widget_account_chart_tile_description"
|
||||
android:initialLayout="@layout/remaining_widget_chart"
|
||||
android:minResizeHeight="56dp"
|
||||
android:minResizeWidth="56dp"
|
||||
android:minHeight="72dp"
|
||||
android:minWidth="72dp"
|
||||
android:resizeMode="horizontal|vertical"
|
||||
android:targetCellHeight="1"
|
||||
android:targetCellWidth="1"
|
||||
android:updatePeriodMillis="1800000"
|
||||
android:widgetCategory="home_screen" />
|
||||
Reference in new issue
Block a user