Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a5571da1 authored by Ahaan Ugale's avatar Ahaan Ugale
Browse files

AF: Show passwords settings for available autofill service.

Separates autofill preferences into 2 categories - one for the passwords
settings and one for the existing default service preference.

The passwords preference controller dynamically creates preferences for
each autofill service.

Bug: 169455298
Test: manual
Test: TODO new test for controller
Change-Id: I369b5db00be75957c869df4d960595e9e1d9772c
parent c0894378
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -11481,8 +11481,10 @@
    <!-- AutoFill strings -->
    <!-- Preference label for choosing auto-fill service. [CHAR LIMIT=60] -->
    <string name="autofill_app">Autofill service</string>
    <!-- Preference category for showing auto-fill services with saved passwords. [CHAR LIMIT=60] -->
    <string name="autofill_passwords">Passwords</string>
    <!-- Keywords for the auto-fill feature. [CHAR LIMIT=NONE] -->
    <string name="autofill_keywords">auto, fill, autofill</string>
    <string name="autofill_keywords">auto, fill, autofill, password</string>
    <!-- Message of the warning dialog for setting the auto-fill app. [CHAR_LIMIT=NONE] -->
    <string name="autofill_confirmation_message">
+21 −8
Original line number Diff line number Diff line
@@ -22,6 +22,18 @@
    android:title="@string/autofill_app"
    settings:keywords="@string/autofill_keywords">

  <PreferenceCategory
      android:key="passwords_category"
      android:persistent="false"
      android:title="@string/autofill_passwords"
      settings:controller="com.android.settings.applications.autofill.PasswordsPreferenceController" >
  </PreferenceCategory>

  <PreferenceCategory
      android:key="default_service_category"
      android:title="@string/autofill_app">

    <!-- TODO(b/169455298): Fix the redundant title. -->
    <com.android.settings.widget.GearPreference
        android:key="default_autofill_main"
        android:title="@string/autofill_app"
@@ -30,6 +42,7 @@
        settings:keywords="@string/autofill_keywords">
      <extra android:name="for_work" android:value="false" />
    </com.android.settings.widget.GearPreference>
  </PreferenceCategory>


  <com.android.settings.widget.WorkOnlyCategory
+94 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.autofill;

import android.annotation.UserIdInt;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.graphics.drawable.Drawable;
import android.os.UserHandle;
import android.service.autofill.AutofillServiceInfo;
import android.text.TextUtils;
import android.util.IconDrawableFactory;

import androidx.preference.Preference;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceScreen;

import com.android.settings.Utils;
import com.android.settings.core.BasePreferenceController;

import java.util.List;

/**
 * Queries available autofill services and adds preferences for those that declare passwords
 * settings.
 */
public class PasswordsPreferenceController extends BasePreferenceController {

    private final PackageManager mPm;
    private final IconDrawableFactory mIconFactory;
    private final List<AutofillServiceInfo> mServices;

    public PasswordsPreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
        mPm = context.getPackageManager();
        mIconFactory = IconDrawableFactory.newInstance(mContext);
        mServices = AutofillServiceInfo.getAvailableServices(mContext, UserHandle.myUserId());
        for (int i = mServices.size() - 1; i >= 0; i--) {
            final String passwordsActivity = mServices.get(i).getPasswordsActivity();
            if (TextUtils.isEmpty(passwordsActivity)) {
                mServices.remove(i);
            }
        }
    }

    @Override
    public int getAvailabilityStatus() {
        return mServices.isEmpty() ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
    }

    @Override
    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);
    }

    private void addPasswordPreferences(
            Context prefContext, @UserIdInt int user, PreferenceGroup group) {
        for (int i = 0; i < mServices.size(); i++) {
            final AutofillServiceInfo service = mServices.get(i);
            final Preference pref = new Preference(prefContext);
            final ServiceInfo serviceInfo = service.getServiceInfo();
            pref.setTitle(serviceInfo.loadLabel(mPm));
            final Drawable icon =
                    mIconFactory.getBadgedIcon(
                            serviceInfo,
                            serviceInfo.applicationInfo,
                            user);
            Utils.setSafeIcon(pref, icon);
            pref.setIntent(
                    new Intent(Intent.ACTION_MAIN)
                            .setClassName(serviceInfo.packageName, service.getPasswordsActivity()));
            group.addPreference(pref);
        }
    }
}