2014-03-18 20:33:51 -04:00
|
|
|
package com.example.hw4;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bradlee Speice, Brandon Rodenmayer
|
|
|
|
* ITIS 4180
|
2014-03-23 20:56:39 -04:00
|
|
|
* Homework 4
|
|
|
|
* MainActivity.java
|
2014-03-18 20:33:51 -04:00
|
|
|
*/
|
2014-03-18 20:36:22 -04:00
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
2014-03-18 20:33:51 -04:00
|
|
|
|
2014-03-18 20:36:22 -04:00
|
|
|
import android.os.AsyncTask;
|
2014-03-18 20:33:51 -04:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.app.Activity;
|
2014-03-18 20:36:22 -04:00
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
2014-03-23 20:56:39 -04:00
|
|
|
import android.content.Intent;
|
2014-03-18 20:36:22 -04:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.view.LayoutInflater;
|
2014-03-18 20:33:51 -04:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.View;
|
2014-03-18 20:36:22 -04:00
|
|
|
import android.view.ViewGroup;
|
2014-03-23 20:56:39 -04:00
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
2014-03-18 20:36:22 -04:00
|
|
|
import android.widget.BaseAdapter;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.GridView;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
2014-03-18 20:33:51 -04:00
|
|
|
|
|
|
|
public class MainActivity extends Activity {
|
|
|
|
|
2014-03-18 20:36:22 -04:00
|
|
|
ProgressDialog progress;
|
|
|
|
LinearLayout root;
|
|
|
|
GridView photoGrid;
|
2014-03-23 20:56:39 -04:00
|
|
|
int[] imageUrlIds = {R.string.uncc_main_thumb, R.string.football_main_thumb,
|
|
|
|
R.string.ifest_main_thumb, R.string.commencement_main_thumb
|
|
|
|
};
|
|
|
|
int[] imageNames = {R.string.label_uncc, R.string.label_sports, R.string.label_ifest, R.string.label_commencement};
|
|
|
|
int[] thumbNames = {R.array.uncc_thumbs, R.array.football_thumbs, R.array.ifest_thumbs, R.array.commencement_thumbs};
|
2014-03-18 20:36:22 -04:00
|
|
|
ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
|
|
|
|
ArrayList<String> bitmapNames = new ArrayList<String>();
|
|
|
|
int downloadProgress;
|
2014-03-23 20:56:39 -04:00
|
|
|
Intent galleryIntent;
|
|
|
|
ImageAdapter imageAdapter;
|
2014-03-18 20:36:22 -04:00
|
|
|
|
2014-03-18 20:33:51 -04:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_main);
|
2014-03-18 20:36:22 -04:00
|
|
|
|
2014-03-23 20:56:39 -04:00
|
|
|
galleryIntent = new Intent(this, GalleryActivity.class);
|
2014-03-18 20:36:22 -04:00
|
|
|
root = (LinearLayout)findViewById(R.id.layout_async);
|
|
|
|
photoGrid = (GridView)findViewById(R.id.grid_async);
|
|
|
|
downloadProgress = 0;
|
|
|
|
|
|
|
|
//set the progress dialog
|
|
|
|
progress = new ProgressDialog(this);
|
|
|
|
progress.setMessage("Loading...");
|
|
|
|
progress.setCancelable(false);
|
|
|
|
progress.show();
|
|
|
|
|
|
|
|
for(int x : imageUrlIds)//download images
|
|
|
|
{
|
|
|
|
new DownloadPhoto().execute(getString(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
//create exit button
|
|
|
|
Button exit = (Button)findViewById(R.id.btn_exit_async);
|
|
|
|
exit.setOnClickListener(new View.OnClickListener() {
|
|
|
|
public void onClick(View v)
|
|
|
|
{
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
2014-03-18 20:33:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
// Inflate the menu; this adds items to the action bar if it is present.
|
2014-03-23 16:15:35 -04:00
|
|
|
getMenuInflater().inflate(R.menu.main, menu);
|
2014-03-18 20:33:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:36:22 -04:00
|
|
|
private class DownloadPhoto extends AsyncTask<String, Void, Bitmap>
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
protected Bitmap doInBackground(String... url)
|
|
|
|
{
|
|
|
|
Bitmap image = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
URL imageUrl = new URL(url[0]);
|
|
|
|
image = BitmapFactory.decodeStream(imageUrl.openStream());
|
|
|
|
}
|
|
|
|
catch(Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bitmap result)
|
|
|
|
{
|
|
|
|
//already a default picture included in grid_schema.xml, so no need to set a blank pic
|
|
|
|
bitmapList.add(result);
|
|
|
|
bitmapNames.add(getString(imageNames[downloadProgress]));
|
|
|
|
|
|
|
|
downloadProgress++;
|
|
|
|
if(downloadProgress>=imageUrlIds.length)
|
|
|
|
{
|
|
|
|
progress.dismiss();
|
|
|
|
//all images are loaded, so set them in the grid
|
2014-03-23 20:56:39 -04:00
|
|
|
imageAdapter = new ImageAdapter(photoGrid.getContext());
|
|
|
|
photoGrid.setAdapter(imageAdapter);
|
|
|
|
|
|
|
|
photoGrid.setOnItemClickListener(new OnItemClickListener()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view,
|
|
|
|
int position, long id)
|
|
|
|
{
|
|
|
|
//Send intent with R.array.label
|
2014-03-24 09:46:15 -04:00
|
|
|
galleryIntent.putExtra("thumbsId", (Integer)(imageAdapter.getItem(position)));
|
2014-03-23 20:56:39 -04:00
|
|
|
startActivity(galleryIntent);
|
|
|
|
}
|
|
|
|
});
|
2014-03-18 20:36:22 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-18 20:33:51 -04:00
|
|
|
}
|
2014-03-18 20:36:22 -04:00
|
|
|
|
|
|
|
public class ImageAdapter extends BaseAdapter
|
|
|
|
{
|
|
|
|
private Context context;
|
|
|
|
|
|
|
|
public ImageAdapter(Context context)
|
|
|
|
{
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount()
|
|
|
|
{
|
|
|
|
return imageUrlIds.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-03-23 20:56:39 -04:00
|
|
|
public Object getItem(int position)
|
2014-03-18 20:36:22 -04:00
|
|
|
{
|
2014-03-23 20:56:39 -04:00
|
|
|
return thumbNames[position];
|
2014-03-18 20:36:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position)//no purpose. only to fill the requirement of needing the method.
|
|
|
|
{
|
|
|
|
return position;
|
|
|
|
}
|
2014-03-18 20:33:51 -04:00
|
|
|
|
2014-03-18 20:36:22 -04:00
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent)
|
|
|
|
{
|
|
|
|
Holder holder = new Holder();
|
|
|
|
View vi = convertView;
|
|
|
|
|
|
|
|
if(vi == null)
|
|
|
|
{
|
|
|
|
//create layout of what we want one grid section to look like
|
|
|
|
vi = getLayoutInflater().inflate(R.layout.grid_schema, null);
|
|
|
|
|
|
|
|
holder.textView = (TextView)vi.findViewById(R.id.textView1);
|
|
|
|
holder.imageView = (ImageView)vi.findViewById(R.id.imageView1);
|
|
|
|
|
|
|
|
vi.setTag(holder);//associate the views in the holder to the grid
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
holder = (Holder)(vi.getTag());
|
|
|
|
}
|
|
|
|
//set the views in the grid to what was loaded
|
|
|
|
holder.textView.setText(getString(R.string.download_error));
|
|
|
|
if(bitmapList.get(position)!=null)
|
|
|
|
{
|
|
|
|
holder.imageView.setImageBitmap(bitmapList.get(position));
|
|
|
|
holder.textView.setText(bitmapNames.get(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
return vi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//views included in one grid section
|
|
|
|
static class Holder
|
|
|
|
{
|
|
|
|
TextView textView;
|
|
|
|
ImageView imageView;
|
|
|
|
}
|
2014-03-18 20:33:51 -04:00
|
|
|
}
|