Subversion Repositories ES

Rev

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

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