Skip to content

Commit

Permalink
Merge branch 'temp'
Browse files Browse the repository at this point in the history
# Conflicts:
#	SleepGarmin-android/app/src/main/java/com/urbandroid/sleep/garmin/SleepAsAndroidProviderService.java
  • Loading branch information
Artaud committed Nov 27, 2018
2 parents a52ae9f + 1ba1272 commit be40d96
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

public class MainActivity extends Activity {

private boolean debug = false;
private static final String TAG = MainActivity.class.getSimpleName();

public static final String PACKAGE_SLEEP = "com.urbandroid.sleep";
Expand Down Expand Up @@ -134,6 +133,11 @@ public void onDeviceStatusChanged(IQDevice device, IQDevice.IQDeviceStatus statu
@Override
public void onDestroy() {
super.onDestroy();
try {
mConnectIQ.unregisterForEvents(mDevice);
} catch (InvalidStateException e) {
Logger.logSevere(e);
}
try {
mConnectIQ.shutdown(this);
} catch (InvalidStateException e) {
Expand Down Expand Up @@ -166,14 +170,10 @@ public void onSdkReady() {
}

@Override
public void onInitializeError(ConnectIQ.IQSdkErrorStatus iqSdkErrorStatus) {

}
public void onInitializeError(ConnectIQ.IQSdkErrorStatus iqSdkErrorStatus) {}

@Override
public void onSdkShutDown() {

}
public void onSdkShutDown() {}
});
Logger.logDebug("ConnectIQ instance set to WIRELESS");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class SleepAsAndroidProviderService extends Service {
private float[] maxFloatValues = null;
private float[] maxRawFloatValues = null;

public static final int MAX_DELIVERY_ERROR = 50;
public static final int MAX_DELIVERY_ERROR = 5;
public static final int MAX_DELIVERY_IN_PROGRESS = 5;
public static final int MESSAGE_INTERVAL = 3000;
public static final int MESSAGE_INTERVAL_ON_FAILURE = 1000;
Expand All @@ -120,7 +120,6 @@ public void onCreate() {
GlobalInitializer.initializeIfRequired(this);
Logger.logDebug(TAG + "Garmin service onCreate");
handler = new Handler();
context = this;

// checking if Start sleep from watch installed
// TODO udelat jako notfiikaci
Expand Down Expand Up @@ -210,7 +209,7 @@ public void onApplicationNotInstalled(String applicationId) {
}
}

private void registerWatchMessagesReceiver(ConnectIQ connectIQ){
private void registerWatchMessagesReceiver(){
Logger.logDebug(TAG + "registerWatchMessageReceiver started");
try {
if (getDevice(connectIQ) != null) {
Expand Down Expand Up @@ -242,7 +241,7 @@ public void onMessageReceived(IQDevice device, IQApp app, List<Object> message,

try {
maxRawFloatValues[i] = Float.valueOf(maxRawValue) * 9.806f / 1000f;
Logger.logDebug(TAG + "New actigraphy [m/s2]: " + maxRawFloatValues[i]);
// Logger.logDebug(TAG + "New actigraphy [m/s2]: " + maxRawFloatValues[i]);
} catch (NumberFormatException e) {
maxRawFloatValues[i] = 0;
}
Expand Down Expand Up @@ -295,7 +294,6 @@ public void onMessageReceived(IQDevice device, IQApp app, List<Object> message,
}

public void sendExplicitBroadcastToSleep(Intent intent) {
intent.putExtra("SOURCE_PACKAGE", getPackageName());
intent.setPackage(MainActivity.PACKAGE_SLEEP);
sendBroadcast(intent);
}
Expand Down Expand Up @@ -423,13 +421,13 @@ private void cancelRecovery(Context context) {
}

private void stopSelfAndDontScheduleRecovery(Context context) {
Logger.logDebug("stopSelfAnd DON'T ScheduleRecovery");
Logger.logDebug(TAG + "stopSelfAndDontScheduleRecovery");
cancelRecovery(context);
stopSelf();
}

private void stopSelfAndScheduleRecovery(Context context) {
Logger.logDebug("stopSelfAndScheduleRecovery");
Logger.logDebug(TAG + "stopSelfAndScheduleRecovery");
stopSelf();

PendingIntent pi = getRecoveryIntent(context);
Expand All @@ -453,7 +451,7 @@ private void stopSelfAndScheduleRecovery(Context context) {
private void doSendMessage(final String message){
Logger.logDebug(TAG + "doSendMessage");
try {
connectIQ.sendMessage(getDevice(connectIQ), getApp(connectIQ), message, new IQSendMessageListener() {
connectIQ.sendMessage(getDevice(), getApp(connectIQ), message, new IQSendMessageListener() {
@Override
public void onMessageStatus(IQDevice iqDevice, IQApp iqApp, IQMessageStatus status) {
// Logger.logDebug(TAG + "onMessageStatus: " + message + " " + iqDevice.getDeviceIdentifier() + " " + iqApp.getApplicationId());
Expand Down Expand Up @@ -523,67 +521,37 @@ public int onStartCommand(final Intent intent, int flags, int startId) {
connectIQ = ConnectIQ.getInstance(this, IQConnectType.WIRELESS);
}

if (!connectIqReady) {
Logger.logDebug(TAG + "Initializing SDK");
if (!connectIqReady && !connectIqInitializing) {
connectIqInitializing = true;
// initialize SDK
connectIQ.initialize(this,true,new ConnectIQListener() {

@Override
public void onInitializeError(IQSdkErrorStatus errStatus) {
Logger.logDebug(TAG + " " + errStatus.toString());
connectIqReady = false;
stopSelfAndScheduleRecovery(getApplicationContext());
}

@Override
public void onSdkReady() {
connectIqInitializing = false;
connectIqReady = true;
Logger.logInfo(TAG + " onSdkReady");

registerWatchMessagesReceiver(connectIQ);
// checkAppIsAvailable();

handleMessageFromSleep(intent);
}

@Override
public void onSdkShutDown() {
Logger.logDebug(TAG + "onSdkShutDown");
connectIqInitializing = false;
connectIqReady = false;
}
});

// connectIQ.initialize(this,true,connectIQSdkListener);
// init a wrapped SDK with fix for "Cannot cast to Long" issue viz https://forums.garmin.com/forum/developers/connect-iq/connect-iq-bug-reports/158068-?p=1278464#post1278464
// context = initializeConnectIQWrapped(this, connectIQ, false, new ConnectIQListener() {
//
// @Override
// public void onInitializeError(IQSdkErrorStatus errStatus) {
// Logger.logDebug(TAG + " " + errStatus.toString());
// connectIqReady = false;
// stopSelfAndScheduleRecovery(getApplicationContext());
// }
//
// @Override
// public void onSdkReady() {
// connectIqInitializing = false;
// connectIqReady = true;
// Logger.logInfo(TAG + " onSdkReady");
//
// registerWatchMessagesReceiver(connectIQ);
// checkAppIsAvailable();
//
// handleMessageFromSleep(intent);
// }
//
// @Override
// public void onSdkShutDown() {
// connectIqInitializing = false;
// connectIqReady = false;
// }
// });
context = initializeConnectIQWrapped(this, connectIQ, false, new ConnectIQListener() {

@Override
public void onInitializeError(IQSdkErrorStatus errStatus) {
Logger.logDebug(TAG + " " + errStatus.toString());
connectIqReady = false;
stopSelfAndScheduleRecovery(getApplicationContext());
}

@Override
public void onSdkReady() {
connectIqInitializing = false;
connectIqReady = true;
Logger.logInfo(TAG + " onSdkReady");

registerWatchMessagesReceiver();
checkAppIsAvailable();

handleMessageFromSleep(intent);
}

@Override
public void onSdkShutDown() { }
});
} else if (!connectIqInitializing) {
handleMessageFromSleep(intent);
}

return START_STICKY;
Expand Down Expand Up @@ -624,7 +592,7 @@ private void handleMessageFromSleep(Intent intent) {
enqueue("BatchSize;" + param);
}
if (action.equals(START_ALARM)) {
long param = intent.getLongExtra("DELAY", 0);
long param = intent.getIntExtra("DELAY", 0);
Logger.logDebug(TAG + "Sending start alarm to watch with delay " + param);
enqueue("StartAlarm;" + param);
}
Expand All @@ -649,7 +617,7 @@ private void handleMessageFromSleep(Intent intent) {
if (watchAppOpenTime == -1 || System.currentTimeMillis() - watchAppOpenTime >= 10000) {
Logger.logDebug(TAG + "Trying to open app on watch...");
watchAppOpenTime = System.currentTimeMillis();
connectIQ.openApplication(getDevice(connectIQ), getApp(connectIQ), new IQOpenApplicationListener() {
connectIQ.openApplication(getDevice(), getApp(), new IQOpenApplicationListener() {
@Override
public void onOpenApplicationResponse(IQDevice iqDevice, IQApp iqApp, IQOpenApplicationStatus iqOpenApplicationStatus) {
Logger.logDebug(TAG + "onOpenApplication response: " + iqOpenApplicationStatus);
Expand All @@ -674,7 +642,7 @@ public void startWatchApp(){
if (watchAppOpenTime == -1 || System.currentTimeMillis() - watchAppOpenTime >= 10000) {
Logger.logDebug(TAG + "Trying to open app on watch...");
watchAppOpenTime = System.currentTimeMillis();
connectIQ.openApplication(getDevice(connectIQ), getApp(connectIQ), new IQOpenApplicationListener() {
connectIQ.openApplication(getDevice(), getApp(), new IQOpenApplicationListener() {
@Override
public void onOpenApplicationResponse(IQDevice iqDevice, IQApp iqApp, IQOpenApplicationStatus iqOpenApplicationStatus) {
if (iqOpenApplicationStatus == IQOpenApplicationStatus.PROMPT_NOT_SHOWN_ON_DEVICE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:layout_marginBottom="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
Expand Down

0 comments on commit be40d96

Please sign in to comment.