Fork me on Github

Shivam Verma
    Blog     Portfolio     About Me / Contact     Feed

Privly reading app integration and detecting swipe gesture.

Reading App

The Privly JS reading app helps a user to view the encrypted content behind privly links. We’ve been working to make sure that the same js app can be used on all platforms and have successfully integrated it on android apart from the web application and the firefox and chrome extensions. This makes it really easy to drop in new Encryption Apps across all platforms.

The only major problem that I encountered while integrating the application was loading a Privly URL in the WebView. The links generated by the encryption applications could not be loaded because part of the URL was encoded and part of it wasn’t. It seems that the android webview is not as forgiving as its web counterpart. So I had to encode the whole URL before passing it on to the JS. This required a decoding method on the JS side since the other methods expected the URL to be partially encoded. And this had to be implemented without making any android specific change to the JS.

I added a decodeURIComponent() to the code before passing the URL to other methods but this caused the JS to call the decode method twice on the same URL leading to completely decoded URL ( since parameters.getApplicationURL() was being called twice) which in turn led to empty parameters being passed around in the application. Finally, as Sean suggested, I ended up removing one of the calls to parameters.getApplicationURL() as it wasn’t really necessary for the working of the current reading application. Now, we had a partially encoded URL which could be successfully used to fetch the privlyDataURL parameter with the random auth_token.

Here’s a screenshot of the reading app in action!

Screenshot_2013-09-02-16-29-04 (1)

Now, I wanted to load the URLs for the reading app from a SQLite Database. This’ll help in connecting a user’s social / email feed (Planned Feature). Adding a SQLite Db was pretty straight forward. The Android Developer Website is an exhaustive resource.

Swipe Gesture Detection

The next feature to be added was swipe to change posts. The Privly URLs were to be fetched from the SQLite Db and move back and forth on right and left swipe. Now, swipe was something that I had never worked on but it was fairly simple. This stackoverflow answer was of great help.

In onCreate() method

// Setup WebView to detect swipes.
gestureDetector = new GestureDetector(this, new SwipeGestureDetector());
gestureListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }
};
webView.setOnTouchListener(gestureListener);

And then we define the SwipeGestureDector() Class

class SwipeGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
    float velocityY) {
        try {
            Values values = new Values(getApplicationContext());
            HashMap<String, Integer> valuesForSwipe = values
            .getValuesForSwipe();
            if (Math.abs(e1.getY() - e2.getY()) > valuesForSwipe.get("swipeMaxOffPath"))
                return false;
            if (e1.getX() - e2.getX() > valuesForSwipe.get("swipeMinDistance")
            && Math.abs(velocityX) > valuesForSwipe.get("swipeThresholdVelocity")) {
                Toast.makeText(getApplicationContext(), "Left Swipe",
                Toast.LENGTH_SHORT).show();
                return true;
            } else if (e2.getX() - e1.getX() > valuesForSwipe.get("swipeMinDistance")
            && Math.abs(velocityX) > valuesForSwipe.get("swipeThresholdVelocity")) {
                Toast.makeText(getApplicationContext(), "Right Swipe",
                Toast.LENGTH_SHORT).show();
                return true;
            }
            } catch (Exception e) {
        }
        return false;
    }

    @Override
    public boolean onDown(MotionEvent event) {
        Log.d("tag", "onDown: " + event.toString());
        return true;
    }

}

We can use the ViewConfiguration Class to fetch the optimum values for variables which help us determine the type of gesture.

HashMap<String, Integer> swipeValues = new HashMap<String, Integer>();
ViewConfiguration vc = ViewConfiguration.get(context);
swipeValues.put("swipeMinDistance", vc.getScaledPagingTouchSlop());
swipeValues.put("swipeThresholdVelocity",
vc.getScaledMinimumFlingVelocity());
swipeValues.put("swipeMaxOffPath", vc.getScaledMinimumFlingVelocity());

Next, I’ll be working on fetching Privly Links from a user’s social/email feed and pushing it to local db.

Undergraduate Thesis

In my Undergrad Thesis I am working on a very interesting problem of Indoor Navigation using smart phones ( where GPS fails!) Just starting to learn how the sensors work. Hell lot of mathematics.

Latest privly-android commit

https://github.com/vshivam/privly-android/tree/1d508671f6732c62d8fc5fafc59f1e55cebd3652

comments powered by Disqus