package org.hangee.system.packagemanager;

import org.qtproject.qt5.android.bindings.QtApplication;
import org.qtproject.qt5.android.bindings.QtActivity;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import android.util.Log;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;

public class PackageManager extends QtActivity
{
	private static HashMap<String, String> categoryMap;
    private static PackageManager m_instance;

    public PackageManager()
    {
		m_instance = this;

		categoryMap = new HashMap();
		categoryMap.put("LAUNCHER", Intent.CATEGORY_LAUNCHER);
	}

	public static void startApp(String pkg, String cls)
	{
		ComponentName component = new ComponentName(pkg, cls);

		// Create new intent and start activity
		Intent appIntent = new Intent();
		appIntent.setComponent(component);
		m_instance.startActivity(appIntent);
	}

	public static ArrayList getApps(String[] categories)
	{
		HashMap<String, Object> app;
        ArrayList<HashMap<String, Object>> apps = new ArrayList();
        String category;

		final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);

        // Set category filter
		for (int i = 0; i < categories.length; i++) {
            category = categoryMap.get(categories[i]);
            mainIntent.addCategory(category);
        }

        // Getting application list
		final List<ResolveInfo> pkgAppList = m_instance.getPackageManager().queryIntentActivities(mainIntent, 0);
		for (int i = 0; i < pkgAppList.size(); i++) {
			ResolveInfo info = pkgAppList.get(i);
			app = new HashMap();
			app.put("appName", info.loadLabel(m_instance.getPackageManager()).toString());
			app.put("packageName", info.activityInfo.packageName);
			app.put("activityName", info.activityInfo.name);

			// TODO: support icon

            apps.add(app);
        }

		return apps;
    }
}

@@ -518,20 +518,17 @@
 oryMap;%0A
-    
+%09
 private 
@@ -562,20 +562,17 @@
 tance;%0A%0A
-    
+%09
 public P
@@ -587,20 +587,17 @@
 nager()%0A
-    
+%09
 %7B%0A%09%09m_in
@@ -1063,24 +1063,18 @@
 t%3E app;%0A
-        
+%09%09
 ArrayLis
@@ -1124,24 +1124,18 @@
 List();%0A
-        
+%09%09
 String c
@@ -1211,24 +1211,18 @@
 null);%0A%0A
-        
+%09%09
 // Set c
@@ -1284,28 +1284,19 @@
  i++) %7B%0A
-            
+%09%09%09
 category
@@ -1330,28 +1330,19 @@
 es%5Bi%5D);%0A
-            
+%09%09%09
 mainInte
@@ -1371,27 +1371,15 @@
 y);%0A
-        %7D%0A%0A        
+%09%09%7D%0A%0A%09%09
 // G
@@ -1846,20 +1846,11 @@
 on%0A%0A
-            
+%09%09%09
 apps
@@ -1864,16 +1864,10 @@
 p);%0A
-        
+%09%09
 %7D%0A%0A%09
@@ -1884,12 +1884,9 @@
 ps;%0A
-    
+%09
 %7D%0A%7D%0A

replace space with tab character