package com.usai.apex; //import android.app.Fragment; import com.usai.util.Crypto; import com.usai.util.Network; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; //import android.app.Fragment; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager.NameNotFoundException; import android.os.AsyncTask; import android.os.Build; //import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.util.Log; import android.view.Gravity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; //import android.widget.TextView; import android.widget.Toast; /** * Activity which displays a login screen to the user, offering registration as * well. */ public class LoginFragment extends Fragment/* implements OnClickListener */ { private String m_sUser; private String m_sPassword; private EditText m_etName; private EditText m_etPassword; private UserLoginTask mAuthTask = null; private TextView mLoginStatusMessageView; private View mLoginFormView; private View mLoginStatusView; private CheckBox m_cbSave; // SQLiteDatabase m_db; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_login, null); TextView tv_ver = (TextView) view.findViewById(R.id.tv_ver); try { tv_ver.setText("ver:"+ApexTrackingApplication.get_instance().getPackageManager().getPackageInfo( "com.usai.apex", 0).versionName); } catch (NameNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // Button btn = (Button) view.findViewById(R.id.sign_in_button); // btn.setOnClickListener(this); // m_cbSave.setOnCheckedChangeListener(new OnCheckedChangeListener() // { // // @Override // public void onCheckedChanged(CompoundButton buttonView, // boolean isChecked) // { // if (isChecked) // { // String user,password; // SharedPreferences RunOnce = ApexTrackingApplication // .get_instance().getSharedPreferences("Apex", 0); // SharedPreferences.Editor editor = RunOnce.edit(); // editor.putString("user", user); // editor.putString("password", user); // } // String vername; // try // { // vername = ApexTrackingApplication.get_instance() // .getPackageManager() // .getPackageInfo("com.usai.apex", 0).versionName; // boolean bFirstRun = RunOnce.getBoolean("FirstRun" + vername // + "_result", true); // if (bFirstRun) // { // SharedPreferences.Editor editor = RunOnce.edit(); // editor.putBoolean("FirstRun" + vername + "_result", // false); // // Don't forget to commit your edits!!! // editor.commit(); // Intent intent = new Intent(); // intent.setClass(this, HelpActivity.class); // intent.putExtra("caller", "result"); // startActivity(intent); // // } // } // catch (NameNotFoundException e) // { // // TODO Auto-generated catch block // e.printStackTrace(); // } // // } // // }); // Set up the login form. // mUser = getIntent().getStringExtra(EXTRA_EMAIL); m_etName = (EditText) view.findViewById(R.id.user); // mUserView.setText(mUser); m_etPassword = (EditText) view.findViewById(R.id.password); m_cbSave = (CheckBox) view.findViewById(R.id.cb_save); SharedPreferences pref = ApexTrackingApplication.get_instance() .getSharedPreferences("Apex", 0); String u = pref.getString("user", null); String p = pref.getString("password", null); if (u != null && p != null) { try { m_etName.setText(Crypto.decrypt("apexu", u)); m_etPassword.setText(Crypto.decrypt("apexp", p)); m_cbSave.setChecked(true); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } m_etPassword .setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { if (id == R.id.login || id == EditorInfo.IME_ACTION_DONE) { InputMethodManager inputMethodManager = (InputMethodManager) getActivity() .getApplicationContext().getSystemService( Context.INPUT_METHOD_SERVICE); // EditText editText = // (EditText)findViewById(R.id.xxxx); inputMethodManager.hideSoftInputFromWindow( m_etPassword.getWindowToken(), 0); // ���� attemptLogin(); return true; } return false; } }); mLoginFormView = view.findViewById(R.id.login_form); mLoginStatusView = view.findViewById(R.id.login_status); mLoginStatusMessageView = (TextView) view .findViewById(R.id.login_status_message); view.findViewById(R.id.sign_in_button).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { InputMethodManager inputMethodManager = (InputMethodManager) getActivity() .getApplicationContext().getSystemService( Context.INPUT_METHOD_SERVICE); // EditText editText = // (EditText)findViewById(R.id.xxxx); inputMethodManager.hideSoftInputFromWindow( m_etPassword.getWindowToken(), 0); attemptLogin(); // showProgress(true); } }); view.findViewById(R.id.tv_retrieve_pass).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setClass(getActivity(), RetrievePasswordActivity.class); startActivity(intent); // showProgress(true); } }); return view; // return super.onCreateView(inflater, container, savedInstanceState); } // /** // * Attempts to sign in or register the account specified by the login // form. // * If there are form errors (invalid email, missing fields, etc.), the // * errors are presented and no actual login attempt is made. // */ public void attemptLogin() { if (mAuthTask != null) { return; } // Reset errors. m_etName.setError(null); m_etPassword.setError(null); // Store values at the time of the login attempt. m_sUser = m_etName.getText().toString(); m_sPassword = m_etPassword.getText().toString(); boolean cancel = false; View focusView = null; // Check for a valid password. if (TextUtils.isEmpty(m_sPassword)) { m_etPassword.setError(getString(R.string.error_field_required)); focusView = m_etPassword; cancel = true; } else if (m_sPassword.length() < 4) { m_etPassword.setError(getString(R.string.error_invalid_password)); focusView = m_etPassword; cancel = true; } // Check for a valid user name. if (TextUtils.isEmpty(m_sUser)) { m_etName.setError(getString(R.string.error_field_required)); focusView = m_etName; cancel = true; } // else if (!m_sName.contains("@")) { // m_etName.setError(getString(R.string.error_invalid_email)); // focusView = m_etName; // cancel = true; // } if (cancel) { // There was an error; don't attempt login and focus the first // form field with an error. focusView.requestFocus(); } else { // Show a progress spinner, and kick off a background task to // perform the user login attempt. mLoginStatusMessageView.setText(R.string.login_progress_signing_in); showProgress(true); mAuthTask = new UserLoginTask(); mAuthTask.execute((Void) null); } } // // /** // * Shows the progress UI and hides the login form. // */ // @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) private void showProgress(final boolean show) { // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow // for very easy animations. If available, use these APIs to fade-in // the progress spinner. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { int shortAnimTime = getResources().getInteger( android.R.integer.config_shortAnimTime); mLoginStatusView.setVisibility(View.VISIBLE); mLoginStatusView.animate().setDuration(shortAnimTime) .alpha(show ? 1 : 0) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mLoginStatusView.setVisibility(show ? View.VISIBLE : View.INVISIBLE); } }); mLoginFormView.setVisibility(View.VISIBLE); mLoginFormView.animate().setDuration(shortAnimTime) .alpha(show ? 0 : 1) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mLoginFormView.setVisibility(show ? View.INVISIBLE : View.VISIBLE); } }); } else { // The ViewPropertyAnimator APIs are not available, so simply show // and hide the relevant UI components. mLoginStatusView .setVisibility(show ? View.VISIBLE : View.INVISIBLE); mLoginFormView.setVisibility(show ? View.INVISIBLE : View.VISIBLE); } } // // /** // * Represents an asynchronous login/registration task used to authenticate // * the user. // */ public class UserLoginTask extends AsyncTask { // int err_code = ERR_CODE_NONE; int errorcode; @Override protected Boolean doInBackground(Void... params) { errorcode = Network.get_Auth(m_sUser, m_sPassword); if (errorcode == Network.RESULT_TRUE) return true; else { return false; } } @Override protected void onPostExecute(final Boolean success) { Log.i("onPostExecute", "entry"); mAuthTask = null; showProgress(false); // if (netconnect == ) // { // // } if (success) { // SharedPreferences.Editor editor = RunOnce.edit(); // editor.putBoolean("FirstRun"+globalUtil.getVerName(this), // false); // // Don't forget to commit your edits!!! // editor.commit(); SharedPreferences pref = ApexTrackingApplication.get_instance() .getSharedPreferences("Apex", 0); SharedPreferences.Editor editor = pref.edit(); try { if (m_cbSave.isChecked()) { editor.putString("user", Crypto.encrypt("apexu", m_sUser)); editor.putString("password", Crypto.encrypt("apexp", m_sPassword)); } else { editor.putString("user", null); editor.putString("password", null); } } catch (Exception e) { editor.putString("user", null); editor.putString("password", null); e.printStackTrace(); } editor.commit(); Intent intent = new Intent(); intent.setClass(getActivity(), FunctionSelectActivity.class); // intent.putExtra("user", m_sUser); // intent.putExtra("password", m_sPassword); startActivity(intent); getActivity().finish(); } else { switch (errorcode) { case Network.RESULT_NET_NOTAVAILABLE: { Toast toast = Toast.makeText(getActivity() .getApplicationContext(), getText(R.string.msg_connection_none), Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return; } case Network.RESULT_NET_ERROR: { Toast toast = Toast.makeText(getActivity() .getApplicationContext(), getText(R.string.msg_net_error), Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return; } case Network.RESULT_VER_LOW: { Toast toast = Toast.makeText(getActivity() .getApplicationContext(), getText(R.string.msg_ver_low), Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return; } case Network.RESULT_ERROR: // case Network.RESULT_RESPONSE_NULL: { Toast toast = Toast.makeText(getActivity() .getApplicationContext(), getText(R.string.msg_net_resulterror), Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return; } case Network.RESULT_FALSE: { m_etPassword .setError(getString(R.string.error_incorrect_password)); m_etPassword.requestFocus(); } default: { Toast toast = Toast.makeText(getActivity() .getApplicationContext(), "An error occur on server code:" + errorcode, Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); break; } } } } @Override protected void onCancelled() { mAuthTask = null; showProgress(false); } } }