diff options
author | Chris Wren <cwren@android.com> | 2016-01-06 17:13:19 -0500 |
---|---|---|
committer | Chris Wren <cwren@android.com> | 2016-01-08 10:51:42 -0500 |
commit | 32b5c8459755811c6258d1d2453bb6d971aa2dc5 (patch) | |
tree | 14cc631dec2475742d2db53735fff5af62c361bf | |
parent | 9141df83627a45d912b66c38f4e727a7521014ca (diff) | |
download | experimental-32b5c8459755811c6258d1d2453bb6d971aa2dc5.tar.gz |
enable snoozw for the sample notification listener
Bug: 19232554
Change-Id: I3f1dc9c1c4475193006b42ff886aa3a8d3853617
4 files changed, 192 insertions, 112 deletions
diff --git a/NotificationListenerSample/res/layout/main.xml b/NotificationListenerSample/res/layout/main.xml index 44c9da4..9cc681c 100644 --- a/NotificationListenerSample/res/layout/main.xml +++ b/NotificationListenerSample/res/layout/main.xml @@ -46,5 +46,13 @@ android:onClick="launchSettings" android:layout_weight="0" /> + <Button + android:id="@+id/snooze" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/snooze" + android:onClick="snooze" + android:layout_weight="0" + /> </LinearLayout> diff --git a/NotificationListenerSample/res/values/strings.xml b/NotificationListenerSample/res/values/strings.xml index 5c74fda..9894c07 100644 --- a/NotificationListenerSample/res/values/strings.xml +++ b/NotificationListenerSample/res/values/strings.xml @@ -22,6 +22,8 @@ <string name="nothing_to_see">Service is disabled. You must enabled it to see notifications.</string> <string name="launch_to_disable">Press to disable service</string> <string name="launch_to_enable">Press to enable service</string> + <string name="snooze">Press to snooze service</string> + <string name="unsnooze">Press to unsnooze service</string> <string name="explanation">This app demonstrates use of the Notification Listener Service API. You must enable the listener service in Security Settings to see it work. </string> diff --git a/NotificationListenerSample/src/com/android/example/notificationlistener/Listener.java b/NotificationListenerSample/src/com/android/example/notificationlistener/Listener.java index 466b084..4f79375 100644 --- a/NotificationListenerSample/src/com/android/example/notificationlistener/Listener.java +++ b/NotificationListenerSample/src/com/android/example/notificationlistener/Listener.java @@ -19,11 +19,13 @@ package com.android.example.notificationlistener; import android.app.Notification; import android.app.PendingIntent; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Handler; import android.os.Message; +import android.os.RemoteException; import android.service.notification.NotificationListenerService; import android.service.notification.NotificationListenerService.Ranking; import android.service.notification.NotificationListenerService.RankingMap; @@ -47,21 +49,49 @@ public class Listener extends NotificationListenerService { private static final int MSG_ORDER = 4; private static final int MSG_DISMISS = 5; private static final int MSG_LAUNCH = 6; - private static final int PAGE = 10; + private static final int MSG_SNOOZE = 7; static final String ACTION_DISMISS = "com.android.example.notificationlistener.DISMISS"; static final String ACTION_LAUNCH = "com.android.example.notificationlistener.LAUNCH"; static final String ACTION_REFRESH = "com.android.example.notificationlistener.REFRESH"; + static final String ACTION_STATE_CHANGE = "com.android.example.notificationlistener.STATE"; static final String EXTRA_KEY = "key"; private static ArrayList<StatusBarNotification> sNotifications; + private static boolean sConnected; public static List<StatusBarNotification> getNotifications() { return sNotifications; } + public static boolean isConnected() { + return sConnected; + } + + public static void toggleSnooze(Context context) { + if (sConnected) { + Log.d(TAG, "scheduling snooze"); + if (sHandler != null) { + sHandler.sendEmptyMessage(MSG_SNOOZE); + } + } else { + Log.d(TAG, "trying to unsnooze"); + try { + NotificationListenerService.requestRebind( + ComponentName.createRelative(context.getPackageName(), + Listener.class.getCanonicalName())); + } catch (RemoteException e) { + Log.e(TAG, "failed to rebind service", e); + } + } + } + private final Ranking mTmpRanking = new Ranking(); + private static Handler sHandler; + + private RankingMap mRankingMap; + private class Delta { final StatusBarNotification mSbn; final RankingMap mRankingMap; @@ -97,113 +127,124 @@ public class Listener extends NotificationListenerService { Log.d(TAG, "received an action broadcast " + intent.getAction()); if (!TextUtils.isEmpty(key)) { Log.d(TAG, " on " + key); - Message.obtain(mHandler, what, key).sendToTarget(); + Message.obtain(sHandler, what, key).sendToTarget(); } } }; - private final Handler mHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - Delta delta = null; - if (msg.obj instanceof Delta) { - delta = (Delta) msg.obj; - } + @Override + public void onCreate() { + super.onCreate(); + sHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + Delta delta = null; + if (msg.obj instanceof Delta) { + delta = (Delta) msg.obj; + } - switch (msg.what) { - case MSG_NOTIFY: - Log.i(TAG, "notify: " + delta.mSbn.getKey()); - synchronized (sNotifications) { - boolean exists = mRankingMap.getRanking(delta.mSbn.getKey(), mTmpRanking); - if (!exists) { - sNotifications.add(delta.mSbn); - } else { - int position = mTmpRanking.getRank(); - sNotifications.set(position, delta.mSbn); - } - mRankingMap = delta.mRankingMap; - Collections.sort(sNotifications, mRankingComparator); - Log.i(TAG, "finish with: " + sNotifications.size()); - } - LocalBroadcastManager.getInstance(Listener.this) - .sendBroadcast(new Intent(ACTION_REFRESH) - .putExtra(EXTRA_KEY, delta.mSbn.getKey())); - break; - - case MSG_CANCEL: - Log.i(TAG, "remove: " + delta.mSbn.getKey()); - synchronized (sNotifications) { - boolean exists = mRankingMap.getRanking(delta.mSbn.getKey(), mTmpRanking); - if (exists) { - sNotifications.remove(mTmpRanking.getRank()); - } - mRankingMap = delta.mRankingMap; - Collections.sort(sNotifications, mRankingComparator); - } - LocalBroadcastManager.getInstance(Listener.this) - .sendBroadcast(new Intent(ACTION_REFRESH)); - break; - - case MSG_ORDER: - Log.i(TAG, "reorder"); - synchronized (sNotifications) { - mRankingMap = delta.mRankingMap; - Collections.sort(sNotifications, mRankingComparator); - } - LocalBroadcastManager.getInstance(Listener.this) - .sendBroadcast(new Intent(ACTION_REFRESH)); - break; - - case MSG_STARTUP: - fetchActive(); - Log.i(TAG, "start with: " + sNotifications.size() + " notifications."); - LocalBroadcastManager.getInstance(Listener.this) - .sendBroadcast(new Intent(ACTION_REFRESH)); - break; - - case MSG_DISMISS: - if (msg.obj instanceof String) { - final String key = (String) msg.obj; - mRankingMap.getRanking(key, mTmpRanking); - StatusBarNotification sbn = sNotifications.get(mTmpRanking.getRank()); - if ((sbn.getNotification().flags & Notification.FLAG_AUTO_CANCEL) != 0 && - sbn.getNotification().contentIntent != null) { - try { - sbn.getNotification().contentIntent.send(); - } catch (PendingIntent.CanceledException e) { - Log.d(TAG, "failed to send intent for " + sbn.getKey(), e); + switch (msg.what) { + case MSG_NOTIFY: + Log.i(TAG, "notify: " + delta.mSbn.getKey()); + synchronized (sNotifications) { + boolean exists = mRankingMap.getRanking(delta.mSbn.getKey(), mTmpRanking); + if (!exists) { + sNotifications.add(delta.mSbn); + } else { + int position = mTmpRanking.getRank(); + sNotifications.set(position, delta.mSbn); } + mRankingMap = delta.mRankingMap; + Collections.sort(sNotifications, mRankingComparator); + Log.i(TAG, "finish with: " + sNotifications.size()); } - cancelNotification(key); - } - break; - - case MSG_LAUNCH: - if (msg.obj instanceof String) { - final String key = (String) msg.obj; - mRankingMap.getRanking(key, mTmpRanking); - StatusBarNotification sbn = sNotifications.get(mTmpRanking.getRank()); - if (sbn.getNotification().contentIntent != null) { - try { - sbn.getNotification().contentIntent.send(); - } catch (PendingIntent.CanceledException e) { - Log.d(TAG, "failed to send intent for " + sbn.getKey(), e); + LocalBroadcastManager.getInstance(Listener.this) + .sendBroadcast(new Intent(ACTION_REFRESH) + .putExtra(EXTRA_KEY, delta.mSbn.getKey())); + break; + + case MSG_CANCEL: + final String cancelKey = delta.mSbn.getKey(); + Log.i(TAG, "remove: " + cancelKey); + synchronized (sNotifications) { + boolean exists = mRankingMap.getRanking(cancelKey, mTmpRanking); + if (exists) { + sNotifications.remove(mTmpRanking.getRank()); } + mRankingMap = delta.mRankingMap; + Collections.sort(sNotifications, mRankingComparator); } - if ((sbn.getNotification().flags & Notification.FLAG_AUTO_CANCEL) != 0) { + LocalBroadcastManager.getInstance(Listener.this) + .sendBroadcast(new Intent(ACTION_REFRESH) + .putExtra(EXTRA_KEY, cancelKey)); + break; + + case MSG_ORDER: + Log.i(TAG, "reorder"); + synchronized (sNotifications) { + mRankingMap = delta.mRankingMap; + Collections.sort(sNotifications, mRankingComparator); + } + LocalBroadcastManager.getInstance(Listener.this) + .sendBroadcast(new Intent(ACTION_REFRESH)); + break; + + case MSG_STARTUP: + sConnected = true; + fetchActive(); + Log.i(TAG, "start with: " + sNotifications.size() + " notifications."); + LocalBroadcastManager.getInstance(Listener.this) + .sendBroadcast(new Intent(ACTION_REFRESH)); + LocalBroadcastManager.getInstance(Listener.this) + .sendBroadcast(new Intent(ACTION_STATE_CHANGE)); + break; + + case MSG_DISMISS: + if (msg.obj instanceof String) { + final String key = (String) msg.obj; + mRankingMap.getRanking(key, mTmpRanking); + StatusBarNotification sbn = sNotifications.get(mTmpRanking.getRank()); + if ((sbn.getNotification().flags & Notification.FLAG_AUTO_CANCEL) != 0 && + sbn.getNotification().contentIntent != null) { + try { + sbn.getNotification().contentIntent.send(); + } catch (PendingIntent.CanceledException e) { + Log.d(TAG, "failed to send intent for " + sbn.getKey(), e); + } + } cancelNotification(key); } - } - break; - } - } - }; + break; - private RankingMap mRankingMap; + case MSG_LAUNCH: + if (msg.obj instanceof String) { + final String key = (String) msg.obj; + mRankingMap.getRanking(key, mTmpRanking); + StatusBarNotification sbn = sNotifications.get(mTmpRanking.getRank()); + if (sbn.getNotification().contentIntent != null) { + try { + sbn.getNotification().contentIntent.send(); + } catch (PendingIntent.CanceledException e) { + Log.d(TAG, "failed to send intent for " + sbn.getKey(), e); + } + } + if ((sbn.getNotification().flags & Notification.FLAG_AUTO_CANCEL) != 0) { + cancelNotification(key); + } + } + break; - @Override - public void onCreate() { - super.onCreate(); + case MSG_SNOOZE: + Log.d(TAG, "trying to snooze"); + try { + requestUnbind(); + } catch (RemoteException e) { + Log.e(TAG, "failed to unbind service", e); + } + break; + } + } + }; Log.d(TAG, "registering broadcast listener"); final IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ACTION_DISMISS); @@ -213,30 +254,38 @@ public class Listener extends NotificationListenerService { @Override public void onDestroy() { + sConnected = false; + LocalBroadcastManager.getInstance(Listener.this) + .sendBroadcast(new Intent(ACTION_STATE_CHANGE)); + sHandler = null; LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver); super.onDestroy(); } @Override public void onListenerConnected() { - Message.obtain(mHandler, MSG_STARTUP).sendToTarget(); + Log.w(TAG, "onListenerConnected: "); + Message.obtain(sHandler, MSG_STARTUP).sendToTarget(); } @Override public void onNotificationRankingUpdate(RankingMap rankingMap) { - Message.obtain(mHandler, MSG_ORDER, + Log.w(TAG, "onNotificationRankingUpdate"); + Message.obtain(sHandler, MSG_ORDER, new Delta(null, rankingMap)).sendToTarget(); } @Override public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) { - Message.obtain(mHandler, MSG_NOTIFY, + Log.w(TAG, "onNotificationPosted: " + sbn.getKey()); + Message.obtain(sHandler, MSG_NOTIFY, new Delta(sbn, rankingMap)).sendToTarget(); } @Override public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) { - Message.obtain(mHandler, MSG_CANCEL, + Log.w(TAG, "onNotificationRemoved: " + sbn.getKey()); + Message.obtain(sHandler, MSG_CANCEL, new Delta(sbn, rankingMap)).sendToTarget(); } @@ -245,6 +294,7 @@ public class Listener extends NotificationListenerService { sNotifications = new ArrayList<StatusBarNotification>(); for (StatusBarNotification sbn : getActiveNotifications()) { sNotifications.add(sbn); + Log.w(TAG, "startup poll: " + sbn.getKey()); } Collections.sort(sNotifications, mRankingComparator); } diff --git a/NotificationListenerSample/src/com/android/example/notificationlistener/NotificationListenerActivity.java b/NotificationListenerSample/src/com/android/example/notificationlistener/NotificationListenerActivity.java index 61040a0..bbba935 100644 --- a/NotificationListenerSample/src/com/android/example/notificationlistener/NotificationListenerActivity.java +++ b/NotificationListenerSample/src/com/android/example/notificationlistener/NotificationListenerActivity.java @@ -45,6 +45,7 @@ public class NotificationListenerActivity extends ListActivity { private static final String TAG = "NotificationListenerActivity"; private Button mLaunchButton; + private Button mSnoozeButton; private TextView mEmptyText; private StatusAdaptor mStatusAdaptor; private final BroadcastReceiver mRefreshListener = new BroadcastReceiver() { @@ -54,6 +55,13 @@ public class NotificationListenerActivity extends ListActivity { updateList(intent.getStringExtra(Listener.EXTRA_KEY)); } }; + private final BroadcastReceiver mStateListener = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + Log.i(TAG, "state tickle"); + checkEnabled(); + } + }; @Override public void onCreate(Bundle savedInstanceState) { @@ -61,6 +69,7 @@ public class NotificationListenerActivity extends ListActivity { setTitle(R.string.long_app_name); setContentView(R.layout.main); mLaunchButton = (Button) findViewById(R.id.launch_settings); + mSnoozeButton = (Button) findViewById(R.id.snooze); mEmptyText = (TextView) findViewById(android.R.id.empty); mStatusAdaptor = new StatusAdaptor(this); setListAdapter(mStatusAdaptor); @@ -68,15 +77,20 @@ public class NotificationListenerActivity extends ListActivity { @Override protected void onStop() { - LocalBroadcastManager.getInstance(this).unregisterReceiver(mRefreshListener); + LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); + localBroadcastManager.unregisterReceiver(mRefreshListener); + localBroadcastManager.unregisterReceiver(mStateListener); super.onStop(); } @Override public void onStart() { super.onStart(); - final IntentFilter intentFilter = new IntentFilter(Listener.ACTION_REFRESH); - LocalBroadcastManager.getInstance(this).registerReceiver(mRefreshListener, intentFilter); + LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this); + localBroadcastManager.registerReceiver(mRefreshListener, + new IntentFilter(Listener.ACTION_REFRESH)); + localBroadcastManager.registerReceiver(mStateListener, + new IntentFilter(Listener.ACTION_STATE_CHANGE)); updateList(null); } @@ -96,8 +110,15 @@ public class NotificationListenerActivity extends ListActivity { if (listeners != null && listeners.contains(LISTENER_PATH)) { mLaunchButton.setText(R.string.launch_to_disable); mEmptyText.setText(R.string.waiting_for_content); + mSnoozeButton.setEnabled(true); + if (Listener.isConnected()) { + mSnoozeButton.setText(R.string.snooze); + } else { + mSnoozeButton.setText(R.string.unsnooze); + } } else { mLaunchButton.setText(R.string.launch_to_enable); + mSnoozeButton.setEnabled(false); mEmptyText.setText(R.string.nothing_to_see); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(R.string.explanation) @@ -117,6 +138,11 @@ public class NotificationListenerActivity extends ListActivity { new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"), 0); } + public void snooze(View v) { + Log.d(TAG, "clicked snooze"); + Listener.toggleSnooze(this); + } + public void dismiss(View v) { Log.d(TAG, "clicked dismiss "); Object tag = v.getTag(); @@ -142,11 +168,9 @@ public class NotificationListenerActivity extends ListActivity { } private void updateList(String key) { - if (mStatusAdaptor.requiresInitialization()) { - final List<StatusBarNotification> notifications = Listener.getNotifications(); - if (notifications != null) { - mStatusAdaptor.init(notifications); - } + final List<StatusBarNotification> notifications = Listener.getNotifications(); + if (notifications != null) { + mStatusAdaptor.setData(notifications); } mStatusAdaptor.update(key); } @@ -252,11 +276,7 @@ public class NotificationListenerActivity extends ListActivity { } } - public boolean requiresInitialization() { - return mNotifications == null; - } - - public void init(List<StatusBarNotification> notifications) { + public void setData(List<StatusBarNotification> notifications) { mNotifications = notifications; } } |