/[caddi]/CaddiPictureUpload/trunk/app/src/main/java/com/caddi/android/caddipictureupload/LoginActivity.java
ViewVC logotype

Contents of /CaddiPictureUpload/trunk/app/src/main/java/com/caddi/android/caddipictureupload/LoginActivity.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download)
Mon Jul 27 06:41:43 2015 UTC (8 years, 10 months ago) by torben
File size: 8052 byte(s)
First import
1 package com.caddi.android.caddipictureupload;
2
3 import android.animation.Animator;
4 import android.animation.AnimatorListenerAdapter;
5 import android.annotation.TargetApi;
6 import android.app.Activity;
7 import android.app.LoaderManager.LoaderCallbacks;
8 import android.content.ContentResolver;
9 import android.content.CursorLoader;
10 import android.content.Intent;
11 import android.content.Loader;
12 import android.database.Cursor;
13 import android.net.Uri;
14 import android.os.AsyncTask;
15 import android.os.Build.VERSION;
16 import android.os.Build;
17 import android.os.Bundle;
18 import android.provider.ContactsContract;
19 import android.text.TextUtils;
20 import android.util.Log;
21 import android.view.KeyEvent;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.view.inputmethod.EditorInfo;
25 import android.widget.ArrayAdapter;
26 import android.widget.AutoCompleteTextView;
27 import android.widget.Button;
28 import android.widget.EditText;
29 import android.widget.TextView;
30
31 import com.caddi.android.caddipictureupload.util.MyTransformation;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36
37 /**
38 * A login screen that offers login via email/password.
39 */
40 public class LoginActivity extends Activity {
41
42 public final static boolean DEBUG = true;
43
44 /**
45 * Keep track of the login task to ensure we can cancel it if requested.
46 */
47 private UserLoginTask mAuthTask = null;
48
49 // UI references.
50 private AutoCompleteTextView mEmailView;
51 private EditText mCaddiView;
52 private EditText mUserView;
53 private EditText mPasswordView;
54 private View mProgressView;
55 private View mLoginFormView;
56
57 @Override
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60 setContentView(R.layout.activity_login);
61
62
63 mCaddiView = (EditText) findViewById( R.id.login_caddiID);
64 mUserView = (EditText) findViewById( R.id.login_username);
65
66 mPasswordView = (EditText) findViewById(R.id.password);
67 mPasswordView.setTransformationMethod( new MyTransformation() );
68
69
70 Button loginButton = (Button) findViewById(R.id.email_sign_in_button);
71 loginButton.setOnClickListener(new OnClickListener() {
72 @Override
73 public void onClick(View view) {
74 attemptLogin();
75 }
76 });
77
78 mLoginFormView = findViewById(R.id.login_form);
79 mProgressView = findViewById(R.id.login_progress);
80
81
82 if (DEBUG) {
83 mCaddiView.setText("fysklik");
84 mUserView.setText("thoerup");
85 mPasswordView.setText("pwhoerup");
86 }
87 }
88
89
90
91 /**
92 * Attempts to sign in or register the account specified by the login form.
93 * If there are form errors (invalid email, missing fields, etc.), the
94 * errors are presented and no actual login attempt is made.
95 */
96 public void attemptLogin() {
97 if (mAuthTask != null) {
98 return;
99 }
100
101 // Reset errors.
102 mCaddiView.setError(null);
103 mUserView.setError(null);
104 mPasswordView.setError(null);
105
106
107 // Store values at the time of the login attempt.
108 String caddiID = mCaddiView.getText().toString();
109 String user = mUserView.getText().toString();
110 String password = mPasswordView.getText().toString();
111
112 boolean cancel = false;
113 View focusView = null;
114
115 if (TextUtils.isEmpty(caddiID) ) {
116 mCaddiView.setError( "Du skal angive Caddi ID" );
117 focusView = mCaddiView;
118 cancel = true;
119 }
120
121 if (TextUtils.isEmpty(user) ) {
122 mUserView.setError( "Du skal angive brugernavn" );
123 focusView = mUserView;
124 cancel = true;
125 }
126
127 // Check for a valid password, if the user entered one.
128 if (TextUtils.isEmpty(password) ) {
129 mPasswordView.setError("Du skal angive password");
130 focusView = mPasswordView;
131 cancel = true;
132 }
133
134
135 if (cancel) {
136 // There was an error; don't attempt login and focus the first
137 // form field with an error.
138 focusView.requestFocus();
139 } else {
140 // Show a progress spinner, and kick off a background task to
141 // perform the user login attempt.
142 showProgress(true);
143 mAuthTask = new UserLoginTask(caddiID, user, password);
144 mAuthTask.execute((Void) null);
145 }
146 }
147
148
149 private boolean isPasswordValid(String password) {
150 //TODO: Replace this with your own logic
151 return password.length() >= 4;
152 }
153
154 /**
155 * Shows the progress UI and hides the login form.
156 */
157 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
158 public void showProgress(final boolean show) {
159 // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
160 // for very easy animations. If available, use these APIs to fade-in
161 // the progress spinner.
162 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
163 int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
164
165 mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
166 mLoginFormView.animate().setDuration(shortAnimTime).alpha(
167 show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
168 @Override
169 public void onAnimationEnd(Animator animation) {
170 mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
171 }
172 });
173
174 mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
175 mProgressView.animate().setDuration(shortAnimTime).alpha(
176 show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
177 @Override
178 public void onAnimationEnd(Animator animation) {
179 mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
180 }
181 });
182 } else {
183 // The ViewPropertyAnimator APIs are not available, so simply show
184 // and hide the relevant UI components.
185 mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
186 mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
187 }
188 }
189
190 @Override
191 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
192 super.onActivityResult(requestCode, resultCode, data);
193 }
194
195 /**
196 * Represents an asynchronous login/registration task used to authenticate
197 * the user.
198 */
199 public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
200
201 private final String mCaddiID;
202 private final String mUsername;
203 private final String mPassword;
204
205 private String mError;
206
207 UserLoginTask(String caddiID, String username, String password) {
208 mCaddiID = caddiID;
209 mUsername = username;
210 mPassword = password;
211 }
212
213 @Override
214 protected Boolean doInBackground(Void... params) {
215 // TODO: attempt authentication against a network service.
216
217 try {
218 return WebserviceWrapper.login(mCaddiID, mUsername, mPassword);
219 } catch (Exception e) {
220 mError = e.getMessage();
221 return false;
222 }
223
224 }
225
226 @Override
227 protected void onPostExecute(final Boolean success) {
228 mAuthTask = null;
229 showProgress(false);
230
231 if (success) {
232
233 mPasswordView.setText("");//Clear password before launching menu
234
235 Intent intent = new Intent(LoginActivity.this, MenuActivity.class);
236 startActivity(intent);
237
238 } else {
239 mPasswordView.setError(mError);
240 mPasswordView.requestFocus();
241 }
242 }
243
244 @Override
245 protected void onCancelled() {
246 mAuthTask = null;
247 showProgress(false);
248 }
249 }
250 }

  ViewVC Help
Powered by ViewVC 1.1.20