1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 09:17:29 +01:00

Use package name for Catima permissions

The Catima permissions had to be split per package-name to avoid
conflicts between debug and release channels due to mismatching apk
signatures.

See https://github.com/CatimaLoyalty/Android/pull/1411
This commit is contained in:
José Rebelo 2023-07-09 14:29:30 +01:00
parent f81ffc3447
commit ac89b1df9d
3 changed files with 58 additions and 46 deletions

View File

@ -35,6 +35,7 @@
<!-- Read loyalty cards from Catima --> <!-- Read loyalty cards from Catima -->
<uses-permission android:name="me.hackerchick.catima.READ_CARDS"/> <uses-permission android:name="me.hackerchick.catima.READ_CARDS"/>
<uses-permission android:name="me.hackerchick.catima.debug.READ_CARDS"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.READ_CALENDAR" />

View File

@ -74,7 +74,7 @@ public class LoyaltyCardsSettingsFragment extends PreferenceFragmentCompat {
private GBDevice device; private GBDevice device;
private void setSettingsFileSuffix(final String settingsFileSuffix) { private void setSettingsFileSuffix(final String settingsFileSuffix) {
Bundle args = new Bundle(); final Bundle args = new Bundle();
args.putString("settingsFileSuffix", settingsFileSuffix); args.putString("settingsFileSuffix", settingsFileSuffix);
setArguments(args); setArguments(args);
} }
@ -113,45 +113,52 @@ public class LoyaltyCardsSettingsFragment extends PreferenceFragmentCompat {
return fragment; return fragment;
} }
protected void reloadPreferences(String catimaPackageName) { protected void reloadPreferences(final String catimaPackageName) {
final CatimaManager catimaManager = new CatimaManager(requireContext()); final CatimaManager catimaManager = new CatimaManager(requireContext());
final List<CharSequence> installedCatimaPackages = catimaManager.findInstalledCatimaPackages(); final List<CharSequence> installedCatimaPackages = catimaManager.findInstalledCatimaPackages();
final boolean catimaInstalled = !installedCatimaPackages.isEmpty(); final boolean catimaInstalled = !installedCatimaPackages.isEmpty();
final ListPreference catimaPackage = findPreference(LOYALTY_CARDS_CATIMA_PACKAGE); final ListPreference catimaPackagePreference = findPreference(LOYALTY_CARDS_CATIMA_PACKAGE);
CatimaContentProvider catima = null;
if (catimaPackage != null) { if (catimaPackagePreference != null) {
catimaPackage.setEntries(installedCatimaPackages.toArray(new CharSequence[0])); catimaPackagePreference.setEntries(installedCatimaPackages.toArray(new CharSequence[0]));
catimaPackage.setEntryValues(installedCatimaPackages.toArray(new CharSequence[0])); catimaPackagePreference.setEntryValues(installedCatimaPackages.toArray(new CharSequence[0]));
catimaPackage.setOnPreferenceChangeListener((preference, newValue) -> { catimaPackagePreference.setVisible(installedCatimaPackages.size() > 1);
LOG.info("Catima package changed to {}", newValue); catimaPackagePreference.setOnPreferenceChangeListener((preference, newValue) -> {
LOG.info("Catima package preference changed to {}", newValue);
reloadPreferences((String) newValue); reloadPreferences((String) newValue);
return true; return true;
}); });
if (catimaInstalled) { if (catimaInstalled) {
// Ensure the currently selected value is actually an installed Catima package // Ensure the currently selected value is actually an installed Catima package
if (StringUtils.isNullOrEmpty(catimaPackage.getValue()) || !installedCatimaPackages.contains(catimaPackage.getValue())) { if (StringUtils.isNullOrEmpty(catimaPackagePreference.getValue()) || !installedCatimaPackages.contains(catimaPackagePreference.getValue())) {
catimaPackage.setValue(installedCatimaPackages.get(0).toString()); catimaPackagePreference.setValue(installedCatimaPackages.get(0).toString());
}
} }
} }
if (installedCatimaPackages.size() <= 1) { final String finalCatimaPackageName;
catimaPackage.setVisible(false);
if (catimaPackageName != null) {
finalCatimaPackageName = catimaPackageName;
} else if (catimaPackagePreference != null) {
finalCatimaPackageName = catimaPackagePreference.getValue();
} else {
LOG.warn("This should never happen - catima package not found");
finalCatimaPackageName = "this.should.never.happen";
} }
catima = new CatimaContentProvider(requireContext(), catimaPackageName != null ? catimaPackageName : catimaPackage.getValue()); final CatimaContentProvider catima = new CatimaContentProvider(requireContext(), finalCatimaPackageName);
}
final Preference openCatima = findPreference(LOYALTY_CARDS_OPEN_CATIMA); final Preference openCatimaPreference = findPreference(LOYALTY_CARDS_OPEN_CATIMA);
if (openCatima != null) { if (openCatimaPreference != null) {
openCatima.setVisible(catimaInstalled); openCatimaPreference.setVisible(catimaInstalled);
openCatima.setOnPreferenceClickListener(preference -> { openCatimaPreference.setOnPreferenceClickListener(preference -> {
if (catimaPackage != null) { if (catimaPackagePreference != null) {
final PackageManager packageManager = requireContext().getPackageManager(); final PackageManager packageManager = requireContext().getPackageManager();
final Intent launchIntent = packageManager.getLaunchIntentForPackage(catimaPackageName != null ? catimaPackageName : catimaPackage.getValue()); final Intent launchIntent = packageManager.getLaunchIntentForPackage(finalCatimaPackageName);
if (launchIntent != null) { if (launchIntent != null) {
startActivity(launchIntent); startActivity(launchIntent);
} }
@ -160,44 +167,44 @@ public class LoyaltyCardsSettingsFragment extends PreferenceFragmentCompat {
}); });
} }
final Preference catimaNotInstalled = findPreference(LOYALTY_CARDS_CATIMA_NOT_INSTALLED); final Preference catimaNotInstalledPreference = findPreference(LOYALTY_CARDS_CATIMA_NOT_INSTALLED);
if (catimaNotInstalled != null) { if (catimaNotInstalledPreference != null) {
catimaNotInstalled.setVisible(!catimaInstalled); catimaNotInstalledPreference.setVisible(!catimaInstalled);
} }
final Preference installCatima = findPreference(LOYALTY_CARDS_INSTALL_CATIMA); final Preference installCatimaPreference = findPreference(LOYALTY_CARDS_INSTALL_CATIMA);
if (installCatima != null) { if (installCatimaPreference != null) {
installCatima.setVisible(!catimaInstalled); installCatimaPreference.setVisible(!catimaInstalled);
installCatima.setOnPreferenceClickListener(preference -> { installCatimaPreference.setOnPreferenceClickListener(preference -> {
installCatima(); installCatima();
return true; return true;
}); });
} }
final boolean permissionGranted = ContextCompat.checkSelfPermission(requireContext(), CatimaContentProvider.PERMISSION_READ_CARDS) == PackageManager.PERMISSION_GRANTED; final boolean permissionGranted = ContextCompat.checkSelfPermission(requireContext(), catima.getReadPermission()) == PackageManager.PERMISSION_GRANTED;
final Preference catimaPermissions = findPreference(LOYALTY_CARDS_CATIMA_PERMISSIONS); final Preference catimaPermissionsPreference = findPreference(LOYALTY_CARDS_CATIMA_PERMISSIONS);
if (catimaPermissions != null) { if (catimaPermissionsPreference != null) {
catimaPermissions.setVisible(catimaInstalled && !permissionGranted); catimaPermissionsPreference.setVisible(catimaInstalled && !permissionGranted);
catimaPermissions.setOnPreferenceClickListener(preference -> { catimaPermissionsPreference.setOnPreferenceClickListener(preference -> {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
requireActivity(), requireActivity(),
new String[]{CatimaContentProvider.PERMISSION_READ_CARDS}, new String[]{catima.getReadPermission()},
LoyaltyCardsSettingsActivity.PERMISSION_REQUEST_CODE LoyaltyCardsSettingsActivity.PERMISSION_REQUEST_CODE
); );
return true; return true;
}); });
} }
final boolean catimaCompatible = catima != null && catima.isCatimaCompatible(); final boolean catimaCompatible = catima.isCatimaCompatible();
final Preference catimaNotCompatible = findPreference(LOYALTY_CARDS_CATIMA_NOT_COMPATIBLE); final Preference catimaNotCompatiblePreference = findPreference(LOYALTY_CARDS_CATIMA_NOT_COMPATIBLE);
if (catimaNotCompatible != null) { if (catimaNotCompatiblePreference != null) {
catimaNotCompatible.setVisible(catimaInstalled && permissionGranted && !catimaCompatible); catimaNotCompatiblePreference.setVisible(catimaInstalled && permissionGranted && !catimaCompatible);
} }
final Preference sync = findPreference(LOYALTY_CARDS_SYNC); final Preference syncPreference = findPreference(LOYALTY_CARDS_SYNC);
if (sync != null) { if (syncPreference != null) {
sync.setEnabled(catimaInstalled); syncPreference.setEnabled(catimaInstalled && catimaCompatible && permissionGranted);
sync.setOnPreferenceClickListener(preference -> { syncPreference.setOnPreferenceClickListener(preference -> {
catimaManager.sync(device); catimaManager.sync(device);
return true; return true;
}); });

View File

@ -41,13 +41,12 @@ public class CatimaContentProvider {
add("me.hackerchick.catima.debug"); add("me.hackerchick.catima.debug");
}}; }};
public static final String PERMISSION_READ_CARDS = "me.hackerchick.catima.READ_CARDS";
private final Context mContext; private final Context mContext;
private final Uri versionUri; private final Uri versionUri;
private final Uri cardsUri; private final Uri cardsUri;
private final Uri groupsUri; private final Uri groupsUri;
private final Uri cardGroupsUri; private final Uri cardGroupsUri;
private final String readPermission;
public CatimaContentProvider(final Context context, final String catimaPackageName) { public CatimaContentProvider(final Context context, final String catimaPackageName) {
this.mContext = context; this.mContext = context;
@ -56,6 +55,11 @@ public class CatimaContentProvider {
this.cardsUri = Uri.parse(String.format(Locale.ROOT, "content://%s/cards", catimaAuthority)); this.cardsUri = Uri.parse(String.format(Locale.ROOT, "content://%s/cards", catimaAuthority));
this.groupsUri = Uri.parse(String.format(Locale.ROOT, "content://%s/groups", catimaAuthority)); this.groupsUri = Uri.parse(String.format(Locale.ROOT, "content://%s/groups", catimaAuthority));
this.cardGroupsUri = Uri.parse(String.format(Locale.ROOT, "content://%s/card_groups", catimaAuthority)); this.cardGroupsUri = Uri.parse(String.format(Locale.ROOT, "content://%s/card_groups", catimaAuthority));
this.readPermission = String.format(Locale.ROOT, "%s.READ_CARDS", catimaPackageName);
}
public String getReadPermission() {
return this.readPermission;
} }
public boolean isCatimaCompatible() { public boolean isCatimaCompatible() {