summaryrefslogtreecommitdiff
path: root/SDL_Android/LivioTesterApp/src/com/livio/sdltester/dialogs/SdlAlertDialog.java
blob: d341c7a99aaba27b7999d53dc51b7944a144a646 (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
package com.livio.sdltester.dialogs;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.livio.sdl.SdlConstants;
import com.livio.sdl.SdlRequestFactory;
import com.livio.sdl.dialogs.BaseOkCancelDialog;
import com.livio.sdl.enums.SdlCommand;
import com.livio.sdl.utils.MathUtils;
import com.livio.sdl.viewhelpers.SeekBarCalculator;
import com.livio.sdltester.R;
import com.smartdevicelink.proxy.RPCRequest;

public class SdlAlertDialog extends BaseOkCancelDialog implements OnSeekBarChangeListener{

	private static final SdlCommand SYNC_COMMAND = SdlCommand.ALERT;
	private static final String DIALOG_TITLE = SYNC_COMMAND.toString();
	
	// a seekbar cannot do decimal points, so it currently ranges 0-50, which is then
	// divided by 10.0f to give us a number of seconds, rounded to 1/10 of a second.
	private static final float TENS_PLACE_DENOMINATOR = 10.0f;
	
	//set up your min & max time allowed here. we multiply by 10 since we're allowing 1/10th of a second selections
	private static final int MINIMUM_ALERT_TONE_TIME = (int) (SdlConstants.AlertConstants.ALERT_TIME_MINIMUM * TENS_PLACE_DENOMINATOR);
	private static final int MAXIMUM_ALERT_TONE_TIME = (int) (SdlConstants.AlertConstants.ALERT_TIME_MAXIMUM * TENS_PLACE_DENOMINATOR);

	//this is your default selection for tone duration.  again, divide by 10 for the actual time in seconds.
	private static final float DEFAULT_TONE_DURATION = 5.0f;  // 5.0 seconds
	
	private EditText et_alert_textToSpeak;
	private EditText et_alert_line1;
	private EditText et_alert_line2;
	private EditText et_alert_line3;
	
	private TextView tv_alert_toneDuration;
	
	private CheckBox check_alert_playTone;
	
	private SeekBar seek_alert_toneDuration;
	private SeekBarCalculator progressCalculator;
	
	private String secondsStr;
	
	//TODO - leaving these out for now - MRB
	//private CheckBox check_alert_includeSoftButtons;
	//private Button but_alert_includeSoftButtons;
	
	public SdlAlertDialog(Context context) {
		super(context, DIALOG_TITLE, R.layout.alert);
		setPositiveButton(okButtonListener);
		createDialog();
	}

	@Override
	protected void findViews(View parent) {
		secondsStr = context.getResources().getString(R.string.units_seconds);
		progressCalculator = new SeekBarCalculator(MINIMUM_ALERT_TONE_TIME, MAXIMUM_ALERT_TONE_TIME, TENS_PLACE_DENOMINATOR);
		
		et_alert_textToSpeak = (EditText) view.findViewById(R.id.et_alert_textToSpeak);
		et_alert_line1 = (EditText) view.findViewById(R.id.et_alert_line1);
		et_alert_line2 = (EditText) view.findViewById(R.id.et_alert_line2);
		et_alert_line3 = (EditText) view.findViewById(R.id.et_alert_line3);
		
		tv_alert_toneDuration = (TextView) view.findViewById(R.id.tv_alert_toneDuration);
		
		seek_alert_toneDuration = (SeekBar) view.findViewById(R.id.seek_alert_toneDuration);
		seek_alert_toneDuration.setMax(progressCalculator.getMaxProgress());
		seek_alert_toneDuration.setProgress(progressCalculator.calculateProgress(DEFAULT_TONE_DURATION));
		seek_alert_toneDuration.setOnSeekBarChangeListener(this);
		
		check_alert_playTone = (CheckBox) view.findViewById(R.id.check_alert_playTone);
		
		//make initial updates to the UI using default values
		updateProgressText(DEFAULT_TONE_DURATION);
		
		//TODO - leaving these out for now - MRB
		//check_alert_includeSoftButtons = (CheckBox) view.findViewById(R.id.check_alert_includeSoftButtons);
		//
		//but_alert_includeSoftButtons = (Button) view.findViewById(R.id.but_alert_includeSoftButtons);
		//TODO - add on click listener to this button.
	}

	private void updateProgressText(float progress){
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append(progress);
		strBuilder.append(secondsStr);
		tv_alert_toneDuration.setText(strBuilder.toString());
	}
	
	// dialog button listsners
	private final DialogInterface.OnClickListener okButtonListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			String textToSpeak = et_alert_textToSpeak.getText().toString();
			String line1   = et_alert_line1.getText().toString();
			String line2   = et_alert_line2.getText().toString();
			String line3   = et_alert_line3.getText().toString();
			int toneDurationInS = (int) progressCalculator.calculateValue(seek_alert_toneDuration.getProgress());
			int toneDurationInMs = MathUtils.convertSecsToMillisecs(toneDurationInS);
			boolean playTone = check_alert_playTone.isChecked();
			
			if(textToSpeak.equals("")){
				textToSpeak = null;
			}
			if(line1.equals("")){
				line1 = " ";
			}
			if(line2.equals("")){
				line2 = " ";
			}
			if(line3.equals("")){
				line3 = " ";
			}
			
			RPCRequest result = SdlRequestFactory.alert(textToSpeak, line1, line2, line3, playTone, toneDurationInMs);
			notifyListener(result);
		}
	};

	/*
	 * On Seek Bar Changed Listener Methods
	 */
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		updateProgressText(progressCalculator.calculateValue(progress));
	}

	@Override public void onStartTrackingTouch(SeekBar seekBar) {}
	@Override public void onStopTrackingTouch(SeekBar seekBar) {}
}