1 package com.android.notificationlog;
2 
3 import java.io.IOException;
4 import java.util.ArrayList;
5 
6 import android.app.ListActivity;
7 import android.content.Context;
8 import android.content.pm.ApplicationInfo;
9 import android.content.pm.PackageManager;
10 import android.content.pm.PackageManager.NameNotFoundException;
11 import android.graphics.drawable.Drawable;
12 import android.os.Bundle;
13 import android.util.EventLog;
14 import android.view.LayoutInflater;
15 import android.view.View;
16 import android.view.ViewGroup;
17 import android.widget.BaseAdapter;
18 import android.widget.ImageView;
19 import android.widget.ListAdapter;
20 import android.widget.TextView;
21 
22 import java.util.Date;
23 
24 public class NotificationLogActivity extends ListActivity {
25     @Override
onCreate(Bundle savedInstanceState)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 
NotificationLogAdapter()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 
getCount()58         public int getCount() {
59             return mNotificationEvents != null ? mNotificationEvents.size() : 0;
60         }
61 
getItem(int position)62         public Object getItem(int position) {
63             return position;
64         }
65 
getItemId(int position)66         public long getItemId(int position) {
67             return position;
68         }
69 
70         @Override
getView(int position, View convertView, ViewGroup parent)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 
bindView(View view, EventLog.Event evt)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