Installer: Introduce enum class for the installerPackageName.

This commit is contained in:
Andreas Schildbach 2018-11-30 18:36:40 +01:00
parent cfabfa2ea8
commit af44f7b436
3 changed files with 83 additions and 9 deletions

View file

@ -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 {

View file

@ -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();
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}