Advertisement
gust94

LoginMainPageActivity

Jan 19th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. package com.dss.invitup.vc.login;
  2. import android.Manifest;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.pm.PackageManager;
  6. import android.graphics.Color;
  7. import android.net.Uri;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.provider.Settings;
  11. import android.support.v4.app.ActivityCompat;
  12. import android.support.v4.app.FragmentManager;
  13. import android.support.v7.app.AppCompatActivity;
  14. import android.telephony.TelephonyManager;
  15. import android.util.Log;
  16. import android.view.Window;
  17. import android.view.WindowManager;
  18. import android.widget.FrameLayout;
  19. import com.dss.invitup.R;
  20. import com.dss.invitup.events.AddPicturePermissionEvent;
  21. import com.dss.invitup.events.LoginMainPageBackButtonTapped;
  22. import com.dss.invitup.singleton.InterfaceManager;
  23. import com.dss.invitup.singleton.SettingsManager;
  24. import com.dss.invitup.vc.homePage.HomePageActivity;
  25. import com.facebook.FacebookSdk;
  26. import bolts.AppLinks;
  27. import de.greenrobot.event.EventBus;
  28. /**
  29. * Copyright © 2016 Dihardja Software Solutions. All rights reserved.
  30. */
  31. public class LoginMainPageActivity extends AppCompatActivity {
  32. LoginMainPageFragment loginMainPageFragment;
  33. SignInFragment signInFragment;
  34. SignUpFragment signUpFragment;
  35. ForgotPasswordFragment forgotPasswordFragment;
  36. FragmentManager fragmentManager;
  37. FrameLayout containerRootFrameLayout;
  38. public final int LOGIN_FRAGMENT = 1;
  39. public final int SIGNIN_FRAGMENT = 2;
  40. public final int SIGNUP_FRAGMENT = 3;
  41. public final int FORGOTPASSWORD_FRAGMENT = 4;
  42. boolean isEligibleToTakePicture = false;
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. requestWindowFeature(Window.FEATURE_NO_TITLE);
  46. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  47. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_login_main_page);
  50. if (SettingsManager.getInstance().getCurrentUser() != null) {
  51. Intent i = new Intent(this, HomePageActivity.class);
  52. startActivity(i);
  53. finish();
  54. return;
  55. } else {
  56. if (!EventBus.getDefault().isRegistered(this)) {
  57. EventBus.getDefault().register(this);
  58. }
  59. containerRootFrameLayout = (FrameLayout) findViewById(R.id.loginContainerRootFrameLayout);
  60. fragmentManager = getSupportFragmentManager();
  61.  
  62. loginMainPageFragment = new LoginMainPageFragment();
  63. signInFragment = new SignInFragment();
  64. signUpFragment = new SignUpFragment();
  65. forgotPasswordFragment = new ForgotPasswordFragment();
  66.  
  67. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  68. Window window = getWindow();
  69. window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
  70. window.setStatusBarColor(Color.parseColor("#4B76A9"));
  71. }
  72. initView();
  73. containerRootFrameLayout.setBackground(InterfaceManager.sharedInstance().getBackgroundAnimation());
  74. if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED ) {
  75. ActivityCompat.requestPermissions(this,
  76. new String[]{Manifest.permission.READ_PHONE_STATE},
  77. 2);
  78. return;
  79. }
  80. else {
  81. registerDeviceId();
  82. }
  83. }
  84. FacebookSdk.sdkInitialize(getApplicationContext());
  85. Uri targetUrl = AppLinks.getTargetUrlFromInboundIntent(this, getIntent());
  86. if(targetUrl!=null){
  87. Log.v("Activity", "App Link Target Url: " + targetUrl.toString());
  88. }
  89. }
  90. @Override
  91. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  92. switch (requestCode){
  93. case 0:
  94. int flag=0;
  95. if(grantResults.length>0){
  96. for(int i=0; i<grantResults.length; i++){
  97. if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
  98. flag++;
  99. }
  100. }
  101. Log.v("login_flag", flag+"");
  102. if(flag==permissions.length){
  103. isEligibleToTakePicture = true;
  104. }else{
  105. isEligibleToTakePicture = false;
  106. }
  107. EventBus.getDefault().post(new AddPicturePermissionEvent(isEligibleToTakePicture, requestCode));
  108. }
  109. break;
  110. case 1:
  111. if(grantResults.length>0) {
  112. if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  113. isEligibleToTakePicture = true;
  114. }else{
  115. isEligibleToTakePicture = false;
  116. }
  117. EventBus.getDefault().post(new AddPicturePermissionEvent(isEligibleToTakePicture, requestCode));
  118. }
  119. break;
  120. }
  121. }
  122. @Override
  123. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  124. super.onActivityResult(requestCode, resultCode, data);
  125. }
  126. public void initView() {
  127. fragmentManager.beginTransaction()
  128. .replace(R.id.loginContainerRootFrameLayout, loginMainPageFragment)
  129. .commit();
  130. }
  131. public void onEvent(LoginMainPageBackButtonTapped event) {
  132. initView();
  133. }
  134. @Override
  135. public void onBackPressed() {
  136. if (SettingsManager.getInstance().getCurrentFragment() == LOGIN_FRAGMENT) {
  137. super.onBackPressed();
  138. finish();
  139. }else if (SettingsManager.getInstance().getCurrentFragment() == FORGOTPASSWORD_FRAGMENT){
  140. fragmentManager.beginTransaction()
  141. .replace(R.id.loginContainerRootFrameLayout, signInFragment)
  142. .commit();
  143. SettingsManager.getInstance().setCurrentFragment(SIGNIN_FRAGMENT);
  144. }else {
  145. fragmentManager.beginTransaction()
  146. .replace(R.id.loginContainerRootFrameLayout, loginMainPageFragment)
  147. .commit();
  148. SettingsManager.getInstance().setCurrentFragment(LOGIN_FRAGMENT);
  149. }
  150. }
  151. public void registerDeviceId(){
  152. TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  153. String deviceId = "";
  154. if(telephonyManager.getDeviceId()!=null){
  155. deviceId = telephonyManager.getDeviceId();
  156. }else{
  157. deviceId = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
  158. }
  159. if(!deviceId.equalsIgnoreCase("")) {
  160. SettingsManager.getInstance().setDeviceId(deviceId);
  161. }
  162. }
  163. @Override
  164. public void onDestroy(){
  165. super.onDestroy();
  166. if(EventBus.getDefault().isRegistered(this)) {
  167. EventBus.getDefault().unregister(this);
  168. }
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement