This tutorial has the following important pre-requisites:
- You have obtained the SDK binaries.
- You have set up Eclipse to build and run Android applications.
- You have set up a LogCat tab within the Eclipse IDE. Details on Eclipse setup can be found on the Eclipse website.
This tutorial takes you through the process of creating an application that uses the SDK binary package. In the approach used in this tutorial, the JAR files that make up the SDK binary package are simply dropped into the libs folder of your existing Android project.
Create an Android project
- From the Eclipse main menu select .
- Enter a name for the Application Name. In this example enter TestApp1.
- Accept the default Project Name.
-
Enter a value for Package Name.
In this example enter com.alfresco.tutorials.testapp1.
- Set Minimum Required SDK to API 14.
- Set Target SDK to API 19.
- Set Compile With to API 19.
- Leave Theme as default.
- Click Next.
-
Leave checkboxes at defaults:
- Create custom launcher icon checkbox is selected.
- Create activity checkbox is selected.
- Mark this project as library checkbox is cleared.
- Create Project in Workspace checkbox is selected.
- Click Next.
- In the Configure Launcher Icon dialog click Next to accept the defaults.
- Select the CreateActivity checkbox and ensure Blank Activity is highlighted.
- Click Next.
- Leave the Activity name as MainActivity and the layout name as activity_main and then click Finish to create the new project.
-
Test your work so far by running the application. You can do this by right-clicking
on the root of your project in the Package Explorer pane then
selecting Run As and then Android
Application.
The application will display Hello world!
Extract the library files
The following files will be extracted:
alfresco-mobile-android-client-api-1.4.0.jar alfresco-opencmis-extension-0.7-ANDROID.jar chemistry-opencmis-android-client-0.11.0.jar slf4j-android-1.6.1-RC1.jar
Add the external library files to the project
- In Mac Finder or Windows Explorer navigate to the directory containing the SDK JAR files, in this example, ~/mobile_sdk/binaries/Android/libs.
- Drag and drop the JAR files into the libs folder of your project in Eclipse's Package Explorer, ensuring that the Copy Files radio button is selected in the File Operation dialog that is displayed during the drag and drop operation.
Add SDK-specific code to your application
- In the Eclipse Package Explorer navigate to your project's source code and load the file MainActivity.java into the editor.
-
Modify the source code so that it matches the following code:
Attention: You will need to provide the URL, username, and password for the SkyVault repository you wish to connect to.
package com.alfresco.tutorials.testapp1; import java.util.List; import org.alfresco.mobile.android.api.exceptions.AlfrescoSessionException; import org.alfresco.mobile.android.api.model.Folder; import org.alfresco.mobile.android.api.model.Node; import org.alfresco.mobile.android.api.model.RepositoryInfo; import org.alfresco.mobile.android.api.model.Site; import org.alfresco.mobile.android.api.services.DocumentFolderService; import org.alfresco.mobile.android.api.services.SiteService; import org.alfresco.mobile.android.api.session.RepositorySession; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Modify to suit your SkyVault server installation String url = "http://localhost:8080/alfresco"; String username = "admin"; String password = "admin"; new ConnectToRepo().execute(url, username, password); } class ConnectToRepo extends AsyncTask<String, Integer, String> { private static final String TAG = "ConnectToRepo"; @Override protected String doInBackground(String... params) { Log.d(TAG, "doInBackground"); Log.d(TAG, params[0] + ":" + params[1] + ":" + params[2]); String url = params[0]; String username = params[1]; String password = params[2]; // HelloRepo try { // connect to on-premise repo RepositorySession session = RepositorySession.connect(url, username, password); if (session != null) { // Get some repository information Log.d(TAG, "baseUrl: " + session.getBaseUrl()); Log.d(TAG, "rootFolder: " + session.getRootFolder().getName()); // Obtain a repository information object RepositoryInfo repoInfo = session.getRepositoryInfo(); Log.d(TAG, "repoId: " + repoInfo.getIdentifier()); Log.d(TAG, "repoName: " + repoInfo.getName()); Log.d(TAG, "repoDescription: " + repoInfo.getDescription()); Log.d(TAG, "repoVersion: " + repoInfo.getVersion()); Log.d(TAG, "repoEdition: " + repoInfo.getEdition()); // Get site service SiteService siteService = session.getServiceRegistry() .getSiteService(); // Get sites for current user List<Site> sites = siteService.getSites(); // Get first site Site site = sites.get(0); // Get site document library Folder folder = siteService.getDocumentLibrary(site); // Find DocumentFolderService DocumentFolderService documentFolderService = session .getServiceRegistry().getDocumentFolderService(); // Get children of document library List<Node> nodes = documentFolderService .getChildren(folder); for (Node node : nodes) { Log.d(TAG, "node: " + node.getTitle() + "=" + node.getName() + " created by: " + node.getCreatedBy() + " isFolder: " + node.isFolder()); } } else { Log.d(TAG, "No Session available!"); } } catch (AlfrescoSessionException e) { Log.e(TAG, "Failed to connect: " + e.toString()); } Log.d(TAG, "doInBackground Complete"); return "doInBackground Complete"; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); Log.d(TAG, "onPostExecute"); Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show(); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); Log.d(TAG, "onProgressUpdate"); } } }
Attention: If you are testing this application against a local SkyVault installation do not use the loopback address 127.0.0.1 as this will not work when you run your application in the Android Emulator. Instead, find your local IP address using ifconfig or similar and use that.This code provides a minimal application that will connect to the specified SkyVault repository, and retrieve some information about that respository. This information is displayed in LogCat in the Eclipse IDE.
Add permissions to the Android manifest file
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Add these two lines, if not present, in the location as illustrated by the following example manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.alfresco.tutorials.myfirstalfrescoapp" android:versionCode="1" android:versionName="1" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:configChanges="orientation|keyboardHidden" android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name="MainActivity" 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 manifest file will now have the necessary permissions to access the internet and write to storage.
Run your application
The Android Emulator will launch. Unlock the Virtual Device and your application will run. The repository information will be displayed in the LogCat tab in the Eclipse IDE (example output shown):
... 03-03 08:01:29.921: D/ConnectToRepo(1115): baseUrl: http://10.244.50.101:8080/SkyVault 03-03 08:01:29.921: D/ConnectToRepo(1115): rootFolder: Company Home 03-03 08:01:29.921: D/ConnectToRepo(1115): repoId: -default- 03-03 08:01:29.921: D/ConnectToRepo(1115): repoName: 03-03 08:01:29.921: D/ConnectToRepo(1115): repoDescription: 03-03 08:01:29.921: D/ConnectToRepo(1115): repoVersion: 5.1.0 (r96687-b328) 03-03 08:01:29.921: D/ConnectToRepo(1115): repoEdition: Enterprise ... 03-03 08:01:30.911: D/ConnectToRepo(1115): node: Agency related files=Agency Files created by: mjackson isFolder: true 03-03 08:01:30.911: D/ConnectToRepo(1115): node: Project finance files=Budget Files created by: mjackson isFolder: true 03-03 08:01:30.911: D/ConnectToRepo(1115): node: Project meeting notes=Meeting Notes created by: mjackson isFolder: true 03-03 08:01:30.911: D/ConnectToRepo(1115): node: Project presentations=Presentations created by: mjackson isFolder: true 03-03 08:01:30.911: D/ConnectToRepo(1115): doInBackground Complete 03-03 08:01:30.921: D/ConnectToRepo(1115): onPostExecute ...
In this tutorial you have learned how to build and run an application that uses the Mobile SDK binary package.