Advertisement
Guest User

android master

a guest
Feb 12th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.26 KB | None | 0 0
  1. package justin_martin.portadiil;
  2.  
  3. import android.support.design.widget.NavigationView;
  4. import android.support.design.widget.Snackbar;
  5. import android.support.v4.content.ContextCompat;
  6. import android.support.v4.view.GravityCompat;
  7. import android.support.v4.widget.DrawerLayout;
  8. import android.support.v7.app.ActionBarDrawerToggle;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.support.v7.widget.Toolbar;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.animation.AccelerateDecelerateInterpolator;
  16. import android.widget.TextView;
  17.  
  18. import com.eftimoff.androipathview.PathView;
  19. import com.google.firebase.auth.FirebaseAuth;
  20. import com.google.firebase.auth.FirebaseUser;
  21. import com.google.firebase.database.DataSnapshot;
  22. import com.google.firebase.database.DatabaseError;
  23. import com.google.firebase.database.DatabaseReference;
  24. import com.google.firebase.database.FirebaseDatabase;
  25. import com.google.firebase.database.ValueEventListener;
  26. import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
  27. import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings;
  28. import com.roger.catloadinglibrary.CatLoadingView;
  29.  
  30. import net.frederico.showtipsview.ShowTipsBuilder;
  31. import net.frederico.showtipsview.ShowTipsView;
  32. import net.frederico.showtipsview.ShowTipsViewInterface;
  33.  
  34. import java.text.DateFormat;
  35. import java.text.SimpleDateFormat;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import java.util.TimeZone;
  39.  
  40. import justin_martin.portadiil.Activity.About.AboutActivity;
  41. import justin_martin.portadiil.Activity.Admin.AdminActivity;
  42. import justin_martin.portadiil.Activity.Agenda.AgendaActivity;
  43. import justin_martin.portadiil.Activity.Event.AddEventActivity;
  44. import justin_martin.portadiil.Activity.Event.ViewEventActivity;
  45. import justin_martin.portadiil.Activity.Fidelity.FidelityCardActivity;
  46. import justin_martin.portadiil.Activity.Fidelity.FidelityCardAddPointsActivity;
  47. import justin_martin.portadiil.Activity.HomePageActivity;
  48. import justin_martin.portadiil.Activity.Idea.ViewIdeaActivity;
  49. import justin_martin.portadiil.Activity.Settings.SettingsActivity;
  50. import justin_martin.portadiil.Model.AppTheme;
  51. import justin_martin.portadiil.Model.ConfigAccess;
  52. import justin_martin.portadiil.Model.User.User;
  53.  
  54. /**
  55.  * Created by justin on 18/10/2017.
  56.  */
  57.  
  58.     public class MasterActivity extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener, Constant {
  59.  
  60.         /*== Loading Dialog ==*/
  61.         private CatLoadingView loadingView;
  62.         private boolean loadingDisplay = false;
  63.  
  64.         /*=== Firebase ===*/
  65.         public final static FirebaseAuth mAuth = FirebaseAuth.getInstance(); //instance de l'application
  66.         public FirebaseUser user;
  67.  
  68.         /*===REFERENCES===*/
  69.         public static DatabaseReference mBase;
  70.         public static DatabaseReference mUserReference;
  71.         public static DatabaseReference mIdeaReference;
  72.         public static DatabaseReference mEventReference;
  73.         public static DatabaseReference mConfigAccessReference;
  74.         public static DatabaseReference mLibsReference;
  75.         public static DatabaseReference mInfosReference;
  76.         private static DatabaseReference mAgendaReference;
  77.         private static DatabaseReference mDevsReference;
  78.  
  79.  
  80.         public static User userConnected;
  81.         public static ConfigAccess configAccess;
  82.  
  83.         /*=== AGENDA ==*/
  84.         public boolean noActionBar = false;
  85.         public AppTheme appTheme;
  86.         public FirebaseRemoteConfig mFirebaseRemoteConfig;
  87.         /*============*/
  88.  
  89.         private static boolean isInitialized = false;
  90.         private boolean asNav = false;
  91.         private MasterActivity thisActivity;
  92.         public static DateFormat dfm = new SimpleDateFormat("dd-MM-yyyy HH:mm");
  93.         public List<User> usersList = new ArrayList<>();
  94.  
  95.         @Override
  96.         protected void onCreate(@Nullable Bundle savedInstanceState) {
  97.             dfm.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
  98.  
  99.             /*========== Agenda Filter =============*/
  100.             if(this instanceof  AgendaActivity || this instanceof AddEventActivity){
  101.                 appTheme = new AppTheme(getApplicationContext(), noActionBar);
  102.                 setTheme(appTheme.getStyle());
  103.             }
  104.             /*======================================*/
  105.  
  106.             super.onCreate(savedInstanceState);
  107.             thisActivity = this;
  108.  
  109.             /*============ Agenda =============*/
  110.             if(!isInitialized){
  111.                 FirebaseDatabase.getInstance().setPersistenceEnabled(true);
  112.                 isInitialized=true;
  113.             }
  114.             mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
  115.         final FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
  116.                 .setDeveloperModeEnabled(false)
  117.                 .build();
  118.         mFirebaseRemoteConfig.setConfigSettings(configSettings);
  119.         /*===================================*/
  120.  
  121.         mBase = FirebaseDatabase.getInstance().getReference();
  122.         mUserReference = mBase.child("users");
  123.         mEventReference = mBase.child("events");
  124.         mIdeaReference = mBase.child("ideas");
  125.         mDevsReference = mBase.child("ConfigApp").child("AboutStorage").child("listDeveloppers");
  126.         mInfosReference = mBase.child("ConfigApp").child("AboutStorage").child("listInfo");
  127.         mLibsReference = mBase.child("ConfigApp").child("AboutStorage").child("listLibs");
  128.         mConfigAccessReference = mBase.child("ConfigApp").child("ConfigAccess");
  129.         mAgendaReference = mBase.child("agenda").child("departement");
  130.         //RAJOUTER LA DATE
  131.  
  132.         if (isConnected()) {
  133.             final ValueEventListener userConnectedListener = new ValueEventListener() {
  134.                 @Override
  135.                 public void onDataChange(DataSnapshot dataSnapshot) {
  136.                     userConnected = dataSnapshot.getValue(User.class);
  137.                     if(userConnected.isToDelete()) deleteAccount();
  138.                     if(thisActivity!=null && asNav) disalbeItemAuthLvl();
  139.                     changeNbPoint();
  140.                     changeUsersList();
  141.                     changeUsersListAddPoint();
  142.                     // ...
  143.                 }
  144.  
  145.                 @Override
  146.                 public void onCancelled(DatabaseError databaseError) {
  147.                     // Getting Post failed, log a message
  148.                     Log.w("loadPost:onCancelled", databaseError.toException());
  149.                     // ...
  150.                 }
  151.             };
  152.             mUserReference.child(user.getUid()).addValueEventListener(userConnectedListener);
  153.  
  154.             final ValueEventListener configAccessListener = new ValueEventListener() {
  155.                 @Override
  156.                 public void onDataChange(DataSnapshot dataSnapshot) {
  157.                     configAccess = dataSnapshot.getValue(ConfigAccess.class);
  158.                     dimissLoadingDialog();
  159.                 }
  160.  
  161.                 @Override
  162.                 public void onCancelled(DatabaseError databaseError) {
  163.  
  164.                 }
  165.             };
  166.             mConfigAccessReference.addValueEventListener(configAccessListener);
  167.         }
  168.  
  169.     }
  170.  
  171.     public boolean isConnected() {
  172.         user = mAuth.getCurrentUser();
  173.         if (user == null) return false;
  174.         else return true;
  175.     }
  176.     public void logout() {
  177.         mAuth.signOut();
  178.     }
  179.  
  180.     public void addNav(){
  181.         if(asNav){
  182.             Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  183.             setSupportActionBar(toolbar);
  184.  
  185.             DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  186.  
  187.             NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
  188.             navView.inflateMenu(R.menu.menu_admin_drawer);
  189.             navView.setNavigationItemSelectedListener(this);
  190.             ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  191.                     this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  192.             drawer.setDrawerListener(toggle);
  193.             toggle.syncState();
  194.  
  195.             TextView textView =  navView.getHeaderView(0).getRootView().findViewById(R.id.versionAppTextView);
  196.             textView.append(BuildConfig.VERSION_NAME);
  197.             if (this instanceof ViewEventActivity)
  198.                 navView.getHeaderView(0).setBackground(getDrawable(R.drawable.nav_bar_header_bg_event));
  199.             else if (this instanceof ViewIdeaActivity)
  200.                 navView.getHeaderView(0).setBackground(getDrawable(R.drawable.nav_bar_header_bg_idea));
  201.             else if (this instanceof FidelityCardActivity)
  202.                 navView.getHeaderView(0).setBackground(getDrawable(R.drawable.nav_bar_header_bg_fidelity));
  203.         }
  204.     }
  205.  
  206.     public static int safeLongToInt(long l) {
  207.         int i = (int)l;
  208.         if ((long)i != l) {
  209.             throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
  210.         }
  211.         return i;
  212.     }
  213.     public void setAsNav(boolean asNav){
  214.         this.asNav = asNav;
  215.     }
  216.  
  217.     private void changeNbPoint() {
  218.         if (this instanceof FidelityCardActivity) {
  219.             ((FidelityCardActivity) this).getNbTampons();
  220.         }
  221.     }
  222.     private void changeUsersList() {
  223.         if (this instanceof FidelityCardActivity) {
  224.             ((FidelityCardActivity) this).fillUsersList();
  225.         }
  226.     }
  227.     private void changeUsersListAddPoint() {
  228.         if (this instanceof FidelityCardAddPointsActivity) {
  229.             ((FidelityCardAddPointsActivity) this).fillUsersList();
  230.         }
  231.     }
  232.  
  233.     public ShowTipsView howToUse(View target, String title, String description, final ShowTipsView nextActive, final MasterActivity displayActivity){
  234.         int BUTTON_NEXT_BG = ContextCompat.getColor(getBaseContext(), R.color.colorAccent);
  235.         int BUTTON_NEXT_TEXTCOLOR = ContextCompat.getColor(getBaseContext(), R.color.colorWhite);
  236.  
  237.         ShowTipsView showtips = new ShowTipsBuilder(this)
  238.                 .setTarget(target)
  239.                 .setTitle(title)
  240.                 .setDescription(description)
  241.                 .setDelay(100)
  242.                 .setButtonText(BUTTON_NEXT_TEXT)
  243.                 .setCloseButtonColor(BUTTON_NEXT_BG)
  244.                 .setCloseButtonTextColor(BUTTON_NEXT_TEXTCOLOR)
  245.                 .setBackgroundAlpha(40)
  246.                 .setCircleColor(BUTTON_NEXT_BG)
  247.                 .build();
  248.  
  249.         showtips.setCallback(new ShowTipsViewInterface(){
  250.             @Override
  251.             public void gotItClicked() {
  252.                 nextActive.show(displayActivity);
  253.             }
  254.         });
  255.         return showtips;
  256.     }
  257.     public ShowTipsView howToUse(final View target, String title, String description){
  258.         int BUTTON_NEXT_BG = ContextCompat.getColor(getBaseContext(), R.color.colorAccent);
  259.         int BUTTON_NEXT_TEXTCOLOR = ContextCompat.getColor(getBaseContext(), R.color.colorWhite);
  260.  
  261.         ShowTipsView showtips = new ShowTipsBuilder(this)
  262.                 .setTarget(target)
  263.                 .setTitle(title)
  264.                 .setDescription(description)
  265.                 .setDelay(100)
  266.                 .setButtonText(BUTTON_NEXT_TEXT)
  267.                 .setCloseButtonColor(BUTTON_NEXT_BG)
  268.                 .setCloseButtonTextColor(BUTTON_NEXT_TEXTCOLOR)
  269.                 .setBackgroundAlpha(40)
  270.                 .setCircleColor(BUTTON_NEXT_BG)
  271.                 .build();
  272.  
  273.         showtips.setCallback(new ShowTipsViewInterface(){
  274.             @Override
  275.             public void gotItClicked() {
  276.                 SharedPreferences preferences = getSharedPreferences(PREFERENCES_FILE, MODE_PRIVATE);
  277.                 SharedPreferences.Editor editor = preferences.edit();
  278.  
  279.                 Log.e("tag", getResources().getResourceEntryName(target.getId()));
  280.                 editor.putBoolean(getResources().getResourceEntryName(target.getId()), true);
  281.                 editor.apply();
  282.             }
  283.         });
  284.         return showtips;
  285.     }
  286.  
  287.     public void maintenanceActivity() {
  288.         findViewById(R.id.classicView).setVisibility(View.INVISIBLE);
  289.         findViewById(R.id.maintenanceView).setVisibility(View.VISIBLE);
  290.         final PathView pathView = (PathView) findViewById(R.id.pathView);
  291.         pathView.getPathAnimator()
  292.                 .delay(100)
  293.                 .duration(2000)
  294.                 .interpolator(new AccelerateDecelerateInterpolator())
  295.                 .start();
  296.     }
  297.     private void deleteAccount() {
  298.         mUserReference.child(userConnected.getUid()).removeValue();
  299.         user.delete();
  300.     }
  301.  
  302.     public void showSnackBar(String snackbarText) {
  303.         Snackbar mySnackbar = Snackbar.make(findViewById(android.R.id.content),
  304.                 snackbarText, Snackbar.LENGTH_SHORT);
  305.         mySnackbar.show();
  306.     }
  307.     public void showLoadingDialog(){
  308.         if(!loadingDisplay) {
  309.             loadingView = new CatLoadingView();
  310.             loadingView.show(getSupportFragmentManager(), "");
  311.             loadingDisplay = true;
  312.         }
  313.     }
  314.     public void dimissLoadingDialog(){
  315.        if(loadingDisplay){
  316.            loadingView.getDialog().cancel();
  317.            loadingDisplay = false;
  318.        }
  319.     }
  320.  
  321.     public String generateToken(int length) {
  322.         String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; // Tu supprimes les lettres dont tu ne veux pas
  323.         String pass = "";
  324.         for (int x = 0; x < length; x++) {
  325.             int i = (int) Math.floor(Math.random() * 62); // Si tu supprimes des lettres tu diminues ce nb
  326.             pass += chars.charAt(i);
  327.         }
  328.         return pass;
  329.     }
  330.     protected void disalbeItemAuthLvl() {
  331.         final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  332.         Menu menuNav = navigationView.getMenu();
  333.  
  334.         final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  335.         final List<Integer> ressourceListToDisable = new ArrayList<>();
  336.         final List<MenuItem> menuItemListToDisable = new ArrayList<>();
  337.         String userRole = userConnected.getAuthLvl();
  338.  
  339.         switch (userRole) {
  340.             case "Non adhérent":
  341.                 menuItemListToDisable.add(menuNav.getItem(4));
  342.                 menuItemListToDisable.add(menuNav.getItem(5));
  343.                 ressourceListToDisable.add(R.id.nav_administration);
  344.                 ressourceListToDisable.add(R.id.nav_fidelity);
  345.                 break;
  346.             case "Adhérent":
  347.                 menuItemListToDisable.add(menuNav.getItem(5));
  348.                 ressourceListToDisable.add(R.id.nav_administration);
  349.                 break;
  350.         }
  351.  
  352.         drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
  353.             @Override
  354.             public void onDrawerSlide(View drawerView, float slideOffset)
  355.             {
  356.  
  357.             }
  358.  
  359.             @RequiresApi(api = Build.VERSION_CODES.M)
  360.             @Override
  361.             public void onDrawerOpened(View drawerView) {
  362.                 for (MenuItem item : menuItemListToDisable) {
  363.                     item.setEnabled(false);
  364.                 }
  365.             }
  366.  
  367.             @Override
  368.             public void onDrawerClosed(View drawerView) {
  369.  
  370.             }
  371.  
  372.             @Override
  373.             public void onDrawerStateChanged(int newState) {
  374.  
  375.  
  376.             }
  377.         });
  378.     }
  379.     @Override
  380.     public boolean onNavigationItemSelected(MenuItem item) {
  381.         // Handle navigation view item clicks here.
  382.         int id = item.getItemId();
  383.         Intent switchAcitity = new Intent(this, HomePageActivity.class);
  384.         Log.e("item", item.getItemId()+"");
  385.  
  386.         if (id == R.id.nav_home) {
  387.             switchAcitity = new Intent(this, HomePageActivity.class);
  388.         } else if (id == R.id.nav_administration) {
  389.             switchAcitity = new Intent(this, AdminActivity.class);
  390.         } else if (id == R.id.nav_agenda) {
  391.             switchAcitity = new Intent(this, AgendaActivity.class);
  392.         } else if (id == R.id.nav_event) {
  393.             switchAcitity = new Intent(this, ViewEventActivity.class);
  394.         } else if (id == R.id.nav_fidelity) {
  395.             switchAcitity = new Intent(this, FidelityCardActivity.class);
  396.         } else if (id == R.id.nav_idea) {
  397.             switchAcitity = new Intent(this, ViewIdeaActivity.class);
  398.         } else if (id == R.id.nav_logout) {
  399.             logout();
  400.         }else if (id == R.id.nav_param) {
  401.             switchAcitity = new Intent(this, SettingsActivity.class);
  402.         } else if (id == R.id.nav_about) {
  403.             switchAcitity = new Intent(this, AboutActivity.class);
  404.         }
  405.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  406.         startActivity(switchAcitity);
  407.         return true;
  408.     }
  409.     @Override
  410.     public void onBackPressed() {
  411.         if(asNav) {
  412.             DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  413.             if (drawer.isDrawerOpen(GravityCompat.START)) {
  414.                 drawer.closeDrawer(GravityCompat.START);
  415.             } else {
  416.                 if(this instanceof HomePageActivity) return;
  417.                 super.onBackPressed();
  418.             }
  419.         }
  420.         else {
  421.             if(this instanceof HomePageActivity) return;
  422.             super.onBackPressed();
  423.         }
  424.     }
  425.  
  426.     public static DatabaseReference getmBase() {
  427.         return mBase;
  428.     }
  429.     public static DatabaseReference getmUserReference() {
  430.     return mUserReference;
  431. }
  432.     public static DatabaseReference getmIdeaReference() {
  433.         return mIdeaReference;
  434.     }
  435.     public static DatabaseReference getmEventReference() {
  436.         return mEventReference;
  437.     }
  438.     public static User getUserConnected() {
  439.         return userConnected;
  440.     }
  441.     public static FirebaseAuth getmAuth() {
  442.         return mAuth;
  443.     }
  444.  
  445.     public static DatabaseReference getmLibsReference() {
  446.         return mLibsReference;
  447.     }
  448.     public static DatabaseReference getmInfosReference() {
  449.         return mInfosReference;
  450.     }
  451.     public static DatabaseReference getmAgendaReference() {return mAgendaReference;}
  452.     public static ConfigAccess getConfigAccess() {
  453.         return configAccess;
  454.     }
  455.     public static DatabaseReference getmDevsReference() {
  456.         return mDevsReference;
  457.     }
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement