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

Commit 32d3d383 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Don't show autofill settings if not enabled" into oc-dev

parents 6e34174f d984e90b
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import android.view.autofill.AutofillManager;

/**
 * This interface replicates a subset of the android.view.autofill.AutofillManager (AFM). The
 * interface exists so that we can use a thin wrapper around the AFM in production code and a mock
 * in tests. We cannot directly mock or shadow the AFM, because some of the methods we rely on are
 * newer than the API version supported by Robolectric.
 */
public interface AutofillManagerWrapper {
    /**
     * Calls {@code AutofillManager.hasAutofillFeature()}.
     *
     * @see AutofillManager#hasAutofillFeature
     */
    public boolean hasAutofillFeature();

    /**
     * Calls {@code AutofillManager.isAutofillSupported()}.
     *
     * @see AutofillManager#isAutofillSupported
     */
    public boolean isAutofillSupported();
}
+45 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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;

import android.view.autofill.AutofillManager;

public class AutofillManagerWrapperImpl implements AutofillManagerWrapper {
    private final AutofillManager mAfm;

    public AutofillManagerWrapperImpl(AutofillManager afm) {
        mAfm = afm;
    }

    @Override
    public boolean hasAutofillFeature() {
        if (mAfm == null) {
            return false;
        }

        return mAfm.hasAutofillFeature();
    }

    @Override
    public boolean isAutofillSupported() {
        if (mAfm == null) {
            return false;
        }

        return mAfm.isAutofillSupported();
    }
}
+10 −1
Original line number Diff line number Diff line
@@ -21,16 +21,25 @@ import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.autofill.AutofillManager;

import com.android.settings.applications.AutofillManagerWrapper;
import com.android.settings.applications.AutofillManagerWrapperImpl;

public class DefaultAutofillPreferenceController extends DefaultAppPreferenceController {
    private AutofillManagerWrapper mAutofillManager;

    public DefaultAutofillPreferenceController(Context context) {
        super(context);

        mAutofillManager = new AutofillManagerWrapperImpl(
                mContext.getSystemService(AutofillManager.class));
    }

    @Override
    public boolean isAvailable() {
        return true;
        return mAutofillManager.hasAutofillFeature()
                && mAutofillManager.isAutofillSupported();
    }

    @Override
+23 −7
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import android.os.UserHandle;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.speech.tts.TtsEngines;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodInfo;
@@ -81,7 +83,16 @@ public class LanguageAndInputSettings extends DashboardFragment {

    @Override
    protected List<PreferenceController> getPreferenceControllers(Context context) {
        final Lifecycle lifecycle = getLifecycle();
        if (mAmbientDisplayConfig == null) {
            mAmbientDisplayConfig = new AmbientDisplayConfiguration(context);
        }

        return buildPreferenceControllers(context, getLifecycle(), mAmbientDisplayConfig);
    }

    private static List<PreferenceController> buildPreferenceControllers(@NonNull Context context,
            @Nullable Lifecycle lifecycle,
            @NonNull AmbientDisplayConfiguration ambientDisplayConfiguration) {
        final List<PreferenceController> controllers = new ArrayList<>();
        // Language
        controllers.add(new PhoneLanguagePreferenceController(context));
@@ -93,11 +104,10 @@ public class LanguageAndInputSettings extends DashboardFragment {
        controllers.add(new PhysicalKeyboardPreferenceController(context, lifecycle));
        final GameControllerPreferenceController gameControllerPreferenceController
                = new GameControllerPreferenceController(context);
        if (lifecycle != null) {
            lifecycle.addObserver(gameControllerPreferenceController);

        if (mAmbientDisplayConfig == null) {
            mAmbientDisplayConfig = new AmbientDisplayConfiguration(context);
        }

        controllers.add(gameControllerPreferenceController);
        // Gestures
        controllers.add(new AssistGesturePreferenceController(context, lifecycle));
@@ -105,9 +115,9 @@ public class LanguageAndInputSettings extends DashboardFragment {
        controllers.add(new DoubleTwistPreferenceController(context, lifecycle));
        controllers.add(new DoubleTapPowerPreferenceController(context, lifecycle));
        controllers.add(new PickupGesturePreferenceController(
                context, lifecycle, mAmbientDisplayConfig, UserHandle.myUserId()));
                context, lifecycle, ambientDisplayConfiguration, UserHandle.myUserId()));
        controllers.add(new DoubleTapScreenPreferenceController(
                context, lifecycle, mAmbientDisplayConfig, UserHandle.myUserId()));
                context, lifecycle, ambientDisplayConfiguration, UserHandle.myUserId()));
        controllers.add(new DefaultAutofillPreferenceController(context));
        return controllers;
    }
@@ -163,5 +173,11 @@ public class LanguageAndInputSettings extends DashboardFragment {
                    sir.xmlResId = R.xml.language_and_input;
                    return Arrays.asList(sir);
                }

                @Override
                public List<PreferenceController> getPreferenceControllers(Context context) {
                    return buildPreferenceControllers(context, null,
                            new AmbientDisplayConfiguration(context));
                }
            };
}
+18 −1
Original line number Diff line number Diff line
@@ -22,10 +22,12 @@ import android.content.pm.PackageManager;
import android.os.UserManager;
import android.provider.Settings;
import android.support.v7.preference.Preference;
import android.view.autofill.AutofillManager;

import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.AutofillManagerWrapper;
import com.android.settings.applications.PackageManagerWrapper;

import org.junit.Before;
@@ -54,6 +56,8 @@ public class DefaultAutofillPreferenceControllerTest {
    private UserManager mUserManager;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private PackageManagerWrapper mPackageManager;
    @Mock
    private AutofillManagerWrapper mAutofillManager;

    private DefaultAutofillPreferenceController mController;

@@ -64,10 +68,23 @@ public class DefaultAutofillPreferenceControllerTest {

        mController = spy(new DefaultAutofillPreferenceController(mContext));
        ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager);
        ReflectionHelpers.setField(mController, "mAutofillManager", mAutofillManager);
    }

    @Test
    public void isAlwaysAvailable() {
    public void isAvailableIfHasFeatureAndSupported() {
        when(mContext.getSystemService(AutofillManager.class)).thenReturn(null);
        assertThat(mController.isAvailable()).isFalse();

        when(mAutofillManager.hasAutofillFeature()).thenReturn(false);
        assertThat(mController.isAvailable()).isFalse();

        when(mAutofillManager.hasAutofillFeature()).thenReturn(true);
        when(mAutofillManager.isAutofillSupported()).thenReturn(false);
        assertThat(mController.isAvailable()).isFalse();

        when(mAutofillManager.hasAutofillFeature()).thenReturn(true);
        when(mAutofillManager.isAutofillSupported()).thenReturn(true);
        assertThat(mController.isAvailable()).isTrue();
    }

Loading