Advertisement
Lenin_Daniel

andoid app rchat

Oct 11th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.98 KB | None | 0 0
  1. package com.wbapps.lenin.lenin;
  2.  
  3. import android.content.Context;
  4. import android.content.res.Resources;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.os.AsyncTask;
  9. import android.support.v7.app.ActionBarActivity;
  10. import android.os.Bundle;
  11. import android.view.KeyEvent;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.inputmethod.InputMethodManager;
  16. import android.widget.EditText;
  17. import android.widget.ScrollView;
  18. import android.widget.TableLayout;
  19. import android.widget.TableRow;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23. import java.net.Socket;
  24. import java.io.*;
  25.  
  26.  
  27. public class MainActivity extends  ActionBarActivity {
  28.     //StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  29.     private TableLayout table;
  30.  
  31.     Resources rs                = null;
  32.     Socket sc                   = null;
  33.     DataOutputStream os         = null;
  34.     boolean socket_conectado    = false;
  35.     boolean layout_usuarios     = false;
  36.     boolean layout_chat         = false;
  37.     String user                 = "";
  38.     String pass                 = "";
  39.     SQLiteDatabase db           = null;
  40.     DataInputStream is          = null;
  41.     String last_socket_query    = "";
  42.     String chat_msg             = "";
  43.     String player_list          = "";
  44.     String label_nickname       = "";
  45.  
  46.     private class ListenSocketTask extends AsyncTask<Void, Void, Void> {
  47.         String line;
  48.         protected Void doInBackground(Void... param) {
  49.             try {
  50.                 line = is.readLine();
  51.             } catch (IOException e) {
  52.  
  53.             }
  54.             return null;
  55.         }
  56.         protected void onPostExecute(Void param) {
  57.             new ListenSocketTask().execute((Void[]) null);
  58.             //Toast toast = Toast.makeText(getApplicationContext(), line, Toast.LENGTH_LONG);
  59.             //toast.show();
  60.             OnSocketMessage(line);
  61.         }
  62.     }
  63.     private class SocketSendQueryThread extends AsyncTask<Void, Void, Void> {
  64.         protected Void doInBackground(Void... param) {
  65.             try {
  66.                 os.writeBytes(last_socket_query);
  67.                 os.flush();
  68.             } catch (IOException e) {
  69.                 e.printStackTrace();
  70.             }
  71.             return null;
  72.         }
  73.         protected void onPostExecute(Void param) {
  74.         }
  75.     }
  76.     private class SocketConnect extends AsyncTask<Void, Void, Void> {
  77.         protected Void doInBackground(Void... param) {
  78.             try {
  79.                 sc = new Socket("redzoneserver.com", 2033);
  80.                 os = new DataOutputStream(sc.getOutputStream());
  81.                 is = new DataInputStream(sc.getInputStream());
  82.                 socket_conectado = true;
  83.                 new ListenSocketTask().execute((Void[]) null);
  84.             } catch (Exception e) {
  85.  
  86.             }
  87.             return null;
  88.         }
  89.         protected void onPostExecute(Void param) {
  90.             if(!socket_conectado) {
  91.                 setContentView(R.layout.internet_error);
  92.                 return;
  93.             }
  94.             UsuariosSQLiteHelper app_db = new UsuariosSQLiteHelper(com.wbapps.lenin.lenin.MainActivity.this, "database");
  95.             db = app_db.getWritableDatabase();
  96.             Cursor result = db.rawQuery("SELECT USER,PASS FROM credenciales", null);
  97.             if(result.moveToFirst()) {
  98.                 user = result.getString(0);
  99.                 pass = result.getString(1);
  100.                 setContentView(R.layout.spiner);
  101.                 SocketSendQuery("login\1" + user + "\1" + pass);
  102.             }
  103.             else {
  104.                 setContentView(R.layout.login);
  105.             }
  106.         }
  107.     }
  108.     @Override
  109.     protected void onCreate(Bundle savedInstanceState) {
  110.         //StrictMode.setThreadPolicy(policy);
  111.         super.onCreate(savedInstanceState);
  112.         new SocketConnect().execute((Void[]) null);
  113.     }
  114.     public void clickbtnReconectar(View v) {
  115.         setContentView(R.layout.spiner);
  116.         new SocketConnect().execute((Void[]) null);
  117.     }
  118.     public void clickbtnPlayers(View v) {
  119.         setContentView(R.layout.players);
  120.         layout_usuarios = true;
  121.         layout_chat     = false;
  122.         SocketSendQuery("players\1");
  123.         UpdatePlayerList();
  124.     }
  125.     public void clickbtnChat(View v) {
  126.         setContentView(R.layout.chat);
  127.         layout_usuarios = false;
  128.         layout_chat     = true;
  129.         TextView obj = (TextView) findViewById(R.id.lbSocket);
  130.         obj.setText(chat_msg);
  131.     }
  132.     public void clickbtnSetLogin(View v) {
  133.         EditText obj = (EditText) findViewById(R.id.txtNick);
  134.         user = obj.getText().toString();
  135.         obj.setText("");
  136.         obj = (EditText) findViewById(R.id.txtPass);
  137.         pass = obj.getText().toString();
  138.         obj.setText("");
  139.         if(user.length() < 1 || pass.length() < 1) {
  140.             Toast.makeText(getBaseContext(), "Rellene todo los campos!", Toast.LENGTH_SHORT).show();
  141.             return;
  142.         }
  143.         db.execSQL("DELETE FROM credenciales");
  144.         db.execSQL("INSERT INTO credenciales (USER,PASS) VALUES ('" + user + "','" + pass + "')");
  145.         InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
  146.         imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
  147.         setContentView(R.layout.spiner);
  148.         SocketSendQuery("login\1" + user + "\1" + pass);
  149.     }
  150.     public void SocketSendQuery(String socket_query) {
  151.         last_socket_query = socket_query;
  152.         try {
  153.             os.writeBytes(last_socket_query);
  154.         } catch (IOException e) {
  155.             e.printStackTrace();
  156.         }
  157.         //new SocketSendQueryThread().execute((Void[]) null);
  158.     }
  159.     public void OnSocketMessage(String data) {
  160.         String[] query = data.split("\1");
  161.         switch(query[0]) {
  162.             //login:
  163.             case "login": {
  164.                 int pLevel = Integer.parseInt(query[1]);
  165.                 if (pLevel > 0) {
  166.                     setContentView(R.layout.chat);
  167.                     layout_chat = true;
  168.                 }
  169.                 else {
  170.                     setContentView(R.layout.login);
  171.                     EditText obj = (EditText) findViewById(R.id.txtNick);
  172.                     obj.setText(user);
  173.                     TextView obj1 = (TextView) findViewById(R.id.lbLoginError);
  174.                     obj1.setText("Credenciales incorrectas!!");
  175.                 }
  176.                 break;
  177.             }
  178.             case "msg": {
  179.                 chat_msg += query[1] + ": " + query[2] + "\n";
  180.                 SetChatString(chat_msg);
  181.                 break;
  182.             }
  183.             case "text": {
  184.                 chat_msg += query[1] + " (ID:"+query[2]+"): "+query[3]+"\n";
  185.                 SetChatString(chat_msg);
  186.                 break;
  187.             }
  188.             case "rcon": {
  189.                 chat_msg += query[1]+"\n";
  190.                 SetChatString(chat_msg);
  191.                 break;
  192.             }
  193.             case "rconEx": {
  194.                 chat_msg += query[1]+"\n";
  195.                 SetChatString(chat_msg);
  196.                 break;
  197.             }
  198.             case "players": {
  199.                 player_list = data;
  200.                 if(layout_usuarios) {
  201.                     UpdatePlayerList();
  202.                 }
  203.             }
  204.         }
  205.     }
  206.     public void SetChatString(String string) {
  207.         if(layout_chat) {
  208.             TextView obj = (TextView) findViewById(R.id.lbSocket);
  209.             obj.setText(string);
  210.             ScrollView obj2 = (ScrollView) findViewById(R.id.scrollView);
  211.             obj2.fullScroll(View.FOCUS_DOWN);
  212.         }
  213.     }
  214.     public void clickbtnEnviarDato(View v) {
  215.         SocketSendChat();
  216.     }
  217.     public void SocketSendChat() {
  218.         EditText text = (EditText)findViewById(R.id.txtSocket);
  219.         String dato = text.getText().toString();
  220.         if(dato.length() > 0) {
  221.             text.setText("");
  222.             SocketSendQuery("msg\1" + dato);
  223.         }
  224.     }
  225.     public void UpdatePlayerList() {
  226.         setContentView(R.layout.players);
  227.         String[] split = player_list.split("\1");
  228.         int t = 0;
  229.         int total = split.length-1;
  230.  
  231.         TableRow fila;
  232.         TextView columna;
  233.         //STYLE-------------------------------------------------------------------------------------
  234.         table = (TableLayout)findViewById(R.id.table);
  235.         TableRow.LayoutParams layoutFila  = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
  236.         TableRow.LayoutParams layoutId    = new TableRow.LayoutParams(70,TableRow.LayoutParams.WRAP_CONTENT);
  237.         TableRow.LayoutParams layoutNombre= new TableRow.LayoutParams(150,TableRow.LayoutParams.WRAP_CONTENT);
  238.         //------------------------------------------------------------------------------------------
  239.  
  240.         fila = new TableRow(this);
  241.         fila.setLayoutParams(layoutFila);
  242.         //-------------------------------------
  243.         columna = new TextView(this);
  244.         columna.setText("ID");
  245.         columna.setLayoutParams(layoutId);
  246.         columna.setTextAppearance(this, R.style.etiqueta);          //Style
  247.         columna.setBackgroundResource(R.drawable.tabla_cabecera);   //Style
  248.         fila.addView(columna);
  249.         //-------------------------------------
  250.         columna = new TextView(this);
  251.         columna.setText("NOMBRE");
  252.         columna.setLayoutParams(layoutNombre);
  253.         columna.setTextAppearance(this, R.style.etiqueta);          //Style
  254.         columna.setBackgroundResource(R.drawable.tabla_cabecera);   //Style
  255.         fila.addView(columna);
  256.         //-------------------------------------
  257.         columna = new TextView(this);
  258.         columna.setText("PAIS");
  259.         //columna.setGravity(Gravity.CENTER_HORIZONTAL);
  260.         columna.setTextAppearance(this,R.style.etiqueta);           //Style
  261.         columna.setBackgroundResource(R.drawable.tabla_cabecera);   //Style
  262.         fila.addView(columna);
  263.         //-------------------------------------
  264.         table.addView(fila);
  265.  
  266.         for( int i=1; i < total; i++) {
  267.             fila = new TableRow(this);
  268.             //-------------------------------------
  269.             columna = new TextView(this);
  270.             columna.setText(split[i]);
  271.             columna.setBackgroundResource(R.drawable.tabla_cuerpo);//Style
  272.             fila.addView(columna);
  273.             //-------------------------------------
  274.             columna = new TextView(this);
  275.             columna.setText(split[i + 2]);
  276.             columna.setBackgroundResource(R.drawable.tabla_cuerpo);//Style
  277.             fila.addView(columna);
  278.             //-------------------------------------
  279.             columna = new TextView(this);
  280.             columna.setText(split[i+4]);
  281.             columna.setBackgroundResource(R.drawable.tabla_cuerpo);//Style
  282.             fila.addView(columna);
  283.             //-------------------------------------
  284.             table.addView(fila);
  285.             i+=5;
  286.             t++;
  287.         }
  288.         TextView text = (TextView)findViewById(R.id.lbChatPlayers);
  289.         text.setText("JUGADORES ("+t+")");
  290.     }
  291.     public class UsuariosSQLiteHelper extends SQLiteOpenHelper {
  292.         public UsuariosSQLiteHelper(Context contexto, String nombre) {
  293.             super(contexto, nombre, null, 1);
  294.         }
  295.         @Override
  296.         public void onCreate(SQLiteDatabase db) {
  297.             db.execSQL("CREATE TABLE IF NOT EXISTS credenciales (USER TEXT,PASS TEXT)");
  298.             //Toast.makeText(getBaseContext(), "NEW DATABASE :)", Toast.LENGTH_LONG).show();
  299.         }
  300.         @Override
  301.         public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
  302.             db.execSQL("DROP TABLE IF EXISTS credenciales");
  303.             db.execSQL("CREATE TABLE IF NOT EXIST credenciales (USER TEXT,PASS TEXT)");
  304.         }
  305.     }
  306.  
  307.     public boolean onCreateOptionsMenu(Menu menu) {
  308.         // Inflate the menu; this adds items to the action bar if it is present.
  309.         getMenuInflater().inflate(R.menu.menu_main, menu);
  310.         return true;
  311.     }
  312.  
  313.     @Override
  314.     public boolean onOptionsItemSelected(MenuItem item) {
  315.         // Handle action bar item clicks here. The action bar will
  316.         // automatically handle clicks on the Home/Up button, so long
  317.         // as you specify a parent activity in AndroidManifest.xml.
  318.         int id = item.getItemId();
  319.  
  320.         //noinspection SimplifiableIfStatement
  321.         if (id == R.id.menuConfig) {
  322.             return true;
  323.         }
  324.         if (id == R.id.menuLogout) {
  325.             setContentView(R.layout.login);
  326.             db.execSQL("DELETE FROM credenciales");
  327.             return true;
  328.         }
  329.         return super.onOptionsItemSelected(item);
  330.     }
  331.     public boolean onKeyDown(int keyCode, KeyEvent event) {
  332.         if (keyCode == KeyEvent.KEYCODE_BACK) {
  333.             System.exit(0);
  334.         }
  335.         return true;
  336.     }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement