Subversion Repositories ES

Rev

Rev 8 | Rev 17 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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