/*
 * Copyright 2016, 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.example.androidthings.assistant;

import android.os.Build;

import com.google.android.things.pio.PeripheralManagerService;

import java.util.List;

@SuppressWarnings("WeakerAccess")
public class BoardDefaults {
    private static final String DEVICE_EDISON_ARDUINO = "edison_arduino";
    private static final String DEVICE_EDISON = "edison";
    private static final String DEVICE_JOULE = "joule";
    private static final String DEVICE_RPI3 = "rpi3";
    private static final String DEVICE_IMX6UL_PICO = "imx6ul_pico";
    private static final String DEVICE_IMX6UL_VVDN = "imx6ul_iopb";
    private static final String DEVICE_IMX7D_PICO = "imx7d_pico";
    private static String sBoardVariant = "";

    /**
     * Return the GPIO pin that the LED is connected on.
     * For example, on Intel Edison Arduino breakout, pin "IO13" is connected to an onboard LED
     * that turns on when the GPIO pin is HIGH, and off when low.
     */
    public static String getGPIOForLED() {
        switch (getBoardVariant()) {
            case DEVICE_EDISON_ARDUINO:
                return "IO13";
            case DEVICE_EDISON:
                return "GP45";
            case DEVICE_JOULE:
                return "J6_25";
            case DEVICE_RPI3:
                return "BCM25";
            case DEVICE_IMX6UL_PICO:
                return "GPIO4_IO22";
            case DEVICE_IMX6UL_VVDN:
                return "GPIO3_IO06";
            case DEVICE_IMX7D_PICO:
                return "GPIO2_IO02";
            default:
                throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
        }
    }

    /**
     * Return the GPIO pin that the Button is connected on.
     */
    public static String getGPIOForButton() {
        switch (getBoardVariant()) {
            case DEVICE_EDISON_ARDUINO:
                return "IO12";
            case DEVICE_EDISON:
                return "GP44";
            case DEVICE_JOULE:
                return "J7_71";
            case DEVICE_RPI3:
                return "BCM23";
            case DEVICE_IMX6UL_PICO:
                return "GPIO2_IO03";
            case DEVICE_IMX6UL_VVDN:
                return "GPIO3_IO01";
            case DEVICE_IMX7D_PICO:
                return "GPIO6_IO14";
            default:
                throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
        }
    }

    /**
     * Return the I2S Bus that the Voice Hat is connected to.
     */
    public static String getI2SDeviceForVoiceHat() {
        switch (getBoardVariant()) {
            case DEVICE_RPI3:
                return "I2S1";
            default:
                throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
        }
    }

    /**
     * Return the GPIO pin for the Voice Hat DAC trigger.
     */
    public static String getGPIOForVoiceHatTrigger() {
        switch (getBoardVariant()) {
            case DEVICE_RPI3:
                return "BCM16";
            default:
                throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
        }
    }

    private static String getBoardVariant() {
        if (!sBoardVariant.isEmpty()) {
            return sBoardVariant;
        }
        sBoardVariant = Build.DEVICE;
        // For the edison check the pin prefix
        // to always return Edison Breakout pin name when applicable.
        if (sBoardVariant.equals(DEVICE_EDISON)) {
            PeripheralManagerService pioService = new PeripheralManagerService();
            List<String> gpioList = pioService.getGpioList();
            if (gpioList.size() != 0) {
                String pin = gpioList.get(0);
                if (pin.startsWith("IO")) {
                    sBoardVariant = DEVICE_EDISON_ARDUINO;
                }
            }
        }
        return sBoardVariant;
    }
}

@@ -687,346 +687,70 @@
 d;%0A%0A
-import com.google.android.things.pio.PeripheralManagerService;%0A%0Aimport java.util.List;%0A%0A@SuppressWarnings(%22WeakerAccess%22)%0Apublic class BoardDefaults %7B%0A    private static final String DEVICE_EDISON_ARDUINO = %22edison_arduino%22;%0A    private static final String DEVICE_EDISON = %22edison%22;%0A    private static final String DEVICE_JOULE = %22joule%22;
+@SuppressWarnings(%22WeakerAccess%22)%0Apublic class BoardDefaults %7B
 %0A   
@@ -872,76 +872,8 @@
 o%22;%0A
-    private static final String DEVICE_IMX6UL_VVDN = %22imx6ul_iopb%22;%0A
     
@@ -937,54 +937,8 @@
 co%22;
-%0A    private static String sBoardVariant = %22%22;
 %0A%0A  
@@ -1233,225 +1233,23 @@
 ch (
-getBoardVariant()) %7B%0A            case DEVICE_EDISON_ARDUINO:%0A                return %22IO13%22;%0A            case DEVICE_EDISON:%0A                return %22GP45%22;%0A            case DEVICE_JOULE:%0A                return %22J6_25%22;
+Build.DEVICE) %7B
 %0A   
@@ -1385,82 +1385,8 @@
 2%22;%0A
-            case DEVICE_IMX6UL_VVDN:%0A                return %22GPIO3_IO06%22;%0A
     
@@ -1723,225 +1723,23 @@
 ch (
-getBoardVariant()) %7B%0A            case DEVICE_EDISON_ARDUINO:%0A                return %22IO12%22;%0A            case DEVICE_EDISON:%0A                return %22GP44%22;%0A            case DEVICE_JOULE:%0A                return %22J7_71%22;
+Build.DEVICE) %7B
 %0A   
@@ -1875,82 +1875,8 @@
 3%22;%0A
-            case DEVICE_IMX6UL_VVDN:%0A                return %22GPIO3_IO01%22;%0A
     
@@ -2218,33 +2218,28 @@
 switch (
-getBoardVariant()
+Build.DEVICE
 ) %7B%0A    
@@ -2567,33 +2567,28 @@
 switch (
-getBoardVariant()
+Build.DEVICE
 ) %7B%0A    
@@ -2774,755 +2774,7 @@
    %7D
-%0A%0A    private static String getBoardVariant() %7B%0A        if (!sBoardVariant.isEmpty()) %7B%0A            return sBoardVariant;%0A        %7D%0A        sBoardVariant = Build.DEVICE;%0A        // For the edison check the pin prefix%0A        // to always return Edison Breakout pin name when applicable.%0A        if (sBoardVariant.equals(DEVICE_EDISON)) %7B%0A            PeripheralManagerService pioService = new PeripheralManagerService();%0A            List%3CString%3E gpioList = pioService.getGpioList();%0A            if (gpioList.size() != 0) %7B%0A                String pin = gpioList.get(0);%0A                if (pin.startsWith(%22IO%22)) %7B%0A                    sBoardVariant = DEVICE_EDISON_ARDUINO;%0A                %7D%0A            %7D%0A        %7D%0A        return sBoardVariant;%0A    %7D
 %0A%7D%0A

Remove Deprecated Signals.