Advertisement
kivaari

Untitled

Jan 24th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.99 KB | None | 0 0
  1. package com.example.istroweil.ichfahrcar;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.drawable.BitmapDrawable;
  7. import android.graphics.drawable.Drawable;
  8. import android.os.Build;
  9. import android.support.annotation.RequiresApi;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.ImageView;
  15. import android.widget.Space;
  16. import android.widget.TextView;
  17.  
  18. import org.json.JSONArray;
  19. import org.json.JSONException;
  20. import org.json.JSONObject;
  21.  
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.Map;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32.  
  33. /**
  34.  * Created by kivaari on 27.12.2016.
  35.  */
  36.  
  37. public class POIListAdapter extends ArrayAdapter<JSONObject>  implements AsyncResponse{
  38.     //TODO change this to HashMap, give Map to downloader task and access them from here, store position in json
  39.     Map<Integer, Bitmap> bmpContainer;
  40.  
  41.     public POIListAdapter(Context context, ArrayList<JSONObject> pois){
  42.         super(context, 0, pois);
  43.         bmpContainer = new HashMap<>();
  44.     }
  45.  
  46.     //TODO make it like this:http://stackoverflow.com/questions/14188685/android-onclick-event-on-custom-listview-adapter-not-working
  47.  
  48.     @RequiresApi(api = Build.VERSION_CODES.M)
  49.     @Override
  50.     public View getView(int position, View convertView, ViewGroup parent){
  51.         JSONObject jsonItem = getItem(position);
  52.         String name = "";
  53.         String type = "";
  54.         String address = "";
  55.         String distance = "";
  56.         Boolean selected = false;
  57.         Boolean visible = false;
  58.         try {
  59.             selected = jsonItem.getBoolean("selected");
  60.             visible = jsonItem.getBoolean("visible");
  61.             name = jsonItem.getString("name");
  62.             type = jsonItem.getString("type");
  63.             address = jsonItem.getString("address");
  64.         } catch (JSONException e) {
  65.             e.printStackTrace();
  66.         }
  67.         Bitmap bmp = null;
  68.         Drawable icon = null;
  69.  
  70.  
  71.         View test = new Space(getContext());    //dummy to test for class equivalence
  72.  
  73.         if(visible){
  74.             if((convertView == null) || (convertView.getClass() == test.getClass()))
  75.                 convertView = LayoutInflater.from(getContext()).inflate(R.layout.component_poi, parent, false);
  76.         }
  77.  
  78.         if(!visible) {
  79.             convertView = new Space(getContext());
  80.             return convertView;
  81.         }
  82.  
  83.         TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
  84.         TextView tvAddress = (TextView) convertView.findViewById(R.id.tvAddress);
  85.         TextView tvDistance = (TextView) convertView.findViewById(R.id.tvDistance);
  86.         ImageView ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
  87.  
  88.         if(selected)
  89.             convertView.setBackgroundColor(getContext().getResources().getColor(R.color.colorLvSelected, null));
  90.         else
  91.             convertView.setBackgroundColor(getContext().getResources().getColor(R.color.colorLvUnselected, null));
  92.  
  93.         try {
  94.             distance = calculateDistance(jsonItem.getDouble("lat"), jsonItem.getDouble("lon"));
  95.         } catch (JSONException e) {
  96.             e.printStackTrace();
  97.         }
  98.  
  99.  
  100.         if(jsonItem.toString().contains("icon")){   //check if there is an icon link present
  101.             if(jsonItem.toString().contains("bmp") == false){   //download bitmap if it is not set yet
  102.                 try {
  103.                     File file = new File(getContext().getFilesDir(), jsonItem.getString("icon"));
  104.  
  105.                     if(!file.exists()) {
  106.                         DownloadImageTask task = (DownloadImageTask) new DownloadImageTask(ivIcon, getContext(), position)
  107.                                 .execute("https://www.fbi.h-da.de/fileadmin/personal/h.wiedling/daten/"
  108.                                         + jsonItem.getString("icon"));
  109.  
  110.                         task.delegate = this;
  111.                     }
  112.                     else{
  113.                         try {
  114.                             BitmapFactory.Options options = new BitmapFactory.Options();
  115.                             options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  116.                             Bitmap bmp_file = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
  117.                             ivIcon.setImageBitmap(bmp_file);
  118.  
  119.                             bmpContainer.put(position, (Bitmap) bmp_file);
  120.  
  121.                             try {
  122.                                 jsonItem.put("bmp", position);
  123.                             } catch (JSONException e) {
  124.                                 e.printStackTrace();
  125.                             }
  126.  
  127.                         } catch (Exception e) {
  128.                             e.printStackTrace();
  129.                         }
  130.                     }
  131.                 } catch (JSONException e) {
  132.                     e.printStackTrace();
  133.                 }
  134.             }
  135.             else{   //set ivIcon image if bitmap has already been saved
  136.                 try {
  137.                     //ISSUE this will cause bugs if bmps get removed or moved around, never redownload icons without clearing list
  138.                     bmp = bmpContainer.get(jsonItem.getInt("bmp"));
  139.  
  140.                 } catch (JSONException e) {
  141.                     e.printStackTrace();
  142.                 }
  143.                 Drawable drawable = new BitmapDrawable(getContext().getResources(), bmp);
  144.                 ivIcon.setImageDrawable(drawable);
  145.             }
  146.         }
  147.         else{
  148.             switch (type) {
  149.                 case "fuel":
  150.                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  151.                         icon = getContext().getDrawable(R.drawable.fuel_poi);
  152.                     } else {
  153.                         icon = getContext().getResources().getDrawable(R.drawable.fuel_poi);
  154.                     }
  155.                     break;
  156.                 case "amenity":
  157.                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  158.                         icon = getContext().getDrawable(R.drawable.amenity);
  159.                     } else {
  160.                         icon = getContext().getResources().getDrawable(R.drawable.amenity);
  161.                     }
  162.                     break;
  163.                 case "pub":
  164.                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  165.                         icon = getContext().getDrawable(R.drawable.pub);
  166.                     } else {
  167.                         icon = getContext().getResources().getDrawable(R.drawable.pub);
  168.                     }
  169.                     break;
  170.             }
  171.             ivIcon.setImageDrawable(icon);
  172.         }
  173.  
  174.         tvAddress.setText(address);
  175.         tvName.setText(name);
  176.         tvDistance.setText(distance);
  177.  
  178.         return convertView;
  179.     }
  180.  
  181.     public String calculateDistance(Double lat, Double lon/*, Coordinate start*/){
  182.         //TODO add actual distance calculation
  183.         Double dist = Car.distance(lat, lon, 1);
  184.         if(dist == -1){
  185.             return "";
  186.         }
  187.         String unit = "m";
  188.         String result = Integer.toString(dist.intValue());
  189.         result = result.concat(unit);
  190.  
  191.         return result;
  192.     }
  193.  
  194.     @Override
  195.     public void processFinish(Object output, int position, processIDs processID) {
  196.         JSONObject jsonObject = getItem(position);
  197.         Bitmap bmp = (Bitmap) output ;
  198.  
  199.         FileOutputStream out = null;
  200.         try {
  201.             File file = new File(getContext().getFilesDir(), jsonObject.getString("icon"));
  202.             file.createNewFile();
  203.             out = new FileOutputStream(file);
  204.              bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
  205.             // PNG is a lossless format, the compression factor (100) is ignored
  206.         } catch (Exception e) {
  207.             e.printStackTrace();
  208.         } finally {
  209.             try {
  210.                 if (out != null) {
  211.                     out.close();
  212.                 }
  213.             } catch (IOException e) {
  214.                 e.printStackTrace();
  215.             }
  216.         }
  217.  
  218.         bmpContainer.put(position, (Bitmap) output);
  219.  
  220.         try {
  221.             jsonObject.put("bmp", position);
  222.         } catch (JSONException e) {
  223.             e.printStackTrace();
  224.         }
  225.     }
  226.  
  227.     @Override
  228.     public void processFinish(Object output, processIDs processID) {
  229.  
  230.     }
  231.  
  232.     @Override
  233.     public void processFinish(processIDs processID) {
  234.  
  235.         //clear selected from adapter once drive is done
  236.         if(processID.equals(processIDs.DRIVEFINISHED)){
  237.             for(int i = 0; i < getCount(); i++){
  238.                 try {
  239.                     getItem(i).put("selected", false);
  240.                 } catch (JSONException e) {
  241.                     e.printStackTrace();
  242.                 }
  243.             }
  244.         }
  245.     }
  246.  
  247.  
  248.     public void filterPOIs(JSONObject newFilter){
  249.         String namef;
  250.         String typef;
  251.  
  252.         for(int i = 0; i < getCount(); i++){
  253.             try {
  254.                 getItem(i).put("visible", false);
  255.             } catch (JSONException e) {
  256.                 e.printStackTrace();
  257.             }
  258.         }
  259.  
  260.         try {
  261.             if (newFilter.toString().contains("namef")) {
  262.                 namef = newFilter.getString("namef").toUpperCase();
  263.                 Pattern p = Pattern.compile(namef);
  264.                 Matcher m;
  265.  
  266.                 for (int i = 0; i < getCount(); i++) {
  267.                     String name = getItem(i).getString("name").toUpperCase();
  268.                     m = p.matcher(name);
  269.  
  270.                     if(m.find())
  271.                         getItem(i).put("visible", true);
  272.                 }
  273.             }
  274.         }
  275.         catch(Exception e){
  276.  
  277.         }
  278.  
  279.         try {
  280.             if (newFilter.toString().contains("typef")) {
  281.                 typef = newFilter.getString("typef").toUpperCase();
  282.                 Pattern p = Pattern.compile(typef);
  283.                 Matcher m;
  284.  
  285.                 for (int i = 0; i < getCount(); i++) {
  286.                     String type = getItem(i).getString("type").toUpperCase();
  287.                     m = p.matcher(type);
  288.  
  289.                     if(m.find() || typef.equals("ALL"))
  290.                         getItem(i).put("visible", true);
  291.                 }
  292.             }
  293.         }
  294.         catch(Exception e){
  295.  
  296.         }
  297.  
  298.         try {
  299.             if (newFilter.toString().contains("history")) {
  300.                 for(int i = 0; i < getCount(); i++){
  301.                     JSONObject jsonObject = getItem(i);
  302.  
  303.                     if(jsonObject.getBoolean("history")){
  304.                         jsonObject.put("visible", true);
  305.                     }
  306.                     else{
  307.                         jsonObject.put("visible", false);
  308.                     }
  309.                 }
  310.             }
  311.         }
  312.         catch(Exception e){
  313.  
  314.         }
  315.  
  316.         notifyDataSetChanged();
  317.     }
  318.  
  319.     public void updateHistory(JSONArray h){
  320.         try {
  321.             for(int i = 0; i < getCount(); i++){
  322.                 getItem(i).put("history", false);
  323.             }
  324.  
  325.  
  326.  
  327.             for (int i = 0; i < h.length(); i++) {
  328.                 JSONObject jsonHistory = h.getJSONObject(i);
  329.  
  330.                 for(int j = 0; j < getCount(); j++){
  331.                     JSONObject jsonAdapter = getItem(j);
  332.  
  333.                     if (jsonHistory.getString("name").equals(jsonAdapter.getString("name")) &&
  334.                             jsonHistory.getString("address").equals(jsonAdapter.getString("address"))){
  335.                         jsonAdapter.put("history", true);
  336.                     }
  337.                 }
  338.  
  339.             }
  340.         }
  341.         catch (Exception e){
  342.  
  343.         }
  344.     }
  345.  
  346.     public void makeAllInvisible(){
  347.         for(int i = 0; i < getCount(); i++){
  348.             try {
  349.                 getItem(i).put("visible", false);
  350.             } catch (JSONException e) {
  351.                 e.printStackTrace();
  352.             }
  353.         }
  354.     }
  355.  
  356.     public void unselectAll() {
  357.         for(int i = 0; i < getCount(); i++){
  358.             try {
  359.                 getItem(i).put("selected", false);
  360.             } catch (JSONException e) {
  361.                 e.printStackTrace();
  362.             }
  363.         }
  364.     }
  365.  
  366.     public void makeAllVisible() {
  367.         for(int i = 0; i < getCount(); i++){
  368.             try {
  369.                 getItem(i).put("visible", true);
  370.             } catch (JSONException e) {
  371.                 e.printStackTrace();
  372.             }
  373.         }
  374.     }
  375.  
  376.     public int getSelectedAmount() {
  377.         int x = 0;
  378.  
  379.         for(int i = 0; i < getCount(); i++){
  380.             try {
  381.                 if(getItem(i).getBoolean("selected"))
  382.                     x++;
  383.             } catch (JSONException e) {
  384.                 e.printStackTrace();
  385.             }
  386.         }
  387.  
  388.         return x;
  389.     }
  390.  
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement