public final class

VersionUtils

extends java.lang.Object

 java.lang.Object

↳androidx.autofill.inline.VersionUtils

Gradle dependencies

compile group: 'androidx.autofill', name: 'autofill', version: '1.3.0-alpha01'

  • groupId: androidx.autofill
  • artifactId: autofill
  • version: 1.3.0-alpha01

Artifact androidx.autofill:autofill:1.3.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

Utility methods responsible for producing/consuming bundles representing supported versions, or the mapping from supported versions to corresponding UI styles.

Summary

Methods
public static java.util.List<java.lang.String>getSupportedVersions(Bundle bundleWithSupportedVersions)

bundleWithSupportedVersions is expected to be generated by either VersionUtils.writeSupportedVersions(Bundle) or VersionUtils.writeStylesToBundle(List, Bundle).

public static booleanisVersionSupported(java.lang.String version)

Returns true if the version is supported in the current library.

public static BundlereadStyleByVersion(Bundle versionedStylesBundle, java.lang.String version)

Reads the style of version from the versionedStylesBundle.

public static voidwriteStylesToBundle(java.util.List<UiVersions.Style> styles, Bundle bundle)

Writes the mapping from the UI version to corresponding style specification to the bundle in the following format: - VersionUtils.KEY_INLINE_UI_VERSIONS -> versions - UiVersions.INLINE_UI_VERSION_1 -> built from BundledStyle - ...

public static voidwriteSupportedVersions(Bundle bundle)

Writes the supported version to the bundle, in the following format: - VersionUtils.KEY_INLINE_UI_VERSIONS -> versions

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static boolean isVersionSupported(java.lang.String version)

Returns true if the version is supported in the current library.

public static java.util.List<java.lang.String> getSupportedVersions(Bundle bundleWithSupportedVersions)

bundleWithSupportedVersions is expected to be generated by either VersionUtils.writeSupportedVersions(Bundle) or VersionUtils.writeStylesToBundle(List, Bundle).

Parameters:

bundleWithSupportedVersions: bundle containing info about supported versions

Returns:

the list of versions that are both specified in the versionedBundle and supported by the current library.

public static void writeSupportedVersions(Bundle bundle)

Writes the supported version to the bundle, in the following format: - VersionUtils.KEY_INLINE_UI_VERSIONS -> versions

See also #getSupportedVersions(Bundle) for how it can be used.

public static void writeStylesToBundle(java.util.List<UiVersions.Style> styles, Bundle bundle)

Writes the mapping from the UI version to corresponding style specification to the bundle in the following format: - VersionUtils.KEY_INLINE_UI_VERSIONS -> versions - UiVersions.INLINE_UI_VERSION_1 -> built from BundledStyle - ... (to add if there is more version in the future)

public static Bundle readStyleByVersion(Bundle versionedStylesBundle, java.lang.String version)

Reads the style of version from the versionedStylesBundle.

Source

/*
 * Copyright 2020 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 androidx.autofill.inline;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.autofill.inline.common.BundledStyle;

import java.util.ArrayList;
import java.util.List;

/**
 * Utility methods responsible for producing/consuming bundles representing supported versions,
 * or the mapping from supported versions to corresponding UI styles.
 *
 */
@RestrictTo(RestrictTo.Scope.LIBRARY)
@RequiresApi(api = Build.VERSION_CODES.R)
public final class VersionUtils {
    /**
     * Bundle key pointing to a list representing UI versions.
     */
    private static final String KEY_INLINE_UI_VERSIONS = "androidx.autofill.inline.ui.version:key";

    /**
     * Returns true if the {@code version} is supported in the current library.
     */
    public static boolean isVersionSupported(@Nullable String version) {
        return UiVersions.getUiVersions().contains(version);
    }

    /**
     * {@code bundleWithSupportedVersions} is expected to be generated by either
     * {@link #writeSupportedVersions(Bundle)} or {@link #writeStylesToBundle(List, Bundle)}.
     *
     * @param bundleWithSupportedVersions bundle containing info about supported versions
     * @return the list of versions that are both specified in the {@code versionedBundle} and
     * supported by the current library.
     */
    @NonNull
    public static List<String> getSupportedVersions(
            @NonNull Bundle bundleWithSupportedVersions) {
        List<String> ret = new ArrayList<>();
        ArrayList<String> versions = bundleWithSupportedVersions.getStringArrayList(
                KEY_INLINE_UI_VERSIONS);
        if (versions != null) {
            for (String version : versions) {
                if (isVersionSupported(version)) {
                    ret.add(version);
                }
            }
        }
        return ret;
    }

    /**
     * Writes the supported version to the {@code bundle}, in the following format:
     *
     * - {@link #KEY_INLINE_UI_VERSIONS} -> {@link List<String>} versions
     *
     * <p>See also {@code #getSupportedVersions(Bundle)} for how it can be used.
     */
    public static void writeSupportedVersions(@NonNull Bundle bundle) {
        bundle.putStringArrayList(KEY_INLINE_UI_VERSIONS,
                new ArrayList<>(UiVersions.getUiVersions()));
    }

    /**
     * Writes the mapping from the UI version to corresponding style specification to the {@code
     * bundle} in the following format:
     *
     * - {@link #KEY_INLINE_UI_VERSIONS} -> {@link List<String>} versions
     * - {@link UiVersions#INLINE_UI_VERSION_1} -> {@link Bundle} built from {@link BundledStyle}
     * - ... (to add if there is more version in the future)
     */
    public static void writeStylesToBundle(@NonNull List<UiVersions.Style> styles,
            @NonNull Bundle bundle) {
        ArrayList<String> versions = new ArrayList<>();
        for (UiVersions.Style style : styles) {
            String version = style.getVersion();
            versions.add(style.getVersion());
            bundle.putBundle(version, style.getBundle());
        }
        bundle.putStringArrayList(KEY_INLINE_UI_VERSIONS, versions);
    }

    /**
     * Reads the style of {@code version} from the {@code versionedStylesBundle}.
     */
    @Nullable
    public static Bundle readStyleByVersion(@NonNull Bundle versionedStylesBundle,
            @NonNull @UiVersions.InlineUiVersion String version) {
        return versionedStylesBundle.getBundle(version);
    }

    private VersionUtils() {

    }
}