Nextrek
@ 748
Nextrek / Android / SmartCharging / SmartCharging / app / src / main / java / it / fedeloper / smartcharging / Controller / LoginActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
package it.fedeloper.smartcharging.Controller; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.*; import com.facebook.*; import com.facebook.login.LoginManager; import com.facebook.login.LoginResult; import it.fedeloper.smartcharging.Manager.PrefsManager; import it.fedeloper.smartcharging.Manager.Utils; import it.fedeloper.smartcharging.Model.FacebookData; import it.fedeloper.smartcharging.R; import it.fedeloper.smartcharging.Service.CallService; import it.fedeloper.smartcharging.Service.EndPoint; import it.fedeloper.smartcharging.Service.HttpClient; import org.json.JSONException; import org.json.JSONObject; import java.util.Arrays; public class LoginActivity extends FragmentActivity { private static final String LOG = "LOGIN_ACTIVITY"; //FB private LoginManager loginManager; private CallbackManager callbackManager; private static Context c; public void ignoraclick(View v) { Intent i = new Intent(LoginActivity.this, SelectionActivity.class); startActivity(i); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(it.fedeloper.smartcharging.R.layout.activity_login2); Button facebookLoginButton = (Button) findViewById(R.id.facebook_login_button); facebookLoginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doFacebookLogin(); } }); inizializeFbLogin(); c = getApplicationContext(); /* TextView btn=(TextView) findViewById(R.id.textView3); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub localeclick(v); } }); List<Integer> ids = new ArrayList<Integer>(); List<Double> lats = new ArrayList<Double>(); List<Double> lons = new ArrayList<Double>(); List<String> nomi = new ArrayList<String>(); List<Integer> tipologie = new ArrayList<Integer>(); ids.add(124);ids.add(3444);ids.add(33334); lats.add(124.23);lats.add(3444.233331);lats.add(33334.1111); lons.add(91.23);lons.add(94.233331);lons.add(94.1111); nomi.add("bar");nomi.add("pizza");nomi.add("abbigliamento"); tipologie.add(3); tipologie.add(5); tipologie.add(9); JSONObject obj = new JSONObject(); try { obj.put("ids", ids); obj.put("lats", lats); obj.put("lons", lons); obj.put("nomi", nomi); obj.put("tipologie", tipologie); } catch (JSONException e) { e.printStackTrace(); } // System.out.print(obj); // ((EditText) findViewById(R.id.editText)).setText(obj.toString()); */ } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); } /** * Facebook Inizialization * First Method to call */ private void inizializeFbLogin(){ FacebookSdk.sdkInitialize(this); callbackManager = CallbackManager.Factory.create(); //Add callback LoginManager.getInstance().registerCallback(callbackManager, new FbLoginNativeCallback()); loginManager = LoginManager.getInstance(); } /** * Callback of the login */ private class FbLoginNativeCallback implements FacebookCallback<LoginResult> { @Override public void onSuccess(final LoginResult loginResult) { //Request Additional Data GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object, GraphResponse response) { try { FacebookData facebookData = new FacebookData(); facebookData.setUserId(object.getString("id")); facebookData.setToken(loginResult.getAccessToken().getToken()); facebookData.setEmail(object.getString("email")); //Make the call sendFacebookLoginDataToServer(facebookData); } catch (JSONException e) { Toast.makeText(LoginActivity.this, R.string.connection_error, Toast.LENGTH_LONG).show(); e.printStackTrace(); } } }); Bundle parameters = new Bundle(); parameters.putString("fields", "id,email,name"); request.setParameters(parameters); request.executeAsync(); } @Override public void onCancel() { Toast.makeText(LoginActivity.this, R.string.errore_msg, Toast.LENGTH_LONG).show(); } @Override public void onError(FacebookException exception) { Toast.makeText(LoginActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show(); } } /** * Do the facebook login * -- Login to facebook * -- On the callback send data to server */ private void doFacebookLogin(){ if(!Utils.isConnectionAvailable(this)){ Toast.makeText(this, R.string.connection_error, Toast.LENGTH_LONG).show(); return; } loginManager.logInWithReadPermissions(this, Arrays.asList("public_profile", "email")); } /** * Get the response and make the call to the server * @param facebookData data */ private void sendFacebookLoginDataToServer(FacebookData facebookData) throws JSONException { Log.d(LOG, "Calling server"); //Send the data to the server final JSONObject jsonObj = new JSONObject(); jsonObj.put("fb_user", facebookData.getUserId()); jsonObj.put("user_email", facebookData.getEmail()); jsonObj.put("fb_token", facebookData.getToken()); CallService callService = new CallService(this, new CallService.ResponseCallback() { @Override public void onSuccess(JSONObject response) { successfulFacebookLogin(response); } @Override public void onError(int responseCode, String errorMsg) { Toast.makeText(LoginActivity.this, R.string.errore_msg, Toast.LENGTH_LONG).show(); } }); callService.makeCall(jsonObj, EndPoint.FACEBOOK_LOGIN); } private void successfulFacebookLogin(JSONObject response){ try { PrefsManager.setLogin(c, response.getString("username"), response.getString("token"), Integer.parseInt(response.getString("id")), response.getString("token")); Intent i = new Intent(this, SelectionActivity.class); startActivity(i); finish(); } catch (JSONException e) { e.printStackTrace(); } Toast.makeText(LoginActivity.this, "LOGIN CON FACEBOOK EFFETTUATO", Toast.LENGTH_LONG).show(); } public void loginclick(View v) { final String user = ((EditText) findViewById(R.id.editText2)).getText().toString(); final String pass = ((EditText) findViewById(R.id.editText3)).getText().toString(); if(user.equals("") || pass.equals("")){ //Toast.makeText(LoginActivity.this, R.string.login_error_field_empty, Toast.LENGTH_SHORT).show(); return; } final JSONObject obj = new JSONObject(); try { obj.put("username", user); obj.put("password", pass); new Thread(new Runnable() { public void run() { JSONObject resp = HttpClient.SendHttpPostAsync(EndPoint.MAIN_END_POINT + "/login.php", obj); try { int code = resp.getInt("return"); int id = resp.getInt("id"); String token = resp.getString("token"); if (code==0) { loginSuccesfully(user, pass, id, token); } else if (code==1) { loginFailed(getString(R.string.userpass_wrong)); } else { loginFailed ("Server Error"); } if (resp.getInt("is_owner")==0) PrefsManager.setUserType(c); else PrefsManager.setLocaleType(c); } catch (Exception e) { Log.e("errore",e.getMessage()); } } }).start(); } catch (JSONException e) { e.printStackTrace(); } } private void loginSuccesfully(String username, String password, int id, String token) { PrefsManager.setLogin(c,username, password, id, token); Intent i = new Intent(LoginActivity.this, SelectionActivity.class); startActivity(i); finish(); } private void loginFailed(final String error) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(LoginActivity.this, error, Toast.LENGTH_SHORT).show(); // AlertDialog.Builder dlgAlert = new AlertDialog.Builder(LoginActivity.this); // dlgAlert.setMessage(error); // dlgAlert.setTitle(getString(R.string.errore_msg)); // dlgAlert.setPositiveButton("OK", null); // dlgAlert.setCancelable(true); // dlgAlert.setPositiveButton("Ok", // new DialogInterface.OnClickListener() { // public void onClick(DialogInterface dialog, int which) { // //dismiss the dialog // } // }); // dlgAlert.create().show(); }}); } public void btnregister(View v) { Intent i = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(i); } } |
Commits for Nextrek/Android/SmartCharging/SmartCharging/app/src/main/java/it/fedeloper/smartcharging/Controller/LoginActivity.java
Revision | Author | Commited | Message |
---|---|---|---|
748 Diff | FFontana | Wed 16 Sep, 2015 15:31:34 +0000 | |
599 Diff | MStefanelli | Fri 28 Aug, 2015 07:16:44 +0000 | |
559 Diff | FFontana | Tue 25 Aug, 2015 13:14:51 +0000 | |
558 Diff | MStefanelli | Tue 25 Aug, 2015 13:10:38 +0000 | me |
548 Diff | FFontana | Tue 25 Aug, 2015 10:17:34 +0000 | |
545 Diff | MStefanelli | Tue 25 Aug, 2015 09:39:56 +0000 | me |
535 Diff | MStefanelli | Mon 24 Aug, 2015 13:31:12 +0000 | WE |
525 Diff | FFontana | Sat 22 Aug, 2015 11:08:25 +0000 | |
524 Diff | FFontana | Sat 22 Aug, 2015 10:47:42 +0000 | |
522 | MStefanelli | Fri 21 Aug, 2015 16:02:26 +0000 |