소스 검색

muevo todo a fragmentos. worko

David 10 년 전
부모
커밋
820f53c63e

+ 3 - 1
app/build.gradle

@@ -27,4 +27,6 @@ dependencies {
     compile project(':picasso-2.5.2')
     compile 'com.android.support:appcompat-v7:22.2.1'
     compile 'com.android.support:design:22.2.1'
-    compile 'com.neovisionaries:nv-websocket-client:1.25'}
+    compile 'com.neovisionaries:nv-websocket-client:1.25'
+    compile 'com.google.android.gms:play-services-appindexing:8.1.0'
+}

+ 28 - 235
app/src/main/java/com/example/david/myapplication/MainActivity.java

@@ -3,6 +3,9 @@ package com.example.david.myapplication;
 import android.content.pm.ActivityInfo;
 import android.net.Uri;
 import android.os.Bundle;
+import android.support.v4.app.FragmentStatePagerAdapter;
+import android.support.v4.view.PagerAdapter;
+import android.support.v4.view.ViewPager;
 import android.support.v7.app.AppCompatActivity;
 import android.text.Layout;
 import android.text.method.ScrollingMovementMethod;
@@ -14,10 +17,7 @@ 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;
 
@@ -32,6 +32,10 @@ import com.neovisionaries.ws.client.WebSocketFrame;
 import com.neovisionaries.ws.client.WebSocketListener;
 import com.squareup.picasso.Picasso;
 
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
+
 
 import org.json.JSONObject;
 
@@ -47,74 +51,15 @@ 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 FragmentActivity {
     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 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();
+    private static final int NUM_PAGES = 2;
+    private ViewPager mPager;
+    private PagerAdapter mPagerAdapter;
 
-        // 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
     protected void onCreate(Bundle savedInstanceState) {
-        //requestWindowFeature(Window.FEATURE_NO_TITLE);
         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
         super.onCreate(savedInstanceState);
@@ -123,183 +68,31 @@ public class MainActivity extends AppCompatActivity implements OnGestureListener
             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);
-        chat = (TextView) findViewById(R.id.chat);
-        lparams = mVideoView.getLayoutParams();
-        //lparams.width=mVideoView.getWidth();
-        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));
-        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
-            @Override
-            public void onPrepared(MediaPlayer mediaPlayer) {
-                mediaPlayer.setPlaybackSpeed(1.0f);
-                //  mediaPlayer.start();
-
-
-            }
-        });
-
-//        mVideoView.setLayoutParams(lparams);
-        mVideoView.setVideoPath(path);
-        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();
+        mPager = (ViewPager) findViewById(R.id.pager);
+        mPagerAdapter = new chatFragmentPagerAdapter(getSupportFragmentManager());
+        mPager.setAdapter(mPagerAdapter);
     }
 
-    private void connectWebSocket() {
-        try {
-            WebSocketFactory factory = new WebSocketFactory();
-            SSLContext context = NaiveSSLContext.getInstance("TLS");
 
-            // Set the custom SSL context.
-            factory.setSSLContext(context);
-            ws = factory.createSocket("wss://ws.especificosba.com.ar/");
-        } catch (Exception e) {
-            return;
+    private class chatFragmentPagerAdapter extends FragmentStatePagerAdapter {
+        public chatFragmentPagerAdapter(FragmentManager fm) {
+            super(fm);
         }
 
-        ws.addListener(new WebSocketAdapter() {
-            @Override
-            public void onTextMessage(WebSocket websocket, String message) throws Exception {
-                //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 Fragment getItem(int position) {
+            switch (position) {
+                case 0:
+                    return new videoFragment();
+                default:
+                    return new chatFragment();
             }
-
-            @Override
-            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", "Didn't connect" + cause.getMessage());
-                status = WS_STATUS.DISCONNECTED;
-            }
-        }).connectAsynchronously();
-    }
-
-    public void sendMessage() {
-        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()) {
-            case MotionEvent.ACTION_DOWN:
-                x1 = event.getX();
-                break;
-            case MotionEvent.ACTION_UP:
-                x2 = event.getX();
-                float deltaX = x2 - x1;
-
-                if (Math.abs(deltaX) > MIN_DISTANCE) {
-                    // Left to Right swipe action
-                    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
-                            viewSwitcher.showPrevious();
-                            Toast.makeText(this, "Right to Left swipe [Previous]", Toast.LENGTH_SHORT).show();
-                        }
-                    }
-
-                } else {
-                    // consider as something else - a screen tap for example
-                }
-                break;
         }
-        return super.onTouchEvent(event);
-    }
-
-    @Override
-    public boolean onDown(MotionEvent motionEvent) {
-        return false;
-    }
-
-    @Override
-    public void onShowPress(MotionEvent motionEvent) {
-
-    }
-
-    @Override
-    public boolean onSingleTapUp(MotionEvent motionEvent) {
-        myDetector.onTouchEvent(motionEvent);
-        return super.onTouchEvent(motionEvent);
-    }
-
-    @Override
-    public boolean onScroll(MotionEvent e1, MotionEvent e2, float v, float v1) {
-        return false;
-    }
-
-    @Override
-    public void onLongPress(MotionEvent motionEvent) {
 
+        @Override
+        public int getCount() {
+            return NUM_PAGES;
+        }
     }
 
-    @Override
-    public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) {
-        return false;
-    }
 }

+ 141 - 0
app/src/main/java/com/example/david/myapplication/chatFragment.java

@@ -0,0 +1,141 @@
+package com.example.david.myapplication;
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+import android.support.v4.app.Fragment;
+import android.text.Layout;
+import android.text.method.ScrollingMovementMethod;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.EditText;
+import android.widget.TextView;
+
+import com.neovisionaries.ws.client.WebSocket;
+import com.neovisionaries.ws.client.WebSocketAdapter;
+import com.neovisionaries.ws.client.WebSocketException;
+import com.neovisionaries.ws.client.WebSocketFactory;
+import com.neovisionaries.ws.client.WebSocketFrame;
+
+import org.json.JSONObject;
+import org.w3c.dom.Text;
+
+import java.util.List;
+import java.util.Map;
+
+import javax.net.ssl.SSLContext;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.TextView;
+
+/**
+ * Created by david on 16/04/16.
+ */
+public class chatFragment extends Fragment {
+    private static final String WSS_URL = "wss://ws.especificosba.com.ar/";
+    private TextView chat;
+    private WebSocket ws;
+    private enum WS_STATUS {
+        CONNECTED,
+        DISCONNECTED
+    };
+    private WS_STATUS status = WS_STATUS.DISCONNECTED;
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        ViewGroup rootView = (ViewGroup) inflater.inflate(
+                R.layout.fragment_chat , container, false);
+
+
+        return rootView;
+    }
+
+    @Override
+    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        connectWebSocket();
+        Button b = (Button) getView().findViewById(R.id.button);
+        chat =(TextView) getView().findViewById(R.id.chat);
+        b.setOnClickListener(new Button.OnClickListener(){
+            @Override
+            public void onClick(View b){
+                sendMessage();
+            }
+        });
+        chat.setMovementMethod(new ScrollingMovementMethod());
+    }
+
+    private void connectWebSocket() {
+        try {
+            WebSocketFactory factory = new WebSocketFactory();
+            SSLContext context = NaiveSSLContext.getInstance("TLS");
+
+            // Set the custom SSL context.
+            factory.setSSLContext(context);
+            ws = factory.createSocket(WSS_URL);
+        } catch (Exception e) {
+            return;
+        }
+
+        ws.addListener(new WebSocketAdapter() {
+            @Override
+            public void onTextMessage(WebSocket websocket, String message) throws Exception {
+                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", "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", "Didn't connect" + cause.getMessage());
+                status = WS_STATUS.DISCONNECTED;
+            }
+        }).connectAsynchronously();
+    }
+
+    public void sendMessage() {
+        EditText editText = (EditText) getView().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("");
+    }
+
+}

+ 55 - 0
app/src/main/java/com/example/david/myapplication/videoFragment.java

@@ -0,0 +1,55 @@
+package com.example.david.myapplication;
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+import android.support.v4.app.Fragment;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+
+import com.squareup.picasso.Picasso;
+
+import io.vov.vitamio.MediaPlayer;
+import io.vov.vitamio.widget.VideoView;
+
+
+/**
+ * Created by david on 16/04/16.
+ */
+public class videoFragment extends Fragment {
+    private static final String path = "rtmp://plataforma.especificosba.com.ar:1935/src/movie";
+    private static final String default_image = "https://plataforma.especificosba.com.ar/diapo/EBA/1.png";
+    private VideoView mVideoView;
+    private ViewGroup.LayoutParams lparams;
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+                             Bundle savedInstanceState) {
+        ViewGroup rootView = (ViewGroup) inflater.inflate(
+                R.layout.fragment_video , container, false);
+
+        return rootView;
+    }
+
+    @Override
+    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        mVideoView = (VideoView) getView().findViewById(R.id.vitamio_videoView);
+        lparams = mVideoView.getLayoutParams();
+        //lparams.width=mVideoView.getWidth();
+        lparams.width = 200;
+        mVideoView.setLayoutParams(lparams);
+        Picasso.with(this.getActivity().getApplicationContext()).load(default_image).into((ImageView) getView().findViewById(R.id.imageView));
+        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
+            @Override
+            public void onPrepared(MediaPlayer mediaPlayer) {
+                mediaPlayer.setPlaybackSpeed(1.0f);
+                //  mediaPlayer.start();
+
+
+            }
+        });
+        mVideoView.setVideoPath(path);
+        mVideoView.start();
+    }
+
+}

+ 5 - 68
app/src/main/res/layout/activity_main.xml

@@ -1,73 +1,10 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<android.support.v4.view.ViewPager
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/pager"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="horizontal"
-    android:weightSum="1">
+    android:layout_height="match_parent">
 
-    <ViewSwitcher
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:id="@+id/viewSwitcher"
-        android:layout_gravity="center_vertical"
-        android:inAnimation="@android:anim/slide_in_left"
-        android:outAnimation="@android:anim/slide_out_right"
-        >
 
 
-        <LinearLayout
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:orientation="horizontal"
-            android:id="@+id/llayout">
-            <LinearLayout
-                android:layout_width="0dp"
-                android:layout_height="match_parent"
-                android:layout_weight="1">
-                <io.vov.vitamio.widget.VideoView
-                    android:layout_width="match_parent"
-                    android:layout_height="match_parent"
-                    android:id="@+id/vitamio_videoView"/>
-            </LinearLayout>
-            <ImageView
-                android:layout_height="match_parent"
-                android:layout_width="0dp"
-                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="match_parent"
-                android:layout_height="0dp"
-                android:layout_weight="4"
-                android:scrollbars = "vertical"
-                android:id="@+id/chat" />
-            <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>
-
-</LinearLayout>
+</android.support.v4.view.ViewPager>

+ 33 - 0
app/src/main/res/layout/fragment_chat.xml

@@ -0,0 +1,33 @@
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+    android:id="@+id/secondLayout">
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_weight="4"
+        android:scrollbars = "vertical"
+        android:id="@+id/chat" />
+    <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>

+ 21 - 0
app/src/main/res/layout/fragment_video.xml

@@ -0,0 +1,21 @@
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="horizontal"
+    android:id="@+id/llayout">
+    <LinearLayout
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_weight="1">
+        <io.vov.vitamio.widget.VideoView
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:id="@+id/vitamio_videoView"/>
+    </LinearLayout>
+    <ImageView
+        android:layout_height="match_parent"
+        android:layout_width="0dp"
+        android:layout_weight="1"
+        android:id="@+id/imageView" />
+</LinearLayout>