Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DBService extends Service {
- private static final String TAG = DBService.class.getName();
- private DatabaseReference reference;
- private static final String FIREBASE_EMAIL = "xxxxxxx@workindia.in";
- private static final String FIREBASE_PASSWORD = "xxxxxx";
- @Override
- public void onCreate() {
- super.onCreate();
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- reference = database.getReference();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- FirebaseAuth auth = ((StartApplication) getApplication()).getAuth();
- FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
- if (user == null) {
- String email = ObjectGraph.getEmployeeProfile().getEmail();
- String password = ObjectGraph.getEmployeeProfile().getMobile_no();
- if (password != null && !password.trim().isEmpty()) {
- if (email == null || email.trim().isEmpty()) {
- email = password + FIREBASE_EMAIL;
- }
- signIn(auth, email, FIREBASE_PASSWORD);
- }
- } else {
- addListeners();
- }
- return START_STICKY;
- }
- @Override
- public IBinder onBind(Intent intent) {
- addListeners();
- return null;
- }
- private void signIn(final FirebaseAuth auth, final String email, final String password) {
- Log.i(TAG, "Login");
- auth.signInWithEmailAndPassword(email, password)
- .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- boolean isResetTimeStamp = true;
- setTimeStamp(isResetTimeStamp);
- addListeners();
- } else {
- register(auth, email, password);
- }
- }
- });
- FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = firebaseAuth.getCurrentUser();
- if (user != null) {
- Log.e(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
- } else {
- Log.e(TAG, "onAuthStateChanged:signed_out");
- }
- }
- };
- auth.addAuthStateListener(authListener);
- }
- private void addListeners() {
- EmployeeProfile profile = ObjectGraph.getEmployeeProfile();
- if (profile != null && profile.getMobile_no() != null && !profile.getMobile_no().trim().isEmpty()) {
- reference.child(AppConstants.WORKINDIA_USERS_LAST_TIME).child(profile.getMobile_no().trim()).addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- Map<String, Object> child = (Map<String, Object>) dataSnapshot.getValue();
- Log.e(TAG, "DATA " + child);
- if (child == null) {
- /*Query Listener Other listener*/
- Query recentPostsQuery = reference.child(AppConstants.WORKINDIA_JOB_NODE).limitToFirst(1000);//.orderByChild("timestamp");
- recentPostsQuery.addValueEventListener(jobBulKDownloadListener);
- } else {
- long lastSyncTime = (Long) child.get(AppConstants.TIMESTAMP);
- Log.e(TAG, "DATA " + lastSyncTime);
- /*Query Listener Other listener*/
- Query recentPostsQuery = reference.child(AppConstants.WORKINDIA_JOB_NODE)
- .orderByChild(AppConstants.TIMESTAMP)
- .startAt(lastSyncTime)
- .limitToFirst(1000);//.orderByChild("timestamp");
- recentPostsQuery.addValueEventListener(jobBulKDownloadListener);
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- Log.e(TAG, databaseError.getMessage(), databaseError.toException());
- }
- });
- }
- }
- private void register(final FirebaseAuth auth, final String email, final String password) {
- Log.e(TAG, "register");
- auth.createUserWithEmailAndPassword(email, password)
- .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- Log.e(TAG, "register true");
- signIn(auth, email, password);
- } else {
- Log.e(TAG, "register fail");
- }
- }
- });
- }
- ValueEventListener jobBulKDownloadListener = new ValueEventListener() {
- @Override
- public void onDataChange(final DataSnapshot dataSnapshot) {
- Toast.makeText(getApplicationContext(), "jobBulKDownloadListener+ onDataChange", Toast.LENGTH_SHORT).show();
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- if (dataSnapshot != null) {
- long time = System.currentTimeMillis();
- Log.d(TAG, "Start Process : " + (System.currentTimeMillis() - time) / 1000 + " Seconds");
- List<Job> jobs = new ArrayList<Job>();
- for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
- WrapperJob job1 = snapshot.getValue(WrapperJob.class);
- Job job = job1.getData();
- jobs.add(job);
- }
- if (jobs.size() > 0) {
- parseJobs(jobs);
- }
- Log.d(TAG, "After Process : " + (System.currentTimeMillis() - time) / 1000 + " Seconds");
- }
- } catch (Exception e) {
- Log.e(TAG, e.getMessage(), e);
- Crashlytics.logException(e);
- }
- }
- }).start();
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- Log.e(TAG, databaseError.getMessage(), databaseError.toException());
- }
- };
- private void parseJobs(List<Job> jobs) {
- /* Job Operations*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement