Baccega Andrea Blog

The future is web3.0

Posted on by veke87


At some point every Android developer will need to develop an app that works ( exploiting each device functionalities ) on both Phones and Tablets ( or even Google TV ).

At the very beginning the first question that pops up in the dev mind is ( at least this was the question that popped out in mine ):

Should I make a single app version that works on both android phones and tablets? Or should i leverage the Google Play Store console benefits and upload different version of the same app optimized per each device?

Lets figure it out using a PRO/CONS approach

Single App

PROs (against to the multiple Apps):

  • Less code to maintain — when making a modification to some device agnostic code you don't need to replicate it anywhere
  • Much more scalable

CONs:

  • You'll be required to handle all the logic that routes specific device activities.

Multiple App

PROs (against to the Single App approach):

  • All the routing staff is made upfront from the Google Play store + AndroidManifest filtering.

CONs:

  • More code to maintain  — Fixing a bug on some shared code will require you to make 2+ app updates.

In this post i'll cover how to understand if the used device is a tablet, a phone or a google tv. This is mostly useful for the Single App approach ( but in some cases could be helpful for in the Multiple App ).

The goal (yes again) is to have some piece of code that will help you understand if the device is a phone, tablet or a google tv.

I started googling and I found a couple of valuable resources on stackoverflow which helped a bit. But I didn't find a complete and elegant solutions in none of them so I started my own research.

I needed to find an open source app that fullfilled the above mentioned prerequisites. It was surprisingly simple since Google, each year, releases the source code of their Google IO app. ( which uses the single app approach ).

If you never used the app I can assure you it works on both tablet and phones.

Lets talk no more and get to the real juice: Google IO Sched uses a  class called UIUtils which provides some static methods. Among of them you'll be able to find the following methods:

  • hasHoneycomb: which returns true if the device is "at least" honeycomb apis => ics returns true
  • isTablet(Context): which returns true if the device is recognized as a tablet
  • isHoneycombTablet(Context): which returns true if both isTablet(Context) and isHoneycomb() returns true.
  • isGoogleTV(Context): which returns true if the device is a GoogleTV

Now you'll probably wondering why there are 2 methods in order to recognize if the device is a tablet or not. Well, the answer is simple: There are tablets out there that are pre-honeycomb so you won't be able to use the (out of the stock) Fragment apis available since Honeycomb.

Here the source code of those methods:

Now lets use this goodness to make something real. Lets suppouse you've 2 activities:

  • MapActivity: which is the activity that will be used for phones and eventually google tv
  • MapMultiPaneActivity: which is the activity designed specifically for tablets.
You could use the approach shown in the code below that also exploits the Facade pattern:
Warning: none of the previous code is mine. Everything was taken from the source code of Google IO Sched. BTW, I hope you enjoyed my effort of making this easy to understand/use

Posted on by veke87 | Posted in Seo | Tagged , , , ,


Leave a comment

Posted on by veke87


Input validation could be very painful. Building forms, in every technology, is a boring and painful job.

Fortunately on the web many libraries have born in order to facilitate this task to web developers. In android there are, built-in, some best practices you can use to facilitate the job but you'll always have to take care about:

  • in-depth data validation
  • mandatory fields check
  • error presentation

Bored about giving birth another painful form I decided to write an opensource library that does almost everything for me. The library is called FormEditText and you'll be able to browse/download the sourcecode here in github.

The basic usage of the library will allow the developer to get an edit text validated through xml attributes.

In this snippet of xml code we have a lot of good tech ( both from Android and my library ):

  • line #3:  we define a new namespace. You'll need to modify the package name ( com.andreabaccega.edittextformexample in the code ) with yours
  • line #9: we provide an hint for the user so that he'll know what he should write inside the field
  • line #10: we provide a value for the stock android:inputType attribute.
  • line #12: we tell the library that the field should be a valid email address

The last thing we'll need to do is to ask the library to check the edittext validity:

This piece of code, performed when the user clicks the "submit" button, will ask the library to validate the content. Here two things could happen:

  1. the input is valid: in this case the library will just return true ( obviously )
  2. the input is not valid: in this case the library will return false and will set an error message that will look like the image on the top of the page.

When in the first case the library won't do anything except returing "true" on line #3 of the previous snippet.

Further readings:

I'll suggest to take a look at the github project. If you want to try the library I also setted up an example app that you can download from the market.

android


FormEditText Library Example

0
1,000 - 5,000
Link Android Market

Posted on by veke87 | Posted in Android, Development | Tagged , , ,


1 Comment

Posted on by veke87


Flatterizor is a PHP library developed in order to issue a very specific problem.

Sometimes you have a complex object and you need to store it into your database. Most cases the serialize function does work. Unfortunately sometimes it does not ( not because serialize is broken/bugged ).

The Library is useful only if the followings are true:

  • You want to store your object inside a database
  • Your object is not "Flat"  ( meaning you've at least a value in your object that is an array or an object itself )
  • You want to use your Database SQL to Search & filter objects with a particular value.

The last sentence is actually the most appealing. Lets take, for example, the following objects:

Continue reading

Posted on by veke87 | Posted in Development, Wordpress | Tagged , ,


Leave a comment

Posted on by veke87


You start doing some research about this topic when one of the following events occurs:

  • You lose your data
  • You start hosting third party data

Continue reading

Posted on by veke87 | Posted in Development | Tagged , , , ,


1 Comment

Posted on by veke87


It happens you need to execute some piece of code only on the first time the user start using your app.

Lets say you want to show a quick tutorial to the user just once — Indeed, when the user open your app for the first time.

Well, an easy solution would be to use a SharedPreference to store the info we need to accomplish this "task".

Below, a snippet with a simple method ( to be included inside your activity class ) that will "solve" the problem.

Posted on by veke87 | Posted in Android, Development | Tagged ,


1 Comment

Posted on by veke87


When you build a new Service-Website from scratch you'll probably have to face with this. Passwords are the most sensitive data we have and your application should behave securely enough to maintain user's data private!

Some time ago you it was a common pattern to store the user password as plain text ( or with some Symmetric-key algorithm ). This let the user to retrieve their password withouth setting a new one.

From this pattern a fork was born which required to answer a predefined question in order to make the application send the password back to the user.

It seems these patterns are not used anymore in favor of the actual one which involves a more structured application/user flow in order to let the user regain access to the application. I'm talking about password reset.

The new way

Continue reading

Posted on by veke87 | Posted in Development | Tagged , , ,


Leave a comment

Posted on by veke87


If you're a web developer you confronted by this at least once. If you don't own a development webserver you'll need to make your developments in the production webserver. This means that everyone could see your work-in-progress easily.

A common pattern is to create an index.html file ( which gets served - almost ever - before index.php ) which will contain a specific landing page with an "under construction" text.

How to serve the index.html file to "regular people" while you work on wordpress ( index.php ) ?

It's quite easy using .htaccess and mod_rewrite of apache. The basic concepts are:

  • WordPress logged in people will see the "work in progress" website
  • Not logged in people will see the "site under construction" page

In order to achieve that, you've to prepend the following lines to your .htaccess file  ( placed in your document root ) :

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} index.html$
  RewriteCond %{HTTP_COOKIE} ^.*wordpress_logged_in.*$ [NC]
  RewriteRule . /index.php [L]
</IfModule>

As you can see we only override the normal web-flow only if the browser is going to request the index.html file and the user has the wordpress_logged_in cookie setted.

Whenever you need to see your "real website" you just need to point your browser to http://example.com/wp-admin , login and then you'll be able to see the website you're creating and not the "under construction" page.

Note: This is only a partial solution and people could be smart enough to emulate the wordpress_logged_in cookie and see your work in progress anyway. In my case-scenarios this was more than enough.

 

Posted on by veke87 | Posted in Development | Tagged , ,


Leave a comment

Posted on by veke87


Sooner or later I always remember I've a blog. This time the topic is XSS.

If you don't have a clue of what XSS is maybe you should read this before reading this article.

Continue reading

Posted on by veke87 | Posted in Development | Tagged , , ,


2 Comments

Posted on by veke87


Sooner or later you'll have to deal with it. If you're an html developer and you write also in javascript you'll surely know the onchange event.

Unfortunately it's a little bit tricky to find the same event on android.

The onChange event is helpful when you've to deal with the following things:

  • Let the user know (in realtime) how many characters he typed.
  • Let the user know (in realtime) how many remaining characters he is allowed to type.
  • Make realtime processing of the content ( like sending it online and fetch some partial results of the partial typed edittext )

You've to implement your own instance of TextWatcher and let the edittext know that you want to be notified at each change by calling the method EditText.addTextChangedListener.

Below i will give you a simple example ( it's written on the fly but you'll understand the idea )

			((EditText)findViewById(R.id.et_testo)).addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
				((TextView)findViewById(R.id.numcaratteri)).setText(String.format(getString(R.string.caratteri), s.length()));

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

		});

Posted on by veke87 | Posted in Android, Development | Tagged ,


7 Comments

Posted on by veke87


Since it was a little hard for me finding it, here you can find the trusted CAs in Android 2.2 Froyo.

In order to get my result on each android device you've to download this file and place it on $JAVA_HOME/lib/ext . Plus, you should have $JAVA_HOME/bin in your $PATH

adb pull /system/etc/security/cacerts.bks cacerts.bks
keystore cacerts.bks -storetype BKS -provider org.bouncycastle.jce.provider.BouncyCastleProvider -storepass changeit -list -v >> certificates.txt

After the break my certificates.txt.

Continue reading

Posted on by veke87 | Posted in Android


6 Comments