Last active
February 8, 2020 10:45
-
-
Save piyush-malaviya/1840a4731a85492f24fde9635dbcb4f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Notification; | |
import android.app.NotificationChannel; | |
import android.app.NotificationManager; | |
import android.app.Service; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.BitmapFactory; | |
import android.location.Location; | |
import android.os.Build; | |
import android.os.IBinder; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.NotificationCompat; | |
import android.support.v4.app.NotificationManagerCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.text.TextUtils; | |
import android.widget.Toast; | |
import com.google.android.gms.location.LocationRequest; | |
import java.text.DecimalFormat; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.Locale; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
public class LocationSharingService extends Service implements LocationHelper.LocationUpdateListener { | |
private static final String START_LOCATION_SHARE = "START_LOCATION_SHARE"; | |
private static final String STOP_LOCATION_SHARE = "STOP_LOCATION_SHARE"; | |
private static final String FORCE_STOP_LOCATION_SHARE = "FORCE_STOP_LOCATION_SHARE"; | |
private static final String REQUEST_ID = "REQUEST_ID"; | |
private static final String CHANNEL_ID = "SEND_LOCATION"; | |
private static final long UPDATE_TIME = TimeUnit.SECONDS.toMillis(20); | |
private static final long UPDATE_METER = 5; | |
private static final String TAG = LocationSharingService.class.getSimpleName(); | |
private int NOTIFICATION_ID = 111; | |
//private boolean isStartForeground = false; | |
private LocationHelper mLocationHelper; | |
private String mRequestId = ""; | |
public static void startLocationShare(Context context, String requestId) { | |
Intent intent = new Intent(context, LocationSharingService.class); | |
intent.setAction(START_LOCATION_SHARE); | |
intent.putExtra(REQUEST_ID, requestId); | |
ContextCompat.startForegroundService(context, intent); | |
} | |
public static void stopLocationShare(Context context, String requestId) { | |
Intent intent = new Intent(context, LocationSharingService.class); | |
intent.setAction(STOP_LOCATION_SHARE); | |
intent.putExtra(REQUEST_ID, requestId); | |
ContextCompat.startForegroundService(context, intent); | |
} | |
public static void forceStopLocationShare(Context context) { | |
Intent intent = new Intent(context, LocationSharingService.class); | |
intent.setAction(FORCE_STOP_LOCATION_SHARE); | |
intent.putExtra(REQUEST_ID, ""); | |
ContextCompat.startForegroundService(context, intent); | |
} | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
createNotificationChannel(); | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID); | |
startForeground(NOTIFICATION_ID, builder.build()); | |
} | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
if (intent != null) { | |
String action = intent.getAction(); | |
if (!TextUtils.isEmpty(action)) { | |
String id = intent.getStringExtra(REQUEST_ID); | |
switch (action) { | |
case START_LOCATION_SHARE: | |
mRequestId = id; | |
startLocationSharing(); | |
break; | |
case STOP_LOCATION_SHARE: | |
if (mRequestId.equals(id)) { | |
stopLocationSharing(); | |
} | |
break; | |
case FORCE_STOP_LOCATION_SHARE: | |
stopLocationSharing(); | |
break; | |
} | |
} | |
} | |
return START_NOT_STICKY; | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if (mLocationHelper != null) { | |
mLocationHelper.stop(); | |
} | |
} | |
private void createNotificationChannel(String channelId) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
// Create the NotificationChannel, but only on API 26+ because | |
// the NotificationChannel class is new and not in the support library | |
CharSequence name = getString(R.string.app_name); | |
String description = getString(R.string.app_name); | |
NotificationChannel channel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_DEFAULT); | |
channel.setDescription(description); | |
channel.setSound(null, null); | |
// Register the channel with the system | |
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
if (notificationManager != null) { | |
notificationManager.createNotificationChannel(channel); | |
} | |
} | |
} | |
private int getNotificationIcon() { | |
boolean whiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); | |
return whiteIcon ? R.drawable.ic_notification_small : R.mipmap.ic_launcher; | |
} | |
private void showLocationSharingNotification() { | |
String messageText = "Location sharing in progress"; | |
createNotificationChannel(CHANNEL_ID); | |
//Intent intent = new Intent(this, SplashActivity.class); | |
//PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID); | |
builder.setContentTitle(getString(R.string.app_name)) | |
.setContentText(messageText) | |
.setSmallIcon(getNotificationIcon()) | |
//.setContentIntent(pendingIntent) | |
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) | |
.setAutoCancel(true); | |
//if (!isStartForeground) { | |
// isStartForeground = true; | |
startForeground(NOTIFICATION_ID, builder.build()); | |
//} | |
} | |
private void showToast(String msg) { | |
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); | |
} | |
private void updateNotification(String lat, String lng) { | |
//String messageText = "Sharing location (" + lat + ", " + lng + ")"; | |
//String title = getString(R.string.app_name); | |
String title = "Location sharing in progress"; | |
String messageText = String.format(Locale.ENGLISH, "Lat: %s\nLng: %s\nID: %s", lat, lng, mRequestId); | |
createNotificationChannel(CHANNEL_ID); | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID); | |
builder.setContentTitle(title) | |
//.setContentText(messageText) | |
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageText)) | |
.setSmallIcon(R.drawable.ic_notification_small) | |
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) | |
.setAutoCancel(true); | |
NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, builder.build()); | |
} | |
private void startLocationSharing() { | |
showLocationSharingNotification(); | |
if (mLocationHelper == null) { | |
mLocationHelper = new LocationHelper(this) | |
.setInterval(UPDATE_TIME) | |
.setFastestInterval(UPDATE_TIME / 2) | |
//.setSmallestDisplacement(UPDATE_METER) | |
.setLocationUpdateListener(this) | |
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
} | |
mLocationHelper.requestContinuousLocation(); | |
} | |
private void stopLocationSharing() { | |
if (mLocationHelper != null) { | |
mLocationHelper.stop(); | |
} | |
NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID); | |
stopForeground(true); | |
} | |
@Override | |
public void onLocationUpdate(Location location) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment