aboutsummaryrefslogtreecommitdiff
path: root/mobileapp/src/smoots/udesign/canvas/VirtualCanvasActivity.java
blob: 8ba75083e4d11d8256ae9e76708063461d7d2917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package smoots.udesign.canvas;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import smoots.udesign.accelerometer.AccelerometerListener;
import smoots.udesign.accelerometer.AccelerometerManager;
import smoots.udesign.canvas.CanvasView.CanvasThread;
import smoots.udesign.colorpicker.ColorPickerDialog;
import smoots.udesign.colorpicker.ColorPickerListener;
import smoots.udesign.io.ClientWriter;
import smoots.udesign.main.R;
import smoots.udesign.main.R.id;
import smoots.udesign.main.R.layout;
import smoots.udesign.main.R.menu;
import smoots.udesign.packet.MotionType;
import smoots.udesign.packet.Packet;
import smoots.udesign.packet.PhoneToServerPacket;
import smoots.udesign.settings.OnSettingsChangedListener;
import smoots.udesign.settings.SettingsDialog;
import smoots.udesign.util.PhoneDebugger;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class VirtualCanvasActivity extends Activity implements
		AccelerometerListener, ColorPickerListener, OnSettingsChangedListener {

	private static final String TAG = "VirtualCanvasActivity";
	private static Context CONTEXT;
	private static String DEFAULT_IP = "18.244.3.179";
	// private static String DEFAULT_IP = "10.189.17.92";
	private int DEFAULT_PORT = 20120;
	private static final long TIME_INTERVAL_THRESHOLD = 500;

	// ACCELEROMETER INFO
	private HashMap<String, Float> sendData;

	// SENDER INFO
	private ClientWriter writerThread;
	private String ipAddr;
	private int port;
	private BlockingQueue<Packet> packetQ;
	private boolean isSending = false;
	private long timeKeeper;

	// COLOR PICKER INFO
	private static final String BRIGHTNESS_PREFERENCE_KEY = "brightness";
	private static final String COLOR_PREFERENCE_KEY = "color";

	// VIRTUALIZATION VIEW AND THREAD
	private CanvasThread mCanvasThread;
	private CanvasView mCanvasView;

	// -------------------------
	// ACTIVITY ATTRIBUTES
	// -------------------------
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initControls();

		if (savedInstanceState == null) {
			// we were just launched: set up a new canvas
			mCanvasThread.setState(CanvasThread.STATE_RUNNING);
			Log.w(this.getClass().getName(), "SIS is null");
		} else {
			// resume a saved canvas
			mCanvasThread.restoreState(savedInstanceState);
			Log.w(this.getClass().getName(), "SIS is nonnull");
		}
	}

	private void initControls() {
		setContentView(R.layout.canvaslo);
		CONTEXT = this;

		this.mCanvasView = (CanvasView) findViewById(R.id.canvas_view);
		this.mCanvasThread = mCanvasView.getThread();
		this.mCanvasView.setTextView((TextView) findViewById(R.id.status_text));

		this.ipAddr = DEFAULT_IP;
		this.port = DEFAULT_PORT;
		this.packetQ = new LinkedBlockingQueue<Packet>();

		this.sendData = new HashMap<String, Float>();
		this.sendData.put("color", (float) 0);

		// txtSendStatus = (TextView) findViewById(R.id.txtSendStatus);
		this.timeKeeper = System.currentTimeMillis();
	}

	protected void onPause() {
		super.onPause();
		this.mCanvasView.getThread().pause(); // pause canvas when Activity pauses
	}

	protected void onResume() {
		super.onResume();
		if (AccelerometerManager.isSupported()) {
			AccelerometerManager.startListening(this);
		}
	}

	protected void onDestroy() {
		super.onDestroy();
		if (AccelerometerManager.isListening()) {
			AccelerometerManager.stopListening();
		}
	}

	protected void onSaveInstanceState(Bundle outState) {
		// just have the View's thread save its state into our Bundle
		super.onSaveInstanceState(outState);
		this.mCanvasThread.saveState(outState);
		Log.w(this.getClass().getName(), "SaveInstanceState called");
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.canvas_menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.send:
			toggleSending();

			/*
			 * // pass Sending Intent to PacketSender Intent toPacketSender =
			 * new Intent(this, PacketSenderService.class); //
			 * toPacketSender.putExtra("ipAddr", this.ipAddr); //
			 * toPacketSender.putExtra("port", this.port);
			 * toPacketSender.putExtra("ipAddr", "18.244.6.28");
			 * toPacketSender.putExtra("port", "20120"); if (!isSending) {
			 * startService(toPacketSender); isSending = true; } else {
			 * stopService(toPacketSender); isSending = false; }
			 */
			return true;

		case R.id.color_picker:
			int color = PreferenceManager.getDefaultSharedPreferences(
					VirtualCanvasActivity.this).getInt(COLOR_PREFERENCE_KEY,
					Color.WHITE);
			new ColorPickerDialog(VirtualCanvasActivity.this,
					VirtualCanvasActivity.this, color).show();
			return true;

		// case R.id.effect_picker:
		// return true;

		case R.id.settings:
			new SettingsDialog(VirtualCanvasActivity.this,
					VirtualCanvasActivity.this).show();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	// -------------------------
	// STATIC
	// -------------------------
	public static Context getContext() {
		return CONTEXT;
	}

	// -------------------------
	// HELPERS
	// -------------------------
	private void toggleSending() {
		// Toast.makeText(this, this.ipAddr + ":" + Integer.toString(this.port),
		// Toast.LENGTH_SHORT).show();

		if (!this.isSending) {
			Log.d(TAG, "Start Sending");
			Socket socket = null;
			try {
				socket = new Socket(this.ipAddr, this.port);
				this.writerThread = new ClientWriter(this.packetQ, socket);
				this.writerThread.setRunning(true);
				this.writerThread.start();
				this.isSending = true;
			} catch (UnknownHostException e) {
				PhoneDebugger.debug(TAG, e);
				Toast.makeText(this, "Socket Not Created", Toast.LENGTH_SHORT)
						.show();
			} catch (IOException e) {
				PhoneDebugger.debug(TAG, e);
				Toast.makeText(this, "Socket Not Created", Toast.LENGTH_SHORT)
						.show();
			}
		} else {
			Log.d(TAG, "Stop Sending");

			this.writerThread.setRunning(false);
			this.writerThread.interrupt();

			Toast.makeText(this, "Writer stopped", Toast.LENGTH_SHORT).show();
			this.isSending = false;
		}

		// String serverAddr = "www.google.com";
		// int serverPort =
		// Integer.parseInt(((EditText)findViewById(R.id.settings_port)).getText().toString());

		/*
		 * String serverAddr = this.ipAddr; //String serverAddr =
		 * ((EditText)findViewById(R.id.settings_ip)).getText().toString(); int
		 * serverPort = this.port; Toast.makeText(this, serverAddr,
		 * Toast.LENGTH_LONG).show();
		 * 
		 * Socket socket = null; try { socket = new Socket(serverAddr,
		 * serverPort); //socket = new Socket(serverAddr, 80); } catch
		 * (Exception e) { printScr("Exception!"); printScr(e.getMessage());
		 * printScr(e.getStackTrace().toString()); } printScr("connecting");
		 * String message = new JSONObject(this.xyzPhonePos).toString();
		 * printScr(message); try { PrintWriter out = new
		 * PrintWriter(socket.getOutputStream(), true); out.println(message);
		 * printScr("sent"); } catch (Exception e) {
		 * printScr("exception occurs"); } finally { printScr("close socket");
		 * socket.close(); }
		 */
	}

	// -------------------------
	// LISTENERS
	// -------------------------
	/**
	 * onAccelerationChanged callback function
	 */
	public void onAccelerationChanged(float x, float y, float z) {
		Log.d(TAG, "accerleration changed to" + Float.toString(x) + ", "
				+ Float.toString(y) + ", " + Float.toString(z));
		this.sendData.put("xPos", x);
		this.sendData.put("yPos", y);
		this.sendData.put("zPos", z);
		// ((TextView) findViewById(R.id.x)).setText(String.valueOf(x));
		// ((TextView) findViewById(R.id.y)).setText(String.valueOf(y));
		// ((TextView) findViewById(R.id.z)).setText(String.valueOf(z));

		this.mCanvasThread.updatePixelVelocity(x, y, z);

		if (this.isSending == true
				&& System.currentTimeMillis() - this.timeKeeper > TIME_INTERVAL_THRESHOLD) {
			// Toast.makeText(this, "Send" + Float.toString(x) + ", " +
			// Float.toString(y) + ", " + Float.toString(z),
			// Toast.LENGTH_SHORT).show();
			this.timeKeeper = System.currentTimeMillis();
			
			this.packetQ.add(new PhoneToServerPacket(
					MotionType.MOVE, 
					this.sendData.get("xPos"), 
					this.sendData.get("yPos"),
					this.sendData.get("color"))
			);
		}
	}

	/**
	 * onShake callback
	 */
	public void onShake(float force) {
		Toast.makeText(this, "Phone shaked : " + force, Toast.LENGTH_LONG)
				.show();

		this.packetQ.add(new PhoneToServerPacket(2, 0, 0, this.sendData.get("color")));
	}

	/**
	 * onColorChanged callback function
	 */
	public void onColorChanged(int color) {
		// txtSendStatus.setText(Integer.toString(color));
		this.sendData.put("color", (float) color);

		this.mCanvasThread.updatePixelColor(color);
		// Button colorBtn = (Button) findViewById(R.id.color_button);
		// colorBtn.getBackground()
		// .setColorFilter(color, PorterDuff.Mode.MULTIPLY);
	}

	/**
	 * onSettingsChanged callback
	 */
	public void onSettingsChanged(String ipAddr, int port) {
		this.ipAddr = ipAddr;
		this.port = port;
	}

	public String getCurrentIP() {
		return this.ipAddr;
	}

	public int getCurrentPort() {
		return this.port;
	}

}