summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/notification/NotificationChannelManager.java
blob: ef0f5f17a7acbdba4db0ef1e0892d85aaa9fbcde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.dialer.notification;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build.VERSION_CODES;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StringDef;
import android.support.v4.os.BuildCompat;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import com.android.contacts.common.compat.TelephonyManagerCompat;
import com.android.dialer.buildtype.BuildType;
import com.android.dialer.common.LogUtil;
import com.android.dialer.common.concurrent.DialerExecutors;
import com.android.dialer.telecom.TelecomUtil;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Objects;

/** Contains info on how to create {@link NotificationChannel NotificationChannels} */
public class NotificationChannelManager {

  private static final String PREFS_FILENAME = "NotificationChannelManager";
  private static final String PREF_NEED_FIRST_INIT = "needFirstInit";
  private static NotificationChannelManager instance;

  public static NotificationChannelManager getInstance() {
    if (instance == null) {
      instance = new NotificationChannelManager();
    }
    return instance;
  }

  /**
   * Set the channel of notification appropriately. Will create the channel if it does not already
   * exist. Safe to call pre-O (will no-op).
   *
   * <p>phoneAccount should only be null if channelName is {@link Channel#DEFAULT} or {@link
   * Channel#MISSED_CALL} since these do not have account-specific settings.
   */
  @TargetApi(26)
  public static void applyChannel(
      @NonNull Notification.Builder notification,
      @NonNull Context context,
      @Channel String channelName,
      @Nullable PhoneAccountHandle phoneAccount) {
    checkNullity(channelName, phoneAccount);

    if (BuildCompat.isAtLeastO()) {
      NotificationChannel channel =
          NotificationChannelManager.getInstance().getChannel(context, channelName, phoneAccount);
      notification.setChannelId(channel.getId());
    }
  }

  private static void checkNullity(
      @Channel String channelName, @Nullable PhoneAccountHandle phoneAccount) {
    if (phoneAccount != null || channelAllowsNullPhoneAccountHandle(channelName)) {
      return;
    }

    // TODO (b/36568553): don't throw an exception once most cases have been identified
    IllegalArgumentException exception =
        new IllegalArgumentException(
            "Phone account handle must not be null on channel " + channelName);
    if (BuildType.get() == BuildType.RELEASE) {
      LogUtil.e("NotificationChannelManager.applyChannel", null, exception);
    } else {
      throw exception;
    }
  }

  private static boolean channelAllowsNullPhoneAccountHandle(@Channel String channelName) {
    switch (channelName) {
      case Channel.DEFAULT:
      case Channel.MISSED_CALL:
        return true;
      default:
        return false;
    }
  }

  /** The base Channel IDs for {@link NotificationChannel} */
  @Retention(RetentionPolicy.SOURCE)
  @StringDef({
    Channel.INCOMING_CALL,
    Channel.ONGOING_CALL,
    Channel.ONGOING_CALL_OLD,
    Channel.MISSED_CALL,
    Channel.VOICEMAIL,
    Channel.EXTERNAL_CALL,
    Channel.DEFAULT
  })
  public @interface Channel {
    @Deprecated String ONGOING_CALL_OLD = "ongoingCall";
    String INCOMING_CALL = "incomingCall";
    String ONGOING_CALL = "ongoingCall2";
    String MISSED_CALL = "missedCall";
    String VOICEMAIL = "voicemail";
    String EXTERNAL_CALL = "externalCall";
    String DEFAULT = "default";
  }

  @Channel
  private static final String[] prepopulatedAccountChannels =
      new String[] {Channel.INCOMING_CALL, Channel.ONGOING_CALL, Channel.VOICEMAIL};

  @Channel
  private static final String[] prepopulatedGlobalChannels =
      new String[] {Channel.MISSED_CALL, Channel.DEFAULT};

  private NotificationChannelManager() {}

  public void firstInitIfNeeded(@NonNull Context context) {
    if (BuildCompat.isAtLeastO()) {
      DialerExecutors.createNonUiTaskBuilder(this::firstInitIfNeededSync)
          .build()
          .executeSerial(context);
    }
  }

  private boolean firstInitIfNeededSync(@NonNull Context context) {
    if (needsFirstInit(context)) {
      initChannels(context);
      return true;
    }
    return false;
  }

  public boolean needsFirstInit(@NonNull Context context) {
    return (BuildCompat.isAtLeastO()
        && getSharedPreferences(context).getBoolean(PREF_NEED_FIRST_INIT, true));
  }

  @RequiresApi(VERSION_CODES.N)
  private SharedPreferences getSharedPreferences(@NonNull Context context) {
    // Use device protected storage since in some cases this will need to be accessed while device
    // is locked
    context = context.createDeviceProtectedStorageContext();
    return context.getSharedPreferences(PREFS_FILENAME, Context.MODE_PRIVATE);
  }

  @RequiresApi(26)
  public Intent getSettingsIntentForChannel(
      @NonNull Context context, @Channel String channelName, PhoneAccountHandle accountHandle) {
    checkNullity(channelName, accountHandle);
    Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
    intent.putExtra(
        Settings.EXTRA_CHANNEL_ID, getChannel(context, channelName, accountHandle).getId());
    intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
    return intent;
  }

  @TargetApi(26)
  @SuppressWarnings("AndroidApiChecker")
  public void initChannels(@NonNull Context context) {
    if (!BuildCompat.isAtLeastO()) {
      return;
    }
    LogUtil.enterBlock("NotificationChannelManager.initChannels");
    List<PhoneAccountHandle> phoneAccounts = TelecomUtil.getCallCapablePhoneAccounts(context);

    // Remove notification channels for PhoneAccounts that don't exist anymore
    NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
    List<NotificationChannelGroup> notificationChannelGroups =
        notificationManager.getNotificationChannelGroups();
    notificationChannelGroups
        .stream()
        .filter(group -> !idExists(group.getId(), phoneAccounts))
        .forEach(group -> deleteGroup(notificationManager, group));

    for (PhoneAccountHandle phoneAccountHandle : phoneAccounts) {
      for (@Channel String channel : prepopulatedAccountChannels) {
        getChannel(context, channel, phoneAccountHandle);
      }
    }

    for (@Channel String channel : prepopulatedGlobalChannels) {
      getChannel(context, channel, null);
    }
    getSharedPreferences(context).edit().putBoolean(PREF_NEED_FIRST_INIT, false).apply();
  }

  @TargetApi(26)
  private void deleteGroup(
      @NonNull NotificationManager notificationManager, @NonNull NotificationChannelGroup group) {
    for (NotificationChannel channel : group.getChannels()) {
      notificationManager.deleteNotificationChannel(channel.getId());
    }
    notificationManager.deleteNotificationChannelGroup(group.getId());
  }

  private boolean idExists(String id, List<PhoneAccountHandle> phoneAccountHandles) {
    for (PhoneAccountHandle handle : phoneAccountHandles) {
      if (Objects.equals(handle.getId(), id)) {
        return true;
      }
    }
    return false;
  }

  @NonNull
  @RequiresApi(26)
  private NotificationChannel getChannel(
      @NonNull Context context,
      @Channel String channelName,
      @Nullable PhoneAccountHandle phoneAccount) {
    String channelId = channelNameToId(channelName, phoneAccount);
    NotificationChannel channel = getNotificationManager(context).getNotificationChannel(channelId);
    if (channel == null) {
      channel = createChannel(context, channelName, phoneAccount);
    }
    return channel;
  }

  private static String channelNameToId(
      @Channel String name, @Nullable PhoneAccountHandle phoneAccountHandle) {
    if (phoneAccountHandle == null) {
      return name;
    } else {
      return name + ":" + phoneAccountHandle.getId();
    }
  }

