diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 98f978d98..a65683a29 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -30,6 +30,20 @@ android:supportsRtl="true" android:theme="@style/AppTheme" android:localeConfig="@xml/locales_config"> + + + + + + + + + + + diff --git a/app/src/main/java/protect/card_locker/CatimaRemoteViewsService.kt b/app/src/main/java/protect/card_locker/CatimaRemoteViewsService.kt new file mode 100644 index 000000000..d4ca18443 --- /dev/null +++ b/app/src/main/java/protect/card_locker/CatimaRemoteViewsService.kt @@ -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 = 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) + } +} \ No newline at end of file diff --git a/app/src/main/java/protect/card_locker/CatimaWidget.kt b/app/src/main/java/protect/card_locker/CatimaWidget.kt new file mode 100644 index 000000000..86b6c6070 --- /dev/null +++ b/app/src/main/java/protect/card_locker/CatimaWidget.kt @@ -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) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index dbcbd8a0d..9adc8ee10 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -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 parseResultList, String group, boolean closeAppOnNoBarcode) { diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index a8eec2319..87a96371d 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -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; + } } diff --git a/app/src/main/res/drawable-nodpi/widget_preview.png b/app/src/main/res/drawable-nodpi/widget_preview.png new file mode 100644 index 000000000..2e738a426 Binary files /dev/null and b/app/src/main/res/drawable-nodpi/widget_preview.png differ diff --git a/app/src/main/res/layout/catima_widget.xml b/app/src/main/res/layout/catima_widget.xml new file mode 100644 index 000000000..e85e1091a --- /dev/null +++ b/app/src/main/res/layout/catima_widget.xml @@ -0,0 +1,17 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/catima_widget_empty.xml b/app/src/main/res/layout/catima_widget_empty.xml new file mode 100644 index 000000000..a1857232c --- /dev/null +++ b/app/src/main/res/layout/catima_widget_empty.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/catima_widget_item.xml b/app/src/main/res/layout/catima_widget_item.xml new file mode 100644 index 000000000..84b757039 --- /dev/null +++ b/app/src/main/res/layout/catima_widget_item.xml @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d78a86dc2..420f84b2f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -362,5 +362,7 @@ This file is not supported Sorry, something went wrong, please try again... Width + Card list Set Barcode Width + No loyalty cards diff --git a/app/src/main/res/xml/catima_widget_info.xml b/app/src/main/res/xml/catima_widget_info.xml new file mode 100644 index 000000000..2efb897aa --- /dev/null +++ b/app/src/main/res/xml/catima_widget_info.xml @@ -0,0 +1,12 @@ + +