package com.pilot51.voicenotify;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class AppList extends ListActivity {
	private Common common;
	private SimpleAdapter adapter;
	private ArrayList<HashMap<String, String>> appArray = new ArrayList<HashMap<String, String>>();
	private ProgressDialog progress;
	private String TAG;
	private ArrayList<String> ignoredApps;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		common = new Common(this);
		TAG = common.TAG;
		ignoredApps = common.readList();
		progress = new ProgressDialog(this);
		progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progress.setCancelable(true);
		progress.setTitle("Apps List");
		progress.show();
		new Thread(new Runnable() {
			public void run() {
				PackageManager packMan = getPackageManager();
				//List<PackageInfo> packList = packMan.getInstalledPackages(0);
				List<ApplicationInfo> appList = packMan.getInstalledApplications(0);
				int listSize = appList.size();
				progress.setMessage("Loading...");
				progress.setMax(listSize);
				ApplicationInfo appInfo = new ApplicationInfo();
				HashMap<String, String> app;
				for (int i = 0; i < listSize; i++) {
					appInfo = appList.get(i);
					app = new HashMap<String, String>();
					String pkg = appInfo.packageName;
					app.put("package", pkg);
					String label = String.valueOf(appInfo.loadLabel(packMan));
					app.put("label", label);
					appArray = insertApp(app, appArray);
					//Log.d(TAG, "Label: " + label + " | Package: " + pkg);
					progress.setProgress(i);
				}
				runOnUiThread(new Runnable() {
					public void run() {
						final ListView lv = getListView();
						//lv.setTextFilterEnabled(true);
						adapter = new SimpleAdapter(AppList.this, appArray, R.layout.app_list_item,
								new String[] {"label", "package"},
								new int[] {R.id.text1, R.id.text2});
						lv.setAdapter(adapter);
						/*
						for (int i = 0; i < lv.getChildCount(); i++) {
							View view = lv.getChildAt(i);
							CheckBox checkbox = (CheckBox)view.findViewById(R.id.checkbox);
							if (ignoredApps.contains(((TextView)view.findViewById(R.id.text2)).getText())) checkbox.setChecked(true);
							else checkbox.setChecked(false);
						}
						*/
						lv.setOnItemClickListener(new OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
								//((TextView)parent.findViewById(R.id.text1)).setBackgroundColor(Color.RED);
								Log.d(TAG, "id: " + id);
								Log.d(TAG, "position: " + position);
								//CheckBox checkbox = (CheckBox)view.findViewById(R.id.checkbox);
								String
									pkg = appArray.get(position).get("package"),
									label = appArray.get(position).get("label");
								if (ignoredApps.contains(pkg)) {
									ignoredApps.remove(pkg);
									//checkbox.setChecked(false);
									Toast.makeText(AppList.this, label + " is not ignored", Toast.LENGTH_SHORT).show();
								} else {
									ignoredApps.add(pkg);
									//checkbox.setChecked(true);
									Toast.makeText(AppList.this, label + " is ignored", Toast.LENGTH_SHORT).show();
								}
								saveList(ignoredApps);
							}
						});
						progress.dismiss();
					}});
			}
		}).start();
	}

	private ArrayList<HashMap<String, String>> insertApp(HashMap<String, String> app, ArrayList<HashMap<String, String>> array) {
		progress.setMessage("Sorting app into list...");
		try {
			int n = array.size();
			String appLabel = app.get("label");
			long i;
			do {
				n--;
				i = appLabel.compareToIgnoreCase(array.get(n).get("label"));
				//Log.d(TAG, "i = " + i + " - " + event.get("name") + ": " + new SimpleDateFormat("yyyy-MMMM-dd HH:mm:ss zzz").format(((Calendar)event.get(cal)).getTime()) + " - " + mapArray.get(n).get("name") + ": " + new SimpleDateFormat("yyyy-MMMM-dd HH:mm:ss zzz").format(((Calendar)mapArray.get(n).get(cal)).getTime()));
			} while (i < 0 & n > 0);
			//String pkg = app.get("package");
			/*if (pkg.equals(array.get(n).get("package"))) {
				return array;
			}
			*/
			if (n + 1 < array.size()) {
				/*if (pkg.equals(array.get(n+1).get("package"))) {
					return array;
				}
				*/
				if (n > 0 | (n == 0 & i >= 0)) {
					//Log.d(TAG, (String)event.get("name") + " is after " + ((String)((HashMap<String, Object>)mapArray.get(n)).get("name")));
					n++;
					//Log.d(TAG, (String)event.get("name") + " is before " + ((String)((HashMap<String, Object>)mapArray.get(n)).get("name")));
				} else if (n == 0 & i < 0) {
					//Log.d(TAG, (String)event.get("name") + " is at the beginning, before " + ((String)((HashMap<String, Object>)mapArray.get(n)).get("name")));
				}
				array.add(n, app);
			} else {
				//Log.d(TAG, (String)event.get("name") + " is at the end, after " + ((String)((HashMap<String, Object>)mapArray.get(n-1)).get("name")));
				array.add(app);
			}
		} catch (IndexOutOfBoundsException e) {
			//Log.i(TAG, "Sort exception, most likely first item in list.");
			e.printStackTrace();
			array.add(app);
		}
		return array;
	}
	
	private void saveList(ArrayList<String> list) {
		try {
			ObjectOutputStream out = new ObjectOutputStream(openFileOutput("ignored_apps", Context.MODE_WORLD_READABLE));
			out.writeObject(list);
			out.flush();
			out.close();
		} catch (IOException e) {
			Log.e(TAG, "Error: Failed to save ignored_apps");
			e.printStackTrace();
		}
	}
}

@@ -1264,16 +1264,53 @@
 List%22);%0A
+%09%09progress.setMessage(%22Loading...%22);%0A
 %09%09progre
@@ -1603,47 +1603,8 @@
 ();%0A
-%09%09%09%09progress.setMessage(%22Loading...%22);%0A
 %09%09%09%09
@@ -2123,16 +2123,18 @@
 ogress(i
++1
 );%0A%09%09%09%09%7D
@@ -3973,59 +3973,8 @@
 ) %7B%0A
-%09%09progress.setMessage(%22Sorting app into list...%22);%0A
 %09%09tr

Fix progress message not displaying.