Snap for 9211918 from 02cf722b24 to tm-qpr2-release

Change-Id: Id065d2e3897ecdf149afecd376bd512dcb294675
This commit is contained in:
Android Build Coastguard Worker
2022-10-24 23:29:52 +00:00
11 changed files with 275 additions and 39 deletions

View File

@@ -331,6 +331,9 @@
surface in search results or not.-->
<bool name="config_show_wifi_settings">true</bool>
<!-- Whether Wi-Fi hotspot settings should be shown or not. -->
<bool name="config_show_wifi_hotspot_settings">true</bool>
<!-- Whether toggle_airplane is available or not. -->
<bool name="config_show_toggle_airplane">true</bool>

View File

@@ -64,7 +64,9 @@ public class MediaOutputUtils {
+ ", play back type : " + pi.getPlaybackType() + ", play back state : "
+ playbackState.getState());
}
if (playbackState.getState() != PlaybackState.STATE_PLAYING) {
if (playbackState.getState() == PlaybackState.STATE_STOPPED
|| playbackState.getState() == PlaybackState.STATE_NONE
|| playbackState.getState() == PlaybackState.STATE_ERROR) {
// do nothing
continue;
}

View File

@@ -60,6 +60,7 @@ import com.android.settingslib.utils.ThreadUtils;
import com.google.android.setupdesign.DividerItemDecoration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
@@ -339,8 +340,13 @@ public class PanelFragment extends Fragment {
mSliceLiveData.put(uri, sliceLiveData);
sliceLiveData.observe(getViewLifecycleOwner(), slice -> {
// If the Slice has already loaded, do nothing.
// If the Slice has already loaded, refresh list with slice data.
if (mPanelSlicesLoaderCountdownLatch.isSliceLoaded(uri)) {
if (mAdapter != null) {
int itemIndex = (new ArrayList<>(mSliceLiveData.keySet())).indexOf(uri);
mAdapter.notifyItemChanged(itemIndex);
}
return;
}

View File

@@ -84,7 +84,7 @@ public class PanelSlicesAdapter
@Override
public void onBindViewHolder(@NonNull SliceRowViewHolder sliceRowViewHolder, int position) {
sliceRowViewHolder.onBind(mSliceLiveData.get(position), position);
sliceRowViewHolder.onBind(mSliceLiveData.get(position).getValue());
}
/**
@@ -132,15 +132,14 @@ public class PanelSlicesAdapter
/**
* Called when the view is displayed.
*/
public void onBind(LiveData<Slice> sliceLiveData, int position) {
sliceLiveData.observe(mPanelFragment.getViewLifecycleOwner(), sliceView);
// Do not show the divider above media devices switcher slice per request
final Slice slice = sliceLiveData.getValue();
public void onBind(Slice slice) {
// Hides slice which reports with error hint or not contain any slice sub-item.
if (slice == null || !isValidSlice(slice)) {
sliceView.setVisibility(View.GONE);
return;
} else {
sliceView.setSlice(slice);
sliceView.setVisibility(View.VISIBLE);
}
// Add divider for the end icon
@@ -154,7 +153,7 @@ public class PanelSlicesAdapter
.action(0 /* attribution */,
SettingsEnums.ACTION_PANEL_INTERACTION,
mMetricsCategory,
sliceLiveData.getValue().getUri().getLastPathSegment()
slice.getUri().getLastPathSegment()
/* log key */,
eventInfo.actionType /* value */);
})

View File

@@ -22,14 +22,20 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.NetworkCapabilities;
import android.net.TetheringManager;
import android.net.wifi.ScanResult;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.wifitrackerlib.WifiEntry;
@@ -38,12 +44,16 @@ import java.nio.charset.StandardCharsets;
/** A utility class for Wi-Fi functions. */
public class WifiUtils extends com.android.settingslib.wifi.WifiUtils {
static final String TAG = "WifiUtils";
private static final int SSID_ASCII_MIN_LENGTH = 1;
private static final int SSID_ASCII_MAX_LENGTH = 32;
private static final int PSK_PASSPHRASE_ASCII_MIN_LENGTH = 8;
private static final int PSK_PASSPHRASE_ASCII_MAX_LENGTH = 63;
private static Boolean sCanShowWifiHotspotCached;
public static boolean isSSIDTooLong(String ssid) {
if (TextUtils.isEmpty(ssid)) {
return false;
@@ -240,4 +250,62 @@ public class WifiUtils extends com.android.settingslib.wifi.WifiUtils {
return WifiEntry.SECURITY_NONE;
}
/**
* Check if Wi-Fi hotspot settings can be displayed.
*
* @param context Context of caller
* @return true if Wi-Fi hotspot settings can be displayed
*/
public static boolean checkShowWifiHotspot(Context context) {
if (context == null) return false;
boolean showWifiHotspotSettings =
context.getResources().getBoolean(R.bool.config_show_wifi_hotspot_settings);
if (!showWifiHotspotSettings) {
Log.w(TAG, "config_show_wifi_hotspot_settings:false");
return false;
}
WifiManager wifiManager = context.getSystemService(WifiManager.class);
if (wifiManager == null) {
Log.e(TAG, "WifiManager is null");
return false;
}
TetheringManager tetheringManager = context.getSystemService(TetheringManager.class);
if (tetheringManager == null) {
Log.e(TAG, "TetheringManager is null");
return false;
}
String[] wifiRegexs = tetheringManager.getTetherableWifiRegexs();
if (wifiRegexs == null || wifiRegexs.length == 0) {
Log.w(TAG, "TetherableWifiRegexs is empty");
return false;
}
return true;
}
/**
* Return the cached result to see if Wi-Fi hotspot settings can be displayed.
*
* @param context Context of caller
* @return true if Wi-Fi hotspot settings can be displayed
*/
public static boolean canShowWifiHotspot(Context context) {
if (sCanShowWifiHotspotCached == null) {
sCanShowWifiHotspotCached = checkShowWifiHotspot(context);
}
return sCanShowWifiHotspotCached;
}
/**
* Sets the sCanShowWifiHotspotCached for testing purposes.
*
* @param cached Cached value for #canShowWifiHotspot()
*/
@VisibleForTesting
public static void setCanShowWifiHotspotCached(Boolean cached) {
sCanShowWifiHotspotCached = cached;
}
}

View File

@@ -16,9 +16,10 @@
package com.android.settings.wifi.tether;
import static com.android.settings.wifi.WifiUtils.canShowWifiHotspot;
import android.annotation.NonNull;
import android.content.Context;
import android.net.TetheringManager;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiClient;
import android.net.wifi.WifiManager;
@@ -46,7 +47,6 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
private static final String WIFI_TETHER_SETTINGS = "wifi_tether";
private boolean mIsWifiTetherable;
private WifiManager mWifiManager;
private boolean mIsWifiTetheringAllow;
private int mSoftApState;
@@ -57,8 +57,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) {
this(context, lifecycle,
context.getSystemService(WifiManager.class),
context.getSystemService(TetheringManager.class),
context.getApplicationContext().getSystemService(WifiManager.class),
true /* initSoftApManager */,
WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context));
}
@@ -68,15 +67,9 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
Context context,
Lifecycle lifecycle,
WifiManager wifiManager,
TetheringManager tetheringManager,
boolean initSoftApManager,
boolean isWifiTetheringAllow) {
super(context);
final String[] wifiRegexs = tetheringManager.getTetherableWifiRegexs();
if (wifiRegexs != null && wifiRegexs.length != 0) {
mIsWifiTetherable = true;
}
mIsWifiTetheringAllow = isWifiTetheringAllow;
if (!isWifiTetheringAllow) return;
@@ -92,7 +85,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
@Override
public boolean isAvailable() {
return mIsWifiTetherable && !Utils.isMonkeyRunning();
return canShowWifiHotspot(mContext) && !Utils.isMonkeyRunning();
}
@Override

View File

@@ -18,6 +18,8 @@ package com.android.settings.wifi.tether;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_CHANGED_ACTION;
import static com.android.settings.wifi.WifiUtils.canShowWifiHotspot;
import android.app.settings.SettingsEnums;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -107,6 +109,13 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (!canShowWifiHotspot(getContext())) {
Log.e(TAG, "can not launch Wi-Fi hotspot settings"
+ " because the config is not set to show.");
finish();
return;
}
setIfOnlyAvailableForAdmins(true);
mUnavailable = isUiRestricted() || !mWifiRestriction.isHotspotAvailable(getContext());
}

View File

@@ -66,7 +66,18 @@ public class MediaOutputUtilsTest {
@Test
public void getActiveLocalMediaController_localMediaPlaying_returnController() {
initPlayback();
initPlayback(PlaybackState.STATE_PLAYING);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isEqualTo(
mMediaController);
}
@Test
public void getActiveLocalMediaController_localMediaPause_returnController() {
initPlayback(PlaybackState.STATE_PAUSED);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -113,6 +124,44 @@ public class MediaOutputUtilsTest {
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_localMediaNone_returnNull() {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_NONE, 0, 1)
.build();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_localMediaError_returnNull() {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
100,
10,
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_ERROR, 0, 1)
.build();
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
@Test
public void getActiveLocalMediaController_bothHaveRemoteMediaAndLocalMedia_returnNull() {
mMediaControllers.clear();
@@ -130,7 +179,7 @@ public class MediaOutputUtilsTest {
mMediaControllers.add(remoteMediaController);
mMediaControllers.add(mMediaController);
initPlayback();
initPlayback(PlaybackState.STATE_PLAYING);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -155,7 +204,7 @@ public class MediaOutputUtilsTest {
final MediaController remoteMediaController = mock(MediaController.class);
mMediaControllers.add(remoteMediaController);
initPlayback();
initPlayback(PlaybackState.STATE_PLAYING);
when(mMediaController.getPlaybackInfo()).thenReturn(mPlaybackInfo);
when(mMediaController.getPlaybackState()).thenReturn(mPlaybackState);
@@ -165,7 +214,7 @@ public class MediaOutputUtilsTest {
assertThat(MediaOutputUtils.getActiveLocalMediaController(mMediaSessionManager)).isNull();
}
private void initPlayback() {
private void initPlayback(int playbackState) {
mPlaybackInfo = new MediaController.PlaybackInfo(
MediaController.PlaybackInfo.PLAYBACK_TYPE_LOCAL,
VolumeProvider.VOLUME_CONTROL_ABSOLUTE,
@@ -174,7 +223,7 @@ public class MediaOutputUtilsTest {
new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
null);
mPlaybackState = new PlaybackState.Builder()
.setState(PlaybackState.STATE_PLAYING, 0, 1)
.setState(playbackState, 0, 1)
.build();
}
}

View File

@@ -16,13 +16,14 @@
package com.android.settings.wifi.tether;
import static com.android.settings.wifi.WifiUtils.setCanShowWifiHotspotCached;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.net.TetheringManager;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
@@ -62,8 +63,6 @@ public class WifiTetherPreferenceControllerTest {
@Mock
private Lifecycle mLifecycle;
@Mock
private TetheringManager mTetheringManager;
@Mock
private WifiManager mWifiManager;
@Mock
private PreferenceScreen mScreen;
@@ -74,38 +73,37 @@ public class WifiTetherPreferenceControllerTest {
@Before
public void setUp() {
setCanShowWifiHotspotCached(true);
FakeFeatureFactory.setupForTest();
mPreference = new PrimarySwitchPreference(mContext);
when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build();
when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration);
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */);
false /* initSoftApManager */, true /* isWifiTetheringAllow */);
mController.displayPreference(mScreen);
}
@Test
public void isAvailable_noTetherRegex_shouldReturnFalse() {
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[]{});
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */);
public void isAvailable_canNotShowWifiHotspot_shouldReturnFalse() {
setCanShowWifiHotspotCached(false);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void isAvailable_hasTetherRegex_shouldReturnTrue() {
public void isAvailable_canShowWifiHostspot_shouldReturnTrue() {
setCanShowWifiHotspotCached(true);
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void displayPreference_wifiTetheringNotAllowed_shouldDisable() {
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
mTetheringManager, false /* initSoftApManager */, false /* isWifiTetheringAllow */);
false /* initSoftApManager */, false /* isWifiTetheringAllow */);
mController.displayPreference(mScreen);
@@ -116,7 +114,7 @@ public class WifiTetherPreferenceControllerTest {
@Test
public void displayPreference_wifiTetheringAllowed_shouldEnable() {
mController = new WifiTetherPreferenceController(mContext, mLifecycle, mWifiManager,
mTetheringManager, false /* initSoftApManager */, true /* isWifiTetheringAllow */);
false /* initSoftApManager */, true /* isWifiTetheringAllow */);
mController.displayPreference(mScreen);

View File

@@ -16,6 +16,8 @@
package com.android.settings.wifi.tether;
import static com.android.settings.wifi.WifiUtils.setCanShowWifiHotspotCached;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -42,6 +44,7 @@ import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.R;
import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowFragment;
@@ -55,6 +58,8 @@ import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.util.ReflectionHelpers;
import java.util.List;
@@ -88,6 +93,7 @@ public class WifiTetherSettingsTest {
@Before
public void setUp() {
setCanShowWifiHotspotCached(true);
doReturn(mWifiManager).when(mContext).getSystemService(WifiManager.class);
doReturn(mConnectivityManager)
.when(mContext).getSystemService(Context.CONNECTIVITY_SERVICE);
@@ -100,6 +106,17 @@ public class WifiTetherSettingsTest {
mWifiTetherSettings = new WifiTetherSettings(mWifiRestriction);
}
@Test
@Config(shadows = ShadowRestrictedDashboardFragment.class)
public void onCreate_canNotShowWifiHotspot_shouldFinish() {
setCanShowWifiHotspotCached(false);
mWifiTetherSettings = spy(new WifiTetherSettings(mWifiRestriction));
mWifiTetherSettings.onCreate(null);
verify(mWifiTetherSettings).finish();
}
@Test
@Config(shadows = ShadowFragment.class)
public void onStart_uiIsRestricted_removeAllPreferences() {
@@ -219,4 +236,13 @@ public class WifiTetherSettingsTest {
mWifiTetherSettings.onCreate(Bundle.EMPTY);
}
@Implements(RestrictedDashboardFragment.class)
public static final class ShadowRestrictedDashboardFragment {
@Implementation
public void onCreate(Bundle icicle) {
// do nothing
}
}
}

View File

@@ -21,19 +21,53 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources;
import android.net.TetheringManager;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.R;
import com.android.wifitrackerlib.WifiEntry;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
@RunWith(AndroidJUnit4.class)
public class WifiUtilsTest {
static final String[] WIFI_REGEXS = {"wifi_regexs"};
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Spy
Context mContext = ApplicationProvider.getApplicationContext();
@Mock
Resources mResources;
@Mock
WifiManager mWifiManager;
@Mock
TetheringManager mTetheringManager;
@Before
public void setUp() {
when(mContext.getResources()).thenReturn(mResources);
when(mResources.getBoolean(R.bool.config_show_wifi_hotspot_settings)).thenReturn(true);
when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
when(mContext.getSystemService(TetheringManager.class)).thenReturn(mTetheringManager);
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(WIFI_REGEXS);
}
@Test
public void testSSID() {
assertThat(WifiUtils.isSSIDTooLong("123")).isFalse();
@@ -108,4 +142,53 @@ public class WifiUtilsTest {
WifiConfiguration config = WifiUtils.getWifiConfig(null /* wifiEntry */,
null /* scanResult */);
}
@Test
public void checkShowWifiHotspot_allReady_returnTrue() {
assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isTrue();
}
@Test
public void checkShowWifiHotspot_contextIsNull_returnFalse() {
assertThat(WifiUtils.checkShowWifiHotspot(null)).isFalse();
}
@Test
public void checkShowWifiHotspot_configIsNotShow_returnFalse() {
when(mResources.getBoolean(R.bool.config_show_wifi_hotspot_settings)).thenReturn(false);
assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isFalse();
}
@Test
public void checkShowWifiHotspot_wifiManagerIsNull_returnFalse() {
when(mContext.getSystemService(WifiManager.class)).thenReturn(null);
assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isFalse();
}
@Test
public void checkShowWifiHotspot_tetheringManagerIsNull_returnFalse() {
when(mContext.getSystemService(TetheringManager.class)).thenReturn(null);
assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isFalse();
}
@Test
public void checkShowWifiHotspot_wifiRegexsIsEmpty_returnFalse() {
when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(null);
assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isFalse();
}
@Test
public void canShowWifiHotspot_cachedIsReady_returnCached() {
WifiUtils.setCanShowWifiHotspotCached(true);
assertThat(WifiUtils.canShowWifiHotspot(null)).isTrue();
WifiUtils.setCanShowWifiHotspotCached(false);
assertThat(WifiUtils.canShowWifiHotspot(null)).isFalse();
}
}