
private OkHttpClient myOkHttpClient;
private String deviceID;
private final String serviceURL = "https://hcpms-p1743065160trial.hanatrial.ondemand.com"; //change p1743065160
private final String appID = "com.sap.stepbystep";
private final String connectionID = "com.sap.edm.sampleservice.v2";
private String messageToToast;
private Toast toast;
private String currentUser;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.CookieManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.sap.cloud.mobile.foundation.authentication.BasicAuthDialogAuthenticator;
import com.sap.cloud.mobile.foundation.authentication.OAuth2BrowserProcessor;
import com.sap.cloud.mobile.foundation.authentication.OAuth2Configuration;
import com.sap.cloud.mobile.foundation.authentication.OAuth2Interceptor;
import com.sap.cloud.mobile.foundation.authentication.OAuth2WebViewProcessor;
import com.sap.cloud.mobile.foundation.common.ClientProvider;
import com.sap.cloud.mobile.foundation.common.EncryptionError;
import com.sap.cloud.mobile.foundation.common.EncryptionUtil;
import com.sap.cloud.mobile.foundation.common.SettingsParameters;
import com.sap.cloud.mobile.foundation.configurationprovider.ConfigurationLoader;
import com.sap.cloud.mobile.foundation.configurationprovider.ConfigurationLoaderCallback;
import com.sap.cloud.mobile.foundation.configurationprovider.ConfigurationPersistenceException;
import com.sap.cloud.mobile.foundation.configurationprovider.ConfigurationProvider;
import com.sap.cloud.mobile.foundation.configurationprovider.ConfigurationProviderError;
import com.sap.cloud.mobile.foundation.configurationprovider.DefaultPersistenceMethod;
import com.sap.cloud.mobile.foundation.configurationprovider.DiscoveryServiceConfigurationProvider;
import com.sap.cloud.mobile.foundation.configurationprovider.FileConfigurationProvider;
import com.sap.cloud.mobile.foundation.configurationprovider.ProviderIdentifier;
import com.sap.cloud.mobile.foundation.configurationprovider.ProviderInputs;
import com.sap.cloud.mobile.foundation.configurationprovider.UserInputs;
import com.sap.cloud.mobile.foundation.logging.Logging;
import com.sap.cloud.mobile.foundation.networking.AppHeadersInterceptor;
import com.sap.cloud.mobile.foundation.networking.WebkitCookieJar;
import com.sap.cloud.mobile.foundation.securestore.OpenFailureException;
import com.sap.cloud.mobile.foundation.securestore.SecureKeyValueStore;
import com.sap.cloud.mobile.foundation.user.UserInfo;
import com.sap.cloud.mobile.foundation.user.UserRoles;
import com.sap.cloud.mobile.foundation.usage.AppUsage;
import com.sap.cloud.mobile.foundation.usage.AppUsageInfo;
import com.sap.cloud.mobile.foundation.usage.AppUsageUploader;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.core.util.StatusPrinter;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
deviceID = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
private void toastAMessage(String msg) {
if (toast != null && toast.getView().isShown()) {
msg = messageToToast + "\n" + msg;
}
else { //clear any previously shown toasts that have since stopped being displayed
messageToToast = "";
}
messageToToast = msg;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(getApplicationContext(),
messageToToast,
Toast.LENGTH_LONG);
toast.show();
}
});
}
private void getUser() {
Log.d(myTag, "In getUser");
SettingsParameters sp = null;
try {
sp = new SettingsParameters(serviceURL, appID, deviceID, "1.0");
}
catch (MalformedURLException e) {
e.printStackTrace();
}
UserRoles roles = new UserRoles(myOkHttpClient, sp);
UserRoles.CallbackListener callbackListener = new UserRoles.CallbackListener() {
@Override
public void onSuccess(@NonNull UserInfo ui) {
Log.d(myTag, "User Name: " + ui.getUserName());
Log.d(myTag, "User Id: " + ui.getId());
String[] roleList = ui.getRoles();
Log.d(myTag, "User has the following Roles");
for (int i=0; i < roleList.length; i++) {
Log.d(myTag, "Role Name " + roleList[i]);
}
currentUser = ui.getId();
toastAMessage("Currently logged with " + ui.getId());
}
@Override
public void onError(@NonNull Throwable throwable) {
toastAMessage("UserRoles onFailure " + throwable.getMessage());
}
};
roles.load(callbackListener);
}
private void enableButtonsOnRegister(final boolean enable) {
final Button uploadLogButton = (Button) findViewById(R.id.b_uploadLog);
final Button onlineODataButton = (Button) findViewById(R.id.b_odata);
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
uploadLogButton.setEnabled(enable);
onlineODataButton.setEnabled(enable);
}
});
}
public void onRegister(View view) {
Log.d(myTag, "In onRegister");
myOkHttpClient = new OkHttpClient.Builder()
.addInterceptor(new AppHeadersInterceptor(appID, deviceID, "1.0"))
.authenticator(new BasicAuthDialogAuthenticator())
.cookieJar(new WebkitCookieJar())
.build();
Request request = new Request.Builder()
.get()
.url(serviceURL + "/" + connectionID + "/")
.build();
Callback updateUICallback = new Callback() {
@Override
public void onFailure(Call call, final IOException e) { //called if there is no network
Log.d(myTag, "onFailure called during authentication " + e.getMessage());
toastAMessage("Registration failed " + e.getMessage());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
if (response.isSuccessful()) {
Log.d(myTag, "Successfully authenticated");
toastAMessage("Successfully authenticated");
enableButtonsOnRegister(true);
getUser();
}
else { //called if the credentials are incorrect
Log.d(myTag, "Registration failed " + response.networkResponse());
toastAMessage("Registration failed " + response.networkResponse());
}
}
};
myOkHttpClient.newCall(request).enqueue(updateUICallback);
}
package com.sap.stepbystep;
import android.app.Application;
import com.sap.cloud.mobile.foundation.authentication.AppLifecycleCallbackHandler;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(AppLifecycleCallbackHandler.getInstance());
}
}
android:name=".MyApplication"
{
"id":"P1743065160",
"schemas":["urn:ietf:params:scim:schemas:core:2.0:User"],
"roles":["Developer","Notification User","Administrator"],
"userName":"P1743065160"
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
28 | |
25 | |
23 | |
19 | |
19 | |
15 | |
12 | |
10 | |
8 | |
8 |