Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e50d7b5d authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[Status Bar] Move WifiPickerTrackerFactory to top-level class.

It will be used by the status bar wifi icon pipeline in a future CL, so
it should no longer be part of AccessPointControllerImpl.

Bug: 292534484
Test: atest AccessPointControllerImplTest
Change-Id: Ic077c0b03cd9c4f19b8c4e8de03db765b31c0c83
parent 1cf4791b
Loading
Loading
Loading
Loading
+0 −86
Original line number Diff line number Diff line
@@ -16,13 +16,7 @@

package com.android.systemui.statusbar.connectivity;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.SimpleClock;
import android.os.SystemClock;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
@@ -35,25 +29,18 @@ import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.settings.UserTracker;
import com.android.wifitrackerlib.MergedCarrierEntry;
import com.android.wifitrackerlib.WifiEntry;
import com.android.wifitrackerlib.WifiPickerTracker;

import java.io.PrintWriter;
import java.time.Clock;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;

import javax.inject.Inject;

/** */
public class AccessPointControllerImpl implements AccessPointController,
        WifiPickerTracker.WifiPickerTrackerCallback,
@@ -272,77 +259,4 @@ public class AccessPointControllerImpl implements AccessPointController,
            }
        }
    };

    /**
     * Factory for creating {@link WifiPickerTracker}.
     *
     * Uses the same time intervals as the Settings page for Wifi.
     */
    @SysUISingleton
    public static class WifiPickerTrackerFactory {

        // 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;

        private final Context mContext;
        private final @Nullable WifiManager mWifiManager;
        private final ConnectivityManager mConnectivityManager;
        private final Handler mMainHandler;
        private final Handler mWorkerHandler;
        private final Clock mClock = new SimpleClock(ZoneOffset.UTC) {
            @Override
            public long millis() {
                return SystemClock.elapsedRealtime();
            }
        };

        @Inject
        public WifiPickerTrackerFactory(
                Context context,
                @Nullable WifiManager wifiManager,
                ConnectivityManager connectivityManager,
                @Main Handler mainHandler,
                @Background Handler workerHandler
        ) {
            mContext = context;
            mWifiManager = wifiManager;
            mConnectivityManager = connectivityManager;
            mMainHandler = mainHandler;
            mWorkerHandler = workerHandler;
        }

        private boolean isSupported() {
            return mWifiManager != null;
        }

        /**
         * Create a {@link WifiPickerTracker}
         *
         * @param lifecycle
         * @param listener
         * @return a new {@link WifiPickerTracker} or {@code null} if {@link WifiManager} is null.
         */
        public @Nullable WifiPickerTracker create(
                Lifecycle lifecycle,
                WifiPickerTracker.WifiPickerTrackerCallback listener
        ) {
            if (mWifiManager == null) {
                return null;
            }
            return new WifiPickerTracker(
                    lifecycle,
                    mContext,
                    mWifiManager,
                    mConnectivityManager,
                    mMainHandler,
                    mWorkerHandler,
                    mClock,
                    MAX_SCAN_AGE_MILLIS,
                    SCAN_INTERVAL_MILLIS,
                    listener
            );
        }
    }
}
+92 −0
Original line number Diff line number Diff line
/*
 * 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.systemui.statusbar.connectivity

import android.content.Context
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.os.Handler
import android.os.SimpleClock
import androidx.lifecycle.Lifecycle
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.util.time.SystemClock
import com.android.wifitrackerlib.WifiPickerTracker
import com.android.wifitrackerlib.WifiPickerTracker.WifiPickerTrackerCallback
import java.time.Clock
import java.time.ZoneOffset
import javax.inject.Inject

/**
 * Factory for creating [WifiPickerTracker] for SysUI.
 *
 * Uses the same time intervals as the Settings page for Wifi.
 */
@SysUISingleton
class WifiPickerTrackerFactory
@Inject
constructor(
    private val context: Context,
    private val wifiManager: WifiManager?,
    private val connectivityManager: ConnectivityManager,
    private val systemClock: SystemClock,
    @Main private val mainHandler: Handler,
    @Background private val workerHandler: Handler,
) {
    private val clock: Clock =
        object : SimpleClock(ZoneOffset.UTC) {
            override fun millis(): Long {
                return systemClock.elapsedRealtime()
            }
        }
    val isSupported: Boolean
        get() = wifiManager != null

    /**
     * Creates a [WifiPickerTracker] instance.
     *
     * @return a new [WifiPickerTracker] or null if [WifiManager] is null.
     */
    fun create(
        lifecycle: Lifecycle,
        listener: WifiPickerTrackerCallback,
    ): WifiPickerTracker? {
        return if (wifiManager == null) {
            null
        } else
            WifiPickerTracker(
                lifecycle,
                context,
                wifiManager,
                connectivityManager,
                mainHandler,
                workerHandler,
                clock,
                MAX_SCAN_AGE_MILLIS,
                SCAN_INTERVAL_MILLIS,
                listener,
            )
    }

    companion object {
        /** Max age of tracked WifiEntries. */
        private const val MAX_SCAN_AGE_MILLIS: Long = 15000
        /** Interval between initiating WifiPickerTracker scans. */
        private const val SCAN_INTERVAL_MILLIS: Long = 10000
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.systemui.statusbar.connectivity.AccessPointController;
import com.android.systemui.statusbar.connectivity.AccessPointControllerImpl;
import com.android.systemui.statusbar.connectivity.NetworkController;
import com.android.systemui.statusbar.connectivity.NetworkControllerImpl;
import com.android.systemui.statusbar.connectivity.WifiPickerTrackerFactory;
import com.android.systemui.statusbar.phone.ConfigurationControllerImpl;
import com.android.systemui.statusbar.policy.BluetoothController;
import com.android.systemui.statusbar.policy.BluetoothControllerImpl;
@@ -165,7 +166,7 @@ public interface StatusBarPolicyModule {
            UserManager userManager,
            UserTracker userTracker,
            @Main Executor mainExecutor,
            AccessPointControllerImpl.WifiPickerTrackerFactory wifiPickerTrackerFactory
            WifiPickerTrackerFactory wifiPickerTrackerFactory
    ) {
        AccessPointControllerImpl controller = new AccessPointControllerImpl(
                userManager,
+2 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.testing.TestableLooper.RunWithLooper
import androidx.lifecycle.Lifecycle
import com.android.systemui.SysuiTestCase
import com.android.systemui.settings.UserTracker
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.capture
import com.android.wifitrackerlib.WifiEntry
import com.android.wifitrackerlib.WifiPickerTracker
@@ -31,7 +32,6 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyList
import org.mockito.Captor
import org.mockito.Mock
@@ -52,7 +52,7 @@ class AccessPointControllerImplTest : SysuiTestCase() {
    private lateinit var userTracker: UserTracker
    @Mock
    private lateinit var wifiPickerTrackerFactory:
            AccessPointControllerImpl.WifiPickerTrackerFactory
            WifiPickerTrackerFactory
    @Mock
    private lateinit var wifiPickerTracker: WifiPickerTracker
    @Mock