pffy.mobile.bran.BranActivity.java Source code

Java tutorial

Introduction

Here is the source code for pffy.mobile.bran.BranActivity.java

Source

/**
 * This is free and unencumbered software released into the public domain.
 * 
 * Anyone is free to copy, modify, publish, use, compile, sell, or distribute
 * this software, either in source code form or as a compiled binary, for any
 * purpose, commercial or non-commercial, and by any means.
 * 
 * In jurisdictions that recognize copyright laws, the author or authors of this
 * software dedicate any and all copyright interest in the software to the
 * public domain. We make this dedication for the benefit of the public at large
 * and to the detriment of our heirs and successors. We intend this dedication
 * to be an overt act of relinquishment in perpetuity of all present and future
 * rights to this software under copyright law.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * For more information, please refer to <http://unlicense.org/>
 */

package pffy.mobile.bran;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.ClipDescription;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @name     : BranActivity.java
 * @version  : 2.35 (r35)
 * @updated  : 2015-10-25
 * @license  : http://unlicense.org/ The Unlicense
 * @git      : https://github.com/pffy/android-panera-bread-nutrition-facts-ui2
 *
 */
public class BranActivity extends Activity implements SimpleDrawerFragment.NavigationDrawerCallbacks {

    // CONSTANTS
    public static final String CRLF = "\r\n";

    public static final String URI_GOOGLE_MARKET = "market://details?id=";
    public static final String URI_GOOGLE_PLAY = "https://play.google.com/store/apps/details?id=";

    // Simple drawer fragment
    private SimpleDrawerFragment mSimpleDrawerFragmentCategory;

