1 | package com.theboboon.diy.streetview.remote.control;
|
---|
2 |
|
---|
3 | import java.lang.ref.WeakReference;
|
---|
4 | import java.util.Date;
|
---|
5 |
|
---|
6 | import android.app.Activity;
|
---|
7 | import android.app.AlertDialog;
|
---|
8 | import android.content.Context;
|
---|
9 | import android.content.Intent;
|
---|
10 | import android.content.SharedPreferences;
|
---|
11 | import android.content.res.Resources;
|
---|
12 | import android.graphics.Bitmap;
|
---|
13 | import android.graphics.BitmapFactory;
|
---|
14 | import android.os.Bundle;
|
---|
15 | import android.os.Handler;
|
---|
16 | import android.os.Message;
|
---|
17 | import android.os.Vibrator;
|
---|
18 | import android.preference.PreferenceManager;
|
---|
19 | import android.text.format.DateFormat;
|
---|
20 | import android.view.Menu;
|
---|
21 | import android.view.MenuItem;
|
---|
22 | import android.view.View;
|
---|
23 | import android.view.View.OnClickListener;
|
---|
24 | import android.view.Window;
|
---|
25 | import android.widget.Button;
|
---|
26 | import android.widget.ImageView;
|
---|
27 | import android.widget.TextView;
|
---|
28 | import android.widget.Toast;
|
---|
29 |
|
---|
30 | public class Main extends Activity {
|
---|
31 | Button btnConnect, btnDisconnect, btnDetect, btnShutdown, btnCapture;
|
---|
32 | TextView captured;
|
---|
33 | ImageView montage_box;
|
---|
34 | Handler main_comms;
|
---|
35 | Handler net_comms;
|
---|
36 | boolean connected = false;
|
---|
37 | SharedPreferences pref;
|
---|
38 | networking net_ops;
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public void onCreate(Bundle savedInstanceState) {
|
---|
42 | super.onCreate(savedInstanceState);
|
---|
43 | requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
|
---|
44 | setContentView(R.layout.activity_main);
|
---|
45 | PreferenceManager.setDefaultValues(this, R.xml.settings, false);
|
---|
46 | pref = PreferenceManager.getDefaultSharedPreferences(this);
|
---|
47 |
|
---|
48 | btnConnect = (Button)findViewById(R.id.btnConnect);
|
---|
49 | btnConnect.setOnClickListener(connect);
|
---|
50 | btnDisconnect = (Button)findViewById(R.id.btnDisconnect);
|
---|
51 | btnDisconnect.setOnClickListener(disconnect);
|
---|
52 | btnDetect = (Button)findViewById(R.id.btnDetectCameras);
|
---|
53 | btnDetect.setOnClickListener(detectCameras);
|
---|
54 | btnCapture = (Button)findViewById(R.id.btnCapture);
|
---|
55 | btnCapture.setOnClickListener(capture);
|
---|
56 | btnShutdown = (Button)findViewById(R.id.btnShutdown);
|
---|
57 | btnShutdown.setOnClickListener(shutdown);
|
---|
58 | montage_box = (ImageView)findViewById(R.id.Montage);
|
---|
59 | captured = (TextView)findViewById(R.id.captured);
|
---|
60 |
|
---|
61 | TextView address_box = (TextView)findViewById(R.id.serverURI);
|
---|
62 |
|
---|
63 | address_box.setText(pref.getString("server", ""));
|
---|
64 | main_comms = new MainHandler(this);
|
---|
65 | net_ops = new networking(main_comms);
|
---|
66 | net_ops.setPriority(10);
|
---|
67 | net_ops.start();
|
---|
68 | net_comms = net_ops.gethandler();
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public boolean onMenuItemSelected(int featureId, MenuItem item) {
|
---|
73 | switch (item.getItemId()) {
|
---|
74 | case R.id.menu_settings:
|
---|
75 | Intent intentDisplaySetting = new Intent(this, SettingsActivity.class);
|
---|
76 | startActivity(intentDisplaySetting);
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 | return super.onMenuItemSelected(featureId, item);
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | protected void onPause() {
|
---|
84 | super.onPause(); // Always call the superclass method first
|
---|
85 | sendcommand("disconnect");
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | protected void onResume()
|
---|
90 | {
|
---|
91 | super.onResume();
|
---|
92 | if (pref.getBoolean("autoConnect", true) && !(pref.getString("server", "").trim().equals("")))
|
---|
93 | {
|
---|
94 | btnConnect.performClick();
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public boolean onCreateOptionsMenu(Menu menu) {
|
---|
100 | getMenuInflater().inflate(R.menu.activity_main, menu);
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public void onStop()
|
---|
106 | {
|
---|
107 | super.onStop();
|
---|
108 | if (connected)
|
---|
109 | {
|
---|
110 | sendcommand("disconnect");
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | private OnClickListener connect = new OnClickListener() {
|
---|
115 | public void onClick(View v){
|
---|
116 | btnConnect.setClickable(false);
|
---|
117 | btnConnect.setText(getResources().getString(R.string.connecting));
|
---|
118 | TextView address_box = (TextView)findViewById(R.id.serverURI);
|
---|
119 | if (!address_box.getText().equals(pref.getString("server", "")))
|
---|
120 | {
|
---|
121 | SharedPreferences.Editor editor = pref.edit();
|
---|
122 | editor.putString("server", address_box.getText().toString());
|
---|
123 | editor.commit();
|
---|
124 | }
|
---|
125 |
|
---|
126 | String[] serverdetails = pref.getString("server", "localhost").split(":");
|
---|
127 | int port;
|
---|
128 | if (serverdetails.length == 2)
|
---|
129 | {
|
---|
130 | port = Integer.parseInt(serverdetails[1].trim());
|
---|
131 | }
|
---|
132 | else
|
---|
133 | {
|
---|
134 | port = 51234;
|
---|
135 | }
|
---|
136 |
|
---|
137 | Bundle mail = new Bundle();
|
---|
138 | mail.putString("command", "connect");
|
---|
139 | mail.putString("address", serverdetails[0]);
|
---|
140 | mail.putInt("port", port);
|
---|
141 | sendToServer(mail);
|
---|
142 | }
|
---|
143 | };
|
---|
144 |
|
---|
145 | private OnClickListener disconnect = new OnClickListener() {
|
---|
146 | public void onClick(View v){
|
---|
147 | btnDisconnect.setClickable(false);
|
---|
148 | btnDisconnect.setText(getResources().getString(R.string.disconnecting));
|
---|
149 | sendcommand("disconnect");
|
---|
150 | }
|
---|
151 | };
|
---|
152 |
|
---|
153 | private OnClickListener shutdown = new OnClickListener() {
|
---|
154 | public void onClick(View v){
|
---|
155 | sendcommand("shutdown");
|
---|
156 | btnShutdown.setClickable(false);
|
---|
157 | btnShutdown.setText(getResources().getString(R.string.shuttingdown));
|
---|
158 | }
|
---|
159 | };
|
---|
160 |
|
---|
161 | private OnClickListener detectCameras = new OnClickListener() {
|
---|
162 | public void onClick(View v){
|
---|
163 | btnDetect.setText(getResources().getString(R.string.detectingCams));
|
---|
164 | btnDetect.setClickable(false);
|
---|
165 | sendcommand("detectcameras");
|
---|
166 | }
|
---|
167 | };
|
---|
168 |
|
---|
169 | private OnClickListener capture = new OnClickListener() {
|
---|
170 | public void onClick(View v){
|
---|
171 | Resources res = getResources();
|
---|
172 | captured.setText("");
|
---|
173 | btnCapture.setClickable(false);
|
---|
174 | btnCapture.setText(res.getString(R.string.capturing));
|
---|
175 | sendcommand("capture");
|
---|
176 | }
|
---|
177 | };
|
---|
178 |
|
---|
179 | public void processmessage(Message msg)
|
---|
180 | {
|
---|
181 | Bundle content = msg.getData();
|
---|
182 | if (content != null && content.containsKey("response"))
|
---|
183 | {
|
---|
184 | setProgressBarIndeterminateVisibility(false);
|
---|
185 | String command = content.getString("response");
|
---|
186 | Resources res = getResources();
|
---|
187 | if (command.equals("error"))
|
---|
188 | {
|
---|
189 | error(content.getString("message"));
|
---|
190 | }
|
---|
191 | if (command.equals("warning"))
|
---|
192 | {
|
---|
193 | warning(content.getString("message"));
|
---|
194 | }
|
---|
195 | if (command.equals("notify"))
|
---|
196 | {
|
---|
197 | notify(content.getString("message"));
|
---|
198 | }
|
---|
199 | if (command.equals("connection_status"))
|
---|
200 | {
|
---|
201 | boolean status = content.getBoolean("status");
|
---|
202 | connected = status;
|
---|
203 | TextView text = (TextView)findViewById(R.id.connectedto);
|
---|
204 | if (status)
|
---|
205 | {
|
---|
206 | text.setText("Connected to " + pref.getString("server", ""));
|
---|
207 | btnConnect.setVisibility(View.GONE);
|
---|
208 | TextView server_box = (TextView)findViewById(R.id.serverURI);
|
---|
209 | server_box.setVisibility(View.GONE);
|
---|
210 | btnDisconnect.setVisibility(View.VISIBLE);
|
---|
211 | btnDisconnect.setClickable(true);
|
---|
212 | btnDisconnect.setText(res.getString(R.string.disconnect));
|
---|
213 | if (pref.getBoolean("autoDetect", true))
|
---|
214 | {
|
---|
215 | btnDetect.performClick();
|
---|
216 | }
|
---|
217 | btnDetect.setVisibility(View.VISIBLE);
|
---|
218 | TextView detectCameras = (TextView)findViewById(R.id.camerasDetected);
|
---|
219 | detectCameras.setVisibility(View.VISIBLE);
|
---|
220 | btnCapture.setClickable(true);
|
---|
221 | btnCapture.setText(res.getString(R.string.capture));
|
---|
222 | btnCapture.setVisibility(View.VISIBLE);
|
---|
223 | montage_box.setVisibility(View.VISIBLE);
|
---|
224 | if (pref.getBoolean("allowShutdown", false))
|
---|
225 | {
|
---|
226 | btnShutdown.setClickable(true);
|
---|
227 | btnShutdown.setText(getResources().getString(R.string.shutdown));
|
---|
228 | btnShutdown.setVisibility(View.VISIBLE);
|
---|
229 | }
|
---|
230 | captured.setText("");
|
---|
231 | captured.setVisibility(View.VISIBLE);
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | text.setText(res.getString(R.string.not_connected));
|
---|
236 | btnConnect.setText(res.getString(R.string.connect_button));
|
---|
237 | btnConnect.setVisibility(View.VISIBLE);
|
---|
238 | btnConnect.setClickable(true);
|
---|
239 | TextView server_box = (TextView)findViewById(R.id.serverURI);
|
---|
240 | server_box.setVisibility(View.VISIBLE);
|
---|
241 | btnDisconnect.setVisibility(View.GONE);
|
---|
242 | btnDetect.setVisibility(View.GONE);
|
---|
243 | TextView detectCameras = (TextView)findViewById(R.id.camerasDetected);
|
---|
244 | detectCameras.setVisibility(View.GONE);
|
---|
245 | btnCapture.setVisibility(View.GONE);
|
---|
246 | montage_box.setVisibility(View.GONE);
|
---|
247 | montage_box.setImageResource(android.R.color.transparent);
|
---|
248 | btnShutdown.setVisibility(View.GONE);
|
---|
249 | captured.setVisibility(View.GONE);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | if (command.equals("detected cameras"))
|
---|
253 | {
|
---|
254 | TextView text = (TextView)findViewById(R.id.camerasDetected);
|
---|
255 | int count = content.getInt("count");
|
---|
256 | if (count > 1)
|
---|
257 | {
|
---|
258 | text.setText("Detected " + count + " cameras.");
|
---|
259 | }
|
---|
260 | else if (count == 1)
|
---|
261 | {
|
---|
262 | text.setText("Detected " + count + " camera.");
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | text.setText(R.string.noCameras);
|
---|
267 | }
|
---|
268 | btnDetect.setText(getResources().getString(R.string.detectCams));
|
---|
269 | btnDetect.setClickable(true);
|
---|
270 | }
|
---|
271 | if (command.equals("capture"))
|
---|
272 | {
|
---|
273 | boolean success = content.getBoolean("success");
|
---|
274 | if (success)
|
---|
275 | {
|
---|
276 | btnCapture.setClickable(true);
|
---|
277 | btnCapture.setText(res.getString(R.string.capture));
|
---|
278 | if (pref.getBoolean("vibrateOnCapture", true))
|
---|
279 | {
|
---|
280 | Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
|
---|
281 | v.vibrate(300);
|
---|
282 | }
|
---|
283 | captured.setText(captured.getText() + res.getString(R.string.capturesuccess) + "\nID: " + content.getString("set"));
|
---|
284 | notify(res.getString(R.string.capturesuccess));
|
---|
285 |
|
---|
286 | if (pref.getBoolean("getMontage", false))
|
---|
287 | {
|
---|
288 | Bundle mail = new Bundle();
|
---|
289 | mail.putString("command", "montage");
|
---|
290 | mail.putString("id", content.getString("set"));
|
---|
291 | sendToServer(mail);
|
---|
292 | }
|
---|
293 | }
|
---|
294 | else
|
---|
295 | {
|
---|
296 | if (!content.containsKey("known") || !content.getBoolean("known"))
|
---|
297 | {
|
---|
298 | error(res.getString(R.string.capturefailed));
|
---|
299 | }
|
---|
300 | btnCapture.setClickable(true);
|
---|
301 | btnCapture.setText(res.getString(R.string.capture));
|
---|
302 | }
|
---|
303 | }
|
---|
304 | if (command.equals("montage"))
|
---|
305 | {
|
---|
306 | byte[] image = content.getByteArray("image");
|
---|
307 | Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
|
---|
308 | montage_box.setImageBitmap(bmp);
|
---|
309 | }
|
---|
310 | }
|
---|
311 | else if (content != null && content.containsKey("pending"))
|
---|
312 | {
|
---|
313 | String command = content.getString("pending");
|
---|
314 | Resources res = getResources();
|
---|
315 | if (command.equals("captured"))
|
---|
316 | {
|
---|
317 | Date d = new Date();
|
---|
318 | CharSequence s = DateFormat.format("hh:mm:ss a", d.getTime());
|
---|
319 | captured.setText(captured.getText() + "[" + s + "] " + res.getString(R.string.gotimage) + " " + content.getString("camera") + "\n");
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | public void warning(String message)
|
---|
325 | {
|
---|
326 | AlertDialog.Builder window = new AlertDialog.Builder(this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
|
---|
327 | window.setMessage(message);
|
---|
328 | window.setTitle("WARNING!");
|
---|
329 | window.setPositiveButton("Ok.", null);
|
---|
330 | window.show();
|
---|
331 | }
|
---|
332 |
|
---|
333 | public void error(String message)
|
---|
334 | {
|
---|
335 | AlertDialog.Builder window = new AlertDialog.Builder(this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);
|
---|
336 | window.setMessage(message);
|
---|
337 | window.setTitle("ERROR!");
|
---|
338 | window.setNeutralButton("Ok.", null);
|
---|
339 | window.show();
|
---|
340 | }
|
---|
341 |
|
---|
342 | public void notify(String message)
|
---|
343 | {
|
---|
344 | Toast.makeText(this, message, Toast.LENGTH_LONG).show();
|
---|
345 | }
|
---|
346 |
|
---|
347 | private void sendcommand(String command)
|
---|
348 | {
|
---|
349 | Bundle mail = new Bundle();
|
---|
350 | mail.putString("command", command);
|
---|
351 | sendToServer(mail);
|
---|
352 | }
|
---|
353 |
|
---|
354 | private void sendToServer(Bundle mail)
|
---|
355 | {
|
---|
356 | setProgressBarIndeterminateVisibility(true);
|
---|
357 | Message msg = net_comms.obtainMessage();
|
---|
358 | msg.setData(mail);
|
---|
359 | msg.sendToTarget();
|
---|
360 | }
|
---|
361 |
|
---|
362 | private static class MainHandler extends Handler
|
---|
363 | {
|
---|
364 | private final WeakReference<Main> mActivity;
|
---|
365 |
|
---|
366 | public MainHandler(Main activity) {
|
---|
367 | mActivity = new WeakReference<Main>(activity);
|
---|
368 | }
|
---|
369 |
|
---|
370 | public void handleMessage(Message msg)
|
---|
371 | {
|
---|
372 | super.handleMessage(msg);
|
---|
373 | mActivity.get().processmessage(msg);
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|