/
MainActivity.java
1065 lines (936 loc) 路 48.1 KB
/
MainActivity.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2016-2020 Open Food Facts
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package openfoodfacts.github.scrachx.openfood.features;
import android.Manifest;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.afollestad.materialdialogs.MaterialDialog;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.holder.StringHolder;
import com.mikepenz.materialdrawer.model.AbstractBadgeableDrawerItem;
import com.mikepenz.materialdrawer.model.DividerDrawerItem;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileSettingDrawerItem;
import com.mikepenz.materialdrawer.model.SectionDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Objects;
import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import openfoodfacts.github.scrachx.openfood.AppFlavors;
import openfoodfacts.github.scrachx.openfood.BuildConfig;
import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.customtabs.CustomTabActivityHelper;
import openfoodfacts.github.scrachx.openfood.customtabs.CustomTabsHelper;
import openfoodfacts.github.scrachx.openfood.customtabs.WebViewFallback;
import openfoodfacts.github.scrachx.openfood.databinding.ActivityMainBinding;
import openfoodfacts.github.scrachx.openfood.features.adapters.PhotosAdapter;
import openfoodfacts.github.scrachx.openfood.features.additives.AdditiveListActivity;
import openfoodfacts.github.scrachx.openfood.features.allergensalert.AllergensAlertFragment;
import openfoodfacts.github.scrachx.openfood.features.categories.activity.CategoryActivity;
import openfoodfacts.github.scrachx.openfood.features.compare.ProductCompareActivity;
import openfoodfacts.github.scrachx.openfood.features.listeners.CommonBottomListenerInstaller;
import openfoodfacts.github.scrachx.openfood.features.product.edit.ProductEditActivity;
import openfoodfacts.github.scrachx.openfood.features.productlists.ProductListsActivity;
import openfoodfacts.github.scrachx.openfood.features.scan.ContinuousScanActivity;
import openfoodfacts.github.scrachx.openfood.features.scanhistory.ScanHistoryActivity;
import openfoodfacts.github.scrachx.openfood.features.search.ProductSearchActivity;
import openfoodfacts.github.scrachx.openfood.features.searchbycode.SearchByCodeFragment;
import openfoodfacts.github.scrachx.openfood.features.shared.BaseActivity;
import openfoodfacts.github.scrachx.openfood.images.ProductImage;
import openfoodfacts.github.scrachx.openfood.jobs.OfflineProductWorker;
import openfoodfacts.github.scrachx.openfood.models.Product;
import openfoodfacts.github.scrachx.openfood.models.ProductImageField;
import openfoodfacts.github.scrachx.openfood.models.ProductState;
import openfoodfacts.github.scrachx.openfood.network.OpenFoodAPIClient;
import openfoodfacts.github.scrachx.openfood.utils.LocaleHelper;
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener;
import openfoodfacts.github.scrachx.openfood.utils.PrefManager;
import openfoodfacts.github.scrachx.openfood.utils.RealPathUtil;
import openfoodfacts.github.scrachx.openfood.utils.SearchSuggestionProvider;
import openfoodfacts.github.scrachx.openfood.utils.SearchType;
import openfoodfacts.github.scrachx.openfood.utils.Utils;
import static openfoodfacts.github.scrachx.openfood.BuildConfig.APP_NAME;
public class MainActivity extends BaseActivity implements NavigationDrawerListener {
private static final long USER_ID = 500;
private static final String CONTRIBUTIONS_SHORTCUT = "CONTRIBUTIONS";
private static final String SCAN_SHORTCUT = "SCAN";
private static final String BARCODE_SHORTCUT = "BARCODE";
private static final int WEEK_IN_MS = 60 * 60 * 24 * 7 * 1000;
public static final String PRODUCT_SEARCH_KEY = "product_search";
private static final String LOG_TAG = MainActivity.class.getSimpleName();
private String barcode;
private ActivityMainBinding binding;
private Uri contributeUri;
private CustomTabActivityHelper customTabActivityHelper;
/**
* Used to re-create the fragment after activity recreation
*/
private CustomTabsIntent customTabsIntent;
private Uri discoverUri;
private CompositeDisposable disp;
private Drawer drawerResult = null;
private AccountHeader headerResult = null;
private PrefManager prefManager;
private MenuItem searchMenuItem;
private Uri userAccountUri;
private final ActivityResultLauncher<Void> loginThenUpdateLauncher = registerForActivityResult(
new LoginActivity.LoginContract(), isLoggedIn -> {
if (isLoggedIn) {
updateConnectedState();
}
});
private Uri userContributeUri;
private final ActivityResultLauncher<Void> loginThenContributionsLauncher = registerForActivityResult(
new LoginActivity.LoginContract(), isLoggedIn -> {
if (isLoggedIn) {
openMyContributionsInBrowser();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
disp = new CompositeDisposable();
if (getResources().getBoolean(R.bool.portrait_only)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Utils.hideKeyboard(this);
final IProfile<ProfileDrawerItem> profile = getUserProfile();
LocaleHelper.setLocale(this, LocaleHelper.getLanguage(this));
setSupportActionBar(binding.toolbarInclude.toolbar);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(false);
swapToHomeFragment();
// chrome custom tab init
customTabActivityHelper = new CustomTabActivityHelper();
customTabActivityHelper.setConnectionCallback(new CustomTabActivityHelper.ConnectionCallback() {
@Override
public void onCustomTabsConnected() {
}
@Override
public void onCustomTabsDisconnected() {
}
});
customTabsIntent = CustomTabsHelper.getCustomTabsIntent(this,
customTabActivityHelper.getSession());
// Create the AccountHeader
AccountHeaderBuilder accountHeaderBuilder = new AccountHeaderBuilder()
.withActivity(this)
.withTranslucentStatusBar(true)
.withTextColorRes(R.color.white)
.addProfiles(profile)
.withOnAccountHeaderProfileImageListener(new AccountHeader.OnAccountHeaderProfileImageListener() {
@Override
public boolean onProfileImageClick(@NonNull View view, @NonNull IProfile profile, boolean current) {
if (!isUserLoggedIn()) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
return false;
}
@Override
public boolean onProfileImageLongClick(@NonNull View view, @NonNull IProfile profile, boolean current) {
return false;
}
})
.withOnAccountHeaderSelectionViewClickListener((view, profile12) -> {
if (!isUserLoggedIn()) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
return false;
})
.withSelectionListEnabledForSingleProfile(false)
.withOnAccountHeaderListener((view, profile1, current) -> {
if (profile1 instanceof IDrawerItem && profile1.getIdentifier() == ITEM_MANAGE_ACCOUNT) {
CustomTabActivityHelper.openCustomTab(MainActivity.this,
customTabsIntent,
userAccountUri,
new WebViewFallback());
}
//false if you have not consumed the event and it should close the drawer
return false;
})
.withSavedInstance(savedInstanceState);
try {
accountHeaderBuilder = accountHeaderBuilder.withHeaderBackground(R.drawable.header);
} catch (OutOfMemoryError e) {
Log.w(LOG_TAG, "Device has too low memory, loading color drawer header...", e);
accountHeaderBuilder = accountHeaderBuilder.withHeaderBackground(new ColorDrawable(ContextCompat.getColor(this, R.color.primary_dark)));
}
headerResult = accountHeaderBuilder.build();
// Add Manage Account profile if the user is connected
SharedPreferences preferences = getSharedPreferences(PreferencesFragment.LOGIN_PREF, 0);
String userSessionPrefs = preferences.getString("user_session", null);
boolean isUserConnected = isUserLoggedIn() && userSessionPrefs != null;
if (isUserConnected) {
updateProfileForCurrentUser();
}
//Create the drawer
drawerResult = new DrawerBuilder()
.withActivity(this)
.withToolbar(binding.toolbarInclude.toolbar)
.withHasStableIds(true)
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.withOnDrawerListener(new Drawer.OnDrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
Utils.hideKeyboard(MainActivity.this);
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
Utils.hideKeyboard(MainActivity.this);
}
})
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.home_drawer).withIcon(GoogleMaterial.Icon.gmd_home).withIdentifier(ITEM_HOME),
new SectionDrawerItem().withName(R.string.search_drawer),
new PrimaryDrawerItem().withName(R.string.search_by_barcode_drawer).withIcon(GoogleMaterial.Icon.gmd_dialpad).withIdentifier(ITEM_SEARCH_BY_CODE),
new PrimaryDrawerItem().withName(R.string.search_by_category).withIcon(GoogleMaterial.Icon.gmd_filter_list).withIdentifier(ITEM_CATEGORIES).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.additives).withIcon(R.drawable.ic_additives).withIdentifier(ITEM_ADDITIVES)
.withSelectable(false),
new PrimaryDrawerItem().withName(R.string.scan_search).withIcon(R.drawable.barcode_grey_24dp).withIdentifier(ITEM_SCAN).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.compare_products).withIcon(GoogleMaterial.Icon.gmd_swap_horiz).withIdentifier(ITEM_COMPARE).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.advanced_search_title).withIcon(GoogleMaterial.Icon.gmd_insert_chart).withIdentifier(ITEM_ADVANCED_SEARCH)
.withSelectable(false),
new PrimaryDrawerItem().withName(R.string.scan_history_drawer).withIcon(GoogleMaterial.Icon.gmd_history).withIdentifier(ITEM_HISTORY).withSelectable(false),
new SectionDrawerItem().withName(R.string.user_drawer).withIdentifier(USER_ID),
new PrimaryDrawerItem().withName(getString(R.string.action_contributes)).withIcon(GoogleMaterial.Icon.gmd_rate_review).withIdentifier(ITEM_MY_CONTRIBUTIONS)
.withSelectable(false),
new PrimaryDrawerItem().withName(R.string.your_lists).withIcon(GoogleMaterial.Icon.gmd_list).withIdentifier(ITEM_YOUR_LISTS).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.products_to_be_completed).withIcon(GoogleMaterial.Icon.gmd_edit).withIdentifier(ITEM_INCOMPLETE_PRODUCTS)
.withSelectable(false),
new PrimaryDrawerItem().withName(R.string.alert_drawer).withIcon(GoogleMaterial.Icon.gmd_warning).withIdentifier(ITEM_ALERT),
new PrimaryDrawerItem().withName(R.string.action_preferences).withIcon(GoogleMaterial.Icon.gmd_settings).withIdentifier(ITEM_PREFERENCES),
new DividerDrawerItem(),
new PrimaryDrawerItem().withName(R.string.action_discover).withIcon(GoogleMaterial.Icon.gmd_info).withIdentifier(ITEM_ABOUT).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.contribute).withIcon(GoogleMaterial.Icon.gmd_group).withIdentifier(ITEM_CONTRIBUTE).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.open_other_flavor_drawer).withIcon(GoogleMaterial.Icon.gmd_shop).withIdentifier(ITEM_OBF).withSelectable(false)
)
.withOnDrawerItemClickListener((view, position, drawerItem) -> {
Fragment newFragment = null;
switch ((int) drawerItem.getIdentifier()) {
case ITEM_HOME:
newFragment = HomeFragment.newInstance();
break;
case ITEM_SEARCH_BY_CODE:
newFragment = new SearchByCodeFragment();
CommonBottomListenerInstaller.selectNavigationItem(binding.bottomNavigationInclude.bottomNavigation, 0);
break;
case ITEM_CATEGORIES:
CategoryActivity.start(this);
break;
case ITEM_ADDITIVES:
AdditiveListActivity.start(this);
break;
case ITEM_SCAN:
openScan();
break;
case ITEM_COMPARE:
ProductCompareActivity.start(this);
break;
case ITEM_HISTORY:
ScanHistoryActivity.start(this);
break;
case ITEM_LOGIN:
loginThenUpdateLauncher.launch(null);
break;
case ITEM_ALERT:
newFragment = AllergensAlertFragment.newInstance();
break;
case ITEM_PREFERENCES:
newFragment = PreferencesFragment.newInstance();
break;
case ITEM_ABOUT:
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, discoverUri, new WebViewFallback());
break;
case ITEM_CONTRIBUTE:
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, contributeUri, new WebViewFallback());
break;
case ITEM_INCOMPLETE_PRODUCTS:
// Search and display the products to be completed by moving to ProductBrowsingListActivity
ProductSearchActivity.start(this, "", SearchType.INCOMPLETE_PRODUCT);
break;
case ITEM_OBF:
boolean otherOFAppInstalled = Utils.isApplicationInstalled
(MainActivity.this, BuildConfig.OFOTHERLINKAPP);
if (otherOFAppInstalled) {
Intent launchIntent = getPackageManager()
.getLaunchIntentForPackage(BuildConfig.OFOTHERLINKAPP);
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(this, R.string.app_disabled_text, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", BuildConfig.OFOTHERLINKAPP, null);
intent.setData(uri);
startActivity(intent);
}
} else {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse
("market://details?id=" + BuildConfig.OFOTHERLINKAPP)));
} catch (ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" +
BuildConfig.OFOTHERLINKAPP)));
}
}
break;
case ITEM_ADVANCED_SEARCH:
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
CustomTabActivityHelper.openCustomTab(this, customTabsIntent, Uri.parse(getString(R.string.advanced_search_url)), new
WebViewFallback());
break;
case ITEM_MY_CONTRIBUTIONS:
openMyContributions();
break;
case ITEM_YOUR_LISTS:
ProductListsActivity.start(this);
break;
case ITEM_LOGOUT:
new MaterialDialog.Builder(MainActivity.this)
.title(R.string.confirm_logout)
.content(R.string.logout_dialog_content)
.positiveText(R.string.txtOk)
.negativeText(R.string.dialog_cancel)
.onPositive((dialog, which) -> logout())
.onNegative((dialog, which) -> {
dialog.dismiss();
Snackbar.make(binding.getRoot(), "Cancelled", BaseTransientBottomBar.LENGTH_SHORT).show();
}).show();
break;
default:
// nothing to do
break;
}
if (newFragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, newFragment)
.addToBackStack(null)
.commit();
}
return false;
})
.withSavedInstance(savedInstanceState)
.withShowDrawerOnFirstLaunch(false)
.build();
drawerResult.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
// Add Drawer items for the connected user
drawerResult.addItemsAtPosition(drawerResult.getPosition(ITEM_MY_CONTRIBUTIONS), isUserLoggedIn() ?
getLogoutDrawerItem() : getLoginDrawerItem());
if (AppFlavors.isFlavors(AppFlavors.OBF)) {
drawerResult.removeItem(ITEM_ALERT);
drawerResult.removeItem(ITEM_ADDITIVES);
drawerResult.updateName(ITEM_OBF, new StringHolder(getString(R.string.open_other_flavor_drawer)));
} else if (AppFlavors.isFlavors(AppFlavors.OPFF)) {
drawerResult.removeItem(ITEM_ALERT);
} else if (AppFlavors.isFlavors(AppFlavors.OPF)) {
drawerResult.removeItem(ITEM_ALERT);
drawerResult.removeItem(ITEM_ADDITIVES);
drawerResult.removeItem(ITEM_ADVANCED_SEARCH);
}
if (!Utils.isApplicationInstalled(MainActivity.this, BuildConfig.OFOTHERLINKAPP)) {
drawerResult.updateName(ITEM_OBF, new StringHolder(getString(R.string.install) + " " + getString(R.string.open_other_flavor_drawer)));
} else {
drawerResult.updateName(ITEM_OBF, new StringHolder(getString(R.string.open_other_flavor_drawer)));
}
// Remove scan item if the device does not have a camera, for example, Chromebooks or
// Fire devices
if (!Utils.isHardwareCameraInstalled(this)) {
drawerResult.removeItem(ITEM_SCAN);
}
// //if you have many different types of DrawerItems you can magically pre-cache those items
// // to get a better scroll performance
// //make sure to init the cache after the DrawerBuilder was created as this will first
// // clear the cache to make sure no old elements are in
// new RecyclerViewCacheUtil<IDrawerItem>().withCacheSize(2).apply(result.getRecyclerView(),
// result.getDrawerItems());
//only set the active selection or active profile if we do not recreate the activity
if (savedInstanceState == null) {
// set the selection to the item with the identifier 1
drawerResult.setSelection(ITEM_HOME, false);
//set the active profile
headerResult.setActiveProfile(profile);
}
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
if (settings.getBoolean("startScan", false)) {
Intent cameraIntent = new Intent(MainActivity.this, ContinuousScanActivity.class);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(cameraIntent);
}
// prefetch uris
contributeUri = Uri.parse(getString(R.string.website_contribute));
discoverUri = Uri.parse(getString(R.string.website_discover));
userContributeUri = Uri.parse(getString(R.string.website_contributor) + getUserLogin());
customTabActivityHelper.mayLaunchUrl(contributeUri, null, null);
customTabActivityHelper.mayLaunchUrl(discoverUri, null, null);
customTabActivityHelper.mayLaunchUrl(userContributeUri, null, null);
if (CONTRIBUTIONS_SHORTCUT.equals(getIntent().getAction())) {
openMyContributions();
}
if (SCAN_SHORTCUT.equals(getIntent().getAction())) {
openScan();
}
if (BARCODE_SHORTCUT.equals(getIntent().getAction())) {
swapToSearchByCode();
}
//Scheduling background image upload job
Utils.scheduleProductUploadJob(this);
OfflineProductWorker.scheduleSync();
//Adds nutriscore and quantity values in old history for schema 5 update
SharedPreferences mSharedPref = getApplicationContext().getSharedPreferences("prefs", 0);
boolean isOldHistoryDataSynced = mSharedPref.getBoolean("is_old_history_data_synced", false);
if (!isOldHistoryDataSynced && Utils.isNetworkConnected(this)) {
OpenFoodAPIClient apiClient = new OpenFoodAPIClient(this);
apiClient.syncOldHistory();
}
CommonBottomListenerInstaller.selectNavigationItem(binding.bottomNavigationInclude.bottomNavigation, 0);
CommonBottomListenerInstaller.install(this, binding.bottomNavigationInclude.bottomNavigation);
handleIntent(getIntent());
}
private void swapToHomeFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.addOnBackStackChangedListener(() -> {
});
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, new HomeFragment())
.commit();
binding.toolbarInclude.toolbar.setTitle(APP_NAME);
}
private void openScan() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest
.permission.CAMERA)) {
new MaterialDialog.Builder(MainActivity.this)
.title(R.string.action_about)
.content(R.string.permission_camera)
.neutralText(R.string.txtOk)
.show().setOnDismissListener(dialogInterface -> ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA},
Utils.MY_PERMISSIONS_REQUEST_CAMERA));
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest
.permission.CAMERA}, Utils.MY_PERMISSIONS_REQUEST_CAMERA);
}
} else {
Intent intent = new Intent(MainActivity.this, ContinuousScanActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
private void updateProfileForCurrentUser() {
headerResult.updateProfile(getUserProfile());
if (isUserLoggedIn()) {
if (headerResult.getProfiles() != null && headerResult.getProfiles().size() < 2) {
headerResult.addProfiles(getProfileSettingDrawerItem());
}
} else {
headerResult.removeProfileByIdentifier(ITEM_MANAGE_ACCOUNT);
}
}
private void openMyContributions() {
if (isUserLoggedIn()) {
openMyContributionsInBrowser();
} else {
new MaterialDialog.Builder(MainActivity.this)
.title(R.string.contribute)
.content(R.string.contribution_without_account)
.positiveText(R.string.create_account_button)
.neutralText(R.string.login_button)
.onPositive((dialog, which) -> CustomTabActivityHelper.openCustomTab(MainActivity.this, customTabsIntent, Uri.parse(getString(R
.string.website) + "cgi/user.pl"), new WebViewFallback()))
.onNeutral((dialog, which) ->
loginThenContributionsLauncher.launch(null))
.show();
}
}
private void openMyContributionsInBrowser() {
String userLogin = getUserLogin();
userContributeUri = Uri.parse(getString(R.string.website_contributor) + userLogin);
ProductSearchActivity.start(this, userLogin, SearchType.CONTRIBUTOR);
}
private IProfile<ProfileSettingDrawerItem> getProfileSettingDrawerItem() {
String userLogin = getUserLogin();
String userSession = getUserSession();
userAccountUri = Uri.parse(getString(R.string.website) + "cgi/user.pl?type=edit&userid=" + userLogin + "&user_id=" + userLogin +
"&user_session=" + userSession);
customTabActivityHelper.mayLaunchUrl(userAccountUri, null, null);
return new ProfileSettingDrawerItem()
.withName(getString(R.string.action_manage_account))
.withIcon(GoogleMaterial.Icon.gmd_settings)
.withIdentifier(ITEM_MANAGE_ACCOUNT)
.withSelectable(false);
}
/**
* Replace logout menu item by the login menu item
* Change current user profile (Anonymous)
* Remove all Account Header items
* Remove user login info
*/
private void logout() {
getSharedPreferences(PreferencesFragment.LOGIN_PREF, MODE_PRIVATE).edit().clear().apply();
updateConnectedState();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = drawerResult.saveInstanceState(outState);
//add the values which need to be saved from the accountHeader to the bundle
outState = headerResult.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the
// activity
if (drawerResult != null && drawerResult.isDrawerOpen()) {
drawerResult.closeDrawer();
} else {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack(getSupportFragmentManager().getBackStackEntryAt(0).getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
//recreate the activity onBackPressed
recreate();
} else {
super.onBackPressed();
}
}
}
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(LocaleHelper.onCreate(newBase));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> {
if (hasFocus) {
binding.bottomNavigationInclude.bottomNavigation.setVisibility(View.GONE);
} else {
binding.bottomNavigationInclude.bottomNavigation.setVisibility(View.VISIBLE);
}
});
searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
binding.bottomNavigationInclude.bottomNavigation.setVisibility(View.GONE);
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
return true;
}
});
if (getIntent().getBooleanExtra(PRODUCT_SEARCH_KEY, false)) {
searchMenuItem.expandActionView();
}
return true;
}
private IDrawerItem<AbstractBadgeableDrawerItem.ViewHolder> getLogoutDrawerItem() {
return new PrimaryDrawerItem()
.withName(getString(R.string.logout_drawer))
.withIcon(GoogleMaterial.Icon.gmd_settings_power)
.withIdentifier(ITEM_LOGOUT)
.withSelectable(false);
}
private IDrawerItem<AbstractBadgeableDrawerItem.ViewHolder> getLoginDrawerItem() {
return new PrimaryDrawerItem()
.withName(R.string.sign_in_drawer)
.withIcon(GoogleMaterial.Icon.gmd_account_circle)
.withIdentifier(ITEM_LOGIN)
.withSelectable(false);
}
private IProfile<ProfileDrawerItem> getUserProfile() {
String userLogin = getSharedPreferences(PreferencesFragment.LOGIN_PREF, 0)
.getString("user", getResources().getString(R.string.txt_anonymous));
return new ProfileDrawerItem()
.withName(userLogin)
.withIcon(R.drawable.img_home)
.withIdentifier(ITEM_USER);
}
@Override
protected void onStart() {
super.onStart();
customTabActivityHelper.bindCustomTabsService(this);
prefManager = new PrefManager(this);
if (AppFlavors.isFlavors(AppFlavors.OFF)
&& isUserLoggedIn()
&& !prefManager.isFirstTimeLaunch()
&& !prefManager.getUserAskedToRate()) {
long firstTimeLaunchTime = prefManager.getFirstTimeLaunchTime();
// Check if it has been a week since first launch
if ((Calendar.getInstance().getTimeInMillis() - firstTimeLaunchTime) >= WEEK_IN_MS) {
showFeedbackDialog();
}
}
}
/**
* show dialog to ask the user to rate the app/give feedback
*/
private void showFeedbackDialog() {
//dialog for rating the app on play store
MaterialDialog.Builder rateDialog = new MaterialDialog.Builder(this)
.title(R.string.app_name)
.content(R.string.user_ask_rate_app)
.positiveText(R.string.rate_app)
.negativeText(R.string.no_thx)
.onPositive((dialog, which) -> {
//open app page in play store
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + getPackageName())));
dialog.dismiss();
})
.onNegative((dialog, which) -> dialog.dismiss());
//dialog for giving feedback
MaterialDialog.Builder feedbackDialog = new MaterialDialog.Builder(this)
.title(R.string.app_name)
.content(R.string.user_ask_show_feedback_form)
.positiveText(R.string.txtOk)
.negativeText(R.string.txtNo)
.onPositive((dialog, which) -> {
//show feedback form
CustomTabActivityHelper.openCustomTab(MainActivity.this,
customTabsIntent, Uri.parse(getString(R.string.feedback_form_url)), new WebViewFallback());
dialog.dismiss();
})
.onNegative((dialog, which) -> dialog.dismiss());
new MaterialDialog.Builder(this)
.title(R.string.app_name)
.content(R.string.user_enjoying_app)
.positiveText(R.string.txtYes)
.onPositive((dialog, which) -> {
prefManager.setUserAskedToRate(true);
rateDialog.show();
dialog.dismiss();
})
.negativeText(R.string.txtNo)
.onNegative((dialog, which) -> {
prefManager.setUserAskedToRate(true);
feedbackDialog.show();
dialog.dismiss();
})
.show();
}
@Override
protected void onStop() {
super.onStop();
customTabActivityHelper.unbindCustomTabsService(this);
}
@Override
protected void onDestroy() {
customTabActivityHelper.setConnectionCallback(null);
disp.dispose();
binding = null;
super.onDestroy();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
String type = intent.getType();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
Log.e("INTENT", "start activity");
String query = intent.getStringExtra(SearchManager.QUERY);
//Saves the most recent queries and adds it to the list of suggestions
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);
ProductSearchActivity.start(this, query, SearchType.SEARCH);
if (searchMenuItem != null) {
searchMenuItem.collapseActionView();
}
} else if (type != null && type.startsWith("image/")) {
if (Intent.ACTION_SEND.equals(intent.getAction())) {
handleSendImage(intent); // Handle single image being sent
} else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
handleSendMultipleImages(intent); // Handle multiple images being sent
}
}
}
/**
* This moves the main activity to the barcode entry fragment.
*/
public void swapToSearchByCode() {
changeFragment(new SearchByCodeFragment(), getResources().getString(R.string.search_by_barcode_drawer), ITEM_SEARCH_BY_CODE);
}
@Override
public void setItemSelected(@NavigationDrawerType Integer type) {
drawerResult.setSelection(type, false);
}
@Override
public void onResume() {
super.onResume();
CommonBottomListenerInstaller.selectNavigationItem(binding.bottomNavigationInclude.bottomNavigation, R.id.home_page);
// change drawer menu item from "install" to "open" when navigating back from play store.
if (Utils.isApplicationInstalled(MainActivity.this, BuildConfig.OFOTHERLINKAPP)) {
drawerResult.updateName(ITEM_OBF, new StringHolder(getString(R.string.open_other_flavor_drawer)));
drawerResult.getAdapter().notifyDataSetChanged();
}
updateConnectedState();
}
private void updateConnectedState() {
updateProfileForCurrentUser();
drawerResult.removeItem(ITEM_LOGIN);
drawerResult.removeItem(ITEM_LOGOUT);
drawerResult.addItemAtPosition(super.isUserLoggedIn() ? getLogoutDrawerItem() : getLoginDrawerItem(), drawerResult.getPosition(ITEM_MY_CONTRIBUTIONS));
}
private void handleSendImage(@NonNull Intent intent) {
ArrayList<Uri> selectedImagesArray = new ArrayList<>();
Uri selectedImage = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (selectedImage != null) {
selectedImagesArray.add(selectedImage);
chooseDialog(selectedImagesArray);
}
}
private void handleSendMultipleImages(@NonNull Intent intent) {
ArrayList<Uri> selectedImagesArray = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (selectedImagesArray != null) {
selectedImagesArray.removeAll(Collections.singleton(null));
chooseDialog(selectedImagesArray);
}
}
private void chooseDialog(ArrayList<Uri> selectedImagesArray) {
disp.add(detectBarcodeInImages(selectedImagesArray).observeOn(AndroidSchedulers.mainThread())
.subscribe(isBarCodePresent -> {
if (isBarCodePresent) {
createAlertDialog(false, barcode, selectedImagesArray);
} else {
createAlertDialog(true, "", selectedImagesArray);
}
}));
}
/**
* IO / Computing intensive operation
*
* @param selectedImages
*/
private Single<Boolean> detectBarcodeInImages(List<Uri> selectedImages) {
return Observable.fromIterable(selectedImages)
.map(uri -> {
Bitmap bMap = null;
try (InputStream imageStream = getContentResolver().openInputStream(uri)) {
bMap = BitmapFactory.decodeStream(imageStream);
} catch (FileNotFoundException e) {
Log.e(MainActivity.class.getSimpleName(), "Could not resolve file from Uri " + uri.toString(), e);
} catch (IOException e) {
Log.e(MainActivity.class.getSimpleName(), "IO error during bitmap stream decoding: " + e.getMessage(), e);
}
//decoding bitmap
if (bMap != null) {
int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
try {
EnumMap<DecodeHintType, Object> decodeHints = new EnumMap<>(DecodeHintType.class);
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
Result decodedResult = reader.decode(bitmap, decodeHints);
if (decodedResult != null) {
barcode = decodedResult.getText();
}
if (barcode != null) {
return true;
}
} catch (FormatException e) {
Toast.makeText(getApplicationContext(), getString(R.string.format_error), Toast.LENGTH_SHORT).show();
Log.e(MainActivity.class.getSimpleName(), "Error decoding bitmap into barcode: " + e.getMessage());
} catch (Exception e) {
Log.e(MainActivity.class.getSimpleName(), "Error decoding bitmap into barcode: " + e.getMessage());
}
}
return false;
})
.filter(Boolean::booleanValue)
.first(false)
.subscribeOn(Schedulers.computation());
}
private void createAlertDialog(boolean hasEditText, @NonNull String barcode, @NonNull ArrayList<Uri> uri) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_barcode, null);
alertDialogBuilder.setView(dialogView);
final EditText barcodeEditText = dialogView.findViewById(R.id.barcode);
final RecyclerView productImages = dialogView.findViewById(R.id.product_image);
LinearLayoutManager layoutManager = new LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL,
false);
productImages.setLayoutManager(layoutManager);
productImages.setAdapter(new PhotosAdapter(uri));
if (hasEditText) {
barcodeEditText.setVisibility(View.VISIBLE);
alertDialogBuilder.setTitle(getString(R.string.no_barcode));
alertDialogBuilder.setMessage(getString(R.string.enter_barcode));
} else {
alertDialogBuilder.setTitle(getString(R.string.code_detected));
alertDialogBuilder.setMessage(barcode + "\n" + getString(R.string.do_you_want_to));
}
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton(R.string.txtYes, (dialog, id) -> {
for (Uri selected : uri) {
OpenFoodAPIClient api = new OpenFoodAPIClient(MainActivity.this);
ProductImage image;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
String tempBarcode;
if (hasEditText) {
tempBarcode = barcodeEditText.getText().toString();
} else {
tempBarcode = barcode;
}
if (tempBarcode.length() > 0) {
dialog.cancel();
if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
File imageFile = new File(RealPathUtil.getRealPath(MainActivity.this, selected));
image = new ProductImage(tempBarcode, ProductImageField.OTHER, imageFile);
disp.add(api.postImg(image).subscribe());
} else {
Product pd = new Product();
pd.setCode(tempBarcode);