应用程序管理器

TaskManager

 

MainActivity

package org.wp.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.AdapterView.OnItemLongClickListener;

public class MainActivity extends Activity {
	private static final int INIT_WHAT = 1;
	private static final int NOTIFY_GRID_ADAPTER_WHAT = 2;
	private static final int NOTIFY_LIST_ADAPTER_WHAT = 3;

	// 滚动条Dialog
	private ProgressDialog pd;
	// 切换标题
	private TextView titleTextView;
	// 视图切换
	private ImageButton viewImageButton;
	private boolean isListView = true;
	// 网格视图
	private GridView gridViewShow;
	// 列表视图
	private ListView listViewShow;
	// 网格视图Adapter
	private GridViewAdapter gridViewAdapter;
	// 列表视图Adapter
	private ListViewAdapter listViewAdapter;
	// 底部主菜单
	private GridView navigationMenu;
	// 切换菜单
	private GridView switchMenu;
	// 切换详细菜单
	private View switchView;
	// 切换详细菜单Dialog
	private AlertDialog switchDialog;
	// 运行菜单
	private View runView;
	// 运行菜单Dialog
	private AlertDialog runDialog;
	// 运行菜单Gallery
	private Gallery gallery;
	// 运行菜单ImageSwitcher
	private ImageSwitcher imageSwitcher;
	// 应用图片所选position
	private int appDrawablePosition;
	// 查询LinearLayout
	private LinearLayout searchLinearLayout;
	// 查询EditText
	private EditText searchEditText;
	// 删除应用position集合
	private List<Integer> uninstallPositions;
	// 是否全部取消卸载
	private boolean cancelFlag = false;
	// 卸载监听器
	private UninstallReceiver uninstallReceiver;
	// 详细菜单
	private AlertDialog detailAlertDialog;
	// 详细Item位置
	private int detailPosition = -1;

	// 当前正在使用的应用程序列表数据
	private List<PackageInfo> currentPackageInfos;
	// 所有应用程序数据
	private List<PackageInfo> systemPackageInfos;
	// 用户安装应用程序数据
	private List<PackageInfo> consumPackageInfos;
	// 正在运行应用程序数据
	private List<PackageInfo> runningPackageInfos;
	// 当前正在使用的应用程序图标数据
	private List<Drawable> currentAppDrawables;
	// 所有应用程序图标数据
	private List<Drawable> systemAppDrawables;
	// 用户安装程序图标数据
	private List<Drawable> consumAppDrawables;
	// 正在运行应用图标数据
	private List<Drawable> runningAppDrawables;

