Use the synchronous version of getBitmapDrawable

This reverts most of commit b5e78d70.
We keep the changes to MediaUtil.getBitmapDrawable. So the actual image
fetching happens on a non-UI thread, but the UI Thread blocks until the
network I/O is done. This is non-ideal, as blocking on the UI thread can
result in an Application Not Responding (ANR) error. However this might
be more desirable then having the application apparently not load a
background image for a long time, and then having it suddenly appear
when the background network loading is finally finished.

Change-Id: I3424918a83284d19ee6ccfc9deb8824f5c0605a2
parent b5e78d70
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
......@@ -13,15 +13,12 @@ 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.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.util.AsyncCallbackPair;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.util.TextViewUtil;
import com.google.appinventor.components.runtime.util.ViewUtil;
import android.view.MotionEvent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
......@@ -33,6 +30,8 @@ import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import java.io.IOException;
/**
* Underlying base class for click-based components, not directly accessible to Simple programmers.
*
......@@ -315,41 +314,17 @@ public abstract class ButtonBase extends AndroidViewComponent
// Load image from file.
if (imagePath.length() > 0) {
MediaUtil.getBitmapDrawableAsync(container.$form(), imagePath, new AsyncCallbackPair<BitmapDrawable>() {
@Override
public void onFailure(String message) {
Log.w(LOG_TAG, "Error loading bitmap for button: " + message);
container.$form().dispatchErrorOccurredEvent(ButtonBase.this, "Image", ErrorMessages.ERROR_CANNOT_READ_FILE, imagePath);
container.$form().runOnUiThread(new Runnable() {
public void run() {
updateAppearance();
}
});
}
@Override
public void onException(Exception e) {
Log.w(LOG_TAG, "Error loading bitmap for button", e);
container.$form().dispatchErrorOccurredEvent(ButtonBase.this, "Image", ErrorMessages.ERROR_CANNOT_READ_FILE, imagePath);
container.$form().runOnUiThread(new Runnable() {
public void run() {
updateAppearance();
}
});
}
@Override
public void onSuccess(BitmapDrawable result) {
backgroundImageDrawable = result;
// Update the appearance based on the new value of backgroundImageDrawable.
container.$form().runOnUiThread(new Runnable() {
public void run() {
updateAppearance();
}
});
}
});
try {
backgroundImageDrawable = MediaUtil.getBitmapDrawable(container.$form(), imagePath);
} catch (IOException ioe) {
// TODO(user): Maybe raise Form.ErrorOccurred.
Log.e(LOG_TAG, "Unable to load " + imagePath);
// Fall through with a value of null for backgroundImageDrawable.
}
}
// Update the appearance based on the new value of backgroundImageDrawable.
updateAppearance();
}
/**
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
......@@ -21,7 +21,6 @@ import com.google.appinventor.components.common.ComponentConstants;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.runtime.collect.Sets;
import com.google.appinventor.components.runtime.util.AsyncCallbackPair;
import com.google.appinventor.components.runtime.util.BoundingBox;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.util.FileUtil;
......@@ -549,46 +548,16 @@ public final class Canvas extends AndroidViewComponent implements ComponentConta
scaledBackgroundBitmap = null;
if (!TextUtils.isEmpty(backgroundImagePath)) {
MediaUtil.getBitmapDrawableAsync(container.$form(), backgroundImagePath, new AsyncCallbackPair<BitmapDrawable>() {
@Override
public void onFailure(String message) {
Log.w(LOG_TAG, "Error loading background for canvas: " + message);
container.$form().dispatchErrorOccurredEvent(Canvas.this, "BackgroundImage", ErrorMessages.ERROR_CANNOT_READ_FILE, backgroundImagePath);
container.$form().runOnUiThread(new Runnable() {
public void run() {
setBackground();
clearDrawingLayer(); // will call invalidate()
}
});
}
try {
backgroundDrawable = MediaUtil.getBitmapDrawable(container.$form(), backgroundImagePath);
} catch (IOException ioe) {
Log.e(LOG_TAG, "Unable to load " + backgroundImagePath);
}
}
@Override
public void onException(Exception e) {
Log.w(LOG_TAG, "Error loading background for canvas", e);
container.$form().dispatchErrorOccurredEvent(Canvas.this, "BackgroundImage", ErrorMessages.ERROR_CANNOT_READ_FILE, backgroundImagePath);
container.$form().runOnUiThread(new Runnable() {
public void run() {
setBackground();
clearDrawingLayer(); // will call invalidate()
}
});
}
setBackground();
@Override
public void onSuccess(BitmapDrawable result) {
backgroundDrawable = result;
container.$form().runOnUiThread(new Runnable() {
public void run() {
setBackground();
clearDrawingLayer(); // will call invalidate()
}
});
}
});
} else {
setBackground();
clearDrawingLayer(); // will call invalidate()
}
clearDrawingLayer(); // will call invalidate()
}
private void setBackground() {
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.runtime;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
......@@ -25,7 +26,6 @@ import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
......@@ -61,9 +61,9 @@ import com.google.appinventor.components.runtime.collect.Lists;
import com.google.appinventor.components.runtime.collect.Maps;
import com.google.appinventor.components.runtime.collect.Sets;
import com.google.appinventor.components.runtime.multidex.MultiDex;
import com.google.appinventor.components.runtime.multidex.MultiDexApplication;
import com.google.appinventor.components.runtime.util.AlignmentUtil;
import com.google.appinventor.components.runtime.util.AnimationUtil;
import com.google.appinventor.components.runtime.util.AsyncCallbackPair;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.util.FullScreenVideoUtil;
import com.google.appinventor.components.runtime.util.JsonUtil;
......@@ -971,41 +971,13 @@ public class Form extends Activity
public void BackgroundImage(String path) {
backgroundImagePath = (path == null) ? "" : path;
MediaUtil.getBitmapDrawableAsync(this, backgroundImagePath, new AsyncCallbackPair<BitmapDrawable>() {
@Override
public void onFailure(String message) {
Log.w(LOG_TAG, "Error loading background for form: " + message);
Form.this.dispatchErrorOccurredEvent(Form.this, "BackgroundImage", ErrorMessages.ERROR_CANNOT_READ_FILE, backgroundImagePath);
backgroundDrawable = null;
androidUIHandler.post(new Runnable() {
public void run() {
setBackground(frameLayout);
}
});
}
@Override
public void onException(Exception e) {
Log.w(LOG_TAG, "Error loading background for form", e);
Form.this.dispatchErrorOccurredEvent(Form.this, "BackgroundImage", ErrorMessages.ERROR_CANNOT_READ_FILE, backgroundImagePath);
backgroundDrawable = null;
androidUIHandler.post(new Runnable() {
public void run() {
setBackground(frameLayout);
}
});
}
@Override
public void onSuccess(BitmapDrawable result) {
backgroundDrawable = result;
androidUIHandler.post(new Runnable() {
public void run() {
setBackground(frameLayout);
}
});
}
});
try {
backgroundDrawable = MediaUtil.getBitmapDrawable(this, backgroundImagePath);
} catch (IOException ioe) {
Log.e(LOG_TAG, "Unable to load " + backgroundImagePath);
backgroundDrawable = null;
}
setBackground(frameLayout);
}
/**
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
......@@ -600,15 +600,7 @@ public class GameClient extends AndroidNonvisibleComponent
@SimpleEvent(description = "Indicates that an error occurred " +
"while communicating with the web server.")
public void WebServiceError(final String functionName, final String message) {
WebServiceError(functionName, message, null);
}
public void WebServiceError(final String functionName, final String message, Exception e) {
if (e != null) {
Log.e(LOG_TAG, "WebServiceError: " + message, e);
} else {
Log.e(LOG_TAG, "WebServiceError: " + message);
}
Log.e(LOG_TAG, "WebServiceError: " + message);
androidUIHandler.post(new Runnable() {
public void run() {
EventDispatcher.dispatchEvent(GameClient.this, "WebServiceError", functionName, message);
......@@ -638,9 +630,6 @@ public class GameClient extends AndroidNonvisibleComponent
processInstanceLists(response);
FunctionCompleted("GetInstanceLists");
}
public void onException(final Exception e) {
WebServiceError("GetInstanceLists", "Failed to get up to date instance lists.", e);
}
public void onFailure(final String message) {
WebServiceError("GetInstanceLists", "Failed to get up to date instance lists.");
}
......@@ -746,9 +735,7 @@ public class GameClient extends AndroidNonvisibleComponent
}
FunctionCompleted("GetMessages");
}
public void onException(Exception e) {
WebServiceError("GetMessages", e.getMessage(), e);
}
public void onFailure(String message) {
WebServiceError("GetMessages", message);
}
......@@ -807,9 +794,6 @@ public class GameClient extends AndroidNonvisibleComponent
}
FunctionCompleted("Invite");
}
public void onException(final Exception e) {
WebServiceError("Invite", e.getMessage(), e);
}
public void onFailure(final String message) {
WebServiceError("Invite", message);
}
......@@ -859,9 +843,6 @@ public class GameClient extends AndroidNonvisibleComponent
processInstanceLists(response);
FunctionCompleted("LeaveInstance");
}
public void onException(final Exception e) {
WebServiceError("LeaveInstance", e.getMessage(), e);
}
public void onFailure(final String message) {
WebServiceError("LeaveInstance", message);
}
......@@ -910,9 +891,6 @@ public class GameClient extends AndroidNonvisibleComponent
NewInstanceMade(InstanceId());
FunctionCompleted("MakeNewInstance");
}
public void onException(final Exception e) {
WebServiceError("MakeNewInstance", e.getMessage(), e);
}
public void onFailure(final String message) {
WebServiceError("MakeNewInstance", message);
}
......@@ -958,9 +936,6 @@ public class GameClient extends AndroidNonvisibleComponent
public void onSuccess(final JSONObject response) {
FunctionCompleted("SendMessage");
}
public void onException(final Exception e) {
WebServiceError("SendMessage", e.getMessage(), e);
}
public void onFailure(final String message) {
WebServiceError("SendMessage", message);
}
......@@ -1016,10 +991,7 @@ public class GameClient extends AndroidNonvisibleComponent
}
FunctionCompleted("ServerCommand");
}
public void onException(Exception e) {
ServerCommandFailure(command, arguments);
WebServiceError("ServerCommand", e.getMessage(), e);
}
public void onFailure(String message) {
ServerCommandFailure(command, arguments);
WebServiceError("ServerCommand", message);
......@@ -1068,9 +1040,6 @@ public class GameClient extends AndroidNonvisibleComponent
processInstanceLists(response);
FunctionCompleted("SetInstance");
}
public void onException(final Exception e) {
WebServiceError("SetInstance", e.getMessage(), e);
}
public void onFailure(final String message) {
WebServiceError("SetInstance", message);
}
......@@ -1111,9 +1080,6 @@ public class GameClient extends AndroidNonvisibleComponent
public void onSuccess(final JSONObject response) {
FunctionCompleted("SetLeader");
}
public void onException(final Exception e) {
WebServiceError("SetLeader", e.getMessage(), e);
}
public void onFailure(final String message) {
WebServiceError("SetLeader", message);
}
......@@ -1204,9 +1170,6 @@ public class GameClient extends AndroidNonvisibleComponent
callback.onFailure("Failed to parse JSON response to command " + commandName);
}
}
public void onException(Exception e) {
callback.onException(e);
}
public void onFailure(String failureMessage) {
Log.d(LOG_TAG, "Posting to server failed for " + commandName + " with arguments " +
params + "\n Failure message: " + failureMessage);
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.runtime;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
......@@ -30,11 +30,16 @@ import com.google.appinventor.components.common.ComponentConstants;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.runtime.util.AlignmentUtil;
import com.google.appinventor.components.runtime.util.AsyncCallbackPair;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.util.ViewUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* A container for components that arranges them linearly, either
* horizontally or vertically.
......@@ -347,33 +352,15 @@ public class HVArrangement extends AndroidViewComponent implements Component, Co
// Load image from file.
if (imagePath.length() > 0) {
MediaUtil.getBitmapDrawableAsync(container.$form(), imagePath, new AsyncCallbackPair<BitmapDrawable>() {
@Override
public void onFailure(String message) {
Log.w(LOG_TAG, "Error loading background for canvas: " + message);
container.$form().dispatchErrorOccurredEvent(HVArrangement.this, "Image", ErrorMessages.ERROR_CANNOT_READ_FILE, imagePath);
}
@Override
public void onException(Exception e) {
Log.w(LOG_TAG, "Error loading background for canvas", e);
container.$form().dispatchErrorOccurredEvent(HVArrangement.this, "Image", ErrorMessages.ERROR_CANNOT_READ_FILE, imagePath);
}
@Override
public void onSuccess(BitmapDrawable result) {
backgroundImageDrawable = result;
container.$form().runOnUiThread(new Runnable() {
public void run() {
updateAppearance();
}
});
try {
backgroundImageDrawable = MediaUtil.getBitmapDrawable(container.$form(), imagePath);
} catch (IOException ioe) {
// Fall through with a value of null for backgroundImageDrawable.
}
});
} else {
// Update the appearance based on the new value of backgroundImageDrawable.
updateAppearance();
}
// Update the appearance based on the new value of backgroundImageDrawable.
updateAppearance();
}
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
......@@ -9,6 +9,7 @@ package com.google.appinventor.components.runtime;
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.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
......@@ -17,19 +18,19 @@ import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.runtime.errors.IllegalArgumentError;
import com.google.appinventor.components.runtime.util.AnimationUtil;
import com.google.appinventor.components.runtime.util.AsyncCallbackPair;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.util.HoneycombUtil;
import com.google.appinventor.components.runtime.util.MediaUtil;
import com.google.appinventor.components.runtime.util.SdkLevel;
import com.google.appinventor.components.runtime.util.ViewUtil;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;
/**
* Component for displaying images and animations.
*
......@@ -42,7 +43,6 @@ import android.widget.ImageView;
@SimpleObject
@UsesPermissions(permissionNames = "android.permission.INTERNET")
public final class Image extends AndroidViewComponent {
private static final String LOG_TAG = Image.class.getSimpleName();
private final ImageView view;
......@@ -104,38 +104,15 @@ public final class Image extends AndroidViewComponent {
public void Picture(String path) {
picturePath = (path == null) ? "" : path;
MediaUtil.getBitmapDrawableAsync(container.$form(), picturePath, new AsyncCallbackPair<BitmapDrawable>() {
@Override
public void onFailure(String message) {
Log.w(LOG_TAG, "Error loading background for image: " + message);
container.$form().dispatchErrorOccurredEvent(Image.this, "Picture", ErrorMessages.ERROR_CANNOT_READ_FILE, picturePath);
container.$form().runOnUiThread(new Runnable() {
public void run() {
ViewUtil.setImage(view, null);
}
});
}
@Override
public void onException(Exception e) {
Log.w(LOG_TAG, "Error loading background for image", e);
container.$form().dispatchErrorOccurredEvent(Image.this, "Picture", ErrorMessages.ERROR_CANNOT_READ_FILE, picturePath);
container.$form().runOnUiThread(new Runnable() {
public void run() {
ViewUtil.setImage(view, null);
}
});
}
Drawable drawable;
try {
drawable = MediaUtil.getBitmapDrawable(container.$form(), picturePath);
} catch (IOException ioe) {
Log.e("Image", "Unable to load " + picturePath);
drawable = null;
}
@Override
public void onSuccess(final BitmapDrawable result) {
container.$form().runOnUiThread(new Runnable() {
public void run() {
ViewUtil.setImage(view, result);
}
});
}
});
ViewUtil.setImage(view, drawable);
}
/**
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
package com.google.appinventor.components.runtime;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
......@@ -18,8 +21,6 @@ import com.google.appinventor.components.annotations.UsesPermissions;
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.AsyncCallbackPair;
import com.google.appinventor.components.runtime.util.ErrorMessages;
import com.google.appinventor.components.runtime.util.MediaUtil;
/**
......@@ -50,7 +51,6 @@ import com.google.appinventor.components.runtime.util.MediaUtil;
@SimpleObject
@UsesPermissions(permissionNames = "android.permission.INTERNET")
public class ImageSprite extends Sprite {
private static final String LOG_TAG = ImageSprite.class.getSimpleName();
private final Form form;
private BitmapDrawable drawable;
private int widthHint = LENGTH_PREFERRED;
......@@ -125,41 +125,14 @@ public class ImageSprite extends Sprite {
@SimpleProperty
public void Picture(String path) {
picturePath = (path == null) ? "" : path;
MediaUtil.getBitmapDrawableAsync(form, picturePath, new AsyncCallbackPair<BitmapDrawable>() {
@Override
public void onFailure(String message) {
Log.w(LOG_TAG, "Error loading background for canvas: " + message);
drawable = null;
form.dispatchErrorOccurredEvent(ImageSprite.this, "Picture", ErrorMessages.ERROR_CANNOT_READ_FILE, picturePath);
form.runOnUiThread(new Runnable() {
public void run() {
registerChange();
}
});
}
@Override
public void onException(Exception e) {
Log.w("ImageSprite", "Unable to load " + picturePath, e);
drawable = null;
form.dispatchErrorOccurredEvent(ImageSprite.this, "Picture", ErrorMessages.ERROR_CANNOT_READ_FILE, picturePath);
form.runOnUiThread(new Runnable() {
public void run() {
registerChange();
}
});
}
@Override
public void onSuccess(BitmapDrawable result) {
drawable = result;
form.runOnUiThread(new Runnable() {
public void run() {
registerChange();
}
});
}
});
try {
drawable = MediaUtil.getBitmapDrawable(form, picturePath);
} catch (IOException ioe) {
Log.e("ImageSprite", "Unable to load " + picturePath);
drawable = null;
}
// note: drawable can be null!
registerChange();
}
// The actual width/height of an ImageSprite whose Width/Height property is set to Automatic or
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package com.google.appinventor.components.runtime;
import android.os.Handler;
import android.util.Log;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
......@@ -124,14 +123,6 @@ public final class MediaStore extends AndroidNonvisibleComponent implements Comp
}
});
}
public void onException(final Exception e) {
androidUIHandler.post(new Runnable() {
public void run() {
Log.e(LOG_TAG_COMPONENT, "Exception in PostMedia", e);
WebServiceError(e.getMessage());
}
});
}
public void onFailure(final String message) {
androidUIHandler.post(new Runnable() {
public void run() {
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
......@@ -25,7 +25,6 @@ import com.google.appinventor.components.runtime.util.JsonUtil;
import com.google.appinventor.components.runtime.util.WebServiceUtil;
import android.os.Handler;
import android.util.Log;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
......@@ -197,14 +196,6 @@ public class TinyWebDB extends AndroidNonvisibleComponent implements Component {
}
});
}
public void onException(final Exception e) {
androidUIHandler.post(new Runnable() {
public void run() {
Log.e(LOG_TAG, "Exception in WebService", e);
WebServiceError(e.getMessage());
}
});
}
public void onFailure(final String message) {
// Pass any failure message from the Web service command back
// to the error handler.
......@@ -316,14 +307,6 @@ public class TinyWebDB extends AndroidNonvisibleComponent implements Component {
}
}
}
public void onException(final Exception e) {
androidUIHandler.post(new Runnable() {
public void run() {
Log.e(LOG_TAG, "Exception in TinyWebDB", e);
WebServiceError(e.getMessage());
}
});
}
public void onFailure(final String message) {
// Signal a Web error event to indicate that there was no response
// to this request for a value. Note that this needs to be posted to the UI
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
......@@ -319,14 +319,6 @@ public class Voting extends AndroidNonvisibleComponent implements Component {
}
}
}
public void onException(final Exception e) {
androidUIHandler.post(new Runnable() {
public void run() {
Log.e(LOG_TAG, "Exception in Voting component: ", e);
WebServiceError(e.getMessage());
}
});
}
public void onFailure(final String message) {
Log.w(LOG_TAG, "postRequestBallot Failure " + message);
androidUIHandler.post(new Runnable() {
......@@ -403,14 +395,6 @@ public class Voting extends AndroidNonvisibleComponent implements Component {
}
});
}
public void onException(final Exception e) {
androidUIHandler.post(new Runnable() {
public void run() {
Log.e(LOG_TAG, "Exception in Voting component: ", e);
WebServiceError(e.getMessage());
}
});
}
public void onFailure(final String message) {
Log.w(LOG_TAG, "postSendBallot Failure " + message);
androidUIHandler.post(new Runnable() {
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
......@@ -28,13 +28,6 @@ package com.google.appinventor.components.runtime.util;
*/
void onFailure(String message);
/**
* Called when an asynchronous call fails to complete due to an exception.
*
* @param e An exception thrown during asynchronous operation.
*/
void onException(Exception e);
/**
* Called when an asynchronous call completes successfully.
*
......
......@@ -59,7 +59,7 @@ public class MediaUtil {
private static class Synchronizer<T> {
private volatile boolean finished = false;
private T result;
private Throwable error;
private String error;
public synchronized void waitfor() {
while (!finished) {
......@@ -76,8 +76,9 @@ public class MediaUtil {
notifyAll();
}
public synchronized void error(Throwable e) {
this.error = e;
public synchronized void error(String error) {
finished = true;
this.error = error;
notifyAll();
}
......@@ -85,7 +86,7 @@ public class MediaUtil {
return result;
}
public Throwable getError() {
public String getError() {
return error;
}
......@@ -328,15 +329,12 @@ public class MediaUtil {
* @param mediaPath the path to the media
* @return a Drawable or null
*
* This function is provided for backward compatibility for
* extensions that may use this synchronous version. We used to do
* all of the work manipulating the image on the UI Thread. Now we
* call the Async method (below) and block until our callback is
* called. Note: This means we are blocking on the UI Thread, which
* is *not* a good idea. Extension writers should upgrade their code
* to use the asynchronous version. Apps that use this method may
* not work on newer Android devices which are strict about how the
* UI Thread my block.
* This version of getBitmapDrawable can be used synchronously. It
* uses the Asynchronous version. Note: This means we are blocking
* on the UI Thread, which is *not* a good idea. However testing has
* revealed that blocking the UI thread may be better then having
* loaded images "appear" fractions of seconds after they were
* requested.
*
*/
public static BitmapDrawable getBitmapDrawable(Form form, String mediaPath)
......@@ -348,12 +346,7 @@ public class MediaUtil {
final AsyncCallbackPair<BitmapDrawable> continuation = new AsyncCallbackPair<BitmapDrawable>() {
@Override
public void onFailure(String message) {
// This is never called by the code below, so we do nothing
syncer.wakeup(null);
}
@Override
public void onException(Exception e) {
syncer.error(e);
syncer.error(message);
}
@Override
public void onSuccess(BitmapDrawable result) {
......@@ -364,18 +357,11 @@ public class MediaUtil {
syncer.waitfor();
BitmapDrawable result = (BitmapDrawable) syncer.getResult();
if (result == null) {
Throwable error = syncer.getError();
if (error != null) {
if (error instanceof IOException) {
throw (IOException)error;
} else {
throw new RuntimeException(error);
}
}
String error = syncer.getError();
throw new IOException(error);
} else {
return result;
}
return null; // Make the compiler happy
}
/**
......@@ -451,9 +437,9 @@ public class MediaUtil {
android.R.drawable.picture_frame, null));
continuation.onSuccess(drawable);
}
continuation.onException(e);
continuation.onFailure(e.getMessage());
} catch(Exception e) {
continuation.onException(e);
continuation.onFailure(e.getMessage());
} finally {
if (is != null) {
try {
......@@ -466,12 +452,7 @@ public class MediaUtil {
}
}
};
// if (mediaSource == MediaSource.URL) {
AsynchUtil.runAsynchronously(loadImage);
// } else {
// loadImage.run();
// }
AsynchUtil.runAsynchronously(loadImage);
}
private static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) {
......
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2016 MIT, All rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
......@@ -98,9 +98,6 @@ public class WebServiceUtil {
callback.onFailure(e.getMessage());
}
}
public void onException(Exception e) {
callback.onException(e);
}
public void onFailure(String failureMessage) {
callback.onFailure(failureMessage);
}
......@@ -120,18 +117,15 @@ public class WebServiceUtil {
* on success.
*/
public void postCommandReturningObject(final String serviceURL,final String commandName,
List<NameValuePair> params, final AsyncCallbackPair<JSONObject> callback) {
List<NameValuePair> params, final AsyncCallbackPair<JSONObject> callback) {
AsyncCallbackPair<String> thisCallback = new AsyncCallbackPair<String>() {
public void onSuccess(String httpResponseString) {
public void onSuccess(String httpResponseString) {
try {
callback.onSuccess(new JSONObject(httpResponseString));
} catch (JSONException e) {
callback.onFailure(e.getMessage());
}
}
public void onException(Exception e) {
callback.onException(e);
}
public void onFailure(String failureMessage) {
callback.onFailure(failureMessage);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment