Client Resources

The ClientResources module allows you to download resources associated with an application to a byte array. This module also provides APIs to download the resource content to a file. SAP Cloud Platform Mobile Services allows administrators to assign various resources to an application.

All the API methods must be invoked from a UI thread. The callback methods are invoked on the UI thread.

Sample Usage

The following sample code shows how to use this API to download the default resource.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ClientResources clientResources = new ClientResources();
clientResources.downloadResource(new ClientResources.CallbackListener<byte[]>() {
        @Override
        public void onSuccess(byte[] byteData){
            //Code to handle the read data
        }

        @Override
        public void onError (Throwable error){
            //Code to handle error
        }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
val clientResources = ClientResources()
clientResources.downloadResource(object : ClientResources.CallbackListener<ByteArray> {
    override fun onSuccess(byteData: ByteArray) {
        //Code to handle the read data
    }

    override fun onError(error: Throwable) {
         //Code to handle error
    }
 })

The following sample code shows how to use the API to download a specific named resource:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ClientResources clientResources = new ClientResources();
clientResources.downloadResource("resourcename", new ClientResources.CallbackListener<byte[]>() {
    @Override
    public void onSuccess(byte[] byteData) {
        //Code to handle the read data
    }

    @Override
    public void onError(Throwable error) {
        //Code to handle error
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
val clientResources = ClientResources()
clientResources.downloadResource("resourcename", object : ClientResources.CallbackListener<ByteArray> {
    override fun onSuccess(byteData: ByteArray) {
        //Code to handle the read data
    }

    override fun onError(error: Throwable) {
        //Code to handle error
    }
})

The following sample code shows how to use the API to download a specific version of a named resource:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
ClientResources clientResources = new ClientResources();
clientResources.downloadResource("resourcename", "1.0", new ClientResources.CallbackListener<byte[]>() {
    @Override
    public void onSuccess( byte[] byteData) {
        //Code to handle the read data
    }

    @Override
    public void onError(Throwable error) {
        //Code to handle error
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
val clientResources = ClientResources()
clientResources.downloadResource("resourcename", "1.0", object : ClientResources.CallbackListener<ByteArray> {
    override fun onSuccess(byteData: ByteArray) {
        //Code to handle the read data
    }

    override fun onError(error: Throwable) {
       //Code to handle error
    }
})

The following sample code shows how to download the default resource and write to a specific file on the device:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
String fileLocation =  Environment.getExternalStorageDirectory().getAbsolutePath();;
ClientResources clientResources = new ClientResources();
clientResources.downloadResource(fileLocation + "/filename.ext", true, new ClientResources.CallbackListener<Void>() {
    @Override
    public void onSuccess(Void v) {
        //Handle success callback
    }
    @Override
    public void onError(Throwable error) {
        //Handle the error callback
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
val fileLocation = Environment.getExternalStorageDirectory().absolutePath
val clientResources = ClientResources()
clientResources.downloadResource("$fileLocation/filename.ext", true, object : ClientResources.CallbackListener<Void> {
    override fun onSuccess(v: Void) {
        //Handle success callback
    }

    override fun onError(error: Throwable) {
        //Handle the error callback
    }
})

The following sample code shows how to download a specific version of a named resource and write to a file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
String fileLocation =  Environment.getExternalStorageDirectory().getAbsolutePath();;
ClientResources clientResources = new ClientResources();
clientResources.downloadResource("nameofresource", "1.0", fileLocation + "/filename.ext", true, new ClientResources.CallbackListener<Void>() {
    @Override
    public void onSuccess(Void v) {
        //Handle success callback
    }
    @Override
    public void onError(Throwable error) {
        //Handle the error callback
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
val fileLocation = Environment.getExternalStorageDirectory().absolutePath
val clientResources = ClientResources()
clientResources.downloadResource("nameofresource", "1.0", "$fileLocation/filename.ext", true, object : ClientResources.CallbackListener<Void> {
    override fun onSuccess(v: Void) {
        //Handle success callback
    }

    override fun onError(error: Throwable) {
        //Handle the error callback
    }
})

The following sample code shows how to get the Resource Information:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
ClientResources clientResources = new ClientResources();
clientResources.fetchResourceInfo(new ClientResources.CallbackListener<ResourceBundle[]>() {
    @Override
    public void onSuccess(ResourceBundle[] info) {
        //Handle success callback
        for (int i=0; i< info.length; i++) {
            Log.i("Bundle Version:",info[i].getBundleVersion());
            Log.i("Bundle Extension:", info[i].getBundleExtension());
            Log.i("Bundle Name:", info[i].getBundleName());
            Log.i("Is default:", info[i].isDefault() ? "true" : "false");
        }
    }

    @Override
    public void onError(@NonNull Throwable error) {
        //Handle the error callback
    }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
val clientResources = ClientResources()
clientResources.fetchResourceInfo(object : ClientResources.CallbackListener<Array<ResourceBundle>> {
    override fun onSuccess(info: Array<ResourceBundle>) {
        //Handle success callback
        for (i in info.indices) {
            Log.i("Bundle Version:", info[i].bundleVersion)
            Log.i("Bundle Extension:", info[i].bundleExtension)
            Log.i("Bundle Name:", info[i].bundleName)
            Log.i("Is default:", if (info[i].isDefault) "true" else "false")
        }
    }

    override fun onError(error: Throwable) {
        //Handle the error callback
    }
})

ClientResourceService

The Android SDK 3.0.0 provides ClientResourceService to handle auto download client resource from server. Mobile application developers only need add the ClientResourceService class to MobileServices.start() method and implement the related listener to handle dowloaded data.

1
2
3
4
ClientResourceService.setRawDownloadListener((data) -> {
    //hanlde the downloaded binary data
});  
MobileServices.start(this, ClientProvider.get(), SettingsProvider.get(), ClientResourceService.class);
1
2
3
4
ClientResourceService.setRawDownloadListener { data ->
  //hanlde the downloaded data
}  
MobileServices.start(this, ClientProvider.get(), SettingsProvider.get(), ClientResourceService::class.java)   

Note

The listener must setup before calling MobileServices.start()method. The Auto download only triggered when the application switch from background to foreground and after succeed onboarding.