Skip to content

Commit

Permalink
Fix Aware
Browse files Browse the repository at this point in the history
  • Loading branch information
oaliaga committed Sep 11, 2018
1 parent 3462211 commit 54d4414
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 42 deletions.
7 changes: 0 additions & 7 deletions app/src/main/java/com/prey/PreyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ public void onCreate() {
String sessionId = PreyUtils.randomAlphaNumeric(16);
PreyLogger.d("#######sessionId:" + sessionId);
PreyConfig.getPreyConfig(this).setSessionId(sessionId);
final String preyInfoVersion = PreyConfig.getPreyConfig(this).getInfoPreyVersion();
String preyVersion = PreyConfig.getPreyConfig(this).getPreyVersion();
PreyLogger.d("PreyVersion[" + preyVersion+"] preyInfoVersion["+preyInfoVersion+"]");
if (!preyVersion.equals(preyInfoVersion)) {
PreyConfig.getPreyConfig(getApplicationContext()).setPreyVersion(preyInfoVersion);
PreyWebServices.getInstance().sendEvent(getApplicationContext(), PreyConfig.ANDROID_VERSION_UPDATED);
}

boolean missing=PreyConfig.getPreyConfig(this).isMissing();

Expand Down
22 changes: 16 additions & 6 deletions app/src/main/java/com/prey/PreyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public class PreyConfig {
private boolean runOnce;

private boolean disablePowerOptions;
private String version;

private PreyConfig(Context ctx) {
this.ctx = ctx;
Expand All @@ -214,6 +215,10 @@ private PreyConfig(Context ctx) {
this.disablePowerOptions = settings.getBoolean(PreyConfig.PREFS_DISABLE_POWER_OPTIONS, false);
} catch ( Exception e){
} catch ( NoClassDefFoundError e) {}
try {
version =getString(PreyConfig.PREY_VERSION, getInfoPreyVersion(ctx));
}catch ( Exception e){
}
}

public static synchronized PreyConfig getPreyConfig(Context ctx) {
Expand All @@ -226,8 +231,10 @@ public static synchronized PreyConfig getPreyConfig(Context ctx) {
return cachedInstance;
}

private static void deleteCacheInstance() {
public static void deleteCacheInstance(Context ctx) {
cachedInstance = null;
PreferenceManager.getDefaultSharedPreferences(ctx).
edit().clear().apply();
}

public Context getContext(){
Expand Down Expand Up @@ -300,7 +307,9 @@ private void removeKey(String key){
SharedPreferences.Editor editor = settings.edit();
editor.remove(key);
editor.commit();
}catch(Exception e){}
}catch(Exception e){
PreyLogger.e("removeKey:"+e.getMessage(),e);
}
}

private long getLong(String key, long defaultValue){
Expand Down Expand Up @@ -431,7 +440,7 @@ public void setSecurityPrivilegesAlreadyPrompted(boolean securityPrivilegesAlrea
editor.commit();
}

public String getInfoPreyVersion() {
public String getInfoPreyVersion(Context ctx) {
String versionName=VERSION_PREY_DEFAULT;
try{
PackageInfo pinfo =ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
Expand Down Expand Up @@ -1096,10 +1105,11 @@ public void setAutoConnect(boolean auto_connect) {
}

public String getPreyVersion(){
return getString(PreyConfig.PREY_VERSION, "");
return version;
}

public void setPreyVersion(String preyVersion) {
saveString(PreyConfig.PREY_VERSION, preyVersion);
public void setPreyVersion(String version) {
this.version=version;
this.saveString(PreyConfig.PREY_VERSION, version);
}
}
8 changes: 5 additions & 3 deletions app/src/main/java/com/prey/actions/aware/AwareConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class AwareConfig {

private AwareConfig(Context ctx) {
this.ctx = ctx;
setLocationAware(getLocationAwareJson());
locationAware=getLocationAwareJson();
}

public boolean locationAware = false;
public boolean locationAware = false;

public static synchronized AwareConfig getAwareConfig(Context ctx) {
if (cachedInstance == null) {
Expand All @@ -44,19 +44,21 @@ public void setLocationAware(boolean locationAware){
}

private boolean getLocationAwareJson() {
locationAware = false;
try {
JSONObject jsnobject = PreyWebServices.getInstance().getStatus(ctx);
if (jsnobject != null) {
PreyLogger.d("AWARE jsnobject :" + jsnobject);
JSONObject jsnobjectSettings = jsnobject.getJSONObject("settings");
JSONObject jsnobjectLocal = jsnobjectSettings.getJSONObject("local");
locationAware = jsnobjectLocal.getBoolean("location_aware");
PreyLogger.d("AWARE getLocationAware :" + locationAware);
} else {
PreyLogger.d("AWARE getLocationAware null");
locationAware = false;
}
} catch (Exception e) {
PreyLogger.e("AWARE Error:" + e.getMessage(), e);
locationAware = false;
}
return locationAware;
}
Expand Down
48 changes: 26 additions & 22 deletions app/src/main/java/com/prey/actions/aware/AwareController.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public static AwareController getInstance() {
return INSTANCE;
}

public void init(final Context ctx) {
public void init(Context ctx) {
try{
PreyLogger.d("AWARE AwareController init:"+AwareConfig.getAwareConfig(ctx).isLocationAware());
if (AwareConfig.getAwareConfig(ctx).isLocationAware()) {
boolean isLocationAware=AwareConfig.getAwareConfig(ctx).isLocationAware();
PreyLogger.d("AWARE AwareController init:"+isLocationAware);
if (isLocationAware) {
PreyLocation locationAware = LocationUtil.getLocation(ctx, null, false);
PreyLocation locationNow = sendAware(ctx, locationAware);
if (locationNow != null) {
Expand Down Expand Up @@ -141,26 +142,29 @@ public static void getSendNowAware(Context ctx) throws Exception{
sendNowAware(ctx,locationNow);
}

private static void sendNowAware(Context ctx, PreyLocation locationNow) throws Exception{
PreyLogger.d("AWARE sendNowAware");
String messageId = null;
String reason = null;
double accD = Math.round(locationNow.getAccuracy() * 100.0) / 100.0;
JSONObject json = new JSONObject();
json.put("lat", Double.toString(locationNow.getLat()));
json.put("lng", Double.toString(locationNow.getLng()));
json.put("accuracy", Double.toString(accD));
json.put("method", locationNow.getMethod());
JSONObject location = new JSONObject();
location.put("location", json);
PreyHttpResponse preyResponse = PreyWebServices.getInstance().sendLocation(ctx, location);
if (preyResponse != null) {
if (preyResponse.getStatusCode() == 201) {
PreyLogger.d("AWARE getStatusCode 201");
AwareConfig.getAwareConfig(ctx).setLocationAware(false);
private static void sendNowAware(Context ctx, PreyLocation locationNow) throws Exception {
boolean isLocationAware = AwareConfig.getAwareConfig(ctx).isLocationAware();
PreyLogger.d("AWARE sendNowAware isLocationAware:"+isLocationAware);
if (isLocationAware){
String messageId = null;
String reason = null;
double accD = Math.round(locationNow.getAccuracy() * 100.0) / 100.0;
JSONObject json = new JSONObject();
json.put("lat", Double.toString(locationNow.getLat()));
json.put("lng", Double.toString(locationNow.getLng()));
json.put("accuracy", Double.toString(accD));
json.put("method", locationNow.getMethod());
JSONObject location = new JSONObject();
location.put("location", json);
PreyHttpResponse preyResponse = PreyWebServices.getInstance().sendLocation(ctx, location);
if (preyResponse != null) {
if (preyResponse.getStatusCode() == 201) {
PreyLogger.d("AWARE getStatusCode 201");
AwareConfig.getAwareConfig(ctx).setLocationAware(false);
}
PreyConfig.getPreyConfig(ctx).setAwareDate(PreyConfig.FORMAT_SDF_AWARE.format(new Date()));
PreyLogger.d("AWARE sendNowAware:" + locationNow.toString());
}
PreyConfig.getPreyConfig(ctx).setAwareDate(PreyConfig.FORMAT_SDF_AWARE.format(new Date()));
PreyLogger.d("AWARE sendNowAware:"+locationNow.toString());
}
}

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/prey/barcodereader/BarcodeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.prey.PreyLogger;
import com.prey.PreyUtils;
import com.prey.R;
import com.prey.actions.aware.AwareController;
import com.prey.activities.PermissionInformationActivity;
import com.prey.activities.SignInActivity;
import com.prey.net.PreyWebServices;
Expand Down Expand Up @@ -176,6 +177,11 @@ protected Void doInBackground(String... data) {
PreyWebServices.getInstance().sendEvent(getApplication(), PreyConfig.ANDROID_SIGN_IN);
String email=PreyWebServices.getInstance().getEmail(getApplicationContext());
PreyConfig.getPreyConfig(getApplicationContext()).setEmail(email);
new Thread() {
public void run() {
AwareController.getInstance().init(getApplicationContext());
}
}.start();
}
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void run() {
String now=PreyConfig.FORMAT_SDF_AWARE.format(new Date());
PreyLogger.d("AWARE USER_PRESENT awareDate:"+awareDate+" now:"+now);
if(!now.equals(awareDate)) {
PreyLogger.d("AWARE getSendNowAware"+now);
PreyLogger.d("AWARE getSendNowAware: "+now);
new Thread() {
public void run() {
try{
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/prey/json/actions/Detach.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
package com.prey.json.actions;

import android.content.Context;
import android.content.Intent;

import com.prey.PreyConfig;
import com.prey.PreyLogger;
import com.prey.actions.aware.AwareConfig;
import com.prey.actions.fileretrieval.FileretrievalController;
import com.prey.actions.geofences.GeofenceController;
import com.prey.actions.observer.ActionResult;
import com.prey.actions.report.ReportScheduled;
import com.prey.activities.LoginActivity;
import com.prey.activities.WelcomeBatchActivity;
import com.prey.backwardcompatibility.FroyoSupport;
import com.prey.net.PreyWebServices;

Expand Down Expand Up @@ -48,6 +52,9 @@ public static String detachDevice(Context ctx){
}
} catch (Exception e) {}
PreyLogger.d("3:"+error);
try {
AwareConfig.getAwareConfig(ctx).setLocationAware(false);
} catch (Exception e) {}
try {
GeofenceController.getInstance().deleteAllZones(ctx);
} catch (Exception e) {}
Expand All @@ -68,6 +75,17 @@ public static String detachDevice(Context ctx){
try { PreyConfig.getPreyConfig(ctx).setDeviceId("");} catch (Exception e) {error = e.getMessage();}
try { PreyConfig.getPreyConfig(ctx).setApiKey("");} catch (Exception e) {error = e.getMessage();}
PreyLogger.d("8:"+error);

PreyLogger.d("Email:"+PreyConfig.getPreyConfig(ctx).getEmail());
PreyLogger.d("DeviceId:"+PreyConfig.getPreyConfig(ctx).getDeviceId());
PreyLogger.d("ApiKey:"+PreyConfig.getPreyConfig(ctx).getApiKey());
try {PreyConfig.deleteCacheInstance(ctx);} catch (Exception e) {}
try {
Intent intent = new Intent(ctx, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
ctx.startActivity(intent);
} catch (Exception e) {}
return error;
}
}
4 changes: 1 addition & 3 deletions app/src/main/java/com/prey/net/UtilConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,10 @@ public static final PreyHttpResponse connection(PreyConfig preyConfig,String uri
PreyLogger.d("X-Prey-Device-ID:"+deviceId);
connection.addRequestProperty("X-Prey-State", status);
PreyLogger.d("X-Prey-State:"+status);

}


connection.addRequestProperty("User-Agent", getUserAgent(preyConfig));
PreyLogger.d("User-Agent"+getUserAgent(preyConfig));
PreyLogger.d("User-Agent:"+getUserAgent(preyConfig));
if (entityFiles==null&&(params!=null&&params.size()>0)){
OutputStream os = connection.getOutputStream();
DataOutputStream dos = new DataOutputStream( os );
Expand Down

0 comments on commit 54d4414

Please sign in to comment.