1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 16:40:32 +00:00

Refactor CheckForNewAppVersion

This commit is contained in:
Stypox 2020-11-21 11:43:57 +01:00
parent 8dc4e6dc2a
commit 17866c29ae
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -58,48 +58,43 @@ public final class CheckForNewAppVersion {
* @param application The application * @param application The application
* @return String with the apk's SHA1 fingeprint in hexadecimal * @return String with the apk's SHA1 fingeprint in hexadecimal
*/ */
@NonNull
private static String getCertificateSHA1Fingerprint(@NonNull final Application application) { private static String getCertificateSHA1Fingerprint(@NonNull final Application application) {
final PackageManager pm = application.getPackageManager(); final PackageInfo packageInfo;
final String packageName = application.getPackageName();
final int flags = PackageManager.GET_SIGNATURES;
PackageInfo packageInfo = null;
try { try {
packageInfo = pm.getPackageInfo(packageName, flags); packageInfo = application.getPackageManager().getPackageInfo(
application.getPackageName(), PackageManager.GET_SIGNATURES);
} catch (final PackageManager.NameNotFoundException e) { } catch (final PackageManager.NameNotFoundException e) {
ErrorActivity.reportError(application, e, null, null, ErrorActivity.reportError(application, e, null, null,
ErrorInfo.make(UserAction.SOMETHING_ELSE, "none", ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
"Could not find package info", R.string.app_ui_crash)); "Could not find package info", R.string.app_ui_crash));
return "";
} }
final Signature[] signatures = packageInfo.signatures; final X509Certificate c;
final byte[] cert = signatures[0].toByteArray();
final InputStream input = new ByteArrayInputStream(cert);
X509Certificate c = null;
try { try {
final Signature[] signatures = packageInfo.signatures;
final byte[] cert = signatures[0].toByteArray();
final InputStream input = new ByteArrayInputStream(cert);
final CertificateFactory cf = CertificateFactory.getInstance("X509"); final CertificateFactory cf = CertificateFactory.getInstance("X509");
c = (X509Certificate) cf.generateCertificate(input); c = (X509Certificate) cf.generateCertificate(input);
} catch (final CertificateException e) { } catch (final CertificateException e) {
ErrorActivity.reportError(application, e, null, null, ErrorActivity.reportError(application, e, null, null,
ErrorInfo.make(UserAction.SOMETHING_ELSE, "none", ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
"Certificate error", R.string.app_ui_crash)); "Certificate error", R.string.app_ui_crash));
return "";
} }
String hexString = null;
try { try {
final MessageDigest md = MessageDigest.getInstance("SHA1"); final MessageDigest md = MessageDigest.getInstance("SHA1");
final byte[] publicKey = md.digest(c.getEncoded()); final byte[] publicKey = md.digest(c.getEncoded());
hexString = byte2HexFormatted(publicKey); return byte2HexFormatted(publicKey);
} catch (NoSuchAlgorithmException | CertificateEncodingException e) { } catch (NoSuchAlgorithmException | CertificateEncodingException e) {
ErrorActivity.reportError(application, e, null, null, ErrorActivity.reportError(application, e, null, null,
ErrorInfo.make(UserAction.SOMETHING_ELSE, "none", ErrorInfo.make(UserAction.SOMETHING_ELSE, "none",
"Could not retrieve SHA1 key", R.string.app_ui_crash)); "Could not retrieve SHA1 key", R.string.app_ui_crash));
return "";
} }
return hexString;
} }
private static String byte2HexFormatted(final byte[] arr) { private static String byte2HexFormatted(final byte[] arr) {
@ -164,10 +159,10 @@ public final class CheckForNewAppVersion {
} }
private static boolean isConnected(@NonNull final App app) { private static boolean isConnected(@NonNull final App app) {
final ConnectivityManager cm = ContextCompat.getSystemService(app, final ConnectivityManager connectivityManager =
ConnectivityManager.class); ContextCompat.getSystemService(app, ConnectivityManager.class);
return cm.getActiveNetworkInfo() != null return connectivityManager != null && connectivityManager.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isConnected(); && connectivityManager.getActiveNetworkInfo().isConnected();
} }
public static boolean isGithubApk(@NonNull final App app) { public static boolean isGithubApk(@NonNull final App app) {