/*
 * Copyright (c) 2012, Apptentive, Inc. All Rights Reserved.
 * Please refer to the LICENSE file for the terms and conditions
 * under which redistribution and use of this file is permitted.
 */

package com.apptentive.android.sdk.util;

import android.os.Build;

/**
 * This class is used to allow access of fields, methods, and classes that are not available in the targeted API level.
 *
 * @author Sky Kelsey.
 */
public class Reflection {

	/**
	 * <p>Returns the Build.BOOTLOADER version String. This field is introduced in API level 8</p>
	 * <p>This field appears to be implemented sporadically.
	 *   <ul>
	 *     <li>Galaxy S Captivate on 2.1: BOOTLOADER = null (Expected, as this has only API 7)</li>
	 *     <li>Nexus S on 2.3.6: BOOTLOADER = I9020XXKA3</li>
	 *     <li>Atrix on 2.2.2: BOOTLOADER = unknown</li>
	 *     <li>Galaxy Tab 10.1 on 3.1: BOOTLOADER = P7500XXKG8</li>
	 *   </ul>
	 * </p>
	 * @return String The Build.BOOTLOADER version String.
	 */
	public static String getBootloaderVersion(){
		try{
			return (String)Build.class.getField("BOOTLOADER").get(null);
		} catch(Exception e) {
			return null;
		}
	}

	/**
	 * <p>Returns the Build.BOOTLOADER version String. This field is introduced in API level 8, but the
	 * getRadioVersion() method was only implemented with API 14 (4.0), and this is the preferred method, since the
	 * RADIO field may not have been initialized when Build is loaded.</p>
	 * <p>This field appears to be implemented sporadically.
	 *   <ul>
	 *     <li>Galaxy S Captivate on 2.1: RADIO = null (Expected, as this has only API 7)</li>
	 *     <li>Nexus S on 2.3.6: RADIO = unknown</li>
	 *     <li>Atrix on 2.2.2: RADIO = unknown</li>
	 *     <li>Galaxy Tab 10.1 on 3.1: RADIO = unknown</li>
	 *     <li>Phones with 4.0 should see a valid radio version.</li>
	 *   </ul>
	 * </p>
	 * @return String The Build.BOOTLOADER version String.
	 */
	public static String getRadioVersion(){
		try{
			return Build.class.getMethod("getRadioVersion", String.class).invoke(null).toString();
		} catch(Exception e) {
			try{
				return (String)Build.class.getField("RADIO").get(null);
			} catch(Exception f) {
				return null;
			}
		}
	}
}

@@ -16,17 +16,17 @@
  (c) 201
-2
+3
 , Appten
@@ -258,16 +258,89 @@
 s.Build;
+%0Aimport com.apptentive.android.sdk.Log;%0A%0Aimport java.lang.reflect.Method;
 %0A%0A/**%0A *
@@ -1231,32 +1231,21 @@
 rns the 
-Build.BOOTLOADER
+radio
  version
@@ -1249,149 +1249,173 @@
 ion 
-S
+s
 tring. 
-This field is introduced in API level 8, but the%0A%09 * getRadioVersion() method was only implemented with API 14 (4.0), and t
+Build.getRadioVersion() is attempted first. It requires reflection, since it%0A%09 * was introduced in API 14. Failing that, Build.RADIO is accessed. T
 his is 
-the
+not
  pre
@@ -1424,50 +1424,34 @@
 rred
- method, since the%0A%09 * RADIO field
+, as this variable
  may not
  hav
@@ -1446,16 +1446,20 @@
  may not
+%0A%09 *
  have be
@@ -1477,29 +1477,113 @@
 zed 
-w
+before t
 he
-n
  Build 
-is loaded.
+object is initialized. It requires reflection as well, as it was introduced%0A%09 * in API 8
 %3C/p%3E
@@ -1917,62 +1917,49 @@
 %3Cli%3E
-Phones with 4.0 should see a valid radio version.%3C/li%3E
+Galaxy Nexus on 4.2.1: RADIO = I9250XXLJ1
 %0A%09 *
@@ -1992,50 +1992,63 @@
 urn 
-String T
+Eit
 he
+r
  Build.
-BOOTLOADER version String
+getRadioVersion(), or Build.BUILD, or null
 .%0A%09 
@@ -2101,22 +2101,31 @@
 try%7B%0A%09%09%09
-return
+Method method =
  Build.c
@@ -2160,81 +2160,262 @@
 ion%22
-, String.class).invoke(null).toString();%0A%09%09%7D catch(Exception e) %7B%0A%09%09%09try%7B
+);%0A%09%09%09Object invoked = method.invoke(null);%0A%09%09%09String ret = invoked.toString();%0A%09%09%09Log.v(%22Build.getRadioVersion() = %25s%22, ret);%0A%09%09%09return ret;%0A%09%09%7D catch(Exception e) %7B%0A%09%09%09try%7B%0A%09%09%09%09Log.v(%22Build.RADIO = %25s%22, (String)Build.class.getField(%22RADIO%22).get(null));
 %0A%09%09%09

Fix optional survey validation, and send button validation.