Friday, January 20, 2012

Publishing Android applications - Part 4

In our previous tutorials, we have walked through on how to setup the android environment and also trying out a sample application.

Here is the detailed documentation on how to publish already developed application:
http://developer.android.com/guide/publishing/publishing.html

Before we move on for publishing the application, there are few things we need to do:
 testing. testing:
Make sure you test your application on emulator thoroughly and then on the real device.
this article lists out some keyboard shortcuts to change the orientation of the device. This will help
test your application in different orientation. Creating virtual devices of different sizes and testing the application
on them will help find any presentation issues with your application.

Performance and SDK compatibility: make sure your application performs as expected and
there are no bottleneck in the speed. Apart from this, try to develop the application for older SDK versions
so that it can run on most of the Android devices. e.g. SDK 2.1 is currently installed on almost 50% of Android devices.
If you develop on later versions then your application will not be compatible and hence will not be installed on these many devices.

 

Getting ready for the publishing...

1. give required permissions to your application

<uses-permission android:name="android.permission.INTERNET"/>

2. choose right icon and name of the application

<application android:label="@string/my_app_name" android:icon="@drawable/my_icon">

3. increment version number in manifest file
...package="com.example" android:versionCode="1"
android:versionName="1.0"...

note that on every update, you need to increment the value of versionCode where as
versionName is your choosen name  of the version.

4. SDK compatibility

make sure you set right values for android:minSdkVersion, android:targetSdkVersion and android:maxSdkVersion attributes.

5. export and sign your application and generate .apk file

go to File > Export

click on "Export Android Application"

now fill in all the options and generate the .apk file. make sure you store the .key file for later application update.

now navigate to http://market.android.com/publish and upload the application.

 

Monday, January 16, 2012

Login to your Google Account on Without Typing Anything

Logging in to your accounts using public computers may be at risk due to virus, key loggers or any other programs which may collect your account credentials and later compromise your account.

Today Google came up with the novel idea “allowing users to login using their trusted phones”.

Here is the solution:

Step 1: open https://accounts.google.com/sesame on the computer which you feel might be at risk.

         opening the above link will show a QR code.

Step 2: make sure you have smart phone with QR code reader application (e.g. Google Goggle, Bakodo, Red Laser etc). Scan the bar code shown on your computer screen (from Step 1).

image

Step 3: you will be shown a URL to open and sign in with your google account (Gmail or iGoogle).

Step 4: within a few moments, Google will sign-in the page and your page on screen will redirected to either Gmail or iGoogle as per your selection in Step 3.

Step 5: now continue using your google account on the computer.

Step 6: After you are done your work on the computer, don’t forget to sign off manually.

 

All is fine so far and loved the creative way to reduce the risk of account take over. This is fantastic and a great way to avoid risk.

However I did not like “Step 6”, why? its because sign-up and sign-in are done differently and from different devices. Since Google has knowledge about both of these devices, so why can’t allow sign off using my device? what if I forgot to sign-off from computer, any way to do the same from my phone device which was used to sign-in? how about when user marks in his device for sign-off and the next action on google account (related to the session with the same QR code) on the computer logs off automatically?

This may be another checkpoint where there needs some improvements.

Google has solved its problem here however the same problem exists for other websites and for whole internet space. The internet needs it badly, to make surfing a better experience.

Hope Google still working on this to make it a general solution which whole industry could benefit.

 

Well done and Good Luck Google!!!

 

-a programmer’s opinion.

Thursday, January 5, 2012

Android: Creating Multi-Column Layout with ArrayAdapter

Android provides ListView for creating list with single column where as there is GridView which is used to create the grid view. However there cases when you would want to create tabular format data view e.g. list view with multiple columns. In order to display a list of data in a multi-column layout spreadsheet style, you could follow some examples on stackoverflow.com. I have gone through most of them and used the simple and easy to use format for my app and thought to share for my viewers.

here are the steps:

Step 1. insert a simple list view which you would turn it to multi column.


file: main.xml

[xml]
<ListView android:id="@+id/mylist" android:layout_width="wrap_content" android:layout_height="wrap_content">
</ListView>
[/xml]

Step 2. create a row format which will be shown for each row in the list. (multi column view).


file: mylistrow.xml

[xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/column1"
android:gravity=”left”
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/column2"
android:gravity=”center”
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView android:id="@+id/column3"
android:gravity=”right”
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
[/xml]

notice the gravity field, which means how you would like to change the orientation of the text. The last two columns have layout weight 1 which means the extra space in the row will be evenly distributed to these columns in order to fill the space. you can read more about weight here.

Step 3. using SimpleAdapter to configure the data binding to these columns


[java]
ListView list = (ListView) findViewById(R.id.mylist);
ArrayList<HashMap<String, String>> mylistData =
new ArrayList<HashMap<String, String>>();

String[] columnTags = new String[] {"col1", "col2", "col3"};

int[] columnIds = new int[] {R.id.column1, R.id.column2, R.id.column3};
for(int i=0; i<3; i++)
{
HashMap<String,String> map = new HashMap<String, String>();
//initialize row data
for(int j=0; j<3; j++)
{
map.put(columnTags[j], "row”+i+”col"+j);
}
mylistData.add(map);
}
SimpleAdapter arrayAdapter =
new SimpleAdapter(this, mylistData, R.layout.mylistrow,
columnTags , columnIds);
list.setAdapter(arrayAdapter);
[/java]

Here is how the list will look like:


















row0col0row0col1row0col2
row1col0row1col1row1col2
row2col0row2col1row2col2


for more details on how to use advanced features of the listview, refer here.

Sunday, January 1, 2012

Wordpress: how to generate DocBook style posts

I'm regular follower of http://www.vogella.de site. What I liked most about the site is its presentation and a way to show users how up to date the articles are. Users like to know how fresh the content is and how frequently they are updated. I would also don't like to read the technical articles which are pretty old and may not be up to how technology is changing.

The site uses "DocBook XSL Stylesheets" tool to generate the contents and manage the revisions. As we all know that most of the blogging sites are based on WordPress. I have done my homework on how easy it is to convert the wordpress posts into revision based article.

sample screenshot:



Here is the code base which you can try out.


Note that all these code changes have to happen in your theme code.

1. functions.php


add below function to functions.php file.

[php]

<?php

function get_post_revision_info($postId) {
global $wpdb;

$sql = "("
. " SELECT ID, post_author, post_name, post_modified, post_parent, post_type"
. " FROM wp_posts"
. " WHERE post_parent =".$postId.""
. " AND post_type = \"revision\""
. " )"
. " UNION"
. " ("
. " SELECT ID, post_author, post_name, post_modified, post_parent, post_type"
. " FROM wp_posts"
. " WHERE id =".$postId.""
. " AND post_type = \"post\""
. " ) ORDER BY `ID` ASC";

$posts=$wpdb->get_results($sql);
$header="";
$lastrevision = "1";
$output="";
$postYear = date('Y'); ;

if ($posts)
{
$cnt = 1;

$output .= "<h3>Revision History</h3>";
$output .="<table>";

foreach ($posts as $post)
{
$lastrevision = $cnt;

$author = get_the_author_meta( 'display_name', $post->post_author);
$datetime = strtotime($post->post_modified);
$modified = date( 'm.d.Y', $datetime);

$rev = "Revision ".$cnt.".0";
$updated = "updated";
if($post->post_type == "post")
{
$updated = "created";
$postYear = date( 'Y', $post->post_modified ); //post created date
}

$output .="<tr>";
$output .="<td>".$rev."<td>"."<td>&nbsp;</td>";
$output .="<td>".$updated."<td>"."<td>&nbsp;</td>";
$output .="<td>".$modified."<td>"."<td>&nbsp;</td>";
$output .="<td>".$author."<td>"."<td>&nbsp;</td>";
$output .="</tr>";

$cnt++;
}
$output .="</table>";
}
else
{
$output .= "no revision history found.<br />";
$output = $sql;
}

$curYear = date('Y'); ;
$copyright = "Copyright &copy; ".$postYear . (($postYear != $curYear) ? '-' . $curYear : ''). " ".get_the_author();

$header .= "Author ".get_the_author()."<br>";
$header .= "Current Version ".$lastrevision.".0"."<br>"; //1.0, 2.0...
$header .= $copyright."<br>";
$header .= date("m.d.Y")."<br>";

$footer = "<h3>Excerpts:</h3>";
$footer .= "<i>".get_the_excerpt()."</i><br/><br/>";
$footer .= "<h2>Article Details</h2>";

return "<h2>".$header."</h2><br/>".$output."<br/><br/>".$footer."<br/>";
}

[/php]

2. single.php


add below code (a call to post_revision_info) in single.php

[php]

<div class="entry-content">
<?php print get_post_revision_info(get_the_ID()); ?>

[/php]

This is my first version of the code. it may not generate the well-formed HTML code or may not suit to your theme.
so please change the code according to your need.

your comments will encourage me to come up with more articles on the related topic.

Thursday, December 29, 2011

Bonus on How To: Building Android Application - Part 3


After going through the Part 1 and Part 2 of “How To: Building Android Application”, we will go explore more on the android sample and some tips to improve the performance of the emulator.

Playing with Android Sample Application


In this tutorial, we will setup the Android sample application which will help you play with most of the android components.

Step1: click on File->New->Other and then select Android->Android Sample Project

click Next and Finish.

in case you see “ This target has no samples. Please select another target.” message, you should click Windows->Android SDK Manager and select the sample and other versions (if needed) and Install the packages. This step will take a while. After that is done, retry Step 1.

useful emulator Shortcuts:


Alt+Enter maximizes the emulator. Nice for demos.

Ctrl+F11 changes the orientation of the emulator.

F8 turns network on / off.

F6 to turn on / off the trackball

Performance Considerations


Try to use a smaller resolution for your emulator as for example HVGA. The emulator gets slower the more pixels its needs to render as it is using software rendering.
While creating device (e.g. myDevice), set the flag "Enabled" for Snapshots. This will save the state of the emulator and let it start much faster.

Sunday, December 18, 2011

Transactional Memory in GCC

Transactional memory is intended to make programming with threads simpler, in particular synchronizing access to data shared between several threads using transactions. As with databases, a transaction is a unit of work that either completes in its entirety or has no effect at all (i.e., transactions execute atomically). Further, transactions are isolated from each other such that each transaction sees a consistent view of memory.

 

Two operations conflict if one of them modifies a memory location and the other one
accesses or modifies the same memory location. The execution of a program contains a data race if it
contains two conflicting operations in different threads, at least one of which is not an atomic operation,
and neither happens before the other. Any such data race results in undefined behavior. A program is race free if none of its executions contain a data race. In a race-free program each read from a memory location
sees the value written by the last write ordered before it by the “happens-before” relationship.

 

Outermost transactions (that is, transactions that are not dynamically nested within other
transactions) appear to execute sequentially in some total global order that contributes to the
“synchronizes with” relationship. Conceptually, every outermost transaction is associated with
StartTransaction and EndTransaction operations, which mark the beginning and end of the
transaction.2 A StartTransaction operation is sequenced before all other operations of its
transaction. All operations of a transaction are sequenced before its EndTransaction operation.

 

A shared memory access can form a data race even if it is performed in a transaction
statement. In the following example, a write by thread T2 forms a data race with both read and
write to x by Thread T1 because it is not ordered with the operations of Thread T1 by the
“happens-before” relationship. To avoid a data race in this example, a programmer should
enclose the write to x in Thread T2 in a transaction statement.

Thread T1

__transaction {
t = x;
x = t+1;
}

Thread T2

x = 1;

Atomic transactions:
A transaction statement without an attribute or annotated with the atomic attribute defines an
atomic transaction. We call such a statement an atomic transaction statement:

__transaction compound-statement
__transaction [[ atomic ]]compound-statement

Safety attributes on functions and function pointers:

To ensure that atomic transactions can be executed atomically, certain statements must not be
executed within atomic transactions; we call such statements unsafe.

In order to specify whether a function is safe, the function declaration may specify transaction_safe or transaction_unsafe attribute.

Memory model;
The memory model rules for transactions (Section 2.1) are sufficient to guarantee that in race
free programs, atomic transactions appear to execute as single indivisible operations. Atomic
transactions cannot contain other forms of synchronization (such as operations on locks or C++0x
atomic operations).

In the following example, both a and b will be read and the difference will be written to c, all atomically and isolated from other transactions:

__transaction_atomic { c = a - b; }
Therefore, another thread can use the following code to concurrently update b without ever causing c to hold a negative value (and without having to use other synchronization constructs such as locks or C++11 atomics): __transaction_atomic { if (a > b) b++; }

 

refer: C++-transactional-constructs-1.0.pdf

 

Saturday, December 17, 2011

How To: Building Android Application with easy steps–Part 2

This is in the continuation of my previous post “How To: Building Android Application with easy steps–Part 1”. In this tutorial, we will go over creating the sample application using the environment we have already setup.

Here is the details of the application we will build:
Number Counting: providing the buttons to increment/decrement the number present in the text box

 

Step 1: create Android project


Select File → New → Other → Android → Android Project and create the Android project named

com.hashfold.android.numbercounting

image

click Next and select the Android SDK version on which your application will be build and compiled.

image

note: selecting the Android 2.1 is the best options as 90% of Android devices till date are based on this version.

Click next and make sure the information filled is matching with below:

image

and then click Finish

note: numbercountingActivity is the name of the activity which will get started when the application starts

here is the final project view after above steps:

image

 

image

 

Step 2: writing application code


so far we have completed creating the project space. Now its time to do some coding and creating the GUI components.

2.1 AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hashfold.android.numbercounting"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name="com.hashfold.android.numbercountingActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

2.2 strings.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="details">number counting application</string>
<string name="hello">Hello! number counting!</string>
<string name="app_name">NumberCounting</string>
<color name="bgcolor">#000000</color>
</resources>

2.3 main.xml layout – the GUI component


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/bgcolor">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="+" android:id="@+id/Plus"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:id="@+id/Minus" android:text="-"></Button>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:id="@+id/Value" android:text="value"></TextView>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:text="Ok" android:id="@+id/Ok"></Button>
</LinearLayout>

</LinearLayout>

2.4 numbercountingActivity.java


package com.hashfold.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.hashfold.android.numbercounting.R;

public class numbercountingActivity extends Activity implements OnClickListener
{
Button Plus, Minus, Ok;
TextView Value;
int score = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Plus = (Button) findViewById(R.id.Plus);
Minus = (Button) findViewById(R.id.Minus);
Ok = (Button) findViewById(R.id.Ok);
Value = (TextView) findViewById(R.id.Value);

Plus.setOnClickListener(this);
Minus.setOnClickListener(this);
Ok.setOnClickListener(this);

}

public void onClick(View v)
{
boolean showText = false;

switch(v.getId())
{
case R.id.Plus: score++; showText = true; break;
case R.id.Minus: score--; showText = true; break;
case R.id.Ok: showText = true; break;
}
if(showText)
Value.setText(String.valueOf(score));
}
}

The R.java is the resource file which is auto generated based on strings.xml and other related resource changes.

Step 3: running the application


setup the run configuration: click on Run->Run configurations

image

and then click on ‘New launch configuration’ and then fill in the information needed as below:

image

image

now click on Apply button and then Run button to run the application.

It will take a while in starting up the Android device emulator with the device select in above step “myDevice”.

Once the device is started, it will look like below:

image

now click on the plus “+” button and see how the application performs

image

If you wish you could start device in full screen mode by pressing Alt+Enter.

image

This helps in having full view of the application.

Having a different orientation view (click Ctrl+F11)

image

turn off the internet (click F8)

image

toggle the internet (click F8)

image