Skip to content

Commit

Permalink
correction unreachable
Browse files Browse the repository at this point in the history
  • Loading branch information
oaliaga committed May 22, 2014
1 parent fb502be commit 63793e8
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 159 deletions.
87 changes: 74 additions & 13 deletions src/com/prey/PreyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.ArrayList;
import java.util.Date;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
Expand All @@ -33,6 +32,8 @@
import com.prey.actions.PreyAction;
import com.prey.activities.FeedbackActivity;
import com.prey.activities.WelcomeActivity;
import com.prey.managers.PreyConnectivityManager;
import com.prey.managers.PreyWifiManager;
import com.prey.net.PreyWebServices;


Expand Down Expand Up @@ -113,6 +114,10 @@ public class PreyConfig {

public static final String SEND_DATA="SEND_DATA";

public static final String LAST_REPORT_START_DATE="LAST_REPORT_START_DATE";

public static final String NEXT_ALERT="NEXT_ALERT";

private boolean sendNotificationId;
private String notificationId;

Expand Down Expand Up @@ -162,6 +167,8 @@ public class PreyConfig {

private boolean sendData;

private boolean nextAlert;

private Context ctx;

private PreyConfig(Context ctx) {
Expand Down Expand Up @@ -209,6 +216,7 @@ private PreyConfig(Context ctx) {

this.installationDate=settings.getLong(PreyConfig.INSTALLATION_DATE, new Date().getTime());
this.sendData=settings.getBoolean(PreyConfig.SEND_DATA, false);
this.nextAlert=settings.getBoolean(PreyConfig.NEXT_ALERT, false);
saveLong(PreyConfig.INSTALLATION_DATE,installationDate);
}

Expand Down Expand Up @@ -507,18 +515,20 @@ public void setSecurityPrivilegesAlreadyPrompted(boolean securityPrivilegesAlrea


public void registerC2dm(){
PreyLogger.d("______________________");
PreyLogger.d("______________________");
PreyLogger.d("___ registerC2dm _____");
PreyLogger.d("______________________");
PreyLogger.d("______________________");
PreyLogger.d("______________________");
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this.ctx, 0, new Intent(), 0)); // boilerplate
String gcmId= FileConfigReader.getInstance(this.ctx).getGcmId();
//PreyLogger.i("gcmId:"+gcmId);
registrationIntent.putExtra("sender",gcmId);
this.ctx.startService(registrationIntent);
if( PreyConfig.getPreyConfig(ctx).isOnline() ){
PreyLogger.d("______________________");
PreyLogger.d("______________________");
PreyLogger.d("___ registerC2dm _____");
PreyLogger.d("______________________");
PreyLogger.d("______________________");
PreyLogger.d("______________________");
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this.ctx, 0, new Intent(), 0)); // boilerplate
String gcmId= FileConfigReader.getInstance(this.ctx).getGcmId();
//PreyLogger.i("gcmId:"+gcmId);
registrationIntent.putExtra("sender",gcmId);
this.ctx.startService(registrationIntent);
}
}

public void unregisterC2dm(boolean updatePrey){
Expand Down Expand Up @@ -783,4 +793,55 @@ public void setSendData(boolean sendData) {
this.sendData = sendData;
this.saveBoolean(PreyConfig.SEND_DATA, sendData);
}

public void setLastReportStartDate(long lastReportStartDate){
saveLong(PreyConfig.LAST_REPORT_START_DATE, lastReportStartDate);
}

public long getLastReportStartDate(){
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx);
return settings.getLong(PreyConfig.LAST_REPORT_START_DATE, 0);
}

public void setNextAlert(boolean nextAlert){
this.nextAlert=nextAlert;
saveBoolean(PreyConfig.NEXT_ALERT, nextAlert);
}

public boolean isNextAlert(){
return nextAlert;
}


public boolean isConnectionExists() {
boolean isConnectionExists = false;
// There is wifi connexion?
if (PreyConnectivityManager.getInstance(ctx).isWifiConnected()) {
isConnectionExists = true;
}
// if there is no connexion wifi, verify mobile connection?
if (!isConnectionExists && PreyConnectivityManager.getInstance(ctx).isMobileConnected()) {
isConnectionExists = true;
}
return isConnectionExists;
}

public boolean isOnline() {
boolean isOnline = false;
try {
int i = 0;
// wait at most 5 seconds
while (!isOnline) {

This comment has been minimized.

Copy link
@fimu

fimu May 30, 2014

didn't you mean: "while (i < 5 && !isOnline)" ...

  • i guess if you don't have connection you don't want to wait forever, or do you?
  • i guess it only stops after "i" overflows
isOnline = PreyWifiManager.getInstance(ctx).isOnline();
if (i < 5 && !isOnline) {
PreyLogger.i("Phone doesn't have internet connection now. Waiting 1 secs for it");
Thread.sleep(1000);
}
i++;
}
} catch (Exception e) {
PreyLogger.e("Error, because:" + e.getMessage(), e);
}
return isOnline;
}
}
5 changes: 2 additions & 3 deletions src/com/prey/actions/alert/AlertThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public void run() {
popup.putExtra("description_message", description);
ctx.startActivity(popup);

Settings.System.putString(ctx.getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED, description);

Settings.System.putString(ctx.getContentResolver(),Settings.System.NEXT_ALARM_FORMATTED,description);
PreyConfig.getPreyConfig(ctx).setNextAlert(true);

PreyWebServices.getInstance().sendNotifyActionResultPreyHttp(ctx, UtilJson.makeMapParam("start","alert","started"));
try {
Expand Down
3 changes: 1 addition & 2 deletions src/com/prey/actions/location/LocationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public static HttpDataService dataLocation(Context ctx) {
try {

data.setList(true);
PreyConfig.getPreyConfig(ctx).setMissing(true);

ctx.startService(intent);
boolean validLocation = false;
Expand Down Expand Up @@ -60,7 +59,7 @@ public static HttpDataService dataLocation(Context ctx) {
data.addDataListAll(parametersMap);

ctx.stopService(intent);
PreyConfig.getPreyConfig(ctx).setMissing(false);


} catch (Exception e) {
PreyLogger.e("Error causa:" + e.getMessage() , e);
Expand Down
10 changes: 9 additions & 1 deletion src/com/prey/actions/observer/ActionsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import org.json.JSONObject;

import android.content.Context;
import android.provider.Settings;

import com.prey.PreyConfig;
import com.prey.PreyLogger;
import com.prey.actions.HttpDataService;
import com.prey.actions.PreyAction;
Expand Down Expand Up @@ -98,7 +100,13 @@ public void jobGroupFinished(ArrayList<ActionResult> results, Context ctx) {
public List<HttpDataService> runActionJson(Context ctx, List<JSONObject> jsonObjectList) {
List<HttpDataService> listData=new ArrayList<HttpDataService>();

PreyLogger.i("runActionJson size:"+(jsonObjectList==null?-1:jsonObjectList.size()));
int size=jsonObjectList==null?-1:jsonObjectList.size();
PreyLogger.i("runActionJson size:"+size);

if(size>=0&&PreyConfig.getPreyConfig(ctx).isNextAlert()){
PreyConfig.getPreyConfig(ctx).setNextAlert(false);
Settings.System.putString(ctx.getContentResolver(),Settings.System.NEXT_ALARM_FORMATTED,"");
}

try {
for(int i=0;jsonObjectList!=null&&i<jsonObjectList.size();i++){
Expand Down
2 changes: 1 addition & 1 deletion src/com/prey/actions/picture/PictureUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static byte[] getPicture(Context ctx, String focus) {
mgr.setStreamSolo(streamType, true);
mgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mgr.setStreamMute(streamType, true);
while (SimpleCameraActivity.activity == null&& i < 20) {
while (SimpleCameraActivity.activity == null&& i < 10) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Expand Down
44 changes: 3 additions & 41 deletions src/com/prey/beta/actions/PreyBetaActionsRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ public List<HttpDataService> execute() {
List<JSONObject> jsonObject = getInstructions();
PreyLogger.d("version:"+version+" body:"+body);
if(jsonObject==null||jsonObject.size()==0){
if("v1".equals(version)&&"run_once".equals(body)){
PreyLogger.d("onlyReport");
onlyReport();
}else{
PreyLogger.d("nothing");
}
PreyLogger.d("nothing");
}else{
PreyLogger.d("runInstructions");
runInstructions(jsonObject);
Expand Down Expand Up @@ -103,41 +98,8 @@ private List<HttpDataService> runInstructions(List<JSONObject> jsonObject) throw
return listData;
}

private void onlyReport() throws PreyException {
boolean missing=executeMissing(3,true);
if(missing){
try{
List<JSONObject> jsonList=new ArrayList<JSONObject>();
JSONObject json = new JSONObject();
json.put("command", "get");
json.put("target", "report");
jsonList.add(json);
runInstructions(jsonList);
}catch(Exception e){}
executeMissing(3,false);
getInstructions();
}

}



private boolean executeMissing(int count,boolean missing){
PreyHttpResponse response=null;
int i=0;
boolean okMissing=false;
while(!okMissing){
try{
response=PreyWebServices.getInstance().setMissing(ctx, missing);
}catch(Exception e){
}
if(response!=null&&response.getStatusLine().getStatusCode()==200){
okMissing=true;
}
if (i==count){
break;
}
i=i+1;
}
return okMissing;
}
}

37 changes: 31 additions & 6 deletions src/com/prey/events/factories/EventFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,52 @@ public static Event getEvent(Context ctx,Intent intent){
if (ACTION_SHUTDOWN.equals(intent.getAction()) ){
return new Event(Event.TURNED_OFF);
}
if (CONNECTIVITY_CHANGE.equals(intent.getAction())|| WIFI_STATE_CHANGED.equals(intent.getAction()) ){
if (CONNECTIVITY_CHANGE.equals(intent.getAction()) ){
JSONObject info=new JSONObject();

int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
PreyLogger.d("__wifiState:"+wifiState);;
try{
if (PreyConnectivityManager.getInstance(ctx).isMobileConnected()) {
info.put("connected", "mobile");
if(!PreyConfig.getPreyConfig(ctx).isRegisterC2dm())
PreyConfig.getPreyConfig(ctx).registerC2dm();

}else{
if (wifiState == WifiManager.WIFI_STATE_UNKNOWN) {
PreyConfig.getPreyConfig(ctx).setRegisterC2dm(false);
}
}
}catch (Exception e) {
}
return new Event(Event.WIFI_CHANGED,info.toString());
}
if ( WIFI_STATE_CHANGED.equals(intent.getAction()) ){
JSONObject info=new JSONObject();
int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
PreyLogger.d("__wifiState:"+wifiState);;
try{
if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
info.put("connected", "wifi");
if(!PreyConfig.getPreyConfig(ctx).isRegisterC2dm())
PreyConfig.getPreyConfig(ctx).registerC2dm();
}
if (PreyConnectivityManager.getInstance(ctx).isMobileConnected()) {
info.put("connected", "mobile");
if(!PreyConfig.getPreyConfig(ctx).isRegisterC2dm())
PreyConfig.getPreyConfig(ctx).registerC2dm();
if (wifiState == WifiManager.WIFI_STATE_DISABLED) {
if(!PreyConnectivityManager.getInstance(ctx).isMobileConnected()){
PreyConfig.getPreyConfig(ctx).setRegisterC2dm(false);
}
}


}catch (Exception e) {
}
return new Event(Event.WIFI_CHANGED,info.toString());
}
if (AIRPLANE_MODE.equals(intent.getAction())){
if(isAirplaneModeOn(ctx))
if(isAirplaneModeOn(ctx)){
PreyConfig.getPreyConfig(ctx).setPreviousSsid("");
PreyConfig.getPreyConfig(ctx).setRegisterC2dm(false);
}
}
/*
if (ACTION_POWER_CONNECTED.equals(intent.getAction()) ){
Expand Down
Loading

0 comments on commit 63793e8

Please sign in to comment.