// WebRadio extension for App Inventor by Marco Perrone package WebRadio; import com.google.appinventor.components.runtime.*; import android.app.Activity; import android.content.Intent; import com.google.appinventor.components.annotations.DesignerComponent; import com.google.appinventor.components.annotations.DesignerProperty; import com.google.appinventor.components.annotations.PropertyCategory; import com.google.appinventor.components.annotations.SimpleFunction; import com.google.appinventor.components.annotations.SimpleEvent; import com.google.appinventor.components.annotations.SimpleObject; import com.google.appinventor.components.annotations.SimpleProperty; import com.google.appinventor.components.annotations.UsesPermissions; import com.google.appinventor.components.annotations.UsesActivities; import com.google.appinventor.components.annotations.UsesLibraries; import com.google.appinventor.components.annotations.UsesActivityMetadata; import com.google.appinventor.components.annotations.androidmanifest.*; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.common.PropertyTypeConstants; import com.google.appinventor.components.common.YaVersion; import com.google.appinventor.components.runtime.util.GingerbreadUtil; import com.google.appinventor.components.runtime.util.SdkLevel; import com.google.appinventor.components.runtime.util.AsynchUtil; import com.google.appinventor.components.runtime.util.YailList; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.net.URI; import java.net.URISyntaxException; import java.net.CookieHandler; import java.util.Scanner; import java.util.Iterator; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.io.FileWriter; import java.io.BufferedWriter; import java.io.StringWriter; import java.io.PrintWriter; import java.io.IOException; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.DataOutputStream; import java.io.BufferedReader; import java.io.OutputStream; @SimpleObject(external=true) @UsesPermissions(permissionNames = "android.permission.INTERNET") @DesignerComponent(version = YaVersion.WEB_COMPONENT_VERSION, description = "

