package me.edanel.fulishe.Utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.FileNotFoundException;

import me.edanel.fulishe.R;


public class ImagePagerActivity extends FragmentActivity {
    private static final String STATE_POSITION = "STATE_POSITION";
    public static final String EXTRA_IMAGE_INDEX = "image_index";
    public static final String EXTRA_IMAGE_URLS = "image_urls";

    private HackyViewPager mPager;
    private int pagerPosition,mNowPosition;
    private TextView indicator, mSave;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_detail_pager);

        pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
        final String[] urls = getIntent().getStringArrayExtra(EXTRA_IMAGE_URLS);


        mPager = (HackyViewPager) findViewById(R.id.pager);
        mSave = (TextView) findViewById(R.id.photoview_save);
        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveImageToGallery(ImagePagerActivity.this,ImageLoader.getInstance().getDiskCache().get(urls[mNowPosition]).getAbsolutePath());
                Toast.makeText(ImagePagerActivity.this, "图片保存成功", Toast.LENGTH_SHORT).show();
            }
        });

        ImagePagerAdapter mAdapter = new ImagePagerAdapter(
                getSupportFragmentManager(), urls);
        mPager.setAdapter(mAdapter);
        indicator = (TextView) findViewById(R.id.indicator);

        CharSequence text = getString(R.string.viewpager_indicator, 1, mPager
                .getAdapter().getCount());
        indicator.setText(text);
        // 更新下标
        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                CharSequence text = getString(R.string.viewpager_indicator,
                        arg0 + 1, mPager.getAdapter().getCount());
                indicator.setText(text);
                mNowPosition = arg0 ;
            }

        });
        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }

        mPager.setCurrentItem(pagerPosition);
    }

    public static void saveImageToGallery(Context context, String imageUrl) {
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), imageUrl, "FLS" + System.currentTimeMillis(), null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + imageUrl)));
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, mPager.getCurrentItem());
    }

    private class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public String[] fileList;

        public ImagePagerAdapter(FragmentManager fm, String[] fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.length;
        }

        @Override
        public Fragment getItem(int position) {
            String url = fileList[position];
            return ImageDetailFragment.newInstance(url);
        }

    }
}
@@ -1634,24 +1634,142 @@
 (View v) %7B%0D%0A
+                if (ImageLoader.getInstance().getDiskCache().get(urls%5BmNowPosition%5D).getAbsolutePath() != null)%7B%0D%0A    
             
@@ -1917,16 +1917,20 @@
         
+    
 Toast.ma
@@ -1996,24 +1996,170 @@
 T).show();%0D%0A
+                %7Delse%7B%0D%0A                    Toast.makeText(ImagePagerActivity.this, %22%E5%9B%BE%E7%89%87%E7%BC%93%E5%AD%98%E8%AF%BB%E5%8F%96%E5%A4%B1%E8%B4%A5%22, Toast.LENGTH_SHORT).show();%0D%0A                %7D%0D%0A%0D%0A
             

fix bugs