Monday, 21 April 2014

Change the language settings of Jenkins

By default, Jenkins uses the default language of your browser, if it is set. To change it, you could do the follow.

1. Download and install the locale plugin
1.1 From the main page of Jenkins, go to Manage Jenkins -> Manage Plugins
1.2 Click on Availables
1.3 Check the "Locale plugin" and clicks on "Download now and install after restart".
Jenkins will download the plugin and restart if not job has been scheduled

2. Set the language
Once the plugin has been installed, change the language using following steps
2.1 From the main page of Jenkins, go to Manage Jenkins -> Configure System
2.2 Under Locale, there will be a field called "Default Language". Enter the new language. It could be "en" or "ENGLISH".
2.3 Under the text box, check the checkBox called "Ignore browser preference and force this language to all users".

Wednesday, 16 April 2014

Install Jenkins on Ubuntu

Jenkins is a continuos integration environment which running on a server, it could connect to a repository such as Git or SVN to retrieve the last version of code and compile it following some general rules.

It is recommended for big projects to ensure that for any new change, the project will be compilable.

To install it on Ubuntu, just follow these steps:
1. Open the console or the terminal
2. Type the follow command to add the key

wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -

3. Type the follow command to add the repository of Jenkins into the list of repositories of your computer

sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list

It might ask you for your credentials. If so, type it and continue.

4. Type the follow command to update the list of resources

sudo apt-get update

5. Type the follow command to install jenkins

sudo apt-get install jenkins

Source and more information:

Friday, 4 April 2014

Setting multiusers for GenyMotion

GenyMotion is a tool for Android which runs the emulators on the Virtual Box of Oracle. It has plugins for eclipse, which allow the user to download and start a emulator faster than the native ones.

Since Android 4.2, the tablet version of Android has multiple users functions. This allows different owners of the same tablet have different spaces, background, accounts, etc. It is something like the multiple users function that Windows, Linux and Mac OS has.

By default, the multiple users function for the emulators of GenyMotion has been disabled. But it could be enabled with the follow commands:

1. Connect to the emulator
1.1. Starts the emulator of GenyMotion. It must has a version older than 4.2 and it must be a tablet (i.e. Nexus 10)
1.2 Open a terminal
1.3 Type the follow command in your terminal to connect to the emulator

adb -e shell

2. Check the maximum support number of users allowed
2.1 After the connection has been established, type the follow:

pm get-max-users
You will get something like:

Maximum supported users: 1

This is the default number of maximum support users.

3. Change the maximum support number of users allowed
3.1 On the terminal type the follow command to remount the disk:

mount -o rw,remount /system

3.2 Change the value in the system file typing the follow command in the terminal:

echo fw.max_users=4>> /system/build.prop

4. Restart the emulator
The easiest way to restart the emulator is use the mouse and do long click on the turn off button on the right bottom corner and select power off option.

After it, close the emulator and start it again from the plugin of Eclipse.

5. Check it
Now if you go to the setting, you will see a option called "Users" below the "Apps" option.


Source:

Friday, 3 January 2014

My singleton for Android

From Wikipedia, there are many way to creates an singleton in Java. The simplest and clearest version is the follow one:

public class SingletonDemo {
    private static SingletonDemo instance = null;

    private SingletonDemo() { }

    public static synchronized SingletonDemo getInstance() {
        if (instance == null) {
            instance = new SingletonDemo ();
}
return instance;
    }
}

But in most of the cases, it is not suitable for Android because the constructor requires the context. So, this is the singleton that I use:

public class SingletonDemo {
    private static SingletonDemo instance = null;
    private static Context context;

    /**
     * To initialize the class. It must be called before call the method getInstance()
     * @param ctx The Context used

     */
    public static void initialize(Context ctx) {
    context = ctx;
    }

    /**
     * Check if the class has been initialized
     * @return true  if the class has been initialized
     *         false Otherwise
     */
    public static boolean hasBeenInitialized() {
    return context != null;

    }

    /**
    * The private constructor. Here you can use the context to initialize your variables.
    */
    private SingletonDemo() {
        // Use context to initialize the variables.
    }

    /**
    * The main method used to get the instance
    */
    public static synchronized SingletonDemo getInstance() {
    if (context == null) {
    throw new IllegalArgumentException("Impossible to get the instance. This class must be initialized before");
    }

    if (instance == null) {
    instance = new SingletonDemo();
    }

    return instance;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException("Clone is not allowed.");
    }


}

Note that the method initialize could be called in the main class and the method getInstance could be called from other classes. This will fix the problem when the caller class requires the singleton but it does not have the context.

