package com.tistory.link2me.addresschart;
import android.annotation.TargetApi; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.ContentValues; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.provider.Settings.Secure; import android.support.annotation.NonNull; import android.support.v4.util.Pair; import android.telephony.TelephonyManager; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast;
import com.google.firebase.iid.FirebaseInstanceId;
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.List; import java.util.Map;
public class Login extends Activity {
String getDeviceID; // 스마트기기의 장치 고유값 EditText etId; EditText etPw;
String loginID; String loginPW; CheckBox autologin; Boolean loginChecked; public SharedPreferences settings;
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login);
// 네트워크 연결상태 체크 if(NetworkConnection() == false){ NotConnected_showAlert(); }
etId = (EditText) findViewById(R.id.login_id_edit); etPw = (EditText) findViewById(R.id.login_pw_edit); autologin = (CheckBox) findViewById(R.id.autologinchk);
settings = getSharedPreferences("settings", Activity.MODE_PRIVATE); loginChecked = settings.getBoolean("LoginChecked", false); if (loginChecked) { etId.setText(settings.getString("loginID", "")); etPw.setText(settings.getString("loginPW", "")); autologin.setChecked(true); }
if(!settings.getString("loginID", "").equals("")) etPw.requestFocus();
Button submit = (Button) findViewById(R.id.login_btn); submit.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View view) { loginID = etId.getText().toString().trim(); loginPW = etPw.getText().toString().trim();
if(loginID != null && !loginID.isEmpty() && loginPW != null && !loginPW.isEmpty()){ new AsyncLogin().execute(loginID,loginPW); } } });
}
private class AsyncLogin extends AsyncTask<String, Void, String> { ProgressDialog pdLoading = new ProgressDialog(Login.this); HttpURLConnection conn;
@Override protected void onPreExecute() { super.onPreExecute(); //this method will be running on UI thread pdLoading.setMessage("\tValidating user..."); pdLoading.setCancelable(false); pdLoading.show(); }
@Override protected String doInBackground(String... params) {
try { URL url = new URL(Value.IPADDRESS + "/loginChk.php"); conn = (HttpURLConnection) url.openConnection(); // 세션 쿠키 전달 String cookieString = CookieManager.getInstance().getCookie(Value.IPADDRESS);
// 단말기의 ID 정보를 얻기 위해서는 READ_PHONE_STATE 권한이 필요 TelephonyManager mTelephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if (mTelephony.getDeviceId() != null){ getDeviceID = mTelephony.getDeviceId(); // 스마트폰 기기 정보 } else { getDeviceID = Secure.getString(getApplicationContext().getContentResolver(), Secure.ANDROID_ID); }
// 사용자 기기의 고유 토큰 정보를 획득 String getToken = FirebaseInstanceId.getInstance().getToken();
System.out.println("userID : " + loginID); System.out.println("Input PW : " + loginPW); System.out.println("DeviceID : " + getDeviceID); System.out.println("Login Token : " + getToken);
if(conn != null){ // 연결되었으면 conn.setConnectTimeout(10000); // milliseconds 연결 타임아웃시간
//add request header conn.setRequestMethod("POST"); conn.setRequestProperty("USER-AGENT", "Mozilla/5.0"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); if (cookieString != null) { conn.setRequestProperty("Cookie", cookieString); Log.e("PHP_getCookie", cookieString); }
conn.setUseCaches(false); conn.setDefaultUseCaches(false); conn.setDoOutput(true); // POST 로 데이터를 넘겨주겠다는 옵션 conn.setDoInput(true); // 서버로부터 응답 헤더와 메시지를 읽어들이겠다는 옵션
// 전달할 인자들 Uri.Builder builder = new Uri.Builder() .appendQueryParameter("loginID", params[0]) .appendQueryParameter("loginPW", params[1]) .appendQueryParameter("deviceID", getDeviceID) .appendQueryParameter("tokenID", getToken); String urlParameters = builder.build().getEncodedQuery();
DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close();
int responseCode = conn.getResponseCode(); System.out.println("GET Response Code : " + responseCode); if(responseCode == HttpURLConnection.HTTP_OK){ // 연결 코드가 리턴되면 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while((line = reader.readLine())!= null){ sb.append(line); } System.out.println("line Value : " + sb.toString().trim()); return sb.toString().trim(); }else{ return("unsuccessful"); } }
} catch(Exception e){ return new String("Exception: " + e.getMessage()); } finally { conn.disconnect(); } return null; }
protected void onPostExecute(String result){ pdLoading.dismiss(); if(result.equalsIgnoreCase("1")){ // 로그인 정보 일치 Toast.makeText(Login.this,"로그인 성공", Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplication(), MainActivity.class)); finish(); // finish()를 호출해서 Activity를 없애줌
} else if(result.equalsIgnoreCase("-1")){ // 등록된 단말기와 불일치 deviceDismatch_showAlert(); } else if (result.equalsIgnoreCase("0")) { showAlert(); } else { Toast.makeText(Login.this, "서버로부터 정보가 잘못 전송되었습니다", Toast.LENGTH_SHORT).show(); } } }
public void onStop(){ // 어플리케이션이 화면에서 사라질때 super.onStop(); // 자동 로그인이 체크되어 있고, 로그인에 성공했으면 폰에 자동로그인 정보 저장 if (autologin.isChecked()) { settings = getSharedPreferences("settings",Activity.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit();
editor.putString("loginID", loginID); editor.putString("loginPW", loginPW); editor.putBoolean("LoginChecked", true);
editor.commit(); } else { // 자동 로그인 체크가 해제되면 폰에 저장된 정보 모두 삭제 settings = getSharedPreferences("settings", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.clear(); // 모든 정보 삭제 editor.commit(); }
}
public void deviceDismatch_showAlert(){ AlertDialog.Builder builder = new AlertDialog.Builder(Login.this); builder.setTitle("등록단말 불일치"); builder.setMessage("최초 등록된 단말기가 아닙니다.\n" + "관리자에게 문의하여 단말기 변경신청을 하시기 바랍니다.") .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }
public void showAlert(){ AlertDialog.Builder builder = new AlertDialog.Builder(Login.this); builder.setTitle("로그인 에러"); builder.setMessage("로그인 정보가 일치하지 않습니다.") .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); AlertDialog alert = builder.create(); alert.show(); }
private void NotConnected_showAlert() { AlertDialog.Builder builder = new AlertDialog.Builder(Login.this); builder.setTitle("네트워크 연결 오류"); builder.setMessage("사용 가능한 무선네트워크가 없습니다.\n" + "먼저 무선네트워크 연결상태를 확인해 주세요.") .setCancelable(false) .setPositiveButton("확인", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { finish(); // exit //application 프로세스를 강제 종료 android.os.Process.killProcess(android.os.Process.myPid() ); } }); AlertDialog alert = builder.create(); alert.show();
}
private boolean NetworkConnection() { int[] networkTypes = {ConnectivityManager.TYPE_MOBILE, ConnectivityManager.TYPE_WIFI}; try { ConnectivityManager manager = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE); for (int networkType : networkTypes) { NetworkInfo activeNetwork = manager.getActiveNetworkInfo(); if(activeNetwork != null && activeNetwork.getType() == networkType){ return true; } } } catch (Exception e) { return false; } return false; }
// Back 버튼을 누르면 어플 종료여부 확인 처리 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if( keyCode == KeyEvent.KEYCODE_BACK ) { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Quit").setMessage("어플을 종료하시겠습니까?").setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { moveTaskToBack(true); // 본Activity finish후 다른 Activity가 뜨는 걸 방지. finish(); //application 프로세스를 강제 종료 android.os.Process.killProcess(android.os.Process.myPid() ); } }).setNegativeButton( "No", null ).show();
return true; }
return super.onKeyDown(keyCode, event); }
@Override protected void onResume() { super.onResume(); if (android.os.Build.VERSION.SDK_INT >= 21) flushCookies(); else CookieSyncManager.getInstance().startSync(); }
@Override protected void onPause() { super.onPause(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //noinspection deprecation CookieSyncManager.getInstance().stopSync(); // 동기화 종료 } }
@TargetApi(21) private void flushCookies() { // 롤리팝 이상에서는 CookieManager의 flush를 하도록 변경됨. CookieManager.getInstance().flush(); } }
|