Merge "Add buttons for new settings design" into main

This commit is contained in:
Becca Hughes
2024-01-19 22:05:14 +00:00
committed by Android (Google) Code Review
13 changed files with 833 additions and 114 deletions

View File

@@ -18,13 +18,17 @@ package com.android.settings.applications.credentials;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.ServiceInfo;
import android.credentials.CredentialProviderInfo;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.os.UserManager;
import android.service.autofill.AutofillServiceInfo;
import android.text.TextUtils;
import android.util.IconDrawableFactory;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -42,6 +46,11 @@ import java.util.Set;
* logic for each row in settings.
*/
public final class CombinedProviderInfo {
private static final String TAG = "CombinedProviderInfo";
private static final String SETTINGS_ACTIVITY_INTENT_ACTION = "android.intent.action.MAIN";
private static final String SETTINGS_ACTIVITY_INTENT_CATEGORY =
"android.intent.category.LAUNCHER";
private final List<CredentialProviderInfo> mCredentialProviderInfos;
private final @Nullable AutofillServiceInfo mAutofillServiceInfo;
private final boolean mIsDefaultAutofillProvider;
@@ -316,4 +325,44 @@ public final class CombinedProviderInfo {
return cmpi;
}
public static @Nullable Intent createSettingsActivityIntent(
@NonNull Context context,
@Nullable CharSequence packageName,
@Nullable CharSequence settingsActivity,
int currentUserId) {
if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(settingsActivity)) {
return null;
}
ComponentName cn =
new ComponentName(String.valueOf(packageName), String.valueOf(settingsActivity));
if (cn == null) {
Log.e(
TAG,
"Failed to deserialize settingsActivity attribute, we got: "
+ String.valueOf(packageName)
+ " and "
+ String.valueOf(settingsActivity));
return null;
}
Intent intent = new Intent(SETTINGS_ACTIVITY_INTENT_ACTION);
intent.addCategory(SETTINGS_ACTIVITY_INTENT_CATEGORY);
intent.setComponent(cn);
int contextUserId = context.getUser().getIdentifier();
if (currentUserId != contextUserId && UserManager.isHeadlessSystemUserMode()) {
Log.w(
TAG,
"onLeftSideClicked(): using context for current user ("
+ currentUserId
+ ") instead of user "
+ contextUserId
+ " on headless system user mode");
context = context.createContextAsUser(UserHandle.of(currentUserId), /* flags= */ 0);
}
return intent;
}
}

View File

@@ -96,9 +96,6 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
private static final String ALTERNATE_INTENT = "android.settings.SYNC_SETTINGS";
private static final String PRIMARY_INTENT = "android.settings.CREDENTIAL_PROVIDER";
private static final int MAX_SELECTABLE_PROVIDERS = 5;
private static final String SETTINGS_ACTIVITY_INTENT_ACTION = "android.intent.action.MAIN";
private static final String SETTINGS_ACTIVITY_INTENT_CATEGORY =
"android.intent.category.LAUNCHER";
private final PackageManager mPm;
private final List<CredentialProviderInfo> mServices;
@@ -495,6 +492,7 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
// If this provider is displayed at the top then we should not show it.
if (topProvider != null
&& topProvider.getApplicationInfo() != null
&& topProvider.getApplicationInfo().packageName.equals(packageName)) {
continue;
}
@@ -504,10 +502,6 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
continue;
}
// Get the settings activity.
CharSequence settingsActivity =
combinedInfo.getCredentialProviderInfos().get(0).getSettingsActivity();
Drawable icon = combinedInfo.getAppIcon(context, getUser());
CharSequence title = combinedInfo.getAppName(context);
@@ -519,7 +513,7 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
icon,
packageName,
combinedInfo.getSettingsSubtitle(),
settingsActivity);
combinedInfo.getSettingsActivity());
output.put(packageName, pref);
group.addPreference(pref);
}
@@ -658,43 +652,12 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl
@Override
public void onLeftSideClicked() {
if (settingsActivity == null) {
Log.w(TAG, "settingsActivity was null");
return;
Intent settingsIntent =
CombinedProviderInfo.createSettingsActivityIntent(
mContext, packageName, settingsActivity, getUser());
if (settingsIntent != null) {
mContext.startActivity(settingsIntent);
}
String settingsActivityStr = String.valueOf(settingsActivity);
ComponentName cn = ComponentName.unflattenFromString(settingsActivityStr);
if (cn == null) {
Log.w(
TAG,
"Failed to deserialize settingsActivity attribute, we got: "
+ settingsActivityStr);
return;
}
Intent intent = new Intent(SETTINGS_ACTIVITY_INTENT_ACTION);
intent.addCategory(SETTINGS_ACTIVITY_INTENT_CATEGORY);
intent.setComponent(cn);
Context context = mContext;
int currentUserId = getUser();
int contextUserId = context.getUser().getIdentifier();
if (currentUserId != contextUserId) {
Log.d(
TAG,
"onLeftSideClicked(): using context for current user ("
+ currentUserId
+ ") instead of user "
+ contextUserId
+ " on headless system user mode");
context =
context.createContextAsUser(
UserHandle.of(currentUserId), /* flags= */ 0);
}
context.startActivity(intent);
}
});

View File

@@ -16,31 +16,32 @@
package com.android.settings.applications.credentials;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.credentials.CredentialManager;
import android.credentials.CredentialProviderInfo;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.autofill.AutofillService;
import android.service.autofill.AutofillServiceInfo;
import android.text.TextUtils;
import android.view.autofill.AutofillManager;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.applications.defaultapps.DefaultAppPreferenceController;
import com.android.settingslib.applications.DefaultAppInfo;
import com.android.settingslib.widget.TwoTargetPreference;
import java.util.ArrayList;
import java.util.List;
public class DefaultCombinedPreferenceController extends DefaultAppPreferenceController
implements Preference.OnPreferenceClickListener {
public class DefaultCombinedPreferenceController extends DefaultAppPreferenceController {
private static final Intent AUTOFILL_PROBE = new Intent(AutofillService.SERVICE_INTERFACE);
private static final String TAG = "DefaultCombinedPreferenceController";
@@ -78,72 +79,80 @@ public class DefaultCombinedPreferenceController extends DefaultAppPreferenceCon
// Despite this method being called getSettingIntent this intent actually
// opens the primary picker. This is so that we can swap the cog and the left
// hand side presses to align the UX.
return new Intent(mContext, CredentialsPickerActivity.class);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
final String prefKey = getPreferenceKey();
final Preference preference = screen.findPreference(prefKey);
if (preference != null) {
preference.setOnPreferenceClickListener((Preference.OnPreferenceClickListener) this);
if (PrimaryProviderPreference.shouldUseNewSettingsUi()) {
// We need to return an empty intent here since the class we inherit
// from will throw an NPE if we return null and we don't want it to
// open anything since we added the buttons.
return new Intent();
}
return createIntentToOpenPicker();
}
@Override
public boolean onPreferenceClick(Preference preference) {
// Get the selected provider.
public void updateState(@NonNull Preference preference) {
final CombinedProviderInfo topProvider = getTopProvider();
if (topProvider == null) {
return false;
if (topProvider != null && mContext != null) {
updatePreferenceForProvider(
preference,
topProvider.getAppName(mContext),
topProvider.getSettingsSubtitle(),
topProvider.getAppIcon(mContext, getUser()),
createSettingsActivityIntent(
topProvider.getPackageName(), topProvider.getSettingsActivity()));
} else {
updatePreferenceForProvider(preference, null, null, null, null);
}
}
@VisibleForTesting
public void updatePreferenceForProvider(
Preference preference,
@Nullable CharSequence appName,
@Nullable String appSubtitle,
@Nullable Drawable appIcon,
@Nullable Intent settingsActivityIntent) {
if (appName == null) {
preference.setTitle(R.string.app_list_preference_none);
} else {
preference.setTitle(appName);
}
// If the top provider has a defined Credential Manager settings
// provider then we should open that up.
final String settingsActivity = topProvider.getSettingsActivity();
if (!TextUtils.isEmpty(settingsActivity)) {
final Intent intent =
new Intent(Intent.ACTION_MAIN)
.setComponent(
new ComponentName(
topProvider.getPackageName(), settingsActivity));
startActivity(intent);
return true;
if (appIcon == null) {
preference.setIcon(null);
} else {
preference.setIcon(Utils.getSafeIcon(appIcon));
}
return false;
preference.setSummary(appSubtitle);
if (preference instanceof PrimaryProviderPreference) {
PrimaryProviderPreference primaryPref = (PrimaryProviderPreference) preference;
primaryPref.setIconSize(TwoTargetPreference.ICON_SIZE_MEDIUM);
primaryPref.setDelegate(
new PrimaryProviderPreference.Delegate() {
public void onOpenButtonClicked() {
if (settingsActivityIntent != null) {
startActivity(settingsActivityIntent);
}
}
public void onChangeButtonClicked() {
startActivity(createIntentToOpenPicker());
}
});
// Hide the open button if there is no defined settings activity.
primaryPref.setOpenButtonVisible(settingsActivityIntent != null);
primaryPref.setButtonsVisible(appName != null);
}
}
private @Nullable CombinedProviderInfo getTopProvider() {
List<CombinedProviderInfo> providers = getAllProviders(getUser());
return CombinedProviderInfo.getTopProvider(providers);
return CombinedProviderInfo.getTopProvider(getAllProviders(getUser()));
}
@Override
protected DefaultAppInfo getDefaultAppInfo() {
CombinedProviderInfo topProvider = getTopProvider();
if (topProvider != null) {
ServiceInfo brandingService = topProvider.getBrandingService();
if (brandingService == null) {
return new DefaultAppInfo(
mContext,
mPackageManager,
getUser(),
topProvider.getApplicationInfo(),
topProvider.getSettingsSubtitle(),
true);
} else {
return new DefaultAppInfo(
mContext,
mPackageManager,
getUser(),
brandingService,
topProvider.getSettingsSubtitle(),
true);
}
}
return null;
}
@@ -180,4 +189,16 @@ public class DefaultCombinedPreferenceController extends DefaultAppPreferenceCon
protected int getUser() {
return UserHandle.myUserId();
}
/** Creates an intent to open the credential picker. */
private Intent createIntentToOpenPicker() {
return new Intent(mContext, CredentialsPickerActivity.class);
}
/** Creates an intent to open the settings activity of the primary provider (if available). */
public @Nullable Intent createSettingsActivityIntent(
@Nullable String packageName, @Nullable String settingsActivity) {
return CombinedProviderInfo.createSettingsActivityIntent(
mContext, packageName, settingsActivity, getUser());
}
}

View File

@@ -0,0 +1,215 @@
/*
* Copyright (C) 2024 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.
*/
package com.android.settings.applications.credentials;
import android.content.Context;
import android.credentials.flags.Flags;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.widget.GearPreference;
/**
* This preference is shown at the top of the "passwords & accounts" screen and allows the user to
* pick their primary credential manager provider.
*/
public class PrimaryProviderPreference extends GearPreference {
public static boolean shouldUseNewSettingsUi() {
return Flags.newSettingsUi();
}
private @Nullable Button mChangeButton = null;
private @Nullable Button mOpenButton = null;
private @Nullable View mButtonFrameView = null;
private @Nullable View mGearView = null;
private @Nullable Delegate mDelegate = null;
private boolean mButtonsVisible = false;
private boolean mOpenButtonVisible = false;
/** Called to send messages back to the parent controller. */
public static interface Delegate {
void onOpenButtonClicked();
void onChangeButtonClicked();
}
public PrimaryProviderPreference(
@NonNull Context context,
@NonNull AttributeSet attrs,
int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initializeNewSettingsUi();
}
public PrimaryProviderPreference(
@NonNull Context context,
@NonNull AttributeSet attrs) {
super(context, attrs);
initializeNewSettingsUi();
}
private void initializeNewSettingsUi() {
if (!shouldUseNewSettingsUi()) {
return;
}
// Change the layout to the new settings ui.
setLayoutResource(R.layout.preference_credential_manager_with_buttons);
}
@Override
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
if (shouldUseNewSettingsUi()) {
onBindViewHolderNewSettingsUi(holder);
} else {
onBindViewHolderOldSettingsUi(holder);
}
}
private void onBindViewHolderOldSettingsUi(PreferenceViewHolder holder) {
setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(@NonNull Preference preference) {
if (mDelegate != null) {
mDelegate.onOpenButtonClicked();
return true;
}
return false;
}
});
// Setup the gear icon to handle opening the change provider scenario.
mGearView = holder.findViewById(R.id.settings_button);
mGearView.setVisibility(View.VISIBLE);
mGearView.setOnClickListener(
new View.OnClickListener() {
public void onClick(@NonNull View v) {
if (mDelegate != null) {
mDelegate.onChangeButtonClicked();
}
}
});
}
private void onBindViewHolderNewSettingsUi(PreferenceViewHolder holder) {
mOpenButton = (Button) holder.findViewById(R.id.open_button);
mOpenButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(@NonNull View v) {
if (mDelegate != null) {
mDelegate.onOpenButtonClicked();
}
}
});
setVisibility(mOpenButton, mOpenButtonVisible);
mChangeButton = (Button) holder.findViewById(R.id.change_button);
mChangeButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(@NonNull View v) {
if (mDelegate != null) {
mDelegate.onChangeButtonClicked();
}
}
});
mButtonFrameView = holder.findViewById(R.id.credman_button_frame);
mButtonFrameView.setVisibility(mButtonsVisible ? View.VISIBLE : View.GONE);
// There is a special case where if the provider == none then we should
// hide the buttons and when the preference is tapped we can open the
// provider selection dialog.
setOnPreferenceClickListener(
new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(@NonNull Preference preference) {
return handlePreferenceClickNewSettingsUi();
}
});
}
private boolean handlePreferenceClickNewSettingsUi() {
if (mDelegate != null && !mButtonsVisible) {
mDelegate.onChangeButtonClicked();
return true;
}
return false;
}
public void setOpenButtonVisible(boolean isVisible) {
if (mOpenButton != null) {
mOpenButton.setVisibility(isVisible ? View.VISIBLE : View.GONE);
setVisibility(mOpenButton, isVisible);
}
mOpenButtonVisible = isVisible;
}
public void setButtonsVisible(boolean isVisible) {
if (mButtonFrameView != null) {
setVisibility(mButtonFrameView, isVisible);
}
mButtonsVisible = isVisible;
}
public void setDelegate(@NonNull Delegate delegate) {
mDelegate = delegate;
}
@Override
protected boolean shouldHideSecondTarget() {
return shouldUseNewSettingsUi();
}
@VisibleForTesting
public @Nullable Button getOpenButton() {
return mOpenButton;
}
@VisibleForTesting
public @Nullable Button getChangeButton() {
return mChangeButton;
}
@VisibleForTesting
public @Nullable View getButtonFrameView() {
return mButtonFrameView;
}
@VisibleForTesting
public @Nullable View getGearView() {
return mGearView;
}
private static void setVisibility(View view, boolean isVisible) {
view.setVisibility(isVisible ? View.VISIBLE : View.GONE);
}
}