	private int[] navigation_item_images = { R.drawable.n_menu_switch,
			R.drawable.n_menu_run, R.drawable.n_menu_search,
			R.drawable.n_menu_delete, R.drawable.n_menu_exit };
	private String[] navigation_item_texts = { "切换", "运行", "查询", "卸载", "退出" };
	private int[] switch_menu_images = { R.drawable.s_menu_running,
			R.drawable.s_menu_consum, R.drawable.s_menu_system };
	private String[] switch_menu_texts = { "正在运行", "用户安装", "全部应用" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 去掉Title
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		// 滚动条Dialog(按取消键不能返回)
		pd = ProgressDialog.show(this, "请稍等...", "正在搜索您所安装的应用程序...", true,
				false);
		// 初始化数据
		Thread thread = new Thread(new LoadData());
		thread.start();

		titleTextView = (TextView) this.findViewById(R.id.titleText);
		gridViewShow = (GridView) this.findViewById(R.id.gridViewShow);
		listViewShow = (ListView) this.findViewById(R.id.listViewShow);

		// 标记删除的应用
		gridViewShow.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (uninstallPositions == null) {
					uninstallPositions = new ArrayList<Integer>();
				}
				LinearLayout ll = (LinearLayout) view;
				ImageView iv = (ImageView) ll.getChildAt(2);
				if (iv.getVisibility() == View.VISIBLE) {
					iv.setVisibility(View.GONE);
					uninstallPositions.remove(new Integer(position));
				} else {
					iv.setVisibility(View.VISIBLE);
					uninstallPositions.add(new Integer(position));
				}
				return false;
			}
		});

		// 标记删除的应用
		listViewShow.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				if (uninstallPositions == null) {
					uninstallPositions = new ArrayList<Integer>();
				}
				RelativeLayout rl = (RelativeLayout) view;
				ImageView iv = (ImageView) rl.getChildAt(2);
				if (iv.getVisibility() == View.VISIBLE) {
					iv.setVisibility(View.GONE);
					uninstallPositions.remove(new Integer(position));
				} else {
					iv.setVisibility(View.VISIBLE);
					uninstallPositions.add(new Integer(position));
				}
				return false;
			}
		});

		// 记录选中position
		gridViewShow.setOnItemClickListener(new GridView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				detailPosition = position;
			}
		});
		listViewShow.setOnItemClickListener(new GridView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				detailPosition = position;
			}
		});

		// 切换视图
		viewImageButton = (ImageButton) this.findViewById(R.id.viewImageButton);
		viewImageButton.setOnClickListener(new ImageButton.OnClickListener() {
			public void onClick(View v) {
				if (isListView) {
					uiHandler.sendEmptyMessage(NOTIFY_LIST_ADAPTER_WHAT);
					Toast.makeText(MainActivity.this, R.string.list_view,
							Toast.LENGTH_SHORT).show();
					viewImageButton.setImageResource(R.drawable.b_grid_view);
					gridViewShow.setVisibility(View.GONE);
					listViewShow.setVisibility(View.VISIBLE);
					isListView = false;
				} else {
					uiHandler.sendEmptyMessage(NOTIFY_GRID_ADAPTER_WHAT);
					Toast.makeText(MainActivity.this, R.string.grid_view,
							Toast.LENGTH_SHORT).show();
					viewImageButton.setImageResource(R.drawable.b_list_view);
					listViewShow.setVisibility(View.GONE);
					gridViewShow.setVisibility(View.VISIBLE);
					isListView = true;
				}
			}
		});

		searchLinearLayout = (LinearLayout) this
				.findViewById(R.id.searchLinearLayout);
		searchEditText = (EditText) this.findViewById(R.id.searchEditText);

		// 注册监听卸载广播
		uninstallReceiver = new UninstallReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
		filter.addDataScheme("package");
		MainActivity.this.registerReceiver(uninstallReceiver, filter);
	}

	// 加载数据
	class LoadData implements Runnable {
		public void run() {
			// systemPackageInfos =
			// getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES
			// | PackageManager.GET_ACTIVITIES);
			// ==================================================================================
			// ActivityInfo activityInfo =
			// currentPackageInfos.get(0).activities[0];
			// ==================================================================================
			// String activityName = activityInfo.name;

			// 所有的应用程序
			systemPackageInfos = getPackageManager().getInstalledPackages(
					PackageManager.GET_UNINSTALLED_PACKAGES);

			if (systemPackageInfos != null) {
				systemAppDrawables = new ArrayList<Drawable>();
				consumPackageInfos = new ArrayList<PackageInfo>();
				consumAppDrawables = new ArrayList<Drawable>();
				// 载入所有的应用程序图标、查找用户安装的软件、载入用户安装的软件图标
				for (int i = 0; i < systemPackageInfos.size(); i++) {
					PackageInfo packageInfo = systemPackageInfos.get(i);
					ApplicationInfo applicationInfo = packageInfo.applicationInfo;
					systemAppDrawables.add(applicationInfo
							.loadIcon(getPackageManager()));
					boolean flag = false;
					if ((applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
						// Updated system app
						flag = true;
					} else if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
						// Non-system app
						flag = true;
					}
					if (flag) {
						consumPackageInfos.add(packageInfo);
						consumAppDrawables.add(applicationInfo
								.loadIcon(getPackageManager()));
					}
				}
			}

			// 查找正在运行的软件、载入正在运行的软件图标
			runningPackageInfos = new ArrayList<PackageInfo>();
			runningAppDrawables = new ArrayList<Drawable>();
			List<ActivityManager.RunningAppProcessInfo> procList = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE))
					.getRunningAppProcesses();
			for (ActivityManager.RunningAppProcessInfo appProcInfo : procList) {
				if (getPackageManager().getLaunchIntentForPackage(
						appProcInfo.processName) != null) {
					try {
						PackageInfo packageInfo = getPackageManager()
								.getPackageInfo(appProcInfo.processName,
										PackageManager.GET_META_DATA);
						if (packageInfo != null) {
							runningPackageInfos.add(packageInfo);
							runningAppDrawables.add(packageInfo.applicationInfo
									.loadIcon(getPackageManager()));
						}
					} catch (NameNotFoundException e) {
						e.printStackTrace();
					}
				}
			}

			// 更新UI
			uiHandler.sendEmptyMessage(INIT_WHAT);
		}
	}

	// 更新UI
	private Handler uiHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			// 初始化数据
			if (msg.what == INIT_WHAT) {
				currentPackageInfos = systemPackageInfos;
				currentAppDrawables = systemAppDrawables;
				gridViewAdapter = new GridViewAdapter(MainActivity.this,
						currentPackageInfos);
				listViewAdapter = new ListViewAdapter(MainActivity.this,
						currentPackageInfos);
				gridViewShow.setAdapter(gridViewAdapter);
				listViewShow.setAdapter(listViewAdapter);
				pd.dismiss();
			} else if (msg.what == NOTIFY_GRID_ADAPTER_WHAT) {
				// 刷新gridview
				if (gridViewAdapter != null) {
					gridViewAdapter.notifyDataSetChanged();
				}
			} else if (msg.what == NOTIFY_LIST_ADAPTER_WHAT) {
				// 刷新listview
				if (listViewAdapter != null) {
					listViewAdapter.notifyDataSetChanged();
				}
			}
		}
	};

	// 网格视图数据Adapter
	class GridViewAdapter extends BaseAdapter {
		private LayoutInflater layoutInflater;
		private List<PackageInfo> packageInfos;

		public GridViewAdapter(Context context, List<PackageInfo> packageInfos) {
			layoutInflater = LayoutInflater.from(context);
			this.packageInfos = packageInfos;
		}

		public int getCount() {
			return packageInfos.size();
		}

		public Object getItem(int position) {
			return packageInfos.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = layoutInflater.inflate(R.layout.gridview_item, null);
			ImageView iv = (ImageView) view.findViewById(R.id.gvs_item_icon);
			TextView tv = (TextView) view.findViewById(R.id.gvs_item_appname);
			ImageView cmiv = (ImageView) view
					.findViewById(R.id.gvs_item_checkmark);
			iv.setImageDrawable(packageInfos.get(position).applicationInfo
					.loadIcon(getPackageManager()));
			tv.setText(packageInfos.get(position).applicationInfo
					.loadLabel(getPackageManager()));
			if (uninstallPositions == null) {
				cmiv.setVisibility(View.GONE);
			} else {
				for (Integer i : uninstallPositions) {
					if (position == i) {
						cmiv.setVisibility(View.VISIBLE);
					}
				}
			}
			return view;
		}
	}

	// 列表视图数据Adapter
	class ListViewAdapter extends BaseAdapter {
		private LayoutInflater layoutInflater;
		private List<PackageInfo> packageInfos;

		public ListViewAdapter(Context context, List<PackageInfo> packageInfos) {
			layoutInflater = LayoutInflater.from(context);
			this.packageInfos = packageInfos;
		}

		public int getCount() {
			return packageInfos.size();
		}

		public Object getItem(int position) {
			return packageInfos.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View view = layoutInflater.inflate(R.layout.listview_item, null);
			ImageView iv = (ImageView) view.findViewById(R.id.lvs_item_icon);
			TextView tv_appname = (TextView) view
					.findViewById(R.id.lvs_item_appname);
			TextView tv_packagename = (TextView) view
					.findViewById(R.id.lvs_item_packagename);
			ImageView cmiv = (ImageView) view
					.findViewById(R.id.lvs_item_checkmark);
			iv.setImageDrawable(packageInfos.get(position).applicationInfo
					.loadIcon(getPackageManager()));
			tv_appname.setText(packageInfos.get(position).applicationInfo
					.loadLabel(getPackageManager()));
			tv_packagename.setText(packageInfos.get(position).packageName);
			if (uninstallPositions == null) {
				cmiv.setVisibility(View.GONE);
			} else {
				for (Integer i : uninstallPositions) {
					if (position == i) {
						cmiv.setVisibility(View.VISIBLE);
					}
				}
			}
			return view;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 当用户点击Menu按钮
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (navigationMenu == null) {
				initNavigationMenu();
				initSwitchMenu();
				initRunMenu();
				initSearchApplications();
			}
			if (navigationMenu.getVisibility() == View.GONE) {
				navigationMenu.setVisibility(View.VISIBLE);
			} else {
				if (searchLinearLayout != null
						&& searchLinearLayout.getVisibility() == View.VISIBLE) {
					searchEditText.setText("");
					searchLinearLayout.setVisibility(View.GONE);
				}
				navigationMenu.setVisibility(View.GONE);
			}
		}

		if (keyCode == KeyEvent.KEYCODE_Q && detailPosition != -1) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("选项");
			builder.setItems(R.array.operates,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							switch (which) {
							case 0:
								List<ResolveInfo> resolveInfoList = searchClassName(detailPosition);
								if (resolveInfoList != null
										&& resolveInfoList.size() != 0) {
									Intent intent = new Intent(
											Intent.ACTION_MAIN);
									intent
											.addCategory(Intent.CATEGORY_LAUNCHER);
									ComponentName cn = new ComponentName(
											resolveInfoList.get(0).activityInfo.packageName,
											resolveInfoList.get(0).activityInfo.name);
									intent.setComponent(cn);
									startActivity(intent);
								} else {
									Toast.makeText(MainActivity.this,
											"该应用无法运行", Toast.LENGTH_SHORT)
											.show();
								}
								break;
							case 1:
								showApplicationDetail(detailPosition);
								break;
							case 2:
								uninstallPositions = null;
								detailPosition = -1;
								uninstallApplication(currentPackageInfos
										.get(detailPosition).packageName);
								break;
							case 3:
								detailPosition = -1;
								break;
							}
						}
					});
			detailAlertDialog = builder.create();
			detailAlertDialog.show();
		}
		return super.onKeyDown(keyCode, event);
	}

	// 显示详细信息
	private void showApplicationDetail(int position) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("详细信息");
		builder.setIcon(currentAppDrawables.get(position));
		StringBuilder sb = new StringBuilder();
		sb.append("程序名称:"
				+ currentPackageInfos.get(position).applicationInfo
						.loadLabel(getPackageManager()) + "\n");
		sb.append("包名:" + currentPackageInfos.get(position).packageName + "\n");
		sb.append("版本名:" + currentPackageInfos.get(position).versionName);
		sb.append("\n版本号:" + currentPackageInfos.get(position).versionCode);
		builder.setMessage(sb.toString());
		builder.setPositiveButton("确定", null);
		builder.create().show();
	}

	// 初始化底部菜单
	private void initNavigationMenu() {
		navigationMenu = (GridView) this.findViewById(R.id.navigation_menu);
		navigationMenu.setAdapter(getMenuAdapter(navigation_item_images,
				navigation_item_texts));
		navigationMenu
				.setOnItemClickListener(new GridView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						switch (position) {
						case 0:
							if (searchLinearLayout != null
									&& searchLinearLayout.getVisibility() == View.VISIBLE) {
								searchEditText.setText("");
								searchLinearLayout.setVisibility(View.GONE);
							}
							switchDialog.show();
							break;
						case 1:
							if (searchLinearLayout != null
									&& searchLinearLayout.getVisibility() == View.VISIBLE) {
								searchEditText.setText("");
								searchLinearLayout.setVisibility(View.GONE);
							}
							runDialog.show();
							break;
						case 2:
							if (searchLinearLayout != null
									&& searchLinearLayout.getVisibility() == View.VISIBLE) {
								searchLinearLayout.setVisibility(View.GONE);
							} else {
								searchEditText.setText("");
								searchLinearLayout.setVisibility(View.VISIBLE);
							}
							break;
						case 3:
							if (searchLinearLayout != null
									&& searchLinearLayout.getVisibility() == View.VISIBLE) {
								searchEditText.setText("");
								searchLinearLayout.setVisibility(View.GONE);
							} else if (uninstallPositions == null
									|| uninstallPositions.size() == 0) {
								Toast.makeText(MainActivity.this,
										"您尚未标记任何应用,请长点击应用图标标记要删除的应用",
										Toast.LENGTH_LONG).show();
							} else {
								List<Integer> tempUninstallPositions = uninstallPositions;
								uninstallPositions = null;
								cancelFlag = true;
								for (int i = 0; i < tempUninstallPositions
										.size(); i++) {
									uninstallApplication(currentPackageInfos
											.get(tempUninstallPositions.get(i)).packageName);
								}
								navigationMenu.setVisibility(View.GONE);
							}
							break;
						case 4:
							finish();
							break;
						}
					}
				});
	}

	// 菜单数据Adapter
	private SimpleAdapter getMenuAdapter(int[] menuItemImages,
			String[] menuItemTexts) {
		ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < menuItemImages.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("menu_item_image", menuItemImages[i]);
			map.put("menu_item_text", menuItemTexts[i]);
			data.add(map);
		}
		SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
				data, R.layout.menu_item, new String[] { "menu_item_image",
						"menu_item_text" }, new int[] { R.id.menu_item_image,
						R.id.menu_item_text });
		return simpleAdapter;
	}

	// 初始化切换菜单Dialog
	private void initSwitchMenu() {
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		switchView = layoutInflater.inflate(R.layout.switch_menu, null);
		switchDialog = new AlertDialog.Builder(this).setView(switchView)
				.create();
		switchMenu = (GridView) switchView.findViewById(R.id.switch_menu);
		switchMenu.setAdapter(getMenuAdapter(switch_menu_images,
				switch_menu_texts));
		switchMenu.setOnItemClickListener(new GridView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					titleTextView.setText(R.string.app_running);
					uninstallPositions = null;
					updateCurrentPackageInfosAndAppDrawables(
							runningPackageInfos, runningAppDrawables);
					switchDialog.dismiss();
					navigationMenu.setVisibility(View.GONE);
					break;
				case 1:
					titleTextView.setText(R.string.app_consum);
					uninstallPositions = null;
					updateCurrentPackageInfosAndAppDrawables(
							consumPackageInfos, consumAppDrawables);
					switchDialog.dismiss();
					navigationMenu.setVisibility(View.GONE);
					break;
				case 2:
					titleTextView.setText(R.string.app_system);
					uninstallPositions = null;
					updateCurrentPackageInfosAndAppDrawables(
							systemPackageInfos, systemAppDrawables);
					switchDialog.dismiss();
					navigationMenu.setVisibility(View.GONE);
					break;
				}
			}
		});
	}

	// 更新当前应用程序数据和应用图标数据
	private void updateCurrentPackageInfosAndAppDrawables(
			List<PackageInfo> packageInfos, List<Drawable> appDrawables) {
		currentPackageInfos = packageInfos;
		currentAppDrawables = appDrawables;
		gridViewAdapter = new GridViewAdapter(MainActivity.this,
				currentPackageInfos);
		listViewAdapter = new ListViewAdapter(MainActivity.this,
				currentPackageInfos);
		gridViewShow.setAdapter(gridViewAdapter);
		listViewShow.setAdapter(listViewAdapter);
		imageSwitcher.setImageDrawable(null);
		gallery.setAdapter(new ImageAdapter(this, currentAppDrawables));
		gallery.setSelection(currentAppDrawables.size() / 2);
	}

	// 初始化运行菜单Dialog
	private void initRunMenu() {
		LayoutInflater layoutInflater = LayoutInflater.from(this);
		runView = layoutInflater.inflate(R.layout.gallery_image_switcher, null);
		runDialog = new AlertDialog.Builder(this).setView(runView).setTitle(
				"请选择要运行的应用程序").setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						if (appDrawablePosition == 0) {
							Toast.makeText(MainActivity.this, "您未选择要运行的程序",
									Toast.LENGTH_SHORT).show();
						} else {
							List<ResolveInfo> resolveInfoList = searchClassName(appDrawablePosition);
							if (resolveInfoList != null
									&& resolveInfoList.size() != 0) {
								Intent intent = new Intent(Intent.ACTION_MAIN);
								intent.addCategory(Intent.CATEGORY_LAUNCHER);
								ComponentName cn = new ComponentName(
										resolveInfoList.get(0).activityInfo.packageName,
										resolveInfoList.get(0).activityInfo.name);
								intent.setComponent(cn);
								startActivity(intent);
								navigationMenu.setVisibility(View.GONE);
							} else {
								Toast.makeText(MainActivity.this, "该应用无法运行",
										Toast.LENGTH_SHORT).show();
							}
						}
					}
				}).setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						runDialog.dismiss();
						navigationMenu.setVisibility(View.GONE);
					}
				}).create();
		imageSwitcher = (ImageSwitcher) runView
				.findViewById(R.id.imageSwitcherId);
		imageSwitcher.setFactory(new ImageViewFactory(this));
		// 动画效果
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
		gallery = (Gallery) runView.findViewById(R.id.galleryId);
		gallery.setAdapter(new ImageAdapter(this, currentAppDrawables));
		// 默认选中位置
		gallery.setSelection(currentAppDrawables.size() / 2);
		gallery.setOnItemClickListener(new Gallery.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				appDrawablePosition = position;
				imageSwitcher.setImageDrawable(currentAppDrawables
						.get(position));
			}
		});
	}

	// Gallery数据
	class ImageAdapter extends BaseAdapter {
		private Context context;
		private List<Drawable> appDrawables;

		public ImageAdapter(Context context, List<Drawable> appDrawables) {
			this.context = context;
			this.appDrawables = appDrawables;
		}

		public int getCount() {
			return appDrawables.size();
		}

		public Object getItem(int position) {
			return appDrawables.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView iv = new ImageView(context);
			iv.setImageDrawable(appDrawables.get(position));
			// 是否保持宽高比
			iv.setAdjustViewBounds(true);
			iv.setLayoutParams(new Gallery.LayoutParams(80, 80));
			iv.setPadding(15, 10, 15, 10);
			return iv;
		}
	}

	// ImageSwitcher工厂
	class ImageViewFactory implements ViewSwitcher.ViewFactory {
		private Context context;

		public ImageViewFactory(Context context) {
			this.context = context;
		}

		public View makeView() {
			ImageView iv = new ImageView(context);
			iv.setLayoutParams(new ImageSwitcher.LayoutParams(90, 90));
			return iv;
		}
	}

	// 查找应用的启动Activity名称
	private List<ResolveInfo> searchClassName(int position) {
		Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
		resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		resolveIntent.setPackage(currentPackageInfos.get(position).packageName);
		List<ResolveInfo> resolveInfoList = getPackageManager()
				.queryIntentActivities(resolveIntent, 0);
		return resolveInfoList;
	}

	// 查询应用程序
	private void initSearchApplications() {
		searchEditText.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String condition = s.toString();
				if (condition.equals("")) {
					gridViewShow.setAdapter(new GridViewAdapter(
							MainActivity.this, currentPackageInfos));
					listViewShow.setAdapter(new ListViewAdapter(
							MainActivity.this, currentPackageInfos));
				}
				List<PackageInfo> tempPackageInfos = new ArrayList<PackageInfo>();
				String str = null;
				for (int i = 0; i < currentPackageInfos.size(); i++) {
					str = (String) currentPackageInfos.get(i).applicationInfo
							.loadLabel(getPackageManager());
					if (str.toLowerCase().contains(condition)) {
						tempPackageInfos.add(currentPackageInfos.get(i));
					}
				}
				gridViewShow.setAdapter(new GridViewAdapter(MainActivity.this,
						tempPackageInfos));
				listViewShow.setAdapter(new ListViewAdapter(MainActivity.this,
						tempPackageInfos));
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void afterTextChanged(Editable s) {
			}
		});
	}

	// 卸载程序
	private void uninstallApplication(String packageName) {
		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_DELETE);
		intent.setData(Uri.parse("package:" + packageName));
		startActivity(intent);
	}

	// 监听程序是否卸载
	class UninstallReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			cancelFlag = false;
			String packageName = intent.getDataString();
			uninstallData(currentPackageInfos, currentAppDrawables, packageName);
			uninstallData(systemPackageInfos, systemAppDrawables, packageName);
			uninstallData(consumPackageInfos, consumAppDrawables, packageName);
			uninstallData(runningPackageInfos, runningAppDrawables, packageName);
			updateCurrentPackageInfosAndAppDrawables(currentPackageInfos,
					currentAppDrawables);
		}
	}

	// 同步数据
	private void uninstallData(List<PackageInfo> packageInfos,
			List<Drawable> appDrawables, String packageName) {
		for (int i = 0; i < packageInfos.size(); i++) {
			if (packageName.equals("package:"
					+ packageInfos.get(i).applicationInfo.packageName)) {
				packageInfos.remove(i);
				appDrawables.remove(i);
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		// 如果全部取消卸载,刷新数据
		if (cancelFlag) {
			cancelFlag = false;
			updateCurrentPackageInfosAndAppDrawables(currentPackageInfos,
					currentAppDrawables);
		}
	}

	// 退出软件(回收资源)
	@Override
	protected void onDestroy() {
		if (pd != null) {
			pd = null;
		}
		if (titleTextView != null) {
			titleTextView = null;
		}
		if (viewImageButton != null) {
			viewImageButton = null;
		}
		if (gridViewShow != null) {
			gridViewShow = null;
		}
		if (listViewShow != null) {
			listViewShow = null;
		}
		if (gridViewAdapter != null) {
			gridViewAdapter = null;
		}
		if (listViewAdapter != null) {
			listViewAdapter = null;
		}
		if (navigationMenu != null) {
			navigationMenu = null;
		}
		if (switchMenu != null) {
			switchMenu = null;
		}
		if (switchView != null) {
			switchView = null;
		}
		if (switchDialog != null) {
			switchDialog = null;
		}
		if (runView != null) {
			runView = null;
		}
		if (runDialog != null) {
			runDialog = null;
		}
		if (gallery != null) {
			gallery = null;
		}
		if (imageSwitcher != null) {
			imageSwitcher = null;
		}
		if (searchLinearLayout != null) {
			searchLinearLayout = null;
		}
		if (searchEditText != null) {
			searchEditText = null;
		}
		if (uninstallPositions != null) {
			uninstallPositions = null;
		}
		if (uninstallReceiver != null) {
			uninstallReceiver = null;
		}
		if (detailAlertDialog != null) {
			detailAlertDialog = null;
		}
		if (currentPackageInfos != null) {
			currentPackageInfos = null;
		}
		if (systemPackageInfos != null) {
			systemPackageInfos = null;
		}
		if (consumPackageInfos != null) {
			consumPackageInfos = null;
		}
		if (runningPackageInfos != null) {
			runningPackageInfos = null;
		}
		if (currentAppDrawables != null) {
			currentAppDrawables = null;
		}
		if (systemAppDrawables != null) {
			systemAppDrawables = null;
		}
		if (consumAppDrawables != null) {
			consumAppDrawables = null;
		}
		if (runningAppDrawables != null) {
			runningAppDrawables = null;
		}
		if (navigation_item_images != null) {
			navigation_item_images = null;
		}
		if (navigation_item_texts != null) {
			navigation_item_texts = null;
		}
		if (switch_menu_images != null) {
			switch_menu_images = null;
		}
		if (switch_menu_texts != null) {
			switch_menu_texts = null;
		}
		// 注销卸载监听器
		MainActivity.this.unregisterReceiver(uninstallReceiver);
		super.onDestroy();
	}
}

 

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
    >
    <!-- 
    	标题栏
     -->
    <RelativeLayout
    	android:layout_width="fill_parent"
    	android:layout_height="25px"
    	android:gravity="center_vertical"
    	android:paddingLeft="5px"
    	android:paddingRight="5px"
    	android:paddingTop="3px"
    	android:background="@drawable/title_bg"
    	>
    	<ImageView
    		android:id="@+id/titleImage"
    		android:layout_width="18px"
    		android:layout_height="18px"
    		android:src="@drawable/app_icon"
    		/>
    	<TextView
    		android:id="@+id/titleText"
    		android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:textColor="#000"
    		android:textSize="15px"
    		android:text="@string/app_system"
    		android:layout_marginLeft="6px"
    		android:layout_toRightOf="@id/titleImage"
    		/>
    	<ImageButton
    		android:id="@+id/viewImageButton"
    		android:layout_width="25px"
    		android:layout_height="25px"
    		android:layout_alignParentRight="true"
    		android:src="@drawable/b_list_view"
    		/>	
    </RelativeLayout>
    
    <!-- 
    	网格方式显示
     -->
    <GridView
    	android:id="@+id/gridViewShow"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	android:listSelector="@drawable/choose_gridview"
    	android:horizontalSpacing="10px"
    	android:verticalSpacing="10px"
    	android:numColumns="3"
   		android:layout_marginTop="28px"
   		android:layout_marginBottom="12px"
   		android:layout_marginLeft="5px"
   		android:layout_marginRight="5px"
    	/>
    
    <!-- 
    	列表方式显示
     -->
 	<ListView
 		android:id="@+id/listViewShow"
 		android:layout_width="fill_parent"
 		android:layout_height="fill_parent"
 		android:listSelector="@drawable/choose_listview"
 		android:layout_marginTop="28px"
   		android:layout_marginBottom="12px"
   		android:layout_marginLeft="5px"
   		android:layout_marginRight="5px"
   		android:visibility="gone"
 		/> 	
    
    <!-- 
    	底部菜单
    	默认不可见
     -->
	<GridView
		android:id="@+id/navigation_menu"
		android:layout_width="fill_parent"
		android:layout_height="65px"
		android:background="@drawable/navigation_bg"
		android:layout_alignParentBottom="true"
		android:numColumns="5"
		android:gravity="center"
		android:horizontalSpacing="10px"
		android:verticalSpacing="10px"
		android:visibility="gone"
		/>
	
	<!-- 
		查询框
	 -->
	<LinearLayout
		android:id="@+id/searchLinearLayout"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:layout_centerVertical="true"
		android:layout_alignParentLeft="true"
		android:layout_above="@id/navigation_menu"
		android:visibility="gone"
		>
		<EditText
			android:id="@+id/searchEditText"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:hint="@string/search_hint"
			android:textSize="18px"		
			/>
	</LinearLayout>	
</RelativeLayout>

 

menu_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	>
  	<ImageView
  		android:id="@+id/menu_item_image"
  		android:layout_width="wrap_content"
  		android:layout_height="wrap_content"
  		android:layout_centerHorizontal="true"
  		/>
  	<TextView
  		android:id="@+id/menu_item_text"
  		android:layout_width="wrap_content"
  		android:layout_height="wrap_content"
  		android:layout_centerHorizontal="true"
  		android:layout_below="@id/menu_item_image"
  		/>	
</RelativeLayout>

 

gridview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  	android:layout_width="90px"
  	android:layout_height="wrap_content"
  	android:orientation="vertical"
  	android:gravity="center"
  	>
  	<ImageView 
  		android:id="@+id/gvs_item_icon"
  		android:layout_width="64px"
  		android:layout_height="64px"
  		/>
  	<TextView
  		android:id="@+id/gvs_item_appname"
  		android:layout_width="wrap_content"
  		android:layout_height="wrap_content"
  		android:lines="2"
  		android:textSize="16px"
  		android:textColor="#fff"
  		android:gravity="center"
  		/>	
  	<ImageView
  		android:id="@+id/gvs_item_checkmark"
  		android:layout_width="18px"
  		android:layout_height="18px"
  		android:src="@drawable/checkmark"
  		android:visibility="gone"
  		/>
</LinearLayout>

 

listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  	android:layout_width="fill_parent"
  	android:layout_height="wrap_content"
  	android:gravity="center_vertical"
  	>
  	<ImageView
  		android:id="@+id/lvs_item_icon"
  		android:layout_width="48px"
  		android:layout_height="48px"
  		android:layout_marginTop="5px"
  		android:layout_marginBottom="5px"
  		/>
  	<LinearLayout
  		android:layout_width="wrap_content"
  		android:layout_height="48px"
  		android:orientation="vertical"
  		android:paddingLeft="5px"
  		android:layout_toRightOf="@id/lvs_item_icon"
  		>
  		<TextView
  			android:id="@+id/lvs_item_appname"
  			android:layout_width="wrap_content"
  			android:layout_height="wrap_content"
  			android:singleLine="true"
  			android:textSize="16px"
  			android:textStyle="bold"
  			android:textColor="#fff"
  			/>
  		<TextView
  			android:id="@+id/lvs_item_packagename"
  			android:layout_width="wrap_content"
  			android:layout_height="wrap_content"
  			android:singleLine="true"
  			android:textColor="#fff"
  			/>
  	</LinearLayout>
  	<ImageView
  		android:id="@+id/lvs_item_checkmark"
  		android:layout_width="25px"
  		android:layout_height="25px"
  		android:layout_alignParentRight="true"
  		android:layout_marginTop="20px"
  		android:layout_marginRight="5px"
  		android:src="@drawable/checkmark"
  		android:visibility="gone"
  		/>
</RelativeLayout>

 

switch_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	>
  	<!-- 
  		android:stretchMode="columnWidth"
  		根据列宽自动缩放
  	 -->
  	<GridView
  		android:id="@+id/switch_menu"
  		android:layout_width="fill_parent"
  		android:layout_height="fill_parent"
  		android:numColumns="3"
  		android:horizontalSpacing="10px"
		android:verticalSpacing="10px"
		android:stretchMode="columnWidth"
		android:gravity="center"
  		/>
</LinearLayout>

 

gallery_image_switcher.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  	android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	>
  	<Gallery
  		android:id="@+id/galleryId"
  		android:layout_width="fill_parent"
  		android:layout_height="110px"
  		android:layout_marginTop="10px"
  		android:layout_alignParentLeft="true"
  		/>
  	<ImageSwitcher
  		android:id="@+id/imageSwitcherId"
  		android:layout_width="90px"
  		android:layout_height="90px"
  		android:layout_alignParentTop="true"
  		android:layout_centerHorizontal="true"
  		android:layout_marginTop="10px"
  		android:layout_alignBottom="@id/galleryId"
  		/>	
</RelativeLayout>

 

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="app_name">应用程序管理器</string>
	<string name="app_running">正在运行的应用程序</string>
	<string name="app_consum">用户安装的应用程序</string>
	<string name="app_system">系统全部的应用程序</string>
	<string name="grid_view">网格视图</string>
	<string name="list_view">列表视图</string>
	<string name="search_hint">请输入您要查找的应用名称</string>
	<string-array name="operates">
		<item>运行程序</item>
		<item>详细信息</item>
		<item>卸载程序</item>
		<item>返回</item>
	</string-array>
</resources>

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.wp.activity" android:versionCode="1" android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".MainActivity" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
	</application>
	<uses-sdk android:minSdkVersion="9" />
</manifest>

  

 

 

你可能感兴趣的:(thread,UI,android,xml,OS)