SHARE
TWEET

Untitled

a guest Mar 18th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class LogoActivity extends AppCompatActivity {
  2.  
  3. private ImageView   mLogo;
  4. private Handler     mHandler;
  5.  
  6. @Override
  7. protected void onCreate(@Nullable Bundle savedInstanceState) {
  8.     super.onCreate(savedInstanceState);
  9.     setContentView(R.layout.activity_logo);
  10.     this.init();
  11. }
  12.  
  13. private void init(){
  14.    mLogo = findViewById(R.id.activity_logo_logo);
  15.     AuthHandler.performStartUpCheck(new AuthHandler.StartupAuthStatus() {
  16.         @Override
  17.         public void noUser() {
  18.             Toast.makeText(LogoActivity.this, "No User", Toast.LENGTH_SHORT).show();
  19.             performSharedElementTransactionToAuthOption();
  20.         }
  21.  
  22.         @Override
  23.         public void onError(String e) {
  24.             Toast.makeText(LogoActivity.this, e, Toast.LENGTH_SHORT).show();
  25.             performSharedElementTransactionToAuthOption();
  26.         }
  27.  
  28.         @Override
  29.         public void onPhone() {
  30.             Toast.makeText(LogoActivity.this, "Phone", Toast.LENGTH_SHORT).show();
  31.             //direct to loadingActivity
  32.         }
  33.  
  34.         @Override
  35.         public void onEmail(boolean isVerified) {
  36.             Toast.makeText(LogoActivity.this, "Email" + String.valueOf(isVerified), Toast.LENGTH_SHORT).show();
  37.             if(isVerified){
  38.                 //direct to LoadingActivity
  39.             }else{
  40.                 performSharedElementTransactionToAuthOption();
  41.             }
  42.         }
  43.     });
  44.    //this.performSharedElementTransaction();
  45. }
  46.  
  47. private void performSharedElementTransactionToAuthOption(){
  48.     //check for auth and proceed to Loading
  49.     Intent pIntent = new Intent(LogoActivity.this, AuthOptionActivity.class);
  50.     ActivityOptions mOptions = ActivityOptions.makeSceneTransitionAnimation(LogoActivity.this, mLogo, mLogo.getTransitionName());
  51.     startActivity(pIntent, mOptions.toBundle());
  52.  
  53. }
  54.  
  55. private void performTransactionToLoadingScreen(){
  56.     /**dummy**/
  57. }
  58.  
  59. @Override
  60. protected void onStop() {
  61.     //Call finish here to avoid flickering
  62.     this.finish();
  63.     super.onStop();
  64. }
  65.      
  66. public class AuthHandler {
  67.  
  68. public interface StartupAuthStatus{
  69.     void noUser();
  70.     void onError(String e);
  71.     void onPhone();
  72.     void onEmail(boolean isVerified);
  73. }
  74.  
  75. public interface SignInStatus{
  76.     void onEmail(boolean isVerified);
  77. }
  78.  
  79. private FirebaseAuth mAuth;
  80. private FirebaseFirestore mFirestore;
  81.  
  82. /**STARTUP PROCEDURE**/
  83.  
  84. public static void performStartUpCheck(final StartupAuthStatus mCallback){
  85.     final FirebaseAuth mAuth = FirebaseAuth.getInstance();
  86.     checkStartupAuthStatus(mAuth, mCallback);
  87.  
  88. }
  89.  
  90. private static void checkStartupAuthStatus(FirebaseAuth mAuth, StartupAuthStatus mCallback){
  91.     mAuth = FirebaseAuth.getInstance();
  92.     checkForCurrentUser(mAuth,mCallback);
  93. }
  94.  
  95. private static void checkForCurrentUser(FirebaseAuth mAuth, StartupAuthStatus mCallback){
  96.     if(mAuth.getCurrentUser()!=null){
  97.         checkRegistrationMethod(mAuth, mCallback);
  98.     }else{
  99.         mCallback.noUser();
  100.     }
  101. }
  102.  
  103. private static void checkRegistrationMethod(final FirebaseAuth mAuth, final StartupAuthStatus mCallback){
  104.     if(mAuth.getUid() == null){
  105.         mCallback.onError("NullPointerInUID");
  106.         return;
  107.     }
  108.     FirebaseFirestore.getInstance().collection("Registration").document(mAuth.getUid()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
  109.         @Override
  110.         public void onComplete(@NonNull Task<DocumentSnapshot> task) {
  111.             if(task.getResult() != null && task.getResult().exists()){
  112.                 Map<String, Object> mData = task.getResult().getData();
  113.                 String mRegistrationForm = (String) mData.get("reg");
  114.                 if(mRegistrationForm.equals("mail")){
  115.                     checkMailVerificationStatus(mAuth, mCallback);
  116.                 }else if(mRegistrationForm.equals("phone")){
  117.                     mCallback.onPhone();
  118.                 }
  119.             }
  120.         }
  121.     }).addOnFailureListener(new OnFailureListener() {
  122.         @Override
  123.         public void onFailure(@NonNull Exception e) {
  124.             mCallback.onError(e.getMessage());
  125.             mAuth.signOut();
  126.         }
  127.     });
  128. }
  129.  
  130. private static  void checkMailVerificationStatus(FirebaseAuth mAuth, StartupAuthStatus mCallback){
  131.     if(mAuth.getCurrentUser().isEmailVerified()){
  132.         mCallback.onEmail(true);
  133.     }else{
  134.         mCallback.onEmail(false);
  135.     }
  136. }
  137.  
  138. /**LOGIN PROCEDURE**/
  139.  
  140. public static void enterUserWithEmailAndPassword(String mEmail, String mPassword){
  141.     FirebaseAuth mAuth = FirebaseAuth.getInstance();
  142.     createUserWithEmailAndPassword(mAuth, mEmail, mPassword);
  143. }
  144.  
  145. private static void createUserWithEmailAndPassword(FirebaseAuth mAuth, String mEmail, String mPassword){
  146.  
  147. }
  148.  
  149. private static void signUserWithEmailAndPassword(FirebaseAuth mAuth, String mEmail, String mPassword){
  150.  
  151. }
  152.  
  153. private static void sendVerificationEmail(FirebaseAuth mAuth){
  154.  
  155. }
  156.  
  157. private static void setupRegistrationModel(String mModelType){
  158.  
  159. }
  160.  
  161. /**DELETE PROCEDURE**/
  162.      
  163. public class AuthOptionActivity extends AppCompatActivity {
  164.  
  165. @Override
  166. protected void onCreate(@Nullable Bundle savedInstanceState) {
  167.     super.onCreate(savedInstanceState);
  168.     setContentView(R.layout.activity_authoption);
  169.     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
  170.         getWindow().setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(android.R.transition.move));
  171.     }
  172.     this.checkAuthStatus();
  173. }
  174.  
  175. @Override
  176. public void onBackPressed() {
  177.     findViewById(R.id.activity_authoption_logo).setTransitionName(null);
  178.     super.onBackPressed();
  179.     /** Problem occured: SharedViewElement will be visible after App was closed.
  180.      * Reason: The View will try to perform the ExitTransition
  181.      * Solution 1: Delete super.onBackPressed(); and override with finish();
  182.      * Solution 2: Set Transitionname to Null**/
  183. }
  184.  
  185. /**The SlideEnter Transition is variable, on First Startup we want a smooth slide in,
  186.  * for phoneauth we want a Left-Side Slide and for Email a Right-Side Slide.
  187.  * The Exit Animation is always slide-out-bottom-smooth.
  188.  * The Reenter Animation is always slide-in-bottom-smooth and due to overlapping
  189.  * the ExitTransition of the View before BackPress will be hidden and is always 0**/
  190.  
  191. private void performFragmentTransaction(Fragment mFragment, String mTag, Boolean mAddToBackStack, int mEnterTransition){
  192.     FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
  193.     mFragmentTransaction.setCustomAnimations(mEnterTransition, 0, R.anim.slide_in_bottom_smooth, 0);
  194.     if(mAddToBackStack){
  195.         mFragmentTransaction.addToBackStack(mTag);
  196.     }
  197.     mFragmentTransaction.replace(R.id.activity_authoption_container, mFragment, mTag);
  198.     mFragmentTransaction.commit();
  199. }
  200.  
  201.  
  202. /**Check Auth Status: if CurrentUser != null but not Email verified -> Show MailAuth**/
  203. private void checkAuthStatus(){
  204.     if(FirebaseAuth.getInstance().getCurrentUser()!=null && !FirebaseAuth.getInstance().getCurrentUser().isEmailVerified()){
  205.         //very bad way to force a backstack, still looking for a solution
  206.         this.showAuthOptionFragment();
  207.         this.showMailAuth();
  208.     }else{
  209.         this.showAuthOptionFragment();
  210.     }
  211. }
  212.  
  213. /**Handle Fragment Transaction including Listener**/
  214. private void showAuthOptionFragment(){
  215.     this.performFragmentTransaction(AuthOptionFragment.newInstance(new AuthOptionFragment.ClickListener() {
  216.         @Override
  217.         public void onMail() {
  218.             showMailAuth();
  219.         }
  220.  
  221.         @Override
  222.         public void onPhone() {
  223.             showPhoneAuth();
  224.         }
  225.     }), "AuthOption", false, R.anim.slide_in_bottom);
  226. }
  227.  
  228. private void showPhoneAuth(){
  229.     this.performFragmentTransaction(_PhoneAuthFragment.newInstance(new _PhoneAuthFragment.AuthStatusListener() {
  230.         @Override
  231.         public void onCancel() {
  232.             //PhoneAuthFragment contains a "back" Button it will fire "onCancel()"
  233.             onBackPressed();
  234.         }
  235.  
  236.         @Override
  237.         public void onSuccess() {
  238.             //show LoadingScreen
  239.         }
  240.     }), "PhoneAuth", true, R.anim.slide_in_left_smooth);
  241. }
  242.  
  243. private void showMailAuth(){
  244.     this.performFragmentTransaction(_MailAuthFragment.newInstance(new _MailAuthFragment.AuthStatusListener() {
  245.         @Override
  246.         public void onCancel() {
  247.             //MailAuthFragment contains a "back" Button it will fire "onCancel()"
  248.             onBackPressed();
  249.         }
  250.  
  251.         @Override
  252.         public void onSuccess() {
  253.             //show LoadingScreen
  254.         }
  255.     }), "MailAuth", true, R.anim.slide_in_right_smooth);
  256. }
  257.      
  258. public class _MailAuthFragment extends Fragment {
  259.  
  260. public interface AuthStatusListener{
  261.     void onCancel();
  262.     void onSuccess();
  263. }
  264.  
  265. private AuthStatusListener mCallback;
  266. private String mActiveWindow;
  267. private CheckCancelButton mCheckCancelButton;
  268.  
  269. private String mEmail;
  270. private String mPassword;
  271.  
  272. @Override
  273. public void onCreate(@Nullable Bundle savedInstanceState) {
  274.     super.onCreate(savedInstanceState);
  275. }
  276.  
  277. @Nullable
  278. @Override
  279. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  280.     View mView = inflater.inflate(R.layout.fragment_mailauth, container, false);
  281.     this.checkAuthStatus();
  282.     this.initButton(mView);
  283.     return mView;
  284. }
  285.  
  286. /**PUBLIC METHODS**/
  287.  
  288. /**Set the Fragment Listener for AuthOptionActivity**/
  289. public void setListener(AuthStatusListener mCallback){
  290.     this.mCallback = mCallback;
  291. }
  292.  
  293. /**Replaces hard coded Instances as Listener will be passed directly**/
  294. public static _MailAuthFragment newInstance(AuthStatusListener mCallback){
  295.     _MailAuthFragment mFragment = new _MailAuthFragment();
  296.     mFragment.setListener(mCallback);
  297.     return mFragment;
  298. }
  299.  
  300. /**PRIVATE METHODS**/
  301.  
  302. /**Check AuthStatus, if CurrentUser!=null but not verified -> Show VerificationScreen**/
  303. private void checkAuthStatus(){
  304.     if(FirebaseAuth.getInstance().getCurrentUser() != null && !FirebaseAuth.getInstance().getCurrentUser().isEmailVerified()){
  305.         this.showConfirmEmailSubFragment(R.anim.slide_in_right_smooth);
  306.     }else{
  307.         this.showEnterEmailSubFragment(R.anim.slide_in_right_smooth);
  308.     }
  309. }
  310.  
  311. /**Handle Registration Process Fragments**/
  312. private void performFragmentTransaction(Fragment mFragment, String mTag, int mEnterAnim){
  313.     mActiveWindow = mTag;
  314.     FragmentTransaction mFragmentTransaction = getChildFragmentManager().beginTransaction();
  315.     mFragmentTransaction.setCustomAnimations(mEnterAnim, 0);
  316.     mFragmentTransaction.replace(R.id.fragment_mailauth_container, mFragment, mTag);
  317.     mFragmentTransaction.commit();
  318. }
  319.  
  320. /**MailAuth receives any valid TextChange from SubFragment**/
  321. private void showEnterEmailSubFragment(int mEnterAnim){
  322.  
  323.     this.performFragmentTransaction(EnterMailSubFragment.newInstance(new EnterMailSubFragment.EnterMailListener() {
  324.         @Override
  325.         public void onEmailChanged(String mEmail) {
  326.             _MailAuthFragment.this.mEmail = mEmail;
  327.         }
  328.  
  329.         @Override
  330.         public void onPasswordChanged(String mPassword) {
  331.             _MailAuthFragment.this.mPassword = mPassword;
  332.         }
  333.     }), "EnterMail", mEnterAnim);
  334.  
  335. }
  336.  
  337. /**MailAuth receives resend VerificationEmailRequest from SubFragment**/
  338. private void showConfirmEmailSubFragment(int mEnterAnim){
  339.     this.performFragmentTransaction(new ConfirmMailSubFragment(), "ConfirmMail", R.anim.slide_in_right_smooth);
  340. }
  341.  
  342. private void initButton(final View mView){
  343.     /**Custom Button using AfterEffects, Bodymovin and LottieView, contains some commands like "transform to tick"**/
  344.     mCheckCancelButton = new CheckCancelButton((LottieAnimationView)mView.findViewById(R.id.fragment_mailauth_nextBtn));
  345.     mCheckCancelButton.setCustomClickListener(new CheckCancelButton.CheckCancelButtonClickListener() {
  346.         @Override
  347.         public void onClick() {
  348.             switch(mActiveWindow){
  349.                 case "EnterMail":
  350.                     // the received Email and Password will be passed to AuthHandlerClass - not implemented yet
  351.                     break;
  352.                 case "ConfirmMail":
  353.                     // the AuthHandler will reloard the currentUser and check for email verification Status - not implemented yet
  354.                     break;
  355.             }
  356.         }
  357.     });
  358. }
  359.      
  360. public class EnterMailSubFragment extends Fragment {
  361.  
  362. public interface EnterMailListener{
  363.     void onEmailChanged(String mEmail);
  364.     void onPasswordChanged(String mPassword);
  365. }
  366.  
  367. private EnterMailListener   mCallback;
  368. private EditText            mEmail, mPassword;
  369. private ImageView           mEmailWarning, mPasswordWarning;
  370.  
  371. @Override
  372. public void onCreate(@Nullable Bundle savedInstanceState) {
  373.     super.onCreate(savedInstanceState);
  374. }
  375. @Nullable
  376. @Override
  377. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  378.     View mView = inflater.inflate(R.layout.subfragment_mailauth, container, false);
  379.     this.initUiWidgets(mView);
  380.     this.initEditText(mView);
  381.     return mView;
  382. }
  383.  
  384. public void setEnterMailListener(EnterMailListener mMailListener){
  385.     this.mCallback = mMailListener;
  386. }
  387.  
  388. public static EnterMailSubFragment newInstance(EnterMailListener mMailListener){
  389.     EnterMailSubFragment mFragment = new EnterMailSubFragment();
  390.     mFragment.setEnterMailListener(mMailListener);
  391.     return mFragment;
  392. }
  393.  
  394. private void initUiWidgets(View mView){
  395.     mEmail =            mView.findViewById(R.id.subfragment_mailauth_email_ET);
  396.     mPassword =         mView.findViewById(R.id.subfragment_mailauth_password_et);
  397.     mEmailWarning =     mView.findViewById(R.id.subfragment_mailauth_email_warning);
  398.     mPasswordWarning =  mView.findViewById(R.id.subfragment_mailauth_password_warning);
  399. }
  400.  
  401. /**Any Valid Entry will be passed to MailFragment**/
  402.  
  403. private void initEditText(final View mView){
  404.     mEmail.addTextChangedListener(new TextWatcher() {
  405.         @Override
  406.         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  407.  
  408.         }
  409.  
  410.         @Override
  411.         public void onTextChanged(CharSequence s, int start, int before, int count) {
  412.  
  413.         }
  414.  
  415.         @Override
  416.         public void afterTextChanged(Editable s) {
  417.             if(mEmail.getText() != null && Patterns.EMAIL_ADDRESS.matcher(mEmail.getText().toString()).matches()){
  418.                 mEmailWarning.setVisibility(View.GONE);
  419.                 mCallback.onEmailChanged(mEmail.getText().toString());
  420.             }else{
  421.                 mEmailWarning.setVisibility(View.VISIBLE);
  422.             }
  423.         }
  424.     });
  425.  
  426.     mPassword.addTextChangedListener(new TextWatcher() {
  427.         @Override
  428.         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  429.  
  430.         }
  431.  
  432.         @Override
  433.         public void onTextChanged(CharSequence s, int start, int before, int count) {
  434.         }
  435.  
  436.         @Override
  437.         public void afterTextChanged(Editable s) {
  438.             if(mPassword.getText() != null && mPassword.getText().length() >= 6){
  439.                 mPasswordWarning.setVisibility(View.GONE);
  440.                 mCallback.onPasswordChanged(mPassword.getText().toString());
  441.             }else{
  442.                 mPasswordWarning.setVisibility(View.VISIBLE);
  443.             }
  444.         }
  445.     });
  446. }
  447.      
  448. public class ConfirmMailSubFragment extends Fragment {
  449.  
  450. public interface ConfirmMailListener{
  451.     void onResendEmail();
  452. }
  453.  
  454. private ConfirmMailListener mCallback;
  455.  
  456. @Override
  457. public void onCreate(@Nullable Bundle savedInstanceState) {
  458.     super.onCreate(savedInstanceState);
  459. }
  460.  
  461. @Nullable
  462. @Override
  463. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  464.     View mView = inflater.inflate(R.layout.subfragment_confirmmail, container, false);
  465.  
  466.     return mView;
  467. }
  468.  
  469. public void setConfirmMailListener(ConfirmMailListener mCallback){
  470.     this.mCallback = mCallback;
  471. }
  472.  
  473. public static ConfirmMailSubFragment newInstance(ConfirmMailListener mCallback){
  474.     ConfirmMailSubFragment mFragment = new ConfirmMailSubFragment();
  475.     mFragment.setConfirmMailListener(mCallback);
  476.     return mFragment;
  477. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top