/
KillAllHumans.java
376 lines (339 loc) · 10.3 KB
/
KillAllHumans.java
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.bluetooth.activities;
import java.io.IOException;
import java.util.Random;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.Face;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.bluetooth.BluetoothActivity;
import com.bluetooth.BluetoothRemoteControlApp;
import com.bluetooth.R;
/**
* This activity uses the camera's face detection feature to detect human faces
* and then charge towards them in order to kill (make the face stop looking
* like a human one). Once can easily see the potential in up scaling the mobile
* platform and adding some water guns/circular saws/machine guns.
* <p>
* This activity also lets the user enable sound effects borrowed from the
* loving the turret gun from the popular Valve game named Portal.
*/
public class KillAllHumans extends BluetoothActivity implements SurfaceHolder.Callback, Camera.FaceDetectionListener
{
private Camera camera;
private SurfaceView svPreview, svOverlay;
private SurfaceHolder previewHolder, overlayHolder;
private TextView tvInfo, tvWheelLeft, tvWheelRight;
private MenuItem soundMenu;
private Button bToggle;
private Paint red, textPaint;
private Random randomInt = new Random();
private MediaPlayer mediaPlayer;
private String[] sleep = {"good_bye", "nap_time", "shutting_down", "sleep_mode"};
private String[] wakeup = {"activated"};
private String[] search = {"anyone_there", "are_you_still_there", "searching"};
private String[] kill = {"i_see_you", "target_aquired", "there_you_are"};
private static final int SLEEP = 0;
private static final int WAKEUP = 1;
private static final int SEARCH = 2;
private static final int KILL = 3;
private Dialog pidSettingsDialog;
private int targets, state, maxSpeed;
private String[] actions = {"Sleep", "Wakeup", "Search", "Kill"};
private boolean previewing, soundEnabled;
private float pid_kp, pid_ki, pid_kd;
private float control, control_p, control_i, control_d;
private int i, left, top, error, wheelLeft, wheelRight, lastError;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.kill_all_humans);
svPreview = (SurfaceView) findViewById(R.id.svPreview);
svOverlay = (SurfaceView) findViewById(R.id.svOverlay);
tvInfo = (TextView) findViewById(R.id.tvInfo);
tvWheelLeft = (TextView) findViewById(R.id.tvWheelLeft);
tvWheelRight = (TextView) findViewById(R.id.tvWheelRight);
bToggle = (Button) findViewById(R.id.bToggle);
bToggle.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if(state == SLEEP)
{
lastError = 0;
control = 0;
control_p = 0;
control_i = 0;
control_d = 0;
setState(WAKEUP);
camera.autoFocus(null);
bToggle.setText(R.string.stop);
}
else
{
write("s,0,0");
targets = 0;
setState(SLEEP);
bToggle.setText(R.string.start);
}
}
});
// Get saved PID values
SharedPreferences killAllHumansPID = this.getSharedPreferences("killAllHumansPID", MODE_PRIVATE);
pid_kp = killAllHumansPID.getFloat("kp", 1);
pid_ki = killAllHumansPID.getFloat("ki", 0);
pid_kd = killAllHumansPID.getFloat("kd", 0);
maxSpeed = killAllHumansPID.getInt("maxSpeed", 50);
pidSettingsDialog = new PIDSettings(KillAllHumans.this, pid_kp, pid_ki, pid_kd, maxSpeed, new Handler(this));
red = new Paint();
red.setColor(Color.RED);
textPaint = new Paint();
textPaint.setColor(Color.RED);
textPaint.setTextSize(20);
textPaint.setTextAlign(Align.CENTER);
previewHolder = svPreview.getHolder();
previewHolder.addCallback(this);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
overlayHolder = svOverlay.getHolder();
overlayHolder.setFormat(PixelFormat.TRANSPARENT);
}
@Override
public boolean handleMessage(Message msg)
{
if(msg.what == BluetoothRemoteControlApp.MSG_2)
{
// Set the new PID constant values
float[] control = (float[]) msg.obj;
pid_kp = control[0];
pid_ki = control[1];
pid_kd = control[2];
maxSpeed = (int) control[3];
// Save them for next time the activity is run
SharedPreferences killAllHumansPID = this.getSharedPreferences("killAllHumansPID", MODE_PRIVATE);
Editor edit = killAllHumansPID.edit();
edit.putFloat("kp", pid_kp);
edit.putFloat("ki", pid_ki);
edit.putFloat("kd", pid_kd);
edit.putInt("maxSpeed", maxSpeed);
edit.commit();
}
return super.handleMessage(msg);
}
@Override
protected void onResume()
{
super.onResume();
state = SLEEP;
targets = 0;
soundEnabled = false;
}
public void surfaceCreated(SurfaceHolder arg0)
{
camera = Camera.open();
camera.setDisplayOrientation(90);
camera.setFaceDetectionListener(this);
try
{
camera.setPreviewDisplay(previewHolder);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Camera.Parameters parameters = camera.getParameters();
Camera.Size result = null;
for(Camera.Size size : parameters.getSupportedPreviewSizes())
{
if(size.width <= width && size.height <= height)
{
if(result == null)
{
result = size;
}
else
{
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if(newArea > resultArea)
{
result = size;
}
}
}
}
Camera.Size size = result;
parameters.setPreviewSize(size.width, size.height);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(parameters);
camera.startPreview();
previewing = true;
camera.startFaceDetection();
}
public void surfaceDestroyed(SurfaceHolder sh)
{
previewing = false;
camera.stopFaceDetection();
camera.stopPreview();
camera.release();
camera = null;
}
public void onFaceDetection(Face[] faces, Camera camera)
{
if(previewing == false)
{
return;
}
Canvas canvas = overlayHolder.lockCanvas();
canvas.drawColor(0, Mode.CLEAR);
if(state != SLEEP)
{
targets = faces.length;
if(targets > 0)
{
setState(KILL);
for(i = 0; i < targets; i++)
{
Rect face = faces[i].rect;
left = canvas.getWidth() - (face.centerY() + 1000) * canvas.getWidth() / 2000;
top = (face.centerX() + 1000) * canvas.getHeight() / 2000;
// Write "target" over each human face
canvas.drawText("Target", left, top, textPaint);
if(i == 0)
{
// Attack the first detected face
error = (int) (100 * (2 * (float) left / canvas.getWidth() - 1));
control_p = error * pid_kp;
control_i = (control_i + error) * pid_ki;
control_d = (error - lastError) * pid_kd;
lastError = error;
control = control_p + control_i + control_d;
wheelLeft = maxSpeed + (int) control;
wheelRight = maxSpeed - (int) control;
write("s," + wheelLeft + "," + wheelRight);
tvWheelLeft.setText("L: " + wheelLeft);
tvWheelRight.setText("R: " + wheelRight);
}
}
}
else if(state != SEARCH)
{
write("r");
setState(SEARCH);
}
}
overlayHolder.unlockCanvasAndPost(canvas);
tvInfo.setText("Targets: " + targets + "\nAction: " + actions[state]);
}
/**
* The state change is mostly serves to play the sound effects when enabled.
*
* @param newState
*/
private void setState(final int newState)
{
if(soundEnabled && mediaPlayer == null && state != newState)
{
String soundName = null;
switch(newState)
{
case SLEEP:
soundName = sleep[randomInt.nextInt(sleep.length)];
break;
case WAKEUP:
soundName = wakeup[randomInt.nextInt(wakeup.length)];
break;
case SEARCH:
soundName = search[randomInt.nextInt(search.length)];
break;
case KILL:
soundName = kill[randomInt.nextInt(kill.length)];
break;
}
mediaPlayer = MediaPlayer.create(getApplicationContext(), getResources().getIdentifier(soundName, "raw", this.getPackageName()));
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp)
{
// Once the sound has finished a new one can be played
mediaPlayer.release();
mediaPlayer = null;
}
});
mediaPlayer.start();
}
state = newState;
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Add the settings icon in the ActionBar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.sound, menu);
// Save reference to toggle icon
soundMenu = menu.findItem(R.id.iSound);
inflater.inflate(R.menu.settings, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Show the settings dialog when settings button is clicked
switch(item.getItemId())
{
case R.id.iSound:
soundEnabled = !soundEnabled;
if(soundEnabled)
{
soundMenu.setIcon(R.drawable.sound_off);
}
else
{
soundMenu.setIcon(R.drawable.sound_on);
}
break;
case R.id.iSettings:
pidSettingsDialog.show();
break;
}
return super.onOptionsItemSelected(item);
}
}