diff options
| author | 2017-03-23 01:26:47 +0000 | |
|---|---|---|
| committer | 2017-03-23 01:26:53 +0000 | |
| commit | 8efea16f1effe474a7c8d806a3609f948c3beae5 (patch) | |
| tree | 752ee0899a400c5afb2e5f3a0f312b88f9d1dadf | |
| parent | 19091a0fbd58bfe15bd4408183a75583716d278c (diff) | |
| parent | 035ff93430b71ef856f299c9f676f8d340403e16 (diff) | |
Merge "Move two target preference layout from settings to lib"
5 files changed, 293 insertions, 3 deletions
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 000000000000..5446acebeeb3 --- /dev/null +++ b/packages/SettingsLib/res/layout/preference_two_target.xml @@ -0,0 +1,93 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> + +<!-- Based off preference_material_settings.xml except that ripple on only on the left side. --> +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:minHeight="?android:attr/listPreferredItemHeight" + android:gravity="center_vertical" + android:background="@android:color/transparent" + android:clipToPadding="false"> + + <LinearLayout + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_weight="1" + android:background="?android:attr/selectableItemBackground" + android:gravity="start|center_vertical" + android:paddingStart="?android:attr/listPreferredItemPaddingStart"> + + <LinearLayout + android:id="@+id/icon_container" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:minWidth="60dp" + android:orientation="horizontal" + android:paddingEnd="12dp" + android:paddingTop="4dp" + android:paddingBottom="4dp"> + <com.android.internal.widget.PreferenceImageView + android:id="@android:id/icon" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:maxWidth="48dp" + android:maxHeight="48dp" /> + </LinearLayout> + + <RelativeLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_weight="1" + android:paddingTop="16dp" + android:paddingBottom="16dp"> + + <TextView + android:id="@android:id/title" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:singleLine="true" + android:textAppearance="?android:attr/textAppearanceListItem" + android:ellipsize="marquee" /> + + <TextView + android:id="@android:id/summary" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_below="@android:id/title" + android:layout_alignStart="@android:id/title" + android:textAppearance="?android:attr/textAppearanceListItemSecondary" + android:textColor="?android:attr/textColorSecondary" + android:maxLines="10" /> + + </RelativeLayout> + + </LinearLayout> + + <include layout="@layout/preference_two_target_divider" /> + + <!-- Preference should place its actual preference widget here. --> + <LinearLayout + android:id="@android:id/widget_frame" + android:layout_width="wrap_content" + android:layout_height="match_parent" + android:minWidth="64dp" + android:gravity="center" + android:orientation="vertical" /> + +</LinearLayout> 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 000000000000..ced61427a792 --- /dev/null +++ b/packages/SettingsLib/res/layout/preference_two_target_divider.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> + +<LinearLayout + xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/two_target_divider" + android:layout_width="wrap_content" + android:layout_height="match_parent" + android:gravity="start|center_vertical" + android:orientation="horizontal" + android:paddingTop="16dp" + android:paddingBottom="16dp"> + <View + android:layout_width="1dp" + android:layout_height="match_parent" + android:background="?android:attr/dividerVertical" /> +</LinearLayout>
\ 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 b30de64418cc..9f21dd21d6ca 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); } @@ -56,6 +54,16 @@ public class RestrictedPreference extends Preference { } @Override + protected int getSecondTargetResId() { + return R.layout.restricted_icon; + } + + @Override + protected boolean shouldHideSecondTarget() { + return !isDisabledByAdmin(); + } + + @Override public void onBindViewHolder(PreferenceViewHolder holder) { super.onBindViewHolder(holder); mHelper.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 000000000000..1c161dffced9 --- /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 000000000000..59eca257443c --- /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); + } +} |