Add Settings UI for MTP transcoding over USB.

Enabling "Transcode exported media" means that the media transferred
from the device via MTP over USB would be transcoded, if necessary. This
switch also sets / unsets the sys.fuse.transcode_mtp system property.

"Transcode exported media" category would be accessible only when the
"File transfer / Audio Auto" or "PTP" radio button has been selected. We
are including PTP also since PTP supports transfer of videos.

Adding UsbDetailsTranscodeMtpController in a separate preference
category than "Charge connected device" primarily because they seemed to
be different in their own rights.

Here are a few screenshots:
https://screenshot.googleplex.com/8jeMstnSFsTtVCS.png
https://screenshot.googleplex.com/76hNz4iXp5dcX4M.png
https://screenshot.googleplex.com/AkTngE5hDDJCovv.png
https://screenshot.googleplex.com/4uQYGXuuSQLoz3w.png

BUG: 158466651
Test: manual testing.  Also added unit test.
Change-Id: I2603a9bffed3320c193cc08f867bd67d9848da18
(cherry picked from commit 974662936e)
This commit is contained in:
Manish Singh
2020-12-24 05:07:21 +00:00
parent dce50f1f77
commit 29a6c26d80
5 changed files with 285 additions and 1 deletions

View File

@@ -0,0 +1,178 @@
/*
* 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.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.os.SystemProperties;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreference;
import com.android.settings.testutils.shadow.ShadowUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class)
public class UsbDetailsTranscodeMtpControllerTest {
private static final String TRANSCODE_MTP_SYS_PROP_KEY = "sys.fuse.transcode_mtp";
private Context mContext;
private PreferenceCategory mPreference;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mScreen;
private UsbDetailsTranscodeMtpController mUnderTest;
@Mock
private UsbBackend mUsbBackend;
@Mock
private UsbDetailsFragment mFragment;
@Mock
private FragmentActivity mActivity;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPreferenceManager = new PreferenceManager(mContext);
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
when(mFragment.getActivity()).thenReturn(mActivity);
when(mActivity.getApplicationContext()).thenReturn(mContext);
when(mFragment.getContext()).thenReturn(mContext);
when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
mUnderTest = new UsbDetailsTranscodeMtpController(mContext, mFragment, mUsbBackend);
mPreference = new PreferenceCategory(mContext);
mPreference.setKey(mUnderTest.getPreferenceKey());
mScreen.addPreference(mPreference);
}
@Test
public void displayRefresh_noUsbConnection_shouldDisablePrefCategory() {
mUnderTest.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mUnderTest.refresh(false /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
DATA_ROLE_NONE);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void displayRefresh_noDataTransfer_shouldDisablePrefCategory() {
mUnderTest.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_NONE, POWER_ROLE_NONE,
DATA_ROLE_NONE);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void displayRefresh_noDataRole_shouldDisablePrefCategory() throws InterruptedException {
mUnderTest.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
DATA_ROLE_NONE);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void displayRefresh_fileTransfer_withAbsentProp_shouldCheck() {
mUnderTest.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
DATA_ROLE_NONE);
assertThat(getSwitchPreference().isChecked()).isTrue();
}
@Test
public void displayRefresh_fileTransfer_withUnsetProp_shouldUncheck() {
mUnderTest.displayPreference(mScreen);
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY, Boolean.toString(false));
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
DATA_ROLE_NONE);
assertThat(getSwitchPreference().isChecked()).isFalse();
}
@Test
public void displayRefresh_fileTransfer_withSetProp_shouldCheck() {
mUnderTest.displayPreference(mScreen);
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY, Boolean.toString(true));
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
DATA_ROLE_NONE);
assertThat(getSwitchPreference().isChecked()).isTrue();
}
@Test
public void click_checked_shouldSetSystemProperty() {
mUnderTest.displayPreference(mScreen);
getSwitchPreference().performClick();
assertThat(SystemProperties.getBoolean(TRANSCODE_MTP_SYS_PROP_KEY, false)).isTrue();
}
@Test
public void click_unChecked_shouldUnsetSystemProperty() {
mUnderTest.displayPreference(mScreen);
getSwitchPreference().performClick();
getSwitchPreference().performClick();
assertThat(SystemProperties.getBoolean(TRANSCODE_MTP_SYS_PROP_KEY, false)).isFalse();
}
@Test
@Config(shadows = ShadowUtils.class)
public void isAvailable_isMonkey_shouldReturnFalse() {
ShadowUtils.setIsUserAMonkey(true);
assertThat(mUnderTest.isAvailable()).isFalse();
}
private SwitchPreference getSwitchPreference() {
return (SwitchPreference) mPreference.getPreference(0);
}
}