Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.55 KB | None | 0 0
  1. package host.exp.exponent.safetywidget;
  2.  
  3. import android.app.KeyguardManager;
  4. import android.app.Notification;
  5. import android.app.Notification.Builder;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.app.Service;
  9. import android.content.BroadcastReceiver;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.content.IntentFilter;
  13. import android.graphics.BitmapFactory;
  14. import android.os.IBinder;
  15. import android.support.annotation.Nullable;
  16. import android.support.v4.app.NotificationCompat;
  17. import android.util.Log;
  18.  
  19. import com.android.volley.RequestQueue;
  20. import com.android.volley.Response;
  21. import com.android.volley.VolleyError;
  22. import com.android.volley.VolleyLog;
  23. import com.android.volley.toolbox.JsonObjectRequest;
  24. import com.android.volley.toolbox.Volley;
  25. import com.facebook.react.bridge.Promise;
  26.  
  27. import org.json.JSONObject;
  28.  
  29. import java.util.HashMap;
  30. import java.util.Timer;
  31. import java.util.TimerTask;
  32.  
  33. import host.exp.exponent.MainActivity;
  34. import host.exp.exponent.R;
  35. import host.exp.exponent.messaging.CustomRNFirebaseMessagingService;
  36.  
  37. public class SafetyWidgetService extends Service {
  38. private static int SAFETY_WIDGET_LIVFREE_ID = 71534111;
  39. public static final String FOREGROUND = "com.livfree.livfree.safetywidget.FOREGROUND";
  40. final static String INTERACTION_TRACKING_INFO = "interaction_tracking_info";
  41. String publicId = null;
  42. String message = null;
  43. int timeInterval = 0;
  44. int unActivePeriod = 0;
  45. boolean stop = false;
  46. boolean isSafety;
  47. int aVariable;
  48. @Nullable
  49. @Override
  50. public IBinder onBind(Intent intent) {
  51. return null;
  52. }
  53. private static final String NOTIFICATION_DELETED_ACTION = "NOTIFICATION_DELETED";
  54.  
  55. private final BroadcastReceiver receiver = new BroadcastReceiver() {
  56. @Override
  57. public void onReceive(Context context, Intent intent) {
  58. Log.d("JAAAO ", "VISE");
  59. aVariable = 0; // Do what you want here
  60. unregisterReceiver(this);
  61. if (publicId != null) {
  62. letsFetch(publicId);
  63. unActivePeriod = 0;
  64. }
  65. }
  66. };
  67.  
  68. @Override
  69. public int onStartCommand(Intent intent, int flags, int startId) {
  70. Timer timer = null;
  71. TimerTask task = null;
  72. if (intent.getExtras().getBoolean("isSafety")) {
  73. publicId = intent.getExtras().getString("DeviceId");
  74. timeInterval = intent.getExtras().getInt("TimeInterval");
  75. message = intent.getExtras().getString("Message");
  76. stop = intent.getExtras().getBoolean("Stop");
  77. Log.d("EVBO ME IZNAD IF", String.valueOf(stop));
  78. if (timeInterval > 0 && !stop) {
  79. timer = new Timer();
  80. task = new TimerTask() {
  81. public void run()
  82. {
  83. getCompatNotification();
  84. }
  85. };
  86. timer.schedule( task, timeInterval, 1000);
  87. } else {
  88. Log.d("EVO ZAUSTAVLJAM ", "sE NE");
  89. timer.cancel();
  90. task.cancel();
  91. }
  92. //getCompatNotification();
  93. //startForeground(SAFETY_WIDGET_LIVFREE_ID, getCompatNotification());
  94. return START_STICKY;
  95. } else {
  96. publicId = intent.getExtras().getString("DeviceId");
  97. Log.d("JA SAM STRING ", String.valueOf(intent.getExtras().getBoolean("snooze")));
  98. if (intent.getExtras().getBoolean("snooze")) {
  99. Log.d("SNOZ", "JE TU");
  100. snooze(publicId);
  101. stopForeground(true);
  102. stopTimer();
  103. } else {
  104. letsFetch(publicId);
  105. stopForeground(true);
  106. stopTimer();
  107. }
  108.  
  109. }
  110. return START_STICKY;
  111. }
  112.  
  113. public void stopTimer() {
  114. if (CustomRNFirebaseMessagingService.t != null) {
  115. CustomRNFirebaseMessagingService.t.cancel();
  116. }
  117. if (CustomRNFirebaseMessagingService.mp != null) {
  118. CustomRNFirebaseMessagingService.mp.stop();
  119. }
  120. if (CustomRNFirebaseMessagingService.v != null) {
  121. CustomRNFirebaseMessagingService.v.cancel();
  122. }
  123. }
  124.  
  125. private void letsFetch(String deviceId) {
  126. RequestQueue queue = Volley.newRequestQueue(MainActivity.instance);
  127. //CHANGE URI http://staging.livfree.io/
  128. final String URL = "https://nameless-bastion-95377.herokuapp.com/v1/devices/" + deviceId + "/check-timers";
  129. HashMap<String, String> params = new HashMap<String, String>();
  130. params.put("longitude", Double.toString(MainActivity.longitude));
  131. params.put("latitude", Double.toString(MainActivity.latitude));
  132. JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
  133. new Response.Listener<JSONObject>() {
  134. @Override
  135. public void onResponse(JSONObject response) {
  136. VolleyLog.v("Response:%n %s", response.toString());
  137. }
  138. }, new Response.ErrorListener() {
  139. @Override
  140. public void onErrorResponse(VolleyError error) {
  141. VolleyLog.e("Error: ", error.getMessage());
  142. }
  143. });
  144. queue.add(req);
  145. }
  146.  
  147. private void snooze(String deviceId) {
  148. RequestQueue queue = Volley.newRequestQueue(MainActivity.instance);
  149. //CHANGE URI http://staging.livfree.io/
  150. final String URL = "https://nameless-bastion-95377.herokuapp.com/v1/devices/" + deviceId + "/snooze";
  151. HashMap<String, String> params = new HashMap<String, String>();
  152. params.put("longitude", Double.toString(MainActivity.longitude));
  153. params.put("latitude", Double.toString(MainActivity.latitude));
  154. JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
  155. new Response.Listener<JSONObject>() {
  156. @Override
  157. public void onResponse(JSONObject response) {
  158. VolleyLog.v("Response:%n %s", response.toString());
  159. }
  160. }, new Response.ErrorListener() {
  161. @Override
  162. public void onErrorResponse(VolleyError error) {
  163. VolleyLog.e("Error: ", error.getMessage());
  164. }
  165. });
  166. queue.add(req);
  167. }
  168.  
  169. private void getCompatNotification() {
  170. KeyguardManager myKM = (KeyguardManager) this.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
  171. Log.d("JA SAM ", String.valueOf(myKM.inKeyguardRestrictedInputMode()));
  172. Log.d("JA SAM PERIOD", String.valueOf(unActivePeriod));
  173. if (myKM.inKeyguardRestrictedInputMode()) {
  174. unActivePeriod = unActivePeriod + 1;
  175. } else {
  176. unActivePeriod = 0;
  177. }
  178. if (unActivePeriod == 300) {
  179. Intent intent = new Intent(NOTIFICATION_DELETED_ACTION);
  180. PendingIntent pendintIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
  181. registerReceiver(receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
  182. NotificationCompat.Builder b = new NotificationCompat.Builder(this.getApplicationContext(), "mymessage");
  183. b.setAutoCancel(true)
  184. .setDefaults(NotificationCompat.DEFAULT_ALL)
  185. .setWhen(System.currentTimeMillis())
  186. .setSmallIcon(R.drawable.notification_icon)
  187. .setContentTitle("Safety Widget")
  188. .setContentText(message)
  189. .setDeleteIntent(pendintIntent)
  190. .setContentInfo("INFO");
  191.  
  192. NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
  193. nm.notify(331, b.build());
  194. }
  195.  
  196. // Notification builder = new Notification.Builder(this)
  197. // .setAutoCancel(true)
  198. // .setContentTitle("Safety Widget")
  199. // .setContentText(message)
  200. // .setStyle(new Notification.BigTextStyle()
  201. // .bigText(message))
  202. // .setSmallIcon(R.drawable.notification_icon)
  203. // .build();
  204. // Intent startIntent = new Intent(getApplicationContext(), MainActivity.class);
  205. // PendingIntent contentIntent = PendingIntent.getActivity(this, 1000, startIntent, 0);
  206. // return builder;
  207. }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement