support locale change for notification channels
[android/platform/frameworks/opt/telephony.git] / src / java / com / android / internal / telephony / util / NotificationChannelController.java
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 package com.android.internal.telephony.util;
17
18 import android.app.NotificationChannel;
19 import android.app.NotificationManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.media.AudioAttributes;
25 import android.provider.Settings;
26
27 import com.android.internal.R;
28
29 import java.util.Arrays;
30 import java.util.List;
31
32
33 public class NotificationChannelController {
34
35     /**
36      * list of {@link android.app.NotificationChannel} for telephony service.
37      */
38     public static final String CHANNEL_ID_ALERT = "alert";
39     public static final String CHANNEL_ID_CALL_FORWARD = "callForward";
40     public static final String CHANNEL_ID_MOBILE_DATA_ALERT = "mobileDataAlert";
41     public static final String CHANNEL_ID_SMS = "sms";
42     public static final String CHANNEL_ID_VOICE_MAIL = "voiceMail";
43     public static final String CHANNEL_ID_WFC = "wfc";
44
45     /**
46      * Creates all notification channels and registers with NotificationManager. If a channel
47      * with the same ID is already registered, NotificationManager will ignore this call.
48      */
49     private static void createAll(Context context) {
50         final NotificationChannel alertChannel = new NotificationChannel(
51                 CHANNEL_ID_ALERT,
52                 context.getText(R.string.notification_channel_network_alert),
53                 NotificationManager.IMPORTANCE_DEFAULT);
54         alertChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
55                 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
56
57         final NotificationChannel voiceMailChannel = new NotificationChannel(
58                 CHANNEL_ID_VOICE_MAIL,
59                 context.getText(R.string.notification_channel_voice_mail),
60                 NotificationManager.IMPORTANCE_DEFAULT);
61         voiceMailChannel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,
62                 new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
63
64         context.getSystemService(NotificationManager.class)
65                 .createNotificationChannels(Arrays.asList(
66                 new NotificationChannel(CHANNEL_ID_CALL_FORWARD,
67                         context.getText(R.string.notification_channel_call_forward),
68                         NotificationManager.IMPORTANCE_LOW),
69                 new NotificationChannel(CHANNEL_ID_MOBILE_DATA_ALERT,
70                         context.getText(R.string.notification_channel_mobile_data_alert),
71                         NotificationManager.IMPORTANCE_DEFAULT),
72                 new NotificationChannel(CHANNEL_ID_SMS,
73                         context.getText(R.string.notification_channel_sms),
74                         NotificationManager.IMPORTANCE_HIGH),
75                 new NotificationChannel(CHANNEL_ID_WFC,
76                         context.getText(R.string.notification_channel_wfc),
77                         NotificationManager.IMPORTANCE_LOW),
78                 alertChannel, voiceMailChannel));
79     }
80
81     public NotificationChannelController(Context context) {
82         context.registerReceiver(mLocaleChangeReceiver,
83                 new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
84         createAll(context);
85     }
86
87     // rename all registered channels on locale change
88     private BroadcastReceiver mLocaleChangeReceiver = new BroadcastReceiver() {
89         @Override
90         public void onReceive(Context context, Intent intent) {
91             createAll(context);
92         }
93     };
94 }