Android Open Source - kluster-android Photo Adapter






From Project

Back to project page kluster-android.

License

The source code is released under:

Apache License

If you think the Android project kluster-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.cs446.kluster.data.serialize;
/* ww w  .  ja  v a2s  . c o m*/
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import android.annotation.TargetApi;

import com.cs446.kluster.map.MapUtils;
import com.cs446.kluster.models.Photo;
import com.google.android.gms.maps.model.LatLng;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

public class PhotoAdapter extends TypeAdapter<Photo> {
  
    public Photo read(JsonReader reader) throws IOException {
      if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
      }
      
      String photoId = null;
      String eventId = null;
      String userId = null;
      Double[] loc = null;
      List<String> tags = null;
      String[] urls = new String[4];
      Date createdAt = new Date();
      String[] rating = new String[2];

      reader.beginObject();
      while (reader.hasNext()) {
          String name = reader.nextName();
          if (name.equals("_id")) {
              photoId = reader.nextString();
          } else if (name.equals("_event")) {
              eventId = reader.nextString();
          } else if (name.equals("_contributor")) {
              userId = reader.nextString();
          } else if (name.equals("loc") && reader.peek() != JsonToken.NULL) {
            List<Double> list = readDoublesArray(reader);
              Collections.reverse(list);
              loc = list.toArray(new Double[2]);
          } else if (name.equals("time")) {
            try {
          createdAt = Photo.getDateFormat().parse(reader.nextString());
        } catch (ParseException e) {
          createdAt = new Date();
        }
          } else if (name.equals("url") && reader.peek() != JsonToken.NULL) {
            reader.beginObject();
            reader.nextName();
            urls[1] = reader.nextString(); // small
            reader.nextName();
            urls[2] = reader.nextString(); // medium
            reader.nextName();
            urls[0] = reader.nextString(); // default
            reader.nextName();
            urls[3] = reader.nextString(); // thumb
            reader.endObject();
          } else if (name.equals("tags") && reader.peek() != JsonToken.NULL) {
              tags = readStringArray(reader);
          } else if (name.equals("rating")) {
            reader.beginObject();
            reader.nextName();
            rating[0] = Integer.toString(reader.nextInt());
            reader.nextName();
            rating[1] = Integer.toString(reader.nextInt());
            reader.endObject();
          } else {
              reader.skipValue();
          }
      }
      reader.endObject();

      return new Photo(photoId, userId, eventId, new LatLng(loc[0], loc[1]), createdAt, urls, tags, rating);
    }
    
    public void write(JsonWriter writer, Photo value) throws IOException {
      if (value == null) {
        writer.nullValue();
        return;
      }
      writer.value(value.getPhotoId());
      writer.value(value.getUserId());
      writer.value(value.getEventId());
      writer.value(MapUtils.latLngToString(value.getLocation()));
      writer.value(Photo.getDateFormat().format(value.getDate()));

    }
    
    @TargetApi(11)
    public static List<String> readStringArray(JsonReader reader) throws IOException {
        List<String> strings = new ArrayList<String>();

        reader.beginArray();
        while (reader.hasNext()) {
            strings.add(reader.nextString());
        }
        reader.endArray();
        return strings;
    }

    @TargetApi(11)
    public static List<Double> readDoublesArray(JsonReader reader) throws IOException {
        List<Double> doubles = new ArrayList<Double>();

        reader.beginArray();
        while (reader.hasNext()) {
            doubles.add(reader.nextDouble());
        }
        reader.endArray();
        return doubles;
    }
  }




Java Source Code List

android.app.GridFragment.java
android.app.NonSwipeableViewPager.java
android.app.ScaleImageView.java
com.cs446.kluster.ConfigManager.java
com.cs446.kluster.KlusterApplication.java
com.cs446.kluster.cache.CacheUtils.java
com.cs446.kluster.cache.DiskLruCache.java
com.cs446.kluster.cache.DiskLruImageCache.java
com.cs446.kluster.cache.KlusterCache.java
com.cs446.kluster.data.AbstractContentStorageAdapter.java
com.cs446.kluster.data.EventProvider.java
com.cs446.kluster.data.EventStorageAdapter.java
com.cs446.kluster.data.PhotoProvider.java
com.cs446.kluster.data.PhotoStorageAdapter.java
com.cs446.kluster.data.SearchProvider.java
com.cs446.kluster.data.SearchStorageAdapter.java
com.cs446.kluster.data.StorageAdapter.java
com.cs446.kluster.data.StorageConstants.java
com.cs446.kluster.data.serialize.AuthUserAdapter.java
com.cs446.kluster.data.serialize.EventAdapter.java
com.cs446.kluster.data.serialize.PhotoAdapter.java
com.cs446.kluster.data.serialize.SerializerUtils.java
com.cs446.kluster.data.serialize.UserAdapter.java
com.cs446.kluster.map.MapUtils.java
com.cs446.kluster.models.AuthUser.java
com.cs446.kluster.models.Event.java
com.cs446.kluster.models.Photo.java
com.cs446.kluster.models.User.java
com.cs446.kluster.net.AuthInterceptor.java
com.cs446.kluster.net.AuthKlusterRestAdapter.java
com.cs446.kluster.net.EventsCallback.java
com.cs446.kluster.net.KlusterRestAdapter.java
com.cs446.kluster.net.KlusterService.java
com.cs446.kluster.net.NetworkUtils.java
com.cs446.kluster.net.PhotosCallback.java
com.cs446.kluster.net.UploadService.java
com.cs446.kluster.views.activities.LoginActivity.java
com.cs446.kluster.views.activities.MainActivity.java
com.cs446.kluster.views.activities.PhotoFactory.java
com.cs446.kluster.views.activities.PhotoGridActivity.java
com.cs446.kluster.views.fragments.DiscoverFragment.java
com.cs446.kluster.views.fragments.EventDialogFragment.java
com.cs446.kluster.views.fragments.EventGridAdapter.java
com.cs446.kluster.views.fragments.EventGridFragment.java
com.cs446.kluster.views.fragments.EventMapFragment.java
com.cs446.kluster.views.fragments.FilterDialogFragment.java
com.cs446.kluster.views.fragments.PhotoGridAdapter.java
com.cs446.kluster.views.fragments.PhotoGridFragment.java
com.cs446.kluster.views.fragments.PhotoViewerFragment.java
com.cs446.kluster.views.fragments.SearchFragment.java
com.cs446.kluster.views.fragments.SearchGridFragment.java
com.cs446.kluster.views.fragments.SearchMapFragment.java
com.cs446.kluster.views.fragments.SignupFragment.java
com.cs446.kluster.views.fragments.UserProfileFragment.java
com.cs446.kluster.views.map.PhotoInfoWindowAdapter.java