|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.fxa.activities; |
|
6 |
|
7 import java.util.concurrent.Executor; |
|
8 import java.util.concurrent.Executors; |
|
9 |
|
10 import org.mozilla.gecko.R; |
|
11 import org.mozilla.gecko.background.common.log.Logger; |
|
12 import org.mozilla.gecko.background.fxa.FxAccountClient; |
|
13 import org.mozilla.gecko.background.fxa.FxAccountClient10.RequestDelegate; |
|
14 import org.mozilla.gecko.background.fxa.FxAccountClient20; |
|
15 import org.mozilla.gecko.background.fxa.FxAccountClient20.LoginResponse; |
|
16 import org.mozilla.gecko.background.fxa.FxAccountClientException.FxAccountClientRemoteException; |
|
17 import org.mozilla.gecko.background.fxa.PasswordStretcher; |
|
18 import org.mozilla.gecko.fxa.FxAccountConstants; |
|
19 import org.mozilla.gecko.fxa.activities.FxAccountSetupTask.FxAccountSignInTask; |
|
20 import org.mozilla.gecko.sync.setup.activities.ActivityUtils; |
|
21 |
|
22 import android.content.Intent; |
|
23 import android.os.Bundle; |
|
24 import android.view.View; |
|
25 import android.view.View.OnClickListener; |
|
26 import android.widget.AutoCompleteTextView; |
|
27 import android.widget.Button; |
|
28 import android.widget.EditText; |
|
29 import android.widget.ProgressBar; |
|
30 import android.widget.TextView; |
|
31 |
|
32 /** |
|
33 * Activity which displays sign in screen to the user. |
|
34 */ |
|
35 public class FxAccountSignInActivity extends FxAccountAbstractSetupActivity { |
|
36 protected static final String LOG_TAG = FxAccountSignInActivity.class.getSimpleName(); |
|
37 |
|
38 private static final int CHILD_REQUEST_CODE = 3; |
|
39 |
|
40 /** |
|
41 * {@inheritDoc} |
|
42 */ |
|
43 @Override |
|
44 public void onCreate(Bundle icicle) { |
|
45 Logger.debug(LOG_TAG, "onCreate(" + icicle + ")"); |
|
46 |
|
47 super.onCreate(icicle); |
|
48 setContentView(R.layout.fxaccount_sign_in); |
|
49 |
|
50 emailEdit = (AutoCompleteTextView) ensureFindViewById(null, R.id.email, "email edit"); |
|
51 passwordEdit = (EditText) ensureFindViewById(null, R.id.password, "password edit"); |
|
52 showPasswordButton = (Button) ensureFindViewById(null, R.id.show_password, "show password button"); |
|
53 remoteErrorTextView = (TextView) ensureFindViewById(null, R.id.remote_error, "remote error text view"); |
|
54 button = (Button) ensureFindViewById(null, R.id.button, "sign in button"); |
|
55 progressBar = (ProgressBar) ensureFindViewById(null, R.id.progress, "progress bar"); |
|
56 |
|
57 minimumPasswordLength = 1; // Minimal restriction on passwords entered to sign in. |
|
58 createSignInButton(); |
|
59 addListeners(); |
|
60 updateButtonState(); |
|
61 createShowPasswordButton(); |
|
62 linkifyPolicy(); |
|
63 |
|
64 View createAccountInsteadLink = ensureFindViewById(null, R.id.create_account_link, "create account instead link"); |
|
65 createAccountInsteadLink.setOnClickListener(new OnClickListener() { |
|
66 @Override |
|
67 public void onClick(View v) { |
|
68 Intent intent = new Intent(FxAccountSignInActivity.this, FxAccountCreateAccountActivity.class); |
|
69 intent.putExtra("email", emailEdit.getText().toString()); |
|
70 intent.putExtra("password", passwordEdit.getText().toString()); |
|
71 // Per http://stackoverflow.com/a/8992365, this triggers a known bug with |
|
72 // the soft keyboard not being shown for the started activity. Why, Android, why? |
|
73 intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); |
|
74 startActivityForResult(intent, CHILD_REQUEST_CODE); |
|
75 } |
|
76 }); |
|
77 |
|
78 // Only set email/password in onCreate; we don't want to overwrite edited values onResume. |
|
79 if (getIntent() != null && getIntent().getExtras() != null) { |
|
80 Bundle bundle = getIntent().getExtras(); |
|
81 emailEdit.setText(bundle.getString("email")); |
|
82 passwordEdit.setText(bundle.getString("password")); |
|
83 } |
|
84 |
|
85 TextView view = (TextView) findViewById(R.id.forgot_password_link); |
|
86 ActivityUtils.linkTextView(view, R.string.fxaccount_sign_in_forgot_password, R.string.fxaccount_link_forgot_password); |
|
87 } |
|
88 |
|
89 /** |
|
90 * We might have switched to the CreateAccount activity; if that activity |
|
91 * succeeds, feed its result back to the authenticator. |
|
92 */ |
|
93 @Override |
|
94 public void onActivityResult(int requestCode, int resultCode, Intent data) { |
|
95 Logger.debug(LOG_TAG, "onActivityResult: " + requestCode); |
|
96 if (requestCode != CHILD_REQUEST_CODE || resultCode != RESULT_OK) { |
|
97 super.onActivityResult(requestCode, resultCode, data); |
|
98 return; |
|
99 } |
|
100 this.setResult(resultCode, data); |
|
101 this.finish(); |
|
102 } |
|
103 |
|
104 public void signIn(String email, String password) { |
|
105 String serverURI = FxAccountConstants.DEFAULT_AUTH_SERVER_ENDPOINT; |
|
106 PasswordStretcher passwordStretcher = makePasswordStretcher(password); |
|
107 // This delegate creates a new Android account on success, opens the |
|
108 // appropriate "success!" activity, and finishes this activity. |
|
109 RequestDelegate<LoginResponse> delegate = new AddAccountDelegate(email, passwordStretcher, serverURI) { |
|
110 @Override |
|
111 public void handleError(Exception e) { |
|
112 showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); |
|
113 } |
|
114 |
|
115 @Override |
|
116 public void handleFailure(FxAccountClientRemoteException e) { |
|
117 showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); |
|
118 } |
|
119 }; |
|
120 |
|
121 Executor executor = Executors.newSingleThreadExecutor(); |
|
122 FxAccountClient client = new FxAccountClient20(serverURI, executor); |
|
123 try { |
|
124 hideRemoteError(); |
|
125 new FxAccountSignInTask(this, this, email, passwordStretcher, client, delegate).execute(); |
|
126 } catch (Exception e) { |
|
127 showRemoteError(e, R.string.fxaccount_sign_in_unknown_error); |
|
128 } |
|
129 } |
|
130 |
|
131 protected void createSignInButton() { |
|
132 button.setOnClickListener(new OnClickListener() { |
|
133 @Override |
|
134 public void onClick(View v) { |
|
135 final String email = emailEdit.getText().toString(); |
|
136 final String password = passwordEdit.getText().toString(); |
|
137 signIn(email, password); |
|
138 } |
|
139 }); |
|
140 } |
|
141 } |