Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-24605] Android: Fixed crashes caused by loading image URLs when JS is running on main thread #10222

Merged
merged 6 commits into from Aug 7, 2018

Conversation

jquick-axway
Copy link
Contributor

@jquick-axway jquick-axway commented Aug 1, 2018

JIRA:

Summary:

  • Fixed image URL crash if JS running on main thread for:
    • TIMOB-24605 View object's "backgroundImage" property.
    • TIMOB-26249 ImageView object's image property while "autorotate" is true.
    • ImageView object's "defaultImage" property.
  • Modified Java "TiImageHelper" class to support reading JPEG EXIF orientation from a stream.
    • Allows ImageView to auto-rotate JPEG downloaded from the Internet if needed.
  • Added new Java class "TiInputStreamWrapper".
    • Used to close the HttpUrlConnection when the stream has been closed.
  • Added redirect endpoint fetching methods to Java "TiResponseCache".
    • Needed to fetch cached image when using a URL that redirects. Major optimization.

Test 1:

  1. Uninstall the app from the device if already installed. (Will delete cached web responses.)
  2. Make sure the Android device has Internet access.
  3. Set "tiapp.xml" file property "run-on-main-thread" to true.
  4. Build and run the below code.
  5. Verify that the image was successfully displayed without crashing.
var window = Ti.UI.createWindow();
window.add(Ti.UI.createView({
	// This URL redirects from HTTP to HTTPS.
	backgroundImage: "http://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Portrait_1.jpg",
}));
window.open();

Test 2:

  1. Uninstall the app from the device if already installed. (Will delete cached web responses.)
  2. Make sure the Android device has Internet access.
  3. Set "tiapp.xml" file property "run-on-main-thread" to true.
  4. Build and run the below code.
  5. Verify that the image was displayed "upright" successfully, without crashing.
var window = Ti.UI.createWindow();
var imageView = Ti.UI.createImageView({
	// This URL redirects from HTTP to HTTPS.
	image: "http://raw.githubusercontent.com/recurser/exif-orientation-examples/master/Portrait_3.jpg",
	autorotate: true,
});
window.add(imageView);
window.open();

…Image" to url while JS running on main thread

- Fixed [TIMOB-26249] where setting ImageView "image" to a URL while "autorotate" is true would cause a crash.
- Fixed crash with ImageView "defaultImage" set to a URL while JS is running on main thread.
- Modified "TiImageHelper" to read exif orientation from a stream. Can now acquire exif info from url based image.
- Added new Java class "TiInputStreamWrapper".
if (TiApplication.isUIThread()) {
// Anonymous class used to call this blockingDownload() method on another thread.
// Downloaded content will be made available via Titanium's "TiResponseCache".
class DownloadOperation implements Runnable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of creating an anonymous class, you should be able to do this:

public InputStream blockingDownload(final URI uri)
{
...
	// Perform the blocking download.
	try {
		Thread downloadThread = new Thread(new Runnable() {
			@Override
			public void run() {
				try (InputStream stream = blockingDownload(uri)) {
					if (stream != null) {
						KrollStreamHelper.pump(stream, null);
					}
				} catch (Exception ex) {
				}
			}
		});
		downloadThread.start();

		// wait for thread to finish
		downloadThread.join();
	} catch (Exception ex) {
	}
	...
...

// Close the connection and attempt to connect to the redirect URL.
httpConnection.disconnect();
continue;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe log a warning if there's a bad response (404?), or throw an exception?

You can test with https://httpbin.org/status/404

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. The image property used to log a 404 warning and now it doesn't. I'll add some log messages.

Also, backgroundImage used to crash the app for a 404 response when "run-on-main-thread" was false. I've inadvertently fixed that. :)

- Add more logging messages.
- Simplified Threaded blocking download code.
@jquick-axway
Copy link
Contributor Author

Updated PR

Copy link
Contributor

@garymathews garymathews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CR: PASS

@build
Copy link
Contributor

build commented Aug 7, 2018

Messages
📖

💾 Here's the generated SDK zipfile.

Generated by 🚫 dangerJS

@hansemannn hansemannn merged commit b795395 into tidev:master Aug 7, 2018
@jquick-axway jquick-axway modified the milestones: 7.4.0, 7.5.0 Aug 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants