package com.cff.appExplorer.activity;
import java.util.ArrayList;
import java.util.List;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.AlteredCharSequence;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class AppExplorerActivity extends Activity implements Runnable,OnItemClickListener{
private static final int SEARCH_APP=0;
private static final int DELETE_APP=1;
GridView gv;
ListView lv;
private List<PackageInfo>packageInfos;
private List<PackageInfo>userPackageInfos;
private List<PackageInfo>showPackageInfos;
private ProgressDialog pd;
ImageButton ib_change_category;
ImageButton ib_change_view;
private boolean allApplication=true;
private boolean isListView=false;
private Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==SEARCH_APP){
showPackageInfos=packageInfos;
gv.setAdapter(new GridViewAdapter(AppExplorerActivity.this,showPackageInfos));
lv.setAdapter(new ListViewAdapter(AppExplorerActivity.this, showPackageInfos));
pd.dismiss();
setProgressBarIndeterminateVisibility(false);
}
if(msg.what==DELETE_APP){
System.out.println("Delete App Success!");
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
//1.获得这个Activity的Window对象
//Window win=getWindow();
//2.为这个Window对象设置flag
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏显示
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.show_app_grid);
setProgressBarIndeterminateVisibility(true);
gv=(GridView)this.findViewById(R.id.gv_apps);
lv=(ListView)this.findViewById(R.id.lv_apps);
gv.setOnItemClickListener(this);
lv.setCacheColorHint(0);
lv.setOnItemClickListener(this);
ib_change_category=(ImageButton)this.findViewById(R.id.ib_change_category);
ib_change_view=(ImageButton)this.findViewById(R.id.ib_change_view);
ib_change_view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(isListView){
//Toast.makeText(AppExplorerActivity.this, "网格显示", Toast.LENGTH_SHORT).show();
MyToast.myToastShow(AppExplorerActivity.this, R.drawable.grids, "网格显示", Toast.LENGTH_SHORT);
ib_change_view.setImageResource(R.drawable.grids);
lv.setVisibility(View.GONE);
gv.setVisibility(View.VISIBLE);
//AlphaAnimation 控制渐变的动画效果
//ScaleAnimation 控制尺寸伸缩的动画效果
//TranslatAnination控制画面皮衣的动画效果
//RotateAnimation控制画面角度的动画效果
AnimationSet set=new AnimationSet(false);
Animation animation=new RotateAnimation(60,0);
animation.setInterpolator(AppExplorerActivity.this,android.R.anim.accelerate_interpolator);//加速
animation.setDuration(500);
set.addAnimation(animation);
animation=new AlphaAnimation(0, 1);
animation.setDuration(500);
set.addAnimation(animation);
gv.startAnimation(set);
isListView=false;
}else {
//Toast.makeText(AppExplorerActivity.this, "列表显示", Toast.LENGTH_SHORT).show();
MyToast.myToastShow(AppExplorerActivity.this, R.drawable.list, "列表显示", Toast.LENGTH_SHORT);
ib_change_view.setImageResource(R.drawable.list);
gv.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
AnimationSet set=new AnimationSet(false);
Animation animation=new TranslateAnimation(200, 1, 200, 1);
animation.setDuration(500);
animation.setInterpolator(AppExplorerActivity.this,android.R.anim.bounce_interpolator);//反弹
set.addAnimation(animation);
animation=new ScaleAnimation(0, 1, 0, 1);
animation.setDuration(500);
set.addAnimation(animation);
lv.startAnimation(set);
isListView=true;
}
}
});
ib_change_category.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(allApplication){
ib_change_category.setImageResource(R.drawable.user);
//gv.setAdapter(new GridViewAdapter(AppExplorerActivity.this,userPackageInfos));
showPackageInfos=userPackageInfos;
allApplication=false;
//Toast.makeText(AppExplorerActivity.this, "用户安装的程序列表",Toast.LENGTH_SHORT).show();
MyToast.myToastShow(AppExplorerActivity.this, R.drawable.user, "用户安装的程序列表", Toast.LENGTH_SHORT);
}else {
ib_change_category.setImageResource(R.drawable.all);
//gv.setAdapter(new GridViewAdapter(AppExplorerActivity.this,packageInfos));
showPackageInfos=packageInfos;
allApplication=true;
//Toast.makeText(AppExplorerActivity.this, "所有的程序列表", Toast.LENGTH_SHORT).show();
MyToast.myToastShow(AppExplorerActivity.this, R.drawable.all, "所有的程序列表", Toast.LENGTH_SHORT);
}
gv.setAdapter(new GridViewAdapter(AppExplorerActivity.this, showPackageInfos));
lv.setAdapter(new GridViewAdapter(AppExplorerActivity.this, showPackageInfos));
}
});
pd=ProgressDialog.show(this,"请稍候···" , "正在搜索你所安装的程序···");
Thread t=new Thread(this);
t.start();
}
/*1.为GridView的每一项准备一个布局文件
* 2.写一个类继承BaseAdapter
* i. 解释:一个类继承了BaseAdapter,需要重写几个方法,分别是getCount()、
* getItem()、getItemId()、getView()
* ii. 最重要的是getCount和getView方法。GridView中的每一项都是一个View。
* 而这个View是通过getView获得的。当某一项需要显示的时候,
* 它就会调用adapter中的getView方法获得需要展示的view。所以,
* 这个GridView中有几项,就会调用几次getView方法,判断有几项,
* 是通过getCount方法获得。所以在getCount中我们应该返回一个list的大小。
* 在getView中,根据参数position获得list中位于position的某一个对象。
* 通过LayoutInflater将布局文件渲染成一个view,再给这个view中的控件赋值
*/
class GridViewAdapter extends BaseAdapter{
LayoutInflater inflater;
List<PackageInfo>pkInfos;
public GridViewAdapter(Context context,List<PackageInfo>packageInfos){
inflater=LayoutInflater.from( context);
this.pkInfos=packageInfos;
}
public int getCount() {
// TODO Auto-generated method stub
return pkInfos.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return pkInfos.get(arg0);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.gv_item, null);
TextView tv=(TextView)view.findViewById(R.id.gv_item_appname);
ImageView iv=(ImageView)view.findViewById(R.id.gv_item_icon);
//tv.setText(packageInfos.get(position).packageName);
tv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));
iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));
return view;
}
}
class ListViewAdapter extends BaseAdapter{
LayoutInflater inflater;
List<PackageInfo>pkInfos;
public ListViewAdapter(Context context,List<PackageInfo>packageInfos){
inflater=LayoutInflater.from( context);
this.pkInfos=packageInfos;
}
public int getCount() {
// TODO Auto-generated method stub
return pkInfos.size();
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return pkInfos.get(arg0);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View view =inflater.inflate(R.layout.lv_item, null);
TextView tv_appname=(TextView)view.findViewById(R.id.lv_item_appname);
TextView tv_packagename=(TextView)view.findViewById(R.id.lv_item_packagename);
ImageView iv=(ImageView)view.findViewById(R.id.lv_icon);
//tv.setText(packageInfos.get(position).packageName);
tv_appname.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));
tv_packagename.setText(pkInfos.get(position).packageName);
iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));
return view;
}
}
public void run() {
packageInfos=getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES|PackageManager.GET_ACTIVITIES);
userPackageInfos=new ArrayList<PackageInfo>();
for(int i=0;i<packageInfos.size();i++){
PackageInfo temp=packageInfos.get(i);
ApplicationInfo appInfo=temp.applicationInfo;
boolean flag=false;
if((appInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)!=0){
flag=true;
}else if ((appInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0) {
flag=true;
}
if(flag)
userPackageInfos.add(temp);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.sendEmptyMessage(SEARCH_APP);
try {
Thread.sleep(5000);
mHandler.sendEmptyMessage(DELETE_APP);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
final PackageInfo tempPkInfo=showPackageInfos.get(position);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("选项");
builder.setItems(R.array.choice, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
String packageName=tempPkInfo.packageName;
ActivityInfo activityInfo=tempPkInfo.activities[0];
if(activityInfo==null){
Toast.makeText(AppExplorerActivity.this, "没有任何activity", Toast.LENGTH_SHORT).show();
return;
}
String activityName=activityInfo.name;
Intent intent=new Intent();
intent.setComponent(new ComponentName(packageName,activityName));
startActivity(intent);
break;
case 1:
showAppDetail(tempPkInfo);
break;
case 2:
Uri packageUri=Uri.parse("package:"+tempPkInfo.packageName);
Intent deleteIntent=new Intent();
deleteIntent.setAction(Intent.ACTION_DELETE);
deleteIntent.setData(packageUri);
startActivityForResult(deleteIntent, 0);
break;
}
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
packageInfos=getPackageManager().getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES|PackageManager.GET_ACTIVITIES);
userPackageInfos=new ArrayList<PackageInfo>();
for(int i=0;i<packageInfos.size();i++){
PackageInfo temp=packageInfos.get(i);
ApplicationInfo appInfo=temp.applicationInfo;
boolean flag=false;
if((appInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)!=0){
flag=true;
}else if ((appInfo.flags&ApplicationInfo.FLAG_SYSTEM)==0) {
flag=true;
}
if(flag)
userPackageInfos.add(temp);
}
if(allApplication){
showPackageInfos=packageInfos;
}else {
showPackageInfos=userPackageInfos;
}
gv.setAdapter(new GridViewAdapter(AppExplorerActivity.this, showPackageInfos));
lv.setAdapter(new GridViewAdapter(AppExplorerActivity.this, showPackageInfos));
}
@Override
protected void onResume() {
super.onResume();
}
private void showAppDetail(PackageInfo packageInfo){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("详细信息");
StringBuffer message=new StringBuffer();
message.append("程序名称:"+packageInfo.applicationInfo.loadLabel(getPackageManager()));
message.append("\n包名:"+packageInfo.packageName);
message.append("\n版本号:"+packageInfo.versionCode);
message.append("\n版本名:"+packageInfo.versionName);
builder.setMessage(message.toString());
builder.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager()));
builder.setPositiveButton("确定", null);
builder.create().show();
}
}