Skip to content

Commit

Permalink
Optimizing notification actions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRolfFR committed Feb 26, 2021
1 parent 6bc42ad commit e6c09c7
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public class NotificationController {

private static final int NOTIFICATION_ID = 234;
private static final String NOTIFICATION_CHANNEL_ID = "optymo_next_times_01";
private static final CharSequence NOTIFICATION_CHANNEL_NAME = "optymo_next_times";
private static final String NOTIFICATION_CHANNEL_DESCRIPTION = "Channel only used to show persistent next times notification";
private static CharSequence NOTIFICATION_CHANNEL_NAME = "Next stops";
private static String NOTIFICATION_CHANNEL_DESCRIPTION = "Persistent notification with next stops";

public static boolean isNotificationShown(Context context) {
SharedPreferences preferences = context.getSharedPreferences("NotificationPrefs", Context.MODE_PRIVATE);
Expand Down Expand Up @@ -154,9 +154,13 @@ void run(Context context) {
// channel for greater versions than 8.0
NotificationChannel mChannel = null; // = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// set category description to current local
NOTIFICATION_CHANNEL_NAME = context.getResources().getString(R.string.notification_channel_title);
NOTIFICATION_CHANNEL_DESCRIPTION = context.getResources().getString(R.string.notification_channel_description);

// cannot be stored as constant because require api 24 min
int importance = NotificationManager.IMPORTANCE_MIN;
// fix with low priority for Xiaomi where importance min is not allowed on the lock screen, set low or higher
int importance = NotificationManager.IMPORTANCE_DEFAULT; // I want to be sure to be displayed
mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
mChannel.setDescription(NOTIFICATION_CHANNEL_DESCRIPTION);
mChannel.setShowBadge(false);
Expand Down Expand Up @@ -190,11 +194,16 @@ void run(Context context) {
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.setCustomBigContentView(notificationLayoutExpanded)
.setOngoing(true)
.setPriority(Notification.PRIORITY_LOW)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) //to show content in lock screen
.setContentTitle(context.getString(R.string.update_never))
.setContentIntent(resultPendingIntent)
.setContentText(context.getString(R.string.notification_content_text));
.setContentText(context.getString(R.string.notification_content_text))
.setNotificationSilent(); // have no sound for this notification

// set importance was deprecated in API 26
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
builder.setPriority(Notification.PRIORITY_DEFAULT);
}

//noinspection ConstantConditions
if (mChannel != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class NotificationService extends IntentService {
public static final String REFRESH_ACTION = "refresh_action";
public static final String NEXT_SIX_GET = "next_six_get";
public static final String CANCEL_ACTION = "notification_cancel_all";
private NotificationController notificationController = new NotificationController();

private int numberOfRequests;
private ArrayList<NextTimeRequest> lastRequests = new ArrayList<>();
Expand All @@ -30,6 +29,10 @@ public NotificationService() {
super("MyNotificationService");
}

private static NotificationController getNotificationController() {
return OnBoot.getNotificationController();
}

@Override
protected void onHandleIntent(@Nullable Intent workIntent) {
if(workIntent != null && workIntent.getAction() != null) {
Expand All @@ -44,6 +47,7 @@ private void handleAction(String action) {
OptymoDirection[] fav = ((GlobalApplication) getApplication()).getFavoritesController().getFavorites();
// Log.d("optymo", "" + fav.length);

NotificationController notificationController = getNotificationController();
// if action is cancel we cancel
if(action.equals(CANCEL_ACTION)) {
notificationController.cancelNotification(this);
Expand Down Expand Up @@ -83,8 +87,9 @@ private void handleAction(String action) {
while(lastRequests.size() > 0) {
NextTimeRequest request = lastRequests.get(0);
request.cancel(true);
lastRequests.clear();
}
//noinspection RedundantOperationOnEmptyContainer
lastRequests.clear();

// make maximum 6 requests
int rNumber = 0;
Expand Down Expand Up @@ -158,11 +163,11 @@ protected void onPostExecute(OptymoNextTime nextTime) {

service.numberOfRequests++;
if (service.numberOfRequests >= service.lastRequests.size())
service.notificationController.setUpdatedAtTitle(service);
getNotificationController().setUpdatedAtTitle(service);

// add line to notification if first 6
if(service.numberOfRequests < 7)
service.notificationController.appendToNotificationBody(nextTime);
getNotificationController().appendToNotificationBody(nextTime);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public class OnBoot extends BroadcastReceiver {

private static int index;
private static NotificationController nCon;

public static int getIndex() {
return index;
Expand All @@ -22,6 +23,14 @@ public static void increaseIndex(int value) {
OnBoot.index += value;
}

public static NotificationController getNotificationController() {
if(nCon == null) {
nCon = new NotificationController();
}

return nCon;
}

@SuppressLint("UnsafeProtectedBroadcastReceiver")
@Override
public void onReceive(Context context, Intent intent) {
Expand Down
39 changes: 24 additions & 15 deletions app/src/main/res/layout/notification_expanded.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,47 @@
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher_background"
android:gravity="center"
android:orientation="horizontal"
android:paddingStart="@dimen/notification_padding"
android:paddingTop="@dimen/component_padding_half"
android:paddingEnd="8dp"
android:paddingBottom="@dimen/component_padding_half"
tools:ignore="UseCompoundDrawables">
android:paddingBottom="@dimen/component_padding_half">

<ImageView
android:id="@+id/imageView2"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:contentDescription="@string/content_description_background"
android:scaleType="fitCenter"
android:src="@drawable/ic_logo_form" />

<TextView
android:id="@+id/notification_expanded_title"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_weight="5"
android:paddingLeft="@dimen/component_padding_half"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/notification_expanded_refresh"
android:layout_toEndOf="@id/imageView2"
android:paddingStart="@dimen/component_padding_half"
android:paddingEnd="0dp"
android:text="@string/update_never"
android:textAppearance="@style/TextAppearance.Compat.Notification.Title"
android:textColor="@color/colorOnPrimary"
android:textSize="16sp"
tools:ignore="RtlHardcoded,RtlSymmetry"
tools:text="Sample title" />
android:textSize="16sp" />

<ImageButton
android:id="@+id/notification_expanded_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/notification_expanded_next"
android:background="@android:color/transparent"
android:contentDescription="@string/content_description"
android:padding="2dp"
Expand All @@ -52,19 +56,24 @@
android:id="@+id/notification_expanded_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:contentDescription="@string/content_description"
android:padding="2dp"
android:src="@drawable/ic_next"
android:tint="@android:color/white" />

</LinearLayout>
</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="@dimen/notification_padding">
android:paddingStart="@dimen/notification_padding"
android:paddingTop="@dimen/component_padding_half"
android:paddingEnd="@dimen/notification_padding"
android:paddingBottom="@dimen/notification_padding">

<LinearLayout
android:id="@+id/notification_next_stop_0"
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@
<string name="map_more_infos">Presionar para ver los proximos horarios</string>
<string name="main_lines_pdf_static">URLs estáticos del %1$s</string>
<string name="main_lines_pdf_dynamic">Últimos pdfs en línea</string>
<string name="main_lines_pdf_loading">Cargamento...</string>
<string name="main_lines_pdf_loading">Cargamento…</string>
<string name="notification_channel_title">Proximos horarios</string>
<string name="notification_channel_description">Notificación persistente con los proximos horarios</string>
</resources>
6 changes: 4 additions & 2 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<string name="dialog_no">non</string>
<string name="notification_refresh_button_text">Actualiser</string>
<string name="notification_content_text">Étendre pour voir les favoris</string>
<string name="update_pending">Mise à jour en cours…</string>
<string name="update_pending">Mise à jour…</string>
<string name="dialog_search_input_hint">Chercher un arrêt, un stop…</string>
<string name="stops">Arrêts</string>
<string name="stop_next_stops">Prochains horaires</string>
Expand All @@ -55,5 +55,7 @@
<string name="map_more_infos">Appuyer pour voir les prochains horaires</string>
<string name="main_lines_pdf_static">URLs statiques du %1$s</string>
<string name="main_lines_pdf_dynamic">Derniers PDFs en ligne</string>
<string name="main_lines_pdf_loading">Chargement...</string>
<string name="main_lines_pdf_loading">Chargement…</string>
<string name="notification_channel_title">Prochains arrêts</string>
<string name="notification_channel_description">Notification persistente avec les prochains arrêts</string>
</resources>
6 changes: 4 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<string name="dialog_no">no</string>
<string name="notification_refresh_button_text">Refresh</string>
<string name="notification_content_text">Expand to see favorites</string>
<string name="update_pending">Update pending…</string>
<string name="update_pending">Updating…</string>
<string name="dialog_search_input_hint">Search stop, line…</string>
<string name="stops">Stops</string>
<string name="stop_next_stops">Next stops</string>
Expand Down Expand Up @@ -65,5 +65,7 @@
<string name="map_more_infos">Tap on me to get next times</string>
<string name="main_lines_pdf_static">Static URLs of %1$s</string>
<string name="main_lines_pdf_dynamic">Latest PDFs online</string>
<string name="main_lines_pdf_loading">Loading...</string>
<string name="main_lines_pdf_loading">Loading…</string>
<string name="notification_channel_title">Next stops</string>
<string name="notification_channel_description">Persistent notification with next stops</string>
</resources>

0 comments on commit e6c09c7

Please sign in to comment.