From 21477c7082352f80bbe6d9bfc7564bc7a95fa4f6 Mon Sep 17 00:00:00 2001 From: Poly Takahiro Horikawa Date: Fri, 13 Dec 2013 21:46:51 -0800 Subject: [PATCH] Add swipe button and clean up layout --- .../glassremote/ui/ControlPanel.java | 50 ++++++++++++++----- .../glassremote/ui/InfoPanel.java | 12 +++-- .../glassremote/ui/MainFrame.java | 26 ++++++---- .../glassremote/util/GlassMessagingUtil.java | 41 ++++++++++++--- 4 files changed, 98 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/polysfactory/glassremote/ui/ControlPanel.java b/src/main/java/com/polysfactory/glassremote/ui/ControlPanel.java index 2f237d0..0170ac8 100644 --- a/src/main/java/com/polysfactory/glassremote/ui/ControlPanel.java +++ b/src/main/java/com/polysfactory/glassremote/ui/ControlPanel.java @@ -1,12 +1,13 @@ package com.polysfactory.glassremote.ui; import java.awt.Color; -import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.BorderFactory; +import javax.swing.GroupLayout; +import javax.swing.GroupLayout.Alignment; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextArea; @@ -19,25 +20,23 @@ @SuppressWarnings("serial") public class ControlPanel extends JPanel { - private static final int WIDTH = 640; - private static final int HEIGHT = 180; private GlassConnection mGlassConnection; public ControlPanel(GlassConnection glassConnection) { - setBackground(Color.BLACK); - setPreferredSize(new Dimension(WIDTH, HEIGHT)); - setLayout(null); + GroupLayout layout = new GroupLayout(this); + layout.setAutoCreateContainerGaps(true); + layout.setAutoCreateGaps(true); + setLayout(layout); mGlassConnection = glassConnection; - final JTextArea textArea = new JTextArea(100, 20); - textArea.setBounds(5, 5, 395, 110); + final JTextArea textArea = new JTextArea(); + textArea.setColumns(20); Border border = BorderFactory.createLineBorder(Color.black); textArea.setBorder(border); add(textArea); - JButton sendButton = new JButton("Send to Timeline"); - sendButton.setBounds(5, 120, 150, 40); + JButton sendButton = new JButton("SEND_TO_TIMELINE"); sendButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -48,8 +47,23 @@ public void actionPerformed(ActionEvent e) { }); add(sendButton); + final JButton swipeLeftButton = new JButton("SWIPE_LEFT"); + swipeLeftButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + List envelopes = GlassMessagingUtil.getSwipeLeftEvents(); + mGlassConnection.writeAsync(envelopes); + } + }); + final JButton swipeRightButton = new JButton("SWIPE_RIGHT"); + swipeRightButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + List envelopes = GlassMessagingUtil.getSwipeRightEvents(); + mGlassConnection.writeAsync(envelopes); + } + }); final JButton swipeDownButton = new JButton("SWIPE_DOWN"); - swipeDownButton.setBounds(400, 5, 150, 40); swipeDownButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -57,7 +71,19 @@ public void actionPerformed(ActionEvent e) { mGlassConnection.writeAsync(envelopes); } }); - add(swipeDownButton); + + layout.setHorizontalGroup(layout + .createParallelGroup() + .addGroup( + layout.createSequentialGroup().addComponent(swipeLeftButton).addComponent(swipeRightButton) + .addComponent(swipeDownButton)) + .addGroup(layout.createSequentialGroup().addComponent(textArea).addComponent(sendButton))); + layout.setVerticalGroup(layout + .createSequentialGroup() + .addGroup( + layout.createParallelGroup().addComponent(swipeLeftButton).addComponent(swipeRightButton) + .addComponent(swipeDownButton)) + .addGroup(layout.createParallelGroup(Alignment.CENTER).addComponent(textArea).addComponent(sendButton))); } } diff --git a/src/main/java/com/polysfactory/glassremote/ui/InfoPanel.java b/src/main/java/com/polysfactory/glassremote/ui/InfoPanel.java index 7d99e78..32dba59 100644 --- a/src/main/java/com/polysfactory/glassremote/ui/InfoPanel.java +++ b/src/main/java/com/polysfactory/glassremote/ui/InfoPanel.java @@ -1,8 +1,8 @@ package com.polysfactory.glassremote.ui; -import java.awt.Color; import java.awt.Dimension; +import javax.swing.GroupLayout; import javax.swing.JPanel; import javax.swing.JTextArea; @@ -14,14 +14,18 @@ public class InfoPanel extends JPanel { private JTextArea textArea; public InfoPanel() { - setBackground(Color.BLACK); setPreferredSize(new Dimension(WIDTH, HEIGHT)); - setLayout(null); + GroupLayout layout = new GroupLayout(this); + layout.setAutoCreateContainerGaps(true); + layout.setAutoCreateGaps(true); + setLayout(layout); textArea = new JTextArea(); textArea.setBounds(5, 5, 630, 30); textArea.setEditable(false); - add(textArea); + + layout.setHorizontalGroup(layout.createParallelGroup().addComponent(textArea)); + layout.setVerticalGroup(layout.createSequentialGroup().addComponent(textArea)); } public void setText(String text) { diff --git a/src/main/java/com/polysfactory/glassremote/ui/MainFrame.java b/src/main/java/com/polysfactory/glassremote/ui/MainFrame.java index 494f724..c9839e8 100644 --- a/src/main/java/com/polysfactory/glassremote/ui/MainFrame.java +++ b/src/main/java/com/polysfactory/glassremote/ui/MainFrame.java @@ -16,7 +16,6 @@ */ package com.polysfactory.glassremote.ui; -import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Insets; import java.awt.event.ActionEvent; @@ -35,10 +34,12 @@ import javax.imageio.ImageIO; import javax.swing.ButtonGroup; import javax.swing.DefaultListModel; +import javax.swing.GroupLayout; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JOptionPane; +import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JRadioButtonMenuItem; import javax.swing.SwingUtilities; @@ -195,21 +196,28 @@ private void initializeFrame() { private void initializePanel() { + JPanel panel = new JPanel(); + GroupLayout layout = new GroupLayout(panel); + panel.setLayout(layout); + mInfoPanel = new InfoPanel(); - add(mInfoPanel, BorderLayout.NORTH); mScreencastPanel = new ScreencastPanel(); mScreencastPanel.setZoom(mZoom); mScreencastPanel.setScreencastMouseEventListener(this); - add(mScreencastPanel, BorderLayout.CENTER); mControlPanel = new ControlPanel(mGlassConnection); - add(mControlPanel, BorderLayout.SOUTH); + + layout.setHorizontalGroup(layout.createParallelGroup().addComponent(mInfoPanel).addComponent(mScreencastPanel) + .addComponent(mControlPanel)); + layout.setVerticalGroup(layout.createSequentialGroup().addComponent(mInfoPanel).addComponent(mScreencastPanel) + .addComponent(mControlPanel)); + + add(panel); } private void initializeMenu() { mPopupMenu = new JPopupMenu(); - initializeSelectDeviceMenu(); mPopupMenu.addSeparator(); initializeZoomMenu(); @@ -335,10 +343,10 @@ public void run() { if (envelope.glassInfoResponseG2C != null) { GlassInfoResponse response = envelope.glassInfoResponseG2C; String info = ""; - info += "device name:" + response.deviceName; - info += ", battery:" + response.batteryLevel + "%\n"; - info += "storage:" + response.externalStorageAvailableBytes + "/" + response.externalStorageTotalBytes - + "bytes available"; + info += "Device name: " + response.deviceName; + info += " Battery: " + response.batteryLevel + "%\n"; + info += "Storage: " + response.externalStorageAvailableBytes + "/" + response.externalStorageTotalBytes + + " bytes available"; mInfoPanel.setText(info); } if (envelope.companionInfo != null) { diff --git a/src/main/java/com/polysfactory/glassremote/util/GlassMessagingUtil.java b/src/main/java/com/polysfactory/glassremote/util/GlassMessagingUtil.java index a833ee8..f4ec924 100644 --- a/src/main/java/com/polysfactory/glassremote/util/GlassMessagingUtil.java +++ b/src/main/java/com/polysfactory/glassremote/util/GlassMessagingUtil.java @@ -66,21 +66,50 @@ public static final Envelope newMotionEventEnvelope(MotionEvent e) { return envelope; } - public static final List getSwipeDownEvents() { + private static final int SWIPE_STEP_COUNT = 2; + private static final long SWIPE_DURATION = 700; + private static final long SWIPE_STEP_DURATION = (long) ((float) SWIPE_DURATION / (float) SWIPE_STEP_COUNT); + + public static final List getSwipeEvents(float startX, float startY, float endX, float endY) { List res = new ArrayList(); - float x = 50.0F; - long downTime = System.currentTimeMillis(); - MotionEvent downEvent = convertMouseEvent2MotionEvent(ACTION_DOWN, x, 10.0F, downTime); + float x = startX; + float y = startY; + float stepX = (endX - startX) / (float) SWIPE_STEP_COUNT; + float stepY = (endY - startY) / (float) SWIPE_STEP_COUNT; + + long downTime = System.currentTimeMillis() - SWIPE_DURATION; + long eventTime = downTime; + MotionEvent downEvent = convertMouseEvent2MotionEvent(ACTION_DOWN, x, y, downTime); res.add(newMotionEventEnvelope(downEvent)); - for (float y = 15.0F; y < 90.0F; y += 8.0F) { + for (int i = 0; i < SWIPE_STEP_COUNT - 1; i++) { + x += stepX; + y += stepY; + eventTime += SWIPE_STEP_DURATION; MotionEvent moveEvent = convertMouseEvent2MotionEvent(ACTION_MOVE, x, y, downTime); + moveEvent.eventTime = eventTime; res.add(newMotionEventEnvelope(moveEvent)); } - MotionEvent upEvent = convertMouseEvent2MotionEvent(ACTION_UP, x, 90.0F, downTime); + x += stepX; + y += stepY; + eventTime += SWIPE_STEP_DURATION; + MotionEvent upEvent = convertMouseEvent2MotionEvent(ACTION_UP, x, y, downTime); + upEvent.eventTime = eventTime; res.add(newMotionEventEnvelope(upEvent)); return res; } + public static final List getSwipeDownEvents() { + return getSwipeEvents(33.3F, 0.001F, 33.3F, 99.999F); + } + + public static final List getSwipeLeftEvents() { + return getSwipeEvents(0.001F, 50.0F, 99.999F, 50.0F); + } + + public static final List getSwipeRightEvents() { + return getSwipeEvents(99.999F, 50.0F, 0.001F, 50.0F); + } + public static Envelope createTimelineMessage(String text) { long now = System.currentTimeMillis(); Envelope envelope = CompanionMessagingUtil.newEnvelope();