package com.ibm.mobilefirstplatform.clientsdk.android.core.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.ibm.mobilefirstplatform.clientsdk.android.core.api.BMSClient;
import com.ibm.mobilefirstplatform.clientsdk.android.core.api.MFPRequest;
import com.ibm.mobilefirstplatform.clientsdk.android.core.api.ResourceRequest;
import com.ibm.mobilefirstplatform.clientsdk.android.core.api.Response;
import com.ibm.mobilefirstplatform.clientsdk.android.core.api.ResponseListener;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.AuthenticationContext;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.AuthenticationListener;
import com.ibm.mobilefirstplatform.clientsdk.android.security.api.AuthorizationManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;

public class MainActivity extends Activity implements ResponseListener{

	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

		try {
			BMSClient.getInstance().initialize(getApplicationContext(), "http://9.148.225.110:9080", "vit1");
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

        BMSClient.getInstance().registerAuthenticationListener("customAuthRealm_1", new CustomChallengeHandler());

//		AuthorizationManager.getInstance().obtainAuthorizationHeader(this, this);
        MFPRequest.registerInterceptor(null);
        AuthorizationManager.getInstance().clearAuthorizationData();
        
        ResourceRequest r = new ResourceRequest(this, "http://9.148.225.110:3000/v1/apps/vit1/service", MFPRequest.GET);
        r.send(this);
    }

	@Override
	public void onSuccess(Response response) {

	}

	@Override
	public void onFailure(Response response, Throwable t, JSONObject extendedInfo) {

	}
}

class CustomChallengeHandler implements AuthenticationListener {

    @Override
    public void onAuthenticationChallengeReceived(AuthenticationContext authContext, JSONObject challenge, Context context) {
        try {
            JSONObject answer = new JSONObject("{\"userName\":\"asaf\",\"password\":\"123\"}");
            authContext.submitAuthenticationChallengeAnswer(answer);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onAuthenticationSuccess(JSONObject info) {

    }

    @Override
    public void onAuthenticationFailure(JSONObject info) {

    }
}

@@ -965,16 +965,421 @@
 tener%7B%0A%0A
+    private final static String backendURL = %22http://9.148.225.187:9080%22; // your BM application URL%0A    private final static String backendGUID = %22vit1%22; // the GUID you get from the dashboard%0A    private final static String customResourceURL = %22http://9.148.225.187:3000/v1/apps/vit1/service%22; // any protected resource%0A    private final static String customRealm = %22customAuthRealm_1%22; // auth realm%0A%0A%0A
 %09@Overri
@@ -1523,16 +1523,18 @@
 main);%0A%0A
+%0A%0A
 %09%09try %7B%0A
@@ -1600,43 +1600,31 @@
 (), 
-%22http://9.148.225.110:9080%22, %22vit1%22
+backendURL, backendGUID
 );%0A%09
@@ -1752,35 +1752,27 @@
 istener(
-%22
 custom
-Auth
 Realm
-_1%22
 , new Cu
@@ -1801,131 +1801,61 @@
 );%0A%0A
-//%09%09AuthorizationManager.getInstance().obtainAuthorizationHeader(this, this);%0A        MFPRequest.registerInterceptor(null);
+        // to make the authorization happen next time
 %0A   
@@ -1987,56 +1987,25 @@
 is, 
-%22http://9.148.225.110:3000/v1/apps/vit1/service%22
+customResourceURL
 , MF
@@ -2099,24 +2099,72 @@
 response) %7B%0A
+        // here we handle authentication success
 %0A%09%7D%0A%0A%09@Overr
@@ -2249,16 +2249,46 @@
 Info) %7B%0A
+        // handle auth failure
 %0A%09%7D%0A%7D%0A%0Ac
@@ -2506,16 +2506,146 @@
   try %7B%0A
+            // provide your custom credentials here%0A            // you can display a dialog here to obtain user name and password%0A
         
@@ -2731,16 +2731,78 @@
 23%5C%22%7D%22);
+%0A%0A            // submit the credentials obtained from the user
 %0A       

Update sample code