Skip to content

Commit

Permalink
Fixed intermittent login screen prompting & Improved location listening
Browse files Browse the repository at this point in the history
  • Loading branch information
charithag committed Apr 25, 2017
1 parent 7144c27 commit 110c5be
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 147 deletions.
4 changes: 2 additions & 2 deletions client/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ android {
targetSdkVersion 23
multiDexEnabled true

versionCode 329
versionName "3.1.15"
versionCode 330
versionName "3.1.16"
}
buildTypes {
release {
Expand Down
2 changes: 1 addition & 1 deletion client/client/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

<application
android:name=".AgentApplication"
android:allowBackup="true"
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.KeyEvent;
Expand Down Expand Up @@ -70,20 +70,15 @@ public class AlreadyRegisteredActivity extends SherlockActivity implements APIRe
private static final int ACTIVATION_REQUEST = 47;
private String regId;
private Context context;
private Resources resources;
private ProgressDialog progressDialog;
private Button btnUnregister;
private TextView txtRegText;
private static final int TAG_BTN_UNREGISTER = 0;
private static final int TAG_BTN_RE_REGISTER = 2;
private boolean freshRegFlag = false;
private boolean isUnregisterBtnClicked = false;
private AlertDialog.Builder alertDialog;
private boolean isPollingStarted;
private DevicePolicyManager devicePolicyManager;
private ComponentName cdmDeviceAdmin;
private DeviceInfo info;
private RelativeLayout unregisterLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -96,8 +91,7 @@ protected void onCreate(Bundle savedInstanceState) {
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
cdmDeviceAdmin = new ComponentName(this, AgentDeviceAdminReceiver.class);
context = this;
resources = context.getResources();
info = new DeviceInfo(context);
DeviceInfo info = new DeviceInfo(context);
Bundle extras = getIntent().getExtras();

if (extras != null) {
Expand Down Expand Up @@ -131,7 +125,7 @@ protected void onCreate(Bundle savedInstanceState) {
btnUnregister = (Button) findViewById(R.id.btnUnreg);
btnUnregister.setTag(TAG_BTN_UNREGISTER);
btnUnregister.setOnClickListener(onClickListenerButtonClicked);
unregisterLayout = (RelativeLayout) findViewById(R.id.unregisterLayout);
RelativeLayout unregisterLayout = (RelativeLayout) findViewById(R.id.unregisterLayout);
if (Constants.HIDE_UNREGISTER_BUTTON) {
unregisterLayout.setVisibility(View.GONE);
}
Expand All @@ -158,24 +152,43 @@ protected void onCreate(Bundle savedInstanceState) {
110);
}
}

try {
int locationSetting = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
if (locationSetting == 0) {
Intent enableLocationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(enableLocationIntent);
Toast.makeText(context, R.string.msg_need_location, Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= 19) {
try {
int locationSetting = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
if (locationSetting == 0) {
Intent enableLocationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(enableLocationIntent);
Toast.makeText(context, R.string.msg_need_location, Toast.LENGTH_LONG).show();
}
} catch (Settings.SettingNotFoundException e) {
Log.w(TAG, "Location setting is not available on this device");
}
} catch (Settings.SettingNotFoundException e) {
Log.w(TAG, "Location setting is not available on this device");
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
protected void onDestroy(){
super.onDestroy();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 110) {
List<String> missingPermissions = new ArrayList<>();
for (int i =0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
missingPermissions.add(permissions[i]);
}
}
if (!missingPermissions.isEmpty()) {
Log.w(TAG, "Permissions not granted: " + missingPermissions.toString());
}
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(Constants.TOKEN_EXPIRED, Constants.SIGN_IN_NOTIFICATION_ID);
mNotificationManager.cancel(Constants.PERMISSION_MISSING, Constants.PERMISSION_MISSING_NOTIFICATION_ID);
}
}

Expand Down Expand Up @@ -341,8 +354,7 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
} else if (keyCode == KeyEvent.KEYCODE_HOME) {
loadHomeScreen();
return true;
}
else {
} else {
return super.onKeyDown(keyCode, event);
}
}
Expand All @@ -355,14 +367,16 @@ protected void onResume() {
Log.d(TAG, "Calling onResume");
}

try {
int locationSetting = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
if (locationSetting != 0) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(Constants.LOCATION_DISABLED, Constants.LOCATION_DISABLED_NOTIFICATION_ID);
if (Build.VERSION.SDK_INT >= 19) {
try {
int locationSetting = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
if (locationSetting != 0) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(Constants.LOCATION_DISABLED, Constants.LOCATION_DISABLED_NOTIFICATION_ID);
}
} catch (Settings.SettingNotFoundException e) {
Log.w(TAG, "Location setting is not available on this device");
}
} catch (Settings.SettingNotFoundException e) {
Log.w(TAG, "Location setting is not available on this device");
}

boolean isRegistered = Preference.getBoolean(context, Constants.PreferenceFlag.REGISTERED);
Expand Down Expand Up @@ -413,7 +427,7 @@ protected void onResume() {
* Displays an internal server error message to the user.
*/
private void displayInternalServerError() {
alertDialog = CommonDialogUtils.getAlertDialogWithOneButtonAndTitle(context,
AlertDialog.Builder alertDialog = CommonDialogUtils.getAlertDialogWithOneButtonAndTitle(context,
getResources().getString(R.string.title_head_connection_error),
getResources().getString(R.string.error_internal_server),
getResources().getString(R.string.button_ok),
Expand Down Expand Up @@ -476,6 +490,7 @@ public void onReceiveAPIResult(Map<String, String> result, int requestCode) {
*/

private void loadHomeScreen() {
finish();
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.text.Editable;
import android.text.TextWatcher;
Expand Down Expand Up @@ -92,40 +93,42 @@ public class AuthenticationActivity extends SherlockActivity implements APIAcces
private EditText etDomain;
private EditText etPassword;
private RadioButton radioBYOD;
private RadioButton radioCOPE;
private String deviceType;
private Context context;
private String username;
private String usernameVal;
private String passwordVal;
private TextView textViewWipeData;
private ProgressDialog progressDialog;
private LinearLayout loginLayout;
private boolean isReLogin = false;
private boolean isCloudLogin = false;

private DeviceInfo deviceInfo;
private static final String TAG = AuthenticationActivity.class.getSimpleName();
private static final String[] SUBSCRIBED_API = new String[]{"android"};
private ClientAuthenticator authenticator;
private Tenant currentTenant;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;

if (Constants.DEFAULT_HOST == null && Preference.getString(context, Constants.PreferenceFlag.IP) == null) {
Intent intent = new Intent(AuthenticationActivity.this, AlreadyRegisteredActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return;
}

setContentView(R.layout.activity_authentication);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.custom_sherlock_bar);
getSupportActionBar().setTitle(Constants.EMPTY_STRING);

context = this;
deviceInfo = new DeviceInfo(context);
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);
loginLayout = (LinearLayout) findViewById(R.id.errorLayout);
etDomain.setFocusable(true);
etDomain.requestFocus();
btnSignIn = (Button) findViewById(R.id.btnSignIn);
Expand All @@ -135,8 +138,10 @@ protected void onCreate(Bundle savedInstanceState) {
// change button color background till user enters a valid input
btnSignIn.setBackground(getResources().getDrawable(R.drawable.btn_grey));
btnSignIn.setTextColor(getResources().getColor(R.color.black));

radioBYOD = (RadioButton) findViewById(R.id.radioBYOD);
RadioButton radioCOPE = (RadioButton) findViewById(R.id.radioCOPE);
TextView textViewSignIn = (TextView) findViewById(R.id.textViewSignIn);
LinearLayout loginLayout = (LinearLayout) findViewById(R.id.errorLayout);

if (Preference.hasPreferenceKey(context, Constants.TOKEN_EXPIRED)) {
etDomain.setEnabled(false);
Expand All @@ -160,6 +165,14 @@ protected void onCreate(Bundle savedInstanceState) {
textViewSignIn.setText(R.string.txt_sign_in_cloud);
}

if (Preference.getBoolean(context, Constants.PreferenceFlag.DEVICE_ACTIVE) && !isReLogin) {
Intent intent = new Intent(AuthenticationActivity.this, AlreadyRegisteredActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
return;
}

TextView textViewSignUp = (TextView) findViewById(R.id.textViewSignUp);
if (!isReLogin && Constants.SIGN_UP_URL != null) {
Linkify.TransformFilter transformFilter = new Linkify.TransformFilter() {
Expand All @@ -178,14 +191,14 @@ public String transformUrl(Matcher match, String url) {
loginLayout.setVisibility(View.GONE);
}

if (Constants.DEFAULT_OWNERSHIP == Constants.OWNERSHIP_COSU) {
if (Constants.OWNERSHIP_COSU.equals(Constants.DEFAULT_OWNERSHIP)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startLockTask();
}
}

textViewWipeData = (TextView) this.findViewById(R.id.textViewWipeData);
if(Constants.DEFAULT_OWNERSHIP == Constants.OWNERSHIP_COSU && Constants.DISPLAY_WIPE_DEVICE_BUTTON){
TextView textViewWipeData = (TextView) this.findViewById(R.id.textViewWipeData);
if(Constants.OWNERSHIP_COSU.equals(Constants.DEFAULT_OWNERSHIP) && Constants.DISPLAY_WIPE_DEVICE_BUTTON){
textViewWipeData.setVisibility(View.VISIBLE);
textViewWipeData.setOnClickListener(new OnClickListener() {
@Override
Expand All @@ -197,9 +210,14 @@ public void onClick(View view) {
public void onClick(DialogInterface dialog, int whichButton) {
DevicePolicyManager devicePolicyManager = (DevicePolicyManager)
getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
devicePolicyManager.
wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE |
DevicePolicyManager.WIPE_RESET_PROTECTION_DATA);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
devicePolicyManager.
wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE |
DevicePolicyManager.WIPE_RESET_PROTECTION_DATA);
} else {
devicePolicyManager.
wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
}
}})
.setNegativeButton(android.R.string.no, null)
.show();
Expand Down Expand Up @@ -243,7 +261,7 @@ public void afterTextChanged(Editable s) {
equals(org.wso2.iot.agent.proxy.utils.Constants.Authenticator.MUTUAL_SSL_AUTHENTICATOR)) {

AuthenticatorFactory authenticatorFactory = new AuthenticatorFactory();
authenticator = authenticatorFactory.getClient(
ClientAuthenticator authenticator = authenticatorFactory.getClient(
org.wso2.iot.agent.proxy.utils.Constants.Authenticator.AUTHENTICATOR_IN_USE,
AuthenticationActivity.this, Constants.AUTHENTICATION_REQUEST_CODE);
authenticator.doAuthenticate();
Expand All @@ -270,12 +288,21 @@ public void afterTextChanged(Editable s) {
// This is added so that in case due to an agent customisation, if the authentication
// activity is called the AUTO_ENROLLMENT_BACKGROUND_SERVICE_ENABLED is set, the activity
// must be finished.
if (Constants.AUTO_ENROLLMENT_BACKGROUND_SERVICE_ENABLED == true) {
if (Constants.AUTO_ENROLLMENT_BACKGROUND_SERVICE_ENABLED) {
finish();
}

}

@Override
protected void onDestroy(){
super.onDestroy();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
}

private OnClickListener onClickAuthenticate = new OnClickListener() {

@Override
Expand Down Expand Up @@ -418,7 +445,6 @@ private void startAuthentication() {
if (CommonUtils.isNetworkAvailable(context)) {
String clientId = Preference.getString(context, Constants.CLIENT_ID);
String clientSecret = Preference.getString(context, Constants.CLIENT_SECRET);
String clientName;

if (clientId == null || clientSecret == null) {
String clientCredentials = Preference.getString(context, getResources().getString(R.string.shared_pref_client_credentials));
Expand Down Expand Up @@ -649,7 +675,9 @@ private void checkManifestPermissions(){
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
if(requestCode == 110){
getConfigurationsFromServer();
}
Expand Down Expand Up @@ -911,13 +939,13 @@ public void onClick(DialogInterface dialog, int id) {
* @param message Message text to be shown as the license.
* @param title Title of the license.
*/
private void showAgreement(final String message, String title) {
private void showAgreement(final String message, final String title) {
AuthenticationActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_terms_popup);
dialog.setTitle(Constants.EULA_TITLE);
dialog.setTitle(title);
dialog.setCancelable(false);

WebView webView = (WebView) dialog.findViewById(R.id.webview);
Expand Down Expand Up @@ -1165,9 +1193,6 @@ public void run() {

/**
* This method is used to retrieve consumer-key and consumer-secret.
*
* @return JSON formatted string.
* @throws AndroidAgentException
*/
private void getClientCredentials() {
String ipSaved = Constants.DEFAULT_HOST;
Expand Down
Loading

0 comments on commit 110c5be

Please sign in to comment.