<commit_before>package net.acuttone.reddimg.views;

import net.acuttone.reddimg.R;
import net.acuttone.reddimg.core.ReddimgApp;
import net.acuttone.reddimg.core.RedditClient;
import net.acuttone.reddimg.core.RedditLink;
import net.acuttone.reddimg.prefs.PrefsActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class LinkViewerActivity extends Activity {

	public static final String LINK_INDEX = "LINK_INDEX";
	private int currentLinkIndex;
	private ImageView viewBitmap;
	private ImageView viewLeftArrow;
	private ImageView viewRightArrow;
	private TextView textViewTitle;
	private AsyncTask<Void,Integer,Void> fadeTask;
	private ImageView viewUpvote;
	private ImageView viewDownvote;
	private TextView textviewLoading;
	private AsyncTask<Integer, RedditLink, Object[]> loadTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.linkviewer);
		textViewTitle = (TextView) findViewById(R.id.textViewTitle);
		textviewLoading = (TextView) findViewById(R.id.textviewLoading);
		textviewLoading.setText("");
		viewBitmap = (ImageView) findViewById(R.id.scrollViewLink).findViewById(R.id.imageViewLink);
		viewUpvote = (ImageView) findViewById(R.id.imageupvote);
		viewUpvote.setAlpha(0);
		viewDownvote = (ImageView) findViewById(R.id.imagedownvote);
		viewDownvote.setAlpha(0);
		viewLeftArrow = (ImageView) findViewById(R.id.imageleftarrow);
		viewRightArrow = (ImageView) findViewById(R.id.imagerightarrow);
		viewLeftArrow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(currentLinkIndex > 0) {
					currentLinkIndex--;
					loadImage();
				}
			}

		});
		viewRightArrow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				currentLinkIndex++;
				loadImage();
			}
		});
		ScrollView scrollView = (ScrollView) findViewById(R.id.scrollViewLink);
		scrollView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				fadeArrows();
				return false;
			}
		});
		
		if(savedInstanceState != null && savedInstanceState.containsKey(LINK_INDEX)) {
			currentLinkIndex = savedInstanceState.getInt(LINK_INDEX);
		} else {
			currentLinkIndex = getIntent().getExtras().getInt(LINK_INDEX);
		}
		loadImage();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(LINK_INDEX, currentLinkIndex);
	}
	
	private void loadImage() {
		if(loadTask != null) {
			loadTask.cancel(true);
		}
		loadTask = new AsyncTask<Integer, RedditLink, Object[]>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				if(fadeTask != null) {
					fadeTask.cancel(true);
				}
				textviewLoading.setText("Loading links...");
				viewLeftArrow.setAlpha(0);
				viewRightArrow.setAlpha(0);
				recycleBitmap();
			}

			@Override
			protected Object[] doInBackground(Integer... params) {
				RedditLink redditLink = ReddimgApp.instance().getLinksQueue().get(params[0]);
				publishProgress(redditLink);
				Bitmap bitmap = ReddimgApp.instance().getImageCache().getImage(redditLink.getUrl());
				Object [] result = new Object[2];
				result[0] = bitmap;
				result[1] = redditLink;
				return result;
			}
			
			@Override
			protected void onProgressUpdate(RedditLink... values) {
				super.onProgressUpdate(values);
				RedditLink link = values[0];
				updateTitle(link);
				textviewLoading.setText("Loading " + link.getUrl());
			}

			@Override
			protected void onPostExecute(Object[] result) {
				super.onPostExecute(result);
				textviewLoading.setText("");
				Bitmap bitmap = (Bitmap) ((Object []) result)[0];
				RedditLink redditLink = (RedditLink) ((Object []) result)[1];
				viewBitmap.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
				viewBitmap.setAdjustViewBounds(true);
				viewBitmap.setImageBitmap(bitmap);
				refreshVoteIndicators(redditLink); 
				fadeArrows();
			}
		};
		loadTask.execute(currentLinkIndex);
	}

	private void fadeArrows() {
		if (fadeTask == null || fadeTask.isCancelled() || fadeTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
			fadeTask = new AsyncTask<Void, Integer, Void>() {

				@Override
				protected Void doInBackground(Void... params) {
					for (int alpha = 256; alpha >= 0 && isCancelled() == false; alpha -= 4) {
						try {
							Thread.sleep(20);
						} catch (InterruptedException e) {
						}
						publishProgress(alpha);
					}
					return null;
				}

				@Override
				protected void onProgressUpdate(Integer... values) {
					if (isCancelled() == false) {
						viewLeftArrow.setAlpha(values[0]);
						viewRightArrow.setAlpha(values[0]);
					}
					super.onProgressUpdate(values);
				}

			};
			fadeTask.execute(null);
		}
	}

	private void refreshVoteIndicators(RedditLink redditLink) {
		if(RedditClient.UPVOTE.equals(redditLink.getVoteStatus())) {
			viewUpvote.setAlpha(255);
			viewDownvote.setAlpha(0);
		} else if(RedditClient.DOWNVOTE.equals(redditLink.getVoteStatus())) {
			viewUpvote.setAlpha(0);
			viewDownvote.setAlpha(255);
		} else {
			viewUpvote.setAlpha(0);
			viewDownvote.setAlpha(0);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		recycleBitmap();
		
		if(fadeTask != null) {
			fadeTask.cancel(true);
		}
	}
	
	private void recycleBitmap() {
		Drawable drawable = viewBitmap.getDrawable();<commit_msg>update updownvote indicators on img change<commit_after>package net.acuttone.reddimg.views;

import net.acuttone.reddimg.R;
import net.acuttone.reddimg.core.ReddimgApp;
import net.acuttone.reddimg.core.RedditClient;
import net.acuttone.reddimg.core.RedditLink;
import net.acuttone.reddimg.prefs.PrefsActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

public class LinkViewerActivity extends Activity {

	public static final String LINK_INDEX = "LINK_INDEX";
	private int currentLinkIndex;
	private ImageView viewBitmap;
	private ImageView viewLeftArrow;
	private ImageView viewRightArrow;
	private TextView textViewTitle;
	private AsyncTask<Void,Integer,Void> fadeTask;
	private ImageView viewUpvote;
	private ImageView viewDownvote;
	private TextView textviewLoading;
	private AsyncTask<Integer, RedditLink, Object[]> loadTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.linkviewer);
		textViewTitle = (TextView) findViewById(R.id.textViewTitle);
		textviewLoading = (TextView) findViewById(R.id.textviewLoading);
		textviewLoading.setText("");
		viewBitmap = (ImageView) findViewById(R.id.scrollViewLink).findViewById(R.id.imageViewLink);
		viewUpvote = (ImageView) findViewById(R.id.imageupvote);
		viewUpvote.setVisibility(View.GONE);
		viewDownvote = (ImageView) findViewById(R.id.imagedownvote);
		viewDownvote.setVisibility(View.GONE);
		viewLeftArrow = (ImageView) findViewById(R.id.imageleftarrow);
		viewRightArrow = (ImageView) findViewById(R.id.imagerightarrow);
		viewLeftArrow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(currentLinkIndex > 0) {
					currentLinkIndex--;
					loadImage();
				}
			}

		});
		viewRightArrow.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				currentLinkIndex++;
				loadImage();
			}
		});
		ScrollView scrollView = (ScrollView) findViewById(R.id.scrollViewLink);
		scrollView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				fadeArrows();
				return false;
			}
		});
		
		if(savedInstanceState != null && savedInstanceState.containsKey(LINK_INDEX)) {
			currentLinkIndex = savedInstanceState.getInt(LINK_INDEX);
		} else {
			currentLinkIndex = getIntent().getExtras().getInt(LINK_INDEX);
		}
		loadImage();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(LINK_INDEX, currentLinkIndex);
	}
	
	private void loadImage() {
		if(loadTask != null) {
			loadTask.cancel(true);
		}
		loadTask = new AsyncTask<Integer, RedditLink, Object[]>() {

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				if(fadeTask != null) {
					fadeTask.cancel(true);
				}
				textviewLoading.setText("Loading links...");
				viewLeftArrow.setAlpha(0);
				viewRightArrow.setAlpha(0);
				viewUpvote.setVisibility(View.GONE);
				viewDownvote.setVisibility(View.GONE);
				recycleBitmap();
			}

			@Override
			protected Object[] doInBackground(Integer... params) {
				RedditLink redditLink = ReddimgApp.instance().getLinksQueue().get(params[0]);
				publishProgress(redditLink);
				Bitmap bitmap = ReddimgApp.instance().getImageCache().getImage(redditLink.getUrl());
				Object [] result = new Object[2];
				result[0] = bitmap;
				result[1] = redditLink;
				return result;
			}
			
			@Override
			protected void onProgressUpdate(RedditLink... values) {
				super.onProgressUpdate(values);
				RedditLink link = values[0];
				updateTitle(link);
				textviewLoading.setText("Loading " + link.getUrl());
			}

			@Override
			protected void onPostExecute(Object[] result) {
				super.onPostExecute(result);
				textviewLoading.setText("");
				Bitmap bitmap = (Bitmap) ((Object []) result)[0];
				RedditLink redditLink = (RedditLink) ((Object []) result)[1];
				viewBitmap.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
				viewBitmap.setAdjustViewBounds(true);
				viewBitmap.setImageBitmap(bitmap);
				refreshVoteIndicators(redditLink); 
				fadeArrows();
			}
		};
		loadTask.execute(currentLinkIndex);
	}

	private void fadeArrows() {
		if (fadeTask == null || fadeTask.isCancelled() || fadeTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
			fadeTask = new AsyncTask<Void, Integer, Void>() {

				@Override
				protected Void doInBackground(Void... params) {
					for (int alpha = 256; alpha >= 0 && isCancelled() == false; alpha -= 4) {
						try {
							Thread.sleep(20);
						} catch (InterruptedException e) {
						}
						publishProgress(alpha);
					}
					return null;
				}

				@Override
				protected void onProgressUpdate(Integer... values) {
					if (isCancelled() == false) {
						viewLeftArrow.setAlpha(values[0]);
						viewRightArrow.setAlpha(values[0]);
					}
					super.onProgressUpdate(values);
				}

			};
			fadeTask.execute(null);
		}
	}

	private void refreshVoteIndicators(RedditLink redditLink) {
		if(RedditClient.UPVOTE.equals(redditLink.getVoteStatus())) {
			viewUpvote.setVisibility(View.VISIBLE);
			viewDownvote.setVisibility(View.GONE);
		} else if(RedditClient.DOWNVOTE.equals(redditLink.getVoteStatus())) {
			viewUpvote.setVisibility(View.GONE);
			viewDownvote.setVisibility(View.VISIBLE);
		} else {
			viewUpvote.setVisibility(View.GONE);
			viewDownvote.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		recycleBitmap();
		
		if(fadeTask != null) {
			fadeTask.cancel(true);
		}
	}
	
	private void recycleBitmap() {
		Drawable drawable = viewBitmap.getDrawable();
{'fraction_non_alphanumeric': 0.1186161449752883, 'fraction_numerical': 0.003922491566643132, 'mean_word_length': 4.571678321678322, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6078129', 'n_tokens_mistral': 4657, 'n_tokens_neox': 3931, 'n_words': 690}