Merge "Set toggle state for profile group" into sc-dev am: 405fac1ca0 am: 4f253570f8

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/15362614

Change-Id: I6f832b9d5e67acd4351a6bf045f57b8b370637e1
This commit is contained in:
Evan Severson
2021-07-29 20:15:17 +00:00
committed by Automerger Merge Worker
3 changed files with 129 additions and 25 deletions

View File

@@ -59,7 +59,8 @@ public abstract class SensorToggleController extends TogglePreferenceController
@Override @Override
public boolean setChecked(boolean isChecked) { public boolean setChecked(boolean isChecked) {
mSensorPrivacyManagerHelper.setSensorBlocked(SETTINGS, getSensor(), !isChecked); mSensorPrivacyManagerHelper.setSensorBlockedForProfileGroup(SETTINGS, getSensor(),
!isChecked);
return true; return true;
} }

View File

@@ -172,6 +172,27 @@ public class SensorPrivacyManagerHelper {
mSensorPrivacyManager.setSensorPrivacy(source, sensor, blocked, userId); mSensorPrivacyManager.setSensorPrivacy(source, sensor, blocked, userId);
} }
/**
* Sets the sensor privacy for the current profile group.
* @param source The source with which sensor privacy is toggled.
* @param sensor The sensor to set for
* @param blocked The state to set to
*/
public void setSensorBlockedForProfileGroup(int source, int sensor, boolean blocked) {
mSensorPrivacyManager.setSensorPrivacyForProfileGroup(source, sensor, blocked);
}
/**
* Sets the sensor privacy for the given user's profile group.
* @param source The source with which sensor privacy is toggled.
* @param sensor The sensor to set for
* @param blocked The state to set to
*/
public void setSensorBlockedForProfileGroup(int source, int sensor, boolean blocked,
int userId) {
mSensorPrivacyManager.setSensorPrivacyForProfileGroup(source, sensor, blocked, userId);
}
/** /**
* Adds a listener for the state of the current user. If the current user changes and the state * Adds a listener for the state of the current user. If the current user changes and the state
* of the new user is different, a callback will be received. * of the new user is different, a callback will be received.

View File

@@ -33,6 +33,8 @@ import android.content.Context;
import android.hardware.SensorPrivacyManager; import android.hardware.SensorPrivacyManager;
import android.hardware.SensorPrivacyManager.OnSensorPrivacyChangedListener; import android.hardware.SensorPrivacyManager.OnSensorPrivacyChangedListener;
import android.util.ArraySet; import android.util.ArraySet;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import com.android.settings.utils.SensorPrivacyManagerHelper; import com.android.settings.utils.SensorPrivacyManagerHelper;
@@ -54,10 +56,10 @@ public class SensorToggleControllerTest {
private Context mContext; private Context mContext;
@Mock @Mock
private SensorPrivacyManager mSensorPrivacyManager; private SensorPrivacyManager mSensorPrivacyManager;
private boolean mMicState; private SparseBooleanArray mMicState;
private boolean mCamState; private SparseBooleanArray mCamState;
private Set<OnSensorPrivacyChangedListener> mMicListeners; private SparseArray<Set<OnSensorPrivacyChangedListener>> mMicListeners;
private Set<OnSensorPrivacyChangedListener> mCamListeners; private SparseArray<Set<OnSensorPrivacyChangedListener>> mCamListeners;
@Before @Before
public void setUp() { public void setUp() {
@@ -73,39 +75,85 @@ public class SensorToggleControllerTest {
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
mMicState = false; mMicState = new SparseBooleanArray();
mCamState = false; mCamState = new SparseBooleanArray();
mMicListeners = new ArraySet<>(); mMicState.put(0, false);
mCamListeners = new ArraySet<>(); mCamState.put(0, false);
mMicState.put(10, false);
mCamState.put(10, false);
mMicListeners = new SparseArray<>();
mCamListeners = new SparseArray<>();
mMicListeners.put(0, new ArraySet<>());
mMicListeners.put(10, new ArraySet<>());
mCamListeners.put(0, new ArraySet<>());
mCamListeners.put(10, new ArraySet<>());
doReturn(0).when(mContext).getUserId(); doReturn(0).when(mContext).getUserId();
doReturn(mSensorPrivacyManager).when(mContext) doReturn(mSensorPrivacyManager).when(mContext)
.getSystemService(SensorPrivacyManager.class); .getSystemService(SensorPrivacyManager.class);
doAnswer(invocation -> mMicState) doAnswer(invocation -> mMicState.get(0))
.when(mSensorPrivacyManager).isSensorPrivacyEnabled(eq(MICROPHONE)); .when(mSensorPrivacyManager).isSensorPrivacyEnabled(eq(MICROPHONE));
doAnswer(invocation -> mCamState) doAnswer(invocation -> mCamState.get(0))
.when(mSensorPrivacyManager).isSensorPrivacyEnabled(eq(CAMERA)); .when(mSensorPrivacyManager).isSensorPrivacyEnabled(eq(CAMERA));
doAnswer(invocation -> mMicState.get(invocation.getArgument(1)))
.when(mSensorPrivacyManager).isSensorPrivacyEnabled(eq(MICROPHONE), anyInt());
doAnswer(invocation -> mCamState.get(invocation.getArgument(1)))
.when(mSensorPrivacyManager).isSensorPrivacyEnabled(eq(CAMERA), anyInt());
doAnswer(invocation -> { doAnswer(invocation -> {
mMicState = invocation.getArgument(1); mMicState.put(0, invocation.getArgument(2));
for (OnSensorPrivacyChangedListener listener : mMicListeners) { mMicState.put(10, invocation.getArgument(2));
listener.onSensorPrivacyChanged(MICROPHONE, mMicState); for (OnSensorPrivacyChangedListener listener : mMicListeners.get(0)) {
listener.onSensorPrivacyChanged(MICROPHONE, mMicState.get(0));
} }
return null; return null;
}).when(mSensorPrivacyManager).setSensorPrivacy(anyInt(), eq(MICROPHONE), anyBoolean()); }).when(mSensorPrivacyManager).setSensorPrivacy(anyInt(), eq(MICROPHONE), anyBoolean());
doAnswer(invocation -> { doAnswer(invocation -> {
mCamState = invocation.getArgument(1); mCamState.put(0, invocation.getArgument(2));
for (OnSensorPrivacyChangedListener listener : mMicListeners) { mCamState.put(10, invocation.getArgument(2));
listener.onSensorPrivacyChanged(CAMERA, mMicState); for (OnSensorPrivacyChangedListener listener : mMicListeners.get(0)) {
listener.onSensorPrivacyChanged(CAMERA, mMicState.get(0));
} }
return null; return null;
}).when(mSensorPrivacyManager).setSensorPrivacy(anyInt(), eq(CAMERA), anyBoolean()); }).when(mSensorPrivacyManager).setSensorPrivacy(anyInt(), eq(CAMERA), anyBoolean());
doAnswer(invocation -> mMicListeners.add(invocation.getArgument(1))) doAnswer(invocation -> {
mMicState.put(0, invocation.getArgument(2));
mMicState.put(10, invocation.getArgument(2));
for (OnSensorPrivacyChangedListener listener : mMicListeners.get(0)) {
listener.onSensorPrivacyChanged(MICROPHONE, mMicState.get(0));
}
for (OnSensorPrivacyChangedListener listener : mMicListeners.get(10)) {
listener.onSensorPrivacyChanged(MICROPHONE, mMicState.get(10));
}
return null;
}).when(mSensorPrivacyManager)
.setSensorPrivacyForProfileGroup(anyInt(), eq(MICROPHONE), anyBoolean());
doAnswer(invocation -> {
mCamState.put(0, invocation.getArgument(2));
mCamState.put(10, invocation.getArgument(2));
for (OnSensorPrivacyChangedListener listener : mCamListeners.get(0)) {
listener.onSensorPrivacyChanged(CAMERA, mCamState.get(0));
}
for (OnSensorPrivacyChangedListener listener : mCamListeners.get(10)) {
listener.onSensorPrivacyChanged(CAMERA, mCamState.get(10));
}
return null;
}).when(mSensorPrivacyManager)
.setSensorPrivacyForProfileGroup(anyInt(), eq(CAMERA), anyBoolean());
doAnswer(invocation -> mMicListeners.get(0).add(invocation.getArgument(1)))
.when(mSensorPrivacyManager).addSensorPrivacyListener(eq(MICROPHONE), any()); .when(mSensorPrivacyManager).addSensorPrivacyListener(eq(MICROPHONE), any());
doAnswer(invocation -> mCamListeners.add(invocation.getArgument(1))) doAnswer(invocation -> mCamListeners.get(0).add(invocation.getArgument(1)))
.when(mSensorPrivacyManager).addSensorPrivacyListener(eq(CAMERA), any()); .when(mSensorPrivacyManager).addSensorPrivacyListener(eq(CAMERA), any());
doAnswer(invocation -> mMicListeners.get(invocation.getArgument(2))
.add(invocation.getArgument(1))).when(mSensorPrivacyManager)
.addSensorPrivacyListener(eq(MICROPHONE), anyInt(), any());
doAnswer(invocation -> mCamListeners.get(invocation.getArgument(2))
.add(invocation.getArgument(1))).when(mSensorPrivacyManager)
.addSensorPrivacyListener(eq(CAMERA), anyInt(), any());
} }
@Test @Test
@@ -143,7 +191,7 @@ public class SensorToggleControllerTest {
mSensorPrivacyManager.setSensorPrivacy(OTHER, MICROPHONE, false); mSensorPrivacyManager.setSensorPrivacy(OTHER, MICROPHONE, false);
MicToggleController micToggleController = new MicToggleController(mContext, "mic_toggle"); MicToggleController micToggleController = new MicToggleController(mContext, "mic_toggle");
micToggleController.setChecked(false); micToggleController.setChecked(false);
assertTrue(mMicState); assertTrue(mMicState.get(0));
} }
@Test @Test
@@ -151,7 +199,23 @@ public class SensorToggleControllerTest {
mSensorPrivacyManager.setSensorPrivacy(OTHER, MICROPHONE, true); mSensorPrivacyManager.setSensorPrivacy(OTHER, MICROPHONE, true);
MicToggleController micToggleController = new MicToggleController(mContext, "mic_toggle"); MicToggleController micToggleController = new MicToggleController(mContext, "mic_toggle");
micToggleController.setChecked(true); micToggleController.setChecked(true);
assertFalse(mMicState); assertFalse(mMicState.get(0));
}
@Test
public void isMicrophoneSensorPrivacyEnabledForProfileUser_uncheckMicToggle_returnTrue() {
mSensorPrivacyManager.setSensorPrivacy(OTHER, MICROPHONE, false);
MicToggleController micToggleController = new MicToggleController(mContext, "mic_toggle");
micToggleController.setChecked(false);
assertTrue(mMicState.get(10));
}
@Test
public void isMicrophoneSensorPrivacyEnabledProfileUser_checkMicToggle_returnFalse() {
mSensorPrivacyManager.setSensorPrivacy(OTHER, MICROPHONE, true);
MicToggleController micToggleController = new MicToggleController(mContext, "mic_toggle");
micToggleController.setChecked(true);
assertFalse(mMicState.get(10));
} }
@Test @Test
@@ -189,20 +253,38 @@ public class SensorToggleControllerTest {
} }
@Test @Test
public void isCameraSensorPrivacyEnabled_uncheckMicToggle_returnTrue() { public void isCameraSensorPrivacyEnabled_uncheckCanToggle_returnTrue() {
mSensorPrivacyManager.setSensorPrivacy(OTHER, CAMERA, false); mSensorPrivacyManager.setSensorPrivacy(OTHER, CAMERA, false);
CameraToggleController camToggleController = CameraToggleController camToggleController =
new CameraToggleController(mContext, "cam_toggle"); new CameraToggleController(mContext, "cam_toggle");
camToggleController.setChecked(false); camToggleController.setChecked(false);
assertTrue(mCamState); assertTrue(mCamState.get(0));
} }
@Test @Test
public void isCameraSensorPrivacyEnabled_checkMicToggle_returnFalse() { public void isCameraSensorPrivacyEnabled_checkCamToggle_returnFalse() {
mSensorPrivacyManager.setSensorPrivacy(OTHER, CAMERA, true); mSensorPrivacyManager.setSensorPrivacy(OTHER, CAMERA, true);
CameraToggleController camToggleController = CameraToggleController camToggleController =
new CameraToggleController(mContext, "cam_toggle"); new CameraToggleController(mContext, "cam_toggle");
camToggleController.setChecked(true); camToggleController.setChecked(true);
assertFalse(mCamState); assertFalse(mCamState.get(0));
}
@Test
public void isCameraSensorPrivacyEnabledForProfileUser_uncheckCamToggle_returnTrue() {
mSensorPrivacyManager.setSensorPrivacy(OTHER, CAMERA, false);
CameraToggleController camToggleController =
new CameraToggleController(mContext, "cam_toggle");
camToggleController.setChecked(false);
assertTrue(mCamState.get(10));
}
@Test
public void isCameraSensorPrivacyEnabledProfileUser_checkCamToggle_returnFalse() {
mSensorPrivacyManager.setSensorPrivacy(OTHER, CAMERA, true);
CameraToggleController camToggleController =
new CameraToggleController(mContext, "cam_toggle");
camToggleController.setChecked(true);
assertFalse(mCamState.get(10));
} }
} }