Guest User

Untitled

a guest
Aug 18th, 2019
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.travelmantics;
  2.  
  3. import android.app.Activity;
  4. import android.util.Log;
  5. import android.widget.Toast;
  6.  
  7. import androidx.annotation.NonNull;
  8. import androidx.annotation.Nullable;
  9.  
  10. import com.firebase.ui.auth.AuthUI;
  11. import com.google.firebase.auth.FirebaseAuth;
  12. import com.google.firebase.database.ChildEventListener;
  13. import com.google.firebase.database.DataSnapshot;
  14. import com.google.firebase.database.DatabaseError;
  15. import com.google.firebase.database.DatabaseReference;
  16. import com.google.firebase.database.FirebaseDatabase;
  17. import com.google.firebase.storage.FirebaseStorage;
  18. import com.google.firebase.storage.StorageReference;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.List;
  23.  
  24. //CLASS TO DEAL WITH FIREBASE DIRECTLY
  25. public class FireBaseUtil {
  26. public static FirebaseDatabase mFirebaseDatabase;
  27. public static DatabaseReference mDatabaseReference;
  28. private static FireBaseUtil fireBaseUtil;
  29. public static FirebaseAuth mFirebaseAuth;
  30. public static FirebaseStorage mStorage;
  31. public static StorageReference mStorageRef;
  32. public static FirebaseAuth.AuthStateListener mAuthListener;
  33. public static ArrayList<TravelDeal> mDeals;
  34. private static ListActivity caller;
  35. private static int RC_SIGN_IN = 100;
  36. public static boolean isAdmin;
  37.  
  38.  
  39. //constructor to avoid being instantiated from outside this class
  40.  
  41. private FireBaseUtil(){}
  42.  
  43.  
  44. public static void attachListener(){
  45. mFirebaseAuth.addAuthStateListener(mAuthListener);
  46. }
  47.  
  48. public static void detachListener(){
  49. mFirebaseAuth.removeAuthStateListener(mAuthListener);
  50. }
  51.  
  52.  
  53. public static void openFbReference(String ref, final ListActivity callerActivity){
  54. if(fireBaseUtil==null) {
  55. fireBaseUtil = new FireBaseUtil();
  56. mFirebaseDatabase = FirebaseDatabase.getInstance();
  57. mFirebaseAuth=FirebaseAuth.getInstance();
  58. caller=callerActivity;
  59. mAuthListener=new FirebaseAuth.AuthStateListener() {
  60. @Override
  61. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  62. if (firebaseAuth.getCurrentUser()==null){
  63.  
  64.  
  65. FireBaseUtil.signIn();}
  66.  
  67. else {
  68. String userid=firebaseAuth.getUid();
  69. checkAdmin(userid);
  70. }
  71.  
  72. }
  73. };
  74. connectStorage();
  75.  
  76. }
  77.  
  78. mDeals = new ArrayList<TravelDeal>();
  79. mDatabaseReference=mFirebaseDatabase.getReference().child(ref);
  80. }
  81.  
  82.  
  83.  
  84.  
  85. private static void checkAdmin(String uid){
  86. FireBaseUtil.isAdmin=false;
  87. DatabaseReference ref=mFirebaseDatabase.getReference().child("administrators").child(uid);
  88.  
  89. ChildEventListener listener=new ChildEventListener() {
  90. @Override
  91. public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
  92. FireBaseUtil.isAdmin=true;
  93. caller.showMenu();
  94. }
  95.  
  96. @Override
  97. public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
  98.  
  99. }
  100.  
  101. @Override
  102. public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
  103.  
  104. }
  105.  
  106. @Override
  107. public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
  108.  
  109. }
  110.  
  111. @Override
  112. public void onCancelled(@NonNull DatabaseError databaseError) {
  113.  
  114. }
  115. };
  116. ref.addChildEventListener(listener);
  117. }
  118.  
  119.  
  120. private static void signIn(){
  121.  
  122. // Choose authentication providers
  123. List<AuthUI.IdpConfig> providers = Arrays.asList(
  124. new AuthUI.IdpConfig.EmailBuilder().build(),
  125. new AuthUI.IdpConfig.GoogleBuilder().build());
  126.  
  127.  
  128.  
  129. // Create and launch sign-in intent
  130. caller.startActivityForResult(
  131. AuthUI.getInstance()
  132. .createSignInIntentBuilder()
  133. .setAvailableProviders(providers).setLogo(R.drawable.ic_tour_logo_text).setTheme(R.style.LoginTheme)
  134. .build(),
  135. RC_SIGN_IN);
  136. }
  137.  
  138. public static void connectStorage(){
  139. mStorage=FirebaseStorage.getInstance();
  140. mStorageRef=mStorage.getReference().child("deals_pictures");
  141. }
  142.  
  143.  
  144. }
RAW Paste Data