Saturday, December 6, 2014

Searching for close matches to my choices


Every user is asked a serious of binary questions:
What do you prefer, cats or dogs
Do prefer winter or summer
Do you consider computer-science gradatees as scientinst. Yes/No

There will be lots of users (1M), but not more than 300 questions.
Find the 10 closest matches for a certain user.


This problem is a similar to search nearest-neighbors of  Hamming distance. It is simplified as there are only binary(yes/no questions), hamming works with more options, but nearest-neighbors is extremely difficult problem....

1. For a query for unkown, new user, this stackoverflow question says that it`s a tricky problem.  For short distances (4 from 32 bit integers) BK-tree and VP-tree can speed up queries up to 10 times, or even 1000 times for very short distance of 1.   For medium ranges and above, no good data structure exists, and brute-force is needed.

If we can assume that our users always have 10 close matches with short distance, we can use one of the suggested data-structure, otherwise, just use blunt-force each time.

2. For a big calculation of all the matches, there might be a better solution. Didn`t find one.

Tuesday, November 4, 2014

Page pagination in JqueryMobile


  Infinite number of different, similar, pages like Tinder app, where each page contains images,labels and text.
One ajax request to the server contains the data for the next 10 pages, so we won`t do the "simple" solution of reading a new ajax page each time.


We will have 3 pages  (match1, match2, match3) each one as a "next" button pointing to the next one.
We will save the data of the next 10 pages at any given point, let`s call it the model.
On each page pagebeforeshow event, we will change the data on the non visible page.
Once the model data is empty (all the 10 pages were seen), we will use an ajax call to fill the model.

model state= 50,51,52,53,...,59
[page1] [page2] [page3]

on the first time (when all pages are empty) and later, when one page becomes obsolete, we will fill the obsolete pages
  50            51         52
[page1] [page2] [page3]

  53            51         52
[page1] [page2] [page3]


 59            57         58
[page1] [page2] [page3]

when we get to this state, once 58 becomes active, we will want to replace page2 with 60, so we will do an ajax call and update the model with it,
delicate points:  
  • If ajax call is on it`s way, there is no reason to make a new call.
  • We should also clean the previous page to null data (default image and text), in case the user will be faster than our ajax call. We may even consider creating a "loading" progress bar, if the ajax is very slow.


Thursday, September 11, 2014

Facebook user authentication

On the client side, you can call the facebook api to login a user using their popup window. The user will get an very temporary (few hours) access-token which will be saved in a cookie (by the facebook api automatically) and can be used to create API calls.

In this post, we will discuss user-authentication which not quite the same. We want that our server will know that the user is legit.

How it worked before:

Signup: You had the signup with user&password pair (and usually an email for lost password...). The server will save this pair in the database.
Sign-in: Each time you attemp to login, you send the pair to the server and the server replay with good or bad.
Sign-in cookie: As you don`t want the user to re-login in every page, or every day, you can decide to put a cookie which bypass this. An (unscure) cookie can be the user&password, but in this case, if someone browse the cookies on the user-machine, he can see them. It can also be a temporary access-token, for example a unique number which is good for one-day and saved with the user/password pair. Tomorrow, it will be deleted and the user will have to relogin.


Signup: The user already signed-up for facebook years ago. The user will have to permit your application to see his details, on the first login.
: The user will see facebook login.
Sign-in cookie: facebook, behind-the-scenes, saves a temporary access-token so you will not have to, so if you call the user login again after a short time, the user will not see the login dialog.

The user code for using it is quite short, as facebook sdk is doing most of the work, and can be seen at facebook api login samples.

Combine the two: But how can my server know that the user is a legit one?

On the client side, if login was correct, we can send the server the facebook user-id and use it as a token.  This is only half-secure, as anyone with some programming skills, can get the facebook user id and modify our client code to impersonate someone else.
But, it will stop non-programmers/hackers , and can be used for prototyping ONLY stage.

A proper authentication is to send the server the facebook-id and the access-token (which is like user/password pair). The server will ask facebook to verify it (instead of looking into the local database) and then send a temporary cookie, which is the same as the user/password cookie. This will used so the user will not have to login again and again, and that our server will not have to verify the facebook-id/access-token.

