Skip to content

Settings

Get Application-Level Settings from SAP Cloud Platform Mobile Services

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
final Settings settings = new Settings(ClientProvider.get(),
    CpmsParameters.getSettingsParameters());

// Must be invoked from UI thread
settings.load(Settings.SettingTarget.APPLICATION, 
    Settings.KeyPath.MOBILESERVICES, 
    new Settings.CallbackListener() {

        // Executes on UI thread
        @Override
        public void onSuccess(@NonNull JSONObject settingsData) {
            try {
                int valueForPasswordPolicyTimeout = settingsData.getJSONObject("settingsExchange")
                    .getJSONObject("passwordPolicy")
                    .getInt("passwordPolicyLockTimeout");
                sLogger.debug("Passcode timeout: {}", valueForPasswordPolicyTimeout);
            } catch (JSONException ex) {
                //Most likely reason is invalid root key specified
                sLogger.error("Unexpected settings data: {}", settingsData.toString());
            }
        }

        // Executes on UI thread
        @Override
        public void onError(@NonNull Throwable ex) {
            // com.sap.cloud.mobile.foundation.networking.HttpException
            if (ex instanceof HttpException) {
                sLogger.error("Settings load failed. Message: {}, http code: {}",
                    ((HttpException) ex).message(), ((HttpException) ex).code());
            } else {
                sLogger.error("Request failed: {}", ex.getMessage());
            }
        }
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
val settings = Settings(ClientProvider.get(), CpmsParameters.getSettingsParameters())

// Must be invoked from UI thread
settings.load(Settings.SettingTarget.APPLICATION,
    Settings.KeyPath.MOBILESERVICES,
    object : Settings.CallbackListener {

        // Executes on UI thread
        override fun onSuccess(settingsData: JSONObject) {
            try {
                val valueForPasswordPolicyTimeout = settingsData.getJSONObject("settingsExchange")
                    .getJSONObject("passwordPolicy")
                    .getInt("passwordPolicyLockTimeout")
                sLogger.debug("Passcode timeout: {}", valueForPasswordPolicyTimeout)
            } catch (ex: JSONException) {
                //Most likely reason is invalid root key specified
                sLogger.error("Unexpected settings data: {}", settingsData.toString())
            }
        }

        // Executes on UI thread
        override fun onError(ex: Throwable) {
            // com.sap.cloud.mobile.foundation.networking.HttpException
            if (ex is HttpException) {
                sLogger.error("Settings load failed. Message: {}, http code: {}",
                    ex.message(), ex.code())
            } else {
                sLogger.error("Request failed: {}", ex.message)
            }
        }
})

Store User Settings on SAP Cloud Platform Mobile Services

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
final Settings settings = new Settings(ClientProvider.get(), 
    CpmsParameters.getSettingsParameters());

// Must be invoked from UI thread
settings.store(Settings.SettingTarget.USER, 
    Settings.KeyPath.LOGSETTINGS,
    new JSONObject().put("logLevel", "TRACE"),
    new Settings.CallbackListener() {

        // Executes on UI thread
        @Override
        public void onSuccess(@NonNull JSONObject settingsData) {
            sLogger.debug("Settings stored.");
        }

        // Executes on UI thread
        @Override
        public void onError(@NonNull Throwable ex) {
            // com.sap.cloud.mobile.foundation.networking.HttpException
            if (ex instanceof HttpException) {
                sLogger.error("Settings store failed. Message: {}, http code: {}",
                    ((HttpException) ex).message(), ((HttpException) ex).code());
            } else {
                sLogger.error("Request failed: {}", ex.getMessage());
            }
        }
    }
);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
val settings = Settings(ClientProvider.get(),
        CpmsParameters.getSettingsParameters())

// Must be invoked from UI thread
settings.store(Settings.SettingTarget.USER,
    Settings.KeyPath.LOGSETTINGS,
    JSONObject().put("logLevel", "TRACE"),
    object : Settings.CallbackListener {

        // Executes on UI thread
        override fun onSuccess(settingsData: JSONObject) {
            sLogger.debug("Settings stored.")
        }

        // Executes on UI thread
        override fun onError(ex: Throwable) {
            // com.sap.cloud.mobile.foundation.networking.HttpException
            if (ex is HttpException) {
                sLogger.error("Settings store failed. Message: {}, http code: {}",
                    ex.message(), ex.code())
            } else {
                sLogger.error("Request failed: {}", ex.message)
            }
        }
    }
)