1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
public class ImagePagerActivity extends Activity {

	private static final String STATE_POSITION = "STATE_POSITION";
	DisplayImageOptions options;
	ImageLoaderConfiguration config;
	ViewPager pager;
	ImageLoader imageLoader;
	
	ImageButton imbClose;
	ImageButton imbMap;
	TextView txtDtTitle;
	TextView txtDtAddress;
	TextView txtDtDesc;
	private static String url;
	
	ArrayList<HashMap<String, Object>> arrCursor;
	ArrayList<HashMap<String, Object>> arrCursorPicture;
	CafeDataSource dataSource;
	String[] urls;
	int pagerPosition;
	ArrayList<HashMap<String, Object>> arrImageUrl;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_image_pager);
		
		imbClose = (ImageButton) findViewById(R.id.imb_close);
		imbMap = (ImageButton) findViewById(R.id.imb_map);
		txtDtTitle = (TextView) findViewById(R.id.txt_dt_title);
		txtDtAddress = (TextView) findViewById(R.id.txt_dt_address);
		txtDtDesc = (TextView) findViewById(R.id.txt_dt_desc);
		
		dataSource = new CafeDataSource(this);
		dataSource.open();
		arrCursor = dataSource.getArrCursor();
		arrCursorPicture = dataSource.getArrCursorPicture();
		pagerPosition  = arrCursorPicture.size();
		
		options = new DisplayImageOptions.Builder()
					.showImageForEmptyUri(R.drawable.ic_empty)
					.showImageOnFail(R.drawable.ic_error)
					.resetViewBeforeLoading()
					.cacheOnDisc()
					.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
					.bitmapConfig(Bitmap.Config.RGB_565)
					.displayer(new FadeInBitmapDisplayer(300))
					.build();
		
		config = new ImageLoaderConfiguration.Builder(ImagePagerActivity.this)
					.threadPriority(Thread.NORM_PRIORITY - 2)
					.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 *1024))
					.denyCacheImageMultipleSizesInMemory()
					.discCacheFileNameGenerator(new Md5FileNameGenerator())
					.tasksProcessingOrder(QueueProcessingType.LIFO)
					.discCacheSize(50 * 1024 * 1024)
					.build();
		ImageLoader.getInstance().init(config);
		
		Bundle bundle = getIntent().getExtras();
		int position = bundle.getInt(CafeDbOpenHelper.CAFE_ID);
		
		txtDtTitle.setText((CharSequence) arrCursor.get(position).get(CafeDbOpenHelper.CAFE_TITLE));
		txtDtAddress.setText((CharSequence) arrCursor.get(position).get(CafeDbOpenHelper.CAFE_ADDRESS));
		txtDtDesc.setText((CharSequence) arrCursor.get(position).get(CafeDbOpenHelper.CAFE_DESCRIPTION));

		imbClose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		if(savedInstanceState != null){
			position = savedInstanceState.getInt(STATE_POSITION);
		}	
		
		pager = (ViewPager) findViewById(R.id.pager);
		arrImageUrl = getArrImageUrl();
		pager.setAdapter(new ImagePagerAdapter(arrImageUrl));
		pager.setCurrentItem(0);
		
		
		
		
	} // End onCreate
	
	public ArrayList<HashMap<String, Object>> getArrImageUrl(){
		arrImageUrl = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map;
		for (int i = 0; i < pagerPosition; i++) {
			map = new HashMap<String, Object>();
			url = "http://xxx.xxx/xxx/place_pictures/"+arrCursorPicture.get(i).get(CafeDbOpenHelper.PICTURE_URL);
			map.put("image_url", url);
			arrImageUrl.add(map);
		}
		return arrImageUrl;
		
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState){
		outState.putInt(STATE_POSITION, pager.getCurrentItem());
	}
	
	private class ImagePagerAdapter extends PagerAdapter {

		private ArrayList<HashMap<String, Object>> myArr = new ArrayList<HashMap<String,Object>>();;
		private LayoutInflater inflater;
		
		
		ImagePagerAdapter(ArrayList<HashMap<String, Object>> myArr) {
			this.myArr = myArr;
			inflater = getLayoutInflater();
			imageLoader = ImageLoader.getInstance();
		}

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

		@Override
		public void finishUpdate(View container) {
		}

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

		@Override
		public Object instantiateItem(ViewGroup view, int position) {
			View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
			ImageView imageView = (ImageView) imageLayout.findViewById(R.id.imv_pager);
			final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
			
			imageLoader.displayImage((String) myArr.get(position).get("image_url"), imageView, options, new SimpleImageLoadingListener() {
				@Override
				public void onLoadingStarted(String imageUri, View view) {
					spinner.setVisibility(View.VISIBLE);
				}

				@Override
				public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
					String message = null;
					switch (failReason.getType()) {
						case IO_ERROR:
							message = "Input/Output error";
							break;
						case DECODING_ERROR:
							message = "Image can't be decoded";
							break;
						case NETWORK_DENIED:
							message = "Downloads are denied";
							break;
						case OUT_OF_MEMORY:
							message = "Out Of Memory error";
							break;
						case UNKNOWN:
							message = "Unknown error";
							break;
					}
					Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();

					spinner.setVisibility(View.GONE);
				}

				@Override
				public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
					spinner.setVisibility(View.GONE);
				}
			});

			((ViewPager) view).addView(imageLayout, 0);
			return imageLayout;
		}

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

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View container) {
		}
	}
	
}