Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 KB | None | 0 0
  1. buildscript {
  2.  
  3. repositories {
  4. google()
  5. jcenter()
  6. }
  7. dependencies {
  8. classpath 'com.android.tools.build:gradle:3.0.1'
  9.  
  10.  
  11. // NOTE: Do not place your application dependencies here; they belong
  12. // in the individual module build.gradle files
  13. classpath 'com.google.gms:google-services:3.2.0'
  14. }
  15. }
  16.  
  17. allprojects {
  18. repositories {
  19. google()
  20. jcenter()
  21. maven {
  22. url "https://maven.google.com" // Google's Maven repository
  23. }
  24. }
  25. //
  26. // task clean(type: Delete) {
  27. // delete rootProject.buildDir
  28. // }
  29.  
  30.  
  31. }
  32.  
  33. apply plugin: 'com.android.application'
  34.  
  35. android {
  36. compileSdkVersion 26
  37. defaultConfig {
  38. applicationId "com.example.android.battle_poke"
  39. minSdkVersion 15
  40. targetSdkVersion 26
  41. versionCode 1
  42. versionName "1.0"
  43. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  44. }
  45. buildTypes {
  46. release {
  47. minifyEnabled false
  48. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  49. }
  50. }
  51. sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/'] } }
  52. }
  53.  
  54. dependencies {
  55. implementation fileTree(dir: 'libs', include: ['*.jar'])
  56. implementation 'com.android.support:appcompat-v7:26.1.0'
  57. implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  58. implementation 'com.google.firebase:firebase-auth:11.8.0'
  59. testImplementation 'junit:junit:4.12'
  60. androidTestImplementation 'com.android.support.test:runner:1.0.1'
  61. androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
  62.  
  63. compile 'com.google.firebase:firebase-auth:11.8.0'
  64.  
  65.  
  66.  
  67. }
  68.  
  69.  
  70. apply plugin: 'com.google.gms.google-services'
  71.  
  72. /**
  73. * Copyright 2016 Google Inc. All Rights Reserved.
  74. *
  75. * Licensed under the Apache License, Version 2.0 (the "License");
  76. * you may not use this file except in compliance with the License.
  77. * You may obtain a copy of the License at
  78. *
  79. * http://www.apache.org/licenses/LICENSE-2.0
  80. *
  81. * Unless required by applicable law or agreed to in writing, software
  82. * distributed under the License is distributed on an "AS IS" BASIS,
  83. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  84. * See the License for the specific language governing permissions and
  85. * limitations under the License.
  86. */
  87.  
  88. package com.example.android.battle_poke;
  89.  
  90. import android.os.Bundle;
  91. import android.support.annotation.NonNull;
  92. import android.text.TextUtils;
  93. import android.util.Log;
  94. import android.view.View;
  95. import android.widget.EditText;
  96. import android.widget.TextView;
  97. import android.widget.Toast;
  98.  
  99. import com.google.android.gms.tasks.OnCompleteListener;
  100. import com.google.android.gms.tasks.Task;
  101. import com.google.firebase.auth.AuthResult;
  102. import com.google.firebase.auth.FirebaseAuth;
  103. import com.google.firebase.auth.FirebaseUser;
  104.  
  105. public class EmailPasswordActivity extends BaseActivity implements
  106. View.OnClickListener {
  107.  
  108. private static final String TAG = "EmailPassword";
  109.  
  110. private TextView mStatusTextView;
  111. private TextView mDetailTextView;
  112. private EditText mEmailField;
  113. private EditText mPasswordField;
  114.  
  115. // [START declare_auth]
  116. private FirebaseAuth mAuth;
  117. // [END declare_auth]
  118.  
  119. @Override
  120. public void onCreate(Bundle savedInstanceState) {
  121. super.onCreate(savedInstanceState);
  122. setContentView(R.layout.activity_emailpassword);
  123.  
  124. // Views
  125. mStatusTextView = findViewById(R.id.status);
  126. mDetailTextView = findViewById(R.id.detail);
  127. mEmailField = findViewById(R.id.field_email);
  128. mPasswordField = findViewById(R.id.field_password);
  129.  
  130. // Buttons
  131. findViewById(R.id.email_sign_in_button).setOnClickListener(this);
  132. findViewById(R.id.email_create_account_button).setOnClickListener(this);
  133. findViewById(R.id.sign_out_button).setOnClickListener(this);
  134. findViewById(R.id.verify_email_button).setOnClickListener(this);
  135.  
  136. // [START initialize_auth]
  137. mAuth = FirebaseAuth.getInstance();
  138. // [END initialize_auth]
  139. }
  140.  
  141. // [START on_start_check_user]
  142. @Override
  143. public void onStart() {
  144. super.onStart();
  145. // Check if user is signed in (non-null) and update UI accordingly.
  146. FirebaseUser currentUser = mAuth.getCurrentUser();
  147. updateUI(currentUser);
  148. }
  149. // [END on_start_check_user]
  150.  
  151. private void createAccount(String email, String password) {
  152. Log.d(TAG, "createAccount:" + email);
  153. if (!validateForm()) {
  154. return;
  155. }
  156.  
  157. showProgressDialog();
  158.  
  159. // [START create_user_with_email]
  160. mAuth.createUserWithEmailAndPassword(email, password)
  161. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  162. @Override
  163. public void onComplete(@NonNull Task<AuthResult> task) {
  164. if (task.isSuccessful()) {
  165. // Sign in success, update UI with the signed-in user's information
  166. Log.d(TAG, "createUserWithEmail:success");
  167. FirebaseUser user = mAuth.getCurrentUser();
  168. updateUI(user);
  169. } else {
  170. // If sign in fails, display a message to the user.
  171. Log.w(TAG, "createUserWithEmail:failure", task.getException());
  172. Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
  173. Toast.LENGTH_SHORT).show();
  174. updateUI(null);
  175. }
  176.  
  177. // [START_EXCLUDE]
  178. hideProgressDialog();
  179. // [END_EXCLUDE]
  180. }
  181. });
  182. // [END create_user_with_email]
  183. }
  184.  
  185. private void signIn(String email, String password) {
  186. Log.d(TAG, "signIn:" + email);
  187. if (!validateForm()) {
  188. return;
  189. }
  190.  
  191. showProgressDialog();
  192.  
  193. // [START sign_in_with_email]
  194. mAuth.signInWithEmailAndPassword(email, password)
  195. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  196. @Override
  197. public void onComplete(@NonNull Task<AuthResult> task) {
  198. if (task.isSuccessful()) {
  199. // Sign in success, update UI with the signed-in user's information
  200. Log.d(TAG, "signInWithEmail:success");
  201. FirebaseUser user = mAuth.getCurrentUser();
  202. updateUI(user);
  203. } else {
  204. // If sign in fails, display a message to the user.
  205. Log.w(TAG, "signInWithEmail:failure", task.getException());
  206. Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
  207. Toast.LENGTH_SHORT).show();
  208. updateUI(null);
  209. }
  210.  
  211. // [START_EXCLUDE]
  212. if (!task.isSuccessful()) {
  213. mStatusTextView.setText("認証に失敗しました");
  214. }
  215. hideProgressDialog();
  216. // [END_EXCLUDE]
  217. }
  218. });
  219. // [END sign_in_with_email]
  220. }
  221.  
  222. private void signOut() {
  223. mAuth.signOut();
  224. updateUI(null);
  225. }
  226.  
  227. private void sendEmailVerification() {
  228. // Disable button
  229. findViewById(R.id.verify_email_button).setEnabled(false);
  230.  
  231. // Send verification email
  232. // [START send_email_verification]
  233. final FirebaseUser user = mAuth.getCurrentUser();
  234. user.sendEmailVerification()
  235. .addOnCompleteListener(this, new OnCompleteListener<Void>() {
  236. @Override
  237. public void onComplete(@NonNull Task<Void> task) {
  238. // [START_EXCLUDE]
  239. // Re-enable button
  240. findViewById(R.id.verify_email_button).setEnabled(true);
  241.  
  242. if (task.isSuccessful()) {
  243. Toast.makeText(EmailPasswordActivity.this,
  244. "Verification email sent to " + user.getEmail(),
  245. Toast.LENGTH_SHORT).show();
  246. } else {
  247. Log.e(TAG, "sendEmailVerification", task.getException());
  248. Toast.makeText(EmailPasswordActivity.this,
  249. "Failed to send verification email.",
  250. Toast.LENGTH_SHORT).show();
  251. }
  252. // [END_EXCLUDE]
  253. }
  254. });
  255. // [END send_email_verification]
  256. }
  257.  
  258. private boolean validateForm() {
  259. boolean valid = true;
  260.  
  261. String email = mEmailField.getText().toString();
  262. if (TextUtils.isEmpty(email)) {
  263. mEmailField.setError("Required.");
  264. valid = false;
  265. } else {
  266. mEmailField.setError(null);
  267. }
  268.  
  269. String password = mPasswordField.getText().toString();
  270. if (TextUtils.isEmpty(password)) {
  271. mPasswordField.setError("Required.");
  272. valid = false;
  273. } else {
  274. mPasswordField.setError(null);
  275. }
  276.  
  277. return valid;
  278. }
  279.  
  280. private void updateUI(FirebaseUser user) {
  281. hideProgressDialog();
  282. if (user != null) {
  283. mStatusTextView.setText(getString(R.string.emailpassword_status_fmt,
  284. user.getEmail(), user.isEmailVerified()));
  285. mDetailTextView.setText(getString(R.string.firebase_status_fmt, user.getUid()));
  286.  
  287. findViewById(R.id.email_password_buttons).setVisibility(View.GONE);
  288. findViewById(R.id.email_password_fields).setVisibility(View.GONE);
  289. findViewById(R.id.signed_in_buttons).setVisibility(View.VISIBLE);
  290.  
  291. findViewById(R.id.verify_email_button).setEnabled(!user.isEmailVerified());
  292. } else {
  293. mStatusTextView.setText("サインアウト");
  294. mDetailTextView.setText(null);
  295.  
  296. findViewById(R.id.email_password_buttons).setVisibility(View.VISIBLE);
  297. findViewById(R.id.email_password_fields).setVisibility(View.VISIBLE);
  298. findViewById(R.id.signed_in_buttons).setVisibility(View.GONE);
  299. }
  300. }
  301.  
  302. @Override
  303. public void onClick(View v) {
  304. int i = v.getId();
  305. if (i == R.id.email_create_account_button) {
  306. createAccount(mEmailField.getText().toString(), mPasswordField.getText().toString());
  307. } else if (i == R.id.email_sign_in_button) {
  308. signIn(mEmailField.getText().toString(), mPasswordField.getText().toString());
  309. } else if (i == R.id.sign_out_button) {
  310. signOut();
  311. } else if (i == R.id.verify_email_button) {
  312. sendEmailVerification();
  313. }
  314. }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement