Integrate Jira with Android

If you want integrate jira in your android proyect you will download a library which is JMC(Jira Mobile Connect).

you donwload here and import project in you workspace.

Then in your android proyect
properties -> android
add JMC library.

I had to edit AndroidManifest.xml file and add the following between the tags:

<service android:name="com.atlassian.jconnect.droid.service.RemoteFeedbackService" />

I had to add some configuration strings for Jira Mobile Connect to be able to know which project and server url to post crash reports and/or feedback to. The server url is the url of the server to where your Jira instance is hosted. The api key is obtained from your install of Jira and the project name is the name of the project. The file that I add this to is /res/values/strings.xml:

<!-- JIRA connect settings -->

<string name="jconnect.droid.config.server_url">http://your-url.com:8080/</string>
<string name="jconnect.droid.config.project">FA</string>
<string name="jconnect.droid.config.api_key">KEY_XXXXXXXXXXX</string>

<string name="crash_notification.title">JMC Sample App Crashed</string>
    <string name="crash_notification.text">Click to send a crash report.</string>
    <string name="crash_notification.dialog_text">The sample app has crashed. Please click okay to send back the data to the developers. (You are awesome btw; just putting it out there.)</string>
    <string name="crash_notification.dialog_request_user_email">Your email address:</string>

You should replace ‘KEY_XXXXXXXXXXX’ with your key and you should replace ‘http://http://your-url.com:8080/’ with your url. You can find your key within Jira at Administration/Projects//Summary/Settings.

Now you create a class that extends application

package com.thedeveloperworldisyours.integratejiraandroid;

import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;
import android.app.Application;
import com.atlassian.jconnect.droid.Api;
@ReportsCrashes(
        formKey = "",
        mode = ReportingInteractionMode.NOTIFICATION,
        resDialogEmailPrompt = R.string.crash_notification_dialog_request_user_email,
        resNotifTickerText = R.string.crash_notification_title,
        resNotifTitle = R.string.crash_notification_title,
        resNotifText = R.string.crash_notification_text,
        resDialogText = R.string.crash_notification_dialog_text)
public class MyApplication extends Application{
	 public void onCreate() {
		 Api.init( this );
	        super.onCreate();
	    }
}

Also you had to edit AndroidManifest.xml and add the Internet permission and the name of application.

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


 android:name="MyApplication" >

If all that you want is crash logging, you can stop here because the call to App.init causes the crash reporting to work.

Next, if you want to send feedback you will create a activity.
Your activity has a button which send feedback.

In your activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TheDeveloperWorldIsYoursActivity" >

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button android:id="@+id/buttonFeedback"
        android:layout_below="@+id/textViewTitle" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/feedback"/>

</RelativeLayout>

Now you Activity.class this code

package com.thedeveloperworldisyours.integratejiraandroid;

import com.atlassian.jconnect.droid.Api;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TheDeveloperWorldIsYoursActivity extends Activity {

	private Button feedbackButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_the_developer_world_is_yours);
		feedbackButton = (Button) findViewById(R.id.buttonFeedback);
		feedbackButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(Api.createFeedbackIntent(TheDeveloperWorldIsYoursActivity.this));
			}
		});
		
	}
}

Know your bugs in Android

In this post you learn to how to know your bugs in your application.

The first step
download library
and put the .jar in libs subfolder of your android project.

The second step
Add the permission of INTERNET in the manifest.xml of your android project.

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.thedeveloperwordisyours.acra"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

The last step

add ACRA.init(this); in your class while extends Application

import org.acra.*;
import org.acra.annotation.*;

@ReportsCrashes(formKey = "", formUri = "http://www.yourselectedbackend.com/reportpath")
public class MyApplication extends Application {
  @Override
  public void onCreate() {
    // The following line triggers the initialization of ACRA
    super.onCreate();
    ACRA.init(this);
  }
}

RoboGuice for dependency injection in Android

