Merge "Refactor sound setting -> work profile initialization logic."
This commit is contained in:
@@ -151,4 +151,41 @@
|
|||||||
android:targetClass="com.android.cellbroadcastreceiver.CellBroadcastSettings" />
|
android:targetClass="com.android.cellbroadcastreceiver.CellBroadcastSettings" />
|
||||||
</com.android.settingslib.RestrictedPreference>
|
</com.android.settingslib.RestrictedPreference>
|
||||||
|
|
||||||
|
<com.android.settings.WorkOnlyCategory
|
||||||
|
android:key="sound_work_settings_section"
|
||||||
|
android:title="@string/sound_work_settings">
|
||||||
|
|
||||||
|
<!-- Use the same sounds of the work profile -->
|
||||||
|
<SwitchPreference
|
||||||
|
android:key="work_use_personal_sounds"
|
||||||
|
android:title="@string/work_use_personal_sounds_title"
|
||||||
|
android:summary="@string/work_use_personal_sounds_summary"
|
||||||
|
android:disableDependentsState="true" />
|
||||||
|
|
||||||
|
<!-- Work phone ringtone -->
|
||||||
|
<com.android.settings.DefaultRingtonePreference
|
||||||
|
android:key="work_ringtone"
|
||||||
|
android:title="@string/work_ringtone_title"
|
||||||
|
android:dialogTitle="@string/work_alarm_ringtone_title"
|
||||||
|
android:ringtoneType="ringtone"
|
||||||
|
android:dependency="work_use_personal_sounds" />
|
||||||
|
|
||||||
|
<!-- Default work notification ringtone -->
|
||||||
|
<com.android.settings.DefaultRingtonePreference
|
||||||
|
android:key="work_notification_ringtone"
|
||||||
|
android:title="@string/work_notification_ringtone_title"
|
||||||
|
android:dialogTitle="@string/work_alarm_ringtone_title"
|
||||||
|
android:ringtoneType="notification"
|
||||||
|
android:dependency="work_use_personal_sounds" />
|
||||||
|
|
||||||
|
<!-- Default work alarm ringtone -->
|
||||||
|
<com.android.settings.DefaultRingtonePreference
|
||||||
|
android:key="work_alarm_ringtone"
|
||||||
|
android:title="@string/work_alarm_ringtone_title"
|
||||||
|
android:dialogTitle="@string/work_alarm_ringtone_title"
|
||||||
|
android:persistent="false"
|
||||||
|
android:ringtoneType="alarm"
|
||||||
|
android:dependency="work_use_personal_sounds" />
|
||||||
|
|
||||||
|
</com.android.settings.WorkOnlyCategory>
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
@@ -1,60 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<!-- Copyright (C) 2016 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.
|
|
||||||
-->
|
|
||||||
|
|
||||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
|
|
||||||
android:title="@string/sound_work_settings"
|
|
||||||
android:key="sound_work_settings">
|
|
||||||
|
|
||||||
<PreferenceCategory
|
|
||||||
android:key="sound_work_settings_section"
|
|
||||||
android:title="@string/sound_work_settings">
|
|
||||||
|
|
||||||
<!-- Use the same sounds of the work profile -->
|
|
||||||
<SwitchPreference
|
|
||||||
android:key="work_use_personal_sounds"
|
|
||||||
android:title="@string/work_use_personal_sounds_title"
|
|
||||||
android:summary="@string/work_use_personal_sounds_summary"
|
|
||||||
android:disableDependentsState="true" />
|
|
||||||
|
|
||||||
<!-- Work phone ringtone -->
|
|
||||||
<com.android.settings.DefaultRingtonePreference
|
|
||||||
android:key="work_ringtone"
|
|
||||||
android:title="@string/work_ringtone_title"
|
|
||||||
android:dialogTitle="@string/work_alarm_ringtone_title"
|
|
||||||
android:ringtoneType="ringtone"
|
|
||||||
android:dependency="work_use_personal_sounds" />
|
|
||||||
|
|
||||||
<!-- Default work notification ringtone -->
|
|
||||||
<com.android.settings.DefaultRingtonePreference
|
|
||||||
android:key="work_notification_ringtone"
|
|
||||||
android:title="@string/work_notification_ringtone_title"
|
|
||||||
android:dialogTitle="@string/work_alarm_ringtone_title"
|
|
||||||
android:ringtoneType="notification"
|
|
||||||
android:dependency="work_use_personal_sounds" />
|
|
||||||
|
|
||||||
<!-- Default work alarm ringtone -->
|
|
||||||
<com.android.settings.DefaultRingtonePreference
|
|
||||||
android:key="work_alarm_ringtone"
|
|
||||||
android:title="@string/work_alarm_ringtone_title"
|
|
||||||
android:dialogTitle="@string/work_alarm_ringtone_title"
|
|
||||||
android:persistent="false"
|
|
||||||
android:ringtoneType="alarm"
|
|
||||||
android:dependency="work_use_personal_sounds" />
|
|
||||||
|
|
||||||
</PreferenceCategory>
|
|
||||||
|
|
||||||
</PreferenceScreen>
|
|
@@ -25,7 +25,6 @@ import android.content.Context;
|
|||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.media.AudioSystem;
|
|
||||||
import android.media.Ringtone;
|
import android.media.Ringtone;
|
||||||
import android.media.RingtoneManager;
|
import android.media.RingtoneManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@@ -38,7 +37,6 @@ import android.support.v7.preference.Preference.OnPreferenceChangeListener;
|
|||||||
import android.support.v7.preference.PreferenceGroup;
|
import android.support.v7.preference.PreferenceGroup;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
import android.support.v7.preference.TwoStatePreference;
|
import android.support.v7.preference.TwoStatePreference;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import com.android.internal.annotations.VisibleForTesting;
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
@@ -62,18 +60,19 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
private static final String KEY_WORK_NOTIFICATION_RINGTONE = "work_notification_ringtone";
|
private static final String KEY_WORK_NOTIFICATION_RINGTONE = "work_notification_ringtone";
|
||||||
private static final String KEY_WORK_ALARM_RINGTONE = "work_alarm_ringtone";
|
private static final String KEY_WORK_ALARM_RINGTONE = "work_alarm_ringtone";
|
||||||
|
|
||||||
|
private final boolean mVoiceCapable;
|
||||||
|
private final UserManager mUserManager;
|
||||||
|
private final SoundSettings mParent;
|
||||||
|
private final AudioHelper mHelper;
|
||||||
|
|
||||||
private PreferenceGroup mWorkPreferenceCategory;
|
private PreferenceGroup mWorkPreferenceCategory;
|
||||||
private TwoStatePreference mWorkUsePersonalSounds;
|
private TwoStatePreference mWorkUsePersonalSounds;
|
||||||
private Preference mWorkPhoneRingtonePreference;
|
private Preference mWorkPhoneRingtonePreference;
|
||||||
private Preference mWorkNotificationRingtonePreference;
|
private Preference mWorkNotificationRingtonePreference;
|
||||||
private Preference mWorkAlarmRingtonePreference;
|
private Preference mWorkAlarmRingtonePreference;
|
||||||
private boolean mVoiceCapable;
|
|
||||||
private UserManager mUserManager;
|
|
||||||
private SoundSettings mParent;
|
|
||||||
private AudioHelper mHelper;
|
|
||||||
|
|
||||||
private @UserIdInt
|
@UserIdInt
|
||||||
int mManagedProfileId;
|
private int mManagedProfileId;
|
||||||
|
|
||||||
public WorkSoundPreferenceController(Context context, SoundSettings parent,
|
public WorkSoundPreferenceController(Context context, SoundSettings parent,
|
||||||
Lifecycle lifecycle) {
|
Lifecycle lifecycle) {
|
||||||
@@ -95,7 +94,10 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
// do nothing
|
mWorkPreferenceCategory = (PreferenceGroup) screen.findPreference(KEY_WORK_CATEGORY);
|
||||||
|
if (mWorkPreferenceCategory != null) {
|
||||||
|
mWorkPreferenceCategory.setVisible(isAvailable());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -106,7 +108,7 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
mContext.registerReceiver(mManagedProfileReceiver, managedProfileFilter);
|
mContext.registerReceiver(mManagedProfileReceiver, managedProfileFilter);
|
||||||
|
|
||||||
mManagedProfileId = mHelper.getManagedProfileId(mUserManager);
|
mManagedProfileId = mHelper.getManagedProfileId(mUserManager);
|
||||||
initWorkPreferences();
|
updateWorkPreferences();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -175,9 +177,9 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
return mHelper.createPackageContextAsUser(mManagedProfileId);
|
return mHelper.createPackageContextAsUser(mManagedProfileId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DefaultRingtonePreference initWorkPreference(String key) {
|
private DefaultRingtonePreference initWorkPreference(PreferenceGroup root, String key) {
|
||||||
DefaultRingtonePreference pref =
|
DefaultRingtonePreference pref =
|
||||||
(DefaultRingtonePreference) mParent.getPreferenceScreen().findPreference(key);
|
(DefaultRingtonePreference) root.findPreference(key);
|
||||||
pref.setOnPreferenceChangeListener(this);
|
pref.setOnPreferenceChangeListener(this);
|
||||||
|
|
||||||
// Required so that RingtonePickerActivity lists the work profile ringtones
|
// Required so that RingtonePickerActivity lists the work profile ringtones
|
||||||
@@ -185,24 +187,18 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
return pref;
|
return pref;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initWorkPreferences() {
|
private void updateWorkPreferences() {
|
||||||
if (mManagedProfileId == UserHandle.USER_NULL || !isAvailable()) {
|
if (mWorkPreferenceCategory == null) {
|
||||||
maybeRemoveWorkPreferences();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
final boolean isAvailable = isAvailable();
|
||||||
if (mWorkPreferenceCategory == null) {
|
mWorkPreferenceCategory.setVisible(isAvailable);
|
||||||
mParent.addPreferencesFromResource(R.xml.sound_work_settings);
|
if (!isAvailable) {
|
||||||
final PreferenceScreen screen = mParent.getPreferenceScreen();
|
return;
|
||||||
|
}
|
||||||
mWorkPreferenceCategory = (PreferenceGroup) screen.findPreference(KEY_WORK_CATEGORY);
|
if (mWorkUsePersonalSounds == null) {
|
||||||
mWorkUsePersonalSounds = (TwoStatePreference)
|
mWorkUsePersonalSounds = (TwoStatePreference)
|
||||||
screen.findPreference(KEY_WORK_USE_PERSONAL_SOUNDS);
|
mWorkPreferenceCategory.findPreference(KEY_WORK_USE_PERSONAL_SOUNDS);
|
||||||
mWorkPhoneRingtonePreference = initWorkPreference(KEY_WORK_PHONE_RINGTONE);
|
|
||||||
mWorkNotificationRingtonePreference = initWorkPreference(
|
|
||||||
KEY_WORK_NOTIFICATION_RINGTONE);
|
|
||||||
mWorkAlarmRingtonePreference = initWorkPreference(KEY_WORK_ALARM_RINGTONE);
|
|
||||||
|
|
||||||
mWorkUsePersonalSounds.setOnPreferenceChangeListener((Preference p, Object value) -> {
|
mWorkUsePersonalSounds.setOnPreferenceChangeListener((Preference p, Object value) -> {
|
||||||
if ((boolean) value) {
|
if ((boolean) value) {
|
||||||
UnifyWorkDialogFragment.show(mParent);
|
UnifyWorkDialogFragment.show(mParent);
|
||||||
@@ -212,14 +208,25 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
if (mWorkPhoneRingtonePreference == null) {
|
||||||
|
mWorkPhoneRingtonePreference = initWorkPreference(mWorkPreferenceCategory,
|
||||||
|
KEY_WORK_PHONE_RINGTONE);
|
||||||
|
}
|
||||||
|
if (mWorkNotificationRingtonePreference == null) {
|
||||||
|
mWorkNotificationRingtonePreference = initWorkPreference(mWorkPreferenceCategory,
|
||||||
|
KEY_WORK_NOTIFICATION_RINGTONE);
|
||||||
|
}
|
||||||
|
if (mWorkAlarmRingtonePreference == null) {
|
||||||
|
mWorkAlarmRingtonePreference = initWorkPreference(mWorkPreferenceCategory,
|
||||||
|
KEY_WORK_ALARM_RINGTONE);
|
||||||
|
}
|
||||||
if (!mVoiceCapable) {
|
if (!mVoiceCapable) {
|
||||||
mWorkPreferenceCategory.removePreference(mWorkPhoneRingtonePreference);
|
mWorkPreferenceCategory.removePreference(mWorkPhoneRingtonePreference);
|
||||||
mWorkPhoneRingtonePreference = null;
|
mWorkPhoneRingtonePreference = null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Context managedProfileContext = getManagedProfileContext();
|
final Context managedProfileContext = getManagedProfileContext();
|
||||||
if (Settings.Secure.getIntForUser(managedProfileContext.getContentResolver(),
|
if (Settings.Secure.getIntForUser(managedProfileContext.getContentResolver(),
|
||||||
Settings.Secure.SYNC_PARENT_SOUNDS, 0, mManagedProfileId) == 1) {
|
Settings.Secure.SYNC_PARENT_SOUNDS, 0, mManagedProfileId) == 1) {
|
||||||
enableWorkSyncSettings();
|
enableWorkSyncSettings();
|
||||||
@@ -237,13 +244,10 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
mWorkUsePersonalSounds.setChecked(true);
|
mWorkUsePersonalSounds.setChecked(true);
|
||||||
|
|
||||||
if (mWorkPhoneRingtonePreference != null) {
|
if (mWorkPhoneRingtonePreference != null) {
|
||||||
mWorkPhoneRingtonePreference.setSummary(
|
mWorkPhoneRingtonePreference.setSummary(R.string.work_sound_same_as_personal);
|
||||||
com.android.settings.R.string.work_sound_same_as_personal);
|
|
||||||
}
|
}
|
||||||
mWorkNotificationRingtonePreference.setSummary(
|
mWorkNotificationRingtonePreference.setSummary(R.string.work_sound_same_as_personal);
|
||||||
com.android.settings.R.string.work_sound_same_as_personal);
|
mWorkAlarmRingtonePreference.setSummary(R.string.work_sound_same_as_personal);
|
||||||
mWorkAlarmRingtonePreference.setSummary(
|
|
||||||
com.android.settings.R.string.work_sound_same_as_personal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disableWorkSync() {
|
private void disableWorkSync() {
|
||||||
@@ -274,28 +278,17 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
updateRingtoneName(managedProfileContext, RingtoneManager.TYPE_ALARM));
|
updateRingtoneName(managedProfileContext, RingtoneManager.TYPE_ALARM));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybeRemoveWorkPreferences() {
|
|
||||||
if (mWorkPreferenceCategory == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
mParent.getPreferenceScreen().removePreference(mWorkPreferenceCategory);
|
|
||||||
mWorkPreferenceCategory = null;
|
|
||||||
mWorkPhoneRingtonePreference = null;
|
|
||||||
mWorkNotificationRingtonePreference = null;
|
|
||||||
mWorkAlarmRingtonePreference = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onManagedProfileAdded(@UserIdInt int profileId) {
|
public void onManagedProfileAdded(@UserIdInt int profileId) {
|
||||||
if (mManagedProfileId == UserHandle.USER_NULL) {
|
if (mManagedProfileId == UserHandle.USER_NULL) {
|
||||||
mManagedProfileId = profileId;
|
mManagedProfileId = profileId;
|
||||||
initWorkPreferences();
|
updateWorkPreferences();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onManagedProfileRemoved(@UserIdInt int profileId) {
|
public void onManagedProfileRemoved(@UserIdInt int profileId) {
|
||||||
if (mManagedProfileId == profileId) {
|
if (mManagedProfileId == profileId) {
|
||||||
mManagedProfileId = mHelper.getManagedProfileId(mUserManager);
|
mManagedProfileId = mHelper.getManagedProfileId(mUserManager);
|
||||||
initWorkPreferences();
|
updateWorkPreferences();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,10 +322,9 @@ public class WorkSoundPreferenceController extends PreferenceController implemen
|
|||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
return new AlertDialog.Builder(getActivity())
|
return new AlertDialog.Builder(getActivity())
|
||||||
.setTitle(com.android.settings.R.string.work_sync_dialog_title)
|
.setTitle(R.string.work_sync_dialog_title)
|
||||||
.setMessage(com.android.settings.R.string.work_sync_dialog_message)
|
.setMessage(R.string.work_sync_dialog_message)
|
||||||
.setPositiveButton(com.android.settings.R.string.work_sync_dialog_yes,
|
.setPositiveButton(R.string.work_sync_dialog_yes, UnifyWorkDialogFragment.this)
|
||||||
UnifyWorkDialogFragment.this)
|
|
||||||
.setNegativeButton(android.R.string.no, null)
|
.setNegativeButton(android.R.string.no, null)
|
||||||
.create();
|
.create();
|
||||||
}
|
}
|
||||||
|
@@ -17,11 +17,10 @@
|
|||||||
package com.android.settings.notification;
|
package com.android.settings.notification;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Build.VERSION_CODES;
|
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceGroup;
|
import android.support.v7.preference.PreferenceCategory;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
import android.support.v7.preference.TwoStatePreference;
|
import android.support.v7.preference.TwoStatePreference;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
@@ -36,17 +35,15 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.Matchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.Matchers.anyInt;
|
import static org.mockito.Matchers.anyInt;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Matchers.eq;
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -65,6 +62,8 @@ public class WorkSoundPreferenceControllerTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private PreferenceScreen mScreen;
|
private PreferenceScreen mScreen;
|
||||||
@Mock
|
@Mock
|
||||||
|
private PreferenceCategory mWorkCategory;
|
||||||
|
@Mock
|
||||||
private TelephonyManager mTelephonyManager;
|
private TelephonyManager mTelephonyManager;
|
||||||
@Mock
|
@Mock
|
||||||
private AudioHelper mAudioHelper;
|
private AudioHelper mAudioHelper;
|
||||||
@@ -78,6 +77,17 @@ public class WorkSoundPreferenceControllerTest {
|
|||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
|
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
|
||||||
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
|
when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
|
||||||
|
when(mScreen.findPreference(KEY_WORK_CATEGORY))
|
||||||
|
.thenReturn(mWorkCategory);
|
||||||
|
when(mWorkCategory.findPreference(KEY_WORK_USE_PERSONAL_SOUNDS))
|
||||||
|
.thenReturn(mock(TwoStatePreference.class));
|
||||||
|
when(mWorkCategory.findPreference(KEY_WORK_PHONE_RINGTONE))
|
||||||
|
.thenReturn(mock(DefaultRingtonePreference.class));
|
||||||
|
when(mWorkCategory.findPreference(KEY_WORK_NOTIFICATION_RINGTONE))
|
||||||
|
.thenReturn(mock(DefaultRingtonePreference.class));
|
||||||
|
when(mWorkCategory.findPreference(KEY_WORK_ALARM_RINGTONE))
|
||||||
|
.thenReturn(mock(DefaultRingtonePreference.class));
|
||||||
|
|
||||||
mController = new WorkSoundPreferenceController(mContext, mFragment, null, mAudioHelper);
|
mController = new WorkSoundPreferenceController(mContext, mFragment, null, mAudioHelper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,45 +122,30 @@ public class WorkSoundPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onResume_available_shouldAddPreferenceCategory() {
|
public void onManagedProfileAdded_shouldDisplayPreferenceCategory() {
|
||||||
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
|
||||||
.thenReturn(UserHandle.myUserId());
|
|
||||||
when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
|
|
||||||
when(mAudioHelper.isSingleVolume()).thenReturn(false);
|
|
||||||
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
|
|
||||||
when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
|
|
||||||
mockWorkCategory();
|
|
||||||
|
|
||||||
mController.onResume();
|
|
||||||
|
|
||||||
verify(mFragment).addPreferencesFromResource(R.xml.sound_work_settings);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void onManagedProfileAdded_shouldAddPreferenceCategory() {
|
|
||||||
// Given a device without any managed profiles:
|
// Given a device without any managed profiles:
|
||||||
when(mAudioHelper.isSingleVolume()).thenReturn(false);
|
when(mAudioHelper.isSingleVolume()).thenReturn(false);
|
||||||
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
|
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
|
||||||
when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
|
when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
|
||||||
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
||||||
.thenReturn(UserHandle.USER_NULL);
|
.thenReturn(UserHandle.USER_NULL);
|
||||||
mockWorkCategory();
|
|
||||||
|
|
||||||
// When the fragment first resumes, the category should not appear.
|
// When the fragment first displays, the category should not appear.
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
verify(mWorkCategory).setVisible(false);
|
||||||
|
|
||||||
|
|
||||||
|
// However, when a managed profile is added later, the category should appear.
|
||||||
mController.onResume();
|
mController.onResume();
|
||||||
|
|
||||||
verify(mFragment, never()).addPreferencesFromResource(R.xml.sound_work_settings);
|
|
||||||
|
|
||||||
// However, when a managed profile is added after resuming, the category should appear.
|
|
||||||
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
||||||
.thenReturn(UserHandle.myUserId());
|
.thenReturn(UserHandle.myUserId());
|
||||||
mController.onManagedProfileAdded(UserHandle.myUserId());
|
mController.onManagedProfileAdded(UserHandle.myUserId());
|
||||||
|
|
||||||
verify(mFragment).addPreferencesFromResource(R.xml.sound_work_settings);
|
verify(mWorkCategory).setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onManagedProfileRemoved_shouldRemovePreferenceCategory() {
|
public void onManagedProfileRemoved_shouldHidePreferenceCategory() {
|
||||||
// Given a device with a managed profile:
|
// Given a device with a managed profile:
|
||||||
when(mAudioHelper.isSingleVolume()).thenReturn(false);
|
when(mAudioHelper.isSingleVolume()).thenReturn(false);
|
||||||
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
|
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
|
||||||
@@ -158,29 +153,44 @@ public class WorkSoundPreferenceControllerTest {
|
|||||||
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
||||||
.thenReturn(UserHandle.myUserId());
|
.thenReturn(UserHandle.myUserId());
|
||||||
when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
|
when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
|
||||||
mockWorkCategory();
|
|
||||||
|
|
||||||
// Which is in resumed state:
|
// Which is in resumed state:
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
mController.onResume();
|
mController.onResume();
|
||||||
|
|
||||||
// When a managed profile is removed, the category should be removed.
|
verify(mWorkCategory, times(2)).setVisible(true);
|
||||||
|
|
||||||
|
// When a managed profile is removed, the category should be hidden.
|
||||||
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
||||||
.thenReturn(UserHandle.USER_NULL);
|
.thenReturn(UserHandle.USER_NULL);
|
||||||
mController.onManagedProfileRemoved(UserHandle.myUserId());
|
mController.onManagedProfileRemoved(UserHandle.myUserId());
|
||||||
|
|
||||||
verify(mScreen).removePreference(mScreen.findPreference(KEY_WORK_CATEGORY));
|
verify(mWorkCategory).setVisible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void displayPreference_isAvailable_shouldShowPreferenceCategory() {
|
||||||
|
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
||||||
|
.thenReturn(UserHandle.myUserId());
|
||||||
|
when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(true);
|
||||||
|
when(mAudioHelper.isSingleVolume()).thenReturn(false);
|
||||||
|
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
|
||||||
|
when(mAudioHelper.createPackageContextAsUser(anyInt())).thenReturn(mContext);
|
||||||
|
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
verify(mWorkCategory).setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onResume_notAvailable_shouldNotAddPreferenceCategory() {
|
public void displayPreference_notAvailable_shouldHidePreferenceCategory() {
|
||||||
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
||||||
.thenReturn(UserHandle.USER_NULL);
|
.thenReturn(UserHandle.USER_NULL);
|
||||||
when(mAudioHelper.isSingleVolume()).thenReturn(true);
|
when(mAudioHelper.isSingleVolume()).thenReturn(true);
|
||||||
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
|
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
|
||||||
|
|
||||||
mController.onResume();
|
mController.displayPreference(mScreen);
|
||||||
|
verify(mWorkCategory).setVisible(false);
|
||||||
verify(mFragment, never()).addPreferencesFromResource(anyInt());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -206,27 +216,19 @@ public class WorkSoundPreferenceControllerTest {
|
|||||||
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
when(mAudioHelper.getManagedProfileId(any(UserManager.class)))
|
||||||
.thenReturn(UserHandle.myUserId());
|
.thenReturn(UserHandle.myUserId());
|
||||||
when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(false);
|
when(mAudioHelper.isUserUnlocked(any(UserManager.class), anyInt())).thenReturn(false);
|
||||||
mockWorkCategory();
|
|
||||||
|
|
||||||
// When resumed:
|
// When resumed:
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
mController.onResume();
|
mController.onResume();
|
||||||
|
|
||||||
// Sound preferences should explain that the profile isn't available yet.
|
verify(mWorkCategory, times(2)).setVisible(true);
|
||||||
verify(mScreen.findPreference(KEY_WORK_PHONE_RINGTONE)).setSummary(eq(notAvailable));
|
|
||||||
verify(mScreen.findPreference(KEY_WORK_NOTIFICATION_RINGTONE)).setSummary(eq(notAvailable));
|
|
||||||
verify(mScreen.findPreference(KEY_WORK_ALARM_RINGTONE)).setSummary(eq(notAvailable));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void mockWorkCategory() {
|
// Sound preferences should explain that the profile isn't available yet.
|
||||||
when(mScreen.findPreference(KEY_WORK_CATEGORY))
|
verify(mWorkCategory.findPreference(KEY_WORK_PHONE_RINGTONE))
|
||||||
.thenReturn(mock(PreferenceGroup.class));
|
.setSummary(eq(notAvailable));
|
||||||
when(mScreen.findPreference(KEY_WORK_USE_PERSONAL_SOUNDS))
|
verify(mWorkCategory.findPreference(KEY_WORK_NOTIFICATION_RINGTONE))
|
||||||
.thenReturn(mock(TwoStatePreference.class));
|
.setSummary(eq(notAvailable));
|
||||||
when(mScreen.findPreference(KEY_WORK_PHONE_RINGTONE))
|
verify(mWorkCategory.findPreference(KEY_WORK_ALARM_RINGTONE))
|
||||||
.thenReturn(mock(DefaultRingtonePreference.class));
|
.setSummary(eq(notAvailable));
|
||||||
when(mScreen.findPreference(KEY_WORK_NOTIFICATION_RINGTONE))
|
|
||||||
.thenReturn(mock(DefaultRingtonePreference.class));
|
|
||||||
when(mScreen.findPreference(KEY_WORK_ALARM_RINGTONE))
|
|
||||||
.thenReturn(mock(DefaultRingtonePreference.class));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user