/*
 * Copyright 2013 Niek Haarman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.haarman.listviewanimations.swinginexamples;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.haarman.listviewanimations.ArrayAdapter;
import com.haarman.listviewanimations.R;
import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;

public class GoogleCardsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_googlecards);

		ListView listView = (ListView) findViewById(R.id.activity_googlecards_listview);

		GoogleCardsAdapter googleCardsAdapter = new GoogleCardsAdapter(this);
		SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(googleCardsAdapter);
		swingBottomInAnimationAdapter.setListView(listView);

		listView.setAdapter(swingBottomInAnimationAdapter);

		googleCardsAdapter.addAll(getItems());
	}

	private ArrayList<Integer> getItems() {
		ArrayList<Integer> items = new ArrayList<Integer>();
		for (int i = 0; i < 100; i++) {
			items.add(i);
		}
		return items;
	}

	private static class GoogleCardsAdapter extends ArrayAdapter<Integer> {

		private Context mContext;
		private LruCache<Integer, Bitmap> mMemoryCache;

		public GoogleCardsAdapter(Context context) {
			mContext = context;

			final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

			// Use 1/8th of the available memory for this memory cache.
			final int cacheSize = maxMemory;
			System.out.println("max: " + maxMemory);
			mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
				@SuppressLint("NewApi")
				@Override
				protected int sizeOf(Integer key, Bitmap bitmap) {
					// The cache size will be measured in kilobytes rather than
					// number of items.
					int size;
					if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
						size = bitmap.getByteCount() / 1024;
					} else {
						size = bitmap.getRowBytes() * bitmap.getHeight() / 1024;
					}
					return size;
				}
			};
		}

		@SuppressLint("NewApi")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			View view = convertView;
			if (view == null) {
				view = LayoutInflater.from(mContext).inflate(R.layout.activity_googlecards_card, parent, false);

				viewHolder = new ViewHolder();
				viewHolder.textView = (TextView) view.findViewById(R.id.activity_googlecards_card_textview);
				view.setTag(viewHolder);

				viewHolder.imageView = (ImageView) view.findViewById(R.id.activity_googlecards_card_imageview);
			} else {
				viewHolder = (ViewHolder) view.getTag();
			}

			viewHolder.textView.setText("This is card " + (getItem(position) + 1));

			int imageResId;
			switch (getItem(position) % 5) {
				case 0:
					imageResId = R.drawable.img_nature1;
					break;
				case 1:
					imageResId = R.drawable.img_nature2;
					break;
				case 2:
					imageResId = R.drawable.img_nature3;
					break;
				case 3:
					imageResId = R.drawable.img_nature4;
					break;
				default:
					imageResId = R.drawable.img_nature5;
			}
			Bitmap bitmap = getBitmapFromMemCache(imageResId);
			if (bitmap == null) {
				bitmap = BitmapFactory.decodeResource(mContext.getResources(), imageResId);
				System.out.println("CREATING: " + (bitmap.getByteCount() / 1024));
				addBitmapToMemoryCache(imageResId, bitmap);
			} else {
				System.out.println("REUSING");
			}
			viewHolder.imageView.setImageResource(imageResId);

			return view;
		}

		private void addBitmapToMemoryCache(int key, Bitmap bitmap) {
			if (getBitmapFromMemCache(key) == null) {
				mMemoryCache.put(key, bitmap);
			}
		}

		private Bitmap getBitmapFromMemCache(int key) {
			return mMemoryCache.get(key);
		}

		private static class ViewHolder {
			TextView textView;
			ImageView imageView;
		}
	}
}

@@ -3064,34 +3064,8 @@
 %09%7D%0A%0A
-%09%09@SuppressLint(%22NewApi%22)%0A
 %09%09@O
@@ -3726,16 +3726,144 @@
 + 1));%0A%0A
+%09%09%09setImageView(viewHolder, position);%0A%0A%09%09%09return view;%0A%09%09%7D%0A%0A%09%09private void setImageView(ViewHolder viewHolder, int position) %7B%0A
 %09%09%09int i
@@ -4233,16 +4233,17 @@
 5;%0A%09%09%09%7D%0A
+%0A
 %09%09%09Bitma
@@ -4401,244 +4401,103 @@
 %09%09%09%09
-System.out.println(%22CREATING: %22 + (bitmap.getByteCount() / 1024));%0A%09%09%09%09addBitmapToMemoryCache(imageResId, bitmap);%0A%09%09%09%7D else %7B%0A%09%09%09%09System.out.println(%22REUSING%22);%0A%09%09%09%7D%0A%09%09%09viewHolder.imageView.setImageResource(imageResId);%0A%0A%09%09%09return view
+addBitmapToMemoryCache(imageResId, bitmap);%0A%09%09%09%7D%0A%09%09%09viewHolder.imageView.setImageBitmap(bitmap)
 ;%0A%09%09

Set bitmap to imageview