Skip to content

Commit e924170

Browse files
tony-makAndrewScull
authored andcommitted
Cleanup isManagedProfile
Change-Id: I97937b81560af7a497fda4523daaa3106da887a1 Fix: 36536209
1 parent 708b6a3 commit e924170

File tree

6 files changed

+18
-34
lines changed

6 files changed

+18
-34
lines changed

app/src/main/java/com/afwsamples/testdpc/FirstAccountReadyBroadcastReceiver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void onReceive(Context context, Intent intent) {
5252
final String action = intent.getAction();
5353
Log.d(TAG, "Received: " + action);
5454

55-
if (!Util.isManagedProfile2(context)) {
55+
if (!Util.isManagedProfileOwner(context)) {
5656
Log.d(TAG, "Not a Managed Profile case. Ignoring broadcast.");
5757
return;
5858
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
import android.content.Context;
2424
import android.os.Build.VERSION_CODES;
2525
import android.os.Bundle;
26-
import android.os.UserManager;
2726
import android.support.v13.app.FragmentTabHost;
2827
import android.support.v4.os.BuildCompat;
2928
import android.support.v7.preference.PreferenceManager;
30-
import android.util.Log;
3129
import android.view.LayoutInflater;
3230
import android.view.View;
3331
import android.view.ViewGroup;
@@ -68,7 +66,7 @@ public View onCreateView(
6866
tabHost.setup(getActivity(), getChildFragmentManager(), View.generateViewId());
6967

7068
final boolean showDualTabs =
71-
Util.isManagedProfile(getActivity()) && BuildCompat.isAtLeastN();
69+
Util.isManagedProfileOwner(getActivity()) && BuildCompat.isAtLeastN();
7270

7371
// Tab for the parent profile
7472
if (showDualTabs) {

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,38 +113,24 @@ public static void updateImageView(Context context, ImageView imageView, Uri uri
113113
}
114114
}
115115

116-
/** Allows to distinguish between Managed Profile and Profile Owner */
116+
/**
117+
* Return {@code true} iff we are the profile owner of a managed profile.
118+
* Note that profile owner can be in primary user and secondary user too.
119+
*/
117120
@TargetApi(VERSION_CODES.N)
118-
public static boolean isManagedProfile(Context context) {
121+
public static boolean isManagedProfileOwner(Context context) {
122+
final DevicePolicyManager dpm = getDevicePolicyManager(context);
123+
119124
if (BuildCompat.isAtLeastN()) {
120-
DevicePolicyManager dpm = getDevicePolicyManager(context);
121125
try {
122126
return dpm.isManagedProfile(DeviceAdminReceiver.getComponentName(context));
123-
} catch (SecurityException e) {
124-
// This is thrown if there is no active admin so not the managed profile
127+
} catch (SecurityException ex) {
128+
// This is thrown if we are neither profile owner nor device owner.
125129
return false;
126130
}
127-
} else {
128-
// If user has more than one profile, then we deal with managed profile.
129-
// Unfortunately there is no public API available to distinguish user profile owner
130-
// and managed profile owner. Thus using this hack.
131-
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
132-
return userManager.getUserProfiles().size() > 1;
133131
}
134-
}
135-
136-
/**
137-
* TODO: unify with {@link Util#isManagedProfile} (there must be one 'correct' way)
138-
* <p>
139-
* TODO: consider setting a target version for the method
140-
*/
141-
public static boolean isManagedProfile2(Context context) {
142-
final DevicePolicyManager dpm = getDevicePolicyManager(context);
143-
144-
// On pre-N devices, the only supported PO flow is managed profile. On N+ devices we need
145-
// to check whether we're running in a managed profile.
146-
return dpm.isProfileOwnerApp(context.getPackageName())
147-
&& (!BuildCompat.isAtLeastN() || Util.isManagedProfile(context));
132+
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
133+
return isProfileOwner(context) && userManager.getUserProfiles().size() > 1;
148134
}
149135

150136
@TargetApi(VERSION_CODES.M)

app/src/main/java/com/afwsamples/testdpc/common/preference/DpcPreferenceHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private int getCurrentUser() {
262262
return USER_PRIMARY_USER;
263263
}
264264

265-
if (Util.isManagedProfile(mContext)) {
265+
if (Util.isManagedProfileOwner(mContext)) {
266266

267267
return USER_MANAGED_PROFILE;
268268
}

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,9 @@ private void showPendingSystemUpdate() {
813813

814814
@TargetApi(Build.VERSION_CODES.O)
815815
private void lockNow() {
816-
if (BuildCompat.isAtLeastO() && Util.isManagedProfile(getActivity())) {
816+
if (BuildCompat.isAtLeastO() && Util.isManagedProfileOwner(getActivity())) {
817817
showLockNowPrompt();
818-
} else if (BuildCompat.isAtLeastN() && Util.isManagedProfile(getActivity())) {
818+
} else if (BuildCompat.isAtLeastN() && Util.isManagedProfileOwner(getActivity())) {
819819
// Always call lock now on the parent for managed profile on N
820820
mDevicePolicyManager.getParentProfileInstance(mAdminComponentName).lockNow();
821821
} else {
@@ -1559,7 +1559,7 @@ private void loadPasswordCompliant() {
15591559

15601560
String summary;
15611561
boolean compliant = mDevicePolicyManager.isActivePasswordSufficient();
1562-
if (Util.isManagedProfile(getActivity())) {
1562+
if (Util.isManagedProfileOwner(getActivity())) {
15631563
DevicePolicyManager parentDpm
15641564
= mDevicePolicyManager.getParentProfileInstance(mAdminComponentName);
15651565
boolean parentCompliant = parentDpm.isActivePasswordSufficient();

app/src/main/java/com/afwsamples/testdpc/profilepolicy/ProfilePolicyManagementFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
128128

129129
@Override
130130
public boolean isAvailable(Context context) {
131-
return Util.isManagedProfile(context);
131+
return Util.isManagedProfileOwner(context);
132132
}
133133

134134
@Override

0 commit comments

Comments
 (0)