From 89dce1068f1bdb4bd91d0378403374744f31b07a Mon Sep 17 00:00:00 2001 From: Sylvia van Os Date: Tue, 27 Jul 2021 21:26:24 +0200 Subject: [PATCH] Shortcut fixes --- CHANGELOG.md | 7 +++++++ .../protect/card_locker/CardShortcutConfigure.java | 14 +++++++++++--- .../java/protect/card_locker/ImportExportTask.java | 1 - .../main/java/protect/card_locker/LoyaltyCard.java | 2 -- .../java/protect/card_locker/ShortcutHelper.java | 13 ++++++++++++- app/src/main/java/protect/card_locker/Utils.java | 6 ++++-- .../main/java/protect/card_locker/ZipUtils.java | 1 - .../card_locker/importexport/CatimaExporter.java | 4 ---- .../protect/card_locker/importexport/Exporter.java | 1 - .../importexport/MultiFormatExporter.java | 1 - .../card_locker/importexport/StocardImporter.java | 6 ------ 11 files changed, 34 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e788a52ae..5f541f89c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## Unreleased + +Changes: + +- Fix shortcut creation +- Generate card-specific shortcut icon + ## v2.0.3 (2021-07-25) Changes: diff --git a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java index c6a8cee52..05ade832c 100644 --- a/app/src/main/java/protect/card_locker/CardShortcutConfigure.java +++ b/app/src/main/java/protect/card_locker/CardShortcutConfigure.java @@ -2,8 +2,8 @@ package protect.card_locker; import android.content.Intent; import android.database.Cursor; +import android.graphics.Bitmap; import android.os.Bundle; -import android.os.Parcelable; import android.util.Log; import android.view.View; import android.widget.Toast; @@ -12,6 +12,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; +import androidx.recyclerview.widget.DefaultItemAnimator; +import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; /** @@ -47,6 +49,11 @@ public class CardShortcutConfigure extends AppCompatActivity implements LoyaltyC } final RecyclerView cardList = findViewById(R.id.list); + + RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); + cardList.setLayoutManager(mLayoutManager); + cardList.setItemAnimator(new DefaultItemAnimator()); + cardList.setVisibility(View.VISIBLE); Cursor cardCursor = db.getLoyaltyCardCursor(); @@ -72,11 +79,12 @@ public class CardShortcutConfigure extends AppCompatActivity implements LoyaltyC bundle.putBoolean("view", true); shortcutIntent.putExtras(bundle); - Parcelable icon = Intent.ShortcutIconResource.fromContext(CardShortcutConfigure.this, R.mipmap.ic_launcher); + Bitmap iconBitmap = Utils.generateIcon(CardShortcutConfigure.this, loyaltyCard, true).getLetterTile(); + Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, loyaltyCard.store); - intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); + intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, iconBitmap); setResult(RESULT_OK, intent); finish(); diff --git a/app/src/main/java/protect/card_locker/ImportExportTask.java b/app/src/main/java/protect/card_locker/ImportExportTask.java index 8701286a8..1538185c3 100644 --- a/app/src/main/java/protect/card_locker/ImportExportTask.java +++ b/app/src/main/java/protect/card_locker/ImportExportTask.java @@ -11,7 +11,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import protect.card_locker.importexport.DataFormat; diff --git a/app/src/main/java/protect/card_locker/LoyaltyCard.java b/app/src/main/java/protect/card_locker/LoyaltyCard.java index 3968c091c..f7f165d25 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCard.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCard.java @@ -7,8 +7,6 @@ import com.google.zxing.BarcodeFormat; import java.math.BigDecimal; import java.util.Currency; import java.util.Date; -import java.util.HashMap; -import java.util.Map; import androidx.annotation.Nullable; diff --git a/app/src/main/java/protect/card_locker/ShortcutHelper.java b/app/src/main/java/protect/card_locker/ShortcutHelper.java index 5773e5432..915e57526 100644 --- a/app/src/main/java/protect/card_locker/ShortcutHelper.java +++ b/app/src/main/java/protect/card_locker/ShortcutHelper.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutManager; +import android.graphics.Bitmap; import android.graphics.drawable.Icon; import android.os.Build; @@ -37,6 +38,8 @@ class ShortcutHelper ShortcutManager shortcutManager = context.getSystemService(ShortcutManager.class); LinkedList list = new LinkedList<>(shortcutManager.getDynamicShortcuts()); + DBHelper dbHelper = new DBHelper(context); + String shortcutId = Integer.toString(card.id); // Sort the shortcuts by rank, so working with the relative order will be easier. @@ -105,6 +108,14 @@ class ShortcutHelper Intent shortcutIntent = prevShortcut.getIntent(); + Bitmap iconBitmap = Utils.generateIcon(context, dbHelper.getLoyaltyCard(Integer.parseInt(prevShortcut.getId())), true).getLetterTile(); + Icon icon; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + icon = Icon.createWithAdaptiveBitmap(iconBitmap); + } else { + icon = Icon.createWithBitmap(iconBitmap); + } + // Prevent instances of the view activity from piling up; if one exists let this // one replace it. shortcutIntent.setFlags(shortcutIntent.getFlags() | Intent.FLAG_ACTIVITY_SINGLE_TOP); @@ -113,7 +124,7 @@ class ShortcutHelper .setShortLabel(prevShortcut.getShortLabel()) .setLongLabel(prevShortcut.getLongLabel()) .setIntent(shortcutIntent) - .setIcon(Icon.createWithResource(context, R.drawable.circle)) + .setIcon(icon) .setRank(index) .build(); diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index c920c70e1..26af7651e 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -9,7 +9,6 @@ import android.graphics.Color; import android.graphics.Matrix; import android.media.ExifInterface; import android.provider.MediaStore; -import android.util.Base64; import android.util.Log; import android.widget.Toast; @@ -33,7 +32,6 @@ import java.util.Currency; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashMap; -import java.util.List; import androidx.core.graphics.ColorUtils; @@ -54,6 +52,10 @@ public class Utils { static final int BITMAP_SIZE_BIG = 512; + static public LetterBitmap generateIcon(Context context, LoyaltyCard loyaltyCard, boolean forShortcut) { + return generateIcon(context, loyaltyCard.store, loyaltyCard.headerColor, forShortcut); + } + static public LetterBitmap generateIcon(Context context, String store, Integer backgroundColor) { return generateIcon(context, store, backgroundColor, false); } diff --git a/app/src/main/java/protect/card_locker/ZipUtils.java b/app/src/main/java/protect/card_locker/ZipUtils.java index 2472f7d44..9935323b5 100644 --- a/app/src/main/java/protect/card_locker/ZipUtils.java +++ b/app/src/main/java/protect/card_locker/ZipUtils.java @@ -12,7 +12,6 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; public class ZipUtils { diff --git a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java index e8b3c5522..00050c691 100644 --- a/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/CatimaExporter.java @@ -4,7 +4,6 @@ import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; -import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.io.outputstream.ZipOutputStream; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.InternalZipConstants; @@ -12,16 +11,13 @@ import net.lingala.zip4j.util.InternalZipConstants; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; -import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; -import java.util.zip.ZipEntry; import protect.card_locker.DBHelper; import protect.card_locker.Group; diff --git a/app/src/main/java/protect/card_locker/importexport/Exporter.java b/app/src/main/java/protect/card_locker/importexport/Exporter.java index 1b4b48322..6f70f6d80 100644 --- a/app/src/main/java/protect/card_locker/importexport/Exporter.java +++ b/app/src/main/java/protect/card_locker/importexport/Exporter.java @@ -4,7 +4,6 @@ import android.content.Context; import java.io.IOException; import java.io.OutputStream; -import java.io.OutputStreamWriter; import protect.card_locker.DBHelper; diff --git a/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java b/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java index 56ca28ece..8e95c49c4 100644 --- a/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java +++ b/app/src/main/java/protect/card_locker/importexport/MultiFormatExporter.java @@ -5,7 +5,6 @@ import android.util.Log; import java.io.IOException; import java.io.OutputStream; -import java.io.OutputStreamWriter; import protect.card_locker.DBHelper; diff --git a/app/src/main/java/protect/card_locker/importexport/StocardImporter.java b/app/src/main/java/protect/card_locker/importexport/StocardImporter.java index a63b26bf5..76292bd52 100644 --- a/app/src/main/java/protect/card_locker/importexport/StocardImporter.java +++ b/app/src/main/java/protect/card_locker/importexport/StocardImporter.java @@ -3,7 +3,6 @@ package protect.card_locker.importexport; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import com.google.zxing.BarcodeFormat; @@ -14,14 +13,9 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; import java.math.BigDecimal; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.text.ParseException; import java.util.ArrayList; import java.util.HashMap;