// Path: app/src/main/java/com/pddstudio/otgsubs/beans/ManifestProcessorBean.java
// @EBean(scope = EBean.Scope.Singleton)
// public class ManifestProcessorBean {
// 
// 	private static final String TAG = ManifestProcessorBean.class.getSimpleName();
// 
// 	private static final String OTG_SUBS_META_DATA_FLAG = "OTGSubs_Supported";
// 
// 	@RootContext
// 	protected Context context;
// 
// 	@Bean
// 	ApkHelper apkHelper;
// 
// 	public List<ApkInfo> getSupportedThemes() {
// 		return StreamSupport.stream(apkHelper.getInstalledApks()).map(apkInfo -> {
// 			try {
// 				ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(apkInfo.getPackageName(), PackageManager.GET_META_DATA);
// 				if(applicationInfo == null) {
// 					return null;
// 				}
// 
// 				Bundle bundle = applicationInfo.metaData;
// 				if(bundle == null) {
// 					return null;
// 				}
// 
// 				boolean supported = bundle.getBoolean(OTG_SUBS_META_DATA_FLAG, false);
// 				if(supported) {
// 					Log.d(TAG, "Found OTGSubs supported apk: " + apkInfo.getPackageName());
// 					return apkInfo;
// 				} else {
// 					return null;
// 				}
// 			} catch (PackageManager.NameNotFoundException e) {
// 				//ignore
// 				return null;
// 			}
// 		}).filter(Objects::nonNull).collect(Collectors.toList());
// 	}
// 
// 	public boolean isPackageSupported(String packageName) {
// 		if(packageName == null || packageName.isEmpty()) {
// 			return false;
// 		}
// 		return StreamSupport.stream(getSupportedThemes()).anyMatch(apkInfo -> apkInfo.getPackageName().equals(packageName));
// 	}
// 
// 	public ApkInfo getApkInfoForPackage(String packageName) {
// 		if(!isPackageSupported(packageName)) {
// 			return null;
// 		}
// 		return StreamSupport.stream(getSupportedThemes()).filter(apkInfo -> apkInfo.getPackageName().equals(packageName)).findAny().orElse(null);
// 	}
// 
// 	public Context getRootContext() {
// 		return context;
// 	}
// 
// }
// 
// Path: substratum-packager/src/main/java/com/pddstudio/substratum/packager/models/ApkInfo.java
// public class ApkInfo implements Serializable {
// 
// 	private String appName;
// 	private String apk;
// 	private String version;
// 	private String source;
// 	private String data;
// 
// 	public ApkInfo(String appName, String apk, String version, String source, String data) {
// 		this.appName = appName;
// 		this.apk = apk;
// 		this.version = version;
// 		this.source = source;
// 		this.data = data;
// 	}
// 
// 	public String getAppName() {
// 		return appName;
// 	}
// 
// 	public void setAppName(String appName) {
// 		this.appName = appName;
// 	}
// 
// 	public String getPackageName() {
// 		return apk;
// 	}
// 
// 	public void setApk(String apk) {
// 		this.apk = apk;
// 	}
// 
// 	public String getVersion() {
// 		return version;
// 	}
// 
// 	public void setVersion(String version) {
// 		this.version = version;
// 	}
// 
// 	public String getSource() {
// 		return source;
// 	}
// 
// 	public void setSource(String source) {
// 		this.source = source;
// 	}
// 
// 	public String getData() {
// 		return data;
// 	}
// 
// 	public void setData(String data) {
// 		this.data = data;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		Gson gson = new GsonBuilder().setPrettyPrinting().create();
// 		return gson.toJson(this);
// 	}
// }


import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.pddstudio.otgsubs.PatcherActivity_;
import com.pddstudio.otgsubs.R;
import com.pddstudio.otgsubs.beans.ManifestProcessorBean;
import com.pddstudio.substratum.packager.models.ApkInfo;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EReceiver;
import org.androidannotations.annotations.ReceiverAction;
import org.androidannotations.annotations.SystemService;
import org.androidannotations.api.support.content.AbstractBroadcastReceiver;
package com.pddstudio.otgsubs.receivers;




/**
 * Created by pddstudio on 11/05/2017.
 */

@EReceiver
public class PackageManagerReceiver extends AbstractBroadcastReceiver {

	private static final String TAG             = PackageManagerReceiver.class.getSimpleName();
	private static final int    NOTIFICATION_ID = 42;

	@SystemService
	protected NotificationManager notificationManager;

	@Bean
	protected ManifestProcessorBean manifestProcessorBean;

	@ReceiverAction(actions = Intent.ACTION_PACKAGE_ADDED, dataSchemes = "package")
	protected void onPackageAdded(Intent intent) {
		Log.d(TAG, "onPackageAdded() : " + intent);
		validatePackageCompatibility(intent);
	}

	@ReceiverAction(actions = Intent.ACTION_PACKAGE_REPLACED, dataSchemes = "package")
	protected void onPackageUpdated(Intent intent) {
		Log.d(TAG, "onPackageUpdated() : " + intent);
		validatePackageCompatibility(intent);
	}

	private void validatePackageCompatibility(Intent intent) {
		int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);

		if (uid == -1) {
			return;
		}

		PackageManager packageManager = manifestProcessorBean.getRootContext().getPackageManager();
		String packageName = packageManager.getNameForUid(uid);
		if (manifestProcessorBean.isPackageSupported(packageName)) {
// Path: app/src/main/java/com/pddstudio/otgsubs/beans/ManifestProcessorBean.java
// @EBean(scope = EBean.Scope.Singleton)
// public class ManifestProcessorBean {
// 
// 	private static final String TAG = ManifestProcessorBean.class.getSimpleName();
// 
// 	private static final String OTG_SUBS_META_DATA_FLAG = "OTGSubs_Supported";
// 
// 	@RootContext
// 	protected Context context;
// 
// 	@Bean
// 	ApkHelper apkHelper;
// 
// 	public List<ApkInfo> getSupportedThemes() {
// 		return StreamSupport.stream(apkHelper.getInstalledApks()).map(apkInfo -> {
// 			try {
// 				ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(apkInfo.getPackageName(), PackageManager.GET_META_DATA);
// 				if(applicationInfo == null) {
// 					return null;
// 				}
// 
// 				Bundle bundle = applicationInfo.metaData;
// 				if(bundle == null) {
// 					return null;
// 				}
// 
// 				boolean supported = bundle.getBoolean(OTG_SUBS_META_DATA_FLAG, false);
// 				if(supported) {
// 					Log.d(TAG, "Found OTGSubs supported apk: " + apkInfo.getPackageName());
// 					return apkInfo;
// 				} else {
// 					return null;
// 				}
// 			} catch (PackageManager.NameNotFoundException e) {
// 				//ignore
// 				return null;
// 			}
// 		}).filter(Objects::nonNull).collect(Collectors.toList());
// 	}
// 
// 	public boolean isPackageSupported(String packageName) {
// 		if(packageName == null || packageName.isEmpty()) {
// 			return false;
// 		}
// 		return StreamSupport.stream(getSupportedThemes()).anyMatch(apkInfo -> apkInfo.getPackageName().equals(packageName));
// 	}
// 
// 	public ApkInfo getApkInfoForPackage(String packageName) {
// 		if(!isPackageSupported(packageName)) {
// 			return null;
// 		}
// 		return StreamSupport.stream(getSupportedThemes()).filter(apkInfo -> apkInfo.getPackageName().equals(packageName)).findAny().orElse(null);
// 	}
// 
// 	public Context getRootContext() {
// 		return context;
// 	}
// 
// }
// 
// Path: substratum-packager/src/main/java/com/pddstudio/substratum/packager/models/ApkInfo.java
// public class ApkInfo implements Serializable {
// 
// 	private String appName;
// 	private String apk;
// 	private String version;
// 	private String source;
// 	private String data;
// 
// 	public ApkInfo(String appName, String apk, String version, String source, String data) {
// 		this.appName = appName;
// 		this.apk = apk;
// 		this.version = version;
// 		this.source = source;
// 		this.data = data;
// 	}
// 
// 	public String getAppName() {
// 		return appName;
// 	}
// 
// 	public void setAppName(String appName) {
// 		this.appName = appName;
// 	}
// 
// 	public String getPackageName() {
// 		return apk;
// 	}
// 
// 	public void setApk(String apk) {
// 		this.apk = apk;
// 	}
// 
// 	public String getVersion() {
// 		return version;
// 	}
// 
// 	public void setVersion(String version) {
// 		this.version = version;
// 	}
// 
// 	public String getSource() {
// 		return source;
// 	}
// 
// 	public void setSource(String source) {
// 		this.source = source;
// 	}
// 
// 	public String getData() {
// 		return data;
// 	}
// 
// 	public void setData(String data) {
// 		this.data = data;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		Gson gson = new GsonBuilder().setPrettyPrinting().create();
// 		return gson.toJson(this);
// 	}
// }


// Path: app/src/main/java/com/pddstudio/otgsubs/receivers/PackageManagerReceiver.java
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.pddstudio.otgsubs.PatcherActivity_;
import com.pddstudio.otgsubs.R;
import com.pddstudio.otgsubs.beans.ManifestProcessorBean;
import com.pddstudio.substratum.packager.models.ApkInfo;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EReceiver;
import org.androidannotations.annotations.ReceiverAction;
import org.androidannotations.annotations.SystemService;
import org.androidannotations.api.support.content.AbstractBroadcastReceiver;

package com.pddstudio.otgsubs.receivers;




/**
 * Created by pddstudio on 11/05/2017.
 */

@EReceiver
public class PackageManagerReceiver extends AbstractBroadcastReceiver {

	private static final String TAG             = PackageManagerReceiver.class.getSimpleName();
	private static final int    NOTIFICATION_ID = 42;

	@SystemService
	protected NotificationManager notificationManager;

	@Bean
	protected ManifestProcessorBean manifestProcessorBean;

	@ReceiverAction(actions = Intent.ACTION_PACKAGE_ADDED, dataSchemes = "package")
	protected void onPackageAdded(Intent intent) {
		Log.d(TAG, "onPackageAdded() : " + intent);
		validatePackageCompatibility(intent);
	}

	@ReceiverAction(actions = Intent.ACTION_PACKAGE_REPLACED, dataSchemes = "package")
	protected void onPackageUpdated(Intent intent) {
		Log.d(TAG, "onPackageUpdated() : " + intent);
		validatePackageCompatibility(intent);
	}

	private void validatePackageCompatibility(Intent intent) {
		int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);

		if (uid == -1) {
			return;
		}

		PackageManager packageManager = manifestProcessorBean.getRootContext().getPackageManager();
		String packageName = packageManager.getNameForUid(uid);
		if (manifestProcessorBean.isPackageSupported(packageName)) {
			ApkInfo info = manifestProcessorBean.getApkInfoForPackage(packageName);