Subversion Repositories ES

Rev

Rev 5 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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