Non-visible component to listen web radio from all over the world.", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "images/nearfield.png") public class WebRadio extends AndroidNonvisibleComponent implements Component { private final Activity activity; private final CookieHandler cookieHandler; private String twoHyphens = "--"; private String boundary = "*****"+Long.toString(System.currentTimeMillis())+"*****"; private String LINE = "\r\n"; private URL url; private HttpURLConnection httpConn = null; private OutputStream outputStream; private PrintWriter writer; private String charset = "utf-8"; @SimpleEvent(description = "Got Countries List") public void GotCountries(int responseCode, String response, YailList countries, String message) { EventDispatcher.dispatchEvent(this, "GotCountries", responseCode, response, countries, message); } @SimpleEvent(description = "Got Stations List") public void GotStations(int responseCode, String response, YailList name, YailList urlStream, YailList homepage, YailList icon, String message) { EventDispatcher.dispatchEvent(this, "GotStations", responseCode, response, name, urlStream, homepage, icon, message); } @SimpleFunction(description = "Get Countries List") public void GetCountries() { AsynchUtil.runAsynchronously(new Runnable() { @Override public void run() { GetCountries_Exec(); } }); } @SimpleFunction(description = "Search Stations") public void SearchStations(final String name, final String country, final String limit) { AsynchUtil.runAsynchronously(new Runnable() { @Override public void run() { SearchStations_Exec(name, country, limit); } }); } public WebRadio(ComponentContainer container) { super(container.$form()); activity = container.$context(); cookieHandler = (SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD) ? GingerbreadUtil.newCookieManager() : null; } void GetCountries_Exec() { try { InputStream inputStream = null; String URLStr = "http://all.api.radio-browser.info/json/countries"; URLStr = URLStr + "?order=name"; URL url = new URL(URLStr); httpConn = (HttpURLConnection) url.openConnection(); httpConn.setRequestMethod("GET"); final int status = httpConn.getResponseCode(); List countries = new ArrayList(); String Msg = ""; String distance = ""; if (status == HttpURLConnection.HTTP_OK) { inputStream = httpConn.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); String inputLine; final StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } final String responseStr = response.toString(); String ImportantValues = ""; String name = ""; if (responseStr.indexOf("name") != -1) { ImportantValues = responseStr.substring(responseStr.indexOf("name")+7); while (ImportantValues.indexOf("name") != -1) { name = ImportantValues.substring(0,ImportantValues.indexOf("\"")); countries.add(name); ImportantValues = ImportantValues.substring(ImportantValues.indexOf("name")+7); } } else { Msg = "No results found"; } httpConn.disconnect(); final List countriesFin = countries; final String Message = Msg; // Dispatch the event. activity.runOnUiThread(new Runnable() { @Override public void run() { GotCountries(status, responseStr , YailList.makeList(countriesFin), Message); } }); } else { InputStream inputStreamErr = httpConn.getErrorStream(); BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream())); String inputLine; final StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } final String responseStr = response.toString(); httpConn.disconnect(); // Dispatch the event. activity.runOnUiThread(new Runnable() { @Override public void run() { GotCountries(status, responseStr , null, ""); } }); } } catch (Exception e) { e.printStackTrace(); StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); appendLog(errors.toString()); } } void SearchStations_Exec(String NAME, String COUNTRY, String LIMIT) { try { InputStream inputStream = null; String URLStr = "http://all.api.radio-browser.info/json/stations/search"; URLStr = URLStr + "?name=" + NAME; URLStr = URLStr + "&country=" + COUNTRY; if (LIMIT.length() == 0) LIMIT = "100"; URLStr = URLStr + "&limit=" + LIMIT; URLStr = URLStr + "&order=name"; URL url = new URL(URLStr); httpConn = (HttpURLConnection) url.openConnection(); httpConn.setRequestMethod("GET"); final int status = httpConn.getResponseCode(); String Msg = ""; List name = new ArrayList(); List urlStream = new ArrayList(); List homepage = new ArrayList(); List icon = new ArrayList(); if (status == HttpURLConnection.HTTP_OK) { inputStream = httpConn.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream())); String inputLine; final StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } final String responseStr = response.toString(); String ImportantValues = response.toString(); if (responseStr.indexOf("name") != -1) { String nameStr = ""; String urlStreamStr = ""; String homepageStr = ""; String iconStr = ""; while (ImportantValues.indexOf("name\"") != -1) { nameStr = ImportantValues.substring(ImportantValues.indexOf("name\"")+7); nameStr = nameStr.substring(0,nameStr.indexOf("\"")); name.add(nameStr); urlStreamStr = ImportantValues.substring(ImportantValues.indexOf("url_resolved\"")+15); urlStreamStr = urlStreamStr.substring(0,urlStreamStr.indexOf("\"")); urlStream.add(urlStreamStr); homepageStr = ImportantValues.substring(ImportantValues.indexOf("homepage\"")+11); homepageStr = homepageStr.substring(0,homepageStr.indexOf("\"")); homepage.add(homepageStr); iconStr = ImportantValues.substring(ImportantValues.indexOf("favicon\"")+10); iconStr = iconStr.substring(0,iconStr.indexOf("\"")); icon.add(iconStr); ImportantValues = ImportantValues.substring(ImportantValues.indexOf("favicon\"")+10); } } else { Msg = "No results found"; } httpConn.disconnect(); final List nameFin = name; final List urlStreamFin = urlStream; final List homepageFin = homepage; final List iconFin = icon; final String Message = Msg; // Dispatch the event. activity.runOnUiThread(new Runnable() { @Override public void run() { GotStations(status, responseStr, YailList.makeList(nameFin), YailList.makeList(urlStreamFin), YailList.makeList(homepageFin), YailList.makeList(iconFin), Message); } }); } else { InputStream inputStreamErr = httpConn.getErrorStream(); BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream())); String inputLine; final StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } final String responseStr = response.toString(); httpConn.disconnect(); // Dispatch the event. activity.runOnUiThread(new Runnable() { @Override public void run() { GotStations(status, responseStr , null, null, null, null, ""); } }); } } catch (Exception e) { e.printStackTrace(); StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); appendLog(errors.toString()); } } public void appendLog(String text) { File logFile = new File("sdcard/WebRadioLog.txt"); if (!logFile.exists()) { try { logFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { //BufferedWriter for performance, true to set append to file flag BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); buf.append(text); buf.newLine(); buf.flush(); buf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }