Subversion Repositories ES

Rev

Rev 17 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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