From 8de9a702d780a0078b8ad2937f23808fd21bf43b Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Tue, 20 Jan 2015 13:48:20 +0530 Subject: [PATCH] added authentication response handler, comment fixes --- .../cdm/agent/AuthenticationActivity.java | 245 ++++++++---------- .../src/org/wso2/cdm/agent/ServerDetails.java | 62 +++-- .../org/wso2/cdm/agent/utils/Constant.java | 27 ++ 3 files changed, 171 insertions(+), 163 deletions(-) create mode 100644 product/modules/agents/android/client/src/org/wso2/cdm/agent/utils/Constant.java diff --git a/product/modules/agents/android/client/src/org/wso2/cdm/agent/AuthenticationActivity.java b/product/modules/agents/android/client/src/org/wso2/cdm/agent/AuthenticationActivity.java index 662052662..c47ba6167 100644 --- a/product/modules/agents/android/client/src/org/wso2/cdm/agent/AuthenticationActivity.java +++ b/product/modules/agents/android/client/src/org/wso2/cdm/agent/AuthenticationActivity.java @@ -28,6 +28,7 @@ import org.wso2.cdm.agent.proxy.IdentityProxy; import org.wso2.cdm.agent.services.AlarmReceiver; import org.wso2.cdm.agent.utils.CommonDialogUtils; import org.wso2.cdm.agent.utils.CommonUtilities; +import org.wso2.cdm.agent.utils.Constant; import org.wso2.cdm.agent.utils.HTTPConnectorUtils; import org.wso2.cdm.agent.utils.Preference; import org.wso2.cdm.agent.utils.ServerUtils; @@ -66,24 +67,26 @@ import com.actionbarsherlock.view.MenuItem; import com.google.android.gcm.GCMRegistrar; /** - * Activity that captures username, password and device ownership details + * Activity that captures username, password and device ownership details. */ public class AuthenticationActivity extends SherlockActivity implements APIAccessCallBack, APIResultCallBack { + private String TAG = AuthenticationActivity.class.getSimpleName(); - Button authenticate; - EditText username; - EditText txtDomain; - EditText password; + Button btnRegister; + EditText etUsername; + EditText etDomain; + EditText etPassword; RadioButton radioBYOD, radioCOPE; String deviceType; Context context; - String senderId = ""; - String usernameForRegister = ""; + String senderId; + String usernameForRegister; String usernameVal; String passwordVal; + String domain; ProgressDialog progressDialog; AlertDialog.Builder alertDialog; @@ -103,21 +106,21 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces context = AuthenticationActivity.this; deviceType = getResources().getString(R.string.device_enroll_type_byod); - txtDomain = (EditText) findViewById(R.id.txtDomain); - username = (EditText) findViewById(R.id.username); - password = (EditText) findViewById(R.id.editText2); + etDomain = (EditText) findViewById(R.id.etDomain); + etUsername = (EditText) findViewById(R.id.etUsername); + etPassword = (EditText) findViewById(R.id.etPassword); radioBYOD = (RadioButton) findViewById(R.id.radioBYOD); radioCOPE = (RadioButton) findViewById(R.id.radioCOPE); - txtDomain.setFocusable(true); - txtDomain.requestFocus(); - authenticate = (Button) findViewById(R.id.btnRegister); - authenticate.setEnabled(false); - authenticate.setOnClickListener(onClickAuthenticate); + etDomain.setFocusable(true); + etDomain.requestFocus(); + btnRegister = (Button) findViewById(R.id.btnRegister); + btnRegister.setEnabled(false); + btnRegister.setOnClickListener(onClickAuthenticate); // change button color background till user enters a valid input - authenticate.setBackground(getResources().getDrawable(R.drawable.btn_grey)); - authenticate.setTextColor(getResources().getColor(R.color.black)); + btnRegister.setBackground(getResources().getDrawable(R.drawable.btn_grey)); + btnRegister.setTextColor(getResources().getColor(R.color.black)); - username.addTextChangedListener(new TextWatcher() { + etUsername.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @@ -133,7 +136,7 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces } }); - password.addTextChangedListener(new TextWatcher() { + etPassword.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @@ -155,18 +158,14 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces @Override public void onClick(View view) { - if (username.getText() != null && !username.getText().toString().trim().equals("") && - password.getText() != null && !password.getText().toString().trim().equals("")) { - - passwordVal = password.getText().toString().trim(); - if (txtDomain.getText() != null && - !txtDomain.getText().toString().trim().equals("")) { - usernameVal = - username.getText().toString().trim() + "@" + - txtDomain.getText().toString().trim(); - - } else { - usernameVal = username.getText().toString().trim(); + if (etUsername.getText() != null && + !etUsername.getText().toString().trim().equals("") && + etPassword.getText() != null && !etPassword.getText().toString().trim().equals("")) { + + passwordVal = etPassword.getText().toString().trim(); + usernameVal = etUsername.getText().toString().trim(); + if (etDomain.getText() != null && !etDomain.getText().toString().trim().equals("")) { + usernameVal += "@" + etDomain.getText().toString().trim(); } if (radioBYOD.isChecked()) { @@ -174,22 +173,24 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces } else { deviceType = getResources().getString(R.string.device_enroll_type_cope); } + StringBuilder messageBuilder = new StringBuilder(); + messageBuilder.append(getResources().getString(R.string.dialog_init_middle)); + messageBuilder.append(" "); + messageBuilder.append(deviceType); + messageBuilder.append(" "); + messageBuilder.append(getResources().getString(R.string.dialog_init_end)); alertDialog = CommonDialogUtils.getAlertDialogWithTwoButtonAndTitle(context, getResources().getString(R.string.dialog_init_device_type), - getResources().getString(R.string.dialog_init_middle) + - " " + - deviceType + - " " + - getResources().getString(R.string.dialog_init_end), + messageBuilder.toString(), getResources().getString(R.string.yes), - getResources().getString(R.string.no), dialogClickListener, dialogClickListener); alertDialog.show(); } else { - if (username.getText() != null && !username.getText().toString().trim().equals("")) { + if (etUsername.getText() != null && + !etUsername.getText().toString().trim().equals("")) { Toast.makeText(context, getResources().getString(R.string.toast_error_password), Toast.LENGTH_LONG).show(); @@ -250,14 +251,14 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces Map requestParametres = new HashMap(); - requestParametres.put("username", + requestParametres.put(Constant.USERNAME, usernameVal); - requestParametres.put("password", + requestParametres.put(Constant.PASSWORD, passwordVal); response = HTTPConnectorUtils.postData(context, CommonUtilities.SERVER_URL + - CommonUtilities.SERVER_AUTHENTICATION_ENDPOINT, + CommonUtilities.SERVER_AUTHENTICATION_ENDPOINT, requestParametres); return response; } @@ -274,55 +275,7 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces @Override protected void onPostExecute(Map result) { - JSONObject response = - null; - if (result != null) { - String responseStatus = - result.get("status"); - try { - if (responseStatus != null) { - if (responseStatus.equalsIgnoreCase(CommonUtilities.REQUEST_SUCCESSFUL)) { - response = - new JSONObject( - result.get("response")); - senderId = - response.getString("senderId"); - getLicense(); - } else if (responseStatus.equalsIgnoreCase(CommonUtilities.UNAUTHORIZED_ACCESS)) { - CommonDialogUtils.stopProgressDialog(progressDialog); - alertDialog = - CommonDialogUtils.getAlertDialogWithOneButtonAndTitle(context, - getResources().getString(R.string.title_head_authentication_error), - getResources().getString(R.string.error_authentication_failed), - getResources().getString(R.string.button_ok), - dialogClickListener); - } else if (responseStatus.trim() - .equals(CommonUtilities.INTERNAL_SERVER_ERROR)) { - CommonDialogUtils.stopProgressDialog(progressDialog); - showInternalServerErrorMessage(); - - } else { - Log.e(TAG, - "Status: " + - responseStatus); - showAuthCommonErrorMessage(); - } - } else { - Log.e(TAG, - "The value of status is null in authenticate()"); - showAuthCommonErrorMessage(); - } - - } catch (JSONException e) { - Log.e(TAG, - e.getMessage()); - showAuthCommonErrorMessage(); - } - } else { - Log.e(TAG, - "The result is null in authenticate()"); - showAuthCommonErrorMessage(); - } + authenticateResponse(result); } @@ -331,14 +284,52 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces mLicenseTask.execute(); } + + /** + * Handles the response received from server for the authentication request. + * @param result Received response from server. + */ + private void authenticateResponse(Map result){ + if (result != null) { + String responseStatus = + result.get(Constant.STATUS); + if (responseStatus != null) { + if (responseStatus.equalsIgnoreCase(CommonUtilities.REQUEST_SUCCESSFUL)) { + getLicense(); + } else if (responseStatus.equalsIgnoreCase(CommonUtilities.UNAUTHORIZED_ACCESS)) { + CommonDialogUtils.stopProgressDialog(progressDialog); + alertDialog = + CommonDialogUtils.getAlertDialogWithOneButtonAndTitle(context, + getResources().getString(R.string.title_head_authentication_error), + getResources().getString(R.string.error_authentication_failed), + getResources().getString(R.string.button_ok), + dialogClickListener); + } else if (responseStatus.trim() + .equals(CommonUtilities.INTERNAL_SERVER_ERROR)) { + Log.e(TAG, "Error: Internal server error"); + showInternalServerErrorMessage(); + + } else { + Log.e(TAG, "Status: " + responseStatus); + showAuthCommonErrorMessage(); + } + } else { + Log.e(TAG, "The value of status is null in authenticating"); + showAuthCommonErrorMessage(); + } + + } else { + Log.e(TAG, "The result is null in authenticating"); + showAuthCommonErrorMessage(); + } + } /** * Initialize get device license agreement. Check if the user has already - * agreed - * to license agreement + * agreed to license agreement */ private void getLicense() { - String isAgreed = + String licenseAgreedResponse = Preference.get(context, getResources().getString(R.string.shared_pref_isagreed)); String type = @@ -347,9 +338,7 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces // No need to display license for COPE devices if (type.trim().equals(getResources().getString(R.string.device_enroll_type_byod))) { - if (isAgreed == null) { - Map requestParams = new HashMap(); - requestParams.put("domain", txtDomain.getText().toString().trim()); + if (licenseAgreedResponse == null) { // Get License OnCancelListener cancelListener = new OnCancelListener() { @@ -361,7 +350,6 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces getResources().getString(R.string.error_enrollment_failed), getResources().getString(R.string.button_ok), null); - } }; @@ -403,7 +391,7 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces response = HTTPConnectorUtils.postData(context, CommonUtilities.SERVER_URL + - CommonUtilities.LICENSE_ENDPOINT, + CommonUtilities.LICENSE_ENDPOINT, null); return response; } @@ -433,12 +421,11 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces private void manipulateLicenseResponse(Map result) { String responseStatus; CommonDialogUtils.stopProgressDialog(progressDialog); - String licenseAgreement = ""; if (result != null) { responseStatus = result.get(CommonUtilities.STATUS_KEY); if (responseStatus.equals(CommonUtilities.REQUEST_SUCCESSFUL)) { - licenseAgreement = result.get("response"); + String licenseAgreement = result.get(Constant.RESPONSE); if (licenseAgreement != null) { Preference.put(context, getResources().getString(R.string.shared_pref_eula), @@ -488,7 +475,6 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces String html = "" + message + ""; String mime = "text/html"; String encoding = "utf-8"; - web.getSettings().setJavaScriptEnabled(true); web.loadDataWithBaseURL(null, html, mime, encoding, null); Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); @@ -545,7 +531,6 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces editor.putString(getResources().getString(R.string.shared_pref_registered), "0"); editor.putString(getResources().getString(R.string.shared_pref_ip), ""); editor.commit(); - // finish(); Intent intentIP = new Intent(AuthenticationActivity.this, ServerDetails.class); intentIP.putExtra(getResources().getString(R.string.intent_extra_from_activity), @@ -567,12 +552,6 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces dialog.cancel(); } }); - /* - * builder1.setNegativeButton("No", new - * DialogInterface.OnClickListener() { public void - * onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); - */ - AlertDialog alert = builder.create(); alert.show(); } @@ -588,12 +567,6 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces dialog.dismiss(); } }); - /* - * builder1.setNegativeButton("No", new - * DialogInterface.OnClickListener() { public void - * onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); - */ - AlertDialog alert = builder.create(); alert.show(); } @@ -611,21 +584,21 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces String serverURL = CommonUtilities.SERVER_PROTOCOL + serverIP + ":" + CommonUtilities.SERVER_PORT + CommonUtilities.OAUTH_ENDPOINT; - if (txtDomain.getText() != null && !txtDomain.getText().toString().trim().equals("")) { + if (etDomain.getText() != null && !etDomain.getText().toString().trim().equals("")) { usernameForRegister = - username.getText().toString().trim() + "@" + - txtDomain.getText().toString().trim(); + etUsername.getText().toString().trim() + "@" + + etDomain.getText().toString().trim(); IdentityProxy.getInstance().init(clientKey, clientSecret, usernameForRegister, - password.getText().toString().trim(), serverURL, + etPassword.getText().toString().trim(), serverURL, AuthenticationActivity.this, this.getApplicationContext()); } else { - usernameForRegister = username.getText().toString().trim(); + usernameForRegister = etUsername.getText().toString().trim(); IdentityProxy.getInstance().init(clientKey, clientSecret, usernameForRegister, - password.getText().toString().trim(), serverURL, + etPassword.getText().toString().trim(), serverURL, AuthenticationActivity.this, this.getApplicationContext()); } @@ -635,19 +608,19 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces boolean isReady = false; - if (username.getText().toString().length() >= 1 && - password.getText().toString().length() >= 1) { + if (etUsername.getText().toString().length() >= 1 && + etPassword.getText().toString().length() >= 1) { isReady = true; } if (isReady) { - authenticate.setBackground(getResources().getDrawable(R.drawable.btn_orange)); - authenticate.setTextColor(getResources().getColor(R.color.white)); - authenticate.setEnabled(true); + btnRegister.setBackground(getResources().getDrawable(R.drawable.btn_orange)); + btnRegister.setTextColor(getResources().getColor(R.color.white)); + btnRegister.setEnabled(true); } else { - authenticate.setBackground(getResources().getDrawable(R.drawable.btn_grey)); - authenticate.setTextColor(getResources().getColor(R.color.black)); - authenticate.setEnabled(false); + btnRegister.setBackground(getResources().getDrawable(R.drawable.btn_grey)); + btnRegister.setTextColor(getResources().getColor(R.color.black)); + btnRegister.setEnabled(false); } } @@ -699,12 +672,12 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces @Override public void onClick(DialogInterface dialog, int which) { - username.setText(CommonUtilities.EMPTY_STRING); - password.setText(CommonUtilities.EMPTY_STRING); - txtDomain.setText(CommonUtilities.EMPTY_STRING); - authenticate.setEnabled(false); - authenticate.setBackground(getResources().getDrawable(R.drawable.btn_grey)); - authenticate.setTextColor(getResources().getColor(R.color.black)); + etUsername.setText(CommonUtilities.EMPTY_STRING); + etPassword.setText(CommonUtilities.EMPTY_STRING); + etDomain.setText(CommonUtilities.EMPTY_STRING); + btnRegister.setEnabled(false); + btnRegister.setBackground(getResources().getDrawable(R.drawable.btn_grey)); + btnRegister.setTextColor(getResources().getColor(R.color.black)); } }; @@ -766,7 +739,7 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces editor.commit(); Map requestParams = new HashMap(); - requestParams.put("domain", txtDomain.getText().toString().trim()); + requestParams.put("domain", etDomain.getText().toString().trim()); // Check network connection availability before calling the API. if (PhoneState.isNetworkAvailable(context)) { // Call get sender ID API. @@ -871,7 +844,7 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces responseStatus = result.get(CommonUtilities.STATUS_KEY); if (responseStatus.equals(CommonUtilities.REQUEST_SUCCESSFUL)) { try { - response = new JSONObject(result.get("response")); + response = new JSONObject(result.get(Constant.RESPONSE)); senderId = response.getString("sender_id"); mode = response.getString("notifier"); interval = (float) Float.parseFloat(response.getString("notifierInterval")); diff --git a/product/modules/agents/android/client/src/org/wso2/cdm/agent/ServerDetails.java b/product/modules/agents/android/client/src/org/wso2/cdm/agent/ServerDetails.java index 5c65322b5..a9e28245a 100644 --- a/product/modules/agents/android/client/src/org/wso2/cdm/agent/ServerDetails.java +++ b/product/modules/agents/android/client/src/org/wso2/cdm/agent/ServerDetails.java @@ -40,12 +40,12 @@ import org.wso2.cdm.agent.utils.Responce; */ public class ServerDetails extends Activity { - TextView serverIP; - Button startRegistration; + TextView evServerIP; + Button btnStartRegistration; Context context; DialogInterface.OnClickListener dialogClickListener; DeviceInfo info; - TextView severAddressLabel; + TextView tvSeverAddress; String senderID = null; ProgressDialog progressDialog; @@ -62,49 +62,51 @@ public class ServerDetails extends Activity { setContentView(R.layout.activity_settings); context = ServerDetails.this; info = new DeviceInfo(ServerDetails.this); - serverIP = (TextView) findViewById(R.id.etServerIP); - severAddressLabel = (TextView) findViewById(R.id.severAddressLabel); - startRegistration = (Button) findViewById(R.id.startRegistration); + evServerIP = (TextView) findViewById(R.id.evServerIP); + tvSeverAddress = (TextView) findViewById(R.id.tvSeverAddress); + btnStartRegistration = (Button) findViewById(R.id.btnStartRegistration); // Checking if the device meets minimum requirements Responce compatibility = info.isCompatible(); if (!compatibility.getCode()) { - startRegistration.setVisibility(View.GONE); - severAddressLabel.setVisibility(View.GONE); - serverIP.setVisibility(View.GONE); + btnStartRegistration.setVisibility(View.GONE); + tvSeverAddress.setVisibility(View.GONE); + evServerIP.setVisibility(View.GONE); alertDialog = - CommonDialogUtils.getAlertDialogWithOneButtonAndTitle(context.getApplicationContext(), + CommonDialogUtils.getAlertDialogWithOneButtonAndTitle(context, getResources().getString(R.string.error_authorization_failed), getResources().getString(compatibility.getDescriptionResourceID()), getResources().getString(R.string.button_ok), onRootedClickListner); } else { - startRegistration.setVisibility(View.VISIBLE); - serverIP.setVisibility(View.VISIBLE); + btnStartRegistration.setVisibility(View.VISIBLE); + evServerIP.setVisibility(View.VISIBLE); String ipSaved = Preference.get(context.getApplicationContext(), getResources().getString(R.string.shared_pref_ip)); - regId = Preference.get(context.getApplicationContext().getApplicationContext(), getResources().getString(R.string.shared_pref_regId)); + regId = Preference.get(context.getApplicationContext(), getResources().getString(R.string.shared_pref_regId)); - //heck if we have the IP saved previously. + //check if we have the IP saved previously. if (ipSaved != null) { - serverIP.setText(ipSaved); + evServerIP.setText(ipSaved); CommonUtilities.setServerURL(ipSaved); startAuthenticationActivity(); } else { - serverIP.setText(CommonUtilities.SERVER_IP); + evServerIP.setText(CommonUtilities.SERVER_IP); } // on click handler for start registration - startRegistration.setOnClickListener(new OnClickListener() { + btnStartRegistration.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - AlertDialog.Builder builder = new AlertDialog.Builder(ServerDetails.this); - builder.setMessage(getResources().getString(R.string.dialog_init_confirmation) + - " " + - serverIP.getText().toString() + - " " + - getResources().getString(R.string.dialog_init_end_general)) + AlertDialog.Builder alertBuilder = new AlertDialog.Builder(ServerDetails.this); + StringBuilder messageBuilder = new StringBuilder(); + messageBuilder.append(getResources().getString(R.string.dialog_init_confirmation)); + messageBuilder.append(" "); + messageBuilder.append(evServerIP.getText().toString()); + messageBuilder.append(" "); + messageBuilder.append(getResources().getString(R.string.dialog_init_end_general)); + alertBuilder.setMessage(messageBuilder.toString()) .setPositiveButton(getResources().getString(R.string.yes), dialogClickListener) .setNegativeButton(getResources().getString(R.string.no), @@ -117,11 +119,11 @@ public class ServerDetails extends Activity { public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: - if (!serverIP.getText().toString().trim().equals("")) { - CommonUtilities.setServerURL(serverIP.getText().toString().trim()); + if (!evServerIP.getText().toString().trim().equals("")) { + CommonUtilities.setServerURL(evServerIP.getText().toString().trim()); Preference.put(context.getApplicationContext(), getResources().getString(R.string.shared_pref_ip), - serverIP.getText().toString().trim()); + evServerIP.getText().toString().trim()); startAuthenticationActivity(); } else { @@ -148,6 +150,9 @@ public class ServerDetails extends Activity { } }; + /** + * This method is called to open AuthenticationActivity. + */ private void startAuthenticationActivity() { Intent intent = new Intent(ServerDetails.this, AuthenticationActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); @@ -161,10 +166,13 @@ public class ServerDetails extends Activity { */ @Override protected void onDestroy() { - //Avoiding memory leaks by destroying context object context = null; super.onDestroy(); } + + + + // Old API manager communication code. // diff --git a/product/modules/agents/android/client/src/org/wso2/cdm/agent/utils/Constant.java b/product/modules/agents/android/client/src/org/wso2/cdm/agent/utils/Constant.java new file mode 100644 index 000000000..8e551e336 --- /dev/null +++ b/product/modules/agents/android/client/src/org/wso2/cdm/agent/utils/Constant.java @@ -0,0 +1,27 @@ +/** + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wso2.cdm.agent.utils; + +/** + * Constant values throughout the agent + */ +public class Constant { + public static final String USERNAME = "username"; + public static final String PASSWORD = "password"; + public static final String STATUS = "status"; + public static final String RESPONSE = "response"; + +}