Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. package com.example.filip.unibook;
  2.  
  3.  
  4. import android.app.Service;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.media.Ringtone;
  8. import android.media.RingtoneManager;
  9. import android.net.Uri;
  10. import android.os.IBinder;
  11. import android.support.annotation.NonNull;
  12. import android.support.annotation.Nullable;
  13. import android.util.Log;
  14.  
  15. import com.google.android.gms.tasks.OnCompleteListener;
  16. import com.google.android.gms.tasks.Task;
  17. import com.google.firebase.auth.FirebaseAuth;
  18. import com.google.firebase.auth.FirebaseUser;
  19. import com.google.firebase.firestore.CollectionReference;
  20. import com.google.firebase.firestore.DocumentReference;
  21. import com.google.firebase.firestore.DocumentSnapshot;
  22. import com.google.firebase.firestore.EventListener;
  23. import com.google.firebase.firestore.FirebaseFirestore;
  24. import com.google.firebase.firestore.FirebaseFirestoreException;
  25. import com.google.firebase.firestore.Query;
  26. import com.google.firebase.firestore.QuerySnapshot;
  27.  
  28. import java.lang.ref.Reference;
  29. import java.sql.Ref;
  30. import java.util.List;
  31. import java.util.Timer;
  32. import java.util.TimerTask;
  33.  
  34. /**
  35. * Created by Ludvig on 2018-03-20.
  36. */
  37.  
  38. public class MyService extends Service {
  39. private FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
  40. String chatId;
  41. FirebaseAuth mAuth = FirebaseAuth.getInstance();
  42. FirebaseUser user = mAuth.getCurrentUser();
  43. String[] chats;
  44.  
  45. private Timer mTimer;
  46. TimerTask timerTask = new TimerTask() {
  47. @Override
  48. public void run() {
  49. Log.e("Log", "Running");
  50. //checkForNotis();
  51. checkForNewMessage();
  52. }
  53. };
  54.  
  55. @Override
  56. public void onCreate(){
  57. super.onCreate();
  58. mTimer = new Timer();
  59. mTimer.schedule(timerTask, 2000, 2 * 8000);
  60. }
  61.  
  62. @Override
  63. public int onStartCommand(Intent intent, int flags, int startId) {
  64. // START YOUR TASKS
  65. return super.onStartCommand(intent, flags, startId);
  66. }
  67.  
  68. @Override
  69. public void onDestroy() {
  70. // STOP YOUR TASKS
  71. super.onDestroy();
  72. try{
  73. mTimer.cancel();
  74. timerTask.cancel();
  75. } catch(Exception e){
  76. e.printStackTrace();
  77. }
  78. }
  79.  
  80. public void checkForNotis() {
  81. DatabaseHelper db = new DatabaseHelper(this);
  82. SharedPreferences sp = new SharedPreferences(this);
  83. User user = db.getUser(sp.getusername());
  84. List<String> notis = db.getNotis(user.getId());
  85. List<Ad> ads = db.getAllAdsNotis("");
  86.  
  87. try {
  88. //Loopa igenom alla ads.
  89. for (int i = 0; i < ads.size(); i++) {
  90. //Loopa igenom alla notiser på en ad.
  91. for (int i2 = 0; i2 < notis.size(); i2++) {
  92. Ad ad;
  93. if (ads.size() == 1) {
  94. ad = ads.get(i);
  95. } else {
  96. //Gör bara kollen på de senast tillagda böckerna
  97. ad = ads.get(db.getNotisCounter(notis.get(i2)));
  98. }
  99. //Kolla om det lagts till nya böcker sen senaste notisen visades, om inte visa ingen ny notis.
  100. if (ads.size() > db.getNotisCounter(ad.getProgram())) {
  101. String program = ad.getProgram();
  102. String course = ad.getCourse();
  103. //Om notisen matchar ett program på en ny bok, visa notis.
  104. if (notis.get(i2).trim().equals(program) || notis.get(i2).trim().equals(course)) {
  105. Notification notification = new Notification(this, program + " i kursen " + course, "Tryck för att öppna UniBook");
  106. notification.notificationManagerCompat.notify(2, notification.mBuilder.build());
  107.  
  108. //Ändra notiscounter på rätt notis.
  109. if(notis.get(i2).equals(program)) {
  110. db.setNotisCounter(ad.getProgram(), ads.size());
  111. break;
  112. }
  113. else if(notis.get(i2).equals(course)){
  114. db.setNotisCounter(ad.getCourse(), ads.size());
  115. break;
  116. }
  117. }
  118. //Om boken inte matchar notisen gå vidare till nästa bok.
  119. else {
  120. int counter = db.getNotisCounter(notis.get(i)) + 1;
  121. db.setNotisCounter(notis.get(i2), counter);
  122. }
  123. }
  124. break;
  125. }
  126. }
  127. } catch (Exception e) {
  128. Log.d("Broken", "Index out of bounds");
  129. }
  130. }
  131.  
  132. public void checkForNewMessage() {
  133. //Uppdatera meddelandelistan när ett nytt meddelande lagts till i databasen.
  134. final CollectionReference colRef = rootRef.collection("Chat");
  135. colRef.get()
  136. .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
  137. @Override
  138. public void onComplete(@NonNull Task<QuerySnapshot> task) {
  139. if (task.isSuccessful()) {
  140. List<DocumentSnapshot> list = task.getResult().getDocuments();
  141.  
  142. if (list.size() > 0) {
  143.  
  144. chats = new String[task.getResult().size()];
  145.  
  146. for (int i = 0; i < list.size(); i++) {
  147. DocumentSnapshot documentSnapshot = list.get(i);
  148. if (documentSnapshot.getString("User1").equals(user.getUid().toString())) {
  149. chats[i] = documentSnapshot.getId().toString();
  150. } else if (documentSnapshot.getString("User2").equals(user.getUid().toString())) {
  151. chats[i] = documentSnapshot.getId().toString();
  152. }
  153.  
  154. if (chats[i] != null) {
  155. checkLatest(chats[i]);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. });
  162. }
  163.  
  164. public void checkLatest(String chatsWithUser){
  165. final DocumentReference colRef = rootRef.collection("Chat").document(chatsWithUser).collection("Messages").document("latest");
  166. colRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
  167. @Override
  168. public void onComplete(@NonNull Task<DocumentSnapshot> task) {
  169. DocumentSnapshot documentSnapshot = task.getResult();
  170. if(documentSnapshot.getString("NotiSent") != null) {
  171. if (!documentSnapshot.getString("UserId").equals(user.getUid().toString()) && documentSnapshot.getString("NotiSent").equals("Not Sent")) {
  172. colRef.update("NotiSent", "Sent");
  173. try {
  174. Uri urinotification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  175. Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), urinotification);
  176. String name = documentSnapshot.getString("Name");
  177. String message = documentSnapshot.getString("Message");
  178. sendNoti(name, message);
  179. r.play();
  180. } catch (Exception ex) {
  181. ex.printStackTrace();
  182. }
  183. }
  184. }
  185. }
  186. });
  187. }
  188.  
  189. public void sendNoti(String name, String message){
  190. String notimeg = name + " har skickat ett meddelande: " + message;
  191. Notification notification = new Notification(this, notimeg, "Tryck för att öppna UniBook");
  192. notification.notificationManagerCompat.notify(2, notification.mBuilder.build());
  193. }
  194.  
  195. @Override
  196. public IBinder onBind(Intent intent) {
  197. return null;
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement