Skip to content

Commit

Permalink
Merge pull request #195 from charithag/master
Browse files Browse the repository at this point in the history
 Fixes and optimizations for 3.3.1 release
  • Loading branch information
hasuniea authored Oct 23, 2018
2 parents e0e3d19 + 7ad12fc commit f043e9e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 27 deletions.
4 changes: 2 additions & 2 deletions client/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ android {
targetSdkVersion 25
multiDexEnabled true

versionCode 3030000
versionName "3.3.0"
versionCode 3030100
versionName "3.3.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.iot.agent.services;

import android.app.NotificationManager;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -729,18 +730,32 @@ public void onReceiveAPIResult(Map<String, String> result, int requestCode) {
}
performOperation(response);
}
if (Preference.getBoolean(context, Constants.TOKEN_EXPIRED)) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.cancel(Constants.TOKEN_EXPIRED, Constants.SIGN_IN_NOTIFICATION_ID);
}
Preference.removePreference(context, Constants.TOKEN_EXPIRED);
LocalNotification.startPolling(context);
}
} else if (Constants.Status.AUTHENTICATION_FAILED.equals(responseStatus) &&
org.wso2.iot.agent.proxy.utils.Constants.REFRESH_TOKEN_EXPIRED.equals(response)) {
Log.i(TAG, "Requesting credentials to obtain new token pair.");
LocalNotification.stopPolling(context);
Preference.putBoolean(context, Constants.TOKEN_EXPIRED, true);
CommonUtils.displayNotification(context,
R.drawable.ic_error_outline_white_24dp,
context.getResources().getString(R.string.title_need_to_sign_in),
context.getResources().getString(R.string.msg_need_to_sign_in),
AuthenticationActivity.class,
Constants.TOKEN_EXPIRED,
Constants.SIGN_IN_NOTIFICATION_ID);
int tokenFailureAttempts = Preference.getInt(context, Constants.TOKEN_FAILURE_ATTEMPTS);
if (tokenFailureAttempts > Constants.MAX_TOKEN_FAILURE_ATTEMPTS) {
Log.i(TAG, "Requesting credentials to obtain new token pair.");
LocalNotification.stopPolling(context);
Preference.putBoolean(context, Constants.TOKEN_EXPIRED, true);
CommonUtils.displayNotification(context,
R.drawable.ic_error_outline_white_24dp,
context.getResources().getString(R.string.title_need_to_sign_in),
context.getResources().getString(R.string.msg_need_to_sign_in),
AuthenticationActivity.class,
Constants.TOKEN_EXPIRED,
Constants.SIGN_IN_NOTIFICATION_ID);
Preference.removePreference(context, Constants.TOKEN_FAILURE_ATTEMPTS);
} else {
Preference.putInt(context, Constants.TOKEN_FAILURE_ATTEMPTS, ++tokenFailureAttempts);
}
}
}
isInCriticalPath = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location != null) {
broadcastLocation(location);
publishLocationInfo(location);
} else {
Log.w(TAG, "No last known location found");
}
Expand All @@ -160,7 +160,7 @@ public void onSuccess(Location location) {
}
}
if (location != null) {
broadcastLocation(location);
publishLocationInfo(location);
} else {
Log.w(TAG, "No last known location found");
}
Expand All @@ -173,7 +173,6 @@ private void broadcastLocation(Location location) {
broadcastIntent.setAction(Constants.LOCATION_UPDATE_BROADCAST_ACTION);
broadcastIntent.putExtra(Constants.Location.LOCATION, location);
sendBroadcast(broadcastIntent);
publishLocationInfo(location);
}

@Override
Expand Down Expand Up @@ -289,7 +288,7 @@ public void onLocationChanged(Location location) {
Log.d(TAG, "Location changed> lat:" + location.getLatitude()
+ " lon:" + location.getLongitude() + " provider:" + location.getProvider());
}
broadcastLocation(location);
publishLocationInfo(location);
}
}

Expand Down Expand Up @@ -329,6 +328,7 @@ private void publishLocationInfo(Location location) {
}
String locationPayload = getLocationPayload(location);
if (lastPublishedLocationTime < location.getTime()) {
broadcastLocation(location);
EventPayload eventPayload = new EventPayload();
eventPayload.setPayload(locationPayload);
eventPayload.setType(Constants.EventListeners.LOCATION_EVENT_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,10 @@ public void monitorPolicy(org.wso2.iot.agent.beans.Operation operation) throws A
op = operationsMapper.getOperation(op);
result.add(policyChecker.checkPolicyState(op));
}
operation.setStatus(resources.getString(R.string.operation_value_completed));
operation.setPayLoad(mapper.writeValueAsString(result));
resultBuilder.build(operation);
}
operation.setStatus(resources.getString(R.string.operation_value_completed));
operation.setPayLoad(mapper.writeValueAsString(result));
resultBuilder.build(operation);
} catch (IOException e) {
operation.setStatus(resources.getString(R.string.operation_value_error));
operation.setOperationResponse("Error in parsing policy monitor payload stream.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public class Constants {
public static final int FIRMWARE_DOWNLOAD_OPERATION_TIMEOUT = 6 * 60 * 1000;
//Is user consent required for file upload from device to server
public static final boolean REQUIRE_CONSENT_FOR_FILE_UPLOAD = BuildConfig.REQUIRE_CONSENT_FOR_FILE_UPLOAD;
public static final int MAX_TOKEN_FAILURE_ATTEMPTS = 10;

// This is used to skip the license
public static final boolean SKIP_LICENSE = BuildConfig.SKIP_LICENSE;
Expand Down Expand Up @@ -154,6 +155,7 @@ public class Constants {
public static final String USERNAME_PATTERN = "[user]";
public static final String EMM_DB = "emm_db";
public static final String TOKEN_EXPIRED = "token_expired";
public static final String TOKEN_FAILURE_ATTEMPTS = "token_failures";
public static final String PERMISSION_MISSING = "permission_missing";
public static final String LOCATION_DISABLED = "location_disabled";
public static final int SIGN_IN_NOTIFICATION_ID = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.wso2.iot.agent.proxy;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
Expand Down Expand Up @@ -47,9 +48,9 @@ public class IdentityProxy implements CallBack {
private APIAccessCallBack apiAccessCallBack;
private TokenCallBack tokenCallBack;
private int requestCode = 0;
private static volatile boolean IS_TOKEN_RENEWING = false;

private IdentityProxy() {

}

public static synchronized IdentityProxy getInstance() {
Expand All @@ -74,6 +75,7 @@ public void setAccessTokenURL(String accessTokenURL) {

@Override
public void receiveAccessToken(String status, String message, Token token) {
IS_TOKEN_RENEWING = false;
if (Constants.DEBUG_ENABLED && token != null) {
Log.d(TAG, "Receive Access Token: " + token.getAccessToken());
}
Expand All @@ -83,6 +85,7 @@ public void receiveAccessToken(String status, String message, Token token) {

@Override
public void receiveNewAccessToken(String status, String message, Token token) {
IS_TOKEN_RENEWING = false;
if (token != null) {
if (Constants.DEBUG_ENABLED) {
Log.d(TAG, "Using Access Token: " + token.getAccessToken());
Expand Down Expand Up @@ -112,16 +115,24 @@ public void init(CredentialInfo info, APIAccessCallBack apiAccessCallBack, Conte
if (this.context == null) {
this.context = context;
}
SharedPreferences mainPref = context.getSharedPreferences(Constants.APPLICATION_PACKAGE
, Context.MODE_PRIVATE);
SharedPreferences mainPref = context.getSharedPreferences(Constants.APPLICATION_PACKAGE,
Context.MODE_PRIVATE);
Editor editor = mainPref.edit();
editor.putString(Constants.CLIENT_ID, clientID);
editor.putString(Constants.CLIENT_SECRET, clientSecret);
editor.putString(Constants.TOKEN_ENDPOINT, info.getTokenEndPoint());
editor.apply();
setAccessTokenURL(info.getTokenEndPoint());
AccessTokenHandler accessTokenHandler = new AccessTokenHandler(info, this);
accessTokenHandler.obtainAccessToken();
long lastTokenRenewalAt = mainPref.getLong(Constants.LAST_TOKEN_RENEWAL, 0);
if (!IS_TOKEN_RENEWING
|| lastTokenRenewalAt + Constants.HttpClient.DEFAULT_TOKEN_TIME_OUT * 2 < System.currentTimeMillis()) {
IS_TOKEN_RENEWING = true;
editor = mainPref.edit();
editor.putLong(Constants.LAST_TOKEN_RENEWAL, System.currentTimeMillis());
editor.commit(); //Need to make sure pref is committed
AccessTokenHandler accessTokenHandler = new AccessTokenHandler(info, this);
accessTokenHandler.obtainAccessToken();
}
}

public void requestToken(Context context, TokenCallBack tokenCallBack, String clientID,
Expand Down Expand Up @@ -205,9 +216,20 @@ private void validateStoredToken() {
}
}

public void refreshToken() {
RefreshTokenHandler refreshTokenHandler = new RefreshTokenHandler(token);
refreshTokenHandler.obtainNewAccessToken();
@SuppressLint("ApplySharedPref")
private void refreshToken() {
SharedPreferences mainPref = context.getSharedPreferences(Constants.APPLICATION_PACKAGE,
Context.MODE_PRIVATE);
long lastTokenRenewalAt = mainPref.getLong(Constants.LAST_TOKEN_RENEWAL, 0);
if (!IS_TOKEN_RENEWING
|| lastTokenRenewalAt + Constants.HttpClient.DEFAULT_TOKEN_TIME_OUT * 2 < System.currentTimeMillis()) {
IS_TOKEN_RENEWING = true;
Editor editor = mainPref.edit();
editor.putLong(Constants.LAST_TOKEN_RENEWAL, System.currentTimeMillis());
editor.commit(); //Need to make sure pref is committed
RefreshTokenHandler refreshTokenHandler = new RefreshTokenHandler(token);
refreshTokenHandler.obtainNewAccessToken();
}
}

public Context getContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private HttpClient(){
public static final String FAILURE_RESPONSE = "fail";
public final static String REFRESH_TOKEN = "refresh_token";
public final static String ACCESS_TOKEN = "access_token";
public final static String LAST_TOKEN_RENEWAL = "last_token_renewal";
public final static String EXPIRE_LABEL = "expires_in";
public static final String ERROR_LABEL = "error";
public static final String ERROR_DESCRIPTION_LABEL = "error_description";
Expand Down

0 comments on commit f043e9e

Please sign in to comment.