|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.afwsamples.testdpc.common.preference; |
| 18 | + |
| 19 | +import android.content.Context; |
| 20 | +import android.support.annotation.StringRes; |
| 21 | +import android.support.v7.preference.EditTextPreference; |
| 22 | +import android.support.v7.preference.PreferenceManager; |
| 23 | +import android.support.v7.preference.PreferenceViewHolder; |
| 24 | +import android.util.AttributeSet; |
| 25 | + |
| 26 | +/** |
| 27 | + * An {@link EditTextPreference} which can be disabled via XML declared constraints. |
| 28 | + * |
| 29 | + * See {@link DpcPreferenceHelper} for details about constraints. |
| 30 | + */ |
| 31 | +public class DpcEditTextPreference extends EditTextPreference implements DpcPreferenceBase { |
| 32 | + private DpcPreferenceHelper mHelper; |
| 33 | + |
| 34 | + public DpcEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 35 | + super(context, attrs, defStyleAttr, defStyleRes); |
| 36 | + mHelper = new DpcPreferenceHelper(context, this, attrs); |
| 37 | + } |
| 38 | + |
| 39 | + public DpcEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) { |
| 40 | + this(context, attrs, defStyleAttr, 0); |
| 41 | + } |
| 42 | + |
| 43 | + public DpcEditTextPreference(Context context, AttributeSet attrs) { |
| 44 | + this(context, attrs, android.support.v7.preference.R.attr.editTextPreferenceStyle); |
| 45 | + } |
| 46 | + |
| 47 | + public DpcEditTextPreference(Context context) { |
| 48 | + this(context, null); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onBindViewHolder(PreferenceViewHolder holder) { |
| 53 | + super.onBindViewHolder(holder); |
| 54 | + mHelper.onBindViewHolder(holder); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void onAttachedToHierarchy(PreferenceManager preferenceManager) { |
| 59 | + mHelper.onAttachedToHierarchy(); |
| 60 | + super.onAttachedToHierarchy(preferenceManager); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void setEnabled(boolean enabled) { |
| 65 | + if (enabled && !mHelper.constraintsMet()) { |
| 66 | + return; // ignore |
| 67 | + } |
| 68 | + super.setEnabled(enabled); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void setMinSdkVersion(int version) { |
| 73 | + mHelper.setMinSdkVersion(version); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void setAdminConstraint(@DpcPreferenceHelper.AdminKind int adminConstraint) { |
| 78 | + mHelper.setAdminConstraint(adminConstraint); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void clearAdminConstraint() { |
| 83 | + mHelper.clearAdminConstraint(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void setUserConstraint(@DpcPreferenceHelper.UserKind int userConstraints) { |
| 88 | + mHelper.setUserConstraint(userConstraints); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void clearUserConstraint() { |
| 93 | + mHelper.clearUserConstraint(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void clearNonCustomConstraints() { |
| 98 | + mHelper.clearNonCustomConstraints(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void setCustomConstraint(CharSequence constraintSummary) { |
| 103 | + mHelper.setCustomConstraint(constraintSummary); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void setCustomConstraint(@StringRes int constraintSummaryRes) { |
| 108 | + mHelper.setCustomConstraint(getContext().getString(constraintSummaryRes)); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void clearCustomConstraint() { |
| 113 | + mHelper.clearCustomConstraint(); |
| 114 | + } |
| 115 | +} |
| 116 | + |
0 commit comments