1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.car.cluster.sample; 18 19 import android.content.Intent; 20 import android.os.Binder; 21 import android.os.Handler; 22 import android.os.IBinder; 23 import android.os.Message; 24 import android.service.notification.NotificationListenerService; 25 import android.service.notification.StatusBarNotification; 26 import android.util.Log; 27 28 /** 29 * Listens to status bar notifications and passes it to the listener. 30 */ 31 public class StatusBarNotificationListener extends NotificationListenerService { 32 33 private static final String TAG = DebugUtil.getTag(StatusBarNotificationListener.class); 34 35 static final String ACTION_LOCAL_BINDING = "local_binding"; 36 37 private Handler mHandler; 38 39 @Override 40 public void onNotificationPosted(StatusBarNotification sbn) { 41 if (MessagingConverter.canConvert(sbn) && mHandler != null) { 42 Message msg = Message.obtain(mHandler); 43 msg.obj = sbn; 44 mHandler.sendMessage(msg); 45 } 46 } 47 48 @Override 49 public IBinder onBind(Intent intent) { 50 Log.d(TAG, "onBind, intent:" + intent); 51 52 return ACTION_LOCAL_BINDING.equals(intent.getAction()) 53 ? new LocalBinder() : super.onBind(intent); 54 } 55 56 public void setHandler(Handler handler) { 57 mHandler = handler; 58 } 59 60 public class LocalBinder extends Binder { 61 public StatusBarNotificationListener getService() { 62 return StatusBarNotificationListener.this; 63 } 64 } 65 } 66