1 |
|
package co.harlequinmettle.healthfoodconcepts; |
2 |
|
|
3 |
|
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
4 |
|
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
5 |
|
import static android.widget.LinearLayout.VERTICAL; |
6 |
|
|
7 |
|
import java.io.FileInputStream; |
8 |
|
import java.io.FileOutputStream; |
9 |
|
import java.io.IOException; |
10 |
|
import java.io.ObjectInputStream; |
11 |
|
import java.io.ObjectOutputStream; |
12 |
|
import java.util.ArrayList; |
13 |
|
import java.util.Arrays; |
14 |
|
import java.util.HashMap; |
15 |
|
import java.util.HashSet; |
16 |
|
import java.util.LinkedHashMap; |
17 |
|
import java.util.Map.Entry; |
18 |
|
import java.util.TreeMap; |
19 |
|
|
20 |
|
import android.app.ActionBar; |
21 |
|
import android.app.Activity; |
22 |
|
import android.content.Context; |
23 |
|
import android.os.Build; |
24 |
|
import android.os.Bundle; |
25 |
|
import android.os.Handler; |
26 |
|
import android.util.DisplayMetrics; |
27 |
|
import android.view.Gravity; |
28 |
|
import android.view.MotionEvent; |
29 |
|
import android.view.View; |
30 |
|
import android.view.View.OnTouchListener; |
31 |
|
import android.view.ViewGroup; |
32 |
|
import android.widget.HorizontalScrollView; |
33 |
|
import android.widget.LinearLayout; |
34 |
|
import android.widget.ProgressBar; |
35 |
|
import android.widget.Toast; |
36 |
|
|
|
|
| 59,7% |
Uncovered Elements: 192 (476) |
Complexity: 109 |
Complexity Density: 0,31 |
|
37 |
|
public class WhatYouEat extends Activity implements OnTouchListener, |
38 |
|
I_Preferences, HasServingSizeInfo { |
39 |
|
|
40 |
|
static Thread loadingThread; |
41 |
|
static HorizontalScrollView application; |
42 |
|
static LinearLayout appAccess; |
43 |
|
static MenuScroller intro, settings; |
44 |
|
static ArrayList<TreeMap<Integer, Boolean>> allMyFoods = new ArrayList<TreeMap<Integer, Boolean>>(); |
45 |
|
|
46 |
|
|
47 |
|
static HashMap<Integer, Float> myFoodQuantities = new HashMap<Integer, Float>(); |
48 |
|
static HashMap<Integer, Integer> myFoodUnitIDs = new HashMap<Integer, Integer>(); |
49 |
|
static HashMap<Integer, String> myFoodUnits = new HashMap<Integer, String>(); |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
static boolean[] myGoodNutrients; |
54 |
|
static float[] goodNutritionGoals; |
55 |
|
|
56 |
|
static Context ctx; |
57 |
|
|
58 |
|
static boolean _loaded = false; |
59 |
|
|
60 |
|
static final int USING_KCAL = 200000; |
61 |
|
static final int USING_SERVING = 400000; |
62 |
|
static final int USING_GRAMS = 800000; |
63 |
|
|
64 |
|
static final int USE_ALL_FOODS = 10000; |
65 |
|
static final int USE_MY_FOOD_GROUPS = 5000; |
66 |
|
static final int USE_MY_FOODS = 500; |
67 |
|
static final int USE_ONE_FOOD_GROUP = 128; |
68 |
|
|
69 |
|
static final int SEARCH_TYPE_SUM = 10000002; |
70 |
|
static final int SEARCH_TYPE_PRODUCT = 10000001; |
71 |
|
|
72 |
|
|
73 |
|
static final int VIEW_VERTICAL = 515151661; |
74 |
|
|
75 |
|
static int myFoods_View = VIEW_VERTICAL; |
76 |
|
static int Foods_Search = USE_ALL_FOODS; |
77 |
|
static int Nutrient_Measure = USING_GRAMS; |
78 |
|
static int Food_Group = 21; |
79 |
|
static int Search_Type = SEARCH_TYPE_PRODUCT; |
80 |
|
|
81 |
|
static boolean longLoading = false; |
82 |
|
static boolean searching = false; |
83 |
|
static boolean calculatingStats = false; |
84 |
|
static boolean statsLoaded = false; |
85 |
|
static float sw, sh; |
86 |
|
static float[][] db = new float[130][8194]; |
87 |
|
static final int FOOD_COUNT = 8194; |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
static String[] foods = new String[8194]; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
static String[] foodGroups = new String[25]; |
97 |
|
|
98 |
|
static String[] nutrients = new String[130]; |
99 |
|
static String[] units = new String[130]; |
100 |
|
|
101 |
|
static int[][] foodsByGroup = new int[25][]; |
102 |
|
|
103 |
|
static String[] searchResults; |
104 |
|
static int[] foodCodeResults; |
105 |
|
static final int PROGRESS_BAR_ID = 100010001; |
106 |
|
static int[] searchTheseFoods; |
107 |
|
|
108 |
|
private static final String DATA_100G = "database_object"; |
109 |
|
public static final int USING_NOT_ZEROS = 1024; |
110 |
|
public static final int USING_ZEROS = 2048; |
111 |
|
public static int DATA_CHOICES = USING_NOT_ZEROS; |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
GeneralLoadingThread a, b, c; |
117 |
|
public static boolean[] MY_FOOD_GROUPS; |
118 |
|
public static boolean[] MY_NUTRIENTS; |
119 |
|
static ProgressBar pb; |
120 |
|
static Handler mHandler = new Handler(); |
121 |
|
|
122 |
|
static HashMap<Integer, StatTool> nutritionStats = new HashMap<Integer, StatTool>(); |
123 |
|
static HashMap<Integer, float[]> highlightFactors = new HashMap<Integer, float[]>(); |
124 |
|
|
125 |
|
|
126 |
|
static int[] optimalServingId = new int[FOOD_COUNT]; |
127 |
|
|
128 |
|
static int[] WITH_SERVING_SIZE = HAS_SERVING_INFO; |
129 |
|
|
130 |
|
static String[][] oddUnits = new String[FOOD_COUNT][]; |
131 |
|
|
132 |
|
|
133 |
|
static float[][] metricConversion = new float[FOOD_COUNT][]; |
134 |
|
|
135 |
|
static float[][] quantityFactor = new float[FOOD_COUNT][]; |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
static final int TEXT_SMALL = 21; |
140 |
|
static final int TEXT_MED = 25; |
141 |
|
static final int TEXT_LARGE = 35; |
142 |
|
static boolean needToSetSearchFoods; |
143 |
|
public static int save_db = 0; |
144 |
|
static WhatYouEat _MyApp; |
145 |
|
|
|
|
| 94,8% |
Uncovered Elements: 3 (58) |
Complexity: 6 |
Complexity Density: 0,12 |
|
146 |
1
|
@Override... |
147 |
|
protected void onCreate(Bundle savedInstanceState) { |
148 |
1
|
super.onCreate(savedInstanceState); |
149 |
1
|
setDefaults(); |
150 |
1
|
_MyApp = this; |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
1
|
DisplayMetrics metrics = new DisplayMetrics(); |
158 |
1
|
getWindowManager().getDefaultDisplay().getMetrics(metrics); |
159 |
1
|
sw = metrics.widthPixels; |
160 |
1
|
sh = metrics.heightPixels; |
161 |
|
|
162 |
1
|
ctx = this; |
163 |
|
|
164 |
1
|
application = new HorizontalScrollView(this); |
165 |
1
|
appAccess = new LinearLayout(this); |
166 |
|
|
167 |
|
|
168 |
1
|
Nutrient_Measure = restorePreference("SEARCHUNITS"); |
169 |
|
|
170 |
1
|
Foods_Search = restorePreference("SEARCHFOODS"); |
171 |
|
|
172 |
1
|
save_db = restorePreference("SAVEDB"); |
173 |
|
|
174 |
1
|
restoreGuidelines(); |
175 |
|
|
176 |
1
|
restoreMyGoodNutrients(); |
177 |
|
|
178 |
1
|
restoreMyFoodUnitIds(); |
179 |
|
|
180 |
1
|
if (!loadMyFoodGroups()) |
181 |
1
|
MY_FOOD_GROUPS = DEFAULT_FOOD_GROUPS; |
182 |
|
|
183 |
1
|
if (!loadMyNutrients()) |
184 |
1
|
MY_NUTRIENTS = DEFAULT_NUTRIENTS; |
185 |
|
|
186 |
8195
|
for (int i = 0; i < optimalServingId.length; i++) { |
187 |
8194
|
optimalServingId[i] = -1; |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
1
|
a = new GeneralLoadingThread(this, nutrients, units, "nutr.txt"); |
192 |
|
|
193 |
1
|
b = new GeneralLoadingThread(this, foodGroups, "group.txt"); |
194 |
|
|
195 |
1
|
c = new GeneralLoadingThread(this, foods, foodsByGroup, "food.txt", |
196 |
|
GeneralLoadingThread.FOOD); |
197 |
|
|
198 |
|
|
199 |
1
|
new Thread(a).start(); |
200 |
1
|
new Thread(b).start(); |
201 |
1
|
new Thread(c).start(); |
202 |
|
|
203 |
1
|
new Thread(new LoadWeightsConversion()).start(); |
204 |
|
|
205 |
|
|
206 |
|
|
207 |
1
|
_loaded = false; |
208 |
1
|
ObjectLoadingThread objLoader = new ObjectLoadingThread(this, db); |
209 |
1
|
loadingThread = new Thread(objLoader); |
210 |
1
|
loadingThread.start(); |
211 |
|
|
212 |
1
|
setFoodsIds(); |
213 |
|
|
214 |
1
|
restoreQuantities(); |
215 |
1
|
restoreUnits(); |
216 |
|
|
217 |
|
|
218 |
1
|
pb = new ProgressBar(this, null, |
219 |
|
android.R.attr.progressBarStyleHorizontal); |
220 |
1
|
pb.setId(PROGRESS_BAR_ID); |
221 |
1
|
intro = new MenuScroller(this, pb, 0); |
222 |
|
|
223 |
1
|
appAccess.addView(intro); |
224 |
|
|
225 |
1
|
application.addView(appAccess); |
226 |
|
|
227 |
1
|
setContentView(application); |
228 |
|
|
229 |
|
|
230 |
1
|
Toast mToast = Toast |
231 |
|
.makeText( |
232 |
|
this, |
233 |
|
"\n\n\n\n\nPlease adjust some settings while database loads\n\n\n\n\n", |
234 |
|
1); |
235 |
1
|
mToast.setGravity(Gravity.TOP, 0, 50); |
236 |
|
|
237 |
1
|
mToast.show(); |
238 |
1
|
if (!loadMyFoods()) |
239 |
26
|
for (int i = 0; i < 25; i++) { |
240 |
25
|
allMyFoods.add(new TreeMap<Integer, Boolean>()); |
241 |
|
} |
242 |
|
|
243 |
|
} |
244 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
245 |
0
|
public void resetStaticContext() {... |
246 |
0
|
ctx = this; |
247 |
|
} |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
249 |
1
|
private void setDefaults() {... |
250 |
8195
|
for (int i = 0; i < FOOD_COUNT; i++) { |
251 |
8194
|
oddUnits[i] = new String[1]; |
252 |
8194
|
oddUnits[i][0] = "grams"; |
253 |
8194
|
metricConversion[i] = new float[1]; |
254 |
8194
|
metricConversion[i][0] = 100; |
255 |
8194
|
quantityFactor[i] = new float[1]; |
256 |
8194
|
quantityFactor[i][0] = 100; |
257 |
|
} |
258 |
|
} |
259 |
|
|
|
|
| 58,3% |
Uncovered Elements: 5 (12) |
Complexity: 3 |
Complexity Density: 0,3 |
|
260 |
1
|
private void restoreMyGoodNutrients() {... |
261 |
1
|
int def = 0; |
262 |
1
|
boolean[] x = null; |
263 |
1
|
try { |
264 |
1
|
FileInputStream fis = ctx.openFileInput("GOODNUTR"); |
265 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
266 |
0
|
x = (boolean[]) objin.readObject(); |
267 |
0
|
objin.close(); |
268 |
|
|
269 |
|
} catch (Exception ioe) { |
270 |
|
} |
271 |
1
|
if (x != null) |
272 |
0
|
myGoodNutrients = x; |
273 |
|
else |
274 |
1
|
myGoodNutrients = DEFAULT_GOOD_NUTRIENTS.clone(); |
275 |
|
} |
276 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0,25 |
|
277 |
1
|
public static String[] getMyFoodsAsTextArray() {... |
278 |
1
|
ArrayList<String> foodListConstruct = new ArrayList<String>(); |
279 |
1
|
for (TreeMap<Integer, Boolean> foodi : WhatYouEat.allMyFoods) { |
280 |
25
|
for (Integer i : foodi.keySet()) { |
281 |
3
|
foodListConstruct.add(foods[i]); |
282 |
|
} |
283 |
|
} |
284 |
1
|
String[] fd = new String[foodListConstruct.size()]; |
285 |
4
|
for (int i = 0; i < fd.length; i++) { |
286 |
3
|
fd[i] = foodListConstruct.get(i); |
287 |
|
} |
288 |
1
|
return fd; |
289 |
|
} |
290 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0,25 |
|
291 |
1
|
public static int[] getMyFoodsIdsArray() {... |
292 |
1
|
ArrayList<Integer> foodIdsConstruct = new ArrayList<Integer>(); |
293 |
1
|
for (TreeMap<Integer, Boolean> foodi : WhatYouEat.allMyFoods) { |
294 |
25
|
for (Integer i : foodi.keySet()) { |
295 |
3
|
foodIdsConstruct.add(i); |
296 |
|
} |
297 |
|
} |
298 |
1
|
int[] ids = new int[foodIdsConstruct.size()]; |
299 |
4
|
for (int i = 0; i < ids.length; i++) { |
300 |
3
|
ids[i] = foodIdsConstruct.get(i); |
301 |
|
} |
302 |
1
|
return ids; |
303 |
|
} |
304 |
|
|
|
|
| 54,1% |
Uncovered Elements: 17 (37) |
Complexity: 9 |
Complexity Density: 0,31 |
|
305 |
9
|
public static void setFoodsIds() {... |
306 |
9
|
int[] theseFoods = null; |
307 |
9
|
switch (Foods_Search) { |
308 |
1
|
case USE_ALL_FOODS: |
309 |
1
|
theseFoods = new int[8194]; |
310 |
8195
|
for (int i = 0; i < 8194; i++) |
311 |
8194
|
theseFoods[i] = i; |
312 |
1
|
break; |
313 |
0
|
case USE_MY_FOODS: |
314 |
0
|
ArrayList<Integer> getMySelectFoods = new ArrayList<Integer>(); |
315 |
|
|
316 |
0
|
for (TreeMap<Integer, Boolean> amf : allMyFoods) { |
317 |
0
|
for (Entry<Integer, Boolean> ent : amf.entrySet()) { |
318 |
0
|
if (ent.getValue()) |
319 |
0
|
getMySelectFoods.add(ent.getKey()); |
320 |
|
} |
321 |
|
} |
322 |
0
|
theseFoods = new int[getMySelectFoods.size()]; |
323 |
0
|
for (int i = 0; i < theseFoods.length; i++) |
324 |
0
|
theseFoods[i] = getMySelectFoods.get(i); |
325 |
|
|
326 |
0
|
break; |
327 |
6
|
case USE_MY_FOOD_GROUPS: |
328 |
6
|
if (_loaded) |
329 |
5
|
theseFoods = constructUniqueValuesFromMyFoodGroups(); |
330 |
|
else |
331 |
1
|
needToSetSearchFoods = true; |
332 |
6
|
break; |
333 |
2
|
case USE_ONE_FOOD_GROUP: |
334 |
2
|
theseFoods = foodsByGroup[Food_Group]; |
335 |
2
|
break; |
336 |
0
|
default: |
337 |
0
|
theseFoods = foodsByGroup[Food_Group]; |
338 |
0
|
break; |
339 |
|
} |
340 |
|
|
341 |
9
|
searchTheseFoods = theseFoods; |
342 |
|
} |
343 |
|
|
|
|
| 25% |
Uncovered Elements: 9 (12) |
Complexity: 5 |
Complexity Density: 0,42 |
|
344 |
10
|
public static String getFoodsSearchDescription() {... |
345 |
|
|
346 |
10
|
switch (Foods_Search) { |
347 |
0
|
case USE_ALL_FOODS: |
348 |
0
|
return "all foods in database"; |
349 |
|
|
350 |
0
|
case USE_MY_FOODS: |
351 |
0
|
return "My Foods"; |
352 |
10
|
case USE_MY_FOOD_GROUPS: |
353 |
10
|
return "My Food Groups"; |
354 |
0
|
case USE_ONE_FOOD_GROUP: |
355 |
0
|
return foodGroups[Food_Group]; |
356 |
0
|
default: |
357 |
0
|
break; |
358 |
|
} |
359 |
|
|
360 |
0
|
return ""; |
361 |
|
} |
362 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 3 |
Complexity Density: 0,3 |
|
363 |
6
|
private static int[] constructUniqueValuesFromMyFoodGroups() {... |
364 |
6
|
HashSet<Integer> uniqueValues = new HashSet<Integer>(); |
365 |
156
|
for (int i = 0; i < MY_FOOD_GROUPS.length; i++) { |
366 |
150
|
if (MY_FOOD_GROUPS[i]) { |
367 |
109
|
for (int j : foodsByGroup[i]) { |
368 |
39369
|
uniqueValues.add(j); |
369 |
|
} |
370 |
|
} |
371 |
|
} |
372 |
6
|
int counter = 0; |
373 |
6
|
int[] uniques = new int[uniqueValues.size()]; |
374 |
6
|
for (int i : uniqueValues) { |
375 |
39369
|
uniques[counter++] = i; |
376 |
|
} |
377 |
6
|
return uniques; |
378 |
|
} |
379 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 3 |
Complexity Density: 0,27 |
|
380 |
0
|
private static int[] constructUniqueValuesFromMyFoods() {... |
381 |
0
|
HashSet<Integer> uniqueValues = new HashSet<Integer>(); |
382 |
0
|
for (int i = 0; i < allMyFoods.size(); i++) { |
383 |
0
|
TreeMap<Integer, Boolean> perGroup = allMyFoods.get(i); |
384 |
0
|
if (MY_FOOD_GROUPS[i]) { |
385 |
0
|
for (int j : perGroup.keySet()) { |
386 |
0
|
uniqueValues.add(j); |
387 |
|
} |
388 |
|
} |
389 |
|
} |
390 |
0
|
int counter = 0; |
391 |
0
|
int[] uniques = new int[uniqueValues.size()]; |
392 |
0
|
for (int i : uniqueValues) { |
393 |
0
|
uniques[counter++] = i; |
394 |
|
} |
395 |
0
|
return uniques; |
396 |
|
} |
397 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
398 |
25
|
public static int[] getFoodSearchIds() {... |
399 |
25
|
return searchTheseFoods; |
400 |
|
} |
401 |
|
|
402 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
403 |
6
|
public static void calculateHighlightNumbers() {... |
404 |
6
|
new Thread(new SimpleStatsBuilder()).start(); |
405 |
|
} |
406 |
|
|
|
|
| 75% |
Uncovered Elements: 4 (16) |
Complexity: 4 |
Complexity Density: 0,33 |
|
407 |
3
|
private int restorePreference(String string) {... |
408 |
3
|
int def = 0; |
409 |
3
|
try { |
410 |
3
|
FileInputStream fis = this.openFileInput(string); |
411 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
412 |
0
|
def = (Integer) objin.readObject(); |
413 |
0
|
objin.close(); |
414 |
|
|
415 |
|
} catch (Exception ioe) { |
416 |
3
|
if (string.equals("SEARCHUNITS")) |
417 |
1
|
return WhatYouEat.USING_GRAMS; |
418 |
2
|
else if (string.equals("SEARCHFOODS")) |
419 |
1
|
return WhatYouEat.USE_MY_FOOD_GROUPS; |
420 |
|
else |
421 |
1
|
return 0; |
422 |
|
} |
423 |
0
|
return def; |
424 |
|
} |
425 |
|
|
|
|
| 58,3% |
Uncovered Elements: 5 (12) |
Complexity: 3 |
Complexity Density: 0,3 |
|
426 |
1
|
static void restoreGuidelines() {... |
427 |
1
|
int def = 0; |
428 |
1
|
float[] x = null; |
429 |
1
|
try { |
430 |
1
|
FileInputStream fis = ctx.openFileInput("GOODNUTRGOALS"); |
431 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
432 |
0
|
x = (float[]) objin.readObject(); |
433 |
0
|
objin.close(); |
434 |
|
|
435 |
|
} catch (Exception ioe) { |
436 |
|
} |
437 |
1
|
if (x != null) |
438 |
0
|
goodNutritionGoals = x; |
439 |
|
else |
440 |
1
|
goodNutritionGoals = DEFAULT_NUTR_GOALS.clone(); |
441 |
|
} |
442 |
|
|
|
|
| 54,5% |
Uncovered Elements: 5 (11) |
Complexity: 3 |
Complexity Density: 0,33 |
|
443 |
1
|
private void restoreUnits() {... |
444 |
1
|
int def = 0; |
445 |
1
|
HashMap<Integer, String> x = null; |
446 |
1
|
try { |
447 |
1
|
FileInputStream fis = this.openFileInput("UNITYMAP"); |
448 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
449 |
0
|
x = (HashMap<Integer, String>) objin.readObject(); |
450 |
0
|
objin.close(); |
451 |
|
|
452 |
|
} catch (Exception ioe) { |
453 |
|
} |
454 |
1
|
if (x != null) |
455 |
0
|
myFoodUnits = x; |
456 |
|
} |
457 |
|
|
|
|
| 54,5% |
Uncovered Elements: 5 (11) |
Complexity: 3 |
Complexity Density: 0,33 |
|
458 |
1
|
private void restoreMyFoodUnitIds() {... |
459 |
1
|
int def = 0; |
460 |
1
|
HashMap<Integer, Integer> x = null; |
461 |
1
|
try { |
462 |
1
|
FileInputStream fis = this.openFileInput("UNITIDMAP"); |
463 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
464 |
0
|
x = (HashMap<Integer, Integer>) objin.readObject(); |
465 |
0
|
objin.close(); |
466 |
|
|
467 |
|
} catch (Exception ioe) { |
468 |
|
} |
469 |
1
|
if (x != null) |
470 |
0
|
myFoodUnitIDs = x; |
471 |
|
} |
472 |
|
|
|
|
| 54,5% |
Uncovered Elements: 5 (11) |
Complexity: 3 |
Complexity Density: 0,33 |
|
473 |
1
|
private void restoreQuantities() {... |
474 |
1
|
int def = 0; |
475 |
1
|
HashMap<Integer, Float> x = null; |
476 |
1
|
try { |
477 |
1
|
FileInputStream fis = this.openFileInput("QUANTITYMAP"); |
478 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
479 |
0
|
x = (HashMap<Integer, Float>) objin.readObject(); |
480 |
0
|
objin.close(); |
481 |
|
|
482 |
|
} catch (Exception ioe) { |
483 |
|
} |
484 |
1
|
if (x != null) |
485 |
0
|
myFoodQuantities = x; |
486 |
|
} |
487 |
|
|
|
|
| 42,9% |
Uncovered Elements: 4 (7) |
Complexity: 2 |
Complexity Density: 0,29 |
|
488 |
1
|
public boolean loadMyFoodGroups() {... |
489 |
1
|
try { |
490 |
|
|
491 |
1
|
FileInputStream fis = this.openFileInput("MYFOODGROUPS"); |
492 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
493 |
0
|
MY_FOOD_GROUPS = (boolean[]) objin.readObject(); |
494 |
0
|
objin.close(); |
495 |
|
|
496 |
|
} catch (Exception ioe) { |
497 |
1
|
return false; |
498 |
|
} |
499 |
0
|
return true; |
500 |
|
} |
501 |
|
|
|
|
| 42,9% |
Uncovered Elements: 4 (7) |
Complexity: 2 |
Complexity Density: 0,29 |
|
502 |
1
|
public boolean loadMyFoods() {... |
503 |
1
|
try { |
504 |
|
|
505 |
1
|
FileInputStream fis = this.openFileInput("MYFOODS"); |
506 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
507 |
0
|
allMyFoods = (ArrayList<TreeMap<Integer, Boolean>>) objin |
508 |
|
.readObject(); |
509 |
0
|
objin.close(); |
510 |
|
|
511 |
|
} catch (Exception ioe) { |
512 |
1
|
return false; |
513 |
|
} |
514 |
0
|
return true; |
515 |
|
} |
516 |
|
|
|
|
| 42,9% |
Uncovered Elements: 4 (7) |
Complexity: 2 |
Complexity Density: 0,29 |
|
517 |
1
|
public boolean loadMyNutrients() {... |
518 |
1
|
try { |
519 |
|
|
520 |
1
|
FileInputStream fis = this.openFileInput("MYNUTRIENTS"); |
521 |
0
|
ObjectInputStream objin = new ObjectInputStream(fis); |
522 |
0
|
MY_NUTRIENTS = (boolean[]) objin.readObject(); |
523 |
0
|
objin.close(); |
524 |
|
|
525 |
|
} catch (Exception ioe) { |
526 |
1
|
return false; |
527 |
|
} |
528 |
0
|
return true; |
529 |
|
} |
530 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0,33 |
|
531 |
46
|
public static void saveObject(Object OBJ, String obLocatorName) {... |
532 |
46
|
try { |
533 |
46
|
FileOutputStream fos = ctx.openFileOutput(obLocatorName, |
534 |
|
Context.MODE_PRIVATE); |
535 |
46
|
ObjectOutputStream objout = new ObjectOutputStream(fos); |
536 |
46
|
objout.writeObject(OBJ); |
537 |
46
|
objout.flush(); |
538 |
46
|
objout.close(); |
539 |
|
|
540 |
|
} catch (IOException ioe) { |
541 |
|
} |
542 |
|
} |
543 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
544 |
1
|
@Override... |
545 |
|
public void onPause() { |
546 |
1
|
super.onPause(); |
547 |
|
|
548 |
|
|
549 |
|
|
550 |
|
|
551 |
|
|
552 |
|
|
553 |
|
} |
554 |
|
|
555 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
|
556 |
0
|
public LinearLayout basicLinearLayout() {... |
557 |
0
|
LinearLayout basic = new LinearLayout(this); |
558 |
0
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( |
559 |
|
MATCH_PARENT, WRAP_CONTENT); |
560 |
0
|
basic.setLayoutParams(params); |
561 |
0
|
basic.setOrientation(VERTICAL); |
562 |
0
|
return basic; |
563 |
|
} |
564 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 6 |
Complexity Density: 0,43 |
|
565 |
0
|
@Override... |
566 |
|
public boolean onTouch(View v, MotionEvent event) { |
567 |
|
|
568 |
0
|
float xDown = event.getX(); |
569 |
0
|
float yDown = event.getY(); |
570 |
0
|
switch (event.getActionMasked()) { |
571 |
0
|
case MotionEvent.ACTION_POINTER_DOWN: |
572 |
0
|
break; |
573 |
0
|
case MotionEvent.ACTION_POINTER_UP: |
574 |
0
|
break; |
575 |
0
|
case MotionEvent.ACTION_MOVE: |
576 |
0
|
break; |
577 |
0
|
case MotionEvent.ACTION_DOWN: |
578 |
|
|
579 |
0
|
break; |
580 |
0
|
case MotionEvent.ACTION_UP: |
581 |
|
|
582 |
0
|
break; |
583 |
|
} |
584 |
0
|
return true; |
585 |
|
} |
586 |
|
|
|
|
| 57,7% |
Uncovered Elements: 47 (111) |
Complexity: 27 |
Complexity Density: 0,34 |
|
587 |
4
|
public static void setSearchResultsFrom(String searchWord, int groups_search) {... |
588 |
4
|
searching = true; |
589 |
4
|
if (searchWord.length() < 3) { |
590 |
1
|
searchResults = new String[1]; |
591 |
1
|
foodCodeResults = new int[1]; |
592 |
1
|
searchResults[0] = "Water, tap, drinking"; |
593 |
1
|
foodCodeResults[0] = 8081; |
594 |
1
|
return; |
595 |
|
} |
596 |
3
|
ArrayList<String> results = new ArrayList<String>(); |
597 |
3
|
ArrayList<Long> resultsID = new ArrayList<Long>(); |
598 |
|
|
599 |
|
|
600 |
3
|
int[] foodIDsToSearch; |
601 |
|
|
602 |
3
|
switch (groups_search) { |
603 |
0
|
case USE_ALL_FOODS: |
604 |
0
|
foodIDsToSearch = new int[FOOD_COUNT]; |
605 |
0
|
for (int i = 0; i < FOOD_COUNT; i++) { |
606 |
0
|
foodIDsToSearch[i] = i; |
607 |
|
} |
608 |
0
|
break; |
609 |
1
|
case USE_MY_FOOD_GROUPS: |
610 |
1
|
foodIDsToSearch = constructUniqueValuesFromMyFoodGroups(); |
611 |
1
|
break; |
612 |
0
|
case USE_MY_FOODS: |
613 |
0
|
foodIDsToSearch = constructUniqueValuesFromMyFoods(); |
614 |
0
|
break; |
615 |
0
|
case USE_ONE_FOOD_GROUP: |
616 |
0
|
foodIDsToSearch = foodsByGroup[Food_Group]; |
617 |
0
|
break; |
618 |
2
|
default: |
619 |
|
|
620 |
2
|
foodIDsToSearch = foodsByGroup[groups_search]; |
621 |
2
|
break; |
622 |
|
} |
623 |
|
|
624 |
3
|
String sw = searchWord.trim(); |
625 |
3
|
while (sw.contains(" ")) { |
626 |
0
|
sw = sw.replaceAll(" ", " "); |
627 |
|
} |
628 |
3
|
sw = " " + sw + " "; |
629 |
|
|
630 |
|
|
631 |
3
|
for (int fdID : foodIDsToSearch) { |
632 |
7855
|
String item = " " + foods[fdID]; |
633 |
7855
|
if (item.toLowerCase().replaceAll("[,\\(\\)]", " ").contains(sw)) { |
634 |
107
|
results.add(item.trim()); |
635 |
107
|
resultsID.add(new Long(fdID)); |
636 |
|
} |
637 |
|
} |
638 |
3
|
if (false) { |
639 |
0
|
sw = sw.trim() + " "; |
640 |
0
|
if (results.size() < 50) |
641 |
0
|
for (int fdID : foodIDsToSearch) { |
642 |
0
|
String item = " " + foods[fdID]; |
643 |
0
|
if (item.toLowerCase().replaceAll("[,\\(\\)]", " ") |
644 |
|
.contains(sw) |
645 |
|
&& !results.contains(item.trim())) { |
646 |
0
|
results.add(item.trim()); |
647 |
0
|
resultsID.add(new Long(fdID)); |
648 |
|
} |
649 |
|
} |
650 |
|
} |
651 |
|
|
652 |
3
|
sw = sw.trim(); |
653 |
3
|
if (results.size() < 40 && sw.length() > 2) |
654 |
2
|
for (int fdID : foodIDsToSearch) { |
655 |
1570
|
String item = foods[fdID]; |
656 |
1570
|
if (!results.contains(item.trim()) |
657 |
|
&& item.toLowerCase().contains(sw)) { |
658 |
|
|
659 |
0
|
results.add(item.trim()); |
660 |
0
|
resultsID.add(new Long(fdID)); |
661 |
|
} |
662 |
|
} |
663 |
|
|
664 |
3
|
if (results.size() < 5) |
665 |
1
|
if (searchWord.length() > 3) { |
666 |
1
|
searchWord = searchWord.trim().substring(0, |
667 |
|
searchWord.length() - 1); |
668 |
1
|
for (int fdID : foodIDsToSearch) { |
669 |
785
|
String item = " " + foods[fdID]; |
670 |
785
|
if (!results.contains(item.trim()) |
671 |
|
&& item.toLowerCase().replaceAll("[,\\(\\)]", " ") |
672 |
|
.contains(searchWord)) |
673 |
0
|
results.add(item.trim()); |
674 |
785
|
resultsID.add(new Long(fdID)); |
675 |
|
|
676 |
|
|
677 |
|
} |
678 |
|
} |
679 |
|
|
680 |
3
|
if (results.size() < 50) { |
681 |
2
|
String[] sWords = searchWord.trim().split(" "); |
682 |
2
|
if (sWords.length > 1) |
683 |
0
|
outerloop: for (int fdID : foodIDsToSearch) { |
684 |
0
|
String item = foods[fdID]; |
685 |
0
|
for (String pt : sWords) { |
686 |
0
|
if (results.contains(item.trim()) |
687 |
|
|| !item.toLowerCase().contains(pt) |
688 |
|
&& pt.length() > 3) |
689 |
0
|
continue outerloop; |
690 |
|
} |
691 |
0
|
results.add(item.trim()); |
692 |
0
|
resultsID.add(new Long(fdID)); |
693 |
0
|
System.out.println("found MULTIPLE WORD CASE---"); |
694 |
0
|
if (results.size() > 50) { |
695 |
0
|
break outerloop; |
696 |
|
} |
697 |
|
} |
698 |
|
} |
699 |
|
|
700 |
|
|
701 |
3
|
searchResults = new String[results.size()]; |
702 |
3
|
foodCodeResults = new int[results.size()]; |
703 |
|
|
704 |
110
|
for (int i = 0; i < results.size(); i++) { |
705 |
107
|
searchResults[i] = results.get(i); |
706 |
107
|
foodCodeResults[i] = (resultsID.get(i).intValue()); |
707 |
|
|
708 |
|
} |
709 |
3
|
searching = false; |
710 |
|
} |
711 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
|
712 |
0
|
static int[] concat(int[] A, int[] B) {... |
713 |
0
|
int aLen = A.length; |
714 |
0
|
int bLen = B.length; |
715 |
0
|
int[] C = new int[aLen + bLen]; |
716 |
0
|
System.arraycopy(A, 0, C, 0, aLen); |
717 |
0
|
System.arraycopy(B, 0, C, aLen, bLen); |
718 |
0
|
return C; |
719 |
|
} |
720 |
|
|
|
|
| 95,2% |
Uncovered Elements: 1 (21) |
Complexity: 4 |
Complexity Density: 0,25 |
|
721 |
|
private static class SimpleStatsBuilder implements Runnable { |
722 |
|
|
|
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 4 |
Complexity Density: 0,25 |
|
723 |
6
|
@Override... |
724 |
|
public void run() { |
725 |
6
|
try { |
726 |
6
|
loadingThread.join(); |
727 |
|
} catch (InterruptedException e) { |
728 |
|
|
729 |
0
|
e.printStackTrace(); |
730 |
|
} |
731 |
6
|
calculatingStats = true; |
732 |
6
|
statsLoaded = false; |
733 |
6
|
long time = System.currentTimeMillis(); |
734 |
|
|
735 |
6
|
int[] foodIds = WhatYouEat.getFoodSearchIds(); |
736 |
786
|
for (int i = 0; i < 130; i++) { |
737 |
780
|
boolean b = MY_NUTRIENTS[i]; |
738 |
780
|
if (!b) |
739 |
314
|
continue; |
740 |
466
|
float[] basically = StatTool.simpleStats(foodIds, i); |
741 |
|
|
742 |
466
|
highlightFactors.put(i, basically); |
743 |
|
|
744 |
466
|
Thread.yield(); |
745 |
|
|
746 |
|
} |
747 |
|
|
748 |
6
|
statsLoaded = true; |
749 |
6
|
calculatingStats = false; |
750 |
|
} |
751 |
|
|
752 |
|
} |
753 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 3 |
Complexity Density: 0,23 |
|
754 |
0
|
public static byte getRelativeAbundance(int nutrient, float data) {... |
755 |
|
|
756 |
0
|
float[] nstat = WhatYouEat.highlightFactors.get(nutrient); |
757 |
0
|
float min = nstat[0]; |
758 |
0
|
float mean = nstat[1]; |
759 |
0
|
float max = nstat[2]; |
760 |
|
|
761 |
0
|
float n = 0; |
762 |
|
|
763 |
0
|
if (data > mean) { |
764 |
0
|
n = data - mean; |
765 |
0
|
n /= (max - mean); |
766 |
|
|
767 |
|
} else { |
768 |
|
|
769 |
0
|
n = -(mean - data) / (mean - min + 1); |
770 |
|
} |
771 |
0
|
int sim = (int) (n * 100); |
772 |
0
|
if (sim > 100) |
773 |
0
|
sim = (int) (101 + 100 * Math.log(n - 100)); |
774 |
|
|
775 |
0
|
return (byte) sim; |
776 |
|
|
777 |
|
} |
778 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0,33 |
|
779 |
0
|
public static float getPer100KcalDataPoint(int foodID, float data) {... |
780 |
0
|
float calories = db[5][foodID]; |
781 |
|
|
782 |
0
|
float rData = (float) (100.0 * data); |
783 |
0
|
if (calories > 10) |
784 |
0
|
rData /= (db[5][foodID]); |
785 |
|
else |
786 |
0
|
rData /= (20); |
787 |
0
|
return (float) (((int) (1000 * (rData))) / 1000.0); |
788 |
|
|
789 |
|
} |
790 |
|
|
791 |
|
|
792 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 3 |
Complexity Density: 0,5 |
|
793 |
657606
|
public static float getPerServingDataPoint(int foodID,... |
794 |
|
float nutritionPer100Grams) { |
795 |
657606
|
if (quantityFactor[foodID] == null||optimalServingId[foodID]<0) |
796 |
18553
|
return -1; |
797 |
|
|
798 |
639053
|
float quantityOfOddUnit = quantityFactor[foodID][optimalServingId[foodID]]; |
799 |
639053
|
float gramsPerOddUnit = metricConversion[foodID][optimalServingId[foodID]]; |
800 |
|
|
801 |
639053
|
float rData = nutritionPer100Grams * gramsPerOddUnit / 100.0f; |
802 |
639053
|
return (float) (((int) (1000 * (rData))) / 1000.0); |
803 |
|
|
804 |
|
} |
805 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0,75 |
|
806 |
9
|
public static int findGroupFromFoodID(int foodId) {... |
807 |
30
|
for (int i = 0; i < 25; i++) { |
808 |
30
|
if (Arrays.binarySearch(foodsByGroup[i], foodId) >= 0) |
809 |
9
|
return i; |
810 |
|
} |
811 |
|
|
812 |
0
|
return 10; |
813 |
|
} |
814 |
|
|
815 |
|
} |
816 |
|
|