    private static int sCategoryKey = 0;
    private static int sMenuItem = 0;
    private static String sExportData = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bran); // drawer
        this.makeDrawer();
    }

    // OPTIONS MENU

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.bran, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // Handles options menu item selection
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.action_categories:
            this.mSimpleDrawerFragmentCategory.openDrawer();
            break;
        case R.id.action_share:
            shareByIntent(this);
            break;
        case R.id.action_exit:
            this.exitApp();
            break;
        case R.id.action_about:
            Toast.makeText(getBaseContext(), this.getVersionInfo(), Toast.LENGTH_LONG).show();
            break;
        case R.id.action_feedback:
            this.getFeedback();
            break;
        default:
            // nothing
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    // GETPREFS: Loads persistent data when user re-opens
    @Override
    protected void onResume() {
        super.onResume();
        this.getGlobalPrefs();
    }

    // SETPREFS: Saves persistent data when user clicks away (home or elsewhere)
    @Override
    protected void onPause() {
        super.onPause();
        this.setGlobalPrefs();

    }

    // HELPER METHODS

    // Gets persistent data for non-static context
    private void getGlobalPrefs() {

        // build drawer first
        this.makeDrawer();

        // then import prefs
        SharedPreferences shpref = this.getPreferences(Context.MODE_PRIVATE);
        sCategoryKey = shpref.getInt(getString(R.string.prefkey_int_foodcatid), 0);
        sMenuItem = shpref.getInt(getString(R.string.prefkey_int_fooditemid), 0);
    }

    // Sets persistent data for non-static context
    private void setGlobalPrefs() {

        SharedPreferences shpref = this.getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = shpref.edit();

        editor.putInt(getString(R.string.prefkey_int_foodcatid), sCategoryKey);
        editor.putInt(getString(R.string.prefkey_int_fooditemid), sMenuItem);

        editor.commit();
    }

    // Loads drawer fragment
    private void makeDrawer() {

        mSimpleDrawerFragmentCategory = (SimpleDrawerFragment) getFragmentManager()
                .findFragmentById(R.id.fragment_navigation_drawer);

        // Set up the drawer.
        mSimpleDrawerFragmentCategory.setUp(R.id.fragment_navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawerlayout_categorymenu));
    }

    // Sends user to store front for feedback
    private void getFeedback() {
        final String appPackageName = getPackageName();

        try {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(URI_GOOGLE_MARKET + appPackageName)));
        } catch (android.content.ActivityNotFoundException anfe) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(URI_GOOGLE_PLAY + appPackageName)));
        }
    }

    // Shares nutrition facts as TXT short format
    private static boolean shareByIntent(Context c) {

        if (sExportData.equals("") || sExportData == null) {
            // do not send empty files
            return false;
        }

        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, sExportData);
        sendIntent.setType(ClipDescription.MIMETYPE_TEXT_PLAIN);
        c.startActivity(Intent.createChooser(sendIntent, c.getResources().getString(R.string.msg_shareto)));

        return true;
    }

    // Exits program
    private void exitApp() {

        // Solution found here:
        // http://stackoverflow.com/a/3226743

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    // EVENT HANDLERS (in non-static context)

    @Override
    public void onNavigationDrawerItemSelected(int position) {

        sCategoryKey = position;

        // if new category chosen, select the first item in the list
        sMenuItem = 0;

        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.framelayout_container, PlaceholderFragment.newInstance(position + 1)).commit();
    }

    public void onSectionAttached(int number) {
        // nothing
    }

    // Returns version info
    private String getVersionInfo() {

        // Based on solution found here:
        // http://stackoverflow.com/a/2941199

        PackageInfo info;
        String str = "";

        try {
            info = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
            str += "v" + info.versionName + " (r" + info.versionCode + ")";
        } catch (NameNotFoundException nnfe) {
            str += "No version info.";
        }

        str += CRLF + getString(R.string.msg_authors);
        return str;
    }

    public static class PlaceholderFragment extends Fragment {

        // CONSTANTS
        public static final String DELIMITER = ";";
        public static final String SPACE = " ";
        public static final String COLON = ":";

        private static final String ARG_SECTION_NUMBER = "section_number";

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        // Views
        private TextView mTextViewOutputCategory;
        private Spinner mSpinnerFoodItem;
        private TextView mTextViewOutputNames;
        private TextView mTextViewOutputFacts;
        private LinearLayout mLinearLayoutApp;

        // Large Screen Views
        private TextView mTextViewCalories;
        private TextView mTextViewCaloriesDailyValue;
        private ProgressBar mProgressBarCaloriesDailyValue;

        private TextView mTextViewTotalFat;
        private TextView mTextViewTotalFatDailyValue;
        private ProgressBar mProgressBarTotalFatDailyValue;

        private TextView mTextViewSaturatedFat;
        private TextView mTextViewSaturatedFatDailyValue;
        private ProgressBar mProgressBarSaturatedFatDailyValue;

        private TextView mTextViewTransFat;

        private TextView mTextViewCholesterol;
        private TextView mTextViewCholesterolDailyValue;
        private ProgressBar mProgressBarCholesterolDailyValue;

        private TextView mTextViewSalt;
        private TextView mTextViewSaltDailyValue;
        private ProgressBar mProgressBarSaltDailyValue;

        private TextView mTextViewCarbs;
        private TextView mTextViewCarbsDailyValue;
        private ProgressBar mProgressBarCarbsDailyValue;

        private TextView mTextViewFiber;
        private TextView mTextViewFiberDailyValue;
        private ProgressBar mProgressBarFiberDailyValue;

        private TextView mTextViewSugar;

        private TextView mTextViewProtein;
        private TextView mTextViewProteinDailyValue;
        private ProgressBar mProgressBarProteinDailyValue;

        // Data
        private ArrayAdapter<String> mArrayAdapterFoodItems;
        private String[] mFoodFacts;

        // Choices
        private int mOldKey;

        // Names and food facts
        private int[] mNames;
        private int[] mFacts;

        // nutrition labels
        private int[] mNutrients;
        private int[] mTxts;

        private boolean isTablet;

        public PlaceholderFragment() {
        }

        // Creates a local view in this static context
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            this.checkForTablet();

            // base container in this static context
            View rootView = inflater.inflate(R.layout.fragment_bran, container, false);

            this.mLinearLayoutApp = (LinearLayout) rootView.findViewById(R.id.linearlayout_app);
            this.mTextViewOutputCategory = (TextView) rootView.findViewById(R.id.textview_output_foodcat);
            this.mSpinnerFoodItem = (Spinner) rootView.findViewById(R.id.spinner_fooditem);
            this.mSpinnerFoodItem.setSelection(sMenuItem);
            this.mTextViewOutputNames = (TextView) rootView.findViewById(R.id.textview_output_foodname);

            // Adds event listeners
            this.mSpinnerFoodItem.setOnItemSelectedListener(mChoiceHandler);
            this.mLinearLayoutApp.setOnLongClickListener(mPokeHandler);

            this.startUp();
            this.updateCategoryLabel();
            this.updateFoodNames();

            if (this.isTablet) {

                // Calories
                this.mTextViewCalories = (TextView) rootView.findViewById(R.id.textview_calories_output);
                this.mTextViewCaloriesDailyValue = (TextView) rootView
                        .findViewById(R.id.textview_dv_calories_output);
                this.mProgressBarCaloriesDailyValue = (ProgressBar) rootView
                        .findViewById(R.id.progressbar_dv_calories);

                // Total Fat
                this.mTextViewTotalFat = (TextView) rootView.findViewById(R.id.textview_totalfat_output);
                this.mTextViewTotalFatDailyValue = (TextView) rootView
                        .findViewById(R.id.textview_dv_totalfat_output);
                this.mProgressBarTotalFatDailyValue = (ProgressBar) rootView
                        .findViewById(R.id.progressbar_dv_totalfat);

                // Saturated Fat
                this.mTextViewSaturatedFat = (TextView) rootView.findViewById(R.id.textview_satfat_output);
                this.mTextViewSaturatedFatDailyValue = (TextView) rootView
                        .findViewById(R.id.textview_dv_satfat_output);
                this.mProgressBarSaturatedFatDailyValue = (ProgressBar) rootView
                        .findViewById(R.id.progressbar_dv_satfat);

                // Trans Fat
                this.mTextViewTransFat = (TextView) rootView.findViewById(R.id.textview_transfat_output);

                // Cholesterol
                this.mTextViewCholesterol = (TextView) rootView.findViewById(R.id.textview_cholesterol_output);
                this.mTextViewCholesterolDailyValue = (TextView) rootView
                        .findViewById(R.id.textview_dv_cholesterol_output);
                this.mProgressBarCholesterolDailyValue = (ProgressBar) rootView
                        .findViewById(R.id.progressbar_dv_cholesterol);

                // Salt
                this.mTextViewSalt = (TextView) rootView.findViewById(R.id.textview_salt_output);
                this.mTextViewSaltDailyValue = (TextView) rootView.findViewById(R.id.textview_dv_salt_output);
                this.mProgressBarSaltDailyValue = (ProgressBar) rootView.findViewById(R.id.progressbar_dv_salt);

                // Carbs
                this.mTextViewCarbs = (TextView) rootView.findViewById(R.id.textview_carbs_output);
                this.mTextViewCarbsDailyValue = (TextView) rootView.findViewById(R.id.textview_dv_carbs_output);
                this.mProgressBarCarbsDailyValue = (ProgressBar) rootView.findViewById(R.id.progressbar_dv_carbs);

                // Fiber
                this.mTextViewFiber = (TextView) rootView.findViewById(R.id.textview_fiber_output);
                this.mTextViewFiberDailyValue = (TextView) rootView.findViewById(R.id.textview_dv_fiber_output);
                this.mProgressBarFiberDailyValue = (ProgressBar) rootView.findViewById(R.id.progressbar_dv_fiber);

                // Sugar
                this.mTextViewSugar = (TextView) rootView.findViewById(R.id.textview_sugar_output);

                // Protein
                this.mTextViewProtein = (TextView) rootView.findViewById(R.id.textview_protein_output);
                this.mTextViewProteinDailyValue = (TextView) rootView.findViewById(R.id.textview_dv_protein_output);
                this.mProgressBarProteinDailyValue = (ProgressBar) rootView
                        .findViewById(R.id.progressbar_dv_protein);

                return rootView;
            } else {

                this.mTextViewOutputFacts = (TextView) rootView.findViewById(R.id.textview_output_foodfacts);
                this.mTextViewOutputFacts.setOnLongClickListener(mPokeHandler);
                return rootView;
            }
        }

        @Override
        public void onPause() {
            super.onPause();

            SharedPreferences shpref = getActivity().getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = shpref.edit();
            editor.putInt(getString(R.string.prefkey_int_oldcatkey), this.mOldKey);
            editor.commit();
        }

        @Override
        public void onResume() {
            super.onResume();

            SharedPreferences shpref = getActivity().getPreferences(Context.MODE_PRIVATE);
            this.mOldKey = shpref.getInt(getString(R.string.prefkey_int_oldcatkey), 0);

            this.checkForTablet();
            this.updateCategoryLabel();
            this.updateFoodNames();
        }

        // Sets food name. Returns this object for chaining.
        private PlaceholderFragment setOutputNames(String str) {
            this.mTextViewOutputNames.setText(str);
            return this;
        }

        // Sets food facts details. Returns this object for chaining.
        private PlaceholderFragment setOutputFacts(String str) {
            if (this.mTextViewOutputFacts != null) {
                this.mTextViewOutputFacts.setText(str);
            }
            return this;
        }

        // Checks for tablets
        private void checkForTablet() {

            // Uses solution found here:
            // http://stackoverflow.com/a/5016350

            // small tablets
            if ((getResources().getConfiguration().screenLayout
                    & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
                this.isTablet = true;
            }

            // larger tablets
            if ((getResources().getConfiguration().screenLayout
                    & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                this.isTablet = true;
            }
        }

        // Updates the category label
        private void updateCategoryLabel() {
            String str = getResources().getStringArray(R.array.categories)[sCategoryKey];
            this.mTextViewOutputCategory.setText(str);
        }

        // Updates the list of food menu items
        private void updateFoodNames() {

            // Saves list of facts into string array
            this.mFoodFacts = getResources().getStringArray(this.mFacts[sCategoryKey]);

            // Saves list of names into string array
            String[] names = getResources().getStringArray(this.mNames[sCategoryKey]);
            int namesArraySizeLimit = names.length;

            // Changes list of food names in the spinner via ArrayAdapter
            this.mArrayAdapterFoodItems = new ArrayAdapter<String>(this.getActivity(),
                    android.R.layout.simple_spinner_item, names);

            this.mArrayAdapterFoodItems.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            this.mSpinnerFoodItem.setAdapter(this.mArrayAdapterFoodItems);

            if (sMenuItem < namesArraySizeLimit) {
                this.mSpinnerFoodItem.setSelection(sMenuItem);
            } else {
                this.mSpinnerFoodItem.setSelection(0);
            }
        }

        // Extracts nutrition facts from food item detail string
        private void extractNutritionFacts(String foodFacts) {

            final String[] arr = foodFacts.split(DELIMITER);

            String export = "";
            String str = "";

            this.setOutputNames(arr[10]);

            if (this.isTablet) {

                this.updateCalories(arr[0]);
                this.updateTotalFat(arr[1]);
                this.updateSaturatedFat(arr[2]);
                this.updateTransFat(arr[3]);
                this.updateCholesterol(arr[4]);
                this.updateSalt(arr[5]);
                this.updateCarbs(arr[6]);
                this.updateFiber(arr[7]);
                this.updateSugar(arr[8]);
                this.updateProtein(arr[9]);

            } else {

                for (int i = 0; i < this.mNutrients.length; i++) {
                    str += padDetails(getString(this.mNutrients[i]), arr[i]);
                }

                this.setOutputFacts(str);
            }

            // Export data
            export += getString(R.string.txt_item) + COLON + arr[10];
            for (int i = 0; i < this.mNutrients.length; i++) {
                export += SPACE + getString(this.mTxts[i]) + COLON + arr[i];
            }

            sExportData = export;
        }

        // Updates Calories views
        private void updateCalories(String value) {

            final int percent = 100;
            final int calories = this.getIntegerFromString(value);

            this.mTextViewCalories.setText(value);
            this.mProgressBarCaloriesDailyValue.setProgress(calories);
            this.mTextViewCaloriesDailyValue
                    .setText("" + (calories * percent / getResources().getInteger(R.integer.int_max_cal_diet2000)));
        }

        // Updates Total Fat views
        private void updateTotalFat(String value) {

            final int percent = 100;
            final int totalFat = this.getIntegerFromString(value);

            this.mTextViewTotalFat.setText(value);
            this.mProgressBarTotalFatDailyValue.setProgress(totalFat);
            this.mTextViewTotalFatDailyValue
                    .setText("" + (totalFat * percent / getResources().getInteger(R.integer.int_max_fat_diet2000)));
        }

        // Updates Saturated Fat views
        private void updateSaturatedFat(String value) {

            final int percent = 100;
            final int saturatedFat = this.getIntegerFromString(value);

            this.mTextViewSaturatedFat.setText(value);
            this.mProgressBarSaturatedFatDailyValue.setProgress(saturatedFat);
            this.mTextViewSaturatedFatDailyValue.setText(
                    "" + (saturatedFat * percent / getResources().getInteger(R.integer.int_max_sfat_diet2000)));
        }

        // Updates Trans Fat views
        private void updateTransFat(String value) {
            this.mTextViewTransFat.setText(value);
        }

        // Updates Cholesterol views
        private void updateCholesterol(String value) {

            final int percent = 100;
            final int cholesterol = this.getIntegerFromString(value);

            this.mTextViewCholesterol.setText(value);
            this.mProgressBarCholesterolDailyValue.setProgress(cholesterol);
            this.mTextViewCholesterolDailyValue.setText(
                    "" + (cholesterol * percent / getResources().getInteger(R.integer.int_max_chol_diet2000)));
        }

        // Updates Salt/Sodium views
        private void updateSalt(String value) {

            final int percent = 100;
            final int salt = this.getIntegerFromString(value);

            this.mTextViewSalt.setText(value);
            this.mProgressBarSaltDailyValue.setProgress(salt);
            this.mTextViewSaltDailyValue
                    .setText("" + (salt * percent / getResources().getInteger(R.integer.int_max_salt_diet2000)));
        }

        // Updates Carbs views
        private void updateCarbs(String value) {

            final int percent = 100;
            final int carbs = this.getIntegerFromString(value);

            this.mTextViewCarbs.setText(value);
            this.mProgressBarCarbsDailyValue.setProgress(carbs);
            this.mTextViewCarbsDailyValue
                    .setText("" + (carbs * percent / getResources().getInteger(R.integer.int_max_carbs_diet2000)));
        }

        // Updates Fiber views
        private void updateFiber(String value) {

            final int percent = 100;
            final int fiber = this.getIntegerFromString(value);

            this.mTextViewFiber.setText(value);
            this.mProgressBarFiberDailyValue.setProgress(fiber);
            this.mTextViewFiberDailyValue
                    .setText("" + (fiber * percent / getResources().getInteger(R.integer.int_max_fbr_diet2000)));
        }

        // Updates Sugar views
        private void updateSugar(String value) {
            this.mTextViewSugar.setText(value);
        }

        // Updates Protein views
        private void updateProtein(String value) {

            final int percent = 100;
            final int protein = this.getIntegerFromString(value);

            this.mTextViewProtein.setText(value);
            this.mProgressBarProteinDailyValue.setProgress(protein);
            this.mTextViewProteinDailyValue
                    .setText("" + (protein * percent / getResources().getInteger(R.integer.int_max_pro_diet2000)));
        }

        // Returns an integer from a string input
        private int getIntegerFromString(String str) {
            try {
                return Integer.valueOf(str).intValue();
            } catch (NumberFormatException nfe) {
                return 0;
            }
        }

        // Formats and pads text string, lining the single-space fonts nicely
        private String padDetails(String str, String data) {
            return String.format("%-" + getResources().getInteger(R.integer.int_txtpadding) + "s", str) + COLON
                    + data + CRLF;
        }

        // Maps menu items and nutrition facts
        private void startUp() {

            // mNames and mFacts are parallel arrays

            // name keys
            this.mNames = new int[] { R.array.bsandwiches, R.array.bfavorites, R.array.souffles, R.array.bagels,
                    R.array.creamcheese, R.array.artisanpastries, R.array.sweetrolls, R.array.scones,
                    R.array.muffins, R.array.cakes, R.array.cookies, R.array.pastries, R.array.artisanbreads,
                    R.array.specialtybreads, R.array.espresso, R.array.coffee, R.array.fruitsmoothies,
                    R.array.frozendrinks, R.array.icedbeverages, R.array.bottledbeverages, R.array.softdrinks,
                    R.array.juices, R.array.panini, R.array.sandwiches, R.array.flatbreadsandwiches, R.array.salads,
                    R.array.dressings, R.array.brothbowls, R.array.pastas, R.array.soups, R.array.panerakidsalads,
                    R.array.panerakidpastas, R.array.panerakidsoups, R.array.panerakidsandwiches,
                    R.array.panerakidsides, R.array.sides };

            // food facts keys
            this.mFacts = new int[] { R.array.bsandwiches_details, R.array.bfavorites_details,
                    R.array.souffles_details, R.array.bagels_details, R.array.creamcheese_details,
                    R.array.artisanpastries_details, R.array.sweetrolls_details, R.array.scones_details,
                    R.array.muffins_details, R.array.cakes_details, R.array.cookies_details,
                    R.array.pastries_details, R.array.artisanbreads_details, R.array.specialtybreads_details,
                    R.array.espresso_details, R.array.coffee_details, R.array.fruitsmoothies_details,
                    R.array.frozendrinks_details, R.array.icedbeverages_details, R.array.bottledbeverages_details,
                    R.array.softdrinks_details, R.array.juices_details, R.array.panini_details,
                    R.array.sandwiches_details, R.array.flatbreadsandwiches_details, R.array.salads_details,
                    R.array.dressings_details, R.array.brothbowls_details, R.array.pastas_details,
                    R.array.soups_details, R.array.panerakidsalads_details, R.array.panerakidpastas_details,
                    R.array.panerakidsoups_details, R.array.panerakidsandwiches_details,
                    R.array.panerakidsides_details, R.array.sides_details };

            // parallel arrays for nutrient labels
            // CAL-FAT-SFAT-TFAT-CHOL-SALT-CARBS-FBR-SGR-PRO

            this.mNutrients = new int[] { R.string.nutrient_cal, R.string.nutrient_fat, R.string.nutrient_sfat,
                    R.string.nutrient_tfat, R.string.nutrient_chol, R.string.nutrient_salt, R.string.nutrient_carbs,
                    R.string.nutrient_fbr, R.string.nutrient_sgr, R.string.nutrient_pro };

            this.mTxts = new int[] { R.string.txt_cal, R.string.txt_fat, R.string.txt_sfat, R.string.txt_tfat,
                    R.string.txt_chol, R.string.txt_salt, R.string.txt_carbs, R.string.txt_fbr, R.string.txt_sgr,
                    R.string.txt_pro };
        }

        // EVENT HANDLERS (in static context)

        // Handles attach events, calls from non-static context
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((BranActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
        }

        // Handles choices
        private AdapterView.OnItemSelectedListener mChoiceHandler = new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> av, View v, int pos, long id) {

                switch (av.getId()) {
                case R.id.spinner_fooditem:
                    // food item changed

                    sMenuItem = pos;
                    try {
                        extractNutritionFacts(mFoodFacts[pos]);
                    } catch (ArrayIndexOutOfBoundsException ex) {
                        // unlikely, but just in case
                        extractNutritionFacts(mFoodFacts[0]);

                    } catch (NullPointerException ex) {
                        // unlikely, but just in case
                        extractNutritionFacts(mFoodFacts[0]);

                    } finally {
                        // nothing
                    }

                    break;
                default:
                    // nothing
                    break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> av) {
                // nothing
            }
        };

        // Handles pokes
        private View.OnLongClickListener mPokeHandler = new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                switch (v.getId()) {
                case R.id.textview_output_foodfacts:
                    shareByIntent(getActivity());
                    return true;
                case R.id.linearlayout_app:
                    getActivity().openOptionsMenu();
                    return true;
                default:
                    // nothing
                    break;
                }

                return false;
            }
        };
    } // leaving static context

}