Rafael Sanches

January 30, 2011

HTC sense ui breaks the way the WebView settings behave

Filed under: android — mufumbo @ 11:32 pm

I am “amused” to discover that HTC sense has changed even the way android breaks to access of a WebView from outside the UI thread.

Since I don’t own a phone with HTC sense, it took me months to understand this and I only fixed the issue thanks to a kind user who sent me logs.

For example, calling this outside from a UI thread works on the normal android SDK’s:
webSettingsObj.setBuiltInZoomControls(true);

Instead, on HTC Sense phones it breaks here:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
 at android.os.Handler.(Handler.java:121)
 at com.htc.multitouch.MultiTouchDetector$MultiTouchHandler.(MultiTouchDetector.java:180)
 at com.htc.multitouch.MultiTouchDetector.(MultiTouchDetector.java:242)
 at android.webkit.WebView$HTCMultiTouch.fnCreateMultiTouchListener(WebView.java:11781)
 at android.webkit.WebView.enableMultiTouch(WebView.java:10893)
 at android.webkit.WebView.updateMultiTouchSupport(WebView.java:905)
 at android.webkit.WebSettings.setBuiltInZoomControls(WebSettings.java:474)

Now, I know know that anything that changes the UI shouldn’t be executed outside of the UI Thread, but still..

January 29, 2011

upload using multipart post using httpclient in android

Filed under: android — mufumbo @ 7:01 pm

A very common caveat, when doing android applications, is fighting to keep the APK size small.

Many applications need the ability to upload binary data to their server and when you arrive there you see that the android SDK doesn’t have the http-client libraries to send multipart posts.

The easiest way is to include the JAR for httpmime and apache_mime4j, but it takes way too much space; 300kb.

To overcome this you can implement your own HttpEntity. In this way the code is:

public class SimpleMultipartEntity implements HttpEntity {

    private final static char[] MULTIPART_CHARS = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
        .toCharArray();

    private String boundary = null;

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    boolean isSetLast = false;
    boolean isSetFirst = false;

    public SimpleMultipartEntity() {
        final StringBuffer buf = new StringBuffer();
        final Random rand = new Random();
        for (int i = 0; i < 30; i++) {
            buf.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]);
        }
        this.boundary = buf.toString();

    }

    public void writeFirstBoundaryIfNeeds(){
        if(!isSetFirst){
            try {
                out.write(("--" + boundary + "\r\n").getBytes());
            } catch (final IOException e) {
                Log.e(Constants.TAG, e.getMessage(), e);
            }
        }
        isSetFirst = true;
    }

    public void writeLastBoundaryIfNeeds() {
        if(isSetLast){
            return ;
        }
        try {
            out.write(("\r\n--" + boundary + "--\r\n").getBytes());
        } catch (final IOException e) {
            Log.e(Constants.TAG, e.getMessage(), e);
        }
        isSetLast = true;
    }

    public void addPart(final String key, final String value) {
        writeFirstBoundaryIfNeeds();
        try {
            out.write(("Content-Disposition: form-data; name=\"" +key+"\"\r\n").getBytes());
            out.write("Content-Type: text/plain; charset=UTF-8\r\n".getBytes());
            out.write("Content-Transfer-Encoding: 8bit\r\n\r\n".getBytes());
            out.write(value.getBytes());
            out.write(("\r\n--" + boundary + "\r\n").getBytes());
        } catch (final IOException e) {
            Log.e(Constants.TAG, e.getMessage(), e);
        }
    }

    public void addPart(final String key, final String fileName, final InputStream fin){
        addPart(key, fileName, fin, "application/octet-stream");
    }

    public void addPart(final String key, final String fileName, final InputStream fin, String type){
        writeFirstBoundaryIfNeeds();
        try {
            type = "Content-Type: "+type+"\r\n";
            out.write(("Content-Disposition: form-data; name=\""+ key+"\"; filename=\"" + fileName + "\"\r\n").getBytes());
            out.write(type.getBytes());
            out.write("Content-Transfer-Encoding: binary\r\n\r\n".getBytes());

            final byte[] tmp = new byte[4096];
            int l = 0;
            while ((l = fin.read(tmp)) != -1) {
                out.write(tmp, 0, l);
            }
            out.flush();
        } catch (final IOException e) {
            Log.e(Constants.TAG, e.getMessage(), e);
        } finally {
            try {
                fin.close();
            } catch (final IOException e) {
                Log.e(Constants.TAG, e.getMessage(), e);
            }
        }
    }

    public void addPart(final String key, final File value) {
        try {
            addPart(key, value.getName(), new FileInputStream(value));
        } catch (final FileNotFoundException e) {
            Log.e(Constants.TAG, e.getMessage(), e);
        }
    }

    @Override
    public long getContentLength() {
        writeLastBoundaryIfNeeds();
        return out.toByteArray().length;
    }

    @Override
    public Header getContentType() {
        return new BasicHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    }

    @Override
    public boolean isChunked() {
        return false;
    }

    @Override
    public boolean isRepeatable() {
        return false;
    }

    @Override
    public boolean isStreaming() {
        return false;
    }

    @Override
    public void writeTo(final OutputStream outstream) throws IOException {
        outstream.write(out.toByteArray());
    }

    @Override
    public Header getContentEncoding() {
        return null;
    }

    @Override
    public void consumeContent() throws IOException,
    UnsupportedOperationException {
        if (isStreaming()) {
            throw new UnsupportedOperationException(
            "Streaming entity does not implement #consumeContent()");
        }
    }

    @Override
    public InputStream getContent() throws IOException,
    UnsupportedOperationException {
        return new ByteArrayInputStream(out.toByteArray());
    }

}

January 13, 2011

server-side calls are better for Ad serving. Downside of AFMA/AdMob.

Filed under: android, programming, revenue, technology — mufumbo @ 7:47 pm

Ad networks that offer server-side calls gives us a big advantage. We can simply get a image + link and print on a android WebView.

Due to limitations of the android framework it’s the only way to have non-intrusive ads inside a WebView. Specially when the WebView must be layout_height=”fill_parent”, for a correct scrolling and zoom control.

JavaScript ads are not an option, since the only work around is to inject JavaScript into the WebView which prevents me from working with JavaScript ads that are based on a inline include.

Advantages of the server-side call instead of SDK:
Reduce APK size: I can implement my own WebView to print ads. AdMob SDK takes 140kb, Google AFMA takes 40kb, so 30% of my app is taken from ads sdk’s.
– AFMA SDK is slow: rendering the adsense javascript is slow. I feel my app freezing and coming back after rendering adsense. Admob is much faster.
– The developer have total control to optimize it.

FYI: After the death of Quattro, the best network that is offering server-side calls is Millenial Media.

Blog at WordPress.com.

%d bloggers like this: