Monday, April 18, 2016

Testing Location based Apps on Android Emulator

Things have changed for the good with Emulator 2.0. I think this is the first time I might end up spending more development time on the emulator than on the device.

In this post, I'll be explaining two approaches of how to effectively test location-based apps using the emulator:

1. Testing a single location for the app

From the Extended Controls > Location you can send specific Latitude, Longitude & Altitude to the device. Here's a GIF which shows how we can send Latitude and Longitude and that gets detected by the application

2. Testing multiple locations using KML

For the demo I'm using a KML file from here: https://developers.google.com/kml/documentation/kml_tut#paths which specifies multiple locations in the Placemark section (To learn more about KML, here's a tutorial: https://developers.google.com/kml/documentation/kml_tut)
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Paths</name>
    <description>Examples of paths. Note that the tessellate tag is by default
      set to 0. If you want to create tessellated lines, they must be authored
      (or edited) directly in KML.</description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    <Placemark>
      <name>Absolute Extruded</name>
      <description>Transparent green wall with yellow outlines</description>
      <styleUrl>#yellowLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates> -112.2550785337791,36.07954952145647,2357
          -112.2549277039738,36.08117083492122,2357
          -112.2552505069063,36.08260761307279,2357
          -112.2564540158376,36.08395660588506,2357
          -112.2580238976449,36.08511401044813,2357
          -112.2595218489022,36.08584355239394,2357
          -112.2608216347552,36.08612634548589,2357 
          -112.2633204928495,36.08621519860091,2357
          -112.2644963846444,36.08627897945274,2357
          -112.2656969554589,36.08649599090644,2357 
        </coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>

Steps

  • Select and Load KML file: From the Extended Controls > Location click on Load GPX/XML 
  • The above step would load locations from KML file in the table 
  • Select the speed at which you want to change the location, for example, Speed 2x would change location every 2 seconds
Here's the pictorial representation of the above steps :

Here's a GIF image showing how it iterates through locations in the table and updates the Latitude & Longitude on the emulator

Wednesday, February 10, 2016

Embedded Fragment Lifecycle

The whole intention of this post is to go through the lifecycle transitions between an activity and an embedded fragment. A fragment can be added to an activity by using the fragment tag and is referred by a class (EmbeddedFragment in example below)

<fragment    
    android:id="@+id/EmbeddedFragment"
    android:name="com.android.example.EmbeddedFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</fragment>






Sunday, January 10, 2016

A fair game.

I've been a carrom fanatic since I was a kid. It runs in the family. The standard carrom game is a 4-player game with 2 teams and the team which scores 29 points first, wins the game.



Last week at a friend's place 4 of us were playing carrom and one of us had to leave early. To not let go off the board, we wished if there could be a standard carrom strategy with 3 players. The discussion and thoughts led to a fair game strategy. I thought of sharing it here, in case just like us, you don't find a 4th player. 

2/4-player game

It's pretty well documented on Wikipedia and wikiHow 

3-player version

This is a modified version of 2/4 player. 
  • Each player has to pocket 6 coins - 3 black and 3 white (18 coins distributed equally among 3 players) 
  • Pocketing the queen must be followed by pocketing another coin in the same strike
  • Point system is a bit different from the standard game
    • The game is over when one of the player reaches 29 points 
    • A player with least points wins the game 
    • When a board is over, the winner gets 0 points, the other 2 players loose the points 
    • If winner pockets the queen, the other player looses 5 points 
    • Here's the points tally based on certain chain of events to understand the point system 
    •    Event  Player A    Player B    Player C  
       Player A won the board and pocketed queen as well
       Player B pocketed 2 coins
       Player C pocketed 4 coins 
      0
      9
      7
       Player A won the board and didn't pocket queen
       Player B pocketed 2 coins
       Player C pocketed 4 coins
      0
      4
      2
  • Foul system
    • If a player takes another black/white after pocketing 3 black/3 white coins, it's considered a foul. The pocketed coin would come back on the board and also the player needs to put an extra coin from his already pocketed coins on the board
Let me know how you feel about it in your comments below. 

Saturday, November 14, 2015

Installing python newspaper on El Capitan

I've beeing struggling to install newspaper library on El Capitan. Finally found a command with which it would install without any issues.

sudo STATIC_DEPS=true pip install newspaper --ignore-installed six

Errors which I was getting, for lxml:

  cc -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -I/usr/include/libxml2 -I/private/tmp/pip-build-ckZpLW/lxml/src/lxml/includes -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/lxml/lxml.etree.c -o build/temp.macosx-10.11-intel-2.7/src/lxml/lxml.etree.o -w -flat_namespace
    In file included from src/lxml/lxml.etree.c:346:
    /private/tmp/pip-build-ckZpLW/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found
    #include "libxml/xmlversion.h"
             ^
    1 error generated.
    error: command 'cc' failed with exit status 1
    
    ----------------------------------------
Command "/usr/bin/python -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-ckZpLW/lxml/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-K1rk1V-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-build-ckZpLW/lxml

and for six module:

  Found existing installation: six 1.4.1
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling six-1.4.1:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 211, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 311, in run
    root=options.root_path,
  File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 640, in install
    requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 716, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip/req/req_uninstall.py", line 125, in remove
    renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip/utils/__init__.py", line 315, in renames
    shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
    copystat(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
    os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-8Vwuj2-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

Saturday, October 3, 2015

Content without the Webview

I've always liked the Reader View in Safari browser (and now I believe evry other browser has it)

For those who don't know what Reader View is, it removes all the clutter from a web page and shows only the text and few relevant images.

Before applying the Reader View
After applying the Reader View





















I've been working on an Android application and wanted to implement something like Reader View for the News section in the application. Webview didn't seem to be the perfect solution.

I thought what if I could extract the text from html on the server side and just send the text to the client and then show the text in whatever format I want to show it. This approach also saves the bandwidth for the user, as less data would be downloaded.

I came across this Python library, newspaper, which did exactly which I expected it to do. With just couple of lines of code I could extract the text from the html page. Sample code:

from newspaper import Article

article = Article(url)
article.download()
article.parse()

news['text'] = article.text.encode("utf-8")

I'm using a firebase based server and the extracted text above could be saved directly at the firebase URL. The client simply connects to it and shows the news.

This is how the news looks on the client at the moment. It's implemented using the plain old TextView.


Of course, a lot has to improve still. Time to explore TextView Spannable! 

Links

  • Newspaper Python library: Link
  • Iphone's Reader View: Link