<commit_before>package io.branch.referral;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.UUID;
import java.util.jar.JarFile;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest;
import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class SystemObserver {
	public static final String BLANK = "bnc_no_value";

	private Context context_;
	private boolean isRealHardwareId;
	
	public SystemObserver(Context context) {
		context_ = context;
		isRealHardwareId = true;
	}
	
	public String getUniqueID(boolean debug) {
		if (context_ != null) { 
			String androidID = null;
			if (!debug) {
				androidID = Secure.getString(context_.getContentResolver(), Secure.ANDROID_ID);
			}
			if (androidID == null) {
				androidID = UUID.randomUUID().toString();
				isRealHardwareId = false;
			}
			return androidID;
		} else 
			return BLANK;
	}
	
	public boolean hasRealHardwareId() {
		return isRealHardwareId;
	}
	
	public String getURIScheme() {
	    return getURIScheme(context_.getPackageName());
	}
	
	public String getURIScheme(String packageName) {
		String scheme = BLANK;
		PackageManager pm = context_.getPackageManager();
		try {
	        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
	        String sourceApk = ai.publicSourceDir;
	        JarFile jf = null;
	        InputStream is = null;
	        byte[] xml = null;
	        try {
	            jf = new JarFile(sourceApk);
            	is = jf.getInputStream(jf.getEntry("AndroidManifest.xml"));
	            xml = new byte[is.available()];
                //noinspection ResultOfMethodCallIgnored
                is.read(xml);
	            scheme = new ApkParser().decompressXML(xml);
	        } catch (Exception ignored) {
	        } finally {
	        	xml = null;
	        	try {
	        		if (is != null) {
	        			is.close();
	        			is = null;
	        		}
	        		if (jf != null) {
	        			jf.close();	
	        			jf = null;
	        		}
	        	} catch (IOException ignored) {}
	        }
	    } catch (NameNotFoundException ignored) {
	    }
		return scheme;
	}
	
	@SuppressLint("NewApi")
	public JSONArray getListOfApps() {
		JSONArray arr = new JSONArray();
		PackageManager pm = context_.getPackageManager();
		
		List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
		if (packages != null) {
			for (ApplicationInfo appInfo : packages) {
				if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) {
					JSONObject packObj = new JSONObject();
					try {
						String label = appInfo.loadLabel(pm).toString();
						if (label != null)
							packObj.put("name", label);
						String packName = appInfo.packageName;
						if (packName != null) {
							packObj.put("app_identifier", packName);
							String uriScheme = getURIScheme(packName);
							if (!uriScheme.equals(SystemObserver.BLANK))<commit_msg>Check memory on retrieving URI Scheme<commit_after>package io.branch.referral;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.List;
import java.util.UUID;
import java.util.jar.JarFile;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class SystemObserver {
	public static final String BLANK = "bnc_no_value";

	private Context context_;
	private boolean isRealHardwareId;
	
	public SystemObserver(Context context) {
		context_ = context;
		isRealHardwareId = true;
	}
	
	public String getUniqueID(boolean debug) {
		if (context_ != null) { 
			String androidID = null;
			if (!debug) {
				androidID = Secure.getString(context_.getContentResolver(), Secure.ANDROID_ID);
			}
			if (androidID == null) {
				androidID = UUID.randomUUID().toString();
				isRealHardwareId = false;
			}
			return androidID;
		} else 
			return BLANK;
	}
	
	public boolean hasRealHardwareId() {
		return isRealHardwareId;
	}
	
	public String getURIScheme() {
	    return getURIScheme(context_.getPackageName());
	}
	
	public String getURIScheme(String packageName) {
		String scheme = BLANK;
		if (!isLowOnMemory()) {
			PackageManager pm = context_.getPackageManager();
			try {
		        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
		        String sourceApk = ai.publicSourceDir;
		        JarFile jf = null;
		        InputStream is = null;
		        byte[] xml = null;
		        try {
		            jf = new JarFile(sourceApk);
	            	is = jf.getInputStream(jf.getEntry("AndroidManifest.xml"));
		            xml = new byte[is.available()];
	                //noinspection ResultOfMethodCallIgnored
	                is.read(xml);
		            scheme = new ApkParser().decompressXML(xml);
		        } catch (Exception ignored) {
		        } finally {
		        	xml = null;
		        	try {
		        		if (is != null) {
		        			is.close();
		        			is = null;
		        		}
		        		if (jf != null) {
		        			jf.close();	
		        			jf = null;
		        		}
		        	} catch (IOException ignored) {}
		        }
		    } catch (NameNotFoundException ignored) {
		    }
		}
		return scheme;
	}
	
	private boolean isLowOnMemory() {
		ActivityManager activityManager = (ActivityManager) context_.getSystemService(Context.ACTIVITY_SERVICE);
		MemoryInfo mi = new MemoryInfo();
		activityManager.getMemoryInfo(mi);
		return mi.lowMemory;
	}
	
	@SuppressLint("NewApi")
	public JSONArray getListOfApps() {
		JSONArray arr = new JSONArray();
		PackageManager pm = context_.getPackageManager();
		
		List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
		if (packages != null) {
			for (ApplicationInfo appInfo : packages) {
				if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) {
					JSONObject packObj = new JSONObject();
					try {
						String label = appInfo.loadLabel(pm).toString();
						if (label != null)
							packObj.put("name", label);
						String packName = appInfo.packageName;
						if (packName != null) {
							packObj.put("app_identifier", packName);
							String uriScheme = getURIScheme(packName);
							if (!uriScheme.equals(SystemObserver.BLANK))
{'fraction_non_alphanumeric': 0.10299272040981397, 'fraction_numerical': 0.0005392289026691831, 'mean_word_length': 3.096631695196024, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5039662', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2173, 'n_words': 482}