Subversion Repositories ES

Rev

Rev 5 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line 1... Line 1...
1
package ch.ffhs.converter;
1
package ch.ffhs.converter;
-
 
2
2
/*
3
/*
3
 * Copyright (C) 2007 The Android Open Source Project
4
 * Copyright (C) 2007 The Android Open Source Project
4
 *
5
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * you may not use this file except in compliance with the License.
Line 13... Line 14...
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 * limitations under the License.
16
 */
17
 */
17
18
-
 
19
import java.text.Collator;
-
 
20
import java.util.ArrayList;
-
 
21
import java.util.Collections;
-
 
22
import java.util.Comparator;
-
 
23
import java.util.HashMap;
-
 
24
import java.util.List;
18
-
 
-
 
25
import java.util.Map;
19
26
20
import android.app.ListActivity;
27
import android.app.ListActivity;
21
import android.content.Intent;
28
import android.content.Intent;
22
import android.content.pm.PackageManager;
29
import android.content.pm.PackageManager;
23
import android.content.pm.ResolveInfo;
30
import android.content.pm.ResolveInfo;
24
import android.os.Bundle;
31
import android.os.Bundle;
25
import android.view.View;
32
import android.view.View;
26
import android.widget.ListView;
33
import android.widget.ListView;
27
import android.widget.SimpleAdapter;
34
import android.widget.SimpleAdapter;
28
35
29
import java.text.Collator;
-
 
30
import java.util.ArrayList;
-
 
31
import java.util.Collections;
36
public class MenuActivity extends ListActivity
32
import java.util.Comparator;
-
 
33
import java.util.HashMap;
-
 
34
import java.util.List;
-
 
35
import java.util.Map;
-
 
36
37
{
37
public class MenuActivity extends ListActivity {
-
 
38
38
39
    @Override
39
  @Override
40
    public void onCreate(Bundle savedInstanceState) {
40
  public void onCreate(Bundle savedInstanceState)
-
 
41
  {
41
        super.onCreate(savedInstanceState);
42
    super.onCreate(savedInstanceState);
42
       
43
43
        Intent intent = getIntent();
44
    Intent intent = this.getIntent();
44
        String path = intent.getStringExtra("ch.ffhs.converter.Path");
45
    String path = intent.getStringExtra("ch.ffhs.converter.Path");
45
       
46
46
        if (path == null) {
47
    if (path == null)
-
 
48
    {
47
            path = "";
49
      path = "";
48
        }
50
    }
49
51
50
        setListAdapter(new SimpleAdapter(this, getData(path),
52
    this.setListAdapter(new SimpleAdapter(this, this.getData(path),
51
                android.R.layout.simple_list_item_1, new String[] { "title" },
53
                android.R.layout.simple_list_item_1, new String[] { "title" },
52
                new int[] { android.R.id.text1 }));
54
                new int[] { android.R.id.text1 }));
53
        getListView().setTextFilterEnabled(true);
55
    this.getListView().setTextFilterEnabled(true);
54
    }
56
  }
55
57
56
    protected List getData(String prefix) {
58
  protected List getData(String prefix)
-
 
59
  {
57
        List<Map> myData = new ArrayList<Map>();
60
    List<Map> myData = new ArrayList<Map>();
58
61
59
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
62
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
60
        mainIntent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
63
    mainIntent.addCategory(ConverterApplication.CATEGORY_CONVERTER);
61
64
62
        PackageManager pm = getPackageManager();
65
    PackageManager pm = this.getPackageManager();
63
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
66
    List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
64
67
65
        if (null == list)
68
    if (null == list)
-
 
69
    {
66
            return myData;
70
      return myData;
-
 
71
    }
67
72
68
        String[] prefixPath;
73
    String[] prefixPath;
69
       
74
70
        if (prefix.equals("")) {
75
    if (prefix.equals(""))
-
 
76
    {
71
            prefixPath = null;
77
      prefixPath = null;
-
 
78
    }
72
        } else {
79
    else
-
 
80
    {
73
            prefixPath = prefix.split("/");
81
      prefixPath = prefix.split("/");
74
        }
82
    }
75
       
83
76
        int len = list.size();
84
    int len = list.size();
77
       
85
78
        Map<String, Boolean> entries = new HashMap<String, Boolean>();
86
    Map<String, Boolean> entries = new HashMap<String, Boolean>();
79
87
80
        for (int i = 0; i < len; i++) {
88
    for (int i = 0; i < len; i++)
-
 
89
    {
81
            ResolveInfo info = list.get(i);
90
      ResolveInfo info = list.get(i);
82
            CharSequence labelSeq = info.loadLabel(pm);
91
      CharSequence labelSeq = info.loadLabel(pm);
83
            String label = labelSeq != null
92
      String label = labelSeq != null
84
                    ? labelSeq.toString()
93
                    ? labelSeq.toString()
85
                    : info.activityInfo.name;
94
                    : info.activityInfo.name;
86
           
95
87
            if (prefix.length() == 0 || label.startsWith(prefix)) {
96
      if (prefix.length() == 0 || label.startsWith(prefix))
-
 
97
      {
88
               
98
89
                String[] labelPath = label.split("/");
99
        String[] labelPath = label.split("/");
90
100
-
 
101
        String nextLabel =
91
                String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
102
          prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
92
103
93
                if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
104
        if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1)
-
 
105
        {
94
                    addItem(myData, nextLabel, activityIntent(
106
          this.addItem(myData, nextLabel, this.activityIntent(
95
                            info.activityInfo.applicationInfo.packageName,
107
                            info.activityInfo.applicationInfo.packageName,
96
                            info.activityInfo.name));
108
                            info.activityInfo.name));
-
 
109
        }
97
                } else {
110
        else
-
 
111
        {
98
                    if (entries.get(nextLabel) == null) {
112
          if (entries.get(nextLabel) == null)
-
 
113
          {
-
 
114
            this.addItem(
-
 
115
              myData,
-
 
116
              nextLabel,
99
                        addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
117
              this.browseIntent(prefix.equals("") ? nextLabel : prefix + "/"
-
 
118
                + nextLabel));
100
                        entries.put(nextLabel, true);
119
            entries.put(nextLabel, true);
101
                    }
120
          }
102
                }
121
        }
103
            }
122
      }
104
        }
123
    }
105
124
106
        Collections.sort(myData, sDisplayNameComparator);
125
    Collections.sort(myData, MenuActivity.sDisplayNameComparator);
107
       
126
108
        return myData;
127
    return myData;
109
    }
128
  }
