-
Notifications
You must be signed in to change notification settings - Fork 16
Notify when network is not present #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,18 +7,13 @@ | |
| package org.dpppt.android.app; | ||
|
|
||
| import android.app.Application; | ||
| import android.app.Notification; | ||
| import android.app.NotificationChannel; | ||
| import android.app.NotificationManager; | ||
| import android.app.PendingIntent; | ||
| import android.content.BroadcastReceiver; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.content.SharedPreferences; | ||
| import android.os.Build; | ||
| import androidx.annotation.RequiresApi; | ||
| import androidx.core.app.NotificationCompat; | ||
|
|
||
| import org.dpppt.android.app.network.ScheduleMonitoringStatusNetwork; | ||
| import org.dpppt.android.app.notifications.NotificationService; | ||
| import org.dpppt.android.sdk.DP3T; | ||
| import org.dpppt.android.sdk.TracingStatus; | ||
| import org.dpppt.android.sdk.internal.backend.models.ApplicationInfo; | ||
|
|
@@ -27,13 +22,17 @@ | |
|
|
||
| public class MainApplication extends Application { | ||
|
|
||
| private static final String NOTIFICATION_CHANNEL_ID = "contact-channel"; | ||
| private NotificationService notificationService; | ||
|
|
||
| @Override | ||
| public void onCreate() { | ||
| super.onCreate(); | ||
|
|
||
| notificationService = new NotificationService(getApplicationContext()); | ||
|
|
||
| if (ProcessUtil.isMainProcess(this)) { | ||
| registerReceiver(broadcastReceiver, DP3T.getUpdateIntentFilter()); | ||
| ScheduleMonitoringStatusNetwork.scheduleJob(this); | ||
| DP3T.init(this, new ApplicationInfo("it.noiapp.demo", "https://protetti.app/")); | ||
| } | ||
| } | ||
|
|
@@ -56,42 +55,17 @@ public void onReceive(Context context, Intent intent) { | |
| if (status.wasContactExposed()) { | ||
|
|
||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
| createNotificationChannel(); | ||
| } | ||
|
|
||
| Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName()); | ||
| PendingIntent contentIntent = null; | ||
| if (launchIntent != null) { | ||
| contentIntent = | ||
| PendingIntent.getActivity(context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT); | ||
| notificationService.createNotificationChannel(); | ||
| } | ||
| Notification notification = | ||
| new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) | ||
| .setContentTitle(context.getString(R.string.push_exposed_title)) | ||
| .setContentText(context.getString(R.string.push_exposed_text)) | ||
| .setPriority(NotificationCompat.PRIORITY_MAX) | ||
| .setSmallIcon(R.drawable.ic_begegnungen) | ||
| .setContentIntent(contentIntent) | ||
| .build(); | ||
|
|
||
| NotificationManager notificationManager = | ||
| (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | ||
| notificationManager.notify(42, notification); | ||
|
|
||
| prefs.edit().putBoolean("notification_shown", true).commit(); | ||
| notificationService.pushNotification( | ||
| context.getString(R.string.push_exposed_title), | ||
| context.getString(R.string.push_exposed_text), | ||
| NotificationService.NOTIFICATION_ID | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| @RequiresApi(api = Build.VERSION_CODES.O) | ||
| private void createNotificationChannel() { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move in new class |
||
| NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | ||
| String channelName = getString(R.string.app_name); | ||
| NotificationChannel channel = | ||
| new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH); | ||
| channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); | ||
| notificationManager.createNotificationChannel(channel); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
|
|
||
| public class MainActivity extends FragmentActivity { | ||
|
|
||
| private static final String PREFS_COVID = "PREFS_COVID"; | ||
| public static final String PREFS_COVID = "PREFS_COVID"; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change visibility to use in other parts |
||
| private static final String PREF_KEY_ONBOARDING_COMPLETED = "PREF_KEY_ONBOARDING_COMPLETED"; | ||
|
|
||
| private static final int REQ_ONBOARDING = 123; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
| import android.content.res.ColorStateList; | ||
| import android.os.Bundle; | ||
| import android.view.View; | ||
| import android.widget.TextView; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.fragment.app.Fragment; | ||
|
|
@@ -18,6 +20,7 @@ | |
| import org.dpppt.android.app.R; | ||
| import org.dpppt.android.app.contacts.ContactsFragment; | ||
| import org.dpppt.android.app.debug.DebugFragment; | ||
| import org.dpppt.android.app.main.model.AppState; | ||
| import org.dpppt.android.app.util.DebugUtils; | ||
| import org.dpppt.android.app.main.views.HeaderView; | ||
| import org.dpppt.android.app.notifications.NotificationsFragment; | ||
|
|
@@ -67,6 +70,18 @@ private void setupHeader(View view) { | |
| headerView = view.findViewById(R.id.main_header_container); | ||
| tracingViewModel.getAppStateLiveData() | ||
| .observe(getViewLifecycleOwner(), appState -> headerView.setState(appState)); | ||
|
|
||
| tracingViewModel.getNetworkEnableLiveData() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adds new liveData to intercept changes of the network connection by change the UI according to this input |
||
| .observe(getViewLifecycleOwner(), | ||
| networkStatus -> { | ||
|
|
||
| if (networkStatus) { | ||
| headerView.setState(AppState.NETWORK_OK); | ||
| return; | ||
| } | ||
|
|
||
| headerView.setState(AppState.NETWORK_KO); | ||
| }); | ||
| } | ||
|
|
||
| private void setupCards(View view) { | ||
|
|
@@ -78,16 +93,23 @@ private void setupCards(View view) { | |
| View contactStatusView = view.findViewById(R.id.contacts_status); | ||
| tracingViewModel.getTracingEnabledLiveData().observe(getViewLifecycleOwner(), | ||
| isTracing -> { | ||
| List<TracingStatus.ErrorState> errors = tracingViewModel.getErrorsLiveData().getValue(); | ||
| TracingStatusHelper.State state = errors.size() > 0 || !isTracing ? TracingStatusHelper.State.WARNING : | ||
| TracingStatusHelper.State.OK; | ||
| int titleRes = state == TracingStatusHelper.State.OK ? R.string.tracing_active_title | ||
| : R.string.tracing_error_title; | ||
| int textRes = state == TracingStatusHelper.State.OK ? R.string.tracing_active_text | ||
| : R.string.tracing_error_text; | ||
| TracingStatusHelper.updateStatusView(contactStatusView, state, titleRes, textRes); | ||
| assignInformationNotice(contactStatusView, isTracing); | ||
| }); | ||
|
|
||
| tracingViewModel.getNetworkEnableLiveData() | ||
| .observe(getViewLifecycleOwner(), | ||
| networkStatus -> { | ||
| Boolean status = tracingViewModel.getTracingEnabledLiveData().getValue(); | ||
| assignInformationNotice(contactStatusView, status); | ||
|
|
||
| if (!networkStatus) { | ||
| TextView textView = contactStatusView.findViewById(R.id.status_text); | ||
| textView.setText(String.format("%s %s", | ||
| textView.getText(), | ||
| getString(R.string.warning_text_network_ko))); | ||
| } | ||
| }); | ||
|
|
||
| view.findViewById(R.id.card_notifications).setOnClickListener( | ||
| v -> getParentFragmentManager().beginTransaction() | ||
| .replace(R.id.main_fragment_container, NotificationsFragment.newInstance()) | ||
|
|
@@ -123,6 +145,22 @@ private void setupCards(View view) { | |
| }); | ||
| } | ||
|
|
||
| private void assignInformationNotice(View contactStatusView, Boolean isTracing) { | ||
| TracingStatusHelper.State state = revealTracingStatus(isTracing); | ||
| int titleRes = state == TracingStatusHelper.State.OK ? R.string.tracing_active_title | ||
| : R.string.tracing_error_title; | ||
| int textRes = state == TracingStatusHelper.State.OK ? R.string.tracing_active_text | ||
| : R.string.tracing_error_text; | ||
| TracingStatusHelper.updateStatusView(contactStatusView, state, titleRes, textRes); | ||
| } | ||
|
|
||
| private TracingStatusHelper.State revealTracingStatus(Boolean isTracing) { | ||
| List<TracingStatus.ErrorState> errors = tracingViewModel.getErrorsLiveData().getValue(); | ||
| assert errors != null; | ||
| return errors.size() > 0 || !isTracing ? TracingStatusHelper.State.WARNING : | ||
| TracingStatusHelper.State.OK; | ||
| } | ||
|
|
||
| private void setupDebugButton(View view) { | ||
| View debugButton = view.findViewById(R.id.main_button_debug); | ||
| if (DebugUtils.isDev()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,5 +10,7 @@ public enum AppState { | |
| TRACING, | ||
| ERROR, | ||
| EXPOSED, | ||
| EXPOSED_ERROR | ||
| EXPOSED_ERROR, | ||
| NETWORK_OK, | ||
| NETWORK_KO, | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JobService to register a callback or a receiver for first time