cm.aptoidetv.pt.ViewPagerAdapterScreenshots.java Source code

Java tutorial

Introduction

Here is the source code for cm.aptoidetv.pt.ViewPagerAdapterScreenshots.java

Source

/*******************************************************************************
 * Copyright (c) 2012 rmateus.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 ******************************************************************************/
package cm.aptoidetv.pt;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class ViewPagerAdapterScreenshots extends PagerAdapter {

    private Context context;
    ArrayList<String> url;
    private boolean hd;

    public ViewPagerAdapterScreenshots(Context context, ArrayList<String> imagesurl, boolean hd) {

        this.context = context;

        this.url = imagesurl;
        this.hd = hd;

    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        final View v = LayoutInflater.from(context).inflate(R.layout.row_item_screenshots_big, null);
        final ProgressBar pb = (ProgressBar) v.findViewById(R.id.screenshots_loading_big);

        String icon;
        if (hd) {
            Log.d("Aptoide-Screenshots", "Icon is hd: " + url.get(position));

            if (url.get(position).contains("_screen")) {
                icon = url.get(position).split("\\|")[1];
                Log.d("Aptoide-Screenshots", "Icon is : " + icon);
            } else {
                icon = url.get(position);
            }

        } else {
            icon = screenshotToThumb(url.get(position));
        }

        Picasso.with(context).load(icon).error(R.drawable.icon_non_available)
                .into((ImageView) v.findViewById(R.id.screenshot_image_big));

        container.addView(v);
        if (!hd) {
            v.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent i = new Intent(context, ScreenshotsViewer.class);
                    i.putStringArrayListExtra("url", url);
                    i.putExtra("position", position);
                    context.startActivity(i);
                }
            });
        }
        return v;

    }

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

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0.equals(arg1);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);

    }

    protected String screenshotToThumb(String string) {

        String[] splitedString = string.split("/");
        StringBuilder db = new StringBuilder();
        for (int i = 0; i != splitedString.length - 1; i++) {
            db.append(splitedString[i]);
            db.append("/");
        }
        db.append("thumbs/mobile/");
        db.append(splitedString[splitedString.length - 1]);

        return db.toString();
    }
}