Subversion Repositories ES

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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