diff --git a/packages/SettingsLib/res/layout/preference_two_target.xml b/packages/SettingsLib/res/layout/preference_two_target.xml
new file mode 100644
index 0000000000000..5446acebeeb36
--- /dev/null
+++ b/packages/SettingsLib/res/layout/preference_two_target.xml
@@ -0,0 +1,93 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/packages/SettingsLib/res/layout/preference_two_target_divider.xml b/packages/SettingsLib/res/layout/preference_two_target_divider.xml
new file mode 100644
index 0000000000000..ced61427a792e
--- /dev/null
+++ b/packages/SettingsLib/res/layout/preference_two_target_divider.xml
@@ -0,0 +1,31 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/packages/SettingsLib/src/com/android/settingslib/RestrictedPreference.java b/packages/SettingsLib/src/com/android/settingslib/RestrictedPreference.java
index b30de64418cc2..9f21dd21d6ca3 100644
--- a/packages/SettingsLib/src/com/android/settingslib/RestrictedPreference.java
+++ b/packages/SettingsLib/src/com/android/settingslib/RestrictedPreference.java
@@ -19,7 +19,6 @@ package com.android.settingslib;
import android.content.Context;
import android.os.UserHandle;
import android.support.v4.content.res.TypedArrayUtils;
-import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceViewHolder;
import android.util.AttributeSet;
@@ -31,13 +30,12 @@ import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
* Preference class that supports being disabled by a user restriction
* set by a device admin.
*/
-public class RestrictedPreference extends Preference {
+public class RestrictedPreference extends TwoTargetPreference {
RestrictedPreferenceHelper mHelper;
public RestrictedPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
- setWidgetLayoutResource(R.layout.restricted_icon);
mHelper = new RestrictedPreferenceHelper(context, this, attrs);
}
@@ -55,6 +53,16 @@ public class RestrictedPreference extends Preference {
this(context, null);
}
+ @Override
+ protected int getSecondTargetResId() {
+ return R.layout.restricted_icon;
+ }
+
+ @Override
+ protected boolean shouldHideSecondTarget() {
+ return !isDisabledByAdmin();
+ }
+
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
diff --git a/packages/SettingsLib/src/com/android/settingslib/TwoTargetPreference.java b/packages/SettingsLib/src/com/android/settingslib/TwoTargetPreference.java
new file mode 100644
index 0000000000000..1c161dffced9d
--- /dev/null
+++ b/packages/SettingsLib/src/com/android/settingslib/TwoTargetPreference.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2017 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.settingslib;
+
+import android.content.Context;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.PreferenceViewHolder;
+import android.util.AttributeSet;
+import android.view.View;
+
+public class TwoTargetPreference extends Preference {
+
+ public TwoTargetPreference(Context context, AttributeSet attrs,
+ int defStyleAttr, int defStyleRes) {
+ super(context, attrs, defStyleAttr, defStyleRes);
+ init();
+ }
+
+ public TwoTargetPreference(Context context, AttributeSet attrs, int defStyleAttr) {
+ super(context, attrs, defStyleAttr);
+ init();
+ }
+
+ public TwoTargetPreference(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ init();
+ }
+
+ public TwoTargetPreference(Context context) {
+ super(context);
+ init();
+ }
+
+ private void init() {
+ setLayoutResource(R.layout.preference_two_target);
+ final int secondTargetResId = getSecondTargetResId();
+ if (secondTargetResId != 0) {
+ setWidgetLayoutResource(secondTargetResId);
+ }
+ }
+
+ @Override
+ public void onBindViewHolder(PreferenceViewHolder holder) {
+ super.onBindViewHolder(holder);
+ final View divider = holder.findViewById(R.id.two_target_divider);
+ final View widgetFrame = holder.findViewById(android.R.id.widget_frame);
+ final boolean shouldHideSecondTarget = shouldHideSecondTarget();
+ if (divider != null) {
+ divider.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE);
+ }
+ if (widgetFrame != null) {
+ widgetFrame.setVisibility(shouldHideSecondTarget ? View.GONE : View.VISIBLE);
+ }
+ }
+
+ protected boolean shouldHideSecondTarget() {
+ return getSecondTargetResId() == 0;
+ }
+
+ protected int getSecondTargetResId() {
+ return 0;
+ }
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/TwoTargetPreferenceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/TwoTargetPreferenceTest.java
new file mode 100644
index 0000000000000..59eca257443c9
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/TwoTargetPreferenceTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2017 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.settingslib;
+
+import android.content.Context;
+import android.support.v7.preference.PreferenceViewHolder;
+import android.view.View;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(SettingLibRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class TwoTargetPreferenceTest {
+
+ private PreferenceViewHolder mViewHolder;
+ @Mock
+ private View mDivider;
+ @Mock
+ private View mWidgetFrame;
+ private TwoTargetPreference mPreference;
+ private Context mContext;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ mContext = RuntimeEnvironment.application;
+ mPreference = spy(new TwoTargetPreference(mContext));
+ mViewHolder = new PreferenceViewHolder(mock(View.class));
+ when(mViewHolder.findViewById(R.id.two_target_divider))
+ .thenReturn(mDivider);
+ when(mViewHolder.findViewById(android.R.id.widget_frame))
+ .thenReturn(mWidgetFrame);
+ }
+
+ @Test
+ public void bind_noSecondTarget_shouldNotDrawDivider() {
+ assertThat(mPreference.shouldHideSecondTarget()).isTrue();
+
+ mPreference.onBindViewHolder(mViewHolder);
+
+ verify(mDivider).setVisibility(View.GONE);
+ verify(mWidgetFrame).setVisibility(View.GONE);
+ }
+
+ @Test
+ public void bind_hasSecondTarget_shouldNotDrawDivider() {
+ doReturn(false).when(mPreference).shouldHideSecondTarget();
+
+ mPreference.onBindViewHolder(mViewHolder);
+
+ verify(mDivider).setVisibility(View.VISIBLE);
+ verify(mWidgetFrame).setVisibility(View.VISIBLE);
+ }
+}