Finally the method hasBeenInitialized is uses to check if the class has been initialized. This will avoid that different instances have different contexts.

Tuesday, 31 December 2013

AutoCompleteTextView - A simple hack to work with complex data models

AutoCompleteTextView was added from the API Level 1 and it is very useful to show a list of hints while the user is typing.

Checking the source code, I found an easy way to hack the view to show the hints for the complex data models. Here is an example:

1. Model:
The model contains 3 fileds: id, name and complete address. The hack overrides the method toString to return the name.

/**
 * Hack. Do not modify. To String Must return the name of the cinema, which is the one returned
 * by the AutoCompleteTextView.
 */
@Override
public String toString() {
    return getCinemaName();
}

2. Main activity
For the main activity, a simple ArrayAdapter is used:
ArrayAdapter<Cinema> adapter = new ArrayAdapter<Cinema>(this,
                android.R.layout.simple_dropdown_item_1line, cinemas);
simpleAutoCompleteTextView.setAdapter(adapter);

This is the result:

Explication
Checking the source code of AutoCompleteTextView you can see that it uses the a list adapter to show the list of hints. By using the ArrayAdapter, which in the method getView it uses the method toString to set the text shown:

T item = getItem(position);
 if (item instanceof CharSequence) {
     text.setText((CharSequence)item);
 } else {
     text.setText(item.toString());
 }

So, by overriding the method toString to output the string to be shown as hint, it could work.

Source Code
GitHub - AutoCompleteTextViewHack

More information
Android developers - AutoCompleteTextView

Extra
Both AutoCompleteTextView and android.R.layout.simple_dropdown_item_1line has problem with old devices. I have tested it on a Samsung Galaxy Ace (GT-S5830) with 2.3.5 and both views shows a white background and white text colour, making the text impossible to see.

For the AutoCompleteTextView, the fix is easy:
<AutoCompleteTextView
        android:id="@+id/simpleAutoCompleteTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:background="@android:color/white"
        >
        <requestFocus />
</AutoCompleteTextView>

And for the dropdown item, the source code could be found in the internet, which the app could have a local version with modification of colours:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    style="?android:attr/dropDownItemStyle"
    android:textAppearance="?android:attr/textAppearanceLargeInverse"
    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="@android:color/black"
    android:background="@android:color/white"
/>

Monday, 30 December 2013

Customize the home up button in the Action Bar (Sherlock)

Like any other elements of Sherlock Action Bar, the home up button, which is the small arrow on the left side of the home(logo) button, could be customized.

Here is a short guide for it:
1. Override the actual style for the pre-v11 devices.
1.1 Copy and paste the follow code in the file styles.xml of the default values folder.
<resources>
    <style name="MyCustomTheme" parent="Theme.Sherlock.Light">
    <item name="homeAsUpIndicator">@drawable/ic_home_up</item>
    </style>
</resources>

Note that the parent could be changed to any Sherlock theme.

2. Override the actual style for the v11+ devices.
2.1 On the same folder where the folder values is, create a new folder called values-v11. Android will automatically look for the content of this folder for devices with API or above.
2.2 Create a new file called styles.xml
2.3 Paste the follow code into the file:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyCustomTheme" parent="Theme.Sherlock.Light">
    <item name="android:homeAsUpIndicator">@drawable/ic_home_up</item>
    </style>
</resources>

Note tha the name of the style must be the same as the file in the default values folder and instead of the item homeAsUpIndicator, it is called android:homeAsUpIndicator.

The item issue is because for devices with API 11 or above, Sherlock Action Bar use the default Action Bar which comes with Android, which the key name is android:homeAsUpIndicator. But for the devices with API 10 or lower, Sherlock Action Bar uses its own ActionBar, which the home as up indicator is called simple "homeAsUpIndicator".

3. Use the new theme in the manifest
3.1 Replace the theme for the application/activity in the AndroidManifest file:
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyCustomTheme" >


Source Code:

Source:

Others

Enable the Indeterminate progress bar in the Sherlock Action Bar

The Sherlock action bar comes itself with an indeterminate progress bar. To activate it, a new flag must be enabled, which must be before the content has been set:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Request the progress bar
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

    setContentView(R.layout.activity_main);
    .....
     }

Once this flag is enabled, to enable the progress bar just do the follow:

setProgressBarIndeterminateVisibility(true);

And to disable it:
setProgressBarIndeterminateVisibility(false);



Be aware there when the flag is set, there are two imports possible. The one which is native is the good one:

import android.view.Window;

Source:

Others: