/*
 * Copyright 2017-2018 Nikita Shakarun
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ru.playsoftware.j2meloader;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;

import org.acra.ACRA;
import org.acra.annotation.AcraCore;
import org.acra.annotation.AcraDialog;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import ru.playsoftware.j2meloader.hockeyapp.HockeySenderFactory;

@AcraCore(buildConfigClass = BuildConfig.class, reportSenderFactoryClasses = {HockeySenderFactory.class})
@AcraDialog(resTitle = R.string.crash_dialog_title, resText = R.string.crash_dialog_message,
		resPositiveButtonText = R.string.report_crash, resNegativeButtonText = R.string.CANCEL_CMD,
		resTheme = R.style.Theme_AppCompat_Dialog)
public class EmulatorApplication extends Application {
	private static final String[] VALID_SIGNATURES = {
			"78EF7758720A9902F731ED706F72C669C39B765C", // GPlay
			"289F84A32207DF89BE749481ED4BD07E15FC268F", // F-Droid
			"FA8AA497194847D5715BAA62C6344D75A936EBA6" // Private
	};

	@SuppressWarnings("ConstantConditions")
	@Override
	protected void attachBaseContext(Context base) {
		super.attachBaseContext(base);
		if (isSignatureValid() && !BuildConfig.FLAVOR.equals("dev")) ACRA.init(this);
	}

	@SuppressLint("PackageManagerGetSignatures")
	private boolean isSignatureValid() {
		try {
			PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
			for (Signature signature : info.signatures) {
				MessageDigest md = MessageDigest.getInstance("SHA-1");
				md.update(signature.toByteArray());
				String sha1 = bytesToHex(md.digest());
				if (!Arrays.asList(VALID_SIGNATURES).contains(sha1)) {
					return false;
				}
			}
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return true;
	}

	private String bytesToHex(byte[] bytes) {
		final char[] hexArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
				'9', 'A', 'B', 'C', 'D', 'E', 'F'};
		char[] hexChars = new char[bytes.length * 2];
		int v;
		for (int i = 0; i < bytes.length; i++) {
			v = bytes[i] & 0xFF;
			hexChars[i * 2] = hexArray[v >>> 4];
			hexChars[i * 2 + 1] = hexArray[v & 0x0F];
		}
		return new String(hexChars);
	}
}

@@ -856,16 +856,41 @@
 gnature;
+%0Aimport android.os.Build;
 %0A%0Aimport
@@ -2087,55 +2087,368 @@
 %7B%0A%09%09
-try %7B%0A%09%09%09PackageInfo info = getPackageManager()
+boolean result = true;%0A%09%09try %7B%0A%09%09%09Signature%5B%5D signatures;%0A%09%09%09if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.P) %7B%0A%09%09%09%09PackageInfo info = getPackageManager()%0A%09%09%09%09%09%09.getPackageInfo(getPackageName(), PackageManager.GET_SIGNING_CERTIFICATES);%0A%09%09%09%09signatures = info.signingInfo.getApkContentsSigners();%0A%09%09%09%7D else %7B%0A%09%09%09%09PackageInfo info = getPackageManager()%0A%09%09%09%09%09%09
 .get
@@ -2509,16 +2509,55 @@
 TURES);%0A
+%09%09%09%09signatures = info.signatures;%0A%09%09%09%7D%0A
 %09%09%09for (
@@ -2578,21 +2578,16 @@
 ature : 
-info.
 signatur
@@ -2800,20 +2800,22 @@
 %0A%09%09%09%09%09re
-turn
+sult =
  false;%0A
@@ -2902,16 +2902,35 @@
 race();%0A
+%09%09%09result = false;%0A
 %09%09%7D catc
@@ -2986,16 +2986,35 @@
 race();%0A
+%09%09%09result = false;%0A
 %09%09%7D%0A%09%09re
@@ -3018,20 +3018,22 @@
 %09return 
-true
+result
 ;%0A%09%7D%0A%0A%09p

Improve signature check