summaryrefslogtreecommitdiff
path: root/native/android/avatar/src/main/java/com/ford/avarsdl/jsoncontroller/JSONVideoController.java
blob: 1af78f0b8c80616d55acc2d72bd7e54a88ed3fcf (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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package com.ford.avarsdl.jsoncontroller;

import java.io.File;
import android.content.SharedPreferences;
import android.util.Log;

import com.ford.avarsdl.activity.AvatarActivity;
import com.ford.avarsdl.downloader.DownloaderClient;
import com.ford.avarsdl.exception.SDCardException;
import com.ford.avarsdl.jsonparser.EVideoMethods;
import com.ford.avarsdl.util.Const;
import com.ford.avarsdl.util.ExtStorageUtils;
import com.ford.avarsdl.util.MessageConst;
import com.ford.avarsdl.util.NetUtils;
import com.ford.avarsdl.util.RPCConst;
import com.ford.avarsdl.util.Utils;
import com.ford.avarsdl.util.WebViewUtils;

/**
 * <b>File:</b> JSONVideoController.java <br>
 * <b>Description:</b> Controller that is responsible for video data
 * transferring through JSON RPC 2.0 protocol <br>
 * 
 * @author kbotnar
 * 
 */
public class JSONVideoController extends JSONController {

	/**
	 * Constructor
	 * 
	 * @param activity
	 *            : the application main activity
	 */
	public JSONVideoController(AvatarActivity activity) {
		super(RPCConst.CN_VIDEO);
		mActivity = activity;
	}

	/**
	 * Send notification to web UI with video progress bar position
	 * 
	 * @param position
	 *            : video progress bar position
	 */
	public void sendPositionNotification(int position) {
		String method = RPCConst.CN_VIDEO + "."
				+ EVideoMethods.positionChanged.toString();
		mJSONParser.putEmptyJSONObject();
		mJSONParser.putIntValue(RPCConst.TAG_VIDEO_CURRENT_POSITION, position);
		this.sendNotification(method, mJSONParser.getJSONObject());
	}

	/**
	 * Set started video duration
	 * 
	 * @param value
	 *            : video duration
	 */
	public void setVideoDuration(int value) {
		mVideoDuration = value;
	}

	// ===================================================================
	// private section
	// ===================================================================
	/**
	 * To show debug messages from current class
	 */
	private final boolean DEBUG = true;
	private final String TAG_NAME = JSONVideoController.class.getSimpleName();
	private AvatarActivity mActivity;
	/**
	 * Video file name in expansion file
	 */
	private String mVideoName;
	private int mVideoDuration = 0;
	private boolean mVideoIsStarted = false; // to prevent double start of video

	@Override
	protected void processRequest(String request) {
		logMsg("Process request");
		String result = processNotification(request);
		if (result != null) {
			mJSONParser.putJSONObject(request);
			sendResponse(mJSONParser.getId(), result);
		}
	}

	@Override
	protected String processNotification(String notification) {
		logMsg("Process notification : " + notification);
		mJSONParser.putJSONObject(notification);
		String method = mJSONParser.getMethod();
		method = method.substring(method.indexOf('.') + 1, method.length());
		String out = null;
		switch (EVideoMethods.valueOf(method)) {
		case start:
			mJSONParser.putJSONObject(mJSONParser.getParams());
			mVideoName = mJSONParser
					.getStringParam(RPCConst.TAG_VIDEO_FILE_NAME);
			double scale = mJSONParser.getDoubleParam(RPCConst.TAG_SCALE);
			mJSONParser.putJSONObject(mJSONParser
					.getJSONObjectParam(RPCConst.TAG_VIDEO_POSITION));
			int left = mJSONParser.getIntParam(RPCConst.TAG_VIDEO_POSITION_X);
			int top = mJSONParser.getIntParam(RPCConst.TAG_VIDEO_POSITION_Y);
			out = start(scale, left, top);
			break;
		case stop:
			out = stop();
			break;
		case pause:
			out = pause();
			break;
		case resume:
			out = resume();
			break;
		case setPositionPaused:
			mJSONParser.putJSONObject(mJSONParser.getParams());
			double pos = mJSONParser
					.getDoubleParam(RPCConst.TAG_VIDEO_POSITION);
			logMsg("POSITION = " + String.valueOf(pos));
			out = setPositionPaused(pos);
			break;
		case setPosition:
			mJSONParser.putJSONObject(mJSONParser.getParams());
			pos = mJSONParser.getDoubleParam(RPCConst.TAG_VIDEO_POSITION);
			logMsg("POSITION = " + String.valueOf(pos));
			out = setPosition(pos);
			break;
		case getPosition:
			out = getPosition();
			break;
		case stateChanged:
			out = stateChanged();
			break;
		case positionChanged:
			out = positionChanged();
			break;
		case setDragState:
			mJSONParser.putJSONObject(mJSONParser.getParams());
			boolean state = mJSONParser
					.getBooleanParam(RPCConst.TAG_VIDEO_DRAG_STATE);
			out = setDragState(state);
			break;
		default:
			mJSONParser.putEmptyJSONRPCObject();
			mJSONParser.putStringValue(RPCConst.TAG_ERROR,
					"VideoController does not support function : " + method);
			out = mJSONParser.getJSONObject();
			break;
		}
		return out;

	}

	@Override
	protected void processResponse(String response) {
		logMsg("Process response");
		if (!processRegistrationResponse(response)) {
			mJSONParser.putJSONObject(response);
			mRequestResponse = mJSONParser.getResult();
		}
	}

	/**
	 * Subscription on properties
	 */
	protected void subscribeToProperties() {
		subscribeTo("VideoPlayerClient.setDragState");
	}

	/**
	 * Start a video with parameters from web UI
	 * 
	 * @param scale
	 *            : video scale
	 * @param leftOffset
	 *            : left video offset
	 * @param topOffset
	 *            : top video offset
	 * @return result JSON object with video duration or error JSON object
	 */
	private String start(double scale, int leftOffset, int topOffset) {
		// check if expansion files are available
		SharedPreferences prefs = mActivity.getSharedPreferences(
				Const.SHPREF_DOWNLOADER_PREFS, 0);
		boolean expFielsAreValid = prefs.getBoolean(
				Const.SHPREF_MAIN_EXPFILE_VALID, false)
				&& prefs.getBoolean(Const.SHPREF_PATCH_EXPFILE_VALID, false);

		// additionally check if it is a welcome video
		if (expFielsAreValid
				|| mVideoName.toLowerCase().compareTo(
						Const.WELCOME_VIDEO_FILE_NAME) == 0) {
			if (!mVideoIsStarted) {
				mVideoIsStarted = true;
				logMsg("Play video " + mVideoName);
				Object obj[] = { mVideoName, scale, leftOffset, topOffset };
				Utils.sendMessageToHandler(Const.VIDEO_START, obj,
						mActivity.getMainHandler());
			}
			// wait for initializing video file and getting its duration
			while (mVideoDuration == 0) {
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			if (mVideoDuration >= 0) {
				mJSONParser.putEmptyJSONObject();
				mJSONParser.putIntValue(RPCConst._TAG_VIDEO_TOTAL_DURATION,
						mVideoDuration);
				return mJSONParser.getJSONObject();
			}
		}
		return findVideoPlayError();
	}

	/**
	 * Finds out what is wrong with video playing
	 * 
	 * @return error JSON object
	 */
	private String findVideoPlayError() {
		mJSONParser.putEmptyJSONObject();
		//put error value as an indicator for parser
		mJSONParser.putStringValue(RPCConst.TAG_ERROR, "");
		// get redownload count
		SharedPreferences prefs = mActivity.getSharedPreferences(
				Const.SHPREF_DOWNLOADER_PREFS, 0);
		int redownload_count = prefs.getInt(Const.SHPREF_REDOWNLOAD_COUNTER, 0);

		// no network connection
		if (!NetUtils.isOnline(mActivity)) {
			mJSONParser.putStringValue(RPCConst.TAG_ERROR_MESSAGE,
					MessageConst.NET_NET_NOT_AVAILABLE);
			mJSONParser.putIntValue(RPCConst.TAG_ERROR_CODE,
					Const.VC_NO_NETWORK_ERR_CODE);
		} // video files are still downloading
		else if (DownloaderClient.IS_PROCESSING_VIDEO) {
			mJSONParser.putStringValue(RPCConst.TAG_ERROR_MESSAGE,
					MessageConst.VC_VIDEO_IS_BEING_PROCESSED_ERR);
			mJSONParser.putIntValue(RPCConst.TAG_ERROR_CODE,
					Const.VC_VIDEO_IS_BEING_DOWNLOADED_ERR_CODE);
			mJSONParser.putIntValue(RPCConst.TAG_ERROR_DATA,
					DownloaderClient.DOWNLOADED_PERSENS);
			DownloaderClient.FINISH_NOTIFY = true;
		}// no space on SD card
		else if (DownloaderClient.SDCARD_IS_FULL) {
			mJSONParser.putStringValue(RPCConst.TAG_ERROR_MESSAGE,
					MessageConst.VC_NO_SPACE_ERR);
			mJSONParser.putIntValue(RPCConst.TAG_ERROR_CODE,
					Const.VC_NO_SPACE_ERR_CODE);
		}// video downloading count is exceeded
		else if (redownload_count < Const.DOWNLOADER_LAUNCH_MAX_NUMBER) {
			mJSONParser.putStringValue(RPCConst.TAG_ERROR_MESSAGE,
					MessageConst.VC_VIDEO_IS_CORRUPTED_ERR);
			mJSONParser.putIntValue(RPCConst.TAG_ERROR_CODE,
					Const.VC_VIDEO_IS_CORRUPTED_ERR_CODE);
		} // video is unavailable due to file corruption
		else {
			mJSONParser.putStringValue(RPCConst.TAG_ERROR_MESSAGE,
					MessageConst.VC_VIDEO_IS_UNAVAILABLE_ERR);
			mJSONParser.putIntValue(RPCConst.TAG_ERROR_CODE,
					Const.VC_VIDEO_IS_UNAVAILABLE_ERR_CODE);
		}
		return mJSONParser.getJSONObject();
	}

	/**
	 * stop video playing
	 * 
	 * @return null, because it is a notification
	 */
	private String stop() {
		logMsg("VideoStop");
		mVideoIsStarted = false;
		Utils.sendMessageToHandler(Const.VIDEO_STOP, null,
				mActivity.getMainHandler());
		mVideoDuration = 0;
		return null;
	}

	/**
	 * pause video playing
	 * 
	 * @return null, because it is a notification
	 */
	private String pause() {
		logMsg("VideoPause");
		Utils.sendMessageToHandler(Const.VIDEO_PAUSE, null,
				mActivity.getMainHandler());
		return null;
	}

	/**
	 * resume video playing
	 * 
	 * @return null, because it is a notification
	 */
	private String resume() {
		logMsg("VideoPlay");
		Utils.sendMessageToHandler(Const.VIDEO_PLAY, null,
				mActivity.getMainHandler());
		return null;
	}

	/**
	 * Set a new position of video while it is paused
	 * 
	 * @param position
	 *            : new position
	 * @return null, because it is a notification
	 */
	private String setPositionPaused(double position) {
		logMsg("VideoSetPositionPaused:" + position + " convert to=" + position);
		Utils.sendMessageToHandler(Const.VIDEO_SET_POSITION_PAUSED, position,
				mActivity.getMainHandler());
		return null;
	}

	/**
	 * Set a new position of video while it is playing
	 * 
	 * @param position
	 *            : new position
	 * @return null, because it is a notification
	 */
	private String setPosition(double position) {
		logMsg("VideoSetPosition:" + position + " covert to=" + position);
		Utils.sendMessageToHandler(Const.VIDEO_SET_POSITION, position,
				mActivity.getMainHandler());
		return null;
	}

	/**
	 * Get current position in seconds
	 * 
	 * @return current position in seconds
	 */
	private String getPosition() {
		double out = mActivity.videoGetPosition();
		logMsg("VideoGetPosition Position=" + out);
		return String.valueOf(out);
	}

	/**
	 * Stub for future
	 * 
	 * @return null
	 */
	private String stateChanged() {
		return null;
	}

	/**
	 * Stub for future
	 * 
	 * @return null
	 */
	private String positionChanged() {
		return null;
	}

	/**
	 * Disable and enable dragging in the application. It should be available
	 * only during video playing
	 * 
	 * @param activate
	 *            : activates dragging
	 * @return null, because it is a notification
	 */
	private String setDragState(boolean activate) {
		if (activate) {
			// allow dragging only if dragging video
			WebViewUtils.setOnTouchListener(mActivity,
					WebViewUtils.NONBLOCKING_ACTION_MOVE_MODE);
		} else {
			// disable dragging when stop scrolling video
			WebViewUtils.setOnTouchListener(mActivity,
					WebViewUtils.BLOCKING_ACTION_MOVE_MODE);
		}
		return null;
	}

	/**
	 * Check if file exists on SD card.
	 * 
	 * @param fileName
	 *            : file name
	 * @return true if file exists and false otherwise.
	 */
	public boolean isFileExist(String fileName) {
		if (fileName == null || "".equals(fileName.trim())) {
			return false;
		}
		try {

			File file = ExtStorageUtils.getFileOnSDCard(mActivity,
					fileName.toLowerCase());
			return file.exists();
		} catch (SDCardException e) {
			e.printStackTrace();
			return false;
		}
	}

	private void logMsg(String msg) {
		if (DEBUG && Const.DEBUG) {
			Log.i(TAG_NAME, msg);
		}
	}

}