Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package host.exp.exponent.safetywidget;
- import android.app.KeyguardManager;
- import android.app.Notification;
- import android.app.Notification.Builder;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.graphics.BitmapFactory;
- import android.os.IBinder;
- import android.support.annotation.Nullable;
- import android.support.v4.app.NotificationCompat;
- import android.util.Log;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.VolleyLog;
- import com.android.volley.toolbox.JsonObjectRequest;
- import com.android.volley.toolbox.Volley;
- import com.facebook.react.bridge.Promise;
- import org.json.JSONObject;
- import java.util.HashMap;
- import java.util.Timer;
- import java.util.TimerTask;
- import host.exp.exponent.MainActivity;
- import host.exp.exponent.R;
- import host.exp.exponent.messaging.CustomRNFirebaseMessagingService;
- public class SafetyWidgetService extends Service {
- private static int SAFETY_WIDGET_LIVFREE_ID = 71534111;
- public static final String FOREGROUND = "com.livfree.livfree.safetywidget.FOREGROUND";
- final static String INTERACTION_TRACKING_INFO = "interaction_tracking_info";
- String publicId = null;
- String message = null;
- int timeInterval = 0;
- int unActivePeriod = 0;
- boolean stop = false;
- boolean isSafety;
- int aVariable;
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- private static final String NOTIFICATION_DELETED_ACTION = "NOTIFICATION_DELETED";
- private final BroadcastReceiver receiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- Log.d("JAAAO ", "VISE");
- aVariable = 0; // Do what you want here
- unregisterReceiver(this);
- if (publicId != null) {
- letsFetch(publicId);
- unActivePeriod = 0;
- }
- }
- };
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Timer timer = null;
- TimerTask task = null;
- if (intent.getExtras().getBoolean("isSafety")) {
- publicId = intent.getExtras().getString("DeviceId");
- timeInterval = intent.getExtras().getInt("TimeInterval");
- message = intent.getExtras().getString("Message");
- stop = intent.getExtras().getBoolean("Stop");
- Log.d("EVBO ME IZNAD IF", String.valueOf(stop));
- if (timeInterval > 0 && !stop) {
- timer = new Timer();
- task = new TimerTask() {
- public void run()
- {
- getCompatNotification();
- }
- };
- timer.schedule( task, timeInterval, 1000);
- } else {
- Log.d("EVO ZAUSTAVLJAM ", "sE NE");
- timer.cancel();
- task.cancel();
- }
- //getCompatNotification();
- //startForeground(SAFETY_WIDGET_LIVFREE_ID, getCompatNotification());
- return START_STICKY;
- } else {
- publicId = intent.getExtras().getString("DeviceId");
- Log.d("JA SAM STRING ", String.valueOf(intent.getExtras().getBoolean("snooze")));
- if (intent.getExtras().getBoolean("snooze")) {
- Log.d("SNOZ", "JE TU");
- snooze(publicId);
- stopForeground(true);
- stopTimer();
- } else {
- letsFetch(publicId);
- stopForeground(true);
- stopTimer();
- }
- }
- return START_STICKY;
- }
- public void stopTimer() {
- if (CustomRNFirebaseMessagingService.t != null) {
- CustomRNFirebaseMessagingService.t.cancel();
- }
- if (CustomRNFirebaseMessagingService.mp != null) {
- CustomRNFirebaseMessagingService.mp.stop();
- }
- if (CustomRNFirebaseMessagingService.v != null) {
- CustomRNFirebaseMessagingService.v.cancel();
- }
- }
- private void letsFetch(String deviceId) {
- RequestQueue queue = Volley.newRequestQueue(MainActivity.instance);
- //CHANGE URI http://staging.livfree.io/
- final String URL = "https://nameless-bastion-95377.herokuapp.com/v1/devices/" + deviceId + "/check-timers";
- HashMap<String, String> params = new HashMap<String, String>();
- params.put("longitude", Double.toString(MainActivity.longitude));
- params.put("latitude", Double.toString(MainActivity.latitude));
- JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- VolleyLog.v("Response:%n %s", response.toString());
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.e("Error: ", error.getMessage());
- }
- });
- queue.add(req);
- }
- private void snooze(String deviceId) {
- RequestQueue queue = Volley.newRequestQueue(MainActivity.instance);
- //CHANGE URI http://staging.livfree.io/
- final String URL = "https://nameless-bastion-95377.herokuapp.com/v1/devices/" + deviceId + "/snooze";
- HashMap<String, String> params = new HashMap<String, String>();
- params.put("longitude", Double.toString(MainActivity.longitude));
- params.put("latitude", Double.toString(MainActivity.latitude));
- JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
- new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- VolleyLog.v("Response:%n %s", response.toString());
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- VolleyLog.e("Error: ", error.getMessage());
- }
- });
- queue.add(req);
- }
- private void getCompatNotification() {
- KeyguardManager myKM = (KeyguardManager) this.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
- Log.d("JA SAM ", String.valueOf(myKM.inKeyguardRestrictedInputMode()));
- Log.d("JA SAM PERIOD", String.valueOf(unActivePeriod));
- if (myKM.inKeyguardRestrictedInputMode()) {
- unActivePeriod = unActivePeriod + 1;
- } else {
- unActivePeriod = 0;
- }
- if (unActivePeriod == 300) {
- Intent intent = new Intent(NOTIFICATION_DELETED_ACTION);
- PendingIntent pendintIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
- registerReceiver(receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
- NotificationCompat.Builder b = new NotificationCompat.Builder(this.getApplicationContext(), "mymessage");
- b.setAutoCancel(true)
- .setDefaults(NotificationCompat.DEFAULT_ALL)
- .setWhen(System.currentTimeMillis())
- .setSmallIcon(R.drawable.notification_icon)
- .setContentTitle("Safety Widget")
- .setContentText(message)
- .setDeleteIntent(pendintIntent)
- .setContentInfo("INFO");
- NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
- nm.notify(331, b.build());
- }
- // Notification builder = new Notification.Builder(this)
- // .setAutoCancel(true)
- // .setContentTitle("Safety Widget")
- // .setContentText(message)
- // .setStyle(new Notification.BigTextStyle()
- // .bigText(message))
- // .setSmallIcon(R.drawable.notification_icon)
- // .build();
- // Intent startIntent = new Intent(getApplicationContext(), MainActivity.class);
- // PendingIntent contentIntent = PendingIntent.getActivity(this, 1000, startIntent, 0);
- // return builder;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement