Guest User

Untitled

a guest
Sep 4th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.89 KB | None | 0 0
  1. mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
  2. .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener(){
  3. @Override
  4. public void OnConnectionFailed(@NonNull ConnectionResult connectionResult) {
  5. Toast.makeText(Login.this, "Something went wrong", Toast.LENGTH_SHORT).show();
  6. }
  7. })
  8.  
  9. package com.app.surebettips;
  10.  
  11. import android.accounts.Account;
  12. import android.content.Intent;
  13. import android.os.Bundle;
  14. import android.support.annotation.NonNull;
  15. import android.support.v4.content.ContextCompat;
  16. import android.support.v7.app.AppCompatActivity;
  17. import android.text.TextUtils;
  18. import android.util.Log;
  19. import android.view.View;
  20. import android.widget.Button;
  21. import android.widget.EditText;
  22. import android.widget.ImageView;
  23. import android.widget.Toast;
  24.  
  25. import com.google.android.gms.auth.api.Auth;
  26. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  27. import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  28. import com.google.android.gms.auth.api.signin.GoogleSignInResult;
  29. import com.google.android.gms.common.ConnectionResult;
  30. import com.google.android.gms.common.SignInButton;
  31. import com.google.android.gms.common.api.GoogleApiClient;
  32. import com.google.android.gms.tasks.OnCompleteListener;
  33. import com.google.android.gms.tasks.Task;
  34. import com.google.firebase.auth.AuthCredential;
  35. import com.google.firebase.auth.AuthResult;
  36. import com.google.firebase.auth.FirebaseAuth;
  37. import com.google.firebase.auth.FirebaseUser;
  38. import com.google.firebase.auth.GoogleAuthProvider;
  39. import com.google.firebase.database.DatabaseReference;
  40. import com.google.firebase.database.FirebaseDatabase;
  41.  
  42. public class Login extends AppCompatActivity {
  43.  
  44. Button loginButton;
  45. EditText logMail;
  46. EditText logPass;
  47. Button registerButton;
  48. FirebaseUser currentUser;
  49. FirebaseDatabase database = FirebaseDatabase.getInstance();
  50. DatabaseReference myRef = database.getReference();
  51. ImageView imageView;
  52. int counter;
  53. private FirebaseAuth mAuth;
  54. private SignInButton mGoogleBtn;
  55. private static final int RC_SIGN_IN = 1;
  56. private GoogleApiClient mGoogleApiClient;
  57. private FirebaseAuth.AuthStateListener mAuthListener;
  58.  
  59.  
  60. @Override
  61. protected void onCreate(Bundle savedInstanceState) {
  62. super.onCreate(savedInstanceState);
  63. setContentView(R.layout.activity_login);
  64. loginButton = findViewById(R.id.loginButton);
  65.  
  66.  
  67.  
  68. mAuth = FirebaseAuth.getInstance();
  69. mAuthListener = new FirebaseAuth.AuthStateListener() {
  70. @Override
  71. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  72. if(firebaseAuth.getCurrentUser() != null){
  73. Toast.makeText(Login.this, "You're here", Toast.LENGTH_SHORT).show();
  74. }
  75. }
  76. };
  77.  
  78. mGoogleBtn = (SignInButton) findViewById(R.id.googleBtn);
  79.  
  80. // Configure sign-in to request the user's ID, email address, and basic
  81. // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
  82. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  83. .requestEmail()
  84. .build();
  85.  
  86.  
  87. mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
  88. .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener(){
  89. @Override
  90. public void OnConnectionFailed(@NonNull ConnectionResult connectionResult) {
  91. Toast.makeText(Login.this, "Something went wrong", Toast.LENGTH_SHORT).show();
  92. }
  93. })
  94.  
  95. .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
  96. .build();
  97.  
  98.  
  99. mGoogleBtn.setOnClickListener(new View.OnClickListener() {
  100. @Override
  101. public void onClick(View view) {
  102. signIn();
  103. }
  104. });
  105. counter = 0;
  106.  
  107. imageView = findViewById(R.id.imageView);
  108.  
  109. try {
  110. imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.arkaplan));
  111. } catch (Exception e) {
  112. Toast.makeText(getApplicationContext(), "Arka plan, cihazınızda desteklenmediği için düz renk olarak atanmıştır", Toast.LENGTH_LONG).show();
  113. }
  114.  
  115. imageView.setOnClickListener(new View.OnClickListener() {
  116. @Override
  117. public void onClick(View v) {
  118. countdatabase(v);
  119. }
  120. });
  121.  
  122.  
  123. logMail = (EditText) findViewById(R.id.logMail);
  124. logPass = (EditText) findViewById(R.id.logPass);
  125.  
  126. registerButton = (Button) findViewById(R.id.registerButton);
  127.  
  128.  
  129. loginButton.setOnClickListener(new View.OnClickListener() {
  130. @Override
  131. public void onClick(View v) {
  132. login(v);
  133. }
  134. });
  135.  
  136. registerButton.setOnClickListener(new View.OnClickListener() {
  137. @Override
  138. public void onClick(View v) {
  139. reg(v);
  140. }
  141. });
  142. }
  143.  
  144. private void countdatabase(View v) {
  145.  
  146. // counter++;
  147.  
  148. if (counter == 15) {
  149.  
  150. myRef.child("maç1").child("Oynayanlar").setValue("Maç1 - Maç2");
  151. myRef.child("maç1").child("Kredi").setValue("20");
  152. myRef.child("maç1").child("Tahmin").setValue("ÜST");
  153. myRef.child("maç1").child("Güven").setValue("%90");
  154. myRef.child("maç1").child("Oran").setValue("1.8");
  155. myRef.child("maç1").child("Saat").setValue("20:00");
  156. myRef.child("maç1").child("Prediction").setValue("Win");
  157.  
  158. myRef.child("maç2").child("Oynayanlar").setValue("Maç1 - Maç2");
  159. myRef.child("maç2").child("Kredi").setValue("20");
  160. myRef.child("maç2").child("Tahmin").setValue("ÜST");
  161. myRef.child("maç2").child("Güven").setValue("%90");
  162. myRef.child("maç2").child("Oran").setValue("1.8");
  163. myRef.child("maç2").child("Saat").setValue("20:00");
  164. myRef.child("maç2").child("Prediction").setValue("Win");
  165.  
  166. myRef.child("maç3").child("Oynayanlar").setValue("Maç1 - Maç2");
  167. myRef.child("maç3").child("Kredi").setValue("20");
  168. myRef.child("maç3").child("Tahmin").setValue("ÜST");
  169. myRef.child("maç3").child("Güven").setValue("%90");
  170. myRef.child("maç3").child("Oran").setValue("1.8");
  171. myRef.child("maç3").child("Saat").setValue("20:00");
  172. myRef.child("maç3").child("Prediction").setValue("Win");
  173.  
  174. myRef.child("maç4").child("Oynayanlar").setValue("Maç1 - Maç2");
  175. myRef.child("maç4").child("Kredi").setValue("20");
  176. myRef.child("maç4").child("Tahmin").setValue("ÜST");
  177. myRef.child("maç4").child("Güven").setValue("%90");
  178. myRef.child("maç4").child("Oran").setValue("1.8");
  179. myRef.child("maç4").child("Saat").setValue("20:00");
  180. myRef.child("maç4").child("Prediction").setValue("Win");
  181.  
  182. myRef.child("maç5").child("Oynayanlar").setValue("Maç1 - Maç2");
  183. myRef.child("maç5").child("Kredi").setValue("20");
  184. myRef.child("maç5").child("Tahmin").setValue("ÜST");
  185. myRef.child("maç5").child("Güven").setValue("%90");
  186. myRef.child("maç5").child("Oran").setValue("1.8");
  187. myRef.child("maç5").child("Saat").setValue("20:00");
  188. myRef.child("maç5").child("Prediction").setValue("Win");
  189.  
  190. myRef.child("maç6").child("Oynayanlar").setValue("Maç1 - Maç2");
  191. myRef.child("maç6").child("Kredi").setValue("20");
  192. myRef.child("maç6").child("Tahmin").setValue("ÜST");
  193. myRef.child("maç6").child("Güven").setValue("%90");
  194. myRef.child("maç6").child("Oran").setValue("1.8");
  195. myRef.child("maç6").child("Saat").setValue("20:00");
  196. myRef.child("maç6").child("Prediction").setValue("Win");
  197.  
  198. myRef.child("maç7").child("Oynayanlar").setValue("Maç1 - Maç2");
  199. myRef.child("maç7").child("Kredi").setValue("20");
  200. myRef.child("maç7").child("Tahmin").setValue("ÜST");
  201. myRef.child("maç7").child("Güven").setValue("%90");
  202. myRef.child("maç7").child("Oran").setValue("1.8");
  203. myRef.child("maç7").child("Saat").setValue("20:00");
  204. myRef.child("maç7").child("Prediction").setValue("Win");
  205.  
  206. myRef.child("maç8").child("Oynayanlar").setValue("Maç1 - Maç2");
  207. myRef.child("maç8").child("Kredi").setValue("20");
  208. myRef.child("maç8").child("Tahmin").setValue("ÜST");
  209. myRef.child("maç8").child("Güven").setValue("%90");
  210. myRef.child("maç8").child("Oran").setValue("1.8");
  211. myRef.child("maç8").child("Saat").setValue("20:00");
  212. myRef.child("maç8").child("Prediction").setValue("Win");
  213.  
  214. myRef.child("maç9").child("Oynayanlar").setValue("Maç1 - Maç2");
  215. myRef.child("maç9").child("Kredi").setValue("20");
  216. myRef.child("maç9").child("Tahmin").setValue("ÜST");
  217. myRef.child("maç9").child("Güven").setValue("%90");
  218. myRef.child("maç9").child("Oran").setValue("1.8");
  219. myRef.child("maç9").child("Saat").setValue("20:00");
  220. myRef.child("maç9").child("Prediction").setValue("Win");
  221.  
  222. myRef.child("maç10").child("Oynayanlar").setValue("Maç1 - Maç2");
  223. myRef.child("maç10").child("Kredi").setValue("20");
  224. myRef.child("maç10").child("Tahmin").setValue("ÜST");
  225. myRef.child("maç10").child("Güven").setValue("%90");
  226. myRef.child("maç10").child("Oran").setValue("1.8");
  227. myRef.child("maç10").child("Saat").setValue("20:00");
  228. myRef.child("maç10").child("Prediction").setValue("Win");
  229.  
  230. myRef.child("maç11").child("Oynayanlar").setValue("Maç1 - Maç2");
  231. myRef.child("maç11").child("Kredi").setValue("20");
  232. myRef.child("maç11").child("Tahmin").setValue("ÜST");
  233. myRef.child("maç11").child("Güven").setValue("%90");
  234. myRef.child("maç11").child("Oran").setValue("1.8");
  235. myRef.child("maç11").child("Saat").setValue("20:00");
  236. myRef.child("maç11").child("Prediction").setValue("Win");
  237.  
  238. myRef.child("maç12").child("Oynayanlar").setValue("Maç1 - Maç2");
  239. myRef.child("maç12").child("Kredi").setValue("20");
  240. myRef.child("maç12").child("Tahmin").setValue("ÜST");
  241. myRef.child("maç12").child("Güven").setValue("%90");
  242. myRef.child("maç12").child("Oran").setValue("1.8");
  243. myRef.child("maç12").child("Saat").setValue("20:00");
  244. myRef.child("maç12").child("Prediction").setValue("Win");
  245.  
  246. myRef.child("maç13").child("Oynayanlar").setValue("BurayıDoldur");
  247. myRef.child("maç13").child("Kredi").setValue("20");
  248. myRef.child("maç13").child("Tahmin").setValue("ÜST");
  249. myRef.child("maç13").child("Güven").setValue("%90");
  250. myRef.child("maç13").child("Oran").setValue("1.8");
  251. myRef.child("maç13").child("Saat").setValue("20:00");
  252. myRef.child("maç13").child("Prediction").setValue("Win");
  253.  
  254. myRef.child("maç14").child("Oynayanlar").setValue("Maç1 - Maç2");
  255. myRef.child("maç14").child("Kredi").setValue("20");
  256. myRef.child("maç14").child("Tahmin").setValue("ÜST");
  257. myRef.child("maç14").child("Güven").setValue("%90");
  258. myRef.child("maç14").child("Oran").setValue("1.8");
  259. myRef.child("maç14").child("Saat").setValue("20:00");
  260. myRef.child("maç14").child("Prediction").setValue("Win");
  261.  
  262. myRef.child("maç15").child("Oynayanlar").setValue("Maç1 - Maç2");
  263. myRef.child("maç15").child("Kredi").setValue("20");
  264. myRef.child("maç15").child("Tahmin").setValue("ÜST");
  265. myRef.child("maç15").child("Güven").setValue("%90");
  266. myRef.child("maç15").child("Oran").setValue("1.8");
  267. myRef.child("maç15").child("Saat").setValue("20:00");
  268. myRef.child("maç15").child("Prediction").setValue("Win");
  269.  
  270. myRef.child("Reklam Kredisi").setValue("15");
  271. myRef.child("Kayan Yazı").setValue("...Kayan Yazı Alanı...");
  272.  
  273. Toast.makeText(getApplicationContext(), "Tüm database güncellendi", Toast.LENGTH_LONG).show();
  274.  
  275. counter = 0;
  276. }
  277.  
  278. }
  279.  
  280.  
  281. public void login(View view) {
  282. if (!TextUtils.isEmpty(logMail.getText().toString()) && !TextUtils.isEmpty(logPass.getText().toString())) {
  283. mAuth.signInWithEmailAndPassword(logMail.getText().toString(), logPass.getText().toString())
  284. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  285. @Override
  286. public void onComplete(@NonNull Task<AuthResult> task) {
  287. if (task.isSuccessful()) {
  288. FirebaseUser user = mAuth.getCurrentUser();
  289. System.out.println("OK");
  290. // Toast.makeText(getApplicationContext(), "Sayın " + user.getEmail() + " hoşgeldiniz", Toast.LENGTH_SHORT).show();
  291. startActivity(new Intent(getApplicationContext(), com.app.surebettips.AnaEkran.class));
  292.  
  293. } else {
  294. Toast.makeText(getApplicationContext(), "Başarısız", Toast.LENGTH_SHORT).show();
  295. System.out.println("NOT OK");
  296.  
  297.  
  298. }
  299.  
  300. // ...
  301. }
  302. });
  303. }
  304.  
  305. }
  306.  
  307. public void reg(View view) {
  308. startActivity(new Intent(getApplicationContext(), com.app.surebettips.Register.class));
  309. }
  310.  
  311. @Override
  312. public void onStart() {
  313. super.onStart();
  314. // Check if user is signed in (non-null) and update UI accordingly.
  315. currentUser = mAuth.getCurrentUser();
  316.  
  317. //Asagi biz ekledik tek satir
  318. mAuth.addAuthStateListener(mAuthListener);
  319. //Yukari biz ekledik tek satir
  320.  
  321. if (mAuth != null && currentUser != null) {
  322. startActivity(new Intent(getApplicationContext(), com.app.surebettips.AnaEkran.class));
  323. }
  324. }
  325.  
  326. private void signIn() {
  327. Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
  328. startActivityForResult(signInIntent, RC_SIGN_IN);
  329. }
  330.  
  331.  
  332.  
  333. @Override
  334. public void OnActivtyResult(int requestCode, int resultCode, Intent data){
  335. super.OnActivtyResult(requestCode,resultCode,data);
  336.  
  337. if(requestCode == RC_SIGN_IN ){
  338.  
  339. GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
  340. if(result.isSuccess()) {
  341. GoogleSignInAccount account = result.getSignInAccount();
  342. firebaseAuthWithGoogleAccount();
  343. }else {
  344. //ArmutSpor
  345. }
  346. }
  347. }
  348.  
  349. private void firebaseAuthWithGoogleAccount(GoogleSignInAccount account) {
  350.  
  351. AuthCredential credential = GoogleAuthProvider.getCredential(Account.getIdToken(), null);
  352. mAuth.signInWithCredential(credential)
  353. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  354. @Override
  355. public void onComplete(@NonNull Task<AuthResult> task) {
  356.  
  357. Log.d("TAG", "signInWithCredential:success" + task.isSuccessful());
  358. //FirebaseUser user = mAuth.getCurrentUser();
  359. //updateUI(user);
  360. if (!task.isSu1ccessful()){
  361. Log.w("TAG", "signInWithCredential:failure", task.getException());
  362. Toast.makeText(Login.this, "Authentication failed", Toast.LENGTH_SHORT).show();
  363. //updateUI(null);
  364. }
  365. }
  366.  
  367.  
  368. });
  369.  
  370. }
  371.  
  372.  
  373. }
Add Comment
Please, Sign In to add comment