|
| 1 | +/* |
| 2 | + * Transportr |
| 3 | + * |
| 4 | + * Copyright (c) 2013 - 2025 Torsten Grote |
| 5 | + * |
| 6 | + * This program is Free Software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as |
| 8 | + * published by the Free Software Foundation, either version 3 of the |
| 9 | + * License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package de.grobox.transportr; |
| 21 | + |
| 22 | +import android.Manifest; |
| 23 | +import android.app.PendingIntent; |
| 24 | +import android.app.Service; |
| 25 | +import static androidx.core.app.NotificationManagerCompat.IMPORTANCE_MAX; |
| 26 | +import android.content.Context; |
| 27 | +import android.content.Intent; |
| 28 | +import android.content.pm.PackageManager; |
| 29 | +import android.content.pm.ServiceInfo; |
| 30 | +import android.location.Location; |
| 31 | +import android.location.LocationListener; |
| 32 | +import android.location.LocationManager; |
| 33 | +import android.os.Build; |
| 34 | +import android.os.Handler; |
| 35 | +import android.os.IBinder; |
| 36 | +import android.os.Looper; |
| 37 | + |
| 38 | +import androidx.annotation.NonNull; |
| 39 | +import androidx.annotation.Nullable; |
| 40 | +import androidx.core.app.ActivityCompat; |
| 41 | +import androidx.core.app.NotificationChannelCompat; |
| 42 | +import androidx.core.app.NotificationCompat; |
| 43 | +import androidx.core.app.NotificationManagerCompat; |
| 44 | +import androidx.core.app.NotificationCompat.Builder; |
| 45 | + |
| 46 | +public class AlertService extends Service implements LocationListener { |
| 47 | + private LocationManager mLocManager = null; |
| 48 | + private boolean isWatchdogRunning = false; |
| 49 | + private long lastLocationUpdate = 0; |
| 50 | + private NotificationManagerCompat mNotifManager = null; |
| 51 | + private static final int NOTIF_ID = 111; |
| 52 | + private static final String CHANNEL_ID = "alert"; |
| 53 | + private Builder mNotifBuilder; |
| 54 | + private String destinationName; |
| 55 | + private String arrivalTime; |
| 56 | + private long arrivalTimeLong; |
| 57 | + private Location destination; |
| 58 | + private PendingIntent stopPendingIntent; |
| 59 | + private static final long ARRIVAL_THRESHOLD_METERS = 150; |
| 60 | + private static final long ARRIVAL_THRESHOLD_SEC = 30; |
| 61 | + private static final long WATCHDOG_INTERVAL_MS = 1_000; // 1 second |
| 62 | + private static final long LOCATION_INTERVAL_MS = 2_000; // 2 seconds |
| 63 | + private static final long LOCATION_TIMEOUT_MS = 10_000; // 10 seconds |
| 64 | + private static final String ACTION_STOP = "STOP"; |
| 65 | + private final Handler handler = new Handler(Looper.getMainLooper()); |
| 66 | + private final Runnable watchdogRunnable = new Runnable() { |
| 67 | + @Override |
| 68 | + public void run() { |
| 69 | + long now = System.currentTimeMillis(); |
| 70 | + if (now - lastLocationUpdate > LOCATION_TIMEOUT_MS) { |
| 71 | + // No location update in the last 10 seconds → take action |
| 72 | + onLocationUpdateTimeout(); |
| 73 | + } |
| 74 | + // Reschedule the check |
| 75 | + if (isWatchdogRunning) handler.postDelayed(this, WATCHDOG_INTERVAL_MS); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + @Nullable |
| 80 | + @Override |
| 81 | + public IBinder onBind(Intent intent) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void onCreate() { |
| 87 | + super.onCreate(); |
| 88 | + mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); |
| 89 | + mNotifManager = NotificationManagerCompat.from(getApplicationContext()); |
| 90 | + NotificationChannelCompat notifChannel = new NotificationChannelCompat.Builder(CHANNEL_ID, IMPORTANCE_MAX).setName(getResources().getString(R.string.action_alert)).setVibrationEnabled(true).build(); |
| 91 | + mNotifManager.createNotificationChannel(notifChannel); |
| 92 | + |
| 93 | + Intent stopIntent = new Intent(this, AlertService.class); |
| 94 | + stopIntent.setAction(ACTION_STOP); |
| 95 | + stopPendingIntent = PendingIntent.getService( |
| 96 | + this, |
| 97 | + 0, |
| 98 | + stopIntent, |
| 99 | + PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int onStartCommand(Intent intent, int flags, int startId) { |
| 105 | + if (intent == null) { |
| 106 | + // Service restarted by system, but we lack destination data → stop. |
| 107 | + stopSelf(); |
| 108 | + return START_NOT_STICKY; |
| 109 | + } |
| 110 | + if (!ACTION_STOP.equals(intent.getAction())) { |
| 111 | + handler.removeCallbacksAndMessages(null); |
| 112 | + arrivalTime = intent.getStringExtra("EXTRA_TIME_STR"); |
| 113 | + arrivalTimeLong = intent.getLongExtra("EXTRA_TIME_LONG",0L); |
| 114 | + destinationName = intent.getStringExtra("EXTRA_LOCATION_NAME"); |
| 115 | + |
| 116 | + double latitude = intent.getDoubleExtra("EXTRA_LATITUDE", 0.0); |
| 117 | + double longitude = intent.getDoubleExtra("EXTRA_LONGITUDE", 0.0); |
| 118 | + destination = new Location("manual"); |
| 119 | + destination.setLatitude(latitude); |
| 120 | + destination.setLongitude(longitude); |
| 121 | + lastLocationUpdate = 0; |
| 122 | + showNotif(); |
| 123 | + startGpsLocListener(); |
| 124 | + isWatchdogRunning = true; |
| 125 | + handler.postDelayed(watchdogRunnable, WATCHDOG_INTERVAL_MS); |
| 126 | + return START_STICKY; |
| 127 | + } else { |
| 128 | + stopSelf(); |
| 129 | + return START_NOT_STICKY; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private void showNotif() { |
| 134 | + updateNotification(null, false); |
| 135 | + |
| 136 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 137 | + startForeground(NOTIF_ID, mNotifBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION); |
| 138 | + } else { |
| 139 | + startForeground(NOTIF_ID, mNotifBuilder.build()); |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + private void startGpsLocListener() { |
| 145 | + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
| 146 | + return; |
| 147 | + } |
| 148 | + mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_INTERVAL_MS, 0, this); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void onLocationChanged(@NonNull Location location) { |
| 153 | + lastLocationUpdate = System.currentTimeMillis(); |
| 154 | + long timeToDestination = (arrivalTimeLong - System.currentTimeMillis()) / 1000; |
| 155 | + String timeString = (timeToDestination > 60) ? getString(R.string.in_x_minutes, Math.round(timeToDestination / 60.0)) : getString(R.string.seconds, timeToDestination); |
| 156 | + long distanceToDestination = (long) destination.distanceTo(location); |
| 157 | + if (distanceToDestination > ARRIVAL_THRESHOLD_METERS){ |
| 158 | + updateNotification(getString(R.string.meter, distanceToDestination) + " / " + timeString , false); |
| 159 | + } else { |
| 160 | + updateNotification(null, true); |
| 161 | + mLocManager.removeUpdates(this); |
| 162 | + isWatchdogRunning = false; |
| 163 | + handler.postDelayed(this::stopSelf, 30000); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private void onLocationUpdateTimeout() { |
| 168 | + long timeToDestination = (arrivalTimeLong - System.currentTimeMillis()) / 1000; |
| 169 | + String timeString = (timeToDestination > 60) ? getString(R.string.in_x_minutes, Math.round(timeToDestination / 60.0)) : getString(R.string.seconds, timeToDestination); |
| 170 | + if (timeToDestination > ARRIVAL_THRESHOLD_SEC){ |
| 171 | + updateNotification( timeString , false); |
| 172 | + } else { |
| 173 | + updateNotification(null, true); |
| 174 | + mLocManager.removeUpdates(this); |
| 175 | + isWatchdogRunning = false; |
| 176 | + handler.postDelayed(this::stopSelf, 30000); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void updateNotification(@Nullable String contentText, boolean hasArrived) { |
| 181 | + mNotifBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) |
| 182 | + .setSilent(!hasArrived) |
| 183 | + .setOnlyAlertOnce(!hasArrived) // or adjust as needed |
| 184 | + .setSmallIcon(R.drawable.ic_transportr) |
| 185 | + .setPriority(hasArrived ? NotificationCompat.PRIORITY_MAX : NotificationCompat.PRIORITY_DEFAULT) //ignored on Android 8+ |
| 186 | + .setAutoCancel(false) |
| 187 | + .setOngoing(true) |
| 188 | + .addAction(R.drawable.ic_stop, getString(R.string.action_stop), stopPendingIntent) |
| 189 | + .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) |
| 190 | + .setContentTitle((hasArrived ? "\ud83c\udfc1 " : "") + destinationName + " " + arrivalTime); //Unicode Character "🏁" (U+1F3C1) |
| 191 | + |
| 192 | + if (contentText != null) { |
| 193 | + mNotifBuilder.setContentText(contentText); |
| 194 | + } |
| 195 | + |
| 196 | + mNotifManager.notify(NOTIF_ID, mNotifBuilder.build()); |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void onDestroy() { |
| 201 | + handler.removeCallbacks(watchdogRunnable); |
| 202 | + mLocManager.removeUpdates(this); |
| 203 | + super.onDestroy(); |
| 204 | + } |
| 205 | +} |
0 commit comments