-
Notifications
You must be signed in to change notification settings - Fork 23
/
CosyDVR.java
331 lines (294 loc) · 10.1 KB
/
CosyDVR.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
package es.esy.CosyDVR;
import es.esy.CosyDVR.BackgroundVideoRecorder;
import es.esy.CosyDVR.CosyDVRPreferenceActivity;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Environment;
//import android.os.SystemClock;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.graphics.Point;
import android.os.IBinder;
import java.io.File;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class CosyDVR extends Activity{
BackgroundVideoRecorder mService;
Button favButton,recButton,flsButton,exiButton,focButton,nigButton;
View mainView;
boolean mBound = false;
boolean mayclick = false;
private int mWidth=1,mHeight=1;
long ExitPressTime = 0;
private float mScaleFactor = 4.0f;
private String[] mFocusNames = {"I",
"V",
"A",
"M",
"E",
};
private final class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(4.0f, Math.min(mScaleFactor, 14.0f));
if(mBound) {
mService.setZoom(mScaleFactor);
}
return true;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
favButton = (Button)findViewById(R.id.fav_button);
recButton = (Button)findViewById(R.id.rec_button);
focButton = (Button)findViewById(R.id.foc_button);
nigButton = (Button)findViewById(R.id.nig_button);
flsButton = (Button)findViewById(R.id.fls_button);
exiButton = (Button)findViewById(R.id.exi_button);
mainView = (View)findViewById(R.id.mainview);
favButton.setOnClickListener(favButtonOnClickListener);
recButton.setOnClickListener(recButtonOnClickListener);
focButton.setOnClickListener(focButtonOnClickListener);
nigButton.setOnClickListener(nigButtonOnClickListener);
flsButton.setOnClickListener(flsButtonOnClickListener);
exiButton.setOnClickListener(exiButtonOnClickListener);
exiButton.setOnLongClickListener(exiButtonOnLongClickListener);
recButton.setOnLongClickListener(recButtonOnLongClickListener);
nigButton.setOnLongClickListener(nigButtonOnLongClickListener);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
final ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(this, new ScaleListener());
mainView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
//Extra analysis for single tap detection. Swipes are detected as autofocus too for now
int action = event.getAction() & MotionEvent.ACTION_MASK;
switch(action) {
case MotionEvent.ACTION_DOWN : {
mayclick = true; //first finger touch is like click
break;
}
case MotionEvent.ACTION_POINTER_DOWN : {
mayclick = false; //second finger is not click
break;
}
case MotionEvent.ACTION_UP : {
if(mayclick) { //first finger up. check if it was single one
if(mBound) {
mService.autoFocus();
}
}
mayclick = false;
}
}
return true;
}
});
updateinterface();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
//aqcuire screen size
Display display = getWindowManager().getDefaultDisplay();
// if (Build.VERSION.SDK_INT >= 13){
Point size = new Point();
display.getSize(size);
mWidth = size.x;
mHeight = size.y - favButton.getHeight();
// }else {
// mWidth = display.getWidth();
// mHeight = display.getHeight();
// }
Intent intent = new Intent(/*CosyDVR.this*/getApplicationContext(), BackgroundVideoRecorder.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
}
@Override
public void onDestroy(){
if(mBound) {
unbindService(mConnection);
mBound = false;
}
super.onDestroy();
}
@Override
public void onPause(){
if(mBound) {
mService.ChangeSurface(1, 1);
}
super.onPause();
this.unregisterReceiver(receiver);
}
@Override
public void onResume(){
updateinterface(); //after preferences
if(mBound) {
mService.ChangeSurface(mWidth, mHeight);
}
super.onResume();
this.registerReceiver(receiver,filter);
}
public void showHint(String text){
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(this);
boolean HIDE_HINTS = sharedPref.getBoolean("hide_hints", false);
if(!HIDE_HINTS) {
Toast infotoast = Toast.makeText(CosyDVR.this, text, Toast.LENGTH_LONG);
infotoast.setGravity(Gravity.BOTTOM/* | Gravity.FILL_HORIZONTAL*/,0,0);
infotoast.setMargin(0,0);
infotoast.show();
}
}
public void updateinterface(){
SharedPreferences sharedPref = PreferenceManager
.getDefaultSharedPreferences(this);
boolean REVERSE_ORIENTATION = sharedPref.getBoolean("reverse_landscape", false);
if(REVERSE_ORIENTATION) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
if(mBound) {
favButton.setText(getString(R.string.fav) + " [" + mService.isFavorite() + "]");
if(mService.isRecording()) {
recButton.setText(getString(R.string.restart));
} else {
recButton.setText(getString(R.string.start));
}
}
}
Button.OnClickListener favButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mBound) {
mService.toggleFavorite();
favButton.setText(getString(R.string.fav) + " [" + mService.isFavorite() + "]");
}
}};
Button.OnClickListener recButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
showHint(getString(R.string.longclick) + ": " + getString(R.string.preferences));
mService.RestartRecording(); //start/restart
}};
Button.OnClickListener focButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
if(mBound) {
mService.toggleFocus();
focButton.setText(getString(R.string.focus) + " [" + mFocusNames[mService.getFocusMode()] + "]");
}
}};
Button.OnClickListener nigButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
showHint(getString(R.string.longclick) + ": " + getString(R.string.timelapse));
if(mBound) {
mService.toggleNight();
}
}};
Button.OnClickListener flsButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mBound) {
mService.toggleFlash();
}
}};
Button.OnLongClickListener recButtonOnLongClickListener
= new Button.OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
mService.ChangeSurface(1, 1);
Intent myIntent = new Intent(getApplicationContext(), CosyDVRPreferenceActivity.class);
startActivity(myIntent);
//mService.ChangeSurface(mWidth, mHeight); //size will be returned with app focus
return true;
}};
Button.OnLongClickListener nigButtonOnLongClickListener
= new Button.OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
if(mBound) {
mService.toggleTimeLapse();
}
return true;
}};
Button.OnClickListener exiButtonOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
showHint(getString(R.string.longclick) + ": " + getString(R.string.exit));
}};
Button.OnLongClickListener exiButtonOnLongClickListener
= new Button.OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
if(mBound) {
unbindService(CosyDVR.this.mConnection);
CosyDVR.this.mBound = false;
}
stopService(new Intent(CosyDVR.this, BackgroundVideoRecorder.class));
CosyDVR.this.finish();
return true;
}};
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
BackgroundVideoRecorder.LocalBinder binder = (BackgroundVideoRecorder.LocalBinder) service;
mService = binder.getService();
mBound = true;
/*if(!mService.isRecording()){
//stopService(new Intent(CosyDVR.this, BackgroundVideoRecorder.class));
recButton.setText(getString(R.string.rec));
}else{
recButton.setText(getString(R.string.stop));
}*/
mService.ChangeSurface(mWidth, mHeight);
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
private IntentFilter filter = new IntentFilter("es.esy.CosyDVR.updateinterface");
private BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context c, Intent i) {
updateinterface();
}
};
}