DotNetCurry Logo

Creating Android Client for Team Foundation Server 2012

Posted by: Subodh Sohoni , on 12/3/2013, in Category Visual Studio, VSTS & TFS
Views: 13770
Abstract: This article explores a unique scenario for TFS data access using an Android Client and presents an innovative use case for the recently launched TFS OData Services

It’s not common to talk about TFS and Android in the same breath. Both are immensely successful products but without any apparent connection between them. The TFS platform has become ubiquitous for managing software development projects in an Enterprise. At the same time, we have to accept the fact that Android rules the smartphone market and is nudging forward in the tablet world. Given this scenario, we are likely to face the situation where managers and developers may want to use their Android devices to get the status of various artifacts in TFS, as well as to trigger builds in TFS. We assume that developers may not use an android device to write code, but they may want to check whether a new work item like a task has been assigned to them or to fire a build and then check the status of that. Managers may want to have a look at the entire dashboard of the project letting them know the status of work items, builds and even the latest check-ins that may have happened.

This article is published from the DotNetCurry .NET Magazine – A Free High Quality Digital Magazine for .NET professionals published once every two months. Subscribe to this eMagazine for Free and get access to hundreds of free tutorials from experts

It is possible to view browser based TFS Web Access application, if the device in use are tablets with sufficiently large screen . But it may not be always so and they may use android phone or phablets also to interact with TFS. On these devices, the web access may not get properly shown and for such devices, a dedicated application that accesses and shows the TFS data in appropriate format may be a good idea. 

Challenges faced while accessing TFS 2012 from Android Device

The biggest challenge that I foresee is that these android devices do not have capability to use TFS APIs directly. There are two reasons for that. The first is that they use Java for application development. The second is that they do not have large memory and processor capability to handle full-fledged TFS APIs. The first challenge is addressed by Microsoft by publishing a Java SDK for TFS. It is downloadable from http://www.microsoft.com/en-in/download/details.aspx?id=22616. The second challenge is more difficult to address. Since we may not be able to use the rich set of TFS APIs, we have to look for something different which is light weight on the client side but still leverages considerable width of TFS data. That something is now available as TFS OData Services.

OData is platform independent set of REST Services published by Microsoft. TFS OData Services is a layer between the TFS and clients that cannot use full set of TFS APIs. Clients can make a simple http based query to the service and TFS OData Service does the translation of that in TFS APIs and vis-à-vis. This reduces the load on the client without affecting TFS at all. It also means that client may use any technology but as long as it can make the http query, it can communicate with TFS, although indirectly.

interfaces

TFS OData Services can be used in two different environments. First one is if you are either using TFS Service published by Microsoft or if your TFS is available in the Internet. Second is if you want to access TFS that is purely on premise without any interface to Internet.

Prerequisites

If you want to use TFS OData Service for the hosted TFS solution by Microsoft, it is available from https://tfsodata.visualstudio.com/DefaultCollection. You need to only ensure that client can pass in requisite credentials. To create such credentials that work with basic authentication, you should create an alias to your account that will allow you to use Basic Authentication. Process to create such alias is as follows:

- Navigate to the account that you want to use on https://tfs.visualstudio.com. For example, you may have https://account.visualstudio.com.

- In the top-right corner, click on your account name and then select My Profile

  • Select the Credentials tab
  • Click the 'Enable alternate credentials and set password' link
  • Enter a password. It is suggested that you choose a unique password here (not associated with any other accounts)
  • Click Save Changes

To authenticate against the OData service, you need to send your basic auth credentials in the following domain\username and password format:

  • account\username
  • password
  • Note: account is from account.visualstudio.com, username is from the Credentials tab under My Profile, and password is the password that you just created

If you want the TFS OData Service to access TFS that is available only on-premise, then you will need to host the TFS OData Service also on-premise. The source code for TFS OData Service is available for download. You can download it from http://www.microsoft.com/en-us/download/details.aspx?id=36230. After downloading that project, you will need to compile the project and then you can host it in your own IIS server within your corporate network. This service allows us to access any TFS, whether it may be in the Internet or in the private network.

topology

For client side development, you will need the Android SDK that contains Android Development Toolkit. It is downloadable from here http://developer.android.com/sdk/index.html. It contains the Eclipse version that has the necessary plugin installed to do Android development. It also has a device emulator that can emulate any android device with proper specification.

You will also need a package called Odata4j which is a toolkit published by Google that helps Java application call and communicate with OData Services easily. This toolkit is downloadable from https://bintray.com/odata4j/odata4j/odata4j-archive

Building Your TFS App

To begin with, you should start the Eclipse that is bundled with Android Development Toolkit (ADT) that you have downloaded and expanded on your machine. Start an android project. In this project, add the Odata4j JARs as external libraries. Now we are ready to add code to the activity that is provided when the project was created. Name of that activity is MainActivity.

How to create the program

