|
1 | 1 | package io.snabble.sdk; |
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint; |
3 | 4 | import android.app.Activity; |
4 | 5 | import android.app.Application; |
| 6 | +import android.content.BroadcastReceiver; |
| 7 | +import android.content.Context; |
| 8 | +import android.content.Intent; |
| 9 | +import android.content.IntentFilter; |
5 | 10 | import android.content.pm.PackageInfo; |
6 | 11 | import android.content.pm.PackageManager; |
| 12 | +import android.net.ConnectivityManager; |
| 13 | +import android.net.NetworkInfo; |
7 | 14 | import android.util.Base64; |
8 | 15 |
|
9 | 16 | import com.google.gson.JsonArray; |
@@ -132,6 +139,7 @@ protected void onError() { |
132 | 139 | } |
133 | 140 |
|
134 | 141 | app.registerActivityLifecycleCallbacks(activityLifecycleCallbacks); |
| 142 | + app.registerReceiver(networkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); |
135 | 143 | } |
136 | 144 |
|
137 | 145 | public String getVersionName() { |
@@ -385,7 +393,7 @@ private void checkCartTimeouts() { |
385 | 393 | } |
386 | 394 | } |
387 | 395 |
|
388 | | - private void processSavedCheckouts() { |
| 396 | + private void processPendingCheckouts() { |
389 | 397 | for (Project project : projects) { |
390 | 398 | project.getCheckout().processPendingCheckouts(); |
391 | 399 | } |
@@ -414,9 +422,29 @@ public interface OnMetadataUpdateListener { |
414 | 422 | public void onActivityStarted(Activity activity) { |
415 | 423 | updateMetadata(); |
416 | 424 | checkCartTimeouts(); |
| 425 | + processPendingCheckouts(); |
417 | 426 | } |
418 | 427 | }; |
419 | 428 |
|
| 429 | + @SuppressLint("MissingPermission") |
| 430 | + private BroadcastReceiver networkChangeReceiver = new BroadcastReceiver() { |
| 431 | + @Override |
| 432 | + public void onReceive(Context context, Intent intent) { |
| 433 | + ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 434 | + |
| 435 | + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); |
| 436 | + if (activeNetwork != null) { |
| 437 | + onConnectionStateChanged(activeNetwork.isConnected()); |
| 438 | + } |
| 439 | + } |
| 440 | + }; |
| 441 | + |
| 442 | + private void onConnectionStateChanged(boolean isConnected) { |
| 443 | + if (isConnected) { |
| 444 | + processPendingCheckouts(); |
| 445 | + } |
| 446 | + } |
| 447 | + |
420 | 448 | /** |
421 | 449 | * Enables debug logging. |
422 | 450 | */ |
|
0 commit comments