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

Commit 31a33ba6 authored by Salvador Martinez's avatar Salvador Martinez Committed by android-build-merger
Browse files

Change UI for wireless AP tether band selection

am: acff57fa

Change-Id: I47124f2fa50015eca97cee589b4a1f94f43123fd
parents ff73564e acff57fa
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -284,17 +284,27 @@

   <!-- Wi-Fi AP band settings.  Either Auto, 2.4GHz or 5GHz. -->
   <!-- Note that adding/removing/moving the items will need wifi settings code change. -->
    <string-array name="wifi_ap_band_config_full">
    <string-array translatable="false" name="wifi_ap_band_config_full">
        <item>0</item>
        <item>1</item>
    </string-array>

    <string-array translatable="false" name="wifi_ap_band_summary_full">
        <item>@string/wifi_ap_choose_2G</item>
        <item>@string/wifi_ap_choose_5G</item>
    </string-array>

    <string-array name="wifi_ap_band_summary_full">
        <item>@string/wifi_ap_2G</item>
        <item>@string/wifi_ap_5G</item>
    <string-array translatable="false" name="wifi_ap_band_dual_mode">
        <item>0</item>
        <item>-1</item>
    </string-array>

    <string-array translatable="false" name="wifi_ap_band_dual_mode_summary">
        <item>@string/wifi_ap_choose_2G</item>
        <item>@string/wifi_ap_prefer_5G</item>
    </string-array>

    <string-array name="wifi_ap_band_config_2G_only">
    <string-array translatable="false" name="wifi_ap_band_config_2G_only">
        <item>@string/wifi_ap_choose_auto</item>
        <item>@string/wifi_ap_choose_2G</item>
    </string-array>
+3 −1
Original line number Diff line number Diff line
@@ -1986,8 +1986,10 @@
    <string name="wifi_ap_choose_auto">Auto</string>
    <!-- Label for the radio button to choose wifi ap 2.4 GHz band -->
    <string name="wifi_ap_choose_2G">2.4 GHz Band</string>
    <!-- Label for the radio button to choose wifi ap 5GHz band -->
    <!-- Label for the radio button to only choose wifi ap 5GHz band -->
    <string name="wifi_ap_choose_5G">5.0 GHz Band</string>
    <!-- Label for the radio button to prefer 5GHz wifi ap band  [CHAR LIMIT=80]-->
    <string name="wifi_ap_prefer_5G">5.0 GHz Band preferred</string>
    <!-- Label for adding to the list of selected bands when 2.4 GHz is selected -->
    <string name="wifi_ap_2G">2.4 GHz</string>
    <!-- Label for adding to the list of selected bands when 5.0 GHz is selected -->
+2 −6
Original line number Diff line number Diff line
@@ -43,11 +43,7 @@
        android:title="@string/wifi_hotspot_auto_off_title"
        android:summary="@string/wifi_hotspot_auto_off_summary" />

    <com.android.settings.widget.HotspotApBandSelectionPreference
    <ListPreference
        android:key="wifi_tether_network_ap_band"
        android:title="@string/wifi_hotspot_ap_band_title"
        android:dialogLayout="@layout/hotspot_ap_band_selection_dialog"
        android:dialogTitle="@string/wifi_hotspot_ap_band_title"
        android:negativeButtonText="@string/cancel"
        android:positiveButtonText="@string/apply" />
        android:title="@string/wifi_hotspot_ap_band_title" />
</PreferenceScreen>
 No newline at end of file
+0 −258
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.VisibleForTesting;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.android.settings.R;
import com.android.settingslib.CustomDialogPreference;

import java.util.ArrayList;

