summaryrefslogtreecommitdiff
path: root/src/com/android/settings/wifi/WifiPickerTrackerHelper.java
blob: 03795c2597173030f52d75ac0f19d45daf20cb36 (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
/*
 * Copyright (C) 2021 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.settings.wifi;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.PersistableBundle;
import android.os.Process;
import android.os.SimpleClock;
import android.os.SystemClock;
import android.telephony.CarrierConfigManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.overlay.FeatureFactory;
import com.android.wifitrackerlib.MergedCarrierEntry;
import com.android.wifitrackerlib.WifiEntry;
import com.android.wifitrackerlib.WifiPickerTracker;

import java.time.Clock;
import java.time.ZoneOffset;

public class WifiPickerTrackerHelper implements LifecycleObserver {

    private static final String TAG = "WifiPickerTrackerHelper";

    // Max age of tracked WifiEntries
    private static final long MAX_SCAN_AGE_MILLIS = 15_000;
    // Interval between initiating WifiPickerTracker scans
    private static final long SCAN_INTERVAL_MILLIS = 10_000;
    // Clock used for evaluating the age of scans
    private static final Clock ELAPSED_REALTIME_CLOCK = new SimpleClock(ZoneOffset.UTC) {
        @Override
        public long millis() {
            return SystemClock.elapsedRealtime();
        }
    };

    protected WifiPickerTracker mWifiPickerTracker;
    // Worker thread used for WifiPickerTracker work
    protected HandlerThread mWorkerThread;

    protected final WifiManager mWifiManager;
    protected final CarrierConfigManager mCarrierConfigManager;

    public WifiPickerTrackerHelper(@NonNull Lifecycle lifecycle, @NonNull Context context,
            @Nullable WifiPickerTracker.WifiPickerTrackerCallback listener) {
        if (lifecycle == null) {
            throw new IllegalArgumentException("lifecycle must be non-null.");
        }
        lifecycle.addObserver(this);
        mWorkerThread = new HandlerThread(TAG
                + "{" + Integer.toHexString(System.identityHashCode(this)) + "}",
                Process.THREAD_PRIORITY_BACKGROUND);
        mWorkerThread.start();

        mWifiPickerTracker = FeatureFactory.getFactory(context)
                .getWifiTrackerLibProvider()
                .createWifiPickerTracker(lifecycle, context,
                new Handler(Looper.getMainLooper()),
                mWorkerThread.getThreadHandler(),
                ELAPSED_REALTIME_CLOCK,
                MAX_SCAN_AGE_MILLIS,
                SCAN_INTERVAL_MILLIS,
                listener);

        mWifiManager = context.getSystemService(WifiManager.class);
        mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
    }

    /** @OnLifecycleEvent(ON_DESTROY) */
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
        mWorkerThread.quit();
    }

    /** Return the WifiPickerTracker class */
    public @NonNull WifiPickerTracker getWifiPickerTracker() {
        return mWifiPickerTracker;
    }

    /** Return the enabled/disabled state of the carrier network provision */
    public boolean isCarrierNetworkProvisionEnabled(int subId) {
        final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId);
        if (config == null) {
            Log.e(TAG, "Could not get carrier config, subId:" + subId);
            return false;
        }
        final boolean enabled = config.getBoolean(
                CarrierConfigManager.KEY_CARRIER_PROVISIONS_WIFI_MERGED_NETWORKS_BOOL);
        Log.i(TAG, "isCarrierNetworkProvisionEnabled:" + enabled);
        return enabled;
    }

    /** Return the enabled/disabled state of the carrier network */
    public boolean isCarrierNetworkEnabled(int subId) {
        return mWifiManager.isCarrierNetworkOffloadEnabled(subId, true /* merged */);
    }

    /** Enables/disables the carrier network */
    public void setCarrierNetworkEnabled(boolean enabled) {
        final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
        if (mergedCarrierEntry == null) {
            return;
        }
        mergedCarrierEntry.setEnabled(enabled);
    }

    /** Connect to the carrier network */
    public boolean connectCarrierNetwork(@Nullable WifiEntry.ConnectCallback callback) {
        final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
        if (mergedCarrierEntry == null || !mergedCarrierEntry.canConnect()) {
            return false;
        }
        mergedCarrierEntry.connect(callback);
        return true;
    }

    /** Confirms connection of the carrier network connected with the internet access */
    public boolean isCarrierNetworkActive() {
        final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
        return (mergedCarrierEntry != null && mergedCarrierEntry.isDefaultNetwork());
    }

    /** Return the carrier network ssid */
    public String getCarrierNetworkSsid() {
        final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
        if (mergedCarrierEntry == null) {
            return null;
        }
        return mergedCarrierEntry.getSsid();
    }

    @VisibleForTesting
    void setWifiPickerTracker(@NonNull WifiPickerTracker wifiPickerTracker) {
        mWifiPickerTracker = wifiPickerTracker;
    }

    @VisibleForTesting
    void setWorkerThread(@NonNull HandlerThread workerThread) {
        mWorkerThread = workerThread;
    }
}