Skip to content

Commit

Permalink
Fixed broadcast error
Browse files Browse the repository at this point in the history
  • Loading branch information
TebbeUbben committed Apr 21, 2018
1 parent f2f4241 commit 37c86ba
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.view.View;
import android.widget.FrameLayout;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import sugar.free.sightparser.handling.HistoryBroadcast;
import sugar.free.sightparser.handling.ServiceConnectionCallback;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.StatusCallback;
Expand Down Expand Up @@ -110,6 +111,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
sightServiceConnector.addStatusCallback(statusCallback);
}

protected void requestHistorySync() {
Intent intent = new Intent();
intent.setAction(HistoryBroadcast.ACTION_START_SYNC);
intent.setPackage(getPackageName());
sendBroadcast(intent);
}

protected int getSelectedNavItemID() {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private void updateViews() {
bolus1Progress.getProgressDrawable().setColorFilter(ContextCompat.getColor(this, getBolusColor(bolus1.getBolusType())), PorterDuff.Mode.SRC_IN);
bolus1Cancel.setVisibility(View.VISIBLE);
} else if (bolus1Container.getVisibility() == View.VISIBLE) {
sendBroadcast(new Intent(HistoryBroadcast.ACTION_START_SYNC));
requestHistorySync();
bolus1Container.setVisibility(View.GONE);
}

Expand All @@ -259,7 +259,7 @@ private void updateViews() {
bolus2Cancel.setVisibility(View.VISIBLE);
bolus2Progress.getProgressDrawable().setColorFilter(ContextCompat.getColor(this, getBolusColor(bolus2.getBolusType())), PorterDuff.Mode.SRC_IN);
} else if (bolus2Container.getVisibility() == View.VISIBLE) {
sendBroadcast(new Intent(HistoryBroadcast.ACTION_START_SYNC));
requestHistorySync();
bolus2Container.setVisibility(View.GONE);
}

Expand All @@ -274,7 +274,7 @@ private void updateViews() {
bolus3Progress.getProgressDrawable().setColorFilter(ContextCompat.getColor(this, getBolusColor(bolus3.getBolusType())), PorterDuff.Mode.SRC_IN);
bolus3Cancel.setVisibility(View.VISIBLE);
} else if (bolus3Container.getVisibility() == View.VISIBLE) {
sendBroadcast(new Intent(HistoryBroadcast.ACTION_START_SYNC));
requestHistorySync();
bolus3Container.setVisibility(View.GONE);
}
}
Expand Down Expand Up @@ -338,7 +338,7 @@ protected void connectedToService() {
protected void statusChanged(Status status) {
if (status == Status.CONNECTED) {
taskRunnerRunnable.run();
sendBroadcast(new Intent(HistoryBroadcast.ACTION_START_SYNC));
requestHistorySync();
} else {
if (confirmationDialog != null) confirmationDialog.hide();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected void connectedToService() {
protected void onStart() {
super.onStart();
registerReceiver(broadcastReceiver, new IntentFilter(HistoryBroadcast.ACTION_SYNC_FINISHED));
sendBroadcast(new Intent(HistoryBroadcast.ACTION_START_SYNC));
requestHistorySync();
}

@Override
Expand Down Expand Up @@ -87,7 +87,7 @@ protected boolean snackbarEnabled() {

@Override
public void onRefresh() {
sendBroadcast(new Intent(HistoryBroadcast.ACTION_START_SYNC));
requestHistorySync();
}

private class LoadDataTask extends AsyncTask<Void, Void, List> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public int onStartCommand(Intent intent, int flags, int startId) {
alarmManager.cancel(pendingIntent);
pendingIntent = null;
}
if (syncing) sendBroadcast(new Intent(HistoryBroadcast.ACTION_STILL_SYNCING));
else if (!connector.isConnectedToService()) sendBroadcast(new Intent(HistoryBroadcast.ACTION_SYNC_FINISHED));
if (syncing) HistorySendIntent.sendStillSyncing(this, getAppsWithHistoryPermission());
else if (!connector.isConnectedToService()) HistorySendIntent.sendSyncFinished(this, getAppsWithHistoryPermission());
else startSync();
} else if (HistoryBroadcast.ACTION_START_RESYNC.equals(action)) {
if (historyResync == null)
Expand Down Expand Up @@ -143,6 +143,7 @@ private List<String> getAppsWithHistoryPermission() {
for (String permission : packageInfo.requestedPermissions) {
if (permission.equals("sugar.free.sightremote.HISTORY_BROADCASTS")) {
packagesWithPermission.add(packageInfo.packageName);
Log.d("Permission", packageInfo.packageName);
}
}
}
Expand All @@ -161,7 +162,7 @@ public void onStatusChange(Status status) {
connector.disconnect();
connector.disconnectFromService();
syncing = false;
sendBroadcast(new Intent(HistoryBroadcast.ACTION_SYNC_FINISHED));
HistorySendIntent.sendSyncFinished(this, getAppsWithHistoryPermission());
if (wakeLock.isHeld()) wakeLock.release();
}
}
Expand All @@ -180,7 +181,7 @@ public void onResult(Object result) {
connector.disconnectFromService();
processHistoryFrames(historyFrames);
syncing = false;
sendBroadcast(new Intent(HistoryBroadcast.ACTION_SYNC_FINISHED));
HistorySendIntent.sendSyncFinished(this, getAppsWithHistoryPermission());
if (wakeLock.isHeld()) wakeLock.release();
} else if (result instanceof ReadDateTimeMessage) {
ReadDateTimeMessage dateTimeMessage = (ReadDateTimeMessage) result;
Expand Down Expand Up @@ -531,7 +532,7 @@ public void onError(Exception e) {
connector.disconnect();
connector.disconnectFromService();
syncing = false;
sendBroadcast(new Intent(HistoryBroadcast.ACTION_SYNC_FINISHED));
HistorySendIntent.sendSyncFinished(this, getAppsWithHistoryPermission());
if (wakeLock.isHeld()) wakeLock.release();
CrashlyticsUtil.logExceptionWithCallStackTrace(e);
}
Expand All @@ -540,7 +541,7 @@ private void startSync() {
if (!wakeLock.isHeld()) wakeLock.acquire(60000);
syncing = true;
connector.connectToService();
sendBroadcast(new Intent(HistoryBroadcast.ACTION_SYNC_STARTED));
HistorySendIntent.sendSyncStarted(this, getAppsWithHistoryPermission());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ public static void send(Context context, OccurenceOfAlert occurenceOfAlert, bool
sendBroadcast(context, intent, packages);
}

public static void sendSyncStarted(Context context, List<String> packages) {
Intent intent = new Intent();
intent.setAction(HistoryBroadcast.ACTION_SYNC_STARTED);
sendBroadcast(context, intent, packages);
}

public static void sendSyncFinished(Context context, List<String> packages) {
Intent intent = new Intent();
intent.setAction(HistoryBroadcast.ACTION_SYNC_FINISHED);
sendBroadcast(context, intent, packages);
}

public static void sendStillSyncing(Context context, List<String> packages) {
Intent intent = new Intent();
intent.setAction(HistoryBroadcast.ACTION_STILL_SYNCING);
sendBroadcast(context, intent, packages);
}

private static void sendBroadcast(Context context, Intent intent, List<String> packages) {
for (String p : packages) {
Intent copy = new Intent(intent);
Expand Down

0 comments on commit 37c86ba

Please sign in to comment.