diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e274510792..27c69b8709 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -114,6 +114,17 @@ + + + + + + + { + Settings mSettings; + private final Context mContext; + private final GroupAdapterListener mListener; + SQLiteDatabase mDatabase; + + public GroupSelectCursorAdapter(Context inputContext, Cursor inputCursor, GroupAdapterListener inputListener) { + super(inputCursor, DBHelper.LoyaltyCardDbGroups.ORDER); + setHasStableIds(true); + mSettings = new Settings(inputContext); + mContext = inputContext.getApplicationContext(); + mListener = inputListener; + mDatabase = new DBHelper(inputContext).getReadableDatabase(); + + swapCursor(inputCursor); + } + + @NonNull + @Override + public GroupSelectCursorAdapter.GroupListItemViewHolder onCreateViewHolder(ViewGroup inputParent, int inputViewType) { + View itemView = LayoutInflater.from(inputParent.getContext()).inflate(R.layout.group_select_layout, inputParent, false); + return new GroupListItemViewHolder(itemView); + } + + public void onBindViewHolder(GroupListItemViewHolder inputHolder, Cursor inputCursor) { + Group group = Group.toGroup(inputCursor); + + inputHolder.mName.setText(group._id); + + int groupCardCount = DBHelper.getGroupCardCount(mDatabase, group._id); + inputHolder.mCardCount.setText(mContext.getResources().getQuantityString(R.plurals.groupCardCount, groupCardCount, groupCardCount)); + + inputHolder.mName.setTextSize(mSettings.getFontSizeMax(mSettings.getMediumFont())); + inputHolder.mCardCount.setTextSize(mSettings.getFontSizeMax(mSettings.getSmallFont())); + + applyClickEvents(inputHolder); + } + + private void applyClickEvents(GroupListItemViewHolder inputHolder) { + inputHolder.mSelect.setOnClickListener(view -> mListener.onSelectButtonClicked(inputHolder.itemView)); + } + + public interface GroupAdapterListener { + void onSelectButtonClicked(View view); + } + + public static class GroupListItemViewHolder extends RecyclerView.ViewHolder { + public TextView mName, mCardCount; + public AppCompatButton mSelect; + + public GroupListItemViewHolder(View inputView) { + super(inputView); + mName = inputView.findViewById(R.id.name); + mCardCount = inputView.findViewById(R.id.cardCount); + mSelect = inputView.findViewById(R.id.select); + } + } +} diff --git a/app/src/main/java/protect/card_locker/GroupShortcutConfigure.java b/app/src/main/java/protect/card_locker/GroupShortcutConfigure.java new file mode 100644 index 0000000000..d49d2cad5e --- /dev/null +++ b/app/src/main/java/protect/card_locker/GroupShortcutConfigure.java @@ -0,0 +1,81 @@ +package protect.card_locker; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.appcompat.widget.Toolbar; +import androidx.core.content.pm.ShortcutInfoCompat; +import androidx.core.content.pm.ShortcutManagerCompat; +import androidx.recyclerview.widget.RecyclerView; + +/** + * The configuration screen for creating a shortcut. + */ +public class GroupShortcutConfigure extends CatimaAppCompatActivity implements GroupSelectCursorAdapter.GroupAdapterListener { + static final String TAG = "Catima"; + private SQLiteDatabase mDatabase; + + @Override + public void onCreate(Bundle bundle) { + super.onCreate(bundle); + + mDatabase = new DBHelper(this).getReadableDatabase(); + + + // Set the result to CANCELED. This will cause nothing to happen if the + // aback button is pressed. + setResult(RESULT_CANCELED); + + setContentView(R.layout.simple_toolbar_list_activity); + Toolbar toolbar = findViewById(R.id.toolbar); + toolbar.setTitle(R.string.shortcutSelectGroup); + + // If there are no groups, bail + if (DBHelper.getGroupCount(mDatabase) == 0) { + Toast.makeText(this, R.string.noGroups, Toast.LENGTH_LONG).show(); + finish(); + } + + final RecyclerView groupList = findViewById(R.id.list); + + Cursor groupCursor = DBHelper.getGroupCursor(mDatabase); + final GroupSelectCursorAdapter adapter = new GroupSelectCursorAdapter(this, groupCursor,this); + groupList.setAdapter(adapter); + } + + private String getGroupName(View view) { + TextView groupNameTextView = view.findViewById(R.id.name); + return (String) groupNameTextView.getText(); + } + + private void onClickAction(View view) { + String groupId = getGroupName(view); + if (groupId == null) { + throw (new IllegalArgumentException("The widget expects a group")); + } + Log.d("groupId", "groupId: " + groupId); + Group group = DBHelper.getGroup(mDatabase, groupId); + if (group == null) { + throw (new IllegalArgumentException("cannot load group " + groupId + " from database")); + } + + Log.d(TAG, "Creating shortcut for group " + group._id + "," + group._id); + + ShortcutInfoCompat shortcut = ShortcutHelper.createGroupShortcutBuilder(GroupShortcutConfigure.this, group).build(); + + setResult(RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(GroupShortcutConfigure.this, shortcut)); + + finish(); + } + + @Override + public void onSelectButtonClicked(View view) { + onClickAction(view); + } + +} diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index fcc3d11aed..9d7ec922cd 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -42,6 +42,7 @@ import com.google.android.material.tabs.TabLayout; import com.jaredrummler.android.colorpicker.ColorPickerDialog; import com.jaredrummler.android.colorpicker.ColorPickerDialogListener; +import com.yalantis.ucrop.BuildConfig; import com.yalantis.ucrop.UCrop; import com.yalantis.ucrop.model.AspectRatio; diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 8ae284413a..ad11d49f62 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -714,7 +714,6 @@ public void onBackPressed() { setFullscreen(false); return; } - super.onBackPressed(); } diff --git a/app/src/main/java/protect/card_locker/MainActivity.java b/app/src/main/java/protect/card_locker/MainActivity.java index ec9a101dd3..35a4f9880a 100644 --- a/app/src/main/java/protect/card_locker/MainActivity.java +++ b/app/src/main/java/protect/card_locker/MainActivity.java @@ -69,6 +69,8 @@ public class MainActivity extends CatimaAppCompatActivity implements LoyaltyCard private View mNoMatchingCardsText; private View mNoGroupCardsText; + private String groupWidget = null; + private boolean mArchiveMode; public static final String BUNDLE_ARCHIVE_MODE = "archiveMode"; @@ -225,6 +227,16 @@ public void onDestroyActionMode(ActionMode inputMode) { } }; + private void extractIntentFields(Intent intent) + { + final Bundle b = intent.getExtras(); + groupWidget = b != null ? b.getString("groupId") : null; + mArchiveMode = b != null && b.getBoolean(BUNDLE_ARCHIVE_MODE, false); + if(!mArchiveMode) { + Log.d(TAG, "View activity: id=" + groupWidget); + } + } + @Override protected void onCreate(Bundle inputSavedInstanceState) { extractIntentFields(getIntent()); @@ -238,7 +250,7 @@ protected void onCreate(Bundle inputSavedInstanceState) { setTitle(R.string.archiveList); setContentView(R.layout.archive_activity); } - + Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -378,10 +390,25 @@ protected void onResume() { updateTabGroups(groupsTabLayout); // Restore settings from Shared Preference - SharedPreferences activeTabPref = getApplicationContext().getSharedPreferences( - getString(R.string.sharedpreference_active_tab), - Context.MODE_PRIVATE); - selectedTab = activeTabPref.getInt(getString(R.string.sharedpreference_active_tab), 0); + if (groupWidget != null){ + Group groupSelected = DBHelper.getGroup(mDatabase, groupWidget); + if (groupSelected == null) { + Log.w(TAG, "Could not lookup group " + groupWidget); + Toast.makeText(this, R.string.noGroupExistsError, Toast.LENGTH_LONG).show(); + finish(); + return; + } + else{ + selectedTab = groupSelected.order+1; + } + } + else { + SharedPreferences activeTabPref = getApplicationContext().getSharedPreferences( + getString(R.string.sharedpreference_active_tab), + Context.MODE_PRIVATE); + selectedTab = activeTabPref.getInt(getString(R.string.sharedpreference_active_tab), 0); + } + SharedPreferences sortPref = getApplicationContext().getSharedPreferences( getString(R.string.sharedpreference_sort), Context.MODE_PRIVATE); @@ -404,6 +431,7 @@ protected void onResume() { mGroup = tab.getTag(); } updateLoyaltyCardList(true); + groupWidget = null; // End of active tab logic if (!mArchiveMode) { @@ -498,11 +526,6 @@ private void updateLoyaltyCardList(boolean updateCount) { } } - private void extractIntentFields(Intent intent) { - final Bundle b = intent.getExtras(); - mArchiveMode = b != null && b.getBoolean(BUNDLE_ARCHIVE_MODE, false); - } - public void updateTabGroups(TabLayout groupsTabLayout) { List newGroups = DBHelper.getGroups(mDatabase); @@ -538,9 +561,6 @@ public boolean onCreateOptionsMenu(Menu inputMenu) { getMenuInflater().inflate(R.menu.archive_menu, inputMenu); } - Utils.updateMenuCardDetailsButtonState(inputMenu.findItem(R.id.action_unfold), mAdapter.showingDetails()); - displayCardSetupOptions(inputMenu, mLoyaltyCardCount > 0); - SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); if (searchManager != null) { mSearchView = (SearchView) inputMenu.findItem(R.id.action_search).getActionView(); @@ -885,4 +905,12 @@ public void onRowClicked(int inputPosition) { startActivity(intent); } } + + @Override + public void onNewIntent(Intent intent) { + super.onNewIntent(intent); + + Log.i(TAG, "Received new intent"); + extractIntentFields(intent); + } } diff --git a/app/src/main/java/protect/card_locker/ManageGroupActivity.java b/app/src/main/java/protect/card_locker/ManageGroupActivity.java index 4b5174dc48..dfa358512f 100644 --- a/app/src/main/java/protect/card_locker/ManageGroupActivity.java +++ b/app/src/main/java/protect/card_locker/ManageGroupActivity.java @@ -235,6 +235,5 @@ public void onRowLongClicked(int inputPosition) { @Override public void onRowClicked(int inputPosition) { mAdapter.toggleSelection(inputPosition); - } } diff --git a/app/src/main/java/protect/card_locker/ShortcutHelper.java b/app/src/main/java/protect/card_locker/ShortcutHelper.java index e974421a95..1b6fb4d938 100644 --- a/app/src/main/java/protect/card_locker/ShortcutHelper.java +++ b/app/src/main/java/protect/card_locker/ShortcutHelper.java @@ -135,7 +135,11 @@ static ShortcutInfoCompat.Builder createShortcutBuilder(Context context, Loyalty 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); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); + final Bundle bundle = new Bundle(); bundle.putInt("id", loyaltyCard.id); bundle.putBoolean("view", true); @@ -149,11 +153,34 @@ static ShortcutInfoCompat.Builder createShortcutBuilder(Context context, Loyalty } IconCompat icon = IconCompat.createWithAdaptiveBitmap(iconBitmap); - return new ShortcutInfoCompat.Builder(context, Integer.toString(loyaltyCard.id)) .setShortLabel(loyaltyCard.store) .setLongLabel(loyaltyCard.store) .setIntent(intent) .setIcon(icon); } + static ShortcutInfoCompat.Builder createGroupShortcutBuilder(Context context, Group group) { + Intent intent = new Intent(context, MainActivity.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.FLAG_ACTIVITY_CLEAR_TOP); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); + final Bundle bundle = new Bundle(); + // Done like with the cards but with group._id (a string) + bundle.putString("groupId", group._id); + intent.putExtras(bundle); + + String shortcutId = "GROUP_" + group._id; + + IconCompat shortcutIcon= IconCompat.createWithResource(context, R.mipmap.ic_launcher); + + return new ShortcutInfoCompat.Builder(context, shortcutId) + .setShortLabel(group._id) + .setLongLabel(group._id) + .setIntent(intent) + .setIcon(shortcutIcon); + } } diff --git a/app/src/main/res/layout/group_main.xml b/app/src/main/res/layout/group_main.xml index 7df73c3e3d..755d89d5a7 100644 --- a/app/src/main/res/layout/group_main.xml +++ b/app/src/main/res/layout/group_main.xml @@ -25,4 +25,5 @@ android:clipToPadding="false" android:scrollbars="vertical" android:visibility="gone" /> + diff --git a/app/src/main/res/layout/group_select_layout.xml b/app/src/main/res/layout/group_select_layout.xml new file mode 100644 index 0000000000..1df7794109 --- /dev/null +++ b/app/src/main/res/layout/group_select_layout.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + +