Working on widget

This commit is contained in:
Oupson 2021-05-10 08:14:46 +02:00
parent ba281b5979
commit 9a023298b6
11 changed files with 151 additions and 143 deletions

View File

@ -1,22 +1,6 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JetCodeStyleSettings>
<option name="PACKAGES_TO_USE_STAR_IMPORTS">
<value>
<package name="java.util" alias="false" withSubpackages="false" />
<package name="kotlinx.android.synthetic" alias="false" withSubpackages="true" />
<package name="io.ktor" alias="false" withSubpackages="true" />
</value>
</option>
<option name="PACKAGES_IMPORT_LAYOUT">
<value>
<package name="" alias="false" withSubpackages="true" />
<package name="java" alias="false" withSubpackages="true" />
<package name="javax" alias="false" withSubpackages="true" />
<package name="kotlin" alias="false" withSubpackages="true" />
<package name="" alias="true" withSubpackages="true" />
</value>
</option>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="XML">

View File

@ -26,5 +26,10 @@
<option name="name" value="maven" />
<option name="url" value="https://jitpack.io" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
</component>
</project>

View File

@ -5,7 +5,7 @@ plugins {
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "fr.oupson.taotoolbox"
@ -13,7 +13,6 @@ android {
targetSdkVersion 30
versionCode 1
versionName "0.0.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

View File

@ -6,8 +6,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"

View File

@ -4,7 +4,6 @@ import android.Manifest
import android.content.pm.PackageManager
import android.database.sqlite.SQLiteDatabase
import android.graphics.*
import android.opengl.Visibility
import android.os.Bundle
import android.util.Log
import android.view.MenuItem

View File

@ -2,7 +2,7 @@ package fr.oupson.taotoolbox.activities
import android.app.Activity
import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.ComponentName
import android.content.Intent
import android.database.sqlite.SQLiteDatabase
import android.graphics.Color
@ -12,7 +12,6 @@ import android.view.MenuItem
import android.view.View
import android.widget.AutoCompleteTextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import fr.oupson.common.api.TaoRestApi
import fr.oupson.common.db.TaoDatabaseHelper
import fr.oupson.common.db.TaoDatabaseHelper.TaoDatabase.*
@ -23,89 +22,13 @@ import fr.oupson.taotoolbox.adapters.LineAdapter
import fr.oupson.taotoolbox.adapters.StopAdapter
import fr.oupson.taotoolbox.databinding.ActivityTaoWidgetConfigurationBinding
import fr.oupson.taotoolbox.receivers.TaoWidget
import fr.oupson.taotoolbox.utils.PrefsWidgetHelper
import kotlinx.coroutines.*
// TODO FIND A WAY TO AUTO SELECT
class TaoWidgetConfigurationActivity : AppCompatActivity() {
companion object {
private const val LINE_ID = "WIDGET_LINE_ID"
private const val LINE_CODE = "WIDGET_LINE_CODE"
private const val ROUTE_ID = "WIDGET_LINE_ID"
private const val ROUTE_NAME = "ROUTE_NAME"
private const val STOP_ID = "WIDGET_STOP_ID"
private const val STOP_NAME = "WIDGET_STOP_NAME"
private const val BG_COLOR = "WIDGET_LINE_BACKGROUND_COLOR"
private const val TEXT_COLOR = "WIDGET_LINE_TEXT_COLOR"
private const val PREFERENCE_NAME = "WIDGET"
private const val TAG = "TaoWidgetConfiguration"
private fun savePrefs(
context: Context,
widgetId: Int,
lineId: String,
lineCode: String,
routeId: String,
routeName: String,
stopId: String,
stopName: String,
bgColor: Int,
textColor: Int
) {
Log.d(TAG, "savePrefs, widget id : $widgetId")
context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit(true) {
putString("${LINE_ID}_$widgetId", lineId)
putString("${LINE_CODE}_$widgetId", lineCode)
putString("${ROUTE_ID}_$widgetId", routeId)
putString("${ROUTE_NAME}_$widgetId", routeName)
putString("${STOP_ID}_$widgetId", stopId)
putString("${STOP_NAME}_$widgetId", stopName)
putInt("${BG_COLOR}_$widgetId", bgColor)
putInt("${TEXT_COLOR}_$widgetId", textColor)
}
}
data class WidgetPrefs(
val lineId: String,
val lineCode: String,
val routeId: String,
val routeName: String,
val stopId: String,
val stopName: String,
val bgColor: Int,
val textColor: Int
)
fun getPrefs(context: Context, widgetId: Int): WidgetPrefs? =
with(context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)) {
Log.d(TAG, "getPrefs, widget id : $widgetId")
WidgetPrefs(
getString("${LINE_ID}_$widgetId", null) ?: return null,
getString("${LINE_CODE}_$widgetId", null) ?: return null,
getString("${ROUTE_ID}_$widgetId", null) ?: return null,
getString("${ROUTE_NAME}_$widgetId", null) ?: return null,
getString("${STOP_ID}_$widgetId", null) ?: return null,
getString("${STOP_NAME}_$widgetId", null) ?: return null,
getInt("${BG_COLOR}_$widgetId", Color.BLACK),
getInt("${TEXT_COLOR}_$widgetId", Color.WHITE),
)
}
fun destroyPrefs(context: Context, widgetId: Int) {
Log.d(TAG, "destroyPrefs, widget id : $widgetId")
val prefs = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
prefs.edit(commit = true) {
remove("${LINE_ID}_$widgetId")
remove("${LINE_CODE}_$widgetId")
remove("${ROUTE_ID}_$widgetId")
remove("${ROUTE_NAME}_$widgetId")
remove("${STOP_ID}_$widgetId")
remove("${STOP_NAME}_$widgetId")
remove("${BG_COLOR}_$widgetId")
remove("${TEXT_COLOR}_$widgetId")
}
}
}
private var widgetId: Int? = null
@ -138,9 +61,26 @@ class TaoWidgetConfigurationActivity : AppCompatActivity() {
AppWidgetManager.INVALID_APPWIDGET_ID
)
if ((widgetId == AppWidgetManager.INVALID_APPWIDGET_ID || widgetId == null) && !BuildConfig.DEBUG) {
setResult(Activity.RESULT_CANCELED, null)
finish()
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID || widgetId == null) {
if (!BuildConfig.DEBUG) {
setResult(Activity.RESULT_CANCELED, null)
finish()
} else {
val appWidgetManager: AppWidgetManager = AppWidgetManager.getInstance(this)
val ids =
appWidgetManager.getAppWidgetIds(ComponentName(this, TaoWidget::class.java))
GlobalScope.launch(Dispatchers.IO) {
for (id in ids) {
TaoWidget.updateAppWidget(
this@TaoWidgetConfigurationActivity,
appWidgetManager,
id,
TaoRestApi()
)
}
}
}
}
binding.loadingProgressBar.visibility = View.VISIBLE
@ -220,7 +160,7 @@ class TaoWidgetConfigurationActivity : AppCompatActivity() {
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID || widgetId == null) {
setResult(Activity.RESULT_CANCELED, null)
} else {
savePrefs(
PrefsWidgetHelper.savePrefs(
this,
widgetId!!,
lineList[linePosition].lineId,

View File

@ -13,7 +13,7 @@ import fr.oupson.common.api.LineColors
import fr.oupson.common.api.RealTimes
import fr.oupson.common.api.TaoRestApi
import fr.oupson.taotoolbox.R
import fr.oupson.taotoolbox.activities.TaoWidgetConfigurationActivity
import fr.oupson.taotoolbox.utils.PrefsWidgetHelper
import kotlinx.coroutines.*
import java.util.*
@ -46,30 +46,28 @@ class TaoWidget : AppWidgetProvider() {
views.setOnClickPendingIntent(R.id.tao_widget_root, pending)
try {
val prefs = TaoWidgetConfigurationActivity.getPrefs(context, appWidgetId)
if (prefs != null) {
val depart = taoRestApi.getNextDeparturesForLine(
prefs.stopId,
prefs.routeId
)
val prefs = PrefsWidgetHelper.getPrefs(context, appWidgetId)!! // Caught
val depart = taoRestApi.getNextDeparturesForLine(
prefs.stopId,
prefs.routeId
)
views.setRealtime(
views.setRealtime(
context,
prefs.stopName,
prefs.routeName.toDirection(),
LineColors.getLineBtm(
context,
prefs.stopName,
prefs.routeName.toDirection(),
LineColors.getLineBtm(
context,
prefs.lineCode,
prefs.bgColor,
prefs.textColor,
256,
256
),
depart
)
}
prefs.lineCode,
prefs.bgColor,
prefs.textColor,
256,
256
),
depart
)
} catch (e: Exception) {
Log.e(TAG, "While updating widget", e)
Log.e(TAG, "While updating widget $appWidgetId", e)
views.setTextViewText(
R.id.widget_tao_stop_name_text_view,
@ -110,7 +108,6 @@ class TaoWidget : AppWidgetProvider() {
)
}
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views)
}
@ -197,14 +194,19 @@ class TaoWidget : AppWidgetProvider() {
}
private val job = Job()
private val coroutineContext = Dispatchers.IO + job
private val widgetScope = CoroutineScope(Dispatchers.IO + job)
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
GlobalScope.launch(coroutineContext) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
Log.d(
TAG,
"onUpdate : appWidgetIds : ${appWidgetIds.contentToString()}"
)
widgetScope.launch {
val taoRestApi = TaoRestApi().also {
it.requestContext = this.coroutineContext
}
@ -215,14 +217,6 @@ class TaoWidget : AppWidgetProvider() {
}
}
override fun onEnabled(context: Context) {
// Enter relevant functionality for when the first widget is created
}
override fun onDisabled(context: Context) {
// Enter relevant functionality for when the last widget is disabled
}
override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
super.onDeleted(context, appWidgetIds)
Log.d(TAG, "onDeleted : ${appWidgetIds?.contentToString()}")
@ -233,7 +227,7 @@ class TaoWidget : AppWidgetProvider() {
if (context != null && appWidgetIds != null) {
for (widgetId in appWidgetIds) {
TaoWidgetConfigurationActivity.destroyPrefs(context, widgetId)
PrefsWidgetHelper.destroyPrefs(context, widgetId)
}
}
}

View File

@ -0,0 +1,89 @@
package fr.oupson.taotoolbox.utils
import android.content.Context
import android.graphics.Color
import android.util.Log
import androidx.core.content.edit
class PrefsWidgetHelper {
companion object {
private const val LINE_ID = "WIDGET_LINE_ID"
private const val LINE_CODE = "WIDGET_LINE_CODE"
private const val ROUTE_ID = "WIDGET_LINE_ID"
private const val ROUTE_NAME = "ROUTE_NAME"
private const val STOP_ID = "WIDGET_STOP_ID"
private const val STOP_NAME = "WIDGET_STOP_NAME"
private const val BG_COLOR = "WIDGET_LINE_BACKGROUND_COLOR"
private const val TEXT_COLOR = "WIDGET_LINE_TEXT_COLOR"
private const val PREFERENCE_NAME = "WIDGET"
private const val TAG = "PrefsWidgetHelper"
fun savePrefs(
context: Context,
widgetId: Int,
lineId: String,
lineCode: String,
routeId: String,
routeName: String,
stopId: String,
stopName: String,
bgColor: Int,
textColor: Int
) {
Log.d(TAG, "savePrefs, widget id : $widgetId")
context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).edit(true) {
putString("${LINE_ID}_$widgetId", lineId)
putString("${LINE_CODE}_$widgetId", lineCode)
putString("${ROUTE_ID}_$widgetId", routeId)
putString("${ROUTE_NAME}_$widgetId", routeName)
putString("${STOP_ID}_$widgetId", stopId)
putString("${STOP_NAME}_$widgetId", stopName)
putInt("${BG_COLOR}_$widgetId", bgColor)
putInt("${TEXT_COLOR}_$widgetId", textColor)
}
}
data class WidgetPrefs(
val lineId: String,
val lineCode: String,
val routeId: String,
val routeName: String,
val stopId: String,
val stopName: String,
val bgColor: Int,
val textColor: Int
)
fun getPrefs(context: Context, widgetId: Int): WidgetPrefs? =
with(context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)) {
Log.d(TAG, "getPrefs, widget id : $widgetId")
WidgetPrefs(
getString("${LINE_ID}_$widgetId", null) ?: return null,
getString("${LINE_CODE}_$widgetId", null) ?: return null,
getString("${ROUTE_ID}_$widgetId", null) ?: return null,
getString("${ROUTE_NAME}_$widgetId", null) ?: return null,
getString("${STOP_ID}_$widgetId", null) ?: return null,
getString("${STOP_NAME}_$widgetId", null) ?: return null,
getInt("${BG_COLOR}_$widgetId", Color.BLACK),
getInt("${TEXT_COLOR}_$widgetId", Color.WHITE),
)
}
fun destroyPrefs(context: Context, widgetId: Int) {
Log.d(TAG, "destroyPrefs, widget id : $widgetId")
val prefs = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
prefs.edit(commit = true) {
remove("${LINE_ID}_$widgetId")
remove("${LINE_CODE}_$widgetId")
remove("${ROUTE_ID}_$widgetId")
remove("${ROUTE_NAME}_$widgetId")
remove("${STOP_ID}_$widgetId")
remove("${STOP_NAME}_$widgetId")
remove("${BG_COLOR}_$widgetId")
remove("${TEXT_COLOR}_$widgetId")
}
}
}
}

View File

@ -1,10 +1,10 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.32"
ext.kotlin_version = "1.5.0"
ext.ktor_version = "1.5.3"
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'
@ -18,7 +18,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

View File

@ -6,7 +6,7 @@ plugins {
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
buildToolsVersion "30.0.3"
defaultConfig {
minSdkVersion 21

View File

@ -5,7 +5,7 @@ plugins {
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "fr.oupson.wear_tao"