<commit_before>package org.inaturalist.android;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewCallback;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ObservationGridAdapter extends ArrayAdapter<JSONObject> {

    private List<JSONObject> mItems;
    private Context mContext;
    private ArrayList<JSONObject> mOriginalItems;
    private int mDimension;

    public ObservationGridAdapter(Context context, int dimension, List<JSONObject> objects) {
        super(context, R.layout.guide_taxon_item, objects);

        mItems = objects;
        mOriginalItems = new ArrayList<JSONObject>(mItems);
        mContext = context;
        mDimension = dimension;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public JSONObject getItem(int index) {
        return mItems.get(index);
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = inflater.inflate(R.layout.guide_taxon_item, parent, false);
        JSONObject item = mItems.get(position);

        TextView idName = (TextView) view.findViewById(R.id.id_name);
        final JSONObject taxon = item.optJSONObject("taxon");

        if (taxon != null) {
            String idNameString = getTaxonName(taxon);
            if (idNameString != null) {
                idName.setText(idNameString);
            } else {
                idName.setText(mContext.getResources().getString(R.string.unknown));
            }
        } else {
            String idNameStr = item.isNull("species_guess") ?
                    mContext.getResources().getString(R.string.unknown) :
                    item.optString("species_guess", mContext.getResources().getString(R.string.unknown));
            idName.setText(idNameStr);
        }


        ImageView taxonPic = (ImageView) view.findViewById(R.id.taxon_pic);

        taxonPic.setLayoutParams(new RelativeLayout.LayoutParams(
                mDimension, mDimension));

        JSONArray observationPhotos;
        boolean isNewApi = !item.has("observation_photos");
        try {
            observationPhotos = item.getJSONArray(isNewApi ? "photos" : "observation_photos");
        } catch (JSONException e1) {
            e1.printStackTrace();
            observationPhotos = new JSONArray();
        }

        if (observationPhotos.length() > 0) {
            JSONObject observationPhoto;
            try {
                String url;
                observationPhoto = observationPhotos.getJSONObject(0);

                if (isNewApi) {
                    url = observationPhoto.optString("url");
                    String extension = url.substring(url.lastIndexOf(".") + 1);
                    url = url.substring(0, url.lastIndexOf("/") + 1) + "medium." + extension;
                } else {
                    JSONObject innerPhoto = observationPhoto.optJSONObject("photo");
                    url = (innerPhoto.isNull("small_url") ? innerPhoto.optString("original_url") : innerPhoto.optString("small_url"));
                    if ((url == null) || (url.length() == 0)) url = innerPhoto.optString("url");
                }
                UrlImageViewHelper.setUrlDrawable(taxonPic, url, ObservationPhotosViewer.observationIcon(item), new UrlImageViewCallback() {
                    @Override
                    public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {
                        imageView.setLayoutParams(new RelativeLayout.LayoutParams(
                                mDimension, mDimension
                        ));
                    }

                    @Override
                    public Bitmap onPreSetBitmap(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {
                        // No post-processing of bitmap
                        return loadedBitmap;
                    }<commit_msg>Use higher-resolution version of observation photos in project details screen<commit_after>package org.inaturalist.android;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewCallback;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ObservationGridAdapter extends ArrayAdapter<JSONObject> {

    private List<JSONObject> mItems;
    private Context mContext;
    private ArrayList<JSONObject> mOriginalItems;
    private int mDimension;

    public ObservationGridAdapter(Context context, int dimension, List<JSONObject> objects) {
        super(context, R.layout.guide_taxon_item, objects);

        mItems = objects;
        mOriginalItems = new ArrayList<JSONObject>(mItems);
        mContext = context;
        mDimension = dimension;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public JSONObject getItem(int index) {
        return mItems.get(index);
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view = inflater.inflate(R.layout.guide_taxon_item, parent, false);
        JSONObject item = mItems.get(position);

        TextView idName = (TextView) view.findViewById(R.id.id_name);
        final JSONObject taxon = item.optJSONObject("taxon");

        if (taxon != null) {
            String idNameString = getTaxonName(taxon);
            if (idNameString != null) {
                idName.setText(idNameString);
            } else {
                idName.setText(mContext.getResources().getString(R.string.unknown));
            }
        } else {
            String idNameStr = item.isNull("species_guess") ?
                    mContext.getResources().getString(R.string.unknown) :
                    item.optString("species_guess", mContext.getResources().getString(R.string.unknown));
            idName.setText(idNameStr);
        }


        ImageView taxonPic = (ImageView) view.findViewById(R.id.taxon_pic);

        taxonPic.setLayoutParams(new RelativeLayout.LayoutParams(
                mDimension, mDimension));

        JSONArray observationPhotos;
        boolean isNewApi = !item.has("observation_photos");
        try {
            observationPhotos = item.getJSONArray(isNewApi ? "photos" : "observation_photos");
        } catch (JSONException e1) {
            e1.printStackTrace();
            observationPhotos = new JSONArray();
        }

        if (observationPhotos.length() > 0) {
            JSONObject observationPhoto;
            try {
                String url;
                observationPhoto = observationPhotos.getJSONObject(0);

                if (isNewApi) {
                    url = observationPhoto.optString("url");
                } else {
                    JSONObject innerPhoto = observationPhoto.optJSONObject("photo");
                    url = (innerPhoto.isNull("small_url") ? innerPhoto.optString("original_url") : innerPhoto.optString("small_url"));
                    if ((url == null) || (url.length() == 0)) url = innerPhoto.optString("url");
                }

                if ((url != null) && (url.length() > 0)) {
                    String extension = url.substring(url.lastIndexOf(".") + 1);
                    url = url.substring(0, url.lastIndexOf("/") + 1) + "medium." + extension;
                }

                UrlImageViewHelper.setUrlDrawable(taxonPic, url, ObservationPhotosViewer.observationIcon(item), new UrlImageViewCallback() {
                    @Override
                    public void onLoaded(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {
                        imageView.setLayoutParams(new RelativeLayout.LayoutParams(
                                mDimension, mDimension
                        ));
                    }

                    @Override
                    public Bitmap onPreSetBitmap(ImageView imageView, Bitmap loadedBitmap, String url, boolean loadedFromCache) {
                        // No post-processing of bitmap
                        return loadedBitmap;
                    }
{'fraction_non_alphanumeric': 0.09506341054396814, 'fraction_numerical': 0.001781783880096426, 'mean_word_length': 2.4102930664760542, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5046662', 'n_tokens_mistral': 2571, 'n_tokens_neox': 2411, 'n_words': 504}