/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.hiimeloyun.android.core;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import com.hiimeloyun.android.WebViewActivity;
import com.hiimeloyun.android.process.HttpAsyncTask;
import org.json.JSONObject;
/**
*
* @author boroo
*/
public abstract class AbstractActivity extends Activity {
public LayoutInflater getInflater() {
return LayoutInflater.from(this);
}
public View getRemoteView(int layoutId) {
return getInflater().inflate(layoutId, null);
}
public void Loading() {
}
public void Loaded() {
}
// -----------------------------------------------------------------------
public JSONObject taskResultRegulate(JSONObject result, HttpAsyncTask task, boolean canceled) {
if (!task.hasError() && canceled == false && result != null) {
try {
if (result.optBoolean("status")) {
Object data = App.util.getJsonData(result);
if (data instanceof String) {
showMessage(data.toString());
} else {
JSONObject jsonData = (JSONObject) data;
return jsonData;
}
} else {
String error = App.util.getJsonError(result);
if (result.optBoolean("html")) {
viewHtml(error);
} else {
showMessage(error);
}
}
} catch (Exception ex) {
showException(ex);
}
} else {
Exception ex = task.getError();
showException(ex);
}
return null;
}
public void viewHtml(String html) {
Intent i = new Intent(this, WebViewActivity.class);
Bundle b = new Bundle();
b.putString("html", html);
i.putExtras(b);
startActivity(i);
}
public void showException(Exception ex) {
if (ex != null) {
App.notice.Toast("System Error ::: " + ex.getMessage(), true);
} else {
App.notice.Toast("System Error!");
}
}
public void showError(String error) {
App.notice.Toast(error, true);
}
public void showMessage(String message) {
App.notice.Toast(message);
}
// -----------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//App.setCurrentActivity(this);
}
@Override
protected void onResume() {
super.onResume();
App.setCurrentActivity(this);
}
@Override
protected void onPause() {
//clearReferences();
super.onPause();
}
@Override
protected void onDestroy() {
clearReferences();
super.onDestroy();
}
// -----------------------------------------------------------------------
private void clearReferences() {
Activity currActivity = App.getCurrentActivity();
if (currActivity != null && currActivity.equals(this)) {
App.setCurrentActivity(null);
}
}
}
MainActivity
package com.hiimeloyun.android;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import com.hiimeloyun.android.core.AbstractActivity;
import com.hiimeloyun.android.core.App;
import com.hiimeloyun.android.core.Constants.HttpMethod;
import com.hiimeloyun.android.core.ParamArray;
import com.hiimeloyun.android.process.*;
import org.json.JSONObject;
public class MainActivity extends AbstractActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initEvents();
}
// -----------------------------------------------------------------------
private void initEvents() {
CheckBox rememberme_checkbox = (CheckBox) findViewById(R.id.chk_rememberme);
rememberme_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton cb, boolean bln) {
}
});
Button buttonLogin = (Button) findViewById(R.id.btn_login);
buttonLogin.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
login();
}
});
String rememberMe = App.config.getItem("rememberMe", "storage");
String rememberPassword = App.config.getItem("rememberPassword", "storage");
CheckBox chk_rememberme = (CheckBox) findViewById(R.id.chk_rememberme);
chk_rememberme.setChecked(App.storage.getBoolean(rememberMe));
EditText txt_password = (EditText) findViewById(R.id.txt_password);
txt_password.setText(App.storage.getString(rememberPassword));
if (chk_rememberme.isChecked()) {
login();
}
}
private void login() {
try {
CheckBox chk_rememberme = (CheckBox) findViewById(R.id.chk_rememberme);
boolean remember = chk_rememberme.isChecked();
EditText txt_password = (EditText) findViewById(R.id.txt_password);
String password = txt_password.getText().toString();
if (password.length() < 6) {
showMessage("Таны нууц үг хамгийн багадаа 6 тэмдэгт байх ёстой!");
return;
}
if (password.length() > 20) {
showMessage("Таны нууц үг хамгийн ихдээ 20 тэмдэгт байх ёстой!");
return;
}
String rememberMe = App.config.getItem("rememberMe", "storage");
String rememberPassword = App.config.getItem("rememberPassword", "storage");
App.storage.editor().putBoolean(rememberMe, remember)
.putString(rememberPassword, remember ? password : "")
.commit();
ParamArray params = new ParamArray();
params.add("device", App.device.getDeviceId());
params.add("serial", App.device.getSimSerial());
params.add("line1", App.device.getPhoneNumber());
params.add("username", App.device.getPhoneNumber());
params.add("password", password);
HttpAsyncTask task = new HttpAsyncTask(HttpMethod.POST);
task.setAction("auth/login");
task.setParamArray(params);
task.setListener(new HttpTaskListener() {
public void OnTaskStarted(HttpAsyncTask asyncTask) {
showMessage("Ачааллаж байна...");
}
public void OnTaskCompleted(JSONObject result, HttpAsyncTask asyncTask, boolean canceled) {
JSONObject data = taskResultRegulate(result, asyncTask, canceled);
callHomeActivity(data);
}
});
task.execute();
} catch (Exception ex) {
showException(ex);
}
}
private void callHomeActivity(JSONObject jsonData) {
try {
if (jsonData == null) {
return;
}
String loggedUser = App.config.getItem("loggedUser", "bag");
App.bag.setData(loggedUser, jsonData);
String user_id = jsonData.getString("id");
HttpAsyncTask task = new HttpAsyncTask(HttpMethod.POST);
ParamArray params = new ParamArray();
params.add("email", "boroo_test@facebook.com");
task.setAction("user/update/" + user_id);
task.setParamArray(params);
task.setListener(new HttpTaskListener() {
public void OnTaskStarted(HttpAsyncTask asyncTask) {
showMessage("Засварлаж байна...");
}
public void OnTaskCompleted(JSONObject result, HttpAsyncTask asyncTask, boolean canceled) {
JSONObject data = taskResultRegulate(result, asyncTask, canceled);
showMessage("Таны И-Мэйл хаяг '" + data.optString("email") + "' болон өөрчлөгдлөө!");
}
});
task.execute();
} catch (Exception ex) {
showException(ex);
}
}
}
No comments:
Post a Comment