diff --git a/app/app-release.apk b/app/app-release.apk index 157e35c667ce..db6de5b6b307 100644 Binary files a/app/app-release.apk and b/app/app-release.apk differ diff --git a/app/build.gradle b/app/build.gradle index d67d0ff826f8..89deb7c061bc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "openfoodfacts.github.scrachx.openfood" minSdkVersion 16 targetSdkVersion 23 - versionCode 20 - versionName "0.2.2" + versionCode 21 + versionName "0.2.3" } dexOptions { diff --git a/app/src/main/java/openfoodfacts/github/scrachx/openfood/fragments/AlertUserFragment.java b/app/src/main/java/openfoodfacts/github/scrachx/openfood/fragments/AlertUserFragment.java index b8884778e85c..0c776f2a4c62 100644 --- a/app/src/main/java/openfoodfacts/github/scrachx/openfood/fragments/AlertUserFragment.java +++ b/app/src/main/java/openfoodfacts/github/scrachx/openfood/fragments/AlertUserFragment.java @@ -1,15 +1,23 @@ package openfoodfacts.github.scrachx.openfood.fragments; +import android.content.Context; import android.content.SharedPreferences; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.MaterialDialog; import com.github.clans.fab.FloatingActionButton; + +import net.steamcrafted.loadtoast.LoadToast; + import org.apache.commons.collections.IteratorUtils; import java.util.ArrayList; import java.util.List; @@ -19,6 +27,7 @@ import butterknife.OnClick; import openfoodfacts.github.scrachx.openfood.R; import openfoodfacts.github.scrachx.openfood.models.Allergen; +import openfoodfacts.github.scrachx.openfood.models.FoodAPIRestClientUsage; import openfoodfacts.github.scrachx.openfood.views.adapters.AllergensAdapter; public class AlertUserFragment extends BaseFragment { @@ -72,26 +81,73 @@ protected void onAddAllergens() { if(a.getIdAllergen().contains("en:")) allS.add(a.getName().substring(a.getName().indexOf(":")+1)); } } - new MaterialDialog.Builder(mView.getContext()) - .title(R.string.title_dialog_alert) - .items(allS) - .itemsCallback(new MaterialDialog.ListCallback() { - @Override - public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { - all.get(which).setEnable("true"); - all.get(which).save(); - boolean canAdd = true; - for(Allergen a : mAllergens) { - if(a.getName().equals(all.get(which).getName())) canAdd = false; + if(allS.size() > 0) { + new MaterialDialog.Builder(mView.getContext()) + .title(R.string.title_dialog_alert) + .items(allS) + .itemsCallback(new MaterialDialog.ListCallback() { + @Override + public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { + all.get(which).setEnable("true"); + all.get(which).save(); + boolean canAdd = true; + for(Allergen a : mAllergens) { + if(a.getName().equals(all.get(which).getName())) canAdd = false; + } + if(canAdd) { + mAllergens.add(all.get(which)); + mAdapter.notifyItemInserted(mAllergens.size() - 1); + mRvAllergens.scrollToPosition(mAdapter.getItemCount() - 1); + } } - if(canAdd) { - mAllergens.add(all.get(which)); - mAdapter.notifyItemInserted(mAllergens.size() - 1); - mRvAllergens.scrollToPosition(mAdapter.getItemCount() - 1); - } - } - }) - .show(); + }) + .show(); + } else { + ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); + if(isConnected) { + final LoadToast lt = new LoadToast(getContext()); + lt.setText(getContext().getString(R.string.toast_retrieving)); + lt.setBackgroundColor(getContext().getResources().getColor(R.color.indigo_600)); + lt.setTextColor(getContext().getResources().getColor(R.color.white)); + lt.show(); + new MaterialDialog.Builder(mView.getContext()) + .title(R.string.title_dialog_alert) + .content(R.string.info_download_data) + .positiveText(R.string.txtYes) + .negativeText(R.string.txtNo) + .onPositive(new MaterialDialog.SingleButtonCallback() { + @Override + public void onClick(@NonNull final MaterialDialog dialog, @NonNull DialogAction which) { + final SharedPreferences.Editor editor = mSettings.edit(); + FoodAPIRestClientUsage api = new FoodAPIRestClientUsage(); + api.getAllergens(new FoodAPIRestClientUsage.OnAllergensCallback() { + @Override + public void onAllergensResponse(boolean value) { + if (!value) { + editor.putBoolean("errorAllergens", true); + editor.apply(); + } else { + editor.putBoolean("errorAllergens", false); + editor.apply(); + } + lt.success(); + dialog.hide(); + } + }); + } + }) + .show(); + } else { + new MaterialDialog.Builder(mView.getContext()) + .title(R.string.title_dialog_alert) + .content(R.string.info_download_data_connection) + .neutralText(R.string.txtOk) + .show(); + } + } + } } diff --git a/app/src/main/java/openfoodfacts/github/scrachx/openfood/models/FoodAPIRestClientUsage.java b/app/src/main/java/openfoodfacts/github/scrachx/openfood/models/FoodAPIRestClientUsage.java index 5cb49d7959d5..209e70a2a857 100644 --- a/app/src/main/java/openfoodfacts/github/scrachx/openfood/models/FoodAPIRestClientUsage.java +++ b/app/src/main/java/openfoodfacts/github/scrachx/openfood/models/FoodAPIRestClientUsage.java @@ -264,8 +264,12 @@ public void onRetry(int retryNo) { }); } - public void getAllergens() { - FoodAPIRestClient.getSync("/allergens.json", null, new AsyncHttpResponseHandler() { + public interface OnAllergensCallback { + public void onAllergensResponse(boolean value); + } + + public void getAllergens(final OnAllergensCallback onAllergensCallback) { + FoodAPIRestClient.getAsync("/allergens.json", null, new AsyncHttpResponseHandler() { @Override public void onStart() { @@ -283,11 +287,14 @@ public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] hea Allergen al = new Allergen(a.getUrl(),a.getName(),a.getProducts(),a.getIdAllergen()); al.save(); } + onAllergensCallback.onAllergensResponse(true); } catch (InvalidFormatException e) { e.printStackTrace(); + onAllergensCallback.onAllergensResponse(false); } } catch (IOException e) { e.printStackTrace(); + onAllergensCallback.onAllergensResponse(false); } } diff --git a/app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SplashActivity.java b/app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SplashActivity.java index 9d5819416db5..c52749f8fd57 100644 --- a/app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SplashActivity.java +++ b/app/src/main/java/openfoodfacts/github/scrachx/openfood/views/SplashActivity.java @@ -3,6 +3,8 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.os.AsyncTask; import android.os.Bundle; @@ -26,8 +28,16 @@ public void onCreate(Bundle icicle) { super.onCreate(icicle); settings = getSharedPreferences("prefs", 0); + SharedPreferences.Editor editor = settings.edit(); boolean firstRun = settings.getBoolean("firstRun", true); - if (!firstRun) { + boolean errorAdditives = settings.getBoolean("errorAdditives", true); + boolean errorAllergens = settings.getBoolean("errorAllergens", true); + if(!errorAdditives && ! errorAllergens) { + editor.putBoolean("firstRun", false); + editor.apply(); + firstRun = false; + } + if (!firstRun ) { Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); SplashActivity.this.startActivity(mainIntent); SplashActivity.this.finish(); @@ -62,33 +72,34 @@ protected Boolean doInBackground(Void... arg0) { String json = null; String json1 = null; try { - InputStream is = getAssets().open("additives_fr.json"); - int size = is.available(); - byte[] buffer = new byte[size]; - is.read(buffer); - is.close(); - json = new String(buffer, "UTF-8"); - ObjectMapper objectMapper = new ObjectMapper(); - List la = objectMapper.readValue(json, new TypeReference>() {}); - for (Additive a : la) { - Additive ta = new Additive(a.getCode(), a.getName(), a.getRisk()); - ta.save(); + boolean errorAdditives = settings.getBoolean("errorAdditives", true); + if(errorAdditives) { + InputStream is = getAssets().open("additives_fr.json"); + int size = is.available(); + byte[] buffer = new byte[size]; + is.read(buffer); + is.close(); + json = new String(buffer, "UTF-8"); + ObjectMapper objectMapper = new ObjectMapper(); + List la = objectMapper.readValue(json, new TypeReference>() {}); + for (Additive a : la) { + Additive ta = new Additive(a.getCode(), a.getName(), a.getRisk()); + ta.save(); + } + + InputStream is1 = getAssets().open("additives_en.json"); + int size1 = is1.available(); + byte[] buffer1 = new byte[size1]; + is1.read(buffer1); + is1.close(); + json1 = new String(buffer1, "UTF-8"); + ObjectMapper objectMapper1 = new ObjectMapper(); + List la1 = objectMapper1.readValue(json1, new TypeReference>() {}); + for (Additive a : la1) { + Additive ta = new Additive(a.getCode(), a.getName(), a.getRisk()); + ta.save(); + } } - - InputStream is1 = getAssets().open("additives_en.json"); - int size1 = is1.available(); - byte[] buffer1 = new byte[size1]; - is1.read(buffer1); - is1.close(); - json1 = new String(buffer1, "UTF-8"); - ObjectMapper objectMapper1 = new ObjectMapper(); - List la1 = objectMapper1.readValue(json1, new TypeReference>() {}); - for (Additive a : la1) { - Additive ta = new Additive(a.getCode(), a.getName(), a.getRisk()); - ta.save(); - } - FoodAPIRestClientUsage api = new FoodAPIRestClientUsage(); - api.getAllergens(); return true; } catch (IOException ex) { ex.printStackTrace(); @@ -97,16 +108,51 @@ protected Boolean doInBackground(Void... arg0) { } @Override - protected void onPostExecute(Boolean result) { + protected void onPostExecute(final Boolean result) { super.onPostExecute(result); - if (result) lt.success(); - else lt.error(); - SharedPreferences.Editor editor = settings.edit(); - editor.putBoolean("firstRun", false); - editor.apply(); - Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); - startActivity(mainIntent); - finish(); + final SharedPreferences.Editor editor = settings.edit(); + boolean errorAllergens = settings.getBoolean("errorAllergens", true); + ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); + NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); + boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting(); + if(isConnected) { + if(errorAllergens) { + FoodAPIRestClientUsage api = new FoodAPIRestClientUsage(); + api.getAllergens(new FoodAPIRestClientUsage.OnAllergensCallback() { + @Override + public void onAllergensResponse(boolean value) { + if (result && value) { + lt.success(); + editor.putBoolean("firstRun", false); + editor.apply(); + } + if(!value){ + lt.error(); + editor.putBoolean("errorAllergens", true); + editor.apply(); + } else { + editor.putBoolean("errorAllergens", false); + editor.apply(); + } + Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); + startActivity(mainIntent); + finish(); + } + }); + } + } else { + if(!result){ + lt.error(); + editor.putBoolean("errorAdditives", true); + editor.apply(); + } else { + editor.putBoolean("errorAdditives", false); + editor.apply(); + } + Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class); + startActivity(mainIntent); + finish(); + } } } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a82b32483fc..11fb3457d3b5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -152,6 +152,8 @@ Permission information Please activate permission in the settings Storage is needed to take or choose pictures + You need to download some data to get this functionality working, please press yes to download it. + You need to download some data to get this functionality working. Please activate internet connection.