And to the implementation

  1. Do it yourself means server code for asking facebook verification and a db table with facebook-user-id to session.
  2. On node.js you can use passport library to do part of the work for you.
  3. Using facebook parse clould-server framework, they will do all of it, including storing the db for you (but it is limiting as you won't have direct access)

Tuesday, September 9, 2014

PhoneGap build

Phonegap build is a great tool for quick testing of your mobile-app as a deployed app. Copy all the relevant client file locally  (including /js /img /css folders). Make sure you have index.html file. create a simple config.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns=""  xmlns:gap="" id=""  versionCode="10" version="1.1.0">
  <!-- versionCode is optional and Android only -->
    MyName application
  <author href="http:/" email="">
    Developer name


Facebook login depends on a domain which you configure at facebook. If your local app is deployed on the phone and the page is not served from the server, it will fail to work.

TODO: solution: load your facebook-login page from the server.

beauty:  to have integration with facebook native app, you will need to work harder and use a plugin, lets ignore for now.

JQueryMobile performance

JQueryMobile defaults are extraordinarily bad and causing bad performance. But with few tweaks you can get to an almost-native performance, as long as you are ok with reduced visual beautiy.

See this blog for some ideas/details/hacks.

Bad transitions performance on Android

On Android, on page-change there is a bad effect of "jitter" once before moving to the page. It is not seen of chrome pc.
few approaches (see stackoverflow)
1 .Simplest : change transition to none.  Fastest on Android. data-transition="none". It also make the wierd temporary scroll bar go away
$.mobile.defaultPageTransition  = 'none';
$.mobile.defaultDialogTransition = 'none';
$.mobile.buttonMarkup.hoverDelay = 0;
Note some places say u need user-scalalbe=no on phono gap even with none data-transition

2. using this: <meta name="viewport" content="width=device-width, user-scalable=no" /> fade works fine without flicker, but it is still slow.

Slider/Range widgets are very slow on Android

known issue planned to be solved on 1.5
iOS performance was ok, on the one sample iPhone I tried.

Click delay?

1. One common internet opinition is to use . Didn`t help me much... 2.   On JQueryMobile I believe there is another issue. When you click on a button, the blue highlight starts, and the actual action/page-transition occurs considrable time afterwards (half-a-second...) You can remove this, by adding the fastclick class to relevant elements, and declare it as below, but note that it can cause problems with paenl buttons and that the highlight disappears...
$( document ).one( "pageshow", function() {
               return false;

TODO; an actual better solution will be to first trigger an async-action (like page change), then show the blue-highlight. It appears it work the other way around now.
wierdly checkbox widget work better.

How to choose multi-panel or single?:

Approach 1 -  copy the header,panel each time.

It is better to have two separate pages (.html), one which each page in it.

<div data-role="page" id="profile">

    <div data-role="header" data-position="fixed" data-tap-toggle="false" data-id="persist1" data-theme="a">
      <span class="ui-title">Edit Profile</span>
        <div class="ui-btn-right">
            <a id="myButton1" class="headerButton" href="index.html" data-role="button">profile</a>            <a id="myButton2" class="headerButton" href="index2.html" data-role="button">index2</a>

        <a href="#myPanel" class="ui-btn ui-btn-left ui-icon-bars ui-btn-icon-notext">not-visible</a>

   <div data-role="panel" id="myPanel">
        <h2>Panel Header</h2>
      <div data-role="main" class="ui-content" id="main-profile">
         profile page

</div> <!--of page-->

in this case, there are three issues:
1. Ugly code which is the same for each page.
2. On transition, the header slightly change, unless you add it data-theme='a' (the default is bad)
3. If you are using multiple pages in the same .html file, after you move to another page, the panel fails to work, probably as we have multiple panels with the same-id.

Approach 2 -  external header and panel

In this case, you have to add a script above ,otherwise the external panel/header will not shown at all.

  $( document ).on( "pagecreate", function() {
  $( "body > [data-role='panel']" ).panel();
  $( "body > [data-role='panel'] [data-role='listview']" ).listview();

  $( document ).one( "pageshow", function() {
   $( "body > [data-role='header']" ).toolbar();
   $( "body > [data-role='header'] [data-role='navbar']" ).navbar();




<div data-role="header" data-id="my-header" data-position="fixed" data-tap-toggle="false" data-theme="b">
     <a href="#myPanel" class="ui-btn ui-btn-left ui-icon-bars ui-btn-icon-notext">not-visible</a>
    <div class="ui-btn-right">
            <a id="myButton1" class="headerButton" href="#first" data-prefetch="true" data-transition="fade" data-role="button">profile</a>
            <a id="myButton2" class="headerButton" href="#second" data-prefetch="true" data-transition="fade" data-role="button">home</a>
</div><!-- /header -->

<div data-role="panel" id="myPanel">
        <h2>Panel Header</h2>
        <p>You can close the panel by clicking outside the panel, pressing the Esc key or by swiping.</p>

<div data-role="page" id="first">
    <div data-role="content"> 
        <p>The content</p>     
        <p>View internal page called <a href="#second">second</a></p> 
    </div><!-- /content -->

<div data-role="page" id="second">
    <div data-role="content"> 
        <p>I'm the second content 2nd</p>     
        <p><a href="#first">Back to first</a></p> 
    </div><!-- /content -->


Issues with this approach
1. You will have to change pragmatically the title, active-button of the header once you move to another page
2. still need the data-theme="a" to have any theme to your page.

Let`s discuss the events in this scenario.  If you programtically modify the content of the page and not loading a different page, the beforecreate,create and init event will be called the first time you move to that page, but not afterwards.  Also remember you have to manually call the page enhance, if your change require widget change.
(Navigate from A to B)
page B---pagebeforecreate   (ONCE)
page B---pagecreate            (ONCE)
page B---pageinit                 (ONCE)
page A---pagebeforehide
page B---pagebeforeshow
page A---pageremove
page A---pagehide
page B---pageshow 

            activepage =  $.mobile.pageContainer.pagecontainer("getActivePage");

            console.log( activepage[0].id);
You can put it on the pagebeforeshow event , as example.


For Android -like drawer:
 <div data-role="panel" id="myPanel"  data-display="overlay" data-position-fixed="true" data-theme="a">
data-position-fixed="true"  - you content will always jump up when you open.
data-display="overlay"  for Android style, use "push"/"reveal" for iOS style.

Monday, September 8, 2014

Profile editor using facebook data

Let`s create a simple mobile web-app, which let you to login into facebook and choose few images which you like best.


At facebook you you can query albums list and then the images of each album.
We will have 3 screens, using jquerymobile for the UI

  1. Login screen, where standard facebook login will happen. There will also be one big button "Find image"
  2. List of album names and number of images in each. each row is clickable. We use simple  jquerymobile list , see w3schools demo
  3. A grid of images of the current album. see here


Facebook login and API calls

First login using facebook, and then make API calls:
graph api explorer can be used to simulate requests.
[TODO] see the full documentation to handle batching/errors

Fetch albums

We will calls FB.api('/me/albums',fucntion) , get the result JSON and extract the albums from it. We will later replace the html of the content div and call JQueryMobile enhanceWithin() to reformat it, otherwise the list will use the browser default list.
Please note that by doing so, we ignore the url hashing scheme, so "back" will not work automatically for us. We should change this later...

function fetchAlbums()
    FB.api('/me/albums', function(result) {
      var html=  '<ul data-role="listview" data-inset="true">';
      for (var i=0; i<; i++)
        var album =[i];
        if (typeof album.count == "undefined")
        html+='<li><a href="#" onclick="fetchImages('')">''<span class="ui-li-count">'+album.count +'</span></a></li>';
      $( "#main" ).html(html).enhanceWithin();


[syntax-highlighting taken from here using brush: javascript like so: <pre class="brush: javascript">]

TODO: an album has a cover_photo property which can be added as a small icon.

Fetch album images

FB.API(albumId+"/photos") will show a list of photos.
Each photo contains array of images and a small 100px picture. doc here
Some, rather ugly, js code is needed to transform gridSize=3 to ui-grid-b(33% each) and ui-block-a/b/c. 

function fetchImages(albumId)
     var request = albumId+'/photos';
     console.log('calling'+ request);
     FB.api(albumId+'/photos',function(result) {
       var gridSize=3; //2 or more
       var maxGridChar= String.fromCharCode("a".charCodeAt(0)+gridSize-2);
       //solo/a/b/c -b means 33%*3
       var html='<div class="ui-grid-' + maxGridChar   +'">';
       for (var i=0; i<;i++)     {
         var uiBlockIndex= String.fromCharCode("a".charCodeAt(0) +i%gridSize);
         html+='<div class="ui-block-'+uiBlockIndex+'">';
         html+=  '<img src="'[i].picture+'"/>';

This table is clearly non responsive and looks rather bad. You can add some css to make it looks a bit better here:

.ui-grid-c img {





.ui-grid-c div {



JQueryMobile proper dynamic pages

This was a quick-and-dirty sample, where we just replaced the html of the ui-content and called enhancedWithin, by doing so we ignored the built-in pages system and also ignored the links routing (we used onclick to replace the content instead of using the href.
TODO: use pagecontainer widget for a proper way to do it. make sure panels still work well in this case.

Monday, July 15, 2013

Android apps - Architecture (Activity,Services,BroadcastRecievers and Alarms)

In this post I will describe the architecture of one application - "Do not disturb" (similar to the iOS6 feature with the same name ) . It appears a simple one, but turns out to be fairly complex from Architecture stand-point.


The app is a custom alarm-clock, during the time it runs, incoming calls and notifications are silenced, unless you call twice in 3 minutes. An sms saying that the call was silenced is sent to the caller.


The straightforward part is an activity which provide the GUI to set the hour and a button to click "start". Upon clicking either start or stop, an intent is sent to a service which handle it.

The service handle the start command by registering an alarm in the alarm-manager to the designated time. It also becomes foreground-service to add a status-bar notification, an icon, of alarm-clock is set, and it stays active without any cpu usage, but will (almost) guranteed to not get killed.
The service handle the stop command by canceling the alarm in the alarm-manager.
On the service sits a broadcast receiver for  telephony, which will count the number of calls of each number, send the automatic SMS if needed , and un-silence the phone if needed.

To cope with a case of device-restart (user-restart or just software/hardware bug) we add a broadcast receiver for BOOT_COMPLETED and ACTION_EXTERNAL_APPLICATIONS_AVAILABLE, which will re-start the service and test if an alarm is active.