Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2014 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 androidx.core.app;
     18 
     19 import android.app.Notification;
     20 import android.app.Service;
     21 import android.content.Intent;
     22 import android.os.Build;
     23 import android.os.IBinder;
     24 import android.os.RemoteException;
     25 import android.support.v4.app.INotificationSideChannel;
     26 
     27 /**
     28  * Abstract service to receive side channel notifications sent from
     29  * {@link androidx.core.app.NotificationManagerCompat}.
     30  *
     31  * <p>To receive side channel notifications, extend this service and register it in your
     32  * android manifest with an intent filter for the BIND_NOTIFICATION_SIDE_CHANNEL action.
     33  * Note: you must also have an enabled
     34  * {@link android.service.notification.NotificationListenerService} within your package.
     35  *
     36  * <p>Example AndroidManifest.xml addition:
     37  * <pre>
     38  * &lt;service android:name="com.example.NotificationSideChannelService"&gt;
     39  *     &lt;intent-filter&gt;
     40  *         &lt;action android:name="android.support.BIND_NOTIFICATION_SIDE_CHANNEL" /&gt;
     41  *     &lt;/intent-filter&gt;
     42  * &lt;/service&gt;</pre>
     43  *
     44  */
     45 public abstract class NotificationCompatSideChannelService extends Service {
     46     @Override
     47     public IBinder onBind(Intent intent) {
     48         if (intent.getAction().equals(NotificationManagerCompat.ACTION_BIND_SIDE_CHANNEL)) {
     49             // Block side channel service connections if the current sdk has no need for
     50             // side channeling.
     51             if (Build.VERSION.SDK_INT > NotificationManagerCompat.MAX_SIDE_CHANNEL_SDK_VERSION) {
     52                 return null;
     53             }
     54             return new NotificationSideChannelStub();
     55         }
     56         return null;
     57     }
     58 
     59     /**
     60      * Handle a side-channeled notification being posted.
     61      */
     62     public abstract void notify(String packageName, int id, String tag, Notification notification);
     63 
     64     /**
     65      * Handle a side-channelled notification being cancelled.
     66      */
     67     public abstract void cancel(String packageName, int id, String tag);
     68 
     69     /**
     70      * Handle the side-channelled cancelling of all notifications for a package.
     71      */
     72     public abstract void cancelAll(String packageName);
     73 
     74     private class NotificationSideChannelStub extends INotificationSideChannel.Stub {
     75         NotificationSideChannelStub() {
     76         }
     77 
     78         @Override
     79         public void notify(String packageName, int id, String tag, Notification notification)
     80                 throws RemoteException {
     81             checkPermission(getCallingUid(), packageName);
     82             long idToken = clearCallingIdentity();
     83             try {
     84                 NotificationCompatSideChannelService.this.notify(packageName, id, tag, notification);
     85             } finally {
     86                 restoreCallingIdentity(idToken);
     87             }
     88         }
     89 
     90         @Override
     91         public void cancel(String packageName, int id, String tag) throws RemoteException {
     92             checkPermission(getCallingUid(), packageName);
     93             long idToken = clearCallingIdentity();
     94             try {
     95                 NotificationCompatSideChannelService.this.cancel(packageName, id, tag);
     96             } finally {
     97                 restoreCallingIdentity(idToken);
     98             }
     99         }
    100 
    101         @Override
    102         public void cancelAll(String packageName) {
    103             checkPermission(getCallingUid(), packageName);
    104             long idToken = clearCallingIdentity();
    105             try {
    106                 NotificationCompatSideChannelService.this.cancelAll(packageName);
    107             } finally {
    108                 restoreCallingIdentity(idToken);
    109             }
    110         }
    111     }
    112 
    113     void checkPermission(int callingUid, String packageName) {
    114         for (String validPackage : getPackageManager().getPackagesForUid(callingUid)) {
    115             if (validPackage.equals(packageName)) {
    116                 return;
    117             }
    118         }
    119         throw new SecurityException("NotificationSideChannelService: Uid " + callingUid
    120                 + " is not authorized for package " + packageName);
    121     }
    122 }
    123