|
| 1 | +package com.omkarmoghe.pokemap.controllers.service; |
| 2 | + |
| 3 | +import android.app.NotificationManager; |
| 4 | +import android.app.PendingIntent; |
| 5 | +import android.app.Service; |
| 6 | +import android.content.BroadcastReceiver; |
| 7 | +import android.content.Context; |
| 8 | +import android.content.Intent; |
| 9 | +import android.content.IntentFilter; |
| 10 | +import android.location.Location; |
| 11 | +import android.os.IBinder; |
| 12 | +import android.support.annotation.Nullable; |
| 13 | +import android.support.v4.app.NotificationCompat; |
| 14 | +import android.util.Log; |
| 15 | + |
| 16 | +import com.google.android.gms.common.ConnectionResult; |
| 17 | +import com.google.android.gms.maps.model.LatLng; |
| 18 | +import com.omkarmoghe.pokemap.R; |
| 19 | +import com.omkarmoghe.pokemap.controllers.app_preferences.PokemapSharedPreferences; |
| 20 | +import com.omkarmoghe.pokemap.controllers.map.LocationManager; |
| 21 | +import com.omkarmoghe.pokemap.controllers.net.NianticManager; |
| 22 | +import com.omkarmoghe.pokemap.models.events.CatchablePokemonEvent; |
| 23 | +import com.omkarmoghe.pokemap.views.MainActivity; |
| 24 | +import com.pokegoapi.api.map.pokemon.CatchablePokemon; |
| 25 | + |
| 26 | +import org.greenrobot.eventbus.EventBus; |
| 27 | +import org.greenrobot.eventbus.Subscribe; |
| 28 | + |
| 29 | +import java.util.List; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | + |
| 33 | +public class PokemonNotificationService extends Service{ |
| 34 | + private static final int notificationId = 2423235; |
| 35 | + private static final String ACTION_STOP_SELF = "com.omkarmoghe.pokemap.STOP_SERVICE"; |
| 36 | + |
| 37 | + private UpdateRunnable updateRunnable; |
| 38 | + private Thread workThread; |
| 39 | + private LocationManager locationManager; |
| 40 | + private NianticManager nianticManager; |
| 41 | + private NotificationCompat.Builder builder; |
| 42 | + private PokemapSharedPreferences preffs; |
| 43 | + |
| 44 | + |
| 45 | + public PokemonNotificationService() { |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public IBinder onBind(Intent intent) { |
| 50 | + throw new UnsupportedOperationException("Not yet implemented"); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onCreate() { |
| 55 | + Log.d("PokeMap","Service.onCreate()"); |
| 56 | + EventBus.getDefault().register(this); |
| 57 | + createNotification(); |
| 58 | + |
| 59 | + preffs = new PokemapSharedPreferences(this); |
| 60 | + |
| 61 | + locationManager = LocationManager.getInstance(this); |
| 62 | + nianticManager = NianticManager.getInstance(); |
| 63 | + |
| 64 | + updateRunnable = new UpdateRunnable(preffs.getServiceRefreshRate()); |
| 65 | + workThread = new Thread(updateRunnable); |
| 66 | + |
| 67 | + initBroadcastReciever(); |
| 68 | + workThread.start(); |
| 69 | + locationManager.onResume(); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * This sets up the broadcast reciever. |
| 75 | + */ |
| 76 | + private void initBroadcastReciever() { |
| 77 | + IntentFilter intentFilter = new IntentFilter(); |
| 78 | + intentFilter.addAction(ACTION_STOP_SELF); |
| 79 | + registerReceiver(mBroadcastReciever,intentFilter); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onDestroy() { |
| 84 | + cancelNotification(); |
| 85 | + updateRunnable.stop(); |
| 86 | + EventBus.getDefault().unregister(this); |
| 87 | + unregisterReceiver(mBroadcastReciever); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public int onStartCommand(Intent intent, int flags, int startId) { |
| 92 | + return START_NOT_STICKY; |
| 93 | + } |
| 94 | + |
| 95 | + private void createNotification(){ |
| 96 | + builder = new NotificationCompat.Builder(getApplication()) |
| 97 | + .setSmallIcon(R.drawable.ic_gps_fixed_white_24px) |
| 98 | + .setContentTitle("Pokemon Service") |
| 99 | + .setContentText("Scanning").setOngoing(true); |
| 100 | + |
| 101 | + Intent i = new Intent(this, MainActivity.class); |
| 102 | + i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); |
| 103 | + |
| 104 | + PendingIntent pi = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_CANCEL_CURRENT); |
| 105 | + |
| 106 | + NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
| 107 | + |
| 108 | + //builder.setContentIntent(pi); |
| 109 | + |
| 110 | + Intent stopService = new Intent(); |
| 111 | + stopService.setAction(ACTION_STOP_SELF); |
| 112 | + |
| 113 | + PendingIntent piStopService = PendingIntent.getBroadcast(this,0,stopService,0); |
| 114 | + builder.addAction(R.drawable.ic_cancel_black_24px,"Stop Service",piStopService); |
| 115 | + |
| 116 | + nm.notify(notificationId,builder.build()); |
| 117 | + } |
| 118 | + |
| 119 | + private void cancelNotification(){ |
| 120 | + NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
| 121 | + nm.cancel(notificationId); |
| 122 | + } |
| 123 | + |
| 124 | + @Subscribe |
| 125 | + public void onEvent(CatchablePokemonEvent event) { |
| 126 | + List<CatchablePokemon> catchablePokemon = event.getCatchablePokemon(); |
| 127 | + |
| 128 | + LatLng location = locationManager.getLocation(); |
| 129 | + Location myLoc = new Location(""); |
| 130 | + myLoc.setLatitude(location.latitude); |
| 131 | + myLoc.setLongitude(location.longitude); |
| 132 | + builder.setContentText(catchablePokemon.size() + " pokemon nearby."); |
| 133 | + builder.setStyle(null); |
| 134 | + |
| 135 | + if(!catchablePokemon.isEmpty()){ |
| 136 | + NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); |
| 137 | + inboxStyle.setBigContentTitle(catchablePokemon.size() + " pokemon in area:"); |
| 138 | + for(CatchablePokemon cp : catchablePokemon){ |
| 139 | + Location pokeLocation = new Location(""); |
| 140 | + pokeLocation.setLatitude(cp.getLatitude()); |
| 141 | + pokeLocation.setLongitude(cp.getLongitude()); |
| 142 | + long remainingTime = cp.getExpirationTimestampMs() - System.currentTimeMillis(); |
| 143 | + inboxStyle.addLine(cp.getPokemonId().name() + "(" + |
| 144 | + TimeUnit.MILLISECONDS.toMinutes(remainingTime) + |
| 145 | + " minutes," + Math.ceil(pokeLocation.distanceTo(myLoc)) + " meters)"); |
| 146 | + } |
| 147 | + |
| 148 | + builder.setStyle(inboxStyle); |
| 149 | + } |
| 150 | + |
| 151 | + NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
| 152 | + nm.notify(notificationId,builder.build()); |
| 153 | + } |
| 154 | + |
| 155 | + private final class UpdateRunnable implements Runnable{ |
| 156 | + private long refreshRate = preffs.getServiceRefreshRate() * 1000; |
| 157 | + private boolean isRunning = true; |
| 158 | + |
| 159 | + public UpdateRunnable(int refreshRate){ |
| 160 | + this.refreshRate = refreshRate; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void run() { |
| 165 | + while(isRunning){ |
| 166 | + try{ |
| 167 | + LatLng currentLocation = locationManager.getLocation(); |
| 168 | + |
| 169 | + if(currentLocation != null){ |
| 170 | + nianticManager.getMapInformation(currentLocation.latitude,currentLocation.longitude,0); |
| 171 | + }else { |
| 172 | + locationManager = LocationManager.getInstance(PokemonNotificationService.this); |
| 173 | + } |
| 174 | + Thread.sleep(refreshRate); |
| 175 | + |
| 176 | + }catch(Exception e){ |
| 177 | + e.printStackTrace(); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + public void stop(){ |
| 183 | + isRunning = false; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private BroadcastReceiver mBroadcastReciever = new BroadcastReceiver() { |
| 188 | + @Override |
| 189 | + public void onReceive(Context context, Intent intent) { |
| 190 | + stopSelf(); |
| 191 | + locationManager.onPause(); |
| 192 | + } |
| 193 | + }; |
| 194 | +} |
0 commit comments