Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [Rebase of initial PR] Allow the users to get Nutri-Score, NOVA and Eco-Score even if they don't have any Network #3288

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<application
android:name=".views.OFFApplication"
android:allowBackup="true"
android:largeHeap="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
Expand Down Expand Up @@ -147,10 +148,20 @@
<activity android:name=".views.PrefManager"/>
<activity android:name=".views.WelcomeActivity"/>
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat"/>

<service android:name=".views.LoadTaxonomiesService"/>
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
<service
android:name=".jobs.SavedProductUploadJob"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
</intent-filter>
</service>
<service android:name=".utils.UploadService" />
<service android:name=".utils.WifiUploadReceiver$WifiService" />
<service android:name=".jobs.DownloadOfflineProductService" />
<service android:name=".jobs.ExtractOfflineProductService" />
<service android:name=".views.LoadTaxonomiesService" />

<provider
android:name=".utils.GenericFileProvider"
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package openfoodfacts.github.scrachx.openfood.models;

public class OfflineListItem {
public final static int TYPE_SMALL = 0;
public final static int TYPE_LARGE = 0;

private String name;
private int type;
private int size;
private String url;
private int progress;

public OfflineListItem() {
progress = 0;
}

public int getProgress() {
return progress;
}

public void setProgress(int progress) {
this.progress = progress;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package openfoodfacts.github.scrachx.openfood.models;

import com.opencsv.bean.CsvBindByName;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Index;

@Entity(indexes = {
@Index(value = "barcode", unique = true)
})
public class OfflineProduct {

@Id
private Long id;
@CsvBindByName(column = "product_name")
private String title;
@CsvBindByName(column = "brands")
private String brands;
@CsvBindByName(column = "code")
private String barcode;
@CsvBindByName(column = "quantity")
private String quantity;
@CsvBindByName(column = "nutrition_grade_fr")
private String nutritionGrade;
private String url;

public OfflineProduct(String title, String brands, String barcode, String quantity, String nutritionGrade, String url) {
this.title = title;
this.brands = brands;
this.barcode = barcode;
this.quantity = quantity;
this.nutritionGrade = nutritionGrade;
this.url = url;
}

@Generated(hash = 319590731)
public OfflineProduct(Long id, String title, String brands, String barcode, String quantity, String nutritionGrade,
String url) {
this.id = id;
this.title = title;
this.brands = brands;
this.barcode = barcode;
this.quantity = quantity;
this.nutritionGrade = nutritionGrade;
this.url = url;
}

@Generated(hash = 1425505421)
public OfflineProduct() {
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getBrands() {
return brands;
}

public void setBrands(String brands) {
this.brands = brands;
}

public String getBarcode() {
return barcode;
}

public void setBarcode(String barcode) {
this.barcode = barcode;
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getQuantity() {
return quantity;
}

public void setQuantity(String quantity) {
this.quantity = quantity;
}

public String getNutritionGrade() {
return nutritionGrade;
}

public void setNutritionGrade(String nutritionGrade) {
this.nutritionGrade = nutritionGrade;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ Call<Search> getProductsByState(@Path("State") String state,
Call<String> editImages(@Query(ApiFields.Keys.BARCODE) String code,
@QueryMap Map<String, String> fields);

/**
* This method downloads the file with a dynamic downloadable url
*
* @param fileUrl
* @return
*/
@GET
Call<ResponseBody> downloadFileWithDynamicUrlSync(@Url String fileUrl);
@GET("/cgi/product_image_unselect.pl")
Call<String> unselectImage(@Query(ApiFields.Keys.BARCODE) String code,
@QueryMap Map<String, String> fields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public interface NavigationDrawerListener {
ITEM_MY_CONTRIBUTIONS,
ITEM_LOGOUT,
ITEM_INCOMPLETE_PRODUCTS,
ITEM_ADDITIVES
ITEM_ADDITIVES,
})
@interface NavigationDrawerType {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ private Utils() {
public static final String SPACE = " ";
public static final int MY_PERMISSIONS_REQUEST_CAMERA = 1;
public static final int MY_PERMISSIONS_REQUEST_STORAGE = 2;
private static final String UPLOAD_JOB_TAG = "upload_saved_product_job";
private static boolean isUploadJobInitialised;
public static final String UPLOAD_JOB_TAG = "upload_saved_product_job";
private static final String TAG = "Utils";
public static boolean isUploadJobInitialised;
public static boolean DISABLE_IMAGE_LOAD = false;
public static final String LAST_REFRESH_DATE = "last_refresh_date_of_taxonomies";
public static final String HEADER_USER_AGENT_SCAN = "Scan";
public static final String HEADER_USER_AGENT_SEARCH = "Search";
public static final int NO_DRAWABLE_RESOURCE = 0;
Expand Down Expand Up @@ -575,6 +578,24 @@ public static boolean isExternalStorageWritable() {
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
}

public static boolean isStoragePermissionGranted(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (activity.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission is granted");
return true;
} else {

Log.v(TAG, "Permission is revoked");
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_STORAGE);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG, "Permission is granted");
return true;
}
}

public static Uri getOutputPicUri(Context context) {
return Uri.fromFile(new File(Utils.makeOrGetPictureDirectory(context), System.currentTimeMillis() + ".jpg"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
import openfoodfacts.github.scrachx.openfood.fragments.FindProductFragment;
import openfoodfacts.github.scrachx.openfood.fragments.HomeFragment;
import openfoodfacts.github.scrachx.openfood.fragments.PreferencesFragment;
import openfoodfacts.github.scrachx.openfood.jobs.DownloadOfflineProductService;
import openfoodfacts.github.scrachx.openfood.models.LabelNameDao;
import openfoodfacts.github.scrachx.openfood.images.ProductImage;
import openfoodfacts.github.scrachx.openfood.jobs.OfflineProductWorker;
import openfoodfacts.github.scrachx.openfood.models.Product;
Expand Down Expand Up @@ -127,6 +129,7 @@ public class MainActivity extends BaseActivity implements CustomTabActivityHelpe
// boolean to determine if scan on shake feature should be enabled
private boolean scanOnShake;
private SharedPreferences shakePreference;
SharedPreferences settings;
private PrefManager prefManager;

@Override
Expand All @@ -139,6 +142,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(binding.getRoot());

shakePreference = PreferenceManager.getDefaultSharedPreferences(this);
settings = getSharedPreferences("prefs", Context.MODE_PRIVATE);

Utils.hideKeyboard(this);

Expand Down Expand Up @@ -669,6 +673,14 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
break;
case Utils.MY_PERMISSIONS_REQUEST_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager
.PERMISSION_GRANTED) {
Intent serviceIntent = new Intent(this, DownloadOfflineProductService.class);
startService(serviceIntent);
}
}
}
}

Expand Down Expand Up @@ -786,6 +798,26 @@ protected void onDestroy() {

@Override
protected void onNewIntent(Intent intent) {
// to handle Extract and Save button from DownloadOfflineProductService
if (intent.getStringExtra("from_download_service") != null && intent.getStringExtra("from_download_service").equals("start_extraction")) {

Bundle bundle = new Bundle();
bundle.putString("from_download_service", "start_extraction");
// moveToProductsDownload(bundle);
}

// to handle retry from DownloadOfflineProductService
if (intent.getStringExtra("from_download_service") != null && intent.getStringExtra("from_download_service").equals("retry")) {
Bundle bundle = new Bundle();
bundle.putString("from_download_service", "retry");
// moveToProductsDownload(bundle);
}
// to handle retry from ExtractOfflineProductService
if (intent.getStringExtra("from_extract_service") != null && intent.getStringExtra("from_extract_service").equals("retry")) {
Bundle bundle = new Bundle();
bundle.putString("from_extract_service", "retry");
// moveToProductsDownload(bundle);
}
super.onNewIntent(intent);
handleIntent(intent);
}
Expand Down Expand Up @@ -822,6 +854,51 @@ public void moveToBarcodeEntry() {
changeFragment(fragment, getResources().getString(R.string.search_by_barcode_drawer), ITEM_SEARCH_BY_CODE);
}

/**
* This moves the main activity to the preferences fragment.
*/
public void moveToPreferences() {
Fragment fragment = new PreferencesFragment();
changeFragment(fragment, getString(R.string.preferences), ITEM_PREFERENCES);
}

/**
* Create the drawer item. This adds a badge if there are items in the offline edit, otherwise
* there is no badge present.
*
* @return drawer item.
*/
private PrimaryDrawerItem createOfflineEditDrawerItem() {
if (numberOFSavedProducts > 0) {
return new PrimaryDrawerItem().withName(R.string.offline_edit_drawer).withIcon(GoogleMaterial.Icon.gmd_local_airport).withIdentifier(ITEM_OFFLINE)
.withBadge(String.valueOf(numberOFSavedProducts)).withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R
.color.md_red_700));
// Fixed merge conflict @pierre
// return new PrimaryDrawerItem().withName(R.string.offline_edit_drawer).withIcon(GoogleMaterial.Icon.gmd_local_airport).withIdentifier(10)
// .withBadge(String.valueOf(numberOFSavedProducts)).withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R
// .color.md_red_700));

} else {
return new PrimaryDrawerItem().withName(R.string.offline_edit_drawer).withIcon(GoogleMaterial.Icon.gmd_local_airport).withIdentifier(ITEM_OFFLINE);
}
}

/**
* Updates the drawer item. This updates the badge if there are items left in offline edit, otherwise
* there is no badge present.
* This function is called from OfflineEditFragment only.
*/
public void updateBadgeOfflineEditDrawerITem(int size) {
int positionOfOfflineBadeItem = result.getPosition(primaryDrawerItem);
if (size > 0) {
primaryDrawerItem = new PrimaryDrawerItem().withName(R.string.offline_edit_drawer).withIcon(GoogleMaterial.Icon.gmd_local_airport).withIdentifier(ITEM_OFFLINE)
.withBadge(String.valueOf(size)).withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_red_700));
} else {
primaryDrawerItem = new PrimaryDrawerItem().withName(R.string.offline_edit_drawer).withIcon(GoogleMaterial.Icon.gmd_local_airport).withIdentifier(ITEM_OFFLINE);
}
result.updateItemAtPosition(primaryDrawerItem, positionOfOfflineBadeItem);
}

@Override
public void setItemSelected(@NavigationDrawerType Integer type) {
result.setSelection(type, false);
Expand Down
Loading