File: WebLayerNotificationChannels.java

package info (click to toggle)
chromium 90.0.4430.212-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,450,632 kB
  • sloc: cpp: 19,832,434; javascript: 2,948,838; ansic: 2,312,399; python: 1,464,622; xml: 584,121; java: 514,189; asm: 470,557; objc: 83,463; perl: 77,861; sh: 77,030; cs: 70,789; fortran: 24,137; tcl: 18,916; php: 18,872; makefile: 16,848; ruby: 16,721; pascal: 13,150; sql: 10,199; yacc: 7,507; lex: 1,313; lisp: 840; awk: 329; jsp: 39; sed: 19
file content (175 lines) | stat: -rw-r--r-- 7,083 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.weblayer_private;

import android.annotation.TargetApi;
import android.app.NotificationManager;
import android.content.SharedPreferences;
import android.os.Build;

import androidx.annotation.StringDef;

import org.chromium.base.ContextUtils;
import org.chromium.components.browser_ui.notifications.NotificationManagerProxyImpl;
import org.chromium.components.browser_ui.notifications.channels.ChannelDefinitions;
import org.chromium.components.browser_ui.notifications.channels.ChannelDefinitions.PredefinedChannel;
import org.chromium.components.browser_ui.notifications.channels.ChannelsInitializer;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/** Defines notification channels for WebLayer. */
@TargetApi(Build.VERSION_CODES.O)
public class WebLayerNotificationChannels extends ChannelDefinitions {
    /**
     * Version number identifying the current set of channels. This must be incremented whenever the
     * set of channels returned by {@link #getStartupChannelIds()} or {@link #getLegacyChannelIds()}
     * changes.
     */
    static final int sChannelsVersion = 0;
    static final String sChannelsVersionKey = "org.chromium.weblayer.notification_channels_version";

    private static class LazyHolder {
        private static WebLayerNotificationChannels sInstance = new WebLayerNotificationChannels();
    }

    public static WebLayerNotificationChannels getInstance() {
        return LazyHolder.sInstance;
    }

    private WebLayerNotificationChannels() {}

    /**
     * To define a new channel, add the channel ID to this StringDef and add a new entry to
     * PredefinedChannels.MAP below with the appropriate channel parameters. To remove an existing
     * channel, remove the ID from this StringDef, remove its entry from Predefined Channels.MAP,
     * and add it to the return value of {@link #getLegacyChannelIds()}.
     */
    @StringDef({ChannelId.ACTIVE_DOWNLOADS, ChannelId.COMPLETED_DOWNLOADS, ChannelId.MEDIA_PLAYBACK,
            ChannelId.WEBRTC_CAM_AND_MIC})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ChannelId {
        String ACTIVE_DOWNLOADS = "org.chromium.weblayer.active_downloads";
        String COMPLETED_DOWNLOADS = "org.chromium.weblayer.completed_downloads";
        String MEDIA_PLAYBACK = "org.chromium.weblayer.media_playback";
        String WEBRTC_CAM_AND_MIC = "org.chromium.weblayer.webrtc_cam_and_mic";
    }

    @StringDef({ChannelGroupId.WEBLAYER})
    @Retention(RetentionPolicy.SOURCE)
    public @interface ChannelGroupId {
        String WEBLAYER = "org.chromium.weblayer";
    }

    // Map defined in static inner class so it's only initialized lazily.
    @TargetApi(Build.VERSION_CODES.N) // for NotificationManager.IMPORTANCE_* constants
    private static class PredefinedChannels {
        static final Map<String, PredefinedChannel> MAP;

        static {
            Map<String, PredefinedChannel> map = new HashMap<>();
            map.put(ChannelId.ACTIVE_DOWNLOADS,
                    PredefinedChannel.create(ChannelId.ACTIVE_DOWNLOADS,
                            R.string.notification_category_downloads,
                            NotificationManager.IMPORTANCE_LOW, ChannelGroupId.WEBLAYER));
            map.put(ChannelId.COMPLETED_DOWNLOADS,
                    PredefinedChannel.create(ChannelId.COMPLETED_DOWNLOADS,
                            R.string.notification_category_completed_downloads,
                            NotificationManager.IMPORTANCE_LOW, ChannelGroupId.WEBLAYER));
            map.put(ChannelId.MEDIA_PLAYBACK,
                    PredefinedChannel.create(ChannelId.MEDIA_PLAYBACK,
                            R.string.notification_category_media_playback,
                            NotificationManager.IMPORTANCE_LOW, ChannelGroupId.WEBLAYER));
            map.put(ChannelId.WEBRTC_CAM_AND_MIC,
                    PredefinedChannel.create(ChannelId.WEBRTC_CAM_AND_MIC,
                            R.string.notification_category_webrtc_cam_and_mic,
                            NotificationManager.IMPORTANCE_LOW, ChannelGroupId.WEBLAYER));
            MAP = Collections.unmodifiableMap(map);
        }
    }

    // Map defined in static inner class so it's only initialized lazily.
    private static class PredefinedChannelGroups {
        static final Map<String, PredefinedChannelGroup> MAP;
        static {
            Map<String, PredefinedChannelGroup> map = new HashMap<>();
            map.put(ChannelGroupId.WEBLAYER,
                    new PredefinedChannelGroup(ChannelGroupId.WEBLAYER,
                            R.string.weblayer_notification_channel_group_name));
            MAP = Collections.unmodifiableMap(map);
        }
    }

    @Override
    public Set<String> getAllChannelGroupIds() {
        return PredefinedChannelGroups.MAP.keySet();
    }

    @Override
    public Set<String> getAllChannelIds() {
        return PredefinedChannels.MAP.keySet();
    }

    @Override
    public Set<String> getStartupChannelIds() {
        return Collections.emptySet();
    }

    @Override
    public List<String> getLegacyChannelIds() {
        return Collections.emptyList();
    }

    @Override
    public PredefinedChannelGroup getChannelGroup(@ChannelGroupId String groupId) {
        return PredefinedChannelGroups.MAP.get(groupId);
    }

    @Override
    public PredefinedChannel getChannelFromId(@ChannelId String channelId) {
        return PredefinedChannels.MAP.get(channelId);
    }

    /**
     * Updates the user-facing channel names after a locale switch.
     */
    public static void onLocaleChanged() {
        if (!isAtLeastO()) return;

        getChannelsInitializer().updateLocale(ContextUtils.getApplicationContext().getResources());
    }

    /**
     * Updates the registered channels based on {@link sChannelsVersion}.
     */
    public static void updateChannelsIfNecessary() {
        if (!isAtLeastO()) return;

        SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
        if (prefs.getInt(sChannelsVersionKey, -1) == sChannelsVersion) return;

        ChannelsInitializer initializer = getChannelsInitializer();
        initializer.deleteLegacyChannels();
        initializer.initializeStartupChannels();
        prefs.edit().putInt(sChannelsVersionKey, sChannelsVersion).apply();
    }

    private static boolean isAtLeastO() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
    }

    private static ChannelsInitializer getChannelsInitializer() {
        assert isAtLeastO();

        return new ChannelsInitializer(
                new NotificationManagerProxyImpl(ContextUtils.getApplicationContext()),
                getInstance(), ContextUtils.getApplicationContext().getResources());
    }
}