Password settings: Fix work profile

Currently, the work profile password settings just point to the personal
profile app. This change fixes this using the forWork metadata tag, that
sets the work profile user in BasePreferenceController.

Also fixes and re-enables the
displayPreference_withPasswords_addsPreference test.

Fix: 192417100
Test: manual - work profile link works, password count is accurate
Test: atest PasswordsPreferenceControllerTest
Change-Id: I6345b69b9c03ff13b8e2784e69dc0188abc436e3
This commit is contained in:
Ahaan Ugale
2021-06-30 00:21:40 -07:00
parent f0d8071bc6
commit d4ce558f33
3 changed files with 63 additions and 25 deletions

View File

@@ -55,6 +55,7 @@ import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.AppPreference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -76,28 +77,30 @@ public class PasswordsPreferenceController extends BasePreferenceController
private LifecycleOwner mLifecycleOwner;
public PasswordsPreferenceController(Context context, String preferenceKey) {
this(context, preferenceKey,
AutofillServiceInfo.getAvailableServices(context, UserHandle.myUserId()));
}
@VisibleForTesting
public PasswordsPreferenceController(
Context context, String preferenceKey, List<AutofillServiceInfo> availableServices) {
super(context, preferenceKey);
mPm = context.getPackageManager();
mIconFactory = IconDrawableFactory.newInstance(mContext);
mServices = new ArrayList<>();
}
@OnLifecycleEvent(ON_CREATE)
void onCreate(LifecycleOwner lifecycleOwner) {
init(lifecycleOwner, AutofillServiceInfo.getAvailableServices(mContext, getUser()));
}
@VisibleForTesting
void init(LifecycleOwner lifecycleOwner, List<AutofillServiceInfo> availableServices) {
mLifecycleOwner = lifecycleOwner;
for (int i = availableServices.size() - 1; i >= 0; i--) {
final String passwordsActivity = availableServices.get(i).getPasswordsActivity();
if (TextUtils.isEmpty(passwordsActivity)) {
availableServices.remove(i);
}
}
mServices = availableServices;
}
@OnLifecycleEvent(ON_CREATE)
void onCreate(LifecycleOwner lifecycleOwner) {
mLifecycleOwner = lifecycleOwner;
// TODO: Reverse the loop above and add to mServices directly.
mServices.clear();
mServices.addAll(availableServices);
}
@Override
@@ -109,8 +112,7 @@ public class PasswordsPreferenceController extends BasePreferenceController
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final PreferenceGroup group = screen.findPreference(getPreferenceKey());
// TODO(b/169455298): Show work profile passwords too.
addPasswordPreferences(screen.getContext(), UserHandle.myUserId(), group);
addPasswordPreferences(screen.getContext(), getUser(), group);
}
private void addPasswordPreferences(
@@ -126,9 +128,15 @@ public class PasswordsPreferenceController extends BasePreferenceController
serviceInfo.applicationInfo,
user);
pref.setIcon(Utils.getSafeIcon(icon));
pref.setIntent(
new Intent(Intent.ACTION_MAIN)
.setClassName(serviceInfo.packageName, service.getPasswordsActivity()));
pref.setOnPreferenceClickListener(p -> {
final Intent intent =
new Intent(Intent.ACTION_MAIN)
.setClassName(
serviceInfo.packageName,
service.getPasswordsActivity());
prefContext.startActivityAsUser(intent, UserHandle.of(user));
return true;
});
// Set an empty summary to avoid a UI flicker when the value loads.
pref.setSummary(R.string.summary_placeholder);
@@ -213,4 +221,9 @@ public class PasswordsPreferenceController extends BasePreferenceController
}
}
}
private int getUser() {
UserHandle workUser = getWorkProfileUser();
return workUser != null ? workUser.getIdentifier() : UserHandle.myUserId();
}
}