Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.f4rpeople.e_reporting.activity;
- import android.app.Activity;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.android.volley.Request.Method;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import com.f4rpeople.e_reporting.MainActivity;
- import com.f4rpeople.e_reporting.R;
- import com.f4rpeople.e_reporting.app.AppConfig;
- import com.f4rpeople.e_reporting.app.AppController;
- import com.f4rpeople.e_reporting.helper.SQLiteHandler;
- import com.f4rpeople.e_reporting.helper.SessionManager;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.HashMap;
- import java.util.Map;
- public class LoginActivity extends Activity {
- private static final String TAG = RegisterActivity.class.getSimpleName();
- private Button btnLogin;
- private Button btnLinkToRegister;
- private EditText inputNamemuch;
- private EditText inputPassword;
- private ProgressDialog pDialog;
- private SessionManager session;
- private SQLiteHandler db;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- inputNamemuch = (EditText) findViewById(R.id.namemuch);
- inputPassword = (EditText) findViewById(R.id.password);
- btnLogin = (Button) findViewById(R.id.btnLogin);
- btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
- // Progress dialog
- pDialog = new ProgressDialog(this);
- pDialog.setCancelable(false);
- // SQLite database handler
- db = new SQLiteHandler(getApplicationContext());
- // Session manager
- session = new SessionManager(getApplicationContext());
- // Check if user is already logged in or not
- if (session.isLoggedIn()) {
- // User is already logged in. Take him to main activity
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- // Login button Click Event
- btnLogin.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- String namemuch = inputNamemuch.getText().toString().trim();
- String password = inputPassword.getText().toString().trim();
- // Check for empty data in the form
- if (!namemuch.isEmpty() && !password.isEmpty()) {
- // login user
- checkLogin(namemuch, password);
- } else {
- // Prompt user to enter credentials
- Toast.makeText(getApplicationContext(),
- "Please enter the credentials!", Toast.LENGTH_LONG)
- .show();
- }
- }
- });
- // Link to Register Screen
- btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- Intent i = new Intent(getApplicationContext(),
- RegisterActivity.class);
- startActivity(i);
- finish();
- }
- });
- }
- /**
- * function to verify login details in mysql db
- * */
- private void checkLogin(final String namemuch, final String password) {
- // parameter login
- String tag_string_req = "req_login";
- pDialog.setMessage("Logging in ...");
- showDialog();
- StringRequest strReq = new StringRequest(Method.POST,
- AppConfig.URL_LOGIN, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- Log.d(TAG, "Login Response: " + response.toString());
- hideDialog();
- try {
- JSONObject jObj = new JSONObject(response);
- boolean error = jObj.getBoolean("error");
- // chek ada error
- if (!error) {
- // berhasil login
- // buat login session
- session.setLogin(true);
- // store data ke SQLite
- JSONObject user = jObj.getJSONObject("user");
- String id = user.getString("id");
- String uid = jObj.getString("uid");
- String name = user.getString("name");
- String namemuch = user.getString("namemuch");
- String nama_admin = user.getString("nama_admin");
- String nama_tml = user.getString("nama_tml");
- String created_at = user.getString("created_at");
- // parameter sqlite
- db.addUser(id, uid, name, namemuch, nama_admin, nama_tml, created_at);
- // Launch ke main activity
- Intent intent = new Intent(LoginActivity.this,
- MainActivity.class);
- startActivity(intent);
- finish();
- } else {
- // Error di login.
- String errorMsg = jObj.getString("error_msg");
- Toast.makeText(getApplicationContext(),
- errorMsg, Toast.LENGTH_LONG).show();
- }
- } catch (JSONException e) {
- // JSON error
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
- }
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.e(TAG, "Login Gagal: " + error.getMessage());
- Toast.makeText(getApplicationContext(),
- error.getMessage(), Toast.LENGTH_LONG).show();
- hideDialog();
- }
- }) {
- @Override
- protected Map<String, String> getParams() {
- // Posting parameters ke login url
- Map<String, String> params = new HashMap<String, String>();
- params.put("namemuch", namemuch);
- params.put("password", password);
- return params;
- }
- };
- AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
- }
- private void showDialog() {
- if (!pDialog.isShowing())
- pDialog.show();
- }
- private void hideDialog() {
- if (pDialog.isShowing())
- pDialog.dismiss();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement