Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.78 KB | None | 0 0
  1. ...
  2. compileSdkVersion 25
  3. buildToolsVersion "25.0.2"
  4. ...
  5.  
  6. ...
  7. <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  8. <uses-permission android:name="android.permission.READ_CONTACTS" />
  9. <uses-permission android:name="android.permission.READ_PROFILE" />
  10. ...
  11.  
  12. ...
  13. <android.support.design.widget.TextInputLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content">
  16.  
  17. <TextView
  18. android:id="@+id/email"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:hint="@string/email"
  22. android:inputType="textEmailAddress"
  23. android:maxLines="1"
  24. android:textColor="@android:color/white"
  25. />
  26.  
  27. </android.support.design.widget.TextInputLayout>
  28. ...
  29.  
  30. package com.company.product.activity;
  31.  
  32. import too.many.imports
  33.  
  34. import com.company.product.R;
  35.  
  36. import static android.Manifest.permission.READ_CONTACTS;
  37.  
  38.  
  39. public class LoginActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
  40.  
  41. // UI references.
  42. private EditText inputPassword;
  43. private AutoCompleteTextView inputEmail;
  44. private FirebaseAuth auth;
  45. private ProgressBar progressBar;
  46. private Button btnSignup, btnLogin, btnReset;
  47.  
  48. /**
  49. * Id to identity READ_CONTACTS permission request.
  50. */
  51. private static final int REQUEST_READ_CONTACTS = 0;
  52.  
  53. @Override
  54. protected void onCreate(Bundle savedInstanceState)
  55. {
  56. super.onCreate(savedInstanceState);
  57.  
  58. // Get Firebase auth instance
  59. auth = FirebaseAuth.getInstance();
  60.  
  61. if (auth.getCurrentUser() != null) {
  62. startActivity(new Intent(LoginActivity.this, MainActivity.class));
  63. finish();
  64. }
  65.  
  66. // set the content view
  67. setContentView(R.layout.activity_login);
  68.  
  69. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  70. setSupportActionBar(toolbar);
  71.  
  72. inputEmail = (AutoCompleteTextView) findViewById(R.id.email);
  73. inputPassword = (EditText) findViewById(R.id.password);
  74. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  75. btnSignup = (Button) findViewById(R.id.btn_signup);
  76. btnLogin = (Button) findViewById(R.id.btn_login);
  77. btnReset = (Button) findViewById(R.id.btn_reset_password);
  78.  
  79. populateAutoComplete();
  80.  
  81. //Get Firebase auth instance
  82. auth = FirebaseAuth.getInstance();
  83.  
  84. btnSignup.setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View v) {
  87. startActivity(new Intent(LoginActivity.this, SignupActivity.class));
  88. }
  89. });
  90.  
  91. btnReset.setOnClickListener(new View.OnClickListener() {
  92. @Override
  93. public void onClick(View v) {
  94. startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
  95. }
  96. });
  97.  
  98. btnLogin.setOnClickListener(new View.OnClickListener() {
  99. @Override
  100. public void onClick(View v) {
  101. String email = inputEmail.getText().toString();
  102. final String password = inputPassword.getText().toString();
  103.  
  104. if (TextUtils.isEmpty(email)) {
  105. Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
  106. return;
  107. }
  108.  
  109. if (TextUtils.isEmpty(password)) {
  110. Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
  111. return;
  112. }
  113.  
  114. progressBar.setVisibility(View.VISIBLE);
  115.  
  116. //authenticate user
  117. auth.signInWithEmailAndPassword(email, password)
  118. .addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>()
  119. {
  120. @Override
  121. public void onComplete(@NonNull Task<AuthResult> task) {
  122. // If sign in fails, display a message to the user. If sign in succeeds
  123. // the auth state listener will be notified and logic to handle the
  124. // signed in user can be handled in the listener.
  125. progressBar.setVisibility(View.GONE);
  126. if (!task.isSuccessful())
  127. {
  128. // there was an error
  129. if (password.length() < 6)
  130. {
  131. inputPassword.setError(getString(R.string.minimum_password));
  132. }
  133. else
  134. {
  135. Toast.makeText(LoginActivity.this, getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
  136. }
  137. }
  138. else
  139. {
  140. // Send to check if the user has been verified.
  141. checkIfEmailVerified();
  142. }
  143. }
  144.  
  145. // Logic for checking if the email is verified or not
  146. private void checkIfEmailVerified()
  147. {
  148. FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  149. if (user.isEmailVerified())
  150. {
  151. // user is verified, finish this activity and send to MainActivity.
  152. Toast.makeText(LoginActivity.this, "Successfully logged in: Lets start flying!", Toast.LENGTH_SHORT).show();
  153. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  154. startActivity(intent);
  155. finish();
  156. }
  157. else
  158. {
  159. // Email is not verified and prompt a message to the user and restart this activity.
  160. // NOTE: don't forget to log out the user.
  161. FirebaseAuth.getInstance().signOut();
  162. }
  163. }
  164.  
  165. });
  166. }
  167. });
  168. }
  169.  
  170. private void populateAutoComplete() {
  171. if (!mayRequestContacts()) {
  172. return;
  173. }
  174.  
  175. getSupportLoaderManager().initLoader(0, null, this);
  176. }
  177.  
  178. private boolean mayRequestContacts() {
  179. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  180. return true;
  181. }
  182. if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
  183. return true;
  184. }
  185. if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
  186. Snackbar.make(inputEmail, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
  187. .setAction(android.R.string.ok, new View.OnClickListener() {
  188. @Override
  189. @TargetApi(Build.VERSION_CODES.M)
  190. public void onClick(View v) {
  191. requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
  192. }
  193. });
  194. } else {
  195. requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
  196. }
  197. return false;
  198. }
  199.  
  200. /**
  201. * Callback received when a permissions request has been completed.
  202. */
  203. @Override
  204. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  205. @NonNull int[] grantResults) {
  206. if (requestCode == REQUEST_READ_CONTACTS) {
  207. if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  208. populateAutoComplete();
  209. }
  210. }
  211. }
  212.  
  213. public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
  214. return new CursorLoader(this,
  215. // Retrieve data rows for the device user's 'profile' contact.
  216. Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
  217. ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION,
  218.  
  219. // Select only email addresses.
  220. ContactsContract.Contacts.Data.MIMETYPE +
  221. " = ?", new String[]{ContactsContract.CommonDataKinds.Email
  222. .CONTENT_ITEM_TYPE},
  223.  
  224. // Show primary email addresses first. Note that there won't be
  225. // a primary email address if the user hasn't specified one.
  226. ContactsContract.Contacts.Data.IS_PRIMARY + " DESC");
  227. }
  228.  
  229. public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
  230. List<String> emails = new ArrayList<>();
  231. cursor.moveToFirst();
  232. while (!cursor.isAfterLast()) {
  233. emails.add(cursor.getString(ProfileQuery.ADDRESS));
  234. cursor.moveToNext();
  235. }
  236.  
  237. addEmailsToAutoComplete(emails);
  238. }
  239.  
  240. public void onLoaderReset(Loader<Cursor> cursorLoader) {
  241.  
  242. }
  243.  
  244. private void addEmailsToAutoComplete(List<String> emailAddressCollection) {
  245. //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list.
  246. ArrayAdapter<String> adapter =
  247. new ArrayAdapter<>(LoginActivity.this,
  248. android.R.layout.simple_dropdown_item_1line, emailAddressCollection);
  249.  
  250. inputEmail.setAdapter(adapter);
  251. }
  252.  
  253.  
  254. private interface ProfileQuery {
  255. String[] PROJECTION = {
  256. ContactsContract.CommonDataKinds.Email.ADDRESS,
  257. ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
  258. };
  259.  
  260. int ADDRESS = 0;
  261. int IS_PRIMARY = 1;
  262. }
  263.  
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement