package com.pkmmte.appscout;

import android.content.Context;
import android.support.annotation.NonNull;

import com.pkmmte.logger.Logger;

public class AppScout {
	// For logging purposes
	private static final String TAG = AppScout.class.getSimpleName();

	// Special logger
	protected static final Logger logger = new Logger(TAG, Logger.Level.NONE);

	// Main singleton instance
	private static volatile AppScout singleton = null;

	// Application context... because of reasons
	private final Context context;

	/**
	 * The global default {@link AppScout} instance.
	 * Call this whenever you want to use this library.
	 */
	public static AppScout with(@NonNull Context context) {
		if(singleton == null)
			singleton = new AppScout(context.getApplicationContext());

		return singleton;
	}

	/* Hidden constructor */
	private AppScout(Context context) {
		this.context = context;
	}

	public RequestCreator auto() {
		return new RequestCreator(this, Api.AUTO);
	}

	public RequestCreator load() {
		return new RequestCreator(this, Api.LOAD);
	}

	public RequestCreator send() {
		return new RequestCreator(this, Api.SEND);
	}

	protected void auto(Request request) {
		logger.medium("auto(" + request + ')');
		//final CallbackHandler handler = request.handler != null ? request.handler : this.handler;
		//final boolean safe = request.safe != null ? request.safe : this.safe;

		// TODO
	}

	protected void load(Request request) {
		logger.medium("load(" + request + ')');
		//final CallbackHandler handler = request.handler != null ? request.handler : this.handler;
		//final boolean safe = request.safe != null ? request.safe : this.safe;

		// TODO
	}

	protected void send(Request request) {
		logger.medium("send(" + request + ')');
		//final CallbackHandler handler = request.handler != null ? request.handler : this.handler;
		//final boolean safe = request.safe != null ? request.safe : this.safe;

		// TODO
	}

	protected enum Api { AUTO, LOAD, SEND }
}

@@ -74,68 +74,266 @@
 oid.
-support.annotation.NonNull;%0A%0Aimport com.pkmmte.logger.Logger
+content.Intent;%0Aimport android.content.pm.ApplicationInfo;%0Aimport android.content.pm.PackageManager;%0Aimport android.support.annotation.NonNull;%0A%0Aimport com.pkmmte.logger.Logger;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List
 ;%0A%0Ap
@@ -540,12 +540,11 @@
 vel.
-NONE
+LOW
 );%0A%0A
@@ -703,16 +703,106 @@
 ntext;%0A%0A
+%09// Cache for loaded apps%0A%09private volatile List%3CApp%3E installedApps = new ArrayList%3C%3E();%0A%0A
 %09/**%0A%09 *
@@ -1667,32 +1667,41 @@
 %0A%0A%09%09// TODO%0A%09%7D%0A%0A
+%09// TODO%0A
 %09protected void 
@@ -1936,32 +1936,1445 @@
 safe;%0A%0A%09%09// TODO
+: Notify callback%0A%0A%09%09// Get list of all installed applications and sort them by name%0A%09%09final PackageManager packageManager = context.getPackageManager();%0A%09%09final List%3CApplicationInfo%3E packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);%0A%09%09Collections.sort(packages, new ApplicationInfo.DisplayNameComparator(packageManager));%0A%0A%09%09// Create new local list for installed apps%0A%09%09final int numPackages = packages.size();%0A%09%09final List%3CApp%3E installed = new ArrayList%3C%3E(numPackages);%0A%09%09logger.medium(%22Found %22 + numPackages + %22 installed packages%22);%0A%0A%09%09for (int index = 0; index %3C numPackages; index++) %7B%0A%09%09%09// Get basic application info%0A%09%09%09final ApplicationInfo packageInfo = packages.get(index);%0A%09%09%09final Intent launchIntent = packageManager.getLaunchIntentForPackage(packageInfo.packageName);%0A%0A%09%09%09// Skip invalid intents%0A%09%09%09if (launchIntent == null)%0A%09%09%09%09continue;%0A%0A%09%09%09// TODO: Notify callback%0A%0A%09%09%09// Build custom immutable App object%0A%09%09%09App app = new App.Builder()%0A%09%09%09%09%09.name(packageInfo.loadLabel(packageManager))%0A%09%09%09%09%09.component(Utils.getComponentString(launchIntent))%0A%09%09%09%09%09.icon(packageInfo.loadIcon(packageManager))%0A%09%09%09%09%09.build();%0A%0A%09%09%09// Append to list%0A%09%09%09installed.add(app);%0A%09%09%09logger.low(app.toString());%0A%09%09%7D%0A%0A%09%09// Replace global installed list with local list%0A%09%09installedApps = installed;%0A%09%09logger.medium(%22Loaded %22 + installed.size() + %22 installed apps%22);%0A%0A%09%09// TODO: Notify callback
 %0A%09%7D%0A%0A%09protected 

Load installed apps