}\r
}\r
};\r
- mAccountManager.signOut(this);\r
+ public void logMessage(final String text) {\r
+ runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ Message msg = new Message();\r
+ msg.obj = text;\r
+ TvLog.append(text + "\n");\r
+ }\r
+ });\r
+ Log.i(TAG, text);\r
+ }\r
+ OcAccountManager.OnPostListener onSignUp = new OcAccountManager.OnPostListener() {\r
+ @Override\r
+ public synchronized void onPostCompleted(List<OcHeaderOption> list,\r
+ OcRepresentation ocRepresentation) {\r
+ logMessage("signUp was successful");\r
+ runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ signIn.setEnabled(true);\r
+ signUp.setEnabled(false);\r
+ }\r
+ });\r
+ try {\r
+ mUserID = ocRepresentation.getValue("uid");\r
+ mAccessToken = ocRepresentation.getValue("accesstoken");\r
+ mRefreshtoken = ocRepresentation.getValue("refreshtoken");\r
+ \r
+ logMessage("\tuserID: " + mUserID);\r
+ logMessage("\taccessToken: " + mAccessToken);\r
+ logMessage("\trefreshToken: " + mRefreshtoken);\r
+ \r
+ if (ocRepresentation.hasAttribute("expiresin")) {\r
+ int expiresIn = ocRepresentation.getValue("expiresin");\r
+ logMessage("\texpiresIn: " + expiresIn);\r
+ }\r
+ } catch (OcException e) {\r
+ Log.e(TAG, e.toString());\r
+ }\r
+ }\r
+ \r
+ \r
+ @Override\r
+ public synchronized void onPostFailed(Throwable throwable) {\r
+ logMessage("Failed to signUp");\r
+ if (throwable instanceof OcException) {\r
+ OcException ocEx = (OcException) throwable;\r
+ Log.e(TAG, ocEx.toString());\r
+ ErrorCode errCode = ocEx.getErrorCode();\r
+ logMessage("Error code: " + errCode);\r
+ }\r
+ }\r
+ };\r
+ OcAccountManager.OnPostListener onSignIn = new OcAccountManager.OnPostListener() {\r
+ @Override\r
+ public synchronized void onPostCompleted(List<OcHeaderOption> list,\r
+ OcRepresentation ocRepresentation) {\r
+ logMessage("signIn was successful");\r
+ runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ signIn.setEnabled(false);\r
+ signOut.setEnabled(true);\r
+ btnEnableRemoteService.setEnabled(true);\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public synchronized void onPostFailed(Throwable throwable) {\r
+ logMessage("Failed to signIn");\r
+ if (throwable instanceof OcException) {\r
+ OcException ocEx = (OcException) throwable;\r
+ Log.e(TAG, ocEx.toString());\r
+ ErrorCode errCode = ocEx.getErrorCode();\r
+ logMessage("Error code: " + errCode);\r
+ if (ErrorCode.UNAUTHORIZED_REQ != errCode) {\r
+ RefreshToken();\r
+ }\r
+ }\r
+ }\r
+ };\r
++ OcAccountManager.OnPostListener onSignOut = new OcAccountManager.OnPostListener() {\r
++ @Override\r
++ public synchronized void onPostCompleted(List<OcHeaderOption> list,\r
++ OcRepresentation ocRepresentation) {\r
++ logMessage("signOut was successful");\r
++ runOnUiThread(new Runnable() {\r
++ public void run() {\r
++ signIn.setEnabled(true);\r
++ signOut.setEnabled(false);\r
++ btnEnableRemoteService.setEnabled(false);\r
++ }\r
++ });\r
++\r
++ }\r
++\r
++ @Override\r
++ public synchronized void onPostFailed(Throwable throwable) {\r
++ logMessage("Failed to signOut");\r
++ if (throwable instanceof OcException) {\r
++ OcException ocEx = (OcException) throwable;\r
++ Log.e(TAG, ocEx.toString());\r
++ ErrorCode errCode = ocEx.getErrorCode();\r
++ logMessage("Error code: " + errCode);\r
++ if (ErrorCode.UNAUTHORIZED_REQ != errCode) {\r
++ RefreshToken();\r
++ }\r
++ }\r
++ }\r
++ };\r
+ @Override\r
+ public void onPostCompleted(List<OcHeaderOption> ocHeaderOptions, OcRepresentation ocRepresentation) {\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void onPostFailed(Throwable throwable) {\r
+ \r
+ }\r
+ private void signIn() {\r
+ try {\r
+ if(mAccountManager==null)\r
+ {\r
+ mAccountManager = OcPlatform.constructAccountManagerObject(CIServer,\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));\r
+ }\r
+ \r
+ mAccountManager.signIn(mUserID, mAccessToken, onSignIn);\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ private void signOut() {\r
+ try {\r
+ logMessage("signOut");\r
+ if(mAccountManager==null)\r
+ {\r
+ try {\r
+ mAccountManager = OcPlatform.constructAccountManagerObject(CIServer,\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
++ mAccountManager.signOut(mAccessToken, onSignOut);\r
+ signIn.setEnabled(false);\r
+ signUp.setEnabled(true);\r
+ btnEnableRemoteService.setEnabled(false);\r
+ logMessage("signOut Successful");\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ private void signUp() {\r
+ try {\r
+ mAccountManager = OcPlatform.constructAccountManagerObject(CIServer,\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ Intent intentLogin = new Intent(this,LoginActivity.class);\r
+ startActivityForResult(intentLogin, REQUEST_LOGIN);\r
+ }\r
+ @Override\r
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {\r
+ super.onActivityResult(requestCode, resultCode, data);\r
+ if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_LOGIN) {\r
+ mAuthCode = data.getStringExtra("authCode");\r
+ mAuthProvider = data.getStringExtra("authProvider");\r
+ logMessage("authCode: " + mAuthCode);\r
+ logMessage("authProvider: " + mAuthProvider);\r
+ \r
+ try {\r
+ mAccountManager = OcPlatform.constructAccountManagerObject(CIServer,\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));\r
+ logMessage("Calling signup API");\r
+ \r
+ mAccountManager.signUp(mAuthProvider, mAuthCode, onSignUp);\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+ \r
+ OcResource.OnPostListener onRefreshTokenPost = new OcResource.OnPostListener() {\r
+ @Override\r
+ public void onPostCompleted(List<OcHeaderOption> list, OcRepresentation ocRepresentation) {\r
+ logMessage("RefreshToken Completed.");\r
+ try {\r
+ mAccessToken = ocRepresentation.getValue("accesstoken");\r
+ mRefreshtoken = ocRepresentation.getValue("refreshtoken");\r
+ }\r
+ catch (OcException e)\r
+ {\r
+ e.printStackTrace();\r
+ }\r
+ signIn();\r
+ }\r
+ \r
+ @Override\r
+ public void onPostFailed(Throwable throwable) {\r
+ logMessage("RefreshToken failed.");\r
+ Log.d(TAG, "onRefreshTokenPost failed..");\r
+ }\r
+ };\r
+ public void RefreshToken() {\r
+ try {\r
+ OcResource authResource = OcPlatform.constructResourceObject(CIServer, "/.well-known/ocf/account/tokenrefresh",\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP, OcConnectivityType.CT_IP_USE_V4),\r
+ false, Arrays.asList("oic.wk.account"), Arrays.asList(OcPlatform.DEFAULT_INTERFACE));\r
+ OcRepresentation rep = new OcRepresentation();\r
+ \r
+ showToast("RefreshToken in progress..");\r
+ logMessage("RefreshToken in progress..");\r
+ rep.setValue("di", deviceID);\r
+ rep.setValue("granttype", "refresh_token");\r
+ rep.setValue("refreshtoken", mRefreshtoken);\r
+ rep.setValue("uid", mUserID);\r
+ authResource.post(rep, new HashMap<String, String>(), onRefreshTokenPost);\r
+ }\r
+ catch(OcException e)\r
+ {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ Log.d(TAG, "No error while executing login");\r
+ }\r
}\r
\r
TvLog.setText("");\r
}\r
- break; }\r
+ break;\r
+ case R.id.signup: {\r
+ if(isStarted == false) {\r
+ Log.e(TAG, "Fail to Sign Up");\r
+ showToast("Start ProviderService First");\r
+ break;\r
+ }\r
+ TvLog.append("Initiating SignUp\n");\r
+ signUp();\r
+ }\r
+ break;\r
+ case R.id.signin: {\r
+ if(isStarted == false) {\r
+ Log.e(TAG, "Fail to Sign In");\r
+ showToast("Start ProviderService First");\r
+ break;\r
+ }\r
+ TvLog.append("Initiating SignIn\n");\r
+ signIn();\r
+ }\r
+ break;\r
+ case R.id.signout: {\r
+ if(isStarted == false) {\r
+ Log.e(TAG, "Fail to Sign out");\r
+ showToast("Start ProviderService First");\r
+ break;\r
+ }\r
+ TvLog.append("Initiating SignOut\n");\r
+ signOut();\r
+ }\r
+ break;\r
+ case R.id.remoteService: {\r
+ remoteService.setEnabled(false);\r
+ if(isStarted == false) {\r
+ Log.e(TAG, "Fail to Enable/Disable RemoteService");\r
+ showToast("Start ProviderService First");\r
+ break;\r
+ }\r
+ if(gRemoteService){\r
+ TvLog.append("Enable Remote Service\n");\r
+ int result = mProviderSample.EnableRemoteService(RemoteAddress);\r
+ remoteService.setText(R.string.disableRemoteService);\r
+ gRemoteService = false;\r
+ remoteService.setEnabled(true);\r
+ TvLog.append("EnableRemoteService Result : " + result + "\n");\r
+ }\r
+ else {\r
+ TvLog.append("Disable Remote Service\n");\r
+ int result = mProviderSample.DisableRemoteService(RemoteAddress);\r
+ remoteService.setText(R.string.enableRemoteService);\r
+ gRemoteService = true;\r
+ remoteService.setEnabled(true);\r
+ TvLog.append("DisableRemoteService Result : " + result + "\n");\r
+ }\r
+ }\r
+ break;\r
+ case R.id.subscribeMQService: {\r
+ if(isStarted == false) {\r
+ Log.e(TAG, "Fail to SubscribeMQService");\r
+ showToast("Start ProviderService First");\r
+ break;\r
+ }\r
+ int result = mProviderSample.subscribeMQService(MQCloudAddress, MQCloudTopic);\r
+ TvLog.append("SubscribeMQService Result : " + result + "\n");\r
+ }\r
+ break;\r
+ }\r
+ }\r
+ };\r
+ public void logMessage(final String text) {\r
+ runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ Message msg = new Message();\r
+ msg.obj = text;\r
+ TvLog.append(text + "\n");\r
+ }\r
+ });\r
+ Log.i(TAG, text);\r
+ }\r
+ OcAccountManager.OnPostListener onSignUp = new OcAccountManager.OnPostListener() {\r
+ @Override\r
+ public synchronized void onPostCompleted(List<OcHeaderOption> list,\r
+ OcRepresentation ocRepresentation) {\r
+ logMessage("signUp was successful");\r
+ runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ signIn.setEnabled(true);\r
+ signUp.setEnabled(false);\r
+ }\r
+ });\r
+ try {\r
+ mUserID = ocRepresentation.getValue("uid");\r
+ mAccessToken = ocRepresentation.getValue("accesstoken");\r
+ mRefreshtoken = ocRepresentation.getValue("refreshtoken");\r
+ \r
+ logMessage("\tuserID: " + mUserID);\r
+ logMessage("\taccessToken: " + mAccessToken);\r
+ logMessage("\trefreshToken: " + mRefreshtoken);\r
+ \r
+ if (ocRepresentation.hasAttribute("expiresin")) {\r
+ int expiresIn = ocRepresentation.getValue("expiresin");\r
+ logMessage("\texpiresIn: " + expiresIn);\r
+ }\r
+ } catch (OcException e) {\r
+ Log.e(TAG, e.toString());\r
+ }\r
+ }\r
+ \r
+ \r
+ @Override\r
+ public synchronized void onPostFailed(Throwable throwable) {\r
+ logMessage("Failed to signUp");\r
+ if (throwable instanceof OcException) {\r
+ OcException ocEx = (OcException) throwable;\r
+ Log.e(TAG, ocEx.toString());\r
+ ErrorCode errCode = ocEx.getErrorCode();\r
+ logMessage("Error code: " + errCode);\r
+ }\r
+ }\r
+ };\r
+ OcAccountManager.OnPostListener onSignIn = new OcAccountManager.OnPostListener() {\r
+ @Override\r
+ public synchronized void onPostCompleted(List<OcHeaderOption> list,\r
+ OcRepresentation ocRepresentation) {\r
+ logMessage("signIn was successful");\r
+ runOnUiThread(new Runnable() {\r
+ public void run() {\r
+ signIn.setEnabled(false);\r
+ signOut.setEnabled(true);\r
+ remoteService.setEnabled(true);\r
+ }\r
+ });\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public synchronized void onPostFailed(Throwable throwable) {\r
+ logMessage("Failed to signIn");\r
+ if (throwable instanceof OcException) {\r
+ OcException ocEx = (OcException) throwable;\r
+ Log.e(TAG, ocEx.toString());\r
+ ErrorCode errCode = ocEx.getErrorCode();\r
+ logMessage("Error code: " + errCode);\r
+ if (ErrorCode.UNAUTHORIZED_REQ != errCode) {\r
+ RefreshToken();\r
+ }\r
+ }\r
+ }\r
+ };\r
++ OcAccountManager.OnPostListener onSignOut = new OcAccountManager.OnPostListener() {\r
++ @Override\r
++ public synchronized void onPostCompleted(List<OcHeaderOption> list,\r
++ OcRepresentation ocRepresentation) {\r
++ logMessage("signOut was successful");\r
++ runOnUiThread(new Runnable() {\r
++ public void run() {\r
++ signIn.setEnabled(true);\r
++ signOut.setEnabled(false);\r
++ remoteService.setEnabled(false);\r
++ }\r
++ });\r
++\r
++ }\r
++\r
++ @Override\r
++ public synchronized void onPostFailed(Throwable throwable) {\r
++ logMessage("Failed to signOut");\r
++ if (throwable instanceof OcException) {\r
++ OcException ocEx = (OcException) throwable;\r
++ Log.e(TAG, ocEx.toString());\r
++ ErrorCode errCode = ocEx.getErrorCode();\r
++ logMessage("Error code: " + errCode);\r
++ if (ErrorCode.UNAUTHORIZED_REQ != errCode) {\r
++ RefreshToken();\r
++ }\r
++ }\r
++ }\r
++ };\r
+ @Override\r
+ public void onPostCompleted(List<OcHeaderOption> ocHeaderOptions, OcRepresentation ocRepresentation) {\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void onPostFailed(Throwable throwable) {\r
+ \r
+ }\r
+ private void signIn() {\r
+ try {\r
+ if(mAccountManager==null)\r
+ {\r
+ mAccountManager = OcPlatform.constructAccountManagerObject(CIServer,\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));\r
+ }\r
+ \r
+ mAccountManager.signIn(mUserID, mAccessToken, onSignIn);\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ private void signOut() {\r
+ try {\r
+ logMessage("signOut");\r
+ if(mAccountManager==null)\r
+ {\r
+ try {\r
+ mAccountManager = OcPlatform.constructAccountManagerObject(CIServer,\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
- mAccountManager.signOut(this);\r
++ mAccountManager.signOut(mAccessToken, onSignOut);\r
+ signIn.setEnabled(false);\r
+ signUp.setEnabled(true);\r
+ remoteService.setEnabled(false);\r
+ logMessage("signOut Successful");\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ \r
+ private void signUp() {\r
+ try {\r
+ mAccountManager = OcPlatform.constructAccountManagerObject(CIServer,\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ Intent intentLogin = new Intent(this, LoginActivity.class);\r
+ startActivityForResult(intentLogin, REQUEST_LOGIN);\r
+ }\r
+ @Override\r
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {\r
+ super.onActivityResult(requestCode, resultCode, data);\r
+ if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_LOGIN) {\r
+ mAuthCode = data.getStringExtra("authCode");\r
+ mAuthProvider = data.getStringExtra("authProvider");\r
+ logMessage("authCode: " + mAuthCode);\r
+ logMessage("authProvider: " + mAuthProvider);\r
+ \r
+ try {\r
+ mAccountManager = OcPlatform.constructAccountManagerObject(CIServer,\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP));\r
+ logMessage("Calling signup API");\r
+ \r
+ mAccountManager.signUp(mAuthProvider, mAuthCode, onSignUp);\r
+ } catch (OcException e) {\r
+ e.printStackTrace();\r
}\r
+ }\r
+ }\r
+ \r
+ OcResource.OnPostListener onRefreshTokenPost = new OcResource.OnPostListener() {\r
+ @Override\r
+ public void onPostCompleted(List<OcHeaderOption> list, OcRepresentation ocRepresentation) {\r
+ logMessage("RefreshToken Completed.");\r
+ try {\r
+ mAccessToken = ocRepresentation.getValue("accesstoken");\r
+ mRefreshtoken = ocRepresentation.getValue("refreshtoken");\r
+ }\r
+ catch (OcException e)\r
+ {\r
+ e.printStackTrace();\r
+ }\r
+ signIn();\r
+ }\r
+ \r
+ @Override\r
+ public void onPostFailed(Throwable throwable) {\r
+ logMessage("RefreshToken failed.");\r
+ Log.d(TAG, "onRefreshTokenPost failed..");\r
+ }\r
};\r
+ public void RefreshToken() {\r
+ try {\r
+ OcResource authResource = OcPlatform.constructResourceObject(CIServer, "/.well-known/ocf/account/tokenrefresh",\r
+ EnumSet.of(OcConnectivityType.CT_ADAPTER_TCP, OcConnectivityType.CT_IP_USE_V4),\r
+ false, Arrays.asList("oic.wk.account"), Arrays.asList(OcPlatform.DEFAULT_INTERFACE));\r
+ OcRepresentation rep = new OcRepresentation();\r
+ \r
+ showToast("RefreshToken in progress..");\r
+ logMessage("RefreshToken in progress..");\r
+ rep.setValue("di", deviceID);\r
+ rep.setValue("granttype", "refresh_token");\r
+ rep.setValue("refreshtoken", mRefreshtoken);\r
+ rep.setValue("uid", mUserID);\r
+ authResource.post(rep, new HashMap<String, String>(), onRefreshTokenPost);\r
+ }\r
+ catch(OcException e)\r
+ {\r
+ e.printStackTrace();\r
+ }\r
+ \r
+ Log.d(TAG, "No error while executing login");\r
+ }\r
}\r
\r