David 10 лет назад
Родитель
Сommit
316c9b587f

+ 5 - 1
app/src/main/AndroidManifest.xml

@@ -17,7 +17,11 @@
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
-        </activity>
+        </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
+     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
+        <meta-data
+            android:name="com.google.android.gms.version"
+            android:value="@integer/google_play_services_version" />
     </application>
 
 </manifest>

+ 142 - 41
app/src/main/java/com/example/david/myapplication/MainActivity.java

@@ -1,21 +1,29 @@
 package com.example.david.myapplication;
 
 import android.content.pm.ActivityInfo;
+import android.net.Uri;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
+import android.text.Layout;
+import android.text.method.ScrollingMovementMethod;
 import android.util.Log;
 import android.view.GestureDetector;
 import android.view.GestureDetector.OnGestureListener;
 import android.view.MotionEvent;
+import android.view.View;
 import android.view.ViewGroup;
 import android.view.Window;
 import android.view.WindowManager;
+import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Toast;
 import android.widget.ViewSwitcher;
 
+import com.google.android.gms.appindexing.Action;
+import com.google.android.gms.appindexing.AppIndex;
+import com.google.android.gms.common.api.GoogleApiClient;
 import com.neovisionaries.ws.client.WebSocket;
 import com.neovisionaries.ws.client.WebSocketAdapter;
 import com.neovisionaries.ws.client.WebSocketException;
@@ -25,6 +33,8 @@ import com.neovisionaries.ws.client.WebSocketListener;
 import com.squareup.picasso.Picasso;
 
 
+import org.json.JSONObject;
+
 import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
@@ -37,45 +47,108 @@ import io.vov.vitamio.LibsChecker;
 import io.vov.vitamio.MediaPlayer;
 import io.vov.vitamio.widget.VideoView;
 
-public class MainActivity extends AppCompatActivity implements OnGestureListener{
+public class MainActivity extends AppCompatActivity implements OnGestureListener {
     private static final String TAG = "MainActivity";
     private static final String path = "rtmp://plataforma.especificosba.com.ar:1935/src/movie";
     private ViewSwitcher viewSwitcher;
     private VideoView mVideoView;
     private GestureDetector myDetector;
     private ViewGroup.LayoutParams lparams;
-    private float x1,x2;
+    private TextView chat;
+    private float x1, x2;
     private int MIN_DISTANCE = 100;
     private WebSocket ws;
+    /**
+     * ATTENTION: This was auto-generated to implement the App Indexing API.
+     * See https://g.co/AppIndexing/AndroidStudio for more information.
+     */
+    private GoogleApiClient client;
+
+    @Override
+    public void onStart() {
+        super.onStart();
+
+        // ATTENTION: This was auto-generated to implement the App Indexing API.
+        // See https://g.co/AppIndexing/AndroidStudio for more information.
+        client.connect();
+        Action viewAction = Action.newAction(
+                Action.TYPE_VIEW, // TODO: choose an action type.
+                "Main Page", // TODO: Define a title for the content shown.
+                // TODO: If you have web page content that matches this app activity's content,
+                // make sure this auto-generated web page URL is correct.
+                // Otherwise, set the URL to null.
+                Uri.parse("http://host/path"),
+                // TODO: Make sure this auto-generated app URL is correct.
+                Uri.parse("android-app://com.example.david.myapplication/http/host/path")
+        );
+        AppIndex.AppIndexApi.start(client, viewAction);
+    }
+
+    @Override
+    public void onStop() {
+        super.onStop();
+
+        // ATTENTION: This was auto-generated to implement the App Indexing API.
+        // See https://g.co/AppIndexing/AndroidStudio for more information.
+        Action viewAction = Action.newAction(
+                Action.TYPE_VIEW, // TODO: choose an action type.
+                "Main Page", // TODO: Define a title for the content shown.
+                // TODO: If you have web page content that matches this app activity's content,
+                // make sure this auto-generated web page URL is correct.
+                // Otherwise, set the URL to null.
+                Uri.parse("http://host/path"),
+                // TODO: Make sure this auto-generated app URL is correct.
+                Uri.parse("android-app://com.example.david.myapplication/http/host/path")
+        );
+        AppIndex.AppIndexApi.end(client, viewAction);
+        client.disconnect();
+    }
+
+    private enum WS_STATUS {
+        CONNECTED,
+        DISCONNECTED
+    }
+
+    ;
+    private WS_STATUS status = WS_STATUS.DISCONNECTED;
 
-        @Override
+    @Override
     protected void onCreate(Bundle savedInstanceState) {
         //requestWindowFeature(Window.FEATURE_NO_TITLE);
-        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
+        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
         super.onCreate(savedInstanceState);
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
-        if (!LibsChecker.checkVitamioLibs(this))
+        if (!LibsChecker.checkVitamioLibs(this)) {
             return;
+        }
         setContentView(R.layout.activity_main);
         connectWebSocket();
 
+        Button b = (Button) findViewById(R.id.button);
+        b.setOnClickListener(new Button.OnClickListener(){
+            @Override
+            public void onClick(View b){
+                sendMessage();
+            }
+        });
         mVideoView = (VideoView) findViewById(R.id.vitamio_videoView);
         viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
-
-        lparams=mVideoView.getLayoutParams();
+        chat = (TextView) findViewById(R.id.chat);
+        lparams = mVideoView.getLayoutParams();
         //lparams.width=mVideoView.getWidth();
-        lparams.width=200;
+        lparams.width = 200;
         mVideoView.setLayoutParams(lparams);
-
+        chat.setMovementMethod(new ScrollingMovementMethod());
+        chat.append("\n");
         myDetector = new GestureDetector(this, this);
 
-        Picasso.with(this.getApplicationContext()).load("https://plataforma.especificosba.com.ar/diapo/EBA/1.png").into((ImageView)findViewById(R.id.imageView));
+        Picasso.with(this.getApplicationContext()).load("https://plataforma.especificosba.com.ar/diapo/EBA/1.png").into((ImageView) findViewById(R.id.imageView));
         mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
             @Override
             public void onPrepared(MediaPlayer mediaPlayer) {
                 mediaPlayer.setPlaybackSpeed(1.0f);
-              //  mediaPlayer.start();
+                //  mediaPlayer.start();
 
 
             }
@@ -86,6 +159,9 @@ public class MainActivity extends AppCompatActivity implements OnGestureListener
         mVideoView.start();
 
 
+        // ATTENTION: This was auto-generated to implement the App Indexing API.
+        // See https://g.co/AppIndexing/AndroidStudio for more information.
+        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
     }
 
     private void connectWebSocket() {
@@ -103,37 +179,67 @@ public class MainActivity extends AppCompatActivity implements OnGestureListener
         ws.addListener(new WebSocketAdapter() {
             @Override
             public void onTextMessage(WebSocket websocket, String message) throws Exception {
-                // Received a text message.
-                TextView textView = (TextView)findViewById(R.id.chat);
-                textView.setText(textView.getText() + "\n" + message);
+                //Log.i("Websocket", "msg:" + message);
+                JSONObject obj = new JSONObject(message);
+                switch (obj.getString("type")) {
+                    case "chat":
+
+                        String formattedMessage = String.format("\n[HORA] %s: %s", obj.getString("user"), obj.getString("text"));
+                        chat.append(formattedMessage);
+                        final Layout layout = chat.getLayout();
+                        if(layout != null){
+                            int scrollDelta = layout.getLineBottom(chat.getLineCount() - 1)
+                                    - chat.getScrollY() - chat.getHeight();
+                            Log.i("Websocket", Integer.toString(scrollDelta));
+                            if(scrollDelta > 0)
+                                chat.scrollBy(0, scrollDelta);
+                        }
+                        chat.invalidate();
+                        break;
+                    default:
+                        break;
+                }
+                ;
             }
-            @Override
-            public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception{
-                Log.i("Websocket", "connecterd");
 
-            }
             @Override
-            public void onDisconnected(WebSocket websocket,WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame,
-                                       boolean closedByServer){
-                Log.i("Websocket", "discon");
+            public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception {
+                Log.i("Websocket", "Connected");
+                status = WS_STATUS.CONNECTED;
+                chat.append("\nConectado al sistema");
+            }
 
+            @Override
+            public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame,
+                                       boolean closedByServer) {
+                Log.i("Websocket", "Disconnected");
+                status = WS_STATUS.DISCONNECTED;
+                websocket.connectAsynchronously();
             }
+
             @Override
-            public void onConnectError(WebSocket websocket, WebSocketException cause){
-                Log.i("Websocket", "didnt even connect"+ cause.getMessage() );
+            public void onConnectError(WebSocket websocket, WebSocketException cause) {
+                Log.i("Websocket", "Didn't connect" + cause.getMessage());
+                status = WS_STATUS.DISCONNECTED;
             }
-        });
-        ws.connectAsynchronously();
+        }).connectAsynchronously();
     }
+
     public void sendMessage() {
-        EditText editText = (EditText)findViewById(R.id.send_text);
-        ws.sendText(editText.getText().toString());
+        EditText editText = (EditText) findViewById(R.id.send_text);
+        String text=editText.getText().toString();
+        if(text.length() < 3) return;
+
+        String formatted=String.format("{ \"type\":\"chat\", \"user\": \"%s %s (%s)\", \"text\":\"%s\" }", "nombre","Apellido","pais", text);
+        Log.i("Websocket", "sending" + formatted);
+
+        ws.sendText(formatted);
         editText.setText("");
     }
+
     @Override
     public boolean onTouchEvent(MotionEvent event) {
-        switch(event.getAction())
-        {
+        switch (event.getAction()) {
             case MotionEvent.ACTION_DOWN:
                 x1 = event.getX();
                 break;
@@ -141,29 +247,24 @@ public class MainActivity extends AppCompatActivity implements OnGestureListener
                 x2 = event.getX();
                 float deltaX = x2 - x1;
 
-                if (Math.abs(deltaX) > MIN_DISTANCE)
-                {
+                if (Math.abs(deltaX) > MIN_DISTANCE) {
                     // Left to Right swipe action
-                    if (x2 > x1)
-                    {
-                        if(viewSwitcher.getCurrentView() != findViewById(R.id.llayout)) { //animation: OK
+                    if (x2 > x1) {
+                        if (viewSwitcher.getCurrentView() != findViewById(R.id.llayout)) { //animation: OK
                             viewSwitcher.showNext();
                             Toast.makeText(this, "Left to Right swipe [Next]", Toast.LENGTH_SHORT).show();
                         }
                     }
 
                     // Right to left swipe action
-                    else
-                    {
-                        if(viewSwitcher.getCurrentView() != findViewById(R.id.secondLayout)) { //animation:notok
+                    else {
+                        if (viewSwitcher.getCurrentView() != findViewById(R.id.secondLayout)) { //animation:notok
                             viewSwitcher.showPrevious();
                             Toast.makeText(this, "Right to Left swipe [Previous]", Toast.LENGTH_SHORT).show();
                         }
                     }
 
-                }
-                else
-                {
+                } else {
                     // consider as something else - a screen tap for example
                 }
                 break;
@@ -173,7 +274,7 @@ public class MainActivity extends AppCompatActivity implements OnGestureListener
 
     @Override
     public boolean onDown(MotionEvent motionEvent) {
-       return false;
+        return false;
     }
 
     @Override

+ 29 - 12
app/src/main/res/layout/activity_main.xml

@@ -23,32 +23,49 @@
             <LinearLayout
                 android:layout_width="0dp"
                 android:layout_height="match_parent"
-                android:layout_weight=".50">
+                android:layout_weight="1">
                 <io.vov.vitamio.widget.VideoView
                     android:layout_width="match_parent"
-                    android:layout_height="wrap_content"
+                    android:layout_height="match_parent"
                     android:id="@+id/vitamio_videoView"/>
             </LinearLayout>
             <ImageView
+                android:layout_height="match_parent"
                 android:layout_width="0dp"
-                android:layout_height="wrap_content"
-                android:layout_weight=".50"
+                android:layout_weight="1"
                 android:id="@+id/imageView" />
         </LinearLayout>
         <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="match_parent"
+            android:orientation="vertical"
             android:id="@+id/secondLayout">
             <TextView
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:text="chattext 2"
+                android:layout_width="match_parent"
+                android:layout_height="0dp"
+                android:layout_weight="4"
+                android:scrollbars = "vertical"
                 android:id="@+id/chat" />
-            <EditText
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:text="intext"
-                android:id="@+id/send_text" />
+            <LinearLayout
+                android:layout_width="match_parent"
+                android:layout_height="0dp"
+                android:layout_weight="1"
+                android:orientation="horizontal"
+                android:id="@+id/bottomLayout">
+                <EditText
+                    android:layout_height="match_parent"
+                    android:layout_weight="4"
+                    android:layout_width="0dp"
+                    android:id="@+id/send_text" />
+                <Button
+                    style="?android:attr/buttonStyleSmall"
+                    android:layout_width="0dp"
+                    android:layout_weight="1"
+                    android:layout_height="match_parent"
+                    android:text="Enviar"
+                    android:id="@+id/button"
+                    android:layout_gravity="center_vertical" />
+            </LinearLayout>
         </LinearLayout>
 
     </ViewSwitcher>

+ 1 - 1
app/src/test/java/com/example/david/myapplication/ExampleUnitTest.java

@@ -2,7 +2,7 @@ package com.example.david.myapplication;
 
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 /**
  * To work on unit tests, switch the Test Artifact in the Build Variants view.