Skip to content

Instantly share code, notes, and snippets.

@alizahid
Last active March 23, 2017 14:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alizahid/05c978e13e24385e2261 to your computer and use it in GitHub Desktop.
Save alizahid/05c978e13e24385e2261 to your computer and use it in GitHub Desktop.
SMP 3.0 SMP07 Android registration and logout
public class LoginActivity extends Activity implements LogonCoreListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadingSpinner = new LoadingSpinner(this);
loadingSpinner.show();
// get instance
logonCore = LogonCore.getInstance();
// set listener for LogonCore
logonCore.setLogonCoreListener(this);
// boot up
logonCore.init(this, "com.example.mysmpapp");
// check if store is open and available
// open and unlock if not
try {
if (!logonCore.isStoreAvailable()) {
logonCore.createStore(null, false);
}
logonCore.unlockStore(null);
} catch (LogonCoreException e) {
e.printStackTrace();
}
// check if user is ALREADY registered or not
if (logonCore.isRegistered()) {
// send user to main activity
Intent intent = new Intent(this, DashboardActivity.class);
startActivity(intent);
// finish this activity
finish();
} else {
// show login form
setContentView(R.layout.activity_login);
}
}
// call this method from your layout
// using android:onClick="login" on a button
// or however else you want to
public void login(View view) {
// replace credentials with the real deal
String username = "test";
String password = "123";
// create LogonCoreContext
// this is used for registration
LogonCoreContext context = logonCore.getLogonContext();
// set configuration
try {
context.setHost("myserver.com");
context.setPort(80);
context.setHttps(false);
// context.setFarmId("MYFARM.SUPFarm");
context.setSecurtityConfig("default");
// context.setResourcePath("ias_relay_server/client/rs_client.dll");
context.setDomain("default");
// MOST important part!
context.setChannel(LogonCore.Channel.REST);
context.setBackendUser(username);
context.setBackendPassword(password);
// call registration
logonCore.register(context);
} catch (LogonCoreException e) {
e.printStackTrace();
}
}
// LogonCoreListener methods
@Override
public void registrationFinished(boolean success, String message, int errorCode, DataVault.DVPasswordPolicy dvPasswordPolicy) {
if (success) {
try {
// if successful, persist registration
// DO NOT follow the manual storage of APPCID as shown in SMP docs
logonCore.persistRegistration();
// send user to main activity
Intent intent = new Intent(this, DashboardActivity.class);
startActivity(intent);
// finish this activity
finish();
} catch (LogonCoreException e) {
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT);
}
}
@Override
public void deregistrationFinished(boolean success) {
}
@Override
public void backendPasswordChanged(boolean success) {
}
@Override
public void applicationSettingsUpdated() {
}
@Override
public void traceUploaded() {
}
}
private void logout() {
// call deregister
LogonCore.getInstance().deregister();
try {
// close online, if you opened it
onlineStore.close();
// clear technical cache for offline store
// so there's nothing in the cache for the next user
// prevents confusion if you're using the requestCacheResponse method
onlineStore.resetCache();
// close offline store
// but not clear the data in it
offlineStore.closeStore();
} catch (ODataException e) {
e.printStackTrace();
}
// back to login activity
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
// finish this!
finish();
}
Copy link

ghost commented May 22, 2015

Hi Ali,

First of all thank you so much for this light to unregister a user on SMP. I have 2 questions, when you say "MOST important part!" context.setChannel(LogonCore.Channel.REST); line, why do you say that? Sorry I'm begining with these OData Libraries and I'm not setting this Channel to my context, but my registration it's working fine. What means to set Channel.REST? And the other question, are you working now with Technical Cache?

Thanks in advances.

Best regards,
Ana Velásquez

@alizahid
Copy link
Author

Hey, Ana.

During my development, there was a time when I could successfully register, but they all showed with the user nosec_identity on the SMP admin cockpit. Debugging showed that the username and password were not being sent.

After a lot of back and forth with SAP, they gave the fix which was to set the channel to LogonCore.Channel.REST. Now the registration on SMP admin cockpit shows with the proper username.

If it works for you without using REST, then you may remove it. Maybe it was our SMP configuration which required this to be set.

For your second question; yes, I'm using the technical cache for OData Online Store.

Copy link

ghost commented May 22, 2015

Ali and do you have a hint on how to implement Technical Cache? I'm developing a native android app using OnlineStore but it may be occasionally offline, for what I've read so far, Technical Cache it's the approach for achieve this. Thank you again. I have other 2 questions, are you working with which versions, SDK and Server? and when you refer to OnlineStore.getInstance().getOnlineStore().close(); that "OnlineStore" is an instance of what, or is the library? I ask this because I'm working with SDK SP05 and the way I open my OnlineStore is this:

OnlineODataStore.open(context, uri, manager, openListener, null);

But I cannot find on what object I should call the .getInstance().getOnlineStore().close(), I don't know If you do understand me.

Thanks in advance again.

@alizahid
Copy link
Author

To implement technical cache, you need to pass an OnlineODataStore.OnlineStoreOptions object to the open method.

 OnlineODataStore.OnlineStoreOptions options = new OnlineODataStore.OnlineStoreOptions(OnlineODataStore.PayloadFormatEnum.JSON); // I use JSON, you can choose XML if you want

 options.useCache = true;
 options.cacheEncryptionKey = "my_secret_key";

And then;

 OnlineODataStore.open(context, uri, manager, openListener, options);

And the SDK will take care of the rest.

As for the code you cannot find, that's my own class for convenience access to the Online Store. I'll edit the gist to remove it.

Copy link

ghost commented Jun 24, 2015

Hi Ali,

I was wondering if you could help me with an issue I'm having and I'll like to know how are you handle it, since on internet I could not find any information of this. The problem is to handle the connection errors of the device to the Managment Cockpit.

I would really appretiate any light that you can give me on this.

Thanks in advance.

Best regards,
Ana

@timothy2005
Copy link

Hi Ali, your code are really helpful, thank you very much.
Is it possible for you to share code of DashboardActivity.class as well?
I really want to learn How to get the business data from backend system.
Now I have successfully registered to SMP through my app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment