Accela Android SDK plugin for Eclipse provides SDK and Eclipse project templates to quickly create Android apps. SDK provides mechanism for user login, getting tokens, using tokens to interact with APIs and logging off users. When you create your app using the Eclipse project template, the code for login function is generated automatically.
1. Declare the "AuthorizationActivity" activity
The following code snippet demonstrates how to declare the "AuthorizationActivity" activity in the AndroidManifest.xml file.
<!-- Important: The activity AuthorizationActivity must be declared here
because it is used in user authorization.
Note the value of android:scheme will be assigned
to the urlSchema property in AccelaMobile class.
-->
<activity android:name="com.accela.mobile.AuthorizationActivity"
android:windowSoftInputMode="stateHidden" android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="aminspectionviewer" android:host="authorize"></data>
</intent-filter>
</activity>
2. Initialize SDK
The following code snippet demonstrates how to initialize an AccelaMobile instance. The app ID and app secret are defined in the <project_root>\assets\accela.properties file.
<project_root> represents the root directory of the current project.
public AccelaMobile getAccelaMobile(Activity activity) {
if (accelaMobile == null) {
// Create an AccelaMobile instance with the app ID and app secret
accelaMobile = new AccelaMobile(activity,
accelaProperties.getProperty("accela.app.id")),
accelaProperties.getProperty("accela.app.secret"));
// Set the environment
accelaMobile.setEnvironment(AccelaMobile.Environment.PROD);
// Set the URL Schema.
// NOTE: The assigned value should be same as the value of "android:scheme"
// under "AuthorizationActivity" node in the project's AndroidManifest.xml.
accelaMobile.setUrlSchema(accelaProperties.getProperty("schema"));
}
return accelaMobile;
}
3. Invoke user login dialogs and receive access token
The following code snippet demonstrates how to launch an Accela login view through a web browser, and defines a session delegate to handle the authorization result asynchronously.
After a user logs in successfully, the amDidLogin() method in this session delegate is called back.
You can replace the following permission scopes with those described at API Reference.
// Specify the permission required.
String[] permissions4Authorization = new String[] {"get_record_inspections"};
// Start web browser for authorization.
appContext.accelaMobile.authorize(permissions4Authorization);
// Session delegate.
private AMSessionDelegate sessionDelegate = new AMSessionDelegate(){
public void amDidLogin() {
// Do something for successful login.
AMLogger.logInfo("Session Delegate.amDidLogin() invoked.");
}
public void amDidLoginFailure(AMError error) {
// Do something for failed login
AMLogger.logInfo("Session Delegate.amDidLoginFailure() invoked: %s", error.toString());
}
public void amDidCancelLogin() {
// Do something if login dialog is cancelled.
AMLogger.logInfo("Session Delegate.amDidCancelLogin() invoked.");
}
public void amDidSessionInvalid(AMError error) {
// Do something if user session becomes invalid.
AMLogger.logInfo("Session Delegate.amDidSessionInvalid() invoked: %s", error.toString());
}
public void amDidLogout() {
// Do something if user logged out.
AMLogger.logInfo("Session Delegate.amDidLogout() invoked.");
}
};
4. Interact with APIs
The following code snippet demonstrates how to call an API to get a list of inspections, and defines a request delegate to handle the returned response asynchronously.
After the valid JSON is returned, the onSuccess() method in this request delegate is called back.
// Populate parameters for request.
String SERVICE_URI_INSPECTION_LIST = "/v4/records/{id}/inspections/";
String servicePath = SERVICE_URI_INSEPCTION_LIST.replace("{id}", "13CAP-00000-00001");
RequestParams requestParams = new RequestParams();
requestParams.put("limit", "10");
requestParams.put("offset", "0");
// Sending HTTP / HTTPS request to call the API
currentRequest = appContext.accelaMobile.request(servicePath, requestParams, requestDelegate);
// Request delegate
private AMRequestDelegate requestDelegate = new AMRequestDelegate() {
@Override
public void onStart() {
// Show progress waiting view
currentRequest.setOwnerView(mainLayout, getResources().getString(R.string.msg_search_inspections));
}
@Override
public void onSuccess(JSONObject responseJson) {
// Dismiss the process waiting view
ProgressDialog progressDialog = currentRequest.getRequestWaitingView();
if ((progressDialog != null) && (progressDialog.isShowing())) {
progressDialog.dismiss();
}
// Parse JSON data
JSONArray inspectionsArray = null;
try {
inspectionsArray = responseJson.getJSONArray("inspections");
} catch (JSONException e) {
AMLogger.logError("Error in parsing JSON: %s", e.getMessage());
}
// Continue to work with the parsed JSON data.
}
public void onFailure(Throwable error) {
// Dismiss the process waiting view
ProgressDialog progressDialog = currentRequest.getRequestWaitingView();
if ((progressDialog != null) && (progressDialog.isShowing())) {
progressDialog.dismiss();
}
// Show dialog with the returned error
createAlertDialog(getResources().getString(R.string.error_request_title),
error.getLocalizedMessage());
}
};
5. Log out
The following code snippet demonstrates how to log off the current user.
After the logout() method is called, the amDidLogout() method in the predefined session delegate is called back automatically.
// Log out user session.
appContext.accelaMobile.logout();