AI2 Component  (Version nb184)
MediaStore.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
5 
6 package com.google.appinventor.components.runtime;
7 
8 import android.os.Handler;
9 
23 
24 import org.apache.http.HttpEntity;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.client.HttpClient;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.entity.mime.HttpMultipartMode;
29 import org.apache.http.entity.mime.MultipartEntityBuilder;
30 import org.apache.http.entity.mime.content.FileBody;
31 import org.apache.http.impl.client.DefaultHttpClient;
32 import org.apache.http.util.EntityUtils;
33 
34 import java.io.BufferedReader;
35 import java.io.File;
36 import java.io.FileNotFoundException;
37 import java.io.InputStreamReader;
38 
39 import java.net.HttpURLConnection;
40 import java.net.URL;
41 
42 // When the component is installed in App Inventor, the Javadoc
43 // comments will become included in the automatically-generated system
44 // documentation, except for lines starting with tags (such as @author).
45 
56 // The annotations here provide information to the compiler about
57 // integrating the component into App Inventor system. The following
58 // three annotations stipulate that MediaStore will appear in the
59 // designer, that it will be an object in the App Inventor language,
60 // and say what Android system permissions it requires.
61 //
62 
63 @DesignerComponent(version = YaVersion.MEDIASTORE_COMPONENT_VERSION,
64  description = "Non-visible component that communicates with a Web service and stores media " +
65  "files.",
66  category = ComponentCategory.INTERNAL,
67  nonVisible = true,
68  iconName = "images/mediastore.png")
69 @SimpleObject
70 @UsesPermissions(permissionNames = "android.permission.INTERNET")
71 @UsesLibraries("httpmime.jar")
72 public final class MediaStore extends AndroidNonvisibleComponent implements Component {
74  private static final String LOG_TAG_COMPONENT = "MediaStore: ";
75  private String serviceURL;
76  private Handler androidUIHandler;
77 
83  public MediaStore(ComponentContainer container) {
84  super(container.$form());
85  componentContainer = container;
86  androidUIHandler = new Handler();
87  serviceURL = "http://ai-mediaservice.appspot.com";
88  }
89 
94  public String ServiceURL() {
95  return serviceURL;
96  }
97 
103  defaultValue = "http://ai-mediaservice.appspot.com")
105  public void ServiceURL(String url) {
106  serviceURL = url;
107  }
108 
115  public void PostMedia(String mediafile) throws FileNotFoundException{
117  public void onSuccess(final String response) {
118  androidUIHandler.post(new Runnable() {
119  public void run() {
120  MediaStored(response);
121  }
122  });
123  }
124  public void onFailure(final String message) {
125  androidUIHandler.post(new Runnable() {
126  public void run() {
127  WebServiceError(message);
128  }
129  });
130  }
131  };
132 
133  try {
134  HttpClient client = new DefaultHttpClient();
135 
136  MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
137  entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
138 
139  String[] pathtokens = mediafile.split("/");
140  String newMediaPath;
141 
142  if (pathtokens[0].equals("file:")) {
143  newMediaPath = new java.io.File(new URL(mediafile).toURI()).getAbsolutePath();
144  } else {
145  newMediaPath = mediafile;
146  }
147 
148  File media = new File(newMediaPath);
149  entityBuilder.addPart("file", new FileBody(media));
150 
151  HttpEntity entity = entityBuilder.build();
152 
153  String uploadURL = getUploadUrl();
154  HttpPost post = new HttpPost(uploadURL);
155  post.setEntity(entity);
156  HttpResponse response = client.execute(post);
157 
158  HttpEntity httpEntity = response.getEntity();
159  String result = EntityUtils.toString(httpEntity);
160  myCallback.onSuccess(result);
161  } catch (Exception e) {
162  e.printStackTrace();
163  myCallback.onFailure(e.getMessage());
164  }
165  }
166 
167  private String getUploadUrl() {
168  try {
169  String url = serviceURL;
170  URL obj = new URL(url);
171 
172  HttpURLConnection con = (HttpURLConnection) obj.openConnection();
173 
174  // optional default is GET
175  con.setRequestMethod("GET");
176 
177  // add request header
178  String USER_AGENT = "AppInventor";
179  con.setRequestProperty("User-Agent", USER_AGENT);
180  con.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
181 
182  // get and build upload URL from response
183  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
184  StringBuilder response = new StringBuilder();
185  String inputLine;
186  while ((inputLine = in.readLine()) != null) {
187  response.append(inputLine);
188  }
189  in.close();
190 
191  // return upload URL
192  return response.toString();
193  } catch (Exception e) {
194  e.printStackTrace();
195  }
196  return "";
197  }
198 
204  @SimpleEvent
205  public void MediaStored(String url) {
206  EventDispatcher.dispatchEvent(this, "MediaStored", url);
207  }
208 
214  @SimpleEvent
215  public void WebServiceError(String message) {
216  EventDispatcher.dispatchEvent(this, "WebServiceError", message);
217  }
218 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.MediaStore.MediaStore
MediaStore(ComponentContainer container)
Definition: MediaStore.java:83
com.google.appinventor.components.runtime.util.AsyncCallbackPair.onFailure
void onFailure(String message)
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.annotations.UsesLibraries
Definition: UsesLibraries.java:21
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_STRING
static final String PROPERTY_TYPE_STRING
Definition: PropertyTypeConstants.java:237
com.google.appinventor.components
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.MediaStore.componentContainer
final ComponentContainer componentContainer
Definition: MediaStore.java:73
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.File
Definition: File.java:53
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.MediaStore
Definition: MediaStore.java:72
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.util.AsyncCallbackPair.onSuccess
void onSuccess(T result)
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.MediaStore.PostMedia
void PostMedia(String mediafile)
Definition: MediaStore.java:115
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.MediaStore.MediaStored
void MediaStored(String url)
Definition: MediaStore.java:205
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.MediaStore.ServiceURL
void ServiceURL(String url)
Definition: MediaStore.java:105
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.MediaStore.WebServiceError
void WebServiceError(String message)
Definition: MediaStore.java:215
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.util.AsyncCallbackPair
Definition: AsyncCallbackPair.java:17
com.google.appinventor