Skip to content

Commit 11d6cfc

Browse files
committed
Introduce XML declared constraints for preferences.
Different features require different API levels or are limited to certain admins and users. Previously, each fragment would maintain a list of preferences to restrict under certain conditions and had to disable those preferences when the fragment loaded. Moving these constraints into the XML is a simpler and more declarative approach. Change-Id: I56ab4581efeba192d3b24794f1b35304ac137ebe
1 parent 50bd422 commit 11d6cfc

File tree

12 files changed

+1044
-357
lines changed

12 files changed

+1044
-357
lines changed

app/src/main/java/com/afwsamples/testdpc/common/Util.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,20 @@ public static boolean isManagedProfile(Context context, ComponentName admin) {
123123
}
124124
}
125125

126+
@TargetApi(VERSION_CODES.M)
127+
public static boolean isPrimaryUser(Context context) {
128+
if (isBeforeM()) {
129+
// Assume only DO can be primary user. This is not perfect but the cases in which it is
130+
// wrong are uncommon and require adb to set up.
131+
final DevicePolicyManager dpm =
132+
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
133+
return dpm.isDeviceOwnerApp(context.getPackageName());
134+
} else {
135+
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
136+
return userManager.isSystemUser();
137+
}
138+
}
139+
126140
public static void disablePreference(Preference preference, int whyResId) {
127141
preference.setEnabled(false);
128142
preference.setSummary(whyResId);
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 {
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+
public void setAdminConstraint(int adminConstraint) {
72+
mHelper.setAdminConstraint(adminConstraint);
73+
}
74+
75+
public void clearAdminConstraint() {
76+
mHelper.clearAdminConstraint();
77+
}
78+
79+
public void setUserConstraint(int userConstraints) {
80+
mHelper.setUserConstraint(userConstraints);
81+
}
82+
83+
public void clearUserConstraint() {
84+
mHelper.clearUserConstraint();
85+
}
86+
87+
public void clearNonCustomConstraints() {
88+
mHelper.clearNonCustomConstraints();
89+
}
90+
91+
public void setCustomConstraint(CharSequence constraintSummary) {
92+
mHelper.setCustomConstraint(constraintSummary);
93+
}
94+
95+
public void setCustomConstraint(@StringRes int constraintSummaryRes) {
96+
mHelper.setCustomConstraint(getContext().getString(constraintSummaryRes));
97+
}
98+
99+
public void clearCustomConstraint() {
100+
mHelper.clearCustomConstraint();
101+
}
102+
}
103+
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.ListPreference;
22+
import android.support.v7.preference.PreferenceManager;
23+
import android.support.v7.preference.PreferenceViewHolder;
24+
import android.util.AttributeSet;
25+
26+
/**
27+
* An {@link ListPreference} which can be disabled via XML declared constraints.
28+
*
29+
* See {@link DpcPreferenceHelper} for details about constraints.
30+
*/
31+
public class DpcListPreference extends ListPreference {
32+
private DpcPreferenceHelper mHelper;
33+
34+
public DpcListPreference(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 DpcListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
40+
this(context, attrs, defStyleAttr, 0);
41+
}
42+
43+
public DpcListPreference(Context context, AttributeSet attrs) {
44+
this(context, attrs, android.support.v7.preference.R.attr.dialogPreferenceStyle);
45+
}
46+
47+
public DpcListPreference(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+
public void setAdminConstraint(int adminConstraint) {
72+
mHelper.setAdminConstraint(adminConstraint);
73+
}
74+
75+
public void clearAdminConstraint() {
76+
mHelper.clearAdminConstraint();
77+
}
78+
79+
public void setUserConstraint(int userConstraints) {
80+
mHelper.setUserConstraint(userConstraints);
81+
}
82+
83+
public void clearUserConstraint() {
84+
mHelper.clearUserConstraint();
85+
}
86+
87+
public void clearNonCustomConstraints() {
88+
mHelper.clearNonCustomConstraints();
89+
}
90+
91+
public void setCustomConstraint(CharSequence constraintSummary) {
92+
mHelper.setCustomConstraint(constraintSummary);
93+
}
94+
95+
public void setCustomConstraint(@StringRes int constraintSummaryRes) {
96+
mHelper.setCustomConstraint(getContext().getString(constraintSummaryRes));
97+
}
98+
99+
public void clearCustomConstraint() {
100+
mHelper.clearCustomConstraint();
101+
}
102+
}
103+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.Preference;
22+
import android.support.v7.preference.PreferenceManager;
23+
import android.support.v7.preference.PreferenceViewHolder;
24+
import android.util.AttributeSet;
25+
26+
/**
27+
* A {@link Preference} which can be disabled via XML declared constraints.
28+
*
29+
* See {@link DpcPreferenceHelper} for details about constraints.
30+
*/
31+
public class DpcPreference extends Preference {
32+
private DpcPreferenceHelper mHelper;
33+
34+
public DpcPreference(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 DpcPreference(Context context, AttributeSet attrs, int defStyleAttr) {
40+
this(context, attrs, defStyleAttr, 0);
41+
}
42+
43+
public DpcPreference(Context context, AttributeSet attrs) {
44+
this(context, attrs, android.support.v7.preference.R.attr.preferenceStyle);
45+
}
46+
47+
public DpcPreference(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+
public void setAdminConstraint(int adminConstraint) {
72+
mHelper.setAdminConstraint(adminConstraint);
73+
}
74+
75+
public void clearAdminConstraint() {
76+
mHelper.clearAdminConstraint();
77+
}
78+
79+
public void setUserConstraint(int userConstraints) {
80+
mHelper.setUserConstraint(userConstraints);
81+
}
82+
83+
public void clearUserConstraint() {
84+
mHelper.clearUserConstraint();
85+
}
86+
87+
public void clearNonCustomConstraints() {
88+
mHelper.clearNonCustomConstraints();
89+
}
90+
91+
public void setCustomConstraint(CharSequence constraintSummary) {
92+
mHelper.setCustomConstraint(constraintSummary);
93+
}
94+
95+
public void setCustomConstraint(@StringRes int constraintSummaryRes) {
96+
mHelper.setCustomConstraint(getContext().getString(constraintSummaryRes));
97+
}
98+
99+
public void clearCustomConstraint() {
100+
mHelper.clearCustomConstraint();
101+
}
102+
}

0 commit comments

Comments
 (0)