summaryrefslogtreecommitdiff
path: root/libs/WifiTrackerLib/src/com/android/wifitrackerlib/MergedCarrierEntry.java
blob: 413a1ee2d8b749d8ab912924dee1a8702bd1db9f (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
/*
 * 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.wifitrackerlib;

import static android.net.wifi.WifiInfo.DEFAULT_MAC_ADDRESS;
import static android.net.wifi.WifiInfo.sanitizeSsid;

import static com.android.wifitrackerlib.Utils.getVerboseLoggingDescription;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;

import java.util.StringJoiner;

/**
 * WifiEntry representation of merged carrier network, uniquely identified by subscription id.
 */
public class MergedCarrierEntry extends WifiEntry {
    static final String KEY_PREFIX = "MergedCarrierEntry:";

    private final int mSubscriptionId;
    @NonNull private final String mKey;
    @NonNull private final Context mContext;
    boolean mIsCellDefaultRoute;

    MergedCarrierEntry(@NonNull Handler callbackHandler,
            @NonNull WifiManager wifiManager,
            boolean forSavedNetworksPage,
            @NonNull Context context,
            int subscriptionId) throws IllegalArgumentException {
        super(callbackHandler, wifiManager, forSavedNetworksPage);
        mContext = context;
        mSubscriptionId = subscriptionId;
        mKey = KEY_PREFIX + subscriptionId;
    }

    @Override
    public String getKey() {
        return mKey;
    }

    @Override
    public String getSummary(boolean concise) {
        StringJoiner sj = new StringJoiner(mContext.getString(
                R.string.wifitrackerlib_summary_separator));
        if (!concise) {
            final String verboseLoggingDescription = getVerboseLoggingDescription(this);
            if (!TextUtils.isEmpty(verboseLoggingDescription)) {
                sj.add(verboseLoggingDescription);
            }
        }
        return sj.toString();
    }

    @Override
    public synchronized String getSsid() {
        if (mWifiInfo != null) {
            return sanitizeSsid(mWifiInfo.getSSID());
        }
        return null;
    }

    @Override
    public synchronized String getMacAddress() {
        if (mWifiInfo != null) {
            final String wifiInfoMac = mWifiInfo.getMacAddress();
            if (!TextUtils.isEmpty(wifiInfoMac)
                    && !TextUtils.equals(wifiInfoMac, DEFAULT_MAC_ADDRESS)) {
                return wifiInfoMac;
            }
        }
        return null;
    }

    @Override
    public synchronized boolean canConnect() {
        return getConnectedState() == CONNECTED_STATE_DISCONNECTED && !mIsCellDefaultRoute;
    }

    /**
     * Connect to this merged carrier network and show the "Wi-Fi won't autoconnect for now" toast.
     * @param callback callback for the connect result
     */
    @Override
    public synchronized void connect(@Nullable ConnectCallback callback) {
        connect(callback, true);
    }

    /**
     * Connect to this merged carrier network.
     * @param callback callback for the connect result
     * @param showToast show the "Wi-Fi won't autoconnect for now" toast if {@code true}
     */
    public synchronized void connect(@Nullable ConnectCallback callback, boolean showToast) {
        mConnectCallback = callback;
        mWifiManager.startRestrictingAutoJoinToSubscriptionId(mSubscriptionId);
        if (showToast) {
            Toast.makeText(mContext, R.string.wifitrackerlib_wifi_wont_autoconnect_for_now,
                    Toast.LENGTH_SHORT).show();
        }
        if (mConnectCallback != null) {
            mCallbackHandler.post(() -> {
                final ConnectCallback connectCallback = mConnectCallback;
                if (connectCallback != null) {
                    connectCallback.onConnectResult(ConnectCallback.CONNECT_STATUS_SUCCESS);
                }
            });
        }
    }

    @Override
    public boolean canDisconnect() {
        return getConnectedState() == CONNECTED_STATE_CONNECTED;
    }

    @Override
    public synchronized void disconnect(@Nullable DisconnectCallback callback) {
        mDisconnectCallback = callback;
        mWifiManager.stopRestrictingAutoJoinToSubscriptionId();
        mWifiManager.startScan();
        if (mDisconnectCallback != null) {
            mCallbackHandler.post(() -> {
                final DisconnectCallback disconnectCallback = mDisconnectCallback;
                if (disconnectCallback != null) {
                    disconnectCallback.onDisconnectResult(
                            DisconnectCallback.DISCONNECT_STATUS_SUCCESS);
                }
            });
        }
    }

    @WorkerThread
    protected boolean connectionInfoMatches(@NonNull WifiInfo wifiInfo) {
        return wifiInfo.isCarrierMerged() && mSubscriptionId == wifiInfo.getSubscriptionId();
    }

    /** Returns whether or not carrier network offload is enabled for this subscription **/
    public boolean isEnabled() {
        return mWifiManager.isCarrierNetworkOffloadEnabled(mSubscriptionId, true);
    }

    /** Enables/disables the carrier network */
    public void setEnabled(boolean enabled) {
        mWifiManager.setCarrierNetworkOffloadEnabled(mSubscriptionId, true, enabled);
        if (!enabled) {
            mWifiManager.stopRestrictingAutoJoinToSubscriptionId();
            mWifiManager.startScan();
        }
    }

    /* package */ int getSubscriptionId() {
        return mSubscriptionId;
    }

    /* package */ synchronized void updateIsCellDefaultRoute(boolean isCellDefaultRoute) {
        mIsCellDefaultRoute = isCellDefaultRoute;
        notifyOnUpdated();
    }
}