Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stephel.sadix.main;
- import java.io.UnsupportedEncodingException;
- import java.net.URISyntaxException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.support.v4.app.FragmentActivity;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.EditText;
- import android.widget.ImageButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.stephel.sadix.R;
- import com.stephel.sadix.constants.ActivityType;
- import com.stephel.sadix.constants.AppSettings;
- import com.stephel.sadix.constants.Url;
- import com.stephel.sadix.dao.LogActivity;
- import com.stephel.sadix.dao.LogActivityDao;
- import com.stephel.sadix.main.dialogs.AlertDialogFragment;
- import com.stephel.sadix.utils.AsyncHttpRequest;
- import com.stephel.sadix.utils.AsyncTaskCompleteListener;
- public class LoginActivity extends FragmentActivity implements AsyncTaskCompleteListener<String>, DialogInterface.OnClickListener {
- private EditText txtPassword;
- private EditText txtUserName;
- private TextView txtVersion;
- private ImageButton btnLogin;
- private Drawable loginBtnClicked;
- private Context context;
- private App app;
- private static final String logTag = LoginActivity.class.toString();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- //initialize components
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- context = this;
- txtVersion = ((TextView) findViewById(R.id.txtVersion));
- txtUserName = ((EditText) findViewById(R.id.txtUserName));
- txtPassword = ((EditText) findViewById(R.id.txtPassword));
- //set-up view
- String version = "0";
- try {
- version = context.getPackageManager().getPackageInfo(
- context.getPackageName(), 0).versionName;
- txtVersion.setText(version);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
- loginBtnClicked = context.getResources().getDrawable(R.drawable.login_btn_clicked);
- btnLogin = ((ImageButton) findViewById(R.id.btnLogin));
- initListener();
- }
- @Override
- public void onResume(){
- super.onResume();
- //TODO check if the device time synched with the server
- //TODO after sync, check if he's logged in
- initSession();
- }
- private void initSession() {
- app = (App)getApplicationContext();
- if(app.getSession().isLoggedIn()){
- launchMainActivity();
- }
- }
- private void launchMainActivity(){
- Intent mainMenuIntent = new Intent(this.getApplicationContext(),
- HomeActivity.class);
- startActivity(mainMenuIntent);
- }
- private void initListener(){
- btnLogin.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- btnLogin.setBackgroundDrawable(loginBtnClicked);
- CharSequence userName = txtUserName.getText();
- CharSequence password = txtPassword.getText();
- //TODO use GSON and dto packages!
- JSONObject jsonLogin = new JSONObject();
- try {
- jsonLogin.put("UserName", userName);
- jsonLogin.put("Password", password);
- jsonLogin.put("SC", AppSettings.SILENT_CODE);
- jsonLogin.put("AppVersion", AppSettings.APP_VERSION);
- String request = jsonLogin.toString();
- AsyncHttpRequest req = new AsyncHttpRequest(context,LoginActivity.this,AsyncHttpRequest.PostMethod,Url.API_POST_LOGIN,request);
- req.ContentTypeHeader = AsyncHttpRequest.ContentTypeJSON;
- req.AcceptHeader=AsyncHttpRequest.ContentTypeJSON;
- //TODO make execute compatible with HONEYCOMB above to maintain concurrent call to API
- req.execute();
- } catch (JSONException je) {
- Log.e(logTag,"Error Creating Json : "+je.getMessage());
- } catch (URISyntaxException e) {
- Log.e(logTag,"Invalid URI : "+e.getMessage());
- } catch (UnsupportedEncodingException e) {
- Log.e(logTag,"Encoding unsupported : "+e.getMessage());
- }
- }
- });
- }
- @SuppressLint("SimpleDateFormat")
- @Override
- public void onTaskComplete(String result, int Tag) {
- try {
- if(result!=null){
- JSONObject json= new JSONObject(result);
- JSONObject jsonSadixException = json.getJSONObject("SadixException");
- String sdxResult = jsonSadixException.getString("Result");
- if(sdxResult.equals("OK")) {
- JSONObject jsonSales = json.getJSONObject("Sales");
- String salesFullName = jsonSales.get("FullName").toString();
- int salesId = jsonSales.getInt("SalesId");
- String serverDateTime = jsonSales.getString("ServerDateTime");
- Log.i(logTag,"Successfully Logging in! Sales Name : "+salesFullName);
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
- String currentDateandTime = sdf.format(date);
- app.getSession().setLoggedIn(true, currentDateandTime, String.valueOf(salesId),
- salesFullName,serverDateTime);
- //check if date of device is synched with server to HH level (hence 13 chars taken into account
- //date part (10 chars) + 'T' (1) + hour part (2))
- if(!currentDateandTime.substring(0, 13).equals(serverDateTime.substring(0,13))){
- AlertDialogFragment syncDialog = AlertDialogFragment.getInstance(
- getString(R.string.dialog_title_sync_server_datetime),
- getString(R.string.dialog_message_sync_server_datetime)+serverDateTime
- );
- syncDialog.show(this.getSupportFragmentManager(),"sync_confirm");
- } else {
- //log to database
- LogActivityDao activityDao = app.getDaoSession().getLogActivityDao();
- LogActivity activity = new LogActivity(null,ActivityType.LOGIN,"",date);
- activityDao.insert(activity);
- Log.d(logTag,"Login activity logged : "+activity.getId());
- //go to main menu
- launchMainActivity();
- }
- } else {
- String sdxResultMsg = jsonSadixException.getString("Message");
- Log.w(logTag,"failed Logging in! "+sdxResultMsg);
- Toast.makeText(this, "Login Failed : " + sdxResult, Toast.LENGTH_LONG).show();
- }
- }
- } catch (JSONException e) {
- Log.e(logTag,"Failed logging in : "+e.getMessage());
- }
- }
- //Handle sync time server dialog
- @Override
- public void onClick(DialogInterface dialog, int whichButton) {
- switch (whichButton){
- case DialogInterface.BUTTON_POSITIVE:
- dialog.dismiss();
- try {
- startActivity(new Intent(
- android.provider.Settings.ACTION_DATE_SETTINGS));
- } catch (Exception e) {
- e.printStackTrace();
- }
- break;
- case DialogInterface.BUTTON_NEGATIVE:
- dialog.dismiss();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement