/
settings.js
411 lines (337 loc) · 12.7 KB
/
settings.js
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
/*
* Mobile Logger. Record geotagged sensor values on a mobile device.
* Copyright (C) 2010 Robert Carlsen
*
* This file is part of Mobile Logger.
*
* Mobile Logger 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/>.
*
*/
/* Settings window
*/
// The Android can't do a grouped table layout but it does have sections.
// Also, I don't think that it can have controls in the table rows, can it?
//
var win = Titanium.UI.currentWindow;
var orangeColor = '#d56009';
// methods for creating custom rows:
function addControlRow(label,property,initialValue)
{
if(initialValue == null) { initialValue = false; }
var row = Ti.UI.createTableViewRow({height:50});
row.backgroundColor = '#fff';
// add a label to the left
// should be bold
var cellLabel = Ti.UI.createLabel({
text:label,
font:{fontSize:16,fontWeight:'bold'},
left:10
});
row.add(cellLabel);
// enable the property to be omitted
// TODO: use a type variable to create different styles of controls?
if(property != null){
// add a switch to the right
var sw = Ti.UI.createSwitch({
right:10,
value:Ti.App.Properties.getBool(property,initialValue)
});
// add a callback function to set application
// properties when the value is changed
sw.addEventListener('change', function(e)
{
// update the property with the state of the switch
Ti.App.Properties.setBool(property,e.value);
Ti.API.info('Property changed: '+property+', '+e.value);
});
row.add(sw);
}
row.className = 'control';
return row;
}
function addExportRow(label,property,valuesList,initialValue)
{
if(initialValue == null) { initialValue = false; }
var row = Ti.UI.createTableViewRow({height:50});
row.backgroundColor = '#fff';
// add a label to the left
// should be bold
var cellLabel = Ti.UI.createLabel({
text:label,
font:{fontSize:16,fontWeight:'bold'},
left:10
});
row.add(cellLabel);
// enable the property to be omitted
// TODO: use a type variable to create different styles of controls?
if(property != null){
row.hasChild = true;
row.value = Ti.App.Properties.getString(property,initialValue);
var cellValue = Ti.UI.createLabel({
text:valuesList[row.value],
font:{fontSize:14},
textAlign:'right',
right:20
});
row.add(cellValue);
// add an event listener to this row
row.addEventListener('click',function(e){
// push a table view with these valuesList
Ti.API.info('In the export table row click event');
var exportWin = Ti.UI.createWindow({
title:'Export Format',
backgroundColor: '#ccc',
barColor:orangeColor
});
var thisTable = Ti.UI.createTableView();
thisTable.style = Ti.UI.iPhone.TableViewStyle.GROUPED;
thisTable.backgroundColor = '#ccc';
var data = [];
for(var i in valuesList) {
if(valuesList.hasOwnProperty(i)){
var thisRow = Ti.UI.createTableViewRow({backgroundColor:'#fff'});
thisRow.title = valuesList[i];
thisRow.value = i;
// check the currently selected export format
if(row.value == thisRow.value) { thisRow.hasCheck = true; }
data.push(thisRow);
}
}
thisTable.setData(data);
thisTable.addEventListener('click',function(r){
Ti.API.info('In the export format window click event');
var rowValue = r.rowData.value;
// trying to get the parentTable to update.
cellValue.text = r.rowData.title;
row.value = rowValue;
Ti.App.Properties.setString(property,rowValue);
Ti.API.info('Set the property: '+property +' to: '+rowValue);
// deselect all rows in the table
var index = r.index;
var section = r.section;
setTimeout(function()
{
// reset checks
for (var i=0;i<section.rows.length;i++) {
section.rows[i].hasCheck = false;
}
// set current check
section.rows[index].hasCheck = true;
},250);
});
exportWin.add(thisTable);
Ti.API.info('Added export table to export window');
Titanium.UI.currentTab.open(exportWin,{animated:true});
Ti.API.info('Export format window should have opened');
});
}
row.className = 'export';
return row;
}
function addAboutRow(label,value)
{
if(label == null) { label = 'About'; }
var row = Ti.UI.createTableViewRow({height:50});
row.backgroundColor = '#fff';
row.hasChild = true;
row.header = '';
// add a label to the left
// should be bold
var cellLabel = Ti.UI.createLabel({
text:label,
font:{fontSize:16,fontWeight:'bold'},
left:10
});
row.add(cellLabel);
// add a child view
row.addEventListener('click',function(e){
Ti.API.info('In the about row click event');
// push a new window
var aboutWin = Ti.UI.createWindow({
title:label,
backgroundColor:'#ccc',
barColor:orangeColor
});
Ti.API.info('Created about window');
var aboutField = Ti.UI.createTextArea({
value:value,
//width:300,
//height:300,
//top:10,
//borderWidth:1,
//borderColor:'#999',
//borderRadius:10,
height:Ti.UI.FILL,
editable:false,
touchEnabled:true,
font:{fontSize:16},
scrollable:true
});
Ti.API.info('Created about field');
//Ti.API.info('Added about string to about field: '+value);
aboutWin.add(aboutField);
//aboutWin.open();//{modal:true});
Titanium.UI.currentTab.open(aboutWin,{animated:true});
Ti.API.info('Should have opened the about window');
});
row.className = 'aboutrow';
return row;
}
function addInfoRow(label,property)
{
var row = Ti.UI.createTableViewRow({height:50});
row.backgroundColor = '#fff';
// add a label to the left
// should be bold
var cellLabel = Ti.UI.createLabel({
text:label,
font:{fontSize:16,fontWeight:'bold'},
left:10
});
row.add(cellLabel);
// enable the property to be omitted
// TODO: use a type variable to create different styles of controls?
if(property != null){
row.hasChild = false;
row.value = property.toString();
var cellValue = Ti.UI.createLabel({
text:row.value,
font:{fontSize:14},
textAlign:'right',
right:20
});
row.add(cellValue);
}
row.selectionStyle = Titanium.UI.iPhone.TableViewCellSelectionStyle.NONE;
row.className = 'info';
return row;
}
// add export db file row
function addExportDbRow(label)
{
var row = Ti.UI.createTableViewRow({height:50});
row.backgroundColor = '#fff';
row.hasChild = true;
//row.header = '';
// add a label to the left
// should be bold
var cellLabel = Ti.UI.createLabel({
text:label,
font:{fontSize:16,fontWeight:'bold'},
left:10
});
row.add(cellLabel);
// get the db and list the size...this could be a memory killer
var logDB = Ti.Database.open("log.db");
var f = logDB.getFile();
Ti.API.info('db file exists: '+ ((f.exists) ? 'yes' : 'no')); // +' path: '+f.resolve());
var cellValue = Ti.UI.createLabel({
text:(f.read.size/1024) + ' kB',
font:{fontSize:14},
textAlign:'right',
right:20
});
row.add(cellValue);
function exportDBCallback(e) {
// the compression may take a while, disable the button
// until the compression is finished:
row.removeEventListener('click',exportDBCallback);
row.setSelectionStyle(Ti.UI.iPhone.TableViewCellSelectionStyle.NONE);
// this may take a while, create an indicator:
var activityIndicator = Ti.UI.createActivityIndicator({
style:Ti.UI.iPhone.ActivityIndicatorStyle.DARK,
right: (10 + cellValue.size.width + 20)
});
activityIndicator.show();
row.add(activityIndicator);
Ti.API.info('In the about row click event');
var emailDialog = Titanium.UI.createEmailDialog();
// alert if email is not supported (eg. don't have an e-mail acconut set up)
if(emailDialog.isSupported()) {
emailDialog.barColor = orangeColor;
emailDialog.subject = "Mobile Logger Database";
//emailDialog.toRecipients = ['foo@yahoo.com'];
emailDialog.messageBody = 'Attached is the sqlite database file from Mobile Logger.';
// Compress the newly created temp file
var zipFilePath = Ti.Compression.compressFile(f.path);
Ti.API.info('zip file path: '+zipFilePath);
if(zipFilePath) { // it was successful, attach this
emailDialog.addAttachment(Ti.Filesystem.getFile(zipFilePath));
}
else {
emailDialog.addAttachment(f);
}
emailDialog.open();
Ti.API.info('Email dialog should have opened');
}
else {
// alert
var dialog = Ti.UI.createAlertDialog({
title: 'Cannot Export',
message: 'Please set up a default e-mail account.',
ok: 'OK'
}).show();
}
// clean up:
activityIndicator.hide();
row.remove(activityIndicator);
row.setSelectionStyle(Ti.UI.iPhone.TableViewCellSelectionStyle.BLUE);
// add the event listener again:
row.addEventListener('click',exportDBCallback);
};
// add a child view
row.addEventListener('click',exportDBCallback);
row.className = 'exportdb';
return row;
}
// set up the settings table rows:
var inputData = [];
//networkRow.header = 'Network';
// network rows removed.
var resumeRow = addControlRow('Auto-Resume Logging','autoResume',false);
//resumeRow.header = 'Configuration';
resumeRow.header = '';
inputData.push(resumeRow);
inputData.push(addControlRow('Metric Units','useMetric'));
inputData.push(addControlRow('Monitor Sound Levels','monitorSound',true));
// should this actually modify the stored data in the db,
// or control whether or not the user ID field is included
// in uploaded or exported data?
var anonRow = addControlRow('Anonymous Export','omitDeviceID',false);
anonRow.header = '';
inputData.push(anonRow);
// trying to get the export to work
var exportRow = addExportRow('Export Format','exportFormat',{csv:'CSV',json:'JSON',gc:'Golden Cheetah',gpx:'GPX'},'csv');
inputData.push(exportRow);
inputData.push(addExportDbRow('Export DB'));
// Set up an about message
var aboutString =
'Log location, heading, speed, altitude, accelerometer, sound level, trip duration and distance. '+
'Export logs via e-mail in CSV, JSON or Golden Cheetah format.\n\n'+
'By default, logs contain a unique identifier for this device. It may be omitted by enabling the "Anonymous Export" option.\n\n'+
'This application has been released as open source software under the GPLv3. '+
'Source code is available at: http://github.com/rcarlsen/Mobile-Logger \n\n'+
'Created by Robert Carlsen in the Interactive Telecommunications Program at New York University.';
inputData.push(addAboutRow('About Mobile Logger',aboutString));
// add a version row:
var versionRow = addInfoRow("Application Version",Ti.App.version);
//versionRow.header = '';
inputData.push(versionRow);
// create the settings table view:
var tableView = Titanium.UI.createTableView({
data:inputData,
style:Titanium.UI.iPhone.TableViewStyle.GROUPED,
backgroundColor: '#ccc'
});
win.add(tableView);