Home | History | Annotate | Download | only in ui
      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.server.telecom.ui;
     18 
     19 import android.app.NotificationChannel;
     20 import android.app.NotificationManager;
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.IntentFilter;
     25 import android.media.AudioAttributes;
     26 import android.net.Uri;
     27 import android.telecom.Log;
     28 
     29 import com.android.server.telecom.R;
     30 
     31 /**
     32  * Manages the {@link android.app.NotificationChannel}s for Telecom.
     33  */
     34 public class NotificationChannelManager {
     35     public static final String CHANNEL_ID_NAME = "Telecom-";
     36 
     37     public static final String CHANNEL_ID_MISSED_CALLS = "TelecomMissedCalls";
     38     public static final String CHANNEL_ID_INCOMING_CALLS = "TelecomIncomingCalls";
     39 
     40     private BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() {
     41         @Override
     42         public void onReceive(Context context, Intent intent) {
     43             Log.i(this, "Locale change; recreating channels.");
     44             createOrUpdateAll(context);
     45         }
     46     };
     47 
     48     public void createChannels(Context context) {
     49         context.registerReceiver(mLocaleChangeReceiver,
     50                 new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
     51 
     52         createOrUpdateAll(context);
     53     }
     54 
     55     private void createOrUpdateAll(Context context) {
     56         createOrUpdateChannel(context, CHANNEL_ID_MISSED_CALLS);
     57         createOrUpdateChannel(context, CHANNEL_ID_INCOMING_CALLS);
     58     }
     59 
     60     private void createOrUpdateChannel(Context context, String channelId) {
     61         NotificationChannel channel = createChannel(context, channelId);
     62         getNotificationManager(context).createNotificationChannel(channel);
     63     }
     64 
     65     private NotificationChannel createChannel(Context context, String channelId) {
     66         Uri silentRingtone = Uri.parse("");
     67 
     68         CharSequence name = "";
     69         int importance = NotificationManager.IMPORTANCE_DEFAULT;
     70         boolean canShowBadge = false;
     71         boolean lights = false;
     72         boolean vibration = false;
     73         Uri sound = silentRingtone;
     74         switch (channelId) {
     75             case CHANNEL_ID_INCOMING_CALLS:
     76                 name = context.getText(R.string.notification_channel_incoming_call);
     77                 importance = NotificationManager.IMPORTANCE_MAX;
     78                 canShowBadge = false;
     79                 lights = true;
     80                 vibration = false;
     81                 sound = silentRingtone;
     82                 break;
     83             case CHANNEL_ID_MISSED_CALLS:
     84                 name = context.getText(R.string.notification_channel_missed_call);
     85                 importance = NotificationManager.IMPORTANCE_DEFAULT;
     86                 canShowBadge = true;
     87                 lights = true;
     88                 vibration = true;
     89                 sound = silentRingtone;
     90                 break;
     91         }
     92 
     93         NotificationChannel channel = new NotificationChannel(channelId, name, importance);
     94         channel.setShowBadge(canShowBadge);
     95         if (sound != null) {
     96             channel.setSound(
     97                     sound,
     98                     new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION)
     99                             .build());
    100         }
    101         channel.enableLights(lights);
    102         channel.enableVibration(vibration);
    103         return channel;
    104     }
    105 
    106     private NotificationManager getNotificationManager(Context context) {
    107         return context.getSystemService(NotificationManager.class);
    108     }
    109 }
    110