How to make in page margin animations smooth for ViewPager pages

tl;dr modify the setOffScreenLimit dynamically.

In order to keep a constant length ViewPager scrolls smooth, setOffScreenLimit(page.length) will keep all the views in memory. However, this poses a problem for any animations that involves calling View.requestLayout function (e.g. any animation that involves making changes to the margin or bounds). It makes them really slow (as per Romain Guy) because the all of the views that's in memory will be invalidated as well. So I tried a few different ways to make things smooth but overriding requestLayout and other invalidate methods will cause many other problems.

A good compromise is to dynamically modify the off screen limit so that most of the scrolls between pages will be very smooth while making sure that all of the in page animations smooth by removing the views when the user. This works really well when you only have 1 or 2 views that will have to make other views off memory.

@Override
public void onPageScrollStateChanged(int state) {
if(state==ViewPager.SCROLL_STATE_IDLE){
if(mViewPager.getCurrentItem()==INDEX_OF_ANIMATED_VIEW){
mViewPager.setOffscreenPageLimit(1);
}else{
mViewPager.setOffscreenPageLimit(OLD_PAGE_LENGTH);
}
}
}

SleepBot Synchronization

Since we started planning for a web platform for SleepBot, the biggest problem I had was how to efficiently synchronize the data people already have on their phone with the central database. 

The simple part: When a user sign in on their phone, all the existing data on the phone are transmitted to the server. The server would first get all the data the user has that already on the server, match the ones that needs to be updated (base on their last modified time) and insert the ones that are missing. The ones that are missing or updated on the device will be sent back. The device would then verify the data, and if there are no error, mark the last updated time.

Now there are a few different things that can happen:

  1. User punched in on the website. 
  2. User edited/updated sleep data on the website.
  3. User punched in on the phone.
  4. User edited/updated sleep data on the website.

For Android 2.2 or above devices that have C2DM enabled, we can use push notifications:

  1. For case 1 and 2, SleepBot will queue all the updates on the server and send updates to the devices via push notifications. 
  2. For case 1, I call it a state change, in which the user has to see immediately on their phone, so when a device receive a notification on case 1, it will try to synchronize immediately, if possible. If any error happens, the device will mark itself as "needs to be updated". When the user opens SleepBot, the device will try to contact with the server again for updates. In these exchanges, the last update time will be used to only exchange the modified entries.
  3. For case 2, SleepBot will only mark the device as "needs to be updated", this way in case there are more updates, only one update will happen until user open SleepBot again on their phones. 

For Androids that does not support push notification (Kindle Fire, Android 2.1 or lower) and iOS, SleepBot will try to synchronize every time it is opened.

Regardless of the device, for case 3, SleepBot will synchronize immediately.

Regardless of the device, for case 4, SleepBot will synchronize upon close.

Note that on the server side, whenever a device try to synchronize, it will propagate the changes to all the devices that are connected to the account except the device that requested the synchronization.

At a glance, this is what I came up with. It should cover all the cases.....not sure if this is the most efficient way of handling it though. 

This is why Flash should not get on mobile too soon

Today I was watching Saturday Night Live on Hulu.com. And the laptop I was using got a blue screen due to overheating. So I monitored the temperature:

[caption id="attachment_146" align="alignnone" width="408" caption="GPU: 85 degrees, Both cores of my CPU > 75 on a T9400+Nvidia 9600M"]GPU: 85 degrees, Both cores of my CPU > 75[/caption]

Normally, this laptop operates with GPU around 60 degrees and CPUs around 50 degrees. When playing low-res videos and local media files. Flash is simply inefficient. I can't imagine the amount of batteries that it will be draining with current algorithms. It will simply kill the battery life.

Update: The CPU and GPU temperature both reached 90 something degrees, and the laptop rebooted shortly after that.