Skip to content

Commit

Permalink
adding foreground support for share
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenBlackWasAlreadyTaken committed Apr 16, 2015
1 parent 9e21364 commit 5f27a91
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@
<orderEntry type="library" exported="" name="usb-serial-for-android-v010" level="project" />
<orderEntry type="library" exported="" name="okhttp-2.2.0" level="project" />
<orderEntry type="library" exported="" name="pebblekit-2.6.0" level="project" />
<orderEntry type="library" exported="" name="acra-4.5.0" level="project" />
<orderEntry type="library" exported="" name="rxjava-1.0.0" level="project" />
<orderEntry type="library" exported="" name="acra-4.5.0" level="project" />
<orderEntry type="library" exported="" name="retrofit-1.9.0" level="project" />
<orderEntry type="library" exported="" name="hellocharts-library-1.1" level="project" />
</component>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
import android.preference.PreferenceManager;
Expand All @@ -50,7 +51,7 @@
import java.util.Date;
import java.util.UUID;

@TargetApi(android.os.Build.VERSION_CODES.JELLY_BEAN_MR2)
@TargetApi(Build.VERSION_CODES.KITKAT)
public class DexCollectionService extends Service {
private final static String TAG = DexCollectionService.class.getSimpleName();
private String mDeviceName;
Expand Down Expand Up @@ -120,7 +121,7 @@ public void setRetryTimer() {
Log.d(TAG, "Restarting in: " + (retry_in / (60 * 1000)) + " minutes");
Calendar calendar = Calendar.getInstance();
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(alarm.RTC_WAKEUP, calendar.getTimeInMillis() + retry_in, PendingIntent.getService(this, 0, new Intent(this, DexCollectionService.class), 0));
alarm.setExact(alarm.RTC_WAKEUP, calendar.getTimeInMillis() + retry_in, PendingIntent.getService(this, 0, new Intent(this, DexCollectionService.class), 0));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.eveningoutpost.dexdrip.Services;

import android.annotation.TargetApi;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
Expand All @@ -17,6 +18,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -50,8 +52,10 @@
import rx.Observable;
import rx.functions.Action1;

@TargetApi(Build.VERSION_CODES.KITKAT)
public class DexShareCollectionService extends Service {
private final static String TAG = DexShareCollectionService.class.getSimpleName();
private ForegroundServiceStarter foregroundServiceStarter;
private String mDeviceAddress;
private String mDeviceName;
private boolean is_connected = false;
Expand Down Expand Up @@ -99,11 +103,15 @@ public class DexShareCollectionService extends Service {

public boolean shouldDisconnect = true;
public boolean share2 = false;
public Service service;

@Override
public void onCreate() {
super.onCreate();
readData = new ReadDataShare(this);
service = this;
foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), service);
foregroundServiceStarter.start();
final IntentFilter bondintent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mPairReceiver, bondintent);
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Expand All @@ -114,9 +122,9 @@ public void onCreate() {
public int onStartCommand(Intent intent, int flags, int startId) {
share2 = prefs.getBoolean("share_auth_mode_two", false);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.LOLLIPOP){
// if (currentapiVersion < android.os.Build.VERSION_CODES.LOLLIPOP){
shouldDisconnect = false;
}
// }
if (CollectionServiceStarter.isBTShare(getApplicationContext())) {
setFailoverTimer();
} else {
Expand All @@ -136,12 +144,27 @@ public void onDestroy() {
super.onDestroy();
close();
setRetryTimer();
foregroundServiceStarter.stop();
unregisterReceiver(mPairReceiver);
Log.w(TAG, "SERVICE STOPPED");
}

public SharedPreferences.OnSharedPreferenceChangeListener prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.compareTo("run_service_in_foreground") == 0) {

Log.e("FOREGROUND", "run_service_in_foreground changed!");
if (prefs.getBoolean("run_service_in_foreground", false)) {
foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), service);
foregroundServiceStarter.start();
Log.w(TAG, "Moving to foreground");
setRetryTimer();
} else {
service.stopForeground(true);
Log.w(TAG, "Removing from foreground");
setRetryTimer();
}
}
if (key.compareTo("dex_collection_method") == 0) {
CollectionServiceStarter collectionServiceStarter = new CollectionServiceStarter(getApplicationContext());
collectionServiceStarter.start(getApplicationContext());
Expand All @@ -158,14 +181,14 @@ public void setRetryTimer() {
BgReading bgReading = BgReading.last();
long retry_in;
if (bgReading != null) {
retry_in = Math.min(Math.max((1000 * 30), (1000 * 60 * 5) - (new Date().getTime() - bgReading.timestamp) + 5), (1000 * 60 * 5));
retry_in = Math.min(Math.max((1000 * 30), (1000 * 60 * 5) - (new Date().getTime() - bgReading.timestamp) + 10000), (1000 * 60 * 5));
} else {
retry_in = (1000 * 60);
}
Log.d(TAG, "Restarting in: " + (retry_in / (60 * 1000)) + " minutes");
Calendar calendar = Calendar.getInstance();
AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
alarm.set(alarm.RTC_WAKEUP, calendar.getTimeInMillis() + retry_in, PendingIntent.getService(this, 0, new Intent(this, DexShareCollectionService.class), 0));
alarm.setExact(alarm.RTC_WAKEUP, calendar.getTimeInMillis() + retry_in, PendingIntent.getService(this, 0, new Intent(this, DexShareCollectionService.class), 0));
}
}

Expand Down Expand Up @@ -593,9 +616,10 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
if(mResponseCharacteristic.getUuid().equals(characteristic.getUuid())) {
attemptRead();
}
} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
} else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
device = gatt.getDevice();
state_authInProgress = true;
bondDevice();
} else {
Log.e(TAG, "The phone is trying to read from paired device without encryption. Android Bug?");
Expand All @@ -614,7 +638,7 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
state_authSucess = true;
mBluetoothGatt.readCharacteristic(mHeartBeatCharacteristic);
}
} else if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
} else if ((status & BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) != 0 || (status & BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION) != 0) {
if (gatt.getDevice().getBondState() == BluetoothDevice.BOND_NONE) {
device = gatt.getDevice();
state_authInProgress = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Created by stephenblack on 11/15/14.
*/
public class BgGraphBuilder {
public int fuzzer = (1000 * 60 * 5);
public int fuzzer = (1000 * 30 * 5);
public double end_time = (new Date().getTime() + (60000 * 10)) / fuzzer;
public double start_time = end_time - ((60000 * 60 * 24)) / fuzzer;
public Context context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.R;
Expand Down Expand Up @@ -56,10 +57,16 @@ private Notification notification() {
}

public void start() {
if (run_service_in_foreground) { mService.startForeground(FOREGROUND_ID, notification()); }
if (run_service_in_foreground) {
Log.e("FOREGROUND", "should be moving to foreground");
mService.startForeground(FOREGROUND_ID, notification());
}
}

public void stop() {
if (run_service_in_foreground) { mService.stopForeground(true); }
if (run_service_in_foreground) {
Log.e("FOREGROUND", "should be moving out of foreground");
mService.stopForeground(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void onCreate(Bundle savedInstanceState) {
} else {
otherCategory.removePreference(predictiveBG);
}
if(prefs.getString("dex_collection_method", "BluetoothWixel").compareTo("BluetoothWixel") != 0) {
if(prefs.getString("dex_collection_method", "BluetoothWixel").compareTo("BluetoothWixel") != 0 && prefs.getString("dex_collection_method", "BluetoothWixel").compareTo("DexcomShare") != 0) {
collectionCategory.removePreference(runInForeground);
}
if(prefs.getString("dex_collection_method", "BluetoothWixel").compareTo("WifiWixel") != 0) {
Expand All @@ -186,7 +186,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
otherCategory.addPreference(interpretRaw);
otherCategory.removePreference(predictiveBG);
}
if(((String) newValue).compareTo("BluetoothWixel") != 0) {
if(((String) newValue).compareTo("BluetoothWixel") != 0 && ((String) newValue).compareTo("DexcomShare") != 0) {
collectionCategory.removePreference(runInForeground);
} else {
collectionCategory.addPreference(runInForeground);
Expand Down

0 comments on commit 5f27a91

Please sign in to comment.