Home | History | Annotate | Download | only in notificationsender
      1 /*
      2  * Copyright (C) 2017 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.cts.managedprofiletests.notificationsender;
     18 
     19 import android.app.Activity;
     20 import android.app.Notification;
     21 import android.app.NotificationChannel;
     22 import android.app.NotificationManager;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.os.Bundle;
     26 import android.util.Log;
     27 
     28 import java.lang.Override;
     29 
     30 /**
     31  * A simple activity to post notifications to test notification
     32  * listener whitelists.
     33  */
     34 public class SendNotification extends Activity {
     35 
     36     private static final String TAG = "ListenerTest";
     37 
     38     static final int NOTIFICATION_ID = 98765;
     39     static final String NOTIFICATION_CHANNEL = "NotificationListenerTest";
     40 
     41     @Override
     42     public void onCreate(Bundle icicle) {
     43         super.onCreate(icicle);
     44         Intent intent = getIntent();
     45         if (intent != null && "POST_NOTIFICATION".equals(intent.getAction())) {
     46             Log.i(TAG, "posting from " + android.os.Process.myUserHandle());
     47             sendNotification();
     48         } else if (intent != null && "CANCEL_NOTIFICATION".equals(intent.getAction())) {
     49             Log.i(TAG, "cancelling from " + android.os.Process.myUserHandle());
     50             cancelNotification();
     51         }
     52         finish();
     53     }
     54 
     55     private void sendNotification() {
     56         NotificationManager notificationManager =
     57                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     58         notificationManager.createNotificationChannel(new NotificationChannel(
     59                         NOTIFICATION_CHANNEL, "Test channel", NotificationManager.IMPORTANCE_DEFAULT));
     60         notificationManager.notify(NOTIFICATION_ID,
     61                 new Notification.Builder(getApplicationContext(), NOTIFICATION_CHANNEL)
     62                 .setSmallIcon(R.raw.ic_contact_picture)
     63                 .setContentTitle("Test title")
     64                 .build());
     65     }
     66 
     67     private void cancelNotification() {
     68         NotificationManager notificationManager =
     69                 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     70         notificationManager.cancel(NOTIFICATION_ID);
     71         notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL);
     72     }
     73 }
     74