Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.wbapps.lenin.lenin;
- import android.content.Context;
- import android.content.res.Resources;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.os.AsyncTask;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.EditText;
- import android.widget.ScrollView;
- import android.widget.TableLayout;
- import android.widget.TableRow;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.net.Socket;
- import java.io.*;
- public class MainActivity extends ActionBarActivity {
- //StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- private TableLayout table;
- Resources rs = null;
- Socket sc = null;
- DataOutputStream os = null;
- boolean socket_conectado = false;
- boolean layout_usuarios = false;
- boolean layout_chat = false;
- String user = "";
- String pass = "";
- SQLiteDatabase db = null;
- DataInputStream is = null;
- String last_socket_query = "";
- String chat_msg = "";
- String player_list = "";
- String label_nickname = "";
- private class ListenSocketTask extends AsyncTask<Void, Void, Void> {
- String line;
- protected Void doInBackground(Void... param) {
- try {
- line = is.readLine();
- } catch (IOException e) {
- }
- return null;
- }
- protected void onPostExecute(Void param) {
- new ListenSocketTask().execute((Void[]) null);
- //Toast toast = Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG);
- //toast.show();
- OnSocketMessage(line);
- }
- }
- private class SocketSendQueryThread extends AsyncTask<Void, Void, Void> {
- protected Void doInBackground(Void... param) {
- try {
- os.writeBytes(last_socket_query);
- os.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- protected void onPostExecute(Void param) {
- }
- }
- private class SocketConnect extends AsyncTask<Void, Void, Void> {
- protected Void doInBackground(Void... param) {
- try {
- sc = new Socket("redzoneserver.com", 2033);
- os = new DataOutputStream(sc.getOutputStream());
- is = new DataInputStream(sc.getInputStream());
- socket_conectado = true;
- new ListenSocketTask().execute((Void[]) null);
- } catch (Exception e) {
- }
- return null;
- }
- protected void onPostExecute(Void param) {
- if(!socket_conectado) {
- setContentView(R.layout.internet_error);
- return;
- }
- UsuariosSQLiteHelper app_db = new UsuariosSQLiteHelper(com.wbapps.lenin.lenin.MainActivity.this, "database");
- db = app_db.getWritableDatabase();
- Cursor result = db.rawQuery("SELECT USER,PASS FROM credenciales", null);
- if(result.moveToFirst()) {
- user = result.getString(0);
- pass = result.getString(1);
- setContentView(R.layout.spiner);
- SocketSendQuery("login\1" + user + "\1" + pass);
- }
- else {
- setContentView(R.layout.login);
- }
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- //StrictMode.setThreadPolicy(policy);
- super.onCreate(savedInstanceState);
- new SocketConnect().execute((Void[]) null);
- }
- public void clickbtnReconectar(View v) {
- setContentView(R.layout.spiner);
- new SocketConnect().execute((Void[]) null);
- }
- public void clickbtnPlayers(View v) {
- setContentView(R.layout.players);
- layout_usuarios = true;
- layout_chat = false;
- SocketSendQuery("players\1");
- UpdatePlayerList();
- }
- public void clickbtnChat(View v) {
- setContentView(R.layout.chat);
- layout_usuarios = false;
- layout_chat = true;
- TextView obj = (TextView) findViewById(R.id.lbSocket);
- obj.setText(chat_msg);
- }
- public void clickbtnSetLogin(View v) {
- EditText obj = (EditText) findViewById(R.id.txtNick);
- user = obj.getText().toString();
- obj.setText("");
- obj = (EditText) findViewById(R.id.txtPass);
- pass = obj.getText().toString();
- obj.setText("");
- if(user.length() < 1 || pass.length() < 1) {
- Toast.makeText(getBaseContext(), "Rellene todo los campos!", Toast.LENGTH_SHORT).show();
- return;
- }
- db.execSQL("DELETE FROM credenciales");
- db.execSQL("INSERT INTO credenciales (USER,PASS) VALUES ('" + user + "','" + pass + "')");
- InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
- setContentView(R.layout.spiner);
- SocketSendQuery("login\1" + user + "\1" + pass);
- }
- public void SocketSendQuery(String socket_query) {
- last_socket_query = socket_query;
- try {
- os.writeBytes(last_socket_query);
- } catch (IOException e) {
- e.printStackTrace();
- }
- //new SocketSendQueryThread().execute((Void[]) null);
- }
- public void OnSocketMessage(String data) {
- String[] query = data.split("\1");
- switch(query[0]) {
- //login:
- case "login": {
- int pLevel = Integer.parseInt(query[1]);
- if (pLevel > 0) {
- setContentView(R.layout.chat);
- layout_chat = true;
- }
- else {
- setContentView(R.layout.login);
- EditText obj = (EditText) findViewById(R.id.txtNick);
- obj.setText(user);
- TextView obj1 = (TextView) findViewById(R.id.lbLoginError);
- obj1.setText("Credenciales incorrectas!!");
- }
- break;
- }
- case "msg": {
- chat_msg += query[1] + ": " + query[2] + "\n";
- SetChatString(chat_msg);
- break;
- }
- case "text": {
- chat_msg += query[1] + " (ID:"+query[2]+"): "+query[3]+"\n";
- SetChatString(chat_msg);
- break;
- }
- case "rcon": {
- chat_msg += query[1]+"\n";
- SetChatString(chat_msg);
- break;
- }
- case "rconEx": {
- chat_msg += query[1]+"\n";
- SetChatString(chat_msg);
- break;
- }
- case "players": {
- player_list = data;
- if(layout_usuarios) {
- UpdatePlayerList();
- }
- }
- }
- }
- public void SetChatString(String string) {
- if(layout_chat) {
- TextView obj = (TextView) findViewById(R.id.lbSocket);
- obj.setText(string);
- ScrollView obj2 = (ScrollView) findViewById(R.id.scrollView);
- obj2.fullScroll(View.FOCUS_DOWN);
- }
- }
- public void clickbtnEnviarDato(View v) {
- SocketSendChat();
- }
- public void SocketSendChat() {
- EditText text = (EditText)findViewById(R.id.txtSocket);
- String dato = text.getText().toString();
- if(dato.length() > 0) {
- text.setText("");
- SocketSendQuery("msg\1" + dato);
- }
- }
- public void UpdatePlayerList() {
- setContentView(R.layout.players);
- String[] split = player_list.split("\1");
- int t = 0;
- int total = split.length-1;
- TableRow fila;
- TextView columna;
- //STYLE-------------------------------------------------------------------------------------
- table = (TableLayout)findViewById(R.id.table);
- TableRow.LayoutParams layoutFila = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
- TableRow.LayoutParams layoutId = new TableRow.LayoutParams(70,TableRow.LayoutParams.WRAP_CONTENT);
- TableRow.LayoutParams layoutNombre= new TableRow.LayoutParams(150,TableRow.LayoutParams.WRAP_CONTENT);
- //------------------------------------------------------------------------------------------
- fila = new TableRow(this);
- fila.setLayoutParams(layoutFila);
- //-------------------------------------
- columna = new TextView(this);
- columna.setText("ID");
- columna.setLayoutParams(layoutId);
- columna.setTextAppearance(this, R.style.etiqueta); //Style
- columna.setBackgroundResource(R.drawable.tabla_cabecera); //Style
- fila.addView(columna);
- //-------------------------------------
- columna = new TextView(this);
- columna.setText("NOMBRE");
- columna.setLayoutParams(layoutNombre);
- columna.setTextAppearance(this, R.style.etiqueta); //Style
- columna.setBackgroundResource(R.drawable.tabla_cabecera); //Style
- fila.addView(columna);
- //-------------------------------------
- columna = new TextView(this);
- columna.setText("PAIS");
- //columna.setGravity(Gravity.CENTER_HORIZONTAL);
- columna.setTextAppearance(this,R.style.etiqueta); //Style
- columna.setBackgroundResource(R.drawable.tabla_cabecera); //Style
- fila.addView(columna);
- //-------------------------------------
- table.addView(fila);
- for( int i=1; i < total; i++) {
- fila = new TableRow(this);
- //-------------------------------------
- columna = new TextView(this);
- columna.setText(split[i]);
- columna.setBackgroundResource(R.drawable.tabla_cuerpo);//Style
- fila.addView(columna);
- //-------------------------------------
- columna = new TextView(this);
- columna.setText(split[i + 2]);
- columna.setBackgroundResource(R.drawable.tabla_cuerpo);//Style
- fila.addView(columna);
- //-------------------------------------
- columna = new TextView(this);
- columna.setText(split[i+4]);
- columna.setBackgroundResource(R.drawable.tabla_cuerpo);//Style
- fila.addView(columna);
- //-------------------------------------
- table.addView(fila);
- i+=5;
- t++;
- }
- TextView text = (TextView)findViewById(R.id.lbChatPlayers);
- text.setText("JUGADORES ("+t+")");
- }
- public class UsuariosSQLiteHelper extends SQLiteOpenHelper {
- public UsuariosSQLiteHelper(Context contexto, String nombre) {
- super(contexto, nombre, null, 1);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE IF NOT EXISTS credenciales (USER TEXT,PASS TEXT)");
- //Toast.makeText(getBaseContext(), "NEW DATABASE :)", Toast.LENGTH_LONG).show();
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
- db.execSQL("DROP TABLE IF EXISTS credenciales");
- db.execSQL("CREATE TABLE IF NOT EXIST credenciales (USER TEXT,PASS TEXT)");
- }
- }
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.menuConfig) {
- return true;
- }
- if (id == R.id.menuLogout) {
- setContentView(R.layout.login);
- db.execSQL("DELETE FROM credenciales");
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- System.exit(0);
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement