Initial widget work

Co-authored-by: Sylvia van Os <sylvia@hackerchick.me>
This commit is contained in:
realwk
2025-07-30 21:21:55 +02:00
committed by Sylvia van Os
parent e217e99864
commit 9a0149def8
11 changed files with 280 additions and 16 deletions

View File

@@ -30,6 +30,20 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:localeConfig="@xml/locales_config">
<receiver
android:name="CatimaWidget"
android:label="@string/card_list_widget_name"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/catima_widget_info" />
</receiver>
<activity
android:name=".MainActivity"
android:exported="true"
@@ -204,5 +218,9 @@
<action android:name="android.service.controls.ControlsProviderService" />
</intent-filter>
</service>
<service
android:name=".CatimaRemoteViewsService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
</application>
</manifest>

View File

@@ -0,0 +1,110 @@
package protect.card_locker
import android.content.Context
import android.content.Intent
import android.database.sqlite.SQLiteDatabase
import android.graphics.Color
import android.graphics.drawable.Icon
import android.os.Build
import android.view.View
import android.widget.RemoteViews
import android.widget.RemoteViewsService
import protect.card_locker.DBHelper.LoyaltyCardArchiveFilter
import kotlin.math.max
class CatimaWidgetRemoteViewsFactory(private var context: Context) :
RemoteViewsService.RemoteViewsFactory {
private var mDatabase: SQLiteDatabase = DBHelper(context).readableDatabase
override fun onCreate() {
onDataSetChanged()
}
private var mCards: ArrayList<LoyaltyCard> = ArrayList()
override fun onDataSetChanged() {
val mOrder = Utils.getLoyaltyCardOrder(context);
val mOrderDirection = Utils.getLoyaltyCardOrderDirection(context);
val cur = DBHelper.getLoyaltyCardCursor(
mDatabase,
"",
null,
mOrder,
mOrderDirection,
LoyaltyCardArchiveFilter.Unarchived
)
mCards.clear()
if (cur.moveToFirst()) {
do {
val item = LoyaltyCard.fromCursor(context, cur)
mCards.add(item)
} while (cur.moveToNext())
}
}
override fun onDestroy() {}
override fun getCount(): Int {
return max(1, mCards.count())
}
private fun createRemoteView(item: LoyaltyCard): RemoteViews
{
val rv = RemoteViews(context.packageName, R.layout.catima_widget_item).apply {
val headerColor = Utils.getHeaderColor(context, item)
val foreground = if (Utils.needsDarkForeground(headerColor)) Color.BLACK else Color.WHITE
setInt(R.id.item_container, "setBackgroundColor", headerColor)
val icon = item.getImageThumbnail(context)
// setImageViewIcon is not supported on Android 5, so force Android 5 down the text path
if (icon != null && Build.VERSION.SDK_INT >= 23) {
setInt(R.id.item_container, "setBackgroundColor", foreground)
setImageViewIcon(R.id.item_image, Icon.createWithBitmap(icon))
setViewVisibility(R.id.item_text, View.INVISIBLE)
setViewVisibility(R.id.item_image, View.VISIBLE)
} else {
setImageViewBitmap(R.id.item_image, null)
setTextViewText(R.id.item_text, item.store)
setViewVisibility(R.id.item_text, View.VISIBLE)
setViewVisibility(R.id.item_image, View.INVISIBLE)
setTextColor(
R.id.item_text,
foreground
)
}
val fillInIntent = Intent().apply {
putExtra(LoyaltyCardViewActivity.BUNDLE_ID, item.id)
}
setOnClickFillInIntent(R.id.item_container, fillInIntent)
}
return rv
}
override fun getViewAt(position: Int): RemoteViews {
if (mCards.isEmpty()) {
return RemoteViews(context.packageName, R.layout.catima_widget_empty)
}
val item = mCards[position]
return createRemoteView(item)
}
override fun getLoadingView(): RemoteViews? = null
override fun getViewTypeCount(): Int = 1
override fun getItemId(position: Int): Long = position.toLong()
override fun hasStableIds(): Boolean = true
}
class CatimaRemoteViewsService : RemoteViewsService() {
override fun onGetViewFactory(intent: Intent?): RemoteViewsFactory {
return CatimaWidgetRemoteViewsFactory(applicationContext)
}
}

View File

@@ -0,0 +1,34 @@
package protect.card_locker
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
class CatimaWidget : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
for (appWidgetId in appWidgetIds) {
val views = RemoteViews(context.packageName, R.layout.catima_widget)
val intent = Intent(context, CatimaRemoteViewsService::class.java)
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
views.setRemoteAdapter(R.id.grid_view, intent)
val templateIntent = Intent(context, LoyaltyCardViewActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
context,
0,
templateIntent,
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
views.setPendingIntentTemplate(R.id.grid_view, pendingIntent)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
}

View File

@@ -2,6 +2,8 @@ package protect.card_locker;
import android.app.Activity;
import android.app.SearchManager;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@@ -330,22 +332,8 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
selectedTab = activeTabPref.getInt(getString(R.string.sharedpreference_active_tab), 0);
// Restore sort preferences from Shared Preferences
// If one of the sorting prefererences has never been set or is set to an invalid value,
// stick to the defaults.
SharedPreferences sortPref = getApplicationContext().getSharedPreferences(
getString(R.string.sharedpreference_sort),
Context.MODE_PRIVATE);
String orderString = sortPref.getString(getString(R.string.sharedpreference_sort_order), null);
String orderDirectionString = sortPref.getString(getString(R.string.sharedpreference_sort_direction), null);
if (orderString != null && orderDirectionString != null) {
try {
mOrder = DBHelper.LoyaltyCardOrder.valueOf(orderString);
mOrderDirection = DBHelper.LoyaltyCardOrderDirection.valueOf(orderDirectionString);
} catch (IllegalArgumentException ignored) {
}
}
mOrder = Utils.getLoyaltyCardOrder(this);
mOrderDirection = Utils.getLoyaltyCardOrderDirection(this);
mGroup = null;
@@ -442,6 +430,14 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard
if (mCurrentActionMode != null) {
mCurrentActionMode.finish();
}
updateWidget(mAdapter.mContext);
}
private void updateWidget(Context context) {
AppWidgetManager manager = AppWidgetManager.getInstance(context);
int[] ids = manager.getAppWidgetIds(new ComponentName(context, CatimaWidget.class));
manager.notifyAppWidgetViewDataChanged(ids, R.id.grid_view);
}
private void processParseResultList(List<ParseResult> parseResultList, String group, boolean closeAppOnNoBarcode) {

View File

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
@@ -1176,4 +1177,40 @@ public class Utils {
return ImageView.ScaleType.FIT_CENTER;
}
public static DBHelper.LoyaltyCardOrder getLoyaltyCardOrder(Context context) {
SharedPreferences sortPref = context.getSharedPreferences(
"sharedpreference_sort",
Context.MODE_PRIVATE
);
String orderString = sortPref.getString("sharedpreference_sort_order", null);
if (orderString != null) {
try {
return DBHelper.LoyaltyCardOrder.valueOf(orderString);
} catch (IllegalArgumentException ignored) {
}
}
return DBHelper.LoyaltyCardOrder.Alpha;
}
public static DBHelper.LoyaltyCardOrderDirection getLoyaltyCardOrderDirection(Context context) {
SharedPreferences sortPref = context.getSharedPreferences(
"sharedpreference_sort",
Context.MODE_PRIVATE
);
String orderDirectionString = sortPref.getString("sharedpreference_sort_direction", null);
if (orderDirectionString != null) {
try {
return DBHelper.LoyaltyCardOrderDirection.valueOf(orderDirectionString);
} catch (IllegalArgumentException ignored) {
}
}
return DBHelper.LoyaltyCardOrderDirection.Ascending;
}
}

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:verticalSpacing="4dp"
android:horizontalSpacing="4dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>

View File

@@ -0,0 +1,12 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/widget_layout"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/no_cards_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/no_loyalty_cards" />
</LinearLayout>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/item_container"
android:padding="4dp">
<ImageView
android:id="@+id/item_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|center" />
<TextView
android:id="@+id/item_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:autoSizeMinTextSize="6sp"
app:autoSizeTextType="uniform"
android:layout_gravity="center|center"
android:gravity="center" />
</FrameLayout>

View File

@@ -362,5 +362,7 @@
<string name="unsupportedFile">This file is not supported</string>
<string name="generic_error_please_retry">Sorry, something went wrong, please try again...</string>
<string name="width">Width</string>
<string name="card_list_widget_name">Card list</string>
<string name="setBarcodeWidth">Set Barcode Width</string>
<string name="no_loyalty_cards">No loyalty cards</string>
</resources>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/catima_widget"
android:initialLayout="@layout/catima_widget"
android:minWidth="245dp"
android:minHeight="54dp"
android:previewImage="@drawable/widget_preview"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="4"
android:targetCellHeight="2"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen" />