  @RequiresApi(26)
  private NotificationChannel createChannel(
      Context context,
      @Channel String channelName,
      @Nullable PhoneAccountHandle phoneAccountHandle) {
    String channelId = channelNameToId(channelName, phoneAccountHandle);

    if (phoneAccountHandle != null) {
      PhoneAccount account = getTelecomManager(context).getPhoneAccount(phoneAccountHandle);
      NotificationChannelGroup group =
          new NotificationChannelGroup(
              phoneAccountHandle.getId(),
              (account == null) ? phoneAccountHandle.getId() : account.getLabel().toString());
      getNotificationManager(context)
          .createNotificationChannelGroup(group); // No-op if already exists
    } else if (!channelAllowsNullPhoneAccountHandle(channelName)) {
      LogUtil.w(
          "NotificationChannelManager.createChannel",
          "Null PhoneAccountHandle with channel " + channelName);
    }

    Uri silentRingtone = Uri.EMPTY;

    CharSequence name;
    int importance;
    boolean canShowBadge;
    boolean lights;
    boolean vibration;
    Uri sound;
    switch (channelName) {
      case Channel.INCOMING_CALL:
        name = context.getText(R.string.notification_channel_incoming_call);
        importance = NotificationManager.IMPORTANCE_MAX;
        canShowBadge = false;
        lights = true;
        vibration = false;
        sound = silentRingtone;
        break;
      case Channel.MISSED_CALL:
        name = context.getText(R.string.notification_channel_missed_call);
        importance = NotificationManager.IMPORTANCE_DEFAULT;
        canShowBadge = true;
        lights = true;
        vibration = true;
        sound = silentRingtone;
        break;
      case Channel.ONGOING_CALL:
        name = context.getText(R.string.notification_channel_ongoing_call);
        importance = NotificationManager.IMPORTANCE_DEFAULT;
        canShowBadge = false;
        lights = false;
        vibration = false;
        sound = silentRingtone;
        deleteOldOngoingCallChannelIfNeeded(context, phoneAccountHandle);
        break;
      case Channel.VOICEMAIL:
        name = context.getText(R.string.notification_channel_voicemail);
        importance = NotificationManager.IMPORTANCE_DEFAULT;
        canShowBadge = true;
        lights = true;
        vibration = true;
        sound =
            TelephonyManagerCompat.getVoicemailRingtoneUri(
                getTelephonyManager(context), phoneAccountHandle);
        break;
      case Channel.EXTERNAL_CALL:
        name = context.getText(R.string.notification_channel_external_call);
        importance = NotificationManager.IMPORTANCE_HIGH;
        canShowBadge = false;
        lights = true;
        vibration = true;
        sound = null;
        break;
      case Channel.DEFAULT:
        name = context.getText(R.string.notification_channel_misc);
        importance = NotificationManager.IMPORTANCE_DEFAULT;
        canShowBadge = false;
        lights = true;
        vibration = true;
        sound = null;
        break;
      default:
        throw new IllegalArgumentException("Unknown channel: " + channelName);
    }

    NotificationChannel channel = new NotificationChannel(channelId, name, importance);
    channel.setShowBadge(canShowBadge);
    if (sound != null) {
      channel.setSound(
          sound,
          new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
    }
    channel.enableLights(lights);
    channel.enableVibration(vibration);
    getNotificationManager(context).createNotificationChannel(channel);
    return channel;
  }

  @RequiresApi(26)
  private void deleteOldOngoingCallChannelIfNeeded(
      @NonNull Context context, PhoneAccountHandle phoneAccountHandle) {
    String channelId = channelNameToId(Channel.ONGOING_CALL_OLD, phoneAccountHandle);
    NotificationManager notificationManager = getNotificationManager(context);
    NotificationChannel channel = notificationManager.getNotificationChannel(channelId);
    if (channel != null) {
      LogUtil.i(
          "NotificationManager.deleteOldOngoingCallChannelIfNeeded",
          "Old ongoing channel found. Deleting to create new channel");
      notificationManager.deleteNotificationChannel(channel.getId());
    }
  }

  private static NotificationManager getNotificationManager(@NonNull Context context) {
    return context.getSystemService(NotificationManager.class);
  }

  private static TelephonyManager getTelephonyManager(@NonNull Context context) {
    return context.getSystemService(TelephonyManager.class);
  }

  private static TelecomManager getTelecomManager(@NonNull Context context) {
    return context.getSystemService(TelecomManager.class);
  }
}