In the first activity, we will collect all the data to connect to the TFS OData Service. For my example, I am going to connect to the TFS OData Service that is published by Microsoft at the url https://tfsodata.visualstudio.com/DefaultCollection. It can accept the Domain that is the account name that I have in the TFS Service. In my case it is SSGSOnline. The user name and password that match the basic authentication alias that I have created are also accepted on this activity.

We are going to connect to TFS and show a list of team projects that I have under my account in the TFS Service. In this activity I have added a ListView to show the list of projects. We define a nested class that extends AsyncTask class. This is a mechanism that is used in Android programming to use multithreading. This class contains a method named doInBackground that gets called when a calling class creates instance of this class and calls the execute method. In the method doInBackground, we can add the code for OData4j objects to access TFS OData Service and indirectly TFS, with my credentials. When the list of projects is available from TFS, we hold it in an ArrayList of strings (called Projects) that is created as a class level variable. Code for doing it is as follows:

@Override
protected ArrayList doInBackground(Void... params) {
//Uncomment following line if you want to debug this code           
//android.os.Debug.waitForDebugger();
try {
    //endpoint is the URL published by TFS OData Service
    // namely https://tfsodata.visualstudio.com/DefaultCollection
    Builder builder = ODataConsumers.newBuilder(endPoint);
    builder.setClientBehaviors(new BasicAuthenticationBehavior(userName,passwd));
    builder.setFormatType(FormatType.JSON); //this is necessary
    ODataConsumer consumer = builder.build();
    List listEntities = consumer.getEntities("Projects").execute().toList();
    if (listEntities.size() > 0) {
        for (OEntity entity : listEntities) {
            Projects.add(entity.getProperty("Name").getValue().toString());       
          }
    }
     } catch (Exception e) {               
     }
   return Projects;           
}

We show these projects in a ListView.

team-project-connect

When a user selects a project, we will call next activity where we will show various iterations under the selected project.

@Override
protected void onPostExecute(ArrayList result) {
super.onPostExecute(result);
    adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1, result);
        list.setAdapter(adapter); //this will show the list of projects
        //following code is an event handler for when a project is selected
list.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView parent, View view, int
position,long id) { 
         String item = ((TextView)view).getText().toString();                   
         Intent intent = new Intent(MainActivity.this,
DisplayIterationsActivity.class);                  
         Bundle bundle = new Bundle();
         bundle.putString("endPoint",endPoint);
         bundle.putString("user", userName);
         bundle.putString("pass", passwd);
         bundle.putString("Project", item);
          intent.putExtras(bundle);
          startActivity(intent);
      }
      });
    }

Filtering Data Using OData Services

TFS OData Service allows us to pass parameters like filter expressions, count, orderby, top/skip, select, format and callback. Syntax for filtering a query is explained in the MSDN article http://msdn.microsoft.com/en-us/library/hh169248(v=nav.70).aspx. Let us use one of them in Android program:

//get a value of iteration that is bundled from earlier activity
iteration = intent.getStringExtra("iteration");
//TFS OData Service returns iteration path with ‘<’ char as separator
//but requires ‘\’ char as separator in filters in queries ?
iteration = iteration.replace("<", "\\");
String StrFilter = "IterationPath eq '" + iteration + "'";
// like eq we can also use lt, gt, ne (not equal) etc in the filter.
List listEntities = consumer.getEntities("WorkItems").filter(StrFilter).execute().toList();

This returns work items in the specified iteration only. We can do computation to get counts and show the data.

iteration-status

For a list of various queries supported by TFS OData Service you can refer to page https://tfsodata.visualstudio.com/Default.aspx . It also provides a few queries that can trigger activities on TFS like queuing a build in addition to getting the data.

Conclusion

Although TFS APIs are meant to be used by rich applications on MS Windows Desktop platform, with the advances in mobile technology and use of devices, it has become necessary to access TFS from mobile apps Eg: Android applications. TFS OData Services creates such an opportunity. It provides provide light weight access to TFS over http/https combined with a powerful mechanism for querying. This should be sufficient to create Android apps that access relevant TFS data and present it innovatively

Download the entire source code from our at GitHub Repository

Was this article worth reading? Share it with fellow developers too. Thanks!
Share on Google+
Further Reading - Articles You May Like!
Author
Subodh is a consultant and corporate trainer. He has overall 28+ years of experience. His specialization is Application Lifecycle Management and Team Foundation Server. He is Microsoft MVP – VS ALM, MCSD – ALM and MCT. He has conducted more than 300 corporate trainings and consulting assignments. He is also a Professional SCRUM Master. He guides teams to become Agile and implement SCRUM. Subodh is authorized by Microsoft to do ALM Assessments on behalf of Microsoft. Follow him on twitter @subodhsohoni


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Michael Bulava on Thursday, May 22, 2014 11:59 AM
Good article..  However I would like you to take a look at the Android TFS Client on the Google Play market.  This is a completely Native Android Client without the requirement of Odata.  Right now it only does Work Items but more features are coming soon.