Fix widget creating different-looking shortcut than app shortcuts

This commit is contained in:
Sylvia van Os
2021-08-08 23:49:10 +02:00
parent c1b02f58ff
commit c273d8d17f
5 changed files with 42 additions and 49 deletions

View File

@@ -1,5 +1,11 @@
# Changelog
## Unreleased
Changes:
- Fix widget creating different-looking shortcut than app shortcuts
## v2.2.2 (2021-08-08)
Changes:

View File

@@ -1,8 +1,6 @@
package protect.card_locker;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
@@ -12,6 +10,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@@ -69,23 +69,9 @@ public class CardShortcutConfigure extends AppCompatActivity implements LoyaltyC
Log.d(TAG, "Creating shortcut for card " + loyaltyCard.store + "," + loyaltyCard.id);
Intent shortcutIntent = new Intent(CardShortcutConfigure.this, LoyaltyCardViewActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
// Prevent instances of the view activity from piling up; if one exists let this
// one replace it.
shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();
bundle.putInt("id", loyaltyCard.id);
bundle.putBoolean("view", true);
shortcutIntent.putExtras(bundle);
ShortcutInfoCompat shortcut = ShortcutHelper.createShortcutBuilder(CardShortcutConfigure.this, loyaltyCard).build();
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, iconBitmap);
setResult(RESULT_OK, intent);
setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(CardShortcutConfigure.this, shortcut));
finish();
}

View File

@@ -696,7 +696,7 @@ public class MainActivity extends AppCompatActivity implements LoyaltyCardCursor
b.putInt("id", loyaltyCard.id);
i.putExtras(b);
ShortcutHelper.updateShortcuts(MainActivity.this, loyaltyCard, i);
ShortcutHelper.updateShortcuts(MainActivity.this, loyaltyCard);
startActivityForResult(i, Utils.MAIN_REQUEST);
}

View File

@@ -2,7 +2,6 @@ package protect.card_locker;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.text.InputType;
import android.view.MenuItem;

View File

@@ -3,16 +3,17 @@ package protect.card_locker;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.core.graphics.drawable.IconCompat;
import android.os.Bundle;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.core.graphics.drawable.IconCompat;
class ShortcutHelper
{
// Android documentation says that no more than 5 shortcuts
@@ -29,7 +30,7 @@ class ShortcutHelper
* card exceeds the max number of shortcuts, then the least recently
* used card shortcut is discarded.
*/
static void updateShortcuts(Context context, LoyaltyCard card, Intent intent)
static void updateShortcuts(Context context, LoyaltyCard card)
{
LinkedList<ShortcutInfoCompat> list = new LinkedList<>(ShortcutManagerCompat.getDynamicShortcuts(context));
@@ -71,11 +72,7 @@ class ShortcutHelper
list.pollLast();
}
ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, Integer.toString(card.id))
.setShortLabel(card.store)
.setLongLabel(card.store)
.setIntent(intent)
.build();
ShortcutInfoCompat shortcut = createShortcutBuilder(context, card).build();
list.addFirst(shortcut);
}
@@ -87,28 +84,11 @@ class ShortcutHelper
{
ShortcutInfoCompat prevShortcut = list.get(index);
Intent shortcutIntent = prevShortcut.getIntent();
LoyaltyCard loyaltyCard = dbHelper.getLoyaltyCard(Integer.parseInt(prevShortcut.getId()));
Bitmap iconBitmap = Utils.generateIcon(context, dbHelper.getLoyaltyCard(Integer.parseInt(prevShortcut.getId())), true).getLetterTile();
IconCompat icon = IconCompat.createWithAdaptiveBitmap(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);
final CharSequence longLabel = prevShortcut.getLongLabel();
ShortcutInfoCompat updatedShortcut = null;
if (longLabel != null) {
updatedShortcut = new ShortcutInfoCompat.Builder(context, prevShortcut.getId())
.setShortLabel(prevShortcut.getShortLabel())
.setLongLabel(longLabel)
.setIntent(shortcutIntent)
.setIcon(icon)
ShortcutInfoCompat updatedShortcut = createShortcutBuilder(context, loyaltyCard)
.setRank(index)
.build();
}
finalList.addLast(updatedShortcut);
}
@@ -137,4 +117,26 @@ class ShortcutHelper
ShortcutManagerCompat.setDynamicShortcuts(context, list);
}
static ShortcutInfoCompat.Builder createShortcutBuilder(Context context, LoyaltyCard loyaltyCard) {
Intent intent = new Intent(context, LoyaltyCardViewActivity.class);
intent.setAction(Intent.ACTION_MAIN);
// Prevent instances of the view activity from piling up; if one exists let this
// one replace it.
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final Bundle bundle = new Bundle();
bundle.putInt("id", loyaltyCard.id);
bundle.putBoolean("view", true);
intent.putExtras(bundle);
Bitmap iconBitmap = Utils.generateIcon(context, loyaltyCard, true).getLetterTile();
IconCompat icon = IconCompat.createWithAdaptiveBitmap(iconBitmap);
return new ShortcutInfoCompat.Builder(context, Integer.toString(loyaltyCard.id))
.setShortLabel(loyaltyCard.store)
.setLongLabel(loyaltyCard.store)
.setIntent(intent)
.setIcon(icon);
}
}