Monday, September 27, 2010

HttpConnection Time out

Here's the code

HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
httppost.setEntity(se);

HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);

HttpEntity entity = httpResponse.getEntity();
return entity;

Thursday, September 23, 2010

Generic Xml Parsing in Android

Here's the code

public void genericXmlParsing(String raw) throws Exception{
DocumentBuilder builder = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(raw)));
NodeList elementNames = doc.getElementsByTagName("ElementName");
NodeList elementValues = doc.getElementsByTagName("ElementValue");
for(int i=0;i Element elementName = (Element)elementNames.item(i);
ParsedXmlData parsedXmlData = new ParsedXmlData();
parsedXmlData.setElementName(elementName.getFirstChild().getNodeValue());
parsedXmlDataList.add(parsedXmlData);
}
for(int i=0;i Element elementValue = (Element)elementValues.item(i);
ParsedXmlData parsedXmlData = parsedXmlDataList.get(i);
parsedXmlData.setElementValue(elementValue.getFirstChild().getNodeValue());
}
}

Wednesday, September 22, 2010

XML Layout for Android

Ques:

Is there a way to display a View, such as a TextView sideways using the XML layout file? I know you can rotate the view using code in an activity, but is there a way just to do it with layout?


here's the answer

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_weight="5"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:text="sideways dummy text sideways dummy text sideways dummy text sideways dummy text sideways dummy text sideways dummy text sideways dummy text"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:text="Text Here"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">



Sunday, September 19, 2010

GIF Animation In Android

The gif animation is supported in Android when GIF animation is played as Movie. Movie is a class provided to you by Android SDK.

In your main or any Activity File

public class AA extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceStated);


setContentView(new MYGIFView());
}

}


Here's the class


private class MYGIFView extends View{

Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
public GIFView(Context context) {
super(context);


Provide your own gif animation file

is=context.getResources().openRawResource(R.drawable.earth);
movie=Movie.decodeStream(is);

}

@Override
protected void onDraw(Canvas canvas) {

canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;

}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
this.invalidate();
}
}

Sunday, August 15, 2010

Best Practices for Handling Android User Data

As the use of mobile applications grows, people are paying more attention to how these applications use their data. While the Android platform contains extensive permissions designed to protect users, application developers are ultimately responsible for how they handle users’ information. It’s important for developers to understand the code they include, and consider the permissions they request, as mishandling these issues can result in users perceiving a violation of trust.

Maintaining a healthy and trustworthy ecosystem is in every Android developer’s best interest.

Here are a few tips for writing trustworthy Android applications:

1.

Maintain a privacy policy
2.

Minimize permissions
3.

Give your users a choice regarding data collection
4.

Don’t collect unnecessary information
5.

Don’t send data off the device
6.

... but if you have to, use encryption and data minimization
7.

Don’t use code you don’t understand
8.

Don’t log device or user specific information.

Maintain a privacy policy

Trustworthy applications are up-front about the data they collect and the reasons for collecting it. Users are generally happy to share information via such apps if they believe they will personally benefit. A clear and concise privacy policy, with details about the type of information collected and how it’s used, goes a long way towards generating trust and good will.
Minimize permissions

Android is unique among mobile operating systems for its simple, straightforward, operating-system-enforced permission model. All Android applications must declare the permissions they require, and users must approve these permissions before the application is installed. Users tend to distrust applications that require excessive permissions.

For example, a user installing this tic-tac-toe game might reasonably wonder why it needs to take pictures.
Give your users a choice regarding data collection

It’s called the paradox of privacy [PDF, 890K]. Users are often happy to share their information, but they want control over that sharing. Trustworthy applications give users control over their information. For example, the Android Browser has privacy settings which enable users to control how their information is shared.
Don’t collect unnecessary information

Trustworthy applications limit the kinds of data they collect. Collecting unnecessary information, especially if you never use it, just invites suspicion. When in doubt, don’t collect it.
Don’t send data off the device

If you have to handle user data, ensure that the data remains on the device whenever possible. Users are comforted knowing that their private information strictly resides in the phone. Sending data outside the phone, even if done for the user’s benefit, tends to draw suspicion.
... but if you have to, use encryption and data minimization

Sometimes, the collection of data is necessary. In that case, applications need to ensure that it is handled safely. A privacy policy will avoid leading to surprised and irritated users; in some cases, it may be advisable to prompt the user before transmitting data off-device.

First, minimize the amount of data you collect. Do you really need the user’s full phone number, or would the area code be sufficient? Can you use a one-way cryptographic hash function on the data before sending it to the server to help protect the user’s confidential information?

A case study: User Favorites

Suppose you want your app to maintain a list of “favorites” for each of your users, without going through a full registration process. In theory, you could do this by sending your server some combination of their phone number, device ID, or SIM ID. But why take the chance of worrying people about privacy issues; why not send a one-way hashed signature of whatever the identifying information is? Or even better, create a random unique id and store it on the phone, and use this unique id as the registration key for your application.

In the end, you’ll will still be able to retrieve their favorites, but you won’t need to send or store anything sensitive.

Second, encryption is critical to the safe handling of user data. Phones often operate on untrusted networks where attackers can sniff confidential traffic. Encrypting data in transit is a critical part of protecting user information.

Finally, when communicating with a server over HTTP, it’s a good idea to avoid encoding user information in a URL that is used with HTTP GET; rather, POST it in a message body. While using POST doesn’t guarantee that your information won’t be sniffed, putting it in the URL increases the likelihood that it will be automatically logged; out of the box, most web server software logs all the URLs that are received.
Don’t use code you don’t understand

In the open-source Android environment, it’s common (and good) practice to rely heavily on other people’s code, in the form of libraries and frameworks. But if that code is handling your users’ information inappropriately, it’s your problem. So make a point of checking code before you rely on it.
Don’t log user or device specific information

Application developers should be careful about on-device logs. Android makes it easy to write to the phone’s log, and anyone who has looked at “logcat” output knows that it is full of important but seemingly random debugging information from many applications. In Android, logs are a shared resource, and are available to an application with the READ_LOGS permission (only with user consent, of course!). Even though the phone log data is temporary and erased on reboot, inappropriate logging of user information could inadvertently leak user data to other applications.

Friday, July 30, 2010

Android Market on Android emultor

Finally, I am able to run the Android Market on the emulator. And also, see all the paid apps and copy-protected app, right here on my emulator.

What do you need?

Well, the basic SDK. 1.5, 1.6 or 1.1

And then, go to the HTC website where you can find the images/recovery images. Download the version (system image only) which you want to run.

http://developer.htc.com/google-io-device.html#s3
(Download the System Image zip)

Extract the files of this zip. There's a system.img file which you will need in the next steps.

Create an AVD (1.1, 1.5 or 1.6) depending on your requirements.

Copy this system.img file into the avd directory. For example, if you created an avd named "MyPhone", go to .avd\MyPhone\ and paste this system.img file here.

Now start the emulator. Voila, You are ready to go. After you sign in with a google account, your phone is ready to use. You now have access to all the market apps right from your emulator.

Note: If you are not able to run it successfully, and if you are getting Network communication error, please download the AVD that I have created from this link.

http://developer.htc.com/adp.html