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