The Android SDK is an Android API to retrieve content from a MACM server.
The SDK may be installed by referencing the MACM-SDK-android-xxx.aar android archive found in this repository's dist/ folder. For instance, this can be done as follows in your build.gradle script, assuming the .aar file was copied in the app's 'libs/' folder:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name: 'MACM-SDK-android-release', ext: 'aar')
}
To install and run the sample app 'AndroidSDKTestApp':
- clone this Git repository
- import it in Android Studio
- the app can now be built and run from Android Studio as any other project
Authentication with the remote server can be done in two ways:
The username and password are hardcoded in the application and the following constructor should be used:
// create a service instance with application credentials
CAASService service = new CAASService("https://macm-rendering.saas.ibmcloud.com", "wps", "vp1234", "username", "password");
- The users must sign in against a MACM server with their own credentials, the following constructor must be used:
// create a service instance without credentials
CAASService service = new CAASService("https://macm-rendering.saas.ibmcloud.com", "wps", "vp1234");
- When the users provide their credentials, the following API checks these credentials against the MACM server:
// handle the sign-in result asynchronously
CAASDataCallback<Void> callback = new CAASDataCallback<Void>() {
@Override
public void onSuccess(CAASRequestResult<Void> requestResult) {
// sign-in successful
}
@Override
public void onError(CAASErrorResult error) {
// handle the error
}
};
// perform the explicit sign-in with user credentials
service.signIn("username", "password", callback);
// create the service that connects to the MACM instance
CAASService service = new CAASService("https://macm-rendering.saas.ibmcloud.com", "wps", "vp1234", "username", "password");
// handle the request result asynchronously
CAASDataCallback<CAASContentItemsList> callback = new CAASDataCallback<CAASContentItemsList>() {
@Override
public void onSuccess(CAASRequestResult<CAASContentItemsList> requestResult) {
CAASContentItemsList itemsList = requestResult.getResult();
List<CAASContentItem> items = itemsList.getContentItems();
// do something with the list of items
}
@Override
public void onError(CAASErrorResult error) {
// handle the error
}
};
// create the request
CAASContentItemsRequest request = new CAASContentItemsRequest(callback);
request.setPath("Samples/Content Types/Book"); // path to "Book" content items
// include the specified properties in the response
request.addProperties(CAASProperties.OIS, CAASProperties.CONTENT_TYPE, CAASProperties.TITLE,
CAASProperties.LAST_MODIFIED_DATE, CAASProperties.CATEGORIES, CAASProperties.KEYWORDS);
// request the url to the cover image
request.addElements("cover");
// request the first 10 items
request.setPageSize(10);
request.setPageNumber(1);
// execute the request
service.executeRequest(request);
// creates the service that connects to the MACM instance
CAASService service = new CAASService("https://macm-rendering.saas.ibmcloud.com", "wps", "vp1234", "username", "password");
// handle the request result asynchronously
CAASDataCallback<CAASContentItemsList> callback = ...;
// create the request
CAASContentItemsRequest request = new CAASContentItemsRequest(callback);
// id of the content items list to retrieve
request.setOid("5f2c3fee-3994-45d4-9570-a6aa67ff2250");
// sort by descending status then ascending title
request.addSortDescriptor("status", false);
request.addSortDescriptor("title", true);
// only include the published items that have any of the specified keywords
request.setWorkflowStatus(CAASContentItemsRequest.WorkflowStatus.Published);
request.addAnyKeywords("bestseller", "top10");
// execute the request
service.executeRequest(request);
// creates the service that connects to the MACM instance
CAASService service = new CAASService("https://macm-rendering.saas.ibmcloud.com", "wps", "vp1234", "username", "password");
// handle the request result asynchronously
CAASDataCallback<CAASContentItem> callback = new CAASDataCallback<CAASContentItem>() {
@Override
public void onSuccess(CAASRequestResult<CAASContentItem> requestResult) {
CAASContentItem item = requestResult.getResult();
// do something with the item
}
@Override
public void onError(CAASErrorResult error) {
// handle the error
}
};
// create the request
CAASContentItemRequest request = new CAASContentItemRequest(callback);
// id of the content item to retrieve
request.setOid("5f2c3fee-3994-45d4-9570-a6aa67ff2250");
// execute the request
service.executeRequest(request);
// create the service that connects to the MACM instance
CAASService service = new CAASService("https://macm-rendering.saas.ibmcloud.com", "wps", "vp1234", "username", "password");
CAASDataCallback<CAASContentItem> callback = ...;
// create the request
CAASContentItemRequest request = new CAASContentItemRequest(callback);
request.setPath("Samples/some/content item path");
// execute the request
service.executeRequest(request);
// create the service that connects to the MACM instance
CAASService service = ...;
final Context context = ...;
final ImageView imageView = ...;
// url retrieved from a previous request
// this is a partial URL, the CAASService instance will infer the full URL
final String url = "/wps/wcm/myconnect/80686c98-9264-4391-bc00-3a039f4dc0b3/cover.jpg?MOD=AJPERES"
// callback that retrieves the image and sets it on a view
CAASDataCallback<byte[]> callback = new CAASDataCallback<byte[]>() {
@Override
public void onSuccess(CAASRequestResult<byte[]> requestResult) {
// get the Content-Type response header
String contentType = requestResult.getHttpURLConnection().getHeaderField("Content-Type");
// if we have an image content type, build a Bitmap object from the raw data
if ((contentType != null) && contentType.startsWith("image")) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
imageView.setImageDrawable(drawable);
}
}
@Override
public void onError(CAASErrorResult error) {
Log.e(LOG_TAG, "failed to load image at " + url + ", with error: " + error);
}
};
CAASAssetRequest request = new CAASAssetRequest(url, callback);
service.executeRequest(request);
CAASService service = ...;
// handle the request result asynchronously
CAASDataCallback<CAASContentItemsList> callback = new CAASDataCallback<CAASContentItemsList>() {
@Override
public void onSuccess(CAASRequestResult<CAASContentItemsList> requestResult) {
CAASContentItemsList projectsList = requestResult.getResult();
List<CAASContentItem> projects = projectsList.getContentItems();
// do something with the list of projects
}
@Override
public void onError(CAASErrorResult error) {
// handle the error
}
};
// create the request
CAASContentItemsRequest request = new CAASContentItemsRequest(callback);
// open projects are always in the "MACM System" library
request.setPath("MACM System/Views/Open Projects");
// include the specified properties in the response
request.addProperties(CAASProperties.UUID, CAASProperties.ITEM_COUNT, CAASProperties.TITLE, CAASProperties.LAST_MODIFIED_DATE);
service.executeRequest(request);
CAASService service = ...;
CAASDataCallback<CAASContentItemsList> callback = ...;
CAASContentItemsRequest request = new CAASContentItemsRequest(callback);
request.setPath("Samples/Views/All");
// set the project scope
request.setProject("MyProject");
service.executeRequest(request);
By default, Android only allows trusted certificates whose certificate authority is in its trusted list.
To disable this behavior, for instance to test with self-signed certificates, the allowUntrustedCertificates
flag must be set to `true', as follows:
// create the service that connects to the MACM instance
CAASService service = new CAASService("https://macm-rendering.saas.ibmcloud.com", "wps", "vp1234", "username", "password");
service.setAllowUntrustedCertificates(true);
...
For the Android SDK to access the available location providers, the application must provide an Android application Context. This can be done as in the following example:
public class MyActivity extends Activity {
...
public void doLogin() {
CAASService service = new CAASService("https://macm-rendering.saas.ibmcloud.com", "wps", "vp1234", "username", "password");
service.setAndroidContext(getApplicationContext());
...;
}
}