RoboGuice is a framework that brings the simplicity and ease of Dependency Injection to Android, using Google’s own Guice library.

The first you download
roboguice-2.0.jar
guice-3.0-no_aop.jar
jsr305-1.3.9.jar

Add the required libraries to the libs folder of you new android project.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TheDeveloperWorldIsYours" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>
package com.thedeveloperworldisyours.inject;

import com.google.inject.Inject;

import android.os.Bundle;
import android.view.Menu;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectResource;
import roboguice.inject.InjectView;
import android.location.LocationManager;
import android.widget.TextView;

public class TheDeveloperWorldIsYours extends RoboActivity {

	@InjectView(R.id.text)
	  TextView name;
	
	@InjectResource(R.string.app_name)
	  String myAppName;
	
	@InjectResource(R.string.action_settings)
	  String myActionSettings;

	@InjectResource(R.string.hello_world)
		String myHelloWorld;
	
	  @Inject
	  LocationManager loc;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_the_developer_world_is_yours);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.the_developer_world_is_yours, menu);
        return true;
    }
    
}

Singleton in Android

A singleton in android it’s the same that in Java. A singleton is a class for which only one instance can be created provides a global point of access this instance. The singleton pattern describe how this can be archived.

Singletons are useful to provide a unique source of data or functionality to other Java Objects. For example you may use a singleton to access your data model from within your application or to define logger which the rest of the application can use.

public class Singleton {
    private static Singleton mInstance = null;
 
    private String mString;
 
    private Singleton(){
        mString = "Hello";
    }
 
    public static Singleton getInstance(){
        if(mInstance == null)
        {
            mInstance = new Singleton();
        }
        return mInstance;
    }
 
    public String getString(){
        return this.mString;
    }
 
    public void setString(String value){
        mString = value;
    }
}

In ActivityA We set the string value.

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class ActivityA extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        //Show the string value defined by the private constructor
        Toast.makeText(getApplicationContext(),Singleton.getInstance().getString(), Toast.LENGTH_LONG).show();
 
        //Change the string value and launch intent to ActivityB
        Singleton.getInstance().setString("Singleton");
        Intent intent = new Intent(getApplicationContext(),ActivityB.class);
        this.startActivity(intent);
    }
}

In ActivityB We get the string.

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class ActivityB extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        //Show the string value of the singleton
        Toast.makeText(getApplicationContext(),Singleton.getInstance().getString(), Toast.LENGTH_SHORT).show();
    }
}

Listener in Android

The observer pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

The object which is being watched is called the subject.The objects which are watching the state changes are calledobservers or listeners.

How to make in android is the same that in java.

The first

1) To Create a class which name is MyListener.
-Interface.
-Variable (listener).
-Void.
-To call the void if (listener!= null).

package com.thedeveloperworldisyours.simplelistener;

public class MyListener {
	public interface Listener {
        public void onStateChange(boolean state);
    }

    private Listener mListener = null;
    public void registerListener (Listener listener) {
        mListener = listener;
    }

    private boolean myBoolean = false;
    public void doYourWork() {
    	myBoolean = true;
        if (mListener != null)
            mListener.onStateChange(myBoolean);
    }
}

The second
2)To use the listener
-Declare the variable
-new Listener
-to use the listener’s void

package com.thedeveloperworldisyours.simplelistener;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class TheDeveloperWorldIsYours extends Activity implements MyListener.Listener{

	private TextView mTextView;
	private MyListener mListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_the_developer_world_is_yours);

		mTextView = (TextView) findViewById(R.id.tvTheDeveloperWorldIsYours);
		mListener = new MyListener();
		mListener.registerListener(this);
		mListener.doYourWork();
	}
	@Override
	public void onStateChange(boolean state) {

		if (state) {
			mTextView.setText("on");
        } else {
        	mTextView.setText("off");
        }

	}

}

Download the example here