Commit a1678bf1 authored by leslie0216's avatar leslie0216
Browse files

-send ball when tap the screen.

parent fd183058
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -24,9 +24,13 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
......@@ -40,6 +44,8 @@ public class MainActivity extends ActionBarActivity implements NfcAdapter.Create
private TextView mTextView;
private MainView m_mainView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -57,6 +63,10 @@ public class MainActivity extends ActionBarActivity implements NfcAdapter.Create
mTextView = (TextView) findViewById(R.id.textView_explanation);
handleIntent(getIntent());
m_mainView = new MainView(this);
this.addContentView(m_mainView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
}
@Override
......@@ -93,9 +103,9 @@ public class MainActivity extends ActionBarActivity implements NfcAdapter.Create
@Override
public NdefMessage createNdefMessage(NfcEvent event) {
Time time = new Time();
time.setToNow();
String text = ("Beam Time: " + time.format("%H:%M:%S"));
//Time time = new Time();
//time.setToNow();
//String text = ("Beam Time: " + time.format("%H:%M:%S"));
//NdefMessage msg = new NdefMessage(NdefRecord.createMime("text/plain", text.getBytes())
/**
......@@ -108,6 +118,9 @@ public class MainActivity extends ActionBarActivity implements NfcAdapter.Create
*/
//,NdefRecord.createApplicationRecord("com.example.android.beam")
//)
String text = m_mainView.encodeMessage();
NdefRecord record=new NdefRecord(NdefRecord.TNF_WELL_KNOWN,NdefRecord.RTD_TEXT,new byte[0],text.getBytes());
return new NdefMessage(record);
......@@ -287,7 +300,24 @@ public class MainActivity extends ActionBarActivity implements NfcAdapter.Create
@Override
protected void onPostExecute(String result) {
if (result != null) {
mTextView.setText("Read content: " + result);
try {
JSONObject jsonObject = new JSONObject(result);
String ballId = jsonObject.getString("ballId");
int ballColor = jsonObject.getInt("ballColor");
m_mainView.receivedBall(ballId, ballColor);
runOnUiThread(new Runnable() {
@Override
public void run() {
showToast("received ball");
m_mainView.invalidate();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
......
package ca.usask.chl848.nfc;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;
/**
* Created by chl848 on 06/08/2015.
*/
public class MainView extends View {
Paint m_paint;
private static final int m_messageTextSize = 50;
private static final int m_textStrokeWidth = 2;
private static final int m_boundaryStrokeWidth = 10;
private String m_message;
public class Ball {
public int m_ballColor;
public float m_ballX;
public float m_ballY;
public boolean m_isTouched;
public String m_id;
public String m_name;
}
private ArrayList<Ball> m_balls;
private int m_touchedBallId;
private float m_ballRadius;
private float m_ballBornX;
private float m_ballBornY;
public MainView (Context context) {
super(context);
m_paint = new Paint();
setBackgroundColor(Color.WHITE);
m_message = "No Message";
m_touchedBallId = -1;
m_balls = new ArrayList<>();
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
m_ballRadius = displayMetrics.widthPixels * 0.08f;
m_ballBornX = displayMetrics.widthPixels * 0.5f;
m_ballBornY = displayMetrics.heightPixels * 0.75f - m_ballRadius * 2.0f;
addBall();
}
@Override
protected void onDraw(Canvas canvas) {
showBoundary(canvas);
showMessage(canvas);
//showRemotePhones(canvas);
showBalls(canvas);
}
public void showBoundary(Canvas canvas) {
m_paint.setColor(Color.RED);
m_paint.setStrokeWidth(m_boundaryStrokeWidth);
m_paint.setStyle(Paint.Style.FILL_AND_STROKE);
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
canvas.drawLine(0, displayMetrics.heightPixels * 0.75f, displayMetrics.widthPixels, displayMetrics.heightPixels * 0.75f, m_paint);
}
public void showMessage(Canvas canvas) {
m_paint.setTextSize(m_messageTextSize);
m_paint.setColor(Color.GREEN);
m_paint.setStrokeWidth(m_textStrokeWidth);
m_paint.setStyle(Paint.Style.FILL_AND_STROKE);
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
canvas.drawText(m_message, displayMetrics.widthPixels * 0.3f, displayMetrics.heightPixels * 0.8f, m_paint);
}
public void showBalls(Canvas canvas) {
if (!m_balls.isEmpty()) {
for (Ball ball : m_balls) {
m_paint.setColor(ball.m_ballColor);
m_paint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawCircle(ball.m_ballX, ball.m_ballY, m_ballRadius, m_paint);
}
}
}
public int getBallCount() {
return m_balls.size();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
float X = event.getX();
float Y = event.getY();
float touchRadius = event.getTouchMajor();
int ballCount = m_balls.size();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
//m_numberOfTouch++;
if (!canTouch(X, Y)) {
break;
}
m_touchedBallId = -1;
for (int i = 0; i < ballCount; ++i){
Ball ball = m_balls.get(i);
ball.m_isTouched = false;
double dist;
dist = Math.sqrt(Math.pow((X - ball.m_ballX), 2) + Math.pow((Y - ball.m_ballY), 2));
if (dist <= (touchRadius + m_ballRadius)) {
ball.m_isTouched = true;
m_touchedBallId = i;
boolean isOverlap = false;
for (int j = 0; j < ballCount; ++j) {
if (j != m_touchedBallId) {
Ball ball2 = m_balls.get(j);
double dist2 = Math.sqrt(Math.pow((X - ball2.m_ballX), 2) + Math.pow((Y - ball2.m_ballY), 2));
if (dist2 <= m_ballRadius * 2) {
isOverlap = true;
}
}
}
if (!isOverlap && !isBoundary(X, Y)) {
ball.m_ballX = X;
ball.m_ballY = Y;
this.invalidate();
}
}
if (m_touchedBallId > -1)
{
break;
}
}
break;
case MotionEvent.ACTION_MOVE:
if (m_touchedBallId > -1) {
Ball ball = m_balls.get(m_touchedBallId);
if (ball.m_isTouched) {
boolean isOverlap = false;
for (int j = 0; j < ballCount; ++j) {
if (j != m_touchedBallId) {
Ball ball2 = m_balls.get(j);
double dist = Math.sqrt(Math.pow((X - ball2.m_ballX), 2) + Math.pow((Y - ball2.m_ballY), 2));
if (dist <= m_ballRadius * 2) {
isOverlap = true;
}
}
}
if (!isOverlap && !isBoundary(X, Y)) {
ball.m_ballX = X;
ball.m_ballY = Y;
this.invalidate();
}
}
}
break;
case MotionEvent.ACTION_UP:
for (Ball ball : m_balls) {
ball.m_isTouched = false;
}
break;
}
return true;
}
private boolean isBoundary(float x, float y) {
boolean rt = false;
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
if ((y-m_ballRadius <= 0.0f) || (y+m_ballRadius >= (displayMetrics.heightPixels * 0.75f)) || (x-m_ballRadius <= 0.0f) || (x+m_ballRadius >= displayMetrics.widthPixels)) {
rt = true;
}
return rt;
}
private boolean canTouch(float x, float y) {
boolean rt = true;
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
if ((y <= 0.0f) || (y >= (displayMetrics.heightPixels * 0.75f)) || (x <= 0.0f) || (x >= displayMetrics.widthPixels)) {
rt = false;
}
return rt;
}
public void addBall() {
Ball ball = new Ball();
Random rnd = new Random();
ball.m_ballColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
ball.m_ballX = m_ballBornX;
ball.m_ballY = m_ballBornY;
ball.m_isTouched = false;
ball.m_id = UUID.randomUUID().toString();
ball.m_name = "";
m_balls.add(ball);
this.invalidate();
}
public void removeBall(String id) {
for (Ball ball : m_balls) {
if (ball.m_id.equalsIgnoreCase(id)) {
m_balls.remove(ball);
m_touchedBallId = -1;
break;
}
}
}
public void receivedBall(String id, int color) {
boolean isReceived = false;
for (Ball ball : m_balls) {
if (ball.m_id.equalsIgnoreCase(id)) {
isReceived = true;
break;
}
}
if (!isReceived) {
Ball ball = new Ball();
ball.m_id = id;
ball.m_ballColor = color;
ball.m_isTouched = false;
ball.m_ballX = m_ballBornX;
ball.m_ballY = m_ballBornY;
m_balls.add(ball);
}
}
public String encodeMessage() {
if (!m_balls.isEmpty()) {
JSONObject jo = new JSONObject();
Ball ball = m_balls.get(0);
try {
jo.put("ballId", ball.m_id);
jo.put("ballColor", ball.m_ballColor);
//jo.put("receiverName", receiverName);
//jo.put("isSendingBall", true);
//jo.put("name", m_name);
//jo.put("color", m_color);
//jo.put("x", 0);
//jo.put("y", 0);
//jo.put("z", 0);
} catch (JSONException e) {
e.printStackTrace();
}
return jo.toString();
}
return "";
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment