Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. public class DBService extends Service {
  2.  
  3. private static final String TAG = DBService.class.getName();
  4. private DatabaseReference reference;
  5.  
  6. private static final String FIREBASE_EMAIL = "xxxxxxx@workindia.in";
  7. private static final String FIREBASE_PASSWORD = "xxxxxx";
  8.  
  9. @Override
  10. public void onCreate() {
  11. super.onCreate();
  12.  
  13. FirebaseDatabase database = FirebaseDatabase.getInstance();
  14. reference = database.getReference();
  15. }
  16.  
  17.  
  18. @Override
  19. public int onStartCommand(Intent intent, int flags, int startId) {
  20.  
  21. FirebaseAuth auth = ((StartApplication) getApplication()).getAuth();
  22. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  23.  
  24. if (user == null) {
  25.  
  26. String email = ObjectGraph.getEmployeeProfile().getEmail();
  27. String password = ObjectGraph.getEmployeeProfile().getMobile_no();
  28. if (password != null && !password.trim().isEmpty()) {
  29. if (email == null || email.trim().isEmpty()) {
  30. email = password + FIREBASE_EMAIL;
  31. }
  32. signIn(auth, email, FIREBASE_PASSWORD);
  33. }
  34. } else {
  35. addListeners();
  36. }
  37. return START_STICKY;
  38.  
  39. }
  40.  
  41. @Override
  42. public IBinder onBind(Intent intent) {
  43. addListeners();
  44. return null;
  45. }
  46.  
  47. private void signIn(final FirebaseAuth auth, final String email, final String password) {
  48.  
  49. Log.i(TAG, "Login");
  50. auth.signInWithEmailAndPassword(email, password)
  51. .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  52. @Override
  53. public void onComplete(@NonNull Task<AuthResult> task) {
  54.  
  55. if (task.isSuccessful()) {
  56. boolean isResetTimeStamp = true;
  57. setTimeStamp(isResetTimeStamp);
  58. addListeners();
  59. } else {
  60. register(auth, email, password);
  61. }
  62. }
  63. });
  64.  
  65. FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
  66. @Override
  67. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  68. FirebaseUser user = firebaseAuth.getCurrentUser();
  69. if (user != null) {
  70. Log.e(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
  71. } else {
  72. Log.e(TAG, "onAuthStateChanged:signed_out");
  73. }
  74. }
  75. };
  76.  
  77. auth.addAuthStateListener(authListener);
  78. }
  79.  
  80. private void addListeners() {
  81. EmployeeProfile profile = ObjectGraph.getEmployeeProfile();
  82. if (profile != null && profile.getMobile_no() != null && !profile.getMobile_no().trim().isEmpty()) {
  83. reference.child(AppConstants.WORKINDIA_USERS_LAST_TIME).child(profile.getMobile_no().trim()).addValueEventListener(new ValueEventListener() {
  84. @Override
  85. public void onDataChange(DataSnapshot dataSnapshot) {
  86. Map<String, Object> child = (Map<String, Object>) dataSnapshot.getValue();
  87. Log.e(TAG, "DATA " + child);
  88. if (child == null) {
  89.  
  90. /*Query Listener Other listener*/
  91. Query recentPostsQuery = reference.child(AppConstants.WORKINDIA_JOB_NODE).limitToFirst(1000);//.orderByChild("timestamp");
  92. recentPostsQuery.addValueEventListener(jobBulKDownloadListener);
  93.  
  94. } else {
  95.  
  96. long lastSyncTime = (Long) child.get(AppConstants.TIMESTAMP);
  97. Log.e(TAG, "DATA " + lastSyncTime);
  98.  
  99. /*Query Listener Other listener*/
  100. Query recentPostsQuery = reference.child(AppConstants.WORKINDIA_JOB_NODE)
  101. .orderByChild(AppConstants.TIMESTAMP)
  102. .startAt(lastSyncTime)
  103. .limitToFirst(1000);//.orderByChild("timestamp");
  104. recentPostsQuery.addValueEventListener(jobBulKDownloadListener);
  105. }
  106. }
  107.  
  108. @Override
  109. public void onCancelled(DatabaseError databaseError) {
  110. Log.e(TAG, databaseError.getMessage(), databaseError.toException());
  111. }
  112. });
  113. }
  114. }
  115.  
  116.  
  117.  
  118. private void register(final FirebaseAuth auth, final String email, final String password) {
  119. Log.e(TAG, "register");
  120. auth.createUserWithEmailAndPassword(email, password)
  121. .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  122.  
  123. @Override
  124. public void onComplete(@NonNull Task<AuthResult> task) {
  125. if (task.isSuccessful()) {
  126. Log.e(TAG, "register true");
  127. signIn(auth, email, password);
  128. } else {
  129. Log.e(TAG, "register fail");
  130. }
  131. }
  132.  
  133. });
  134. }
  135.  
  136.  
  137. ValueEventListener jobBulKDownloadListener = new ValueEventListener() {
  138. @Override
  139. public void onDataChange(final DataSnapshot dataSnapshot) {
  140.  
  141. Toast.makeText(getApplicationContext(), "jobBulKDownloadListener+ onDataChange", Toast.LENGTH_SHORT).show();
  142. new Thread(new Runnable() {
  143. @Override
  144. public void run() {
  145. try {
  146. if (dataSnapshot != null) {
  147. long time = System.currentTimeMillis();
  148. Log.d(TAG, "Start Process : " + (System.currentTimeMillis() - time) / 1000 + " Seconds");
  149. List<Job> jobs = new ArrayList<Job>();
  150. for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
  151.  
  152. WrapperJob job1 = snapshot.getValue(WrapperJob.class);
  153. Job job = job1.getData();
  154. jobs.add(job);
  155. }
  156.  
  157.  
  158. if (jobs.size() > 0) {
  159. parseJobs(jobs);
  160. }
  161. Log.d(TAG, "After Process : " + (System.currentTimeMillis() - time) / 1000 + " Seconds");
  162. }
  163. } catch (Exception e) {
  164. Log.e(TAG, e.getMessage(), e);
  165. Crashlytics.logException(e);
  166. }
  167. }
  168. }).start();
  169. }
  170.  
  171. @Override
  172. public void onCancelled(DatabaseError databaseError) {
  173. Log.e(TAG, databaseError.getMessage(), databaseError.toException());
  174. }
  175. };
  176.  
  177.  
  178. private void parseJobs(List<Job> jobs) {
  179.  
  180. /* Job Operations*/
  181. }
  182.  
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement