summaryrefslogtreecommitdiff
path: root/espresso/espresso-sample/src/main/java/com/google/android/apps/common/testing/ui/testapp/MainActivity.java
blob: c5ad7628aa624db2be53138c72c65d6948e0ab4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.common.testing.ui.testapp;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Displays a list with all available activities.
 */
public class MainActivity extends ListActivity {
  private static final String TAG = MainActivity.class.getSimpleName();

  private static final Comparator<Map<String, Object>> sDisplayNameComparator =
      new Comparator<Map<String, Object>>() {
        private final Collator collator = Collator.getInstance();

        @Override
        public int compare(Map<String, Object> map1, Map<String, Object> map2) {
          return collator.compare(map1.get("title"), map2.get("title"));
        }
      };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new SimpleAdapter(
        this, getData(), android.R.layout.simple_list_item_1, new String[] {"title"},
        new int[] {android.R.id.text1}));
    getListView().setTextFilterEnabled(true);
  }

  private List<Map<String, Object>> getData() {
    List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

    PackageInfo info = null;
    try {
      info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES);
    } catch (NameNotFoundException e) {
      Log.e(TAG, "Packageinfo not found in: " + getPackageName());
    }

    if (null == info) {
      return data;
    } else {
      for (ActivityInfo activityInfo : info.activities) {

        if (!activityInfo.name.equals(getComponentName().getClassName())) {
          String[] label = activityInfo.name.split(getPackageName() + ".");
          addItem(data, label[1],
              createActivityIntent(activityInfo.applicationInfo.packageName, activityInfo.name));
        }
      }
    }

    Collections.sort(data, sDisplayNameComparator);
    return data;
  }

  private Intent createActivityIntent(String pkg, String componentName) {
    Intent result = new Intent();
    result.setClassName(pkg, componentName);
    return result;
  }

  private void addItem(List<Map<String, Object>> data, String name, Intent intent) {
    Map<String, Object> temp = new HashMap<String, Object>();
    temp.put("title", name);
    temp.put("intent", intent);
    data.add(temp);
  }

  @Override
  protected void onListItemClick(ListView listView, View view, int position, long id) {
    Map<?, ?> map = (Map<?, ?>) listView.getItemAtPosition(position);

    Intent intent = (Intent) map.get("intent");
    startActivity(intent);
  }
}