All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.android.builder.testing.api.DeviceConfig Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
/*
 * Copyright (C) 2015 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.builder.testing.api;

import com.android.annotations.NonNull;
import com.android.annotations.Nullable;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

/**
 * Encapsulation of the device configuration obtained from "shell am get-config" command.
 */
public interface DeviceConfig {

    public enum Catetory {
        CONFIG, ABI
    }

    @NonNull
    public Optional getValue(Catetory catetory);

    @NonNull
    public List getAbis();

    @NonNull
    public String getConfigForAllAbis();

    @NonNull
    public String getConfigFor(@Nullable String abi);

    public static class Builder {

        private static class Values {

            @NonNull
            private final Catetory mCatetory;

            @NonNull
            private final String value;

            private Values(@NonNull Catetory catetory, @NonNull String value) {
                mCatetory = catetory;
                this.value = value;
            }
        }

        public static DeviceConfig parse(Collection lines) {
            ImmutableList.Builder valuesBuilder = ImmutableList.builder();
            for (String line : lines) {
                for (Catetory catetory : Catetory.values()) {
                    String key = catetory.name().toLowerCase(Locale.US) + ": ";
                    if (line.startsWith(key)) {
                        valuesBuilder.add(new Values(catetory, line.substring(key.length())));
                    }
                }
            }
            final ImmutableList values = valuesBuilder.build();
            return new DeviceConfig() {

                @Override
                @NonNull
                public Optional getValue(Catetory catetory) {
                    for (Values value : values) {
                        if (value.mCatetory.equals(catetory)) {
                            return Optional.of(value.value);
                        }
                    }
                    return Optional.absent();
                }

                @Override
                @NonNull
                public List getAbis() {
                    ImmutableList.Builder abiBuilder = ImmutableList.builder();
                    Optional abis = getValue(Catetory.ABI);
                    if (abis.isPresent()) {
                        StringTokenizer stringTokenizer = new StringTokenizer(abis.get(), ",");
                        while (stringTokenizer.hasMoreElements()) {
                            abiBuilder.add(stringTokenizer.nextToken());
                        }
                    }
                    return abiBuilder.build();
                }

                @Override
                @NonNull
                public String getConfigForAllAbis() {
                    StringBuilder completeConfig = new StringBuilder();
                    Optional config = getValue(Catetory.CONFIG);
                    Optional abi = getValue(Catetory.ABI);
                    if (config.isPresent()) {
                        completeConfig.append(config.get());
                        if (abi.isPresent()) {
                            completeConfig.append(":");
                        }
                    }
                    if (abi.isPresent()) {
                        completeConfig.append(abi.get());
                    }
                    return completeConfig.toString();
                }

                @Override
                @NonNull
                public String getConfigFor(@Nullable String abi) {
                    StringBuilder completeConfig = new StringBuilder();
                    Optional config = getValue(Catetory.CONFIG);
                    if (config.isPresent()) {
                        completeConfig.append(config.get());
                        if (!Strings.isNullOrEmpty(abi)) {
                            completeConfig.append(":");
                        }
                    }
                    if (!Strings.isNullOrEmpty(abi)) {
                        completeConfig.append(abi);
                    }
                    return completeConfig.toString();
                }
            };
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy