1bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerpackage com.android.notificationlog;
2bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
3bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport java.io.IOException;
4bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport java.util.ArrayList;
5bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
6bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.app.ListActivity;
7bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.content.Context;
8bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.content.pm.ApplicationInfo;
9bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.content.pm.PackageManager;
10bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.content.pm.PackageManager.NameNotFoundException;
11bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.graphics.drawable.Drawable;
12bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.os.Bundle;
13bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.util.EventLog;
14bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.view.LayoutInflater;
15bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.view.View;
16bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.view.ViewGroup;
17bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.widget.BaseAdapter;
18bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.widget.ImageView;
19bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.widget.ListAdapter;
20bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport android.widget.TextView;
21bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
22bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerimport java.util.Date;
23bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
24bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandlerpublic class NotificationLogActivity extends ListActivity {
25bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler    @Override
26bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler    public void onCreate(Bundle savedInstanceState) {
27bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        super.onCreate(savedInstanceState);
28bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
29bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        setContentView(R.layout.main);
30bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
31bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        ListAdapter adapter = new NotificationLogAdapter();
32bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        setListAdapter(adapter);
33bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        getListView().setTextFilterEnabled(true);
34bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler    }
35bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
36bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
37bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler    class NotificationLogAdapter extends BaseAdapter {
38bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        private ArrayList<EventLog.Event> mNotificationEvents;
39bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        private final PackageManager mPM;
40bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        private final LayoutInflater mInflater;
41bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
42bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        public NotificationLogAdapter() {
43bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            mPM = getPackageManager();
44bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
45bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            mNotificationEvents = new ArrayList<EventLog.Event>();
46bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
47bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            int[] tags = new int[] { EventLog.getTagCode("notification_enqueue") };
48bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            try {
49bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                EventLog.readEvents(tags, mNotificationEvents);
50bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            } catch (IOException e) {
51bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                // TODO Auto-generated catch block
52bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                e.printStackTrace();
53bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                return;
54bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            }
55bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            android.util.Log.d("NotificationLogActivity", "loaded " + getCount() + " entries");
56bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        }
57bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
58bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        public int getCount() {
59bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            return mNotificationEvents != null ? mNotificationEvents.size() : 0;
60bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        }
61bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
62bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        public Object getItem(int position) {
63bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            return position;
64bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        }
65bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
66bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        public long getItemId(int position) {
67bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            return position;
68bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        }
69bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
70bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        @Override
71bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        public View getView(int position, View convertView, ViewGroup parent) {
72bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            View view;
73bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            if (convertView == null) {
74bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                view = mInflater.inflate(R.layout.row, parent, false);
75bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            } else {
76bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                view = convertView;
77bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            }
78bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            bindView(view, mNotificationEvents.get(position));
79bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            return view;
80bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        }
81bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
82bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
83bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        private final void bindView(View view, EventLog.Event evt) {
84bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            TextView title = (TextView)view.findViewById(R.id.title);
85bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            TextView more = (TextView)view.findViewById(R.id.text);
86bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            TextView time = (TextView)view.findViewById(R.id.time);
87bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            ImageView icon = (ImageView)view.findViewById(R.id.icon);
88bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
89bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            Object[] data = (Object[]) evt.getData();
90bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            // EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag,
91bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            // notification.toString());
92bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            String pkg = (String) data[0];
93bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            int id = (Integer) data[1];
94bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            String tag = (String) data[2];
95bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            String text = (String) data[3];
96bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler
97bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            ApplicationInfo appInfo;
98bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            Drawable appIcon = null;
99bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            try {
100bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                appInfo = mPM.getApplicationInfo(pkg, 0);
101bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                pkg =  mPM.getApplicationLabel(appInfo) + " (" + pkg + ")";
102bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler                appIcon = mPM.getApplicationIcon(appInfo);
103bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            } catch (NameNotFoundException e) {
104bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            }
105bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            title.setText(pkg);
106bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            more.setText(text);
107bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            time.setText(new Date(evt.getTimeNanos()/1000000).toString());
108bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler            icon.setImageDrawable(appIcon);
109bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler        }
110bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler    }
111bbc4a6dfa6358e60a88d69b4601dc2236e56423aDaniel Sandler}
112