summaryrefslogtreecommitdiff
path: root/Tethering/src/com/android/networkstack/tethering/TetheringNotificationUpdater.java
blob: a0198cc9c12685f1c66be5149a7bbf42745ee6d2 (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
/*
 * Copyright (C) 2020 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.networkstack.tethering;

import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
import static android.text.TextUtils.isEmpty;

import android.app.Notification;
import android.app.Notification.Action;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.NetworkCapabilities;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.UserHandle;
import android.provider.Settings;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.SparseArray;

import androidx.annotation.DrawableRes;
import androidx.annotation.IntDef;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.internal.annotations.VisibleForTesting;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * A class to display tethering-related notifications.
 *
 * <p>This class is not thread safe, it is intended to be used only from the tethering handler
 * thread. However the constructor is an exception, as it is called on another thread ;
 * therefore for thread safety all members of this class MUST either be final or initialized
 * to their default value (0, false or null).
 *
 * @hide
 */
public class TetheringNotificationUpdater {
    private static final String TAG = TetheringNotificationUpdater.class.getSimpleName();
    private static final String CHANNEL_ID = "TETHERING_STATUS";
    private static final String WIFI_DOWNSTREAM = "WIFI";
    private static final String USB_DOWNSTREAM = "USB";
    private static final String BLUETOOTH_DOWNSTREAM = "BT";
    @VisibleForTesting
    static final String ACTION_DISABLE_TETHERING =
            "com.android.server.connectivity.tethering.DISABLE_TETHERING";
    private static final boolean NOTIFY_DONE = true;
    private static final boolean NO_NOTIFY = false;
    @VisibleForTesting
    static final int EVENT_SHOW_NO_UPSTREAM = 1;
    // Id to update and cancel restricted notification. Must be unique within the tethering app.
    @VisibleForTesting
    static final int RESTRICTED_NOTIFICATION_ID = 1001;
    // Id to update and cancel no upstream notification. Must be unique within the tethering app.
    @VisibleForTesting
    static final int NO_UPSTREAM_NOTIFICATION_ID = 1002;
    // Id to update and cancel roaming notification. Must be unique within the tethering app.
    @VisibleForTesting
    static final int ROAMING_NOTIFICATION_ID = 1003;
    @VisibleForTesting
    static final int NO_ICON_ID = 0;
    @VisibleForTesting
    static final int DOWNSTREAM_NONE = 0;
    // Refer to TelephonyManager#getSimCarrierId for more details about carrier id.
    @VisibleForTesting
    static final int VERIZON_CARRIER_ID = 1839;
    private final Context mContext;
    private final NotificationManager mNotificationManager;
    private final NotificationChannel mChannel;
    private final Handler mHandler;

    // WARNING : the constructor is called on a different thread. Thread safety therefore
    // relies on these values being initialized to 0, false or null, and not any other value. If you
    // need to change this, you will need to change the thread where the constructor is invoked, or
    // to introduce synchronization.
    // Downstream type is one of ConnectivityManager.TETHERING_* constants, 0 1 or 2.
    // This value has to be made 1 2 and 4, and OR'd with the others.
    private int mDownstreamTypesMask = DOWNSTREAM_NONE;
    private boolean mNoUpstream = false;
    private boolean mRoaming = false;

    // WARNING : this value is not able to being initialized to 0 and must have volatile because
    // telephony service is not guaranteed that is up before tethering service starts. If telephony
    // is up later than tethering, TetheringNotificationUpdater will use incorrect and valid
    // subscription id(0) to query resources. Therefore, initialized subscription id must be
    // INVALID_SUBSCRIPTION_ID.
    private volatile int mActiveDataSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(value = {
            RESTRICTED_NOTIFICATION_ID,
            NO_UPSTREAM_NOTIFICATION_ID,
            ROAMING_NOTIFICATION_ID
    })
    @interface NotificationId {}

    private static final class MccMncOverrideInfo {
        public final String visitedMccMnc;
        public final int homeMcc;
        public final int homeMnc;
        MccMncOverrideInfo(String visitedMccMnc, int mcc, int mnc) {
            this.visitedMccMnc = visitedMccMnc;
            this.homeMcc = mcc;
            this.homeMnc = mnc;
        }
    }

    private static final SparseArray<MccMncOverrideInfo> sCarrierIdToMccMnc = new SparseArray<>();

    static {
        sCarrierIdToMccMnc.put(VERIZON_CARRIER_ID, new MccMncOverrideInfo("20404", 311, 480));
    }

    public TetheringNotificationUpdater(@NonNull final Context context,
            @NonNull final Looper looper) {
        mContext = context;
        mNotificationManager = (NotificationManager) context.createContextAsUser(UserHandle.ALL, 0)
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mChannel = new NotificationChannel(
                CHANNEL_ID,
                context.getResources().getString(R.string.notification_channel_tethering_status),
                NotificationManager.IMPORTANCE_LOW);
        mNotificationManager.createNotificationChannel(mChannel);
        mHandler = new NotificationHandler(looper);
    }

    private class NotificationHandler extends Handler {
        NotificationHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case EVENT_SHOW_NO_UPSTREAM:
                    notifyTetheringNoUpstream();
                    break;
            }
        }
    }

    /** Called when downstream has changed */
    public void onDownstreamChanged(@IntRange(from = 0, to = 7) final int downstreamTypesMask) {
        updateActiveNotifications(
                mActiveDataSubId, downstreamTypesMask, mNoUpstream, mRoaming);
    }

    /** Called when active data subscription id changed */
    public void onActiveDataSubscriptionIdChanged(final int subId) {
        updateActiveNotifications(subId, mDownstreamTypesMask, mNoUpstream, mRoaming);
    }

    /** Called when upstream network capabilities changed */
    public void onUpstreamCapabilitiesChanged(@Nullable final NetworkCapabilities capabilities) {
        final boolean isNoUpstream = (capabilities == null);
        final boolean isRoaming = capabilities != null
                && !capabilities.hasCapability(NET_CAPABILITY_NOT_ROAMING);
        updateActiveNotifications(
                mActiveDataSubId, mDownstreamTypesMask, isNoUpstream, isRoaming);
    }

    @NonNull
    @VisibleForTesting
    final Handler getHandler() {
        return mHandler;
    }

    @NonNull
    @VisibleForTesting
    Resources getResourcesForSubId(@NonNull final Context context, final int subId) {
        final Resources res = SubscriptionManager.getResourcesForSubId(context, subId);
        final TelephonyManager tm =
                ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
                        .createForSubscriptionId(mActiveDataSubId);
        final int carrierId = tm.getSimCarrierId();
        final String mccmnc = tm.getSimOperator();
        final MccMncOverrideInfo overrideInfo = sCarrierIdToMccMnc.get(carrierId);
        if (overrideInfo != null && overrideInfo.visitedMccMnc.equals(mccmnc)) {
            // Re-configure MCC/MNC value to specific carrier to get right resources.
            final Configuration config = res.getConfiguration();
            config.mcc = overrideInfo.homeMcc;
            config.mnc = overrideInfo.homeMnc;
            return context.createConfigurationContext(config).getResources();
        }
        return res;
    }

    private void updateActiveNotifications(final int subId, final int downstreamTypes,
            final boolean noUpstream, final boolean isRoaming) {
        final boolean tetheringActiveChanged =
                (downstreamTypes == DOWNSTREAM_NONE) != (mDownstreamTypesMask == DOWNSTREAM_NONE);
        final boolean subIdChanged = subId != mActiveDataSubId;
        final boolean upstreamChanged = noUpstream != mNoUpstream;
        final boolean roamingChanged = isRoaming != mRoaming;
        final boolean updateAll = tetheringActiveChanged || subIdChanged;
        mActiveDataSubId = subId;
        mDownstreamTypesMask = downstreamTypes;
        mNoUpstream = noUpstream;
        mRoaming = isRoaming;

        if (updateAll || upstreamChanged) updateNoUpstreamNotification();
        if (updateAll || roamingChanged) updateRoamingNotification();
    }

    private void updateNoUpstreamNotification() {
        final boolean tetheringInactive = mDownstreamTypesMask == DOWNSTREAM_NONE;

        if (tetheringInactive || !mNoUpstream || setupNoUpstreamNotification() == NO_NOTIFY) {
            clearNotification(NO_UPSTREAM_NOTIFICATION_ID);
            mHandler.removeMessages(EVENT_SHOW_NO_UPSTREAM);
        }
    }

    private void updateRoamingNotification() {
        final boolean tetheringInactive = mDownstreamTypesMask == DOWNSTREAM_NONE;

        if (tetheringInactive || !mRoaming || setupRoamingNotification() == NO_NOTIFY) {
            clearNotification(ROAMING_NOTIFICATION_ID);
        }
    }

    @VisibleForTesting
    void tetheringRestrictionLifted() {
        clearNotification(RESTRICTED_NOTIFICATION_ID);
    }

    private void clearNotification(@NotificationId final int id) {
        mNotificationManager.cancel(null /* tag */, id);
    }

    @VisibleForTesting
    static String getSettingsPackageName(@NonNull final PackageManager pm) {
        final Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
        final ComponentName settingsComponent = settingsIntent.resolveActivity(pm);
        return settingsComponent != null
                ? settingsComponent.getPackageName() : "com.android.settings";
    }

    @VisibleForTesting
    void notifyTetheringDisabledByRestriction() {
        final Resources res = getResourcesForSubId(mContext, mActiveDataSubId);
        final String title = res.getString(R.string.disable_tether_notification_title);
        final String message = res.getString(R.string.disable_tether_notification_message);
        if (isEmpty(title) || isEmpty(message)) return;

        final PendingIntent pi = PendingIntent.getActivity(
                mContext.createContextAsUser(UserHandle.CURRENT, 0 /* flags */),
                0 /* requestCode */,
                new Intent(Settings.ACTION_TETHER_SETTINGS)
                        .setPackage(getSettingsPackageName(mContext.getPackageManager()))
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                PendingIntent.FLAG_IMMUTABLE,
                null /* options */);

        showNotification(R.drawable.stat_sys_tether_general, title, message,
                RESTRICTED_NOTIFICATION_ID, false /* ongoing */, pi, new Action[0]);
    }

    private void notifyTetheringNoUpstream() {
        final Resources res = getResourcesForSubId(mContext, mActiveDataSubId);
        final String title = res.getString(R.string.no_upstream_notification_title);
        final String message = res.getString(R.string.no_upstream_notification_message);
        final String disableButton =
                res.getString(R.string.no_upstream_notification_disable_button);
        if (isEmpty(title) || isEmpty(message) || isEmpty(disableButton)) return;

        final Intent intent = new Intent(ACTION_DISABLE_TETHERING);
        intent.setPackage(mContext.getPackageName());
        final PendingIntent pi = PendingIntent.getBroadcast(
                mContext.createContextAsUser(UserHandle.CURRENT, 0 /* flags */),
                0 /* requestCode */,
                intent,
                PendingIntent.FLAG_IMMUTABLE);
        final Action action = new Action.Builder(NO_ICON_ID, disableButton, pi).build();

        showNotification(R.drawable.stat_sys_tether_general, title, message,
                NO_UPSTREAM_NOTIFICATION_ID, true /* ongoing */, null /* pendingIntent */, action);
    }

    private boolean setupRoamingNotification() {
        final Resources res = getResourcesForSubId(mContext, mActiveDataSubId);
        final boolean upstreamRoamingNotification =
                res.getBoolean(R.bool.config_upstream_roaming_notification);

        if (!upstreamRoamingNotification) return NO_NOTIFY;

        final String title = res.getString(R.string.upstream_roaming_notification_title);
        final String message = res.getString(R.string.upstream_roaming_notification_message);
        if (isEmpty(title) || isEmpty(message)) return NO_NOTIFY;

        final PendingIntent pi = PendingIntent.getActivity(
                mContext.createContextAsUser(UserHandle.CURRENT, 0 /* flags */),
                0 /* requestCode */,
                new Intent(Settings.ACTION_TETHER_SETTINGS)
                        .setPackage(getSettingsPackageName(mContext.getPackageManager()))
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
                PendingIntent.FLAG_IMMUTABLE,
                null /* options */);

        showNotification(R.drawable.stat_sys_tether_general, title, message,
                ROAMING_NOTIFICATION_ID, true /* ongoing */, pi, new Action[0]);
        return NOTIFY_DONE;
    }

    private boolean setupNoUpstreamNotification() {
        final Resources res = getResourcesForSubId(mContext, mActiveDataSubId);
        final int delayToShowUpstreamNotification =
                res.getInteger(R.integer.delay_to_show_no_upstream_after_no_backhaul);

        if (delayToShowUpstreamNotification < 0) return NO_NOTIFY;

        mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_SHOW_NO_UPSTREAM),
                delayToShowUpstreamNotification);
        return NOTIFY_DONE;
    }

    private void showNotification(@DrawableRes final int iconId, @NonNull final String title,
            @NonNull final String message, @NotificationId final int id, final boolean ongoing,
            @Nullable PendingIntent pi, @NonNull final Action... actions) {
        final Notification notification =
                new Notification.Builder(mContext, mChannel.getId())
                        .setSmallIcon(iconId)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setOngoing(ongoing)
                        .setColor(mContext.getColor(
                                android.R.color.system_notification_accent_color))
                        .setVisibility(Notification.VISIBILITY_PUBLIC)
                        .setCategory(Notification.CATEGORY_STATUS)
                        .setContentIntent(pi)
                        .setActions(actions)
                        .build();

        mNotificationManager.notify(null /* tag */, id, notification);
    }
}