110
129
111
    private final static Comparator<Map> sDisplayNameComparator = new Comparator<Map>() {
130
  private final static Comparator<Map> sDisplayNameComparator =
-
 
131
    new Comparator<Map>() {
112
        private final Collator   collator = Collator.getInstance();
132
      private final Collator collator = Collator.getInstance();
113
133
114
        public int compare(Map map1, Map map2) {
134
      public int compare(Map map1, Map map2)
-
 
135
      {
115
            return collator.compare(map1.get("title"), map2.get("title"));
136
        return this.collator.compare(map1.get("title"), map2.get("title"));
116
        }
137
      }
117
    };
138
    };
118
139
119
    protected Intent activityIntent(String pkg, String componentName) {
140
  protected Intent activityIntent(String pkg, String componentName)
-
 
141
  {
120
        Intent result = new Intent();
142
    Intent result = new Intent();
121
        result.setClassName(pkg, componentName);
143
    result.setClassName(pkg, componentName);
122
        return result;
144
    return result;
123
    }
145
  }
124
   
146
125
    protected Intent browseIntent(String path) {
147
  protected Intent browseIntent(String path)
-
 
148
  {
126
        Intent result = new Intent();
149
    Intent result = new Intent();
127
        result.setClass(this, MenuActivity.class);
150
    result.setClass(this, MenuActivity.class);
128
        result.putExtra("ch.ffhs.converter.Path", path);
151
    result.putExtra("ch.ffhs.converter.Path", path);
129
        return result;
152
    return result;
130
    }
153
  }
131
154
132
    protected void addItem(List<Map> data, String name, Intent intent) {
155
  protected void addItem(List<Map> data, String name, Intent intent)
-
 
156
  {
133
        Map<String, Object> temp = new HashMap<String, Object>();
157
    Map<String, Object> temp = new HashMap<String, Object>();
134
        temp.put("title", name);
158
    temp.put("title", name);
135
        temp.put("intent", intent);
159
    temp.put("intent", intent);
136
        data.add(temp);
160
    data.add(temp);
137
    }
161
  }
138
162
139
    @Override
163
  @Override
140
    protected void onListItemClick(ListView l, View v, int position, long id) {
164
  protected void onListItemClick(ListView l, View v, int position, long id)
-
 
165
  {
141
        Map map = (Map) l.getItemAtPosition(position);
166
    Map map = (Map) l.getItemAtPosition(position);
142
167
143
        Intent intent = (Intent) map.get("intent");
168
    Intent intent = (Intent) map.get("intent");
144
        startActivity(intent);
169
    this.startActivity(intent);
145
    }
170
  }
146
171
147
}
172
}