From af44f7b436f133a5c0bd4c03ccccebebd6e0e661 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Fri, 30 Nov 2018 18:36:40 +0100 Subject: [PATCH] Installer: Introduce enum class for the installerPackageName. --- .../schildbach/oeffi/OeffiMainActivity.java | 15 ++-- .../schildbach/oeffi/util/ErrorReporter.java | 7 +- .../de/schildbach/oeffi/util/Installer.java | 70 +++++++++++++++++++ 3 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 oeffi/src/de/schildbach/oeffi/util/Installer.java diff --git a/oeffi/src/de/schildbach/oeffi/OeffiMainActivity.java b/oeffi/src/de/schildbach/oeffi/OeffiMainActivity.java index 67910cd..5bc2765 100644 --- a/oeffi/src/de/schildbach/oeffi/OeffiMainActivity.java +++ b/oeffi/src/de/schildbach/oeffi/OeffiMainActivity.java @@ -32,8 +32,6 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import androidx.annotation.Nullable; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,6 +52,7 @@ import de.schildbach.oeffi.util.DialogBuilder; import de.schildbach.oeffi.util.DividerItemDecoration; import de.schildbach.oeffi.util.Downloader; import de.schildbach.oeffi.util.ErrorReporter; +import de.schildbach.oeffi.util.Installer; import de.schildbach.oeffi.util.NavigationMenuAdapter; import de.schildbach.oeffi.util.UiThreadExecutor; import de.schildbach.pte.NetworkId; @@ -80,6 +79,7 @@ import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.PopupMenu; +import androidx.annotation.Nullable; import androidx.drawerlayout.widget.DrawerLayout; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -393,8 +393,9 @@ public abstract class OeffiMainActivity extends OeffiActivity { remoteFileName.append('-').append(flavor); remoteFileName.append(".txt"); remoteUrl.addPathSegment(remoteFileName.toString()); - remoteUrl.addEncodedQueryParameter("installer", - Strings.nullToEmpty(getPackageManager().getInstallerPackageName(getPackageName()))); + final String installerPackageName = Installer.installerPackageName(this); + if (installerPackageName != null) + remoteUrl.addEncodedQueryParameter("installer", installerPackageName); remoteUrl.addQueryParameter("sdk", Integer.toString(Build.VERSION.SDK_INT)); remoteUrl.addQueryParameter("task", taskName()); final File localFile = new File(getFilesDir(), "messages.txt"); @@ -529,13 +530,11 @@ public abstract class OeffiMainActivity extends OeffiActivity { return false; } } else if (name.equals("installer")) { - final String installer = Strings - .nullToEmpty(getPackageManager().getInstallerPackageName(getPackageName())); + final String installer = Strings.nullToEmpty(Installer.installerPackageName(this)); if (!value.equalsIgnoreCase(installer)) return false; } else if (name.equals("not-installer")) { - final String installer = Strings - .nullToEmpty(getPackageManager().getInstallerPackageName(getPackageName())); + final String installer = Strings.nullToEmpty(Installer.installerPackageName(this)); if (value.equalsIgnoreCase(installer)) return false; } else { diff --git a/oeffi/src/de/schildbach/oeffi/util/ErrorReporter.java b/oeffi/src/de/schildbach/oeffi/util/ErrorReporter.java index 31e079f..37a33a0 100644 --- a/oeffi/src/de/schildbach/oeffi/util/ErrorReporter.java +++ b/oeffi/src/de/schildbach/oeffi/util/ErrorReporter.java @@ -113,7 +113,12 @@ public class ErrorReporter implements Thread.UncaughtExceptionHandler { report.append("Date: " + new Date() + "\n"); report.append("Version: " + pi.versionName + " (" + pi.versionCode + ")\n"); report.append("Package: " + pi.packageName + "\n"); - report.append("Installer: " + pm.getInstallerPackageName(pi.packageName) + "\n"); + final String installerPackageName = Installer.installerPackageName(context); + final Installer installer = Installer.from(installerPackageName); + if (installer != null) + report.append("Installer: " + installer.displayName + " (" + installerPackageName + ")\n"); + else + report.append("Installer: unknown\n"); } catch (final NameNotFoundException x) { x.printStackTrace(); } diff --git a/oeffi/src/de/schildbach/oeffi/util/Installer.java b/oeffi/src/de/schildbach/oeffi/util/Installer.java new file mode 100644 index 0000000..e0c8e79 --- /dev/null +++ b/oeffi/src/de/schildbach/oeffi/util/Installer.java @@ -0,0 +1,70 @@ +/* + * Copyright the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.schildbach.oeffi.util; + +import android.app.Application; +import android.content.Context; +import android.content.pm.PackageManager; +import okhttp3.HttpUrl; + +public enum Installer { + F_DROID("F-Droid"), GOOGLE_PLAY("Google Play"), AMAZON_APPSTORE("Amazon Appstore"); + + public final String displayName; + + private Installer(final String displayName) { + this.displayName = displayName; + } + + public static String installerPackageName(final Context context) { + final PackageManager pm = context.getPackageManager(); + return pm.getInstallerPackageName(context.getPackageName()); + } + + public static Installer from(final String installerPackageName) { + if ("org.fdroid.fdroid".equals(installerPackageName) + || "org.fdroid.fdroid.privileged".equals(installerPackageName)) + return F_DROID; + if ("com.android.vending".equals(installerPackageName)) + return GOOGLE_PLAY; + if ("com.amazon.venezia".equals(installerPackageName)) + return AMAZON_APPSTORE; + return null; + } + + public static Installer from(final Context context) { + return from(installerPackageName(context)); + } + + public HttpUrl appStorePageFor(final Application application) { + final HttpUrl.Builder url; + if (this == F_DROID) { + url = HttpUrl.parse("https://f-droid.org/de/packages/").newBuilder(); + url.addPathSegment(application.getPackageName()); + } else if (this == GOOGLE_PLAY) { + url = HttpUrl.parse("https://play.google.com/store/apps/details").newBuilder(); + url.addQueryParameter("id", application.getPackageName()); + } else if (this == AMAZON_APPSTORE) { + url = HttpUrl.parse("https://www.amazon.com/gp/mas/dl/android").newBuilder(); + url.addQueryParameter("p", application.getPackageName()); + } else { + throw new IllegalStateException(this.toString()); + } + return url.build(); + } +}