summaryrefslogtreecommitdiff
path: root/libs/WifiTrackerLib/src/com/android/wifitrackerlib/HotspotNetworkEntry.java
blob: 0495fef9c9ad46f00786774000d1ccd8b7ec7833 (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (C) 2023 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.os.Build.VERSION_CODES;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.sharedconnectivity.app.HotspotNetwork;
import android.net.wifi.sharedconnectivity.app.HotspotNetworkConnectionStatus;
import android.net.wifi.sharedconnectivity.app.NetworkProviderInfo;
import android.net.wifi.sharedconnectivity.app.SharedConnectivityManager;
import android.os.Handler;
import android.text.BidiFormatter;
import android.util.Log;

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

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Objects;

/**
 * WifiEntry representation of a Hotspot Network provided via {@link SharedConnectivityManager}.
 */
@TargetApi(VERSION_CODES.UPSIDE_DOWN_CAKE)
public class HotspotNetworkEntry extends WifiEntry {
    static final String TAG = "HotspotNetworkEntry";
    public static final String KEY_PREFIX = "HotspotNetworkEntry:";

    @NonNull private final WifiTrackerInjector mInjector;
    @NonNull private final Context mContext;
    @Nullable private final SharedConnectivityManager mSharedConnectivityManager;

    @Nullable private HotspotNetwork mHotspotNetworkData;
    @NonNull private HotspotNetworkEntryKey mKey;

    private boolean mServerInitiatedConnection = false;

    /**
     * If editing this IntDef also edit the definition in:
     * {@link android.net.wifi.sharedconnectivity.app.HotspotNetwork}
     *
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            HotspotNetwork.NETWORK_TYPE_UNKNOWN,
            HotspotNetwork.NETWORK_TYPE_CELLULAR,
            HotspotNetwork.NETWORK_TYPE_WIFI,
            HotspotNetwork.NETWORK_TYPE_ETHERNET
    })
    public @interface NetworkType {} // TODO(b/271868642): Add IfThisThanThat lint

    /**
     * If editing this IntDef also edit the definition in:
     * {@link android.net.wifi.sharedconnectivity.app.NetworkProviderInfo}
     *
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            NetworkProviderInfo.DEVICE_TYPE_UNKNOWN,
            NetworkProviderInfo.DEVICE_TYPE_PHONE,
            NetworkProviderInfo.DEVICE_TYPE_TABLET,
            NetworkProviderInfo.DEVICE_TYPE_LAPTOP,
            NetworkProviderInfo.DEVICE_TYPE_WATCH,
            NetworkProviderInfo.DEVICE_TYPE_AUTO
    })
    public @interface DeviceType {} // TODO(b/271868642): Add IfThisThanThat lint

    /**
     * If editing this IntDef also edit the definition in:
     * {@link android.net.wifi.sharedconnectivity.app.HotspotNetworkConnectionStatus}
     *
     * @hide
     */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_UNKNOWN,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_ENABLING_HOTSPOT,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_UNKNOWN_ERROR,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_PROVISIONING_FAILED,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_TETHERING_TIMEOUT,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_TETHERING_UNSUPPORTED,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_NO_CELL_DATA,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_ENABLING_HOTSPOT_FAILED,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_ENABLING_HOTSPOT_TIMEOUT,
            HotspotNetworkConnectionStatus.CONNECTION_STATUS_CONNECT_TO_HOTSPOT_FAILED,
    })
    public @interface ConnectionStatus {} // TODO(b/271868642): Add IfThisThanThat lint

    /**
     * Create a HotspotNetworkEntry from HotspotNetwork data.
     */
    HotspotNetworkEntry(
            @NonNull WifiTrackerInjector injector,
            @NonNull Context context, @NonNull Handler callbackHandler,
            @NonNull WifiManager wifiManager,
            @Nullable SharedConnectivityManager sharedConnectivityManager,
            @NonNull HotspotNetwork hotspotNetworkData) {
        super(callbackHandler, wifiManager, false /*forSavedNetworksPage*/);
        mInjector = injector;
        mContext = context;
        mSharedConnectivityManager = sharedConnectivityManager;
        mHotspotNetworkData = hotspotNetworkData;
        mKey = new HotspotNetworkEntryKey(hotspotNetworkData);
    }

    /**
     * Create a HotspotNetworkEntry from HotspotNetworkEntryKey.
     */
    HotspotNetworkEntry(
            @NonNull WifiTrackerInjector injector,
            @NonNull Context context, @NonNull Handler callbackHandler,
            @NonNull WifiManager wifiManager,
            @Nullable SharedConnectivityManager sharedConnectivityManager,
            @NonNull HotspotNetworkEntryKey key) {
        super(callbackHandler, wifiManager, false /*forSavedNetworksPage*/);
        mInjector = injector;
        mContext = context;
        mSharedConnectivityManager = sharedConnectivityManager;
        mHotspotNetworkData = null;
        mKey = key;
    }

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

    public HotspotNetworkEntryKey getHotspotNetworkEntryKey() {
        return mKey;
    }

    /**
     * Updates the hotspot data for this entry. Creates a new key when called.
     *
     * @param hotspotNetworkData An updated data set from SharedConnectivityService.
     */
    @WorkerThread
    protected synchronized void updateHotspotNetworkData(
            @NonNull HotspotNetwork hotspotNetworkData) {
        mHotspotNetworkData = hotspotNetworkData;
        mKey = new HotspotNetworkEntryKey(hotspotNetworkData);
        notifyOnUpdated();
    }

    @WorkerThread
    protected synchronized boolean connectionInfoMatches(@NonNull WifiInfo wifiInfo) {
        if (mKey.isVirtualEntry()) {
            return false;
        }
        return Objects.equals(mKey.getBssid(), wifiInfo.getBSSID());
    }

    @Override
    public String getTitle() {
        if (mHotspotNetworkData == null) {
            return "";
        }
        return mHotspotNetworkData.getNetworkProviderInfo().getDeviceName();
    }

    @Override
    public String getSummary(boolean concise) {
        if (mHotspotNetworkData == null) {
            return "";
        }
        if (getConnectedState() != CONNECTED_STATE_CONNECTED && mServerInitiatedConnection) {
            return mContext.getString(R.string.wifitrackerlib_hotspot_network_connecting);
        }
        return mContext.getString(R.string.wifitrackerlib_hotspot_network_summary,
                BidiFormatter.getInstance().unicodeWrap(mHotspotNetworkData.getNetworkName()),
                BidiFormatter.getInstance().unicodeWrap(
                        mHotspotNetworkData.getNetworkProviderInfo().getModelName()));
    }

    /**
     * Alternate summary string to be used on Network & internet page.
     *
     * @return Display string.
     */
    public String getAlternateSummary() {
        if (mHotspotNetworkData == null) {
            return "";
        }
        return mContext.getString(R.string.wifitrackerlib_hotspot_network_alternate,
                BidiFormatter.getInstance().unicodeWrap(mHotspotNetworkData.getNetworkName()),
                BidiFormatter.getInstance().unicodeWrap(
                        mHotspotNetworkData.getNetworkProviderInfo().getDeviceName()));
    }

    /**
     * Connection strength between the host device and the internet.
     *
     * @return Displayed connection strength in the range 0 to 4.
     */
    @IntRange(from = 0, to = 4)
    public int getUpstreamConnectionStrength() {
        if (mHotspotNetworkData == null) {
            return 0;
        }
        return mHotspotNetworkData.getNetworkProviderInfo().getConnectionStrength();
    }

    /**
     * Network type used by the host device to connect to the internet.
     *
     * @return NetworkType enum.
     */
    @NetworkType
    public int getNetworkType() {
        if (mHotspotNetworkData == null) {
            return HotspotNetwork.NETWORK_TYPE_UNKNOWN;
        }
        return mHotspotNetworkData.getHostNetworkType();
    }

    /**
     * Device type of the host device.
     *
     * @return DeviceType enum.
     */
    @DeviceType
    public int getDeviceType() {
        if (mHotspotNetworkData == null) {
            return NetworkProviderInfo.DEVICE_TYPE_UNKNOWN;
        }
        return mHotspotNetworkData.getNetworkProviderInfo().getDeviceType();
    }

    /**
     * The battery percentage of the host device.
     */
    @IntRange(from = 0, to = 100)
    public int getBatteryPercentage() {
        if (mHotspotNetworkData == null) {
            return 0;
        }
        return mHotspotNetworkData.getNetworkProviderInfo().getBatteryPercentage();
    }

    /**
     * If the host device is currently charging its battery.
     */
    public boolean isBatteryCharging() {
        if (mHotspotNetworkData == null) {
            return false;
        }
        return mHotspotNetworkData.getExtras().getBoolean("is_battery_charging", false);
    }

    @Override
    public boolean canConnect() {
        return getConnectedState() == CONNECTED_STATE_DISCONNECTED;
    }

    @Override
    public void connect(@Nullable ConnectCallback callback) {
        mConnectCallback = callback;
        if (mSharedConnectivityManager == null) {
            if (callback != null) {
                mCallbackHandler.post(() -> callback.onConnectResult(
                        ConnectCallback.CONNECT_STATUS_FAILURE_UNKNOWN));
            }
            return;
        }
        mSharedConnectivityManager.connectHotspotNetwork(mHotspotNetworkData);
    }

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

    @Override
    public void disconnect(@Nullable DisconnectCallback callback) {
        mDisconnectCallback = callback;
        if (mSharedConnectivityManager == null) {
            if (callback != null) {
                mCallbackHandler.post(() -> callback.onDisconnectResult(
                        DisconnectCallback.DISCONNECT_STATUS_FAILURE_UNKNOWN));
            }
            return;
        }
        mSharedConnectivityManager.disconnectHotspotNetwork(mHotspotNetworkData);
    }

    @Nullable
    public HotspotNetwork getHotspotNetworkData() {
        return mHotspotNetworkData;
    }

    /**
     * Trigger ConnectCallback with data from SharedConnectivityService.
     * @param status HotspotNetworkConnectionStatus#ConnectionStatus enum.
     */
    public void onConnectionStatusChanged(@ConnectionStatus int status) {
        if (mConnectCallback == null) return;
        switch (status) {
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_ENABLING_HOTSPOT:
                mServerInitiatedConnection = true;
                notifyOnUpdated();
                break;
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_UNKNOWN_ERROR:
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_PROVISIONING_FAILED:
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_TETHERING_TIMEOUT:
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_TETHERING_UNSUPPORTED:
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_NO_CELL_DATA:
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_ENABLING_HOTSPOT_FAILED:
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_ENABLING_HOTSPOT_TIMEOUT:
            case HotspotNetworkConnectionStatus.CONNECTION_STATUS_CONNECT_TO_HOTSPOT_FAILED:
                mCallbackHandler.post(() -> mConnectCallback.onConnectResult(
                        ConnectCallback.CONNECT_STATUS_FAILURE_UNKNOWN));
                mServerInitiatedConnection = false;
                notifyOnUpdated();
                break;
            default:
                // Do nothing
        }
    }

    static class HotspotNetworkEntryKey {
        private static final String KEY_IS_VIRTUAL_ENTRY_KEY = "IS_VIRTUAL_ENTRY_KEY";
        private static final String KEY_DEVICE_ID_KEY = "DEVICE_ID_KEY";
        private static final String KEY_BSSID_KEY = "BSSID_KEY";
        private static final String KEY_SCAN_RESULT_KEY = "SCAN_RESULT_KEY";

        private boolean mIsVirtualEntry;
        private long mDeviceId;
        @Nullable
        private String mBssid;
        @Nullable
        private StandardWifiEntry.ScanResultKey mScanResultKey;

        /**
         * Creates a HotspotNetworkEntryKey based on a {@link HotspotNetwork} parcelable object.
         *
         * @param hotspotNetworkData A {@link HotspotNetwork} object from SharedConnectivityService.
         */
        HotspotNetworkEntryKey(@NonNull HotspotNetwork hotspotNetworkData) {
            mDeviceId = hotspotNetworkData.getDeviceId();
            if (hotspotNetworkData.getHotspotSsid() == null
                    || (hotspotNetworkData.getHotspotBssid() == null)
                    || (hotspotNetworkData.getHotspotSecurityTypes() == null)) {
                mIsVirtualEntry = true;
                mBssid = null;
                mScanResultKey = null;
            } else {
                mIsVirtualEntry = false;
                mBssid = hotspotNetworkData.getHotspotBssid();
                mScanResultKey = new StandardWifiEntry.ScanResultKey(
                        hotspotNetworkData.getHotspotSsid(),
                        new ArrayList<>(hotspotNetworkData.getHotspotSecurityTypes()));
            }
        }

        /**
         * Creates a HotspotNetworkEntryKey from its String representation.
         */
        HotspotNetworkEntryKey(@NonNull String string) {
            mScanResultKey = null;
            if (!string.startsWith(KEY_PREFIX)) {
                Log.e(TAG, "String key does not start with key prefix!");
                return;
            }
            try {
                final JSONObject keyJson = new JSONObject(
                        string.substring(KEY_PREFIX.length()));
                if (keyJson.has(KEY_IS_VIRTUAL_ENTRY_KEY)) {
                    mIsVirtualEntry = keyJson.getBoolean(KEY_IS_VIRTUAL_ENTRY_KEY);
                }
                if (keyJson.has(KEY_DEVICE_ID_KEY)) {
                    mDeviceId = keyJson.getLong(KEY_DEVICE_ID_KEY);
                }
                if (keyJson.has(KEY_BSSID_KEY)) {
                    mBssid = keyJson.getString(KEY_BSSID_KEY);
                }
                if (keyJson.has(KEY_SCAN_RESULT_KEY)) {
                    mScanResultKey = new StandardWifiEntry.ScanResultKey(keyJson.getString(
                            KEY_SCAN_RESULT_KEY));
                }
            } catch (JSONException e) {
                Log.e(TAG, "JSONException while converting HotspotNetworkEntryKey to string: " + e);
            }
        }

        /**
         * Returns the JSON String representation of this HotspotNetworkEntryKey.
         */
        @Override
        public String toString() {
            final JSONObject keyJson = new JSONObject();
            try {
                keyJson.put(KEY_IS_VIRTUAL_ENTRY_KEY, mIsVirtualEntry);
                keyJson.put(KEY_DEVICE_ID_KEY, mDeviceId);
                if (mBssid != null) {
                    keyJson.put(KEY_BSSID_KEY, mBssid);
                }
                if (mScanResultKey != null) {
                    keyJson.put(KEY_SCAN_RESULT_KEY, mScanResultKey.toString());
                }
            } catch (JSONException e) {
                Log.wtf(TAG,
                        "JSONException while converting HotspotNetworkEntryKey to string: " + e);
            }
            return KEY_PREFIX + keyJson.toString();
        }

        public boolean isVirtualEntry() {
            return mIsVirtualEntry;
        }

        public long getDeviceId() {
            return mDeviceId;
        }

        /**
         * Returns the BSSID of this HotspotNetworkEntryKey to match against wifiInfo
         */
        @Nullable
        String getBssid() {
            return mBssid;
        }

        /**
         * Returns the ScanResultKey of this HotspotNetworkEntryKey to match against ScanResults
         */
        @Nullable
        StandardWifiEntry.ScanResultKey getScanResultKey() {
            return mScanResultKey;
        }
    }
}