/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.cocos2d;
import
java.text.Collator;
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;
import
android.app.ListActivity;
import
android.content.Intent;
import
android.content.pm.PackageManager;
import
android.content.pm.ResolveInfo;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.ListView;
import
android.widget.SimpleAdapter;
public
class
Cocos2D
extends
ListActivity {
//这个类用了ListActivity
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
//继承 ListActivity 的 onCreate方法 //以下是设置list里的适配器
setListAdapter(
new
SimpleAdapter(
this
,
//新的简单适配器,运用了自己的上下文
(List<Map<String, ?>>)getData(
"org.cocos2d.tests"
),
//getData是个方法,getData(String)往下看有方法的声明
android.R.layout.simple_list_item_1,
new
String[]{
"title"
},
new
int
[]{android.R.id.text1}));
getListView().setTextFilterEnabled(
true
);
//启用过滤窗口
}
protected
List<?> getData(String prefix) {
//即上述方法
List<Map<String,?>> myData =
new
ArrayList<Map<String,?>>();
//一个用来盛放map的list,map立盛放String
Intent mainIntent =
new
Intent(Intent.ACTION_MAIN,
null
);
//意图指定跳到系统桌面
mainIntent.addCategory(Intent.CATEGORY_TEST);
//供测试使用
PackageManager pm = getPackageManager();
//包控制器,用来获得现在有的进程
List<ResolveInfo> list = pm.queryIntentActivities(mainIntent,
0
);
//通过Intent查找相关的Activity,更准确
if
(
null
== list)
//没找到那个测试意图
return
myData;
//就结束
/* 以下是个没有被使用的方法用来分割字符串,用/
String[] prefixPath;
if (prefix.equals("")) {
prefixPath = null;
} else {
prefixPath = prefix.split("/");
}*/
int
len = list.size();
//得到list的长度
// Map<String, Boolean> entries = new HashMap<String, Boolean>();//一个没有被使用的map,用了哈希类型的map
for
(
int
i =
0
; i < len; i++) {
ResolveInfo info = list.get(i);
//用来循环刚才得到的那个list
String activityName = info.activityInfo.name;
//得到activity的名字
if
(prefix.length() ==
0
|| activityName.startsWith(prefix)) {
//如果要查找的那个activity的包名长度为0或者与我们要找的那个名字是以这个开头的
String[] labelPath = activityName.split(
"\\."
);
//把包名用.分开
String nextLabel = labelPath[labelPath.length -
1
];
//得到最后一个.后面的名字,即类名
addItem(myData, nextLabel, activityIntent(
//现在把他添加到mydata的集合,这个方法在后面会出现,就是分别把各种信息用map区分了
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name));
}
}
Collections.sort(myData, sDisplayNameComparator);
//Collections是个很有用的集合,其中有个sort是用来排序的,参数中sDisplayNameComparator是个比较方法
return
myData;
}
//下面这个定义,乍一看是方法,其实是个字段,定义一个比较方法的引用
private
final
static
Comparator<Map<String, ?>> sDisplayNameComparator =
new
Comparator<Map<String, ?>>() {
//对那个引用的实现
private
final
Collator collator = Collator.getInstance();
//新疆一个集合
public
int
compare(Map<String,?> map1, Map<String,?> map2) {
//两个的比较规则
return
collator.compare(map1.get(
"title"
), map2.get(
"title"
));
//就是比较他们字符的那种方法,就是从左到右一个一个字母,比ascll码
}
};
protected
Intent activityIntent(String pkg, String componentName) {
Intent result =
new
Intent();
result.setClassName(pkg, componentName);
//设置意图从哪个包到哪个类
return
result;
}
protected
Intent browserIntent(String path) {
Intent result =
new
Intent();
result.setClass(
this
, Cocos2D.
class
);
//设置从哪个class到哪
result.putExtra(
"org.cocos2d.tests.Path"
, path);
//加一个额外的变量
return
result;
}
protected
void
addItem(List<Map<String,?>> data, String name, Intent intent) {
Map<String, Object> temp =
new
HashMap<String, Object>();
temp.put(
"title"
, name);
temp.put(
"intent"
, intent);
data.add(temp);
//对data添加项目
}
@Override
protected
void
onListItemClick(ListView l, View v,
int
position,
long
id) {
Map<String,?> map = (Map<String,?>) l.getItemAtPosition(position);
//得到点击了哪个包
Intent intent = (Intent) map.get(
"intent"
);
//找到那个类全名
startActivity(intent);
}
}