/*
 * 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.button;

import android.os.Build;

@SuppressWarnings("WeakerAccess")
public class BoardDefaults {
    private static final String DEVICE_EDISON = "edison";
    private static final String DEVICE_RPI3 = "rpi3";
    private static final String DEVICE_NXP = "imx6ul";

    /**
     * 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 (Build.DEVICE) {
            case DEVICE_EDISON:
                return "IO13";
            case DEVICE_RPI3:
                return "BCM6";
            case DEVICE_NXP:
                return "GPIO4_IO21";
            default:
                throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
        }
    }

    /**
     * Return the GPIO pin that the Button is connected on.
     */
    public static String getGPIOForButton() {
        switch (Build.DEVICE) {
            case DEVICE_EDISON:
                return "IO12";
            case DEVICE_RPI3:
                return "BCM21";
            case DEVICE_NXP:
                return "GPIO4_IO20";
            default:
                throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
        }
    }
}

@@ -684,70 +684,232 @@
 d;%0A%0A
-@SuppressWarnings(%22WeakerAccess%22)%0Apublic class BoardDefaults %7B
+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   
@@ -1071,16 +1071,62 @@
 imx6ul%22;
+%0A    private static String sBoardVariant = %22%22;
 %0A%0A    /*
@@ -1409,36 +1409,41 @@
     switch (
-Build.DEVICE
+getBoardVariant()
 ) %7B%0A        
@@ -1456,32 +1456,40 @@
 se DEVICE_EDISON
+_ARDUINO
 :%0A              
@@ -1501,24 +1501,87 @@
 urn %22IO13%22;%0A
+            case DEVICE_EDISON:%0A                return %22GP45%22;%0A
             
@@ -1960,28 +1960,33 @@
 switch (
-Build.DEVICE
+getBoardVariant()
 ) %7B%0A    
@@ -2003,32 +2003,40 @@
 se DEVICE_EDISON
+_ARDUINO
 :%0A              
@@ -2052,16 +2052,79 @@
 %22IO12%22;%0A
+            case DEVICE_EDISON:%0A                return %22GP44%22;%0A
         
@@ -2368,11 +2368,759 @@
  %7D%0A    %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

add preferred pin for Edison SOM