public class HotspotApBandSelectionPreference extends CustomDialogPreference implements
        CompoundButton.OnCheckedChangeListener, DialogInterface.OnShowListener {
    private static final int UNSET = Integer.MIN_VALUE;

    @VisibleForTesting
    static final String KEY_CHECKED_BANDS = "checked_bands";
    @VisibleForTesting
    static final String KEY_HOTSPOT_SUPER_STATE = "hotspot_super_state";

    @VisibleForTesting
    CheckBox mBox2G;
    @VisibleForTesting
    CheckBox mBox5G;
    @VisibleForTesting
    ArrayList<Integer> mRestoredBands;
    @VisibleForTesting
    boolean mShouldRestore;

    private String[] mBandEntries;
    private int mExistingConfigValue = UNSET;

    public HotspotApBandSelectionPreference(Context context) {
        super(context);
    }

    public HotspotApBandSelectionPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HotspotApBandSelectionPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public HotspotApBandSelectionPreference(Context context, AttributeSet attrs, int defStyleAttr,
            int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        SavedState myState = (SavedState) state;

        super.onRestoreInstanceState(myState.getSuperState());

        mShouldRestore = myState.shouldRestore;
        if (mShouldRestore) {
            mRestoredBands = new ArrayList<>();
            if (myState.enabled2G) {
                mRestoredBands.add(WifiConfiguration.AP_BAND_2GHZ);
            }
            if (myState.enabled5G) {
                mRestoredBands.add(WifiConfiguration.AP_BAND_5GHZ);
            }
        } else {
            mRestoredBands = null;
        }
        updatePositiveButton();
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        final Context context = getContext();

        // Register so we can adjust the buttons if needed once the dialog is available.
        setOnShowListener(this);

        mBandEntries = context.getResources().getStringArray(R.array.wifi_ap_band_config_full);
        // add a checkbox for every band entry.
        addApBandViews((LinearLayout) view);
        // try to update the button just in case we already missed the onShow call.
        updatePositiveButton();
        // clear any saved state so it doesn't leak across multiple rotations/dialog closings
        mRestoredBands = null;
        mShouldRestore = false;
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();

        SavedState myState = new SavedState(superState);
        myState.shouldRestore = getDialog() != null;
        myState.enabled2G = mBox2G != null && mBox2G.isChecked();
        myState.enabled5G = mBox5G != null && mBox5G.isChecked();
        return myState;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (!(buttonView instanceof CheckBox)) {
            return;
        }
        updatePositiveButton();
    }

    @Override
    protected void onClick(DialogInterface dialog, int which) {
        // we only want to persist our enabled bands if apply is clicked
        if (which == DialogInterface.BUTTON_POSITIVE) {
            if (mBox2G.isChecked() || mBox5G.isChecked()) {
                int wifiBand = getWifiBand();
                mExistingConfigValue = wifiBand;
                callChangeListener(wifiBand);
            }
        }
    }

    /**
     * Used to set the band selection for the preference if one already exists
     * @param band the band to set it to from {@link WifiConfiguration}
     */
    public void setExistingConfigValue(int band) {
        mExistingConfigValue = band;
    }

    private void addApBandViews(LinearLayout view) {
        mBox2G = view.findViewById(R.id.box_2g);
        mBox2G.setText(mBandEntries[WifiConfiguration.AP_BAND_2GHZ]);
        mBox2G.setChecked(restoreBandIfNeeded(WifiConfiguration.AP_BAND_2GHZ));
        mBox2G.setOnCheckedChangeListener(this);

        mBox5G = view.findViewById(R.id.box_5g);
        mBox5G.setText(mBandEntries[WifiConfiguration.AP_BAND_5GHZ]);
        mBox5G.setChecked(restoreBandIfNeeded(WifiConfiguration.AP_BAND_5GHZ));
        mBox5G.setOnCheckedChangeListener(this);
    }

    private boolean restoreBandIfNeeded(int band) {
        // Only use the provided config if we aren't restoring, restore if state available
        return (isBandPreviouslySelected(band) && !mShouldRestore)
                || (mShouldRestore && mRestoredBands.contains(band));
    }

    private void updatePositiveButton() {
        AlertDialog dialog = (AlertDialog) getDialog();
        Button button = dialog == null ? null : dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        if (button != null && mBox5G != null && mBox2G != null) {
            button.setEnabled(mBox2G.isChecked() || mBox5G.isChecked());
        }
    }

    @VisibleForTesting
    int getWifiBand() {
        final boolean checked_2g = mBox2G.isChecked();
        final boolean checked_5g = mBox5G.isChecked();
        if (checked_2g && checked_5g) {
            return WifiConfiguration.AP_BAND_ANY;
        } else if (checked_2g && !checked_5g) {
            return WifiConfiguration.AP_BAND_2GHZ;
        } else if (checked_5g && !checked_2g) {
            return WifiConfiguration.AP_BAND_5GHZ;
        } else {
            throw new IllegalStateException("Wifi Config only supports selecting one or all bands");
        }
    }

    private boolean isBandPreviouslySelected(int bandIndex) {
        switch(mExistingConfigValue) {
            case WifiConfiguration.AP_BAND_ANY:
                return true;
            case WifiConfiguration.AP_BAND_2GHZ:
                return bandIndex == 0;
            case WifiConfiguration.AP_BAND_5GHZ:
                return bandIndex == 1;
            case UNSET:
            default:
                return false;
        }
    }

    @Override
    public void onShow(DialogInterface dialog) {
        updatePositiveButton();
    }

    private static class SavedState extends BaseSavedState {
        boolean shouldRestore;
        boolean enabled2G;
        boolean enabled5G;

        public SavedState(Parcelable source) {
            super(source);
        }

        private SavedState(Parcel in) {
            super(in);
            shouldRestore =  in.readByte() == 1;
            enabled2G = in.readByte() == 1;
            enabled5G = in.readByte() == 1;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeByte((byte) (shouldRestore ? 1 : 0));
            dest.writeByte((byte) (enabled2G ? 1: 0));
            dest.writeByte((byte) (enabled5G ? 1 : 0));
        }

        @Override
        public String toString() {
            return "HotspotApBandSelectionPreference.SavedState{"
                    + Integer.toHexString(System.identityHashCode(this))
                    + " shouldRestore=" + shouldRestore
                    + " enabled2G=" + enabled2G
                    + " enabled5G=" + enabled5G + "}";
        }

        public static final Parcelable.Creator<SavedState> CREATOR
                = new Parcelable.Creator<SavedState>() {
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}
+48 −18
Original line number Diff line number Diff line
@@ -16,36 +16,31 @@

package com.android.settings.wifi.tether;

import static android.net.wifi.WifiConfiguration.AP_BAND_2GHZ;
import static android.net.wifi.WifiConfiguration.AP_BAND_5GHZ;

import android.content.Context;
import android.content.res.Resources;
import android.icu.text.ListFormatter;
import android.net.wifi.WifiConfiguration;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.util.Log;

import com.android.settings.R;
import com.android.settings.widget.HotspotApBandSelectionPreference;

public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferenceController {

    private static final String TAG = "WifiTetherApBandPref";
    private static final String PREF_KEY = "wifi_tether_network_ap_band";
    public static final String[] BAND_VALUES =
            {String.valueOf(AP_BAND_2GHZ), String.valueOf(AP_BAND_5GHZ)};

    private final String[] mBandEntries;
    private final String[] mBandSummaries;
    private String[] mBandEntries;
    private String[] mBandSummaries;
    private int mBandIndex;
    private boolean isDualMode;

    public WifiTetherApBandPreferenceController(Context context,
            OnTetherConfigUpdateListener listener) {
        super(context, listener);
        Resources res = mContext.getResources();
        mBandEntries = res.getStringArray(R.array.wifi_ap_band_config_full);
        mBandSummaries = res.getStringArray(R.array.wifi_ap_band_summary_full);
        isDualMode = mWifiManager.isDualModeSupported();
        updatePreferenceEntries();
    }

    @Override
@@ -55,7 +50,7 @@ public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferen
            mBandIndex = 0;
            Log.d(TAG, "Updating band index to 0 because no config");
        } else if (is5GhzBandSupported()) {
            mBandIndex = config.apBand;
            mBandIndex = validateSelection(config.apBand);
            Log.d(TAG, "Updating band index to " + mBandIndex);
        } else {
            config.apBand = 0;
@@ -63,21 +58,23 @@ public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferen
            mBandIndex = config.apBand;
            Log.d(TAG, "5Ghz not supported, updating band index to " + mBandIndex);
        }
        HotspotApBandSelectionPreference preference =
                (HotspotApBandSelectionPreference) mPreference;
        ListPreference preference =
                (ListPreference) mPreference;
        preference.setEntries(mBandSummaries);
        preference.setEntryValues(mBandEntries);

        if (!is5GhzBandSupported()) {
            preference.setEnabled(false);
            preference.setSummary(R.string.wifi_ap_choose_2G);
        } else {
            preference.setExistingConfigValue(config.apBand);
            preference.setValue(Integer.toString(config.apBand));
            preference.setSummary(getConfigSummary());
        }
    }

    String getConfigSummary() {
        if (mBandIndex == WifiConfiguration.AP_BAND_ANY) {
            return ListFormatter.getInstance().format((Object[]) mBandSummaries);
           return mContext.getString(R.string.wifi_ap_prefer_5G);
        }
        return mBandSummaries[mBandIndex];
    }
@@ -89,13 +86,46 @@ public class WifiTetherApBandPreferenceController extends WifiTetherBasePreferen

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        mBandIndex = (Integer) newValue;
        mBandIndex = validateSelection(Integer.parseInt((String) newValue));
        Log.d(TAG, "Band preference changed, updating band index to " + mBandIndex);
        preference.setSummary(getConfigSummary());
        mListener.onTetherConfigUpdated();
        return true;
    }

    private int validateSelection(int band) {
        // Reset the band to 2.4 GHz if we get a weird config back to avoid a crash.
        final boolean isDualMode = mWifiManager.isDualModeSupported();

        // unsupported states:
        // 1: no dual mode means we can't have AP_BAND_ANY - default to 5GHZ
        // 2: no 5 GHZ support means we can't have AP_BAND_5GHZ - default to 2GHZ
        // 3: With Dual mode support we can't have AP_BAND_5GHZ - default to ANY
        if (!isDualMode && WifiConfiguration.AP_BAND_ANY == band) {
            return WifiConfiguration.AP_BAND_5GHZ;
        } else if (!mWifiManager.is5GHzBandSupported() && WifiConfiguration.AP_BAND_5GHZ == band) {
            return WifiConfiguration.AP_BAND_2GHZ;
        } else if (isDualMode && WifiConfiguration.AP_BAND_5GHZ == band) {
            return WifiConfiguration.AP_BAND_ANY;
        }

        return band;
    }

    @VisibleForTesting
    void updatePreferenceEntries() {
        Resources res = mContext.getResources();
        int entriesRes = R.array.wifi_ap_band_config_full;
        int summariesRes = R.array.wifi_ap_band_summary_full;
        // change the list options if this is a dual mode device
        if (isDualMode) {
            entriesRes = R.array.wifi_ap_band_dual_mode;
            summariesRes = R.array.wifi_ap_band_dual_mode_summary;
        }
        mBandEntries = res.getStringArray(entriesRes);
        mBandSummaries = res.getStringArray(summariesRes);
    }

    private boolean is5GhzBandSupported() {
        final String countryCode = mWifiManager.getCountryCode();
        if (!mWifiManager.isDualBandSupported() || countryCode == null) {
Loading