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

Commit 808a9f11 authored by Arc Wang's avatar Arc Wang Committed by Android (Google) Code Review
Browse files

Merge "[Wi-Fi] Replace WifiTracker with WifiTracker2 in WifiDetailPreferenceController2"

parents 70133b36 b60e91bf
Loading
Loading
Loading
Loading
+131 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.net.NetworkInfo.DetailedState;

import com.android.wifitrackerlib.WifiEntry;

/**
 * {@link WifiEntry is working in progess, many methods are not available, this class is to group
 * all the unavalable {@link AccessPoint} methods & constants.
 *
 * TODO(b/143326832): Replace all methods & constants with WifiEntry version when it's available.
 * TODO(b/143326832): How about AccessPoint#getSettingsSummary(boolean convertSavedAsDisconnected)?
 */
public class WifiEntryShell {
    public WifiEntryShell(){};

    /**
     * Lower bound on the 2.4 GHz (802.11b/g/n) WLAN channels
     */
    public static final int LOWER_FREQ_24GHZ = 2400;

    /**
     * Upper bound on the 2.4 GHz (802.11b/g/n) WLAN channels
     */
    public static final int HIGHER_FREQ_24GHZ = 2500;

    /**
     * Lower bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels
     */
    public static final int LOWER_FREQ_5GHZ = 4900;

    /**
     * Upper bound on the 5.0 GHz (802.11a/h/j/n/ac) WLAN channels
     */
    public static final int HIGHER_FREQ_5GHZ = 5900;

    /**
     * Mapping of the corresponding {@link WifiConfiguration} field
     */
    public static int getNetworkId(WifiEntry wifiEntry) {
        return 0;
    }

    /**
     * Mapping of the corresponding {@link WifiConfiguration} field
     */
    public static boolean hiddenSSID(WifiEntry wifiEntry) {
        return false;
    }

    /**
     * Mapping of the corresponding {@link WifiDetailPreferenceController} method
     */
    public static boolean canModifyNetwork(WifiEntry wifiEntry) {
        return false;
    }

    /**
     * Mapping of the corresponding {@link AccessPoint} method
     */
    public static String getSecurityString(WifiEntry wifiEntry, boolean concise) {
        return "None";
    }

    /**
     * Mapping of the corresponding {@link AccessPoint} method
     */
    public static DetailedState getDetailedState(WifiEntry wifiEntry) {
        return null;
    }

    // Passpoint methods

    /**
     * Mapping of the corresponding {@link AccessPoint} method
     */
    public static boolean isPasspoint(WifiEntry wifiEntry) {
        return false;
    }

    /**
     * Mapping of the corresponding {@link AccessPoint} method
     */
    public static boolean isExpired(WifiEntry wifiEntry) {
        return false;
    }

    /**
     * Mapping of the corresponding {@link AccessPoint} method
     */
    public static boolean isPasspointConfigurationR1(WifiEntry wifiEntry) {
        return false;
    }

    /**
     * Mapping of the corresponding {@link AccessPoint} method
     */
    public static boolean isPasspointConfigurationOsuProvisioned(WifiEntry wifiEntry) {
        return false;
    }

    /**
     * Mapping of the corresponding {@link AccessPoint} method
     */
    public static boolean isOsuProvider(WifiEntry wifiEntry) {
        return false;
    }

    /**
     * Mapping of the corresponding {@link AccessPoint} method
     */
    public static String getPasspointFqdn(WifiEntry wifiEntry) {
        return "Fake passpoint FQDN";
    }
}
+6 −11
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ import androidx.preference.Preference;

import com.android.settings.core.BasePreferenceController;
import com.android.settings.wifi.dpp.WifiDppUtils;
import com.android.settingslib.wifi.AccessPoint;
import com.android.wifitrackerlib.WifiEntry;

/**
 * {@link BasePreferenceController} that launches Wi-Fi Easy Connect configurator flow
@@ -36,7 +36,7 @@ public class AddDevicePreferenceController2 extends BasePreferenceController {

    private static final String KEY_ADD_DEVICE = "add_device_to_network";

    private AccessPoint mAccessPoint;
    private WifiEntry mWifiEntry;
    private WifiManager mWifiManager;

    public AddDevicePreferenceController2(Context context) {
@@ -45,18 +45,13 @@ public class AddDevicePreferenceController2 extends BasePreferenceController {
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    }

    /**
     * Initiate with an {@link AccessPoint}.
     */
    public AddDevicePreferenceController2 init(AccessPoint accessPoint) {
        mAccessPoint = accessPoint;

        return this;
    public void setWifiEntry(WifiEntry wifiEntry) {
        mWifiEntry = wifiEntry;
    }

    @Override
    public int getAvailabilityStatus() {
        if (WifiDppUtils.isSupportConfiguratorQrCodeScanner(mContext, mAccessPoint)) {
        if (WifiDppUtils.isSupportConfiguratorQrCodeScanner(mContext, mWifiEntry)) {
            return AVAILABLE;
        } else {
            return CONDITIONALLY_UNAVAILABLE;
@@ -75,7 +70,7 @@ public class AddDevicePreferenceController2 extends BasePreferenceController {

    private void launchWifiDppConfiguratorQrCodeScanner() {
        final Intent intent = WifiDppUtils.getConfiguratorQrCodeScannerIntentOrNull(mContext,
                mWifiManager, mAccessPoint);
                mWifiManager, mWifiEntry);

        if (intent == null) {
            Log.e(TAG, "Launch Wi-Fi QR code scanner with a wrong Wi-Fi network!");
+142 −452

File changed.

Preview size limit exceeded, changes collapsed.

+22 −24
Original line number Diff line number Diff line
@@ -18,8 +18,6 @@ package com.android.settings.wifi.details2;

import android.app.backup.BackupManager;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

import androidx.annotation.VisibleForTesting;
import androidx.preference.DropDownPreference;
@@ -29,6 +27,7 @@ import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.wifi.WifiDialog;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.wifitrackerlib.WifiEntry;

/**
 * {@link AbstractPreferenceController} that controls whether the wifi network is metered or not
@@ -37,14 +36,12 @@ public class WifiMeteredPreferenceController2 extends BasePreferenceController i
        Preference.OnPreferenceChangeListener, WifiDialog.WifiDialogListener {

    private static final String KEY_WIFI_METERED = "metered";
    private WifiConfiguration mWifiConfiguration;
    private WifiManager mWifiManager;
    private WifiEntry mWifiEntry;
    private Preference mPreference;

    public WifiMeteredPreferenceController2(Context context, WifiConfiguration wifiConfiguration) {
    public WifiMeteredPreferenceController2(Context context, WifiEntry wifiEntry) {
        super(context, KEY_WIFI_METERED);
        mWifiConfiguration = wifiConfiguration;
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        mWifiEntry = wifiEntry;
    }

    @Override
@@ -62,10 +59,10 @@ public class WifiMeteredPreferenceController2 extends BasePreferenceController i

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (mWifiConfiguration != null) {
            mWifiConfiguration.meteredOverride = Integer.parseInt((String) newValue);
        if (mWifiEntry.isSaved()) {
            mWifiEntry.setMeteredChoice(Integer.parseInt((String) newValue));
        }
        mWifiManager.updateNetwork(mWifiConfiguration);

        // Stage the backup of the SettingsProvider package which backs this up
        BackupManager.dataChanged("com.android.providers.settings");
        updateSummary((DropDownPreference) preference, getMeteredOverride());
@@ -74,11 +71,11 @@ public class WifiMeteredPreferenceController2 extends BasePreferenceController i

    @VisibleForTesting
    int getMeteredOverride() {
        if (mWifiConfiguration != null) {
        if (mWifiEntry.isSaved()) {
            // Wrap the meteredOverride since robolectric cannot recognize it
            return mWifiConfiguration.meteredOverride;
            return mWifiEntry.getMeteredChoice();
        }
        return WifiConfiguration.METERED_OVERRIDE_NONE;
        return WifiEntry.METERED_CHOICE_AUTO;
    }

    private void updateSummary(DropDownPreference preference, int meteredOverride) {
@@ -93,16 +90,17 @@ public class WifiMeteredPreferenceController2 extends BasePreferenceController i

    @Override
    public void onSubmit(WifiDialog dialog) {
        if (dialog.getController() != null) {
            final WifiConfiguration newConfig = dialog.getController().getConfig();
            if (newConfig == null || mWifiConfiguration == null) {
                return;
            }

            if (newConfig.meteredOverride != mWifiConfiguration.meteredOverride) {
                mWifiConfiguration = newConfig;
                onPreferenceChange(mPreference, String.valueOf(newConfig.meteredOverride));
            }
        }
        // TODO(b/143326832): Create WifiDialog2 and let it work for WifiEntry.
        //if (dialog.getController() != null) {
        //    final WifiConfiguration newConfig = dialog.getController().getConfig();
        //    if (newConfig == null || mWifiConfiguration == null) {
        //        return;
        //    }
        //
        //    if (newConfig.meteredOverride != mWifiConfiguration.meteredOverride) {
        //        mWifiConfiguration = newConfig;
        //        onPreferenceChange(mPreference, String.valueOf(newConfig.meteredOverride));
        //    }
        //}
    }
}
+69 −22
Original line number Diff line number Diff line
@@ -21,45 +21,59 @@ import android.app.Dialog;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkScoreManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Process;
import android.os.SimpleClock;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.wifi.WifiConfigUiBase;
import com.android.settings.wifi.WifiDialog;
import com.android.settings.wifi.savedaccesspoints2.SavedAccessPointsWifiSettings2;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.wifi.AccessPoint;
import com.android.wifitrackerlib.NetworkDetailsTracker;
import com.android.wifitrackerlib.WifiEntry;

import java.time.Clock;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * Detail page for the currently connected wifi network.
 *
 * <p>The AccessPoint should be saved to the intent Extras when launching this class via
 * {@link AccessPoint#saveWifiState(Bundle)} in order to properly render this page.
 * <p>The key of {@link WifiEntry} should be saved to the intent Extras when launching this class
 * in order to properly render this page.
 */
public class WifiNetworkDetailsFragment2 extends DashboardFragment implements
        WifiDialog.WifiDialogListener {

    private static final String TAG = "WifiNetworkDetailsFrg2";

    private AccessPoint mAccessPoint;
    // Max age of tracked WifiEntries
    private static final long MAX_SCAN_AGE_MILLIS = 15_000;
    // Interval between initiating SavedNetworkTracker scans
    private static final long SCAN_INTERVAL_MILLIS = 10_000;

    private NetworkDetailsTracker mNetworkDetailsTracker;
    private HandlerThread mWorkerThread;
    private WifiDetailPreferenceController2 mWifiDetailPreferenceController2;
    private List<WifiDialog.WifiDialogListener> mWifiDialogListeners = new ArrayList<>();

    @Override
    public void onAttach(Context context) {
        mAccessPoint = new AccessPoint(context, getArguments());
        super.onAttach(context);
    public void onDestroy() {
        mWorkerThread.quit();

        super.onDestroy();
    }

    @Override
@@ -87,15 +101,15 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements

    @Override
    public Dialog onCreateDialog(int dialogId) {
        if (getActivity() == null || mWifiDetailPreferenceController2 == null
                || mAccessPoint == null) {
        if (getActivity() == null || mWifiDetailPreferenceController2 == null) {
            return null;
        }
        return WifiDialog.createModal(getActivity(), this, mAccessPoint,
                WifiConfigUiBase.MODE_MODIFY);
        // TODO(b/143326832): Replace it with WifiEntry.
        return null;
        //return WifiDialog.createModal(getActivity(), this, mAccessPoint,
        //        WifiConfigUiBase.MODE_MODIFY);
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        MenuItem item = menu.add(0, Menu.FIRST, 0, R.string.wifi_modify);
@@ -124,9 +138,11 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        final ConnectivityManager cm = context.getSystemService(ConnectivityManager.class);
        setupNetworksDetailTracker();
        final WifiEntry wifiEntry = mNetworkDetailsTracker.getWifiEntry();

        mWifiDetailPreferenceController2 = WifiDetailPreferenceController2.newInstance(
                mAccessPoint,
                wifiEntry,
                cm,
                context,
                this,
@@ -134,20 +150,20 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements
                getSettingsLifecycle(),
                context.getSystemService(WifiManager.class),
                mMetricsFeatureProvider);

        controllers.add(mWifiDetailPreferenceController2);
        controllers.add(new AddDevicePreferenceController2(context).init(mAccessPoint));

        final AddDevicePreferenceController2 addDevicePreferenceController2 =
                new AddDevicePreferenceController2(context);
        addDevicePreferenceController2.setWifiEntry(wifiEntry);
        controllers.add(addDevicePreferenceController2);

        final WifiMeteredPreferenceController2 meteredPreferenceController2 =
                new WifiMeteredPreferenceController2(context, mAccessPoint.getConfig());
                new WifiMeteredPreferenceController2(context, wifiEntry);
        controllers.add(meteredPreferenceController2);

        final WifiPrivacyPreferenceController2 privacyController2 =
                new WifiPrivacyPreferenceController2(context);
        privacyController2.setWifiConfiguration(mAccessPoint.getConfig());
        privacyController2.setIsEphemeral(mAccessPoint.isEphemeral());
        privacyController2.setIsPasspoint(
                mAccessPoint.isPasspoint() || mAccessPoint.isPasspointConfig());
        privacyController2.setWifiEntry(wifiEntry);
        controllers.add(privacyController2);

        // Sets callback listener for wifi dialog.
@@ -164,4 +180,35 @@ public class WifiNetworkDetailsFragment2 extends DashboardFragment implements
            listener.onSubmit(dialog);
        }
    }

    private void setupNetworksDetailTracker() {
        if (mNetworkDetailsTracker != null) {
            return;
        }

        final Context context = getContext();
        mWorkerThread = new HandlerThread(TAG
                + "{" + Integer.toHexString(System.identityHashCode(this)) + "}",
                Process.THREAD_PRIORITY_BACKGROUND);
        mWorkerThread.start();
        final Clock elapsedRealtimeClock = new SimpleClock(ZoneOffset.UTC) {
            @Override
            public long millis() {
                return SystemClock.elapsedRealtime();
            }
        };

        mNetworkDetailsTracker = NetworkDetailsTracker.createNetworkDetailsTracker(
                getSettingsLifecycle(),
                context,
                context.getSystemService(WifiManager.class),
                context.getSystemService(ConnectivityManager.class),
                context.getSystemService(NetworkScoreManager.class),
                new Handler(Looper.getMainLooper()),
                mWorkerThread.getThreadHandler(),
                elapsedRealtimeClock,
                MAX_SCAN_AGE_MILLIS,
                SCAN_INTERVAL_MILLIS,
                getArguments().getString(SavedAccessPointsWifiSettings2.KEY_KEY));
    }
}
Loading