Subversion Repositories ES

Rev

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

Rev Author Line No. Line
8 PointedEar 1
package de.pointedears.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
 
14 PointedEar 36
/**
37
 * Generates the main menu as a list of activities from the manifest
38
 *
39
 * @author
40
 *         Thomas 'PointedEars' Lahn;
41
 *         Class courtesy of The Android Open Source Project
42
 *         (Android 2.2 SDK Examples), slightly adapted
43
 */
6 PointedEar 44
public class MenuActivity extends ListActivity
45
{
5 PointedEar 46
 
6 PointedEar 47
  @Override
48
  public void onCreate(Bundle savedInstanceState)
49
  {
50
    super.onCreate(savedInstanceState);
5 PointedEar 51
 
6 PointedEar 52
    Intent intent = this.getIntent();
14 PointedEar 53
    String path = intent.getStringExtra("de.pointedears.converter.Path"); //$NON-NLS-1$
5 PointedEar 54
 
6 PointedEar 55
    if (path == null)
56
    {
14 PointedEar 57
      path = ""; //$NON-NLS-1$
6 PointedEar 58
    }
59
 
60
    this.setListAdapter(new SimpleAdapter(this, this.getData(path),
14 PointedEar 61
                android.R.layout.simple_list_item_1, new String[] { "title" }, //$NON-NLS-1$
62
      new int[] { android.R.id.text1 }));
6 PointedEar 63
    this.getListView().setTextFilterEnabled(true);
64
  }
65
 
14 PointedEar 66
  /**
67
   * @param prefix
68
   * @return
69
   */
6 PointedEar 70
  protected List getData(String prefix)
71
  {
72
    List<Map> myData = new ArrayList<Map>();
73
 
74
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
75
    mainIntent.addCategory(ConverterApplication.CATEGORY_CONVERTER);
76
 
77
    PackageManager pm = this.getPackageManager();
78
    List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
79
 
80
    if (null == list)
81
    {
82
      return myData;
5 PointedEar 83
    }
84
 
6 PointedEar 85
    String[] prefixPath;
5 PointedEar 86
 
14 PointedEar 87
    if (prefix.equals("")) //$NON-NLS-1$
6 PointedEar 88
    {
89
      prefixPath = null;
90
    }
91
    else
92
    {
14 PointedEar 93
      prefixPath = prefix.split("/"); //$NON-NLS-1$
6 PointedEar 94
    }
5 PointedEar 95
 
6 PointedEar 96
    int len = list.size();
5 PointedEar 97
 
6 PointedEar 98
    Map<String, Boolean> entries = new HashMap<String, Boolean>();
5 PointedEar 99
 
6 PointedEar 100
    for (int i = 0; i < len; i++)
101
    {
102
      ResolveInfo info = list.get(i);
103
      CharSequence labelSeq = info.loadLabel(pm);
104
      String label = labelSeq != null
5 PointedEar 105
                    ? labelSeq.toString()
106
                    : info.activityInfo.name;
107
 
6 PointedEar 108
      if (prefix.length() == 0 || label.startsWith(prefix))
109
      {
5 PointedEar 110
 
14 PointedEar 111
        String[] labelPath = label.split("/"); //$NON-NLS-1$
6 PointedEar 112
 
113
        String nextLabel =
114
          prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];
115
 
116
        if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1)
117
        {
118
          this.addItem(myData, nextLabel, this.activityIntent(
5 PointedEar 119
                            info.activityInfo.applicationInfo.packageName,
120
                            info.activityInfo.name));
121
        }
6 PointedEar 122
        else
123
        {
124
          if (entries.get(nextLabel) == null)
125
          {
126
            this.addItem(
127
              myData,
128
              nextLabel,
14 PointedEar 129
              this.browseIntent(prefix.equals("") ? nextLabel : prefix + "/" //$NON-NLS-1$//$NON-NLS-2$
6 PointedEar 130
                + nextLabel));
131
            entries.put(nextLabel, true);
132
          }
133
        }
134
      }
5 PointedEar 135
    }
136
 
6 PointedEar 137
    Collections.sort(myData, MenuActivity.sDisplayNameComparator);
5 PointedEar 138
 
6 PointedEar 139
    return myData;
140
  }
141
 
142
  private final static Comparator<Map> sDisplayNameComparator =
143
    new Comparator<Map>() {
144
      private final Collator collator = Collator.getInstance();
145
 
146
      public int compare(Map map1, Map map2)
147
      {
14 PointedEar 148
        return this.collator.compare(map1.get("title"), map2.get("title")); //$NON-NLS-1$ //$NON-NLS-2$
6 PointedEar 149
      }
5 PointedEar 150
    };
151
 
14 PointedEar 152
  /**
153
   * @param pkg
154
   * @param componentName
155
   * @return
156
   */
6 PointedEar 157
  protected Intent activityIntent(String pkg, String componentName)
158
  {
159
    Intent result = new Intent();
160
    result.setClassName(pkg, componentName);
161
    return result;
162
  }
5 PointedEar 163
 
14 PointedEar 164
  /**
165
   * @param path
166
   * @return
167
   */
6 PointedEar 168
  protected Intent browseIntent(String path)
169
  {
170
    Intent result = new Intent();
171
    result.setClass(this, MenuActivity.class);
14 PointedEar 172
    result.putExtra("de.pointedears.converter.Path", path); //$NON-NLS-1$
6 PointedEar 173
    return result;
174
  }
5 PointedEar 175
 
14 PointedEar 176
  /**
177
   * @param data
178
   * @param name
179
   * @param intent
180
   */
6 PointedEar 181
  protected void addItem(List<Map> data, String name, Intent intent)
182
  {
183
    Map<String, Object> temp = new HashMap<String, Object>();
14 PointedEar 184
    temp.put("title", name); //$NON-NLS-1$
185
    temp.put("intent", intent); //$NON-NLS-1$
6 PointedEar 186
    data.add(temp);
187
  }
5 PointedEar 188
 
14 PointedEar 189
  /*
190
   * (non-Javadoc)
191
   *
192
   * @see android.app.ListActivity#onListItemClick(android.widget.ListView,
193
   * android.view.View, int, long)
194
   */
6 PointedEar 195
  @Override
196
  protected void onListItemClick(ListView l, View v, int position, long id)
197
  {
198
    Map map = (Map) l.getItemAtPosition(position);
5 PointedEar 199
 
14 PointedEar 200
    Intent intent = (Intent) map.get("intent"); //$NON-NLS-1$
6 PointedEar 201
    this.startActivity(intent);
202
  }
203
 
5 PointedEar 204
}