Merge "Update RestrictedSwitchPreference to have an additional summary." into nyc-dev

This commit is contained in:
Sudheer Shanka
2016-04-14 21:05:24 +00:00
committed by Android (Google) Code Review
3 changed files with 135 additions and 5 deletions

View File

@@ -0,0 +1,86 @@
<?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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:clipToPadding="false">
<LinearLayout
android:id="@+id/icon_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="60dp"
android:gravity="start|center_vertical"
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" />
<TextView android:id="@+id/additional_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/summary"
android:layout_alignStart="@android:id/summary"
android:textAppearance="?android:attr/textAppearanceListItemSecondary"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="10"
android:visibility="gone" />
</RelativeLayout>
<!-- 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:gravity="end|center_vertical"
android:paddingStart="16dp"
android:orientation="vertical" />
</LinearLayout>

View File

@@ -15,10 +15,18 @@
--> -->
<resources> <resources>
<declare-styleable name="RestrictedPreference"> <declare-styleable name="RestrictedPreference">
<!-- The user restriction on which the preference disabled by admin state will be based on. -->
<attr name="userRestriction" format="string" /> <attr name="userRestriction" format="string" />
<!-- If true then we can use enabled/disabled by admin strings for summary (android.R.id.summary). -->
<attr name="useAdminDisabledSummary" format="boolean" /> <attr name="useAdminDisabledSummary" format="boolean" />
<!-- If true, an additional summary will be added in addition to the existing summary and
this will be used for enabled/disabled by admin strings leaving android.R.id.summary untouched.
As such when this is true, useAdminDisabledSummary will be overwritten to false. -->
<attr name="useAdditionalSummary" format="boolean" />
</declare-styleable> </declare-styleable>
<declare-styleable name="WifiEncryptionState"> <declare-styleable name="WifiEncryptionState">
<attr name="state_encrypted" format="boolean" /> <attr name="state_encrypted" format="boolean" />
</declare-styleable> </declare-styleable>

View File

@@ -17,12 +17,14 @@
package com.android.settingslib; package com.android.settingslib;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray;
import android.os.UserHandle; import android.os.UserHandle;
import android.support.v4.content.res.TypedArrayUtils; import android.support.v4.content.res.TypedArrayUtils;
import android.support.v7.preference.PreferenceManager; import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceViewHolder; import android.support.v7.preference.PreferenceViewHolder;
import android.support.v14.preference.SwitchPreference; import android.support.v14.preference.SwitchPreference;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
@@ -34,12 +36,28 @@ import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
*/ */
public class RestrictedSwitchPreference extends SwitchPreference { public class RestrictedSwitchPreference extends SwitchPreference {
RestrictedPreferenceHelper mHelper; RestrictedPreferenceHelper mHelper;
boolean mUseAdditionalSummary = false;
public RestrictedSwitchPreference(Context context, AttributeSet attrs, public RestrictedSwitchPreference(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) { int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes); super(context, attrs, defStyleAttr, defStyleRes);
setWidgetLayoutResource(R.layout.restricted_switch_widget); setWidgetLayoutResource(R.layout.restricted_switch_widget);
mHelper = new RestrictedPreferenceHelper(context, this, attrs); mHelper = new RestrictedPreferenceHelper(context, this, attrs);
if (attrs != null) {
final TypedArray attributes = context.obtainStyledAttributes(attrs,
R.styleable.RestrictedPreference);
final TypedValue useAdditionalSummary =
attributes.peekValue(R.styleable.RestrictedPreference_useAdditionalSummary);
if (useAdditionalSummary != null) {
mUseAdditionalSummary =
(useAdditionalSummary.type == TypedValue.TYPE_INT_BOOLEAN
&& useAdditionalSummary.data != 0);
}
}
if (mUseAdditionalSummary) {
setLayoutResource(R.layout.restricted_switch_preference);
useAdminDisabledSummary(false);
}
} }
public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
@@ -67,11 +85,29 @@ public class RestrictedSwitchPreference extends SwitchPreference {
if (switchWidget != null) { if (switchWidget != null) {
switchWidget.setVisibility(isDisabledByAdmin() ? View.GONE : View.VISIBLE); switchWidget.setVisibility(isDisabledByAdmin() ? View.GONE : View.VISIBLE);
} }
final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary); if (mUseAdditionalSummary) {
if (summaryView != null && isDisabledByAdmin()) { final TextView additionalSummaryView = (TextView) holder.findViewById(
summaryView.setText( R.id.additional_summary);
isChecked() ? R.string.enabled_by_admin : R.string.disabled_by_admin); if (additionalSummaryView != null) {
summaryView.setVisibility(View.VISIBLE); if (isDisabledByAdmin()) {
additionalSummaryView.setText(
isChecked() ? R.string.enabled_by_admin : R.string.disabled_by_admin);
additionalSummaryView.setVisibility(View.VISIBLE);
}
} else {
additionalSummaryView.setVisibility(View.GONE);
}
} else {
final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
if (summaryView != null) {
if (isDisabledByAdmin()) {
summaryView.setText(
isChecked() ? R.string.enabled_by_admin : R.string.disabled_by_admin);
summaryView.setVisibility(View.VISIBLE);
}
}
// No need to change the visibility to GONE in the else case here since Preference class
// would have already changed it if there is no summary to display.
} }
} }