package com.chromium.fontinstaller;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<String> {
	Context context;
	int layoutResourceId;
	int textViewResourceId;
	ArrayList<String> data = new ArrayList<String>();

	public CustomAdapter(Context context, int layoutResourceId, int textViewResourceId, ArrayList<String> fontList) {

		super(context, layoutResourceId,textViewResourceId, fontList);
		this.layoutResourceId = layoutResourceId;
		this.textViewResourceId = textViewResourceId;
		this.context = context;
		this.data = fontList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;

		LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
		row = inflater.inflate(layoutResourceId, parent, false);

		ImageView alreadyDownloaded = (ImageView) row.findViewById(R.id.alreadyDownloaded);
		TextView textView = (TextView) row.findViewById(R.id.fontTextView);
		textView.setText(data.get(position));

		File dfDir = new File(Environment.getExternalStorageDirectory() + "/DownloadedFonts/"+ (removeSpaces(data.get(position))));

		if(dfDir.isDirectory()) {
			alreadyDownloaded.setVisibility(View.VISIBLE);
		}

		String fontNameWithoutSpaces = (removeSpaces(data.get(position)));

		File previewListDir = new File(Environment.getExternalStorageDirectory() + "/ListPreviews");

		if (previewListDir.isDirectory()) {
			
			File specificFontFile = new File(Environment.getExternalStorageDirectory() + "/ListPreviews/" + fontNameWithoutSpaces + "FontPack/Roboto-Regular.ttf");
			if (specificFontFile.exists()){
				// create new typeface from /sdcard/previewfont 
				Typeface preview = Typeface.createFromFile("/sdcard/ListPreviews/" + fontNameWithoutSpaces + "FontPack/Roboto-Regular.ttf");
				// set textView to the new typeface
				textView.setTypeface(preview);
			}
			else {
				CustomAlerts.showBasicAlert("Unable to load fonts", "Some fonts failed to be set." +
						" Please go to the settings of this app, choose the 'Disable True Font Display' option," +
						" then try again.", context);
			}	
		}
		return row;
	}

	public static String removeSpaces (String line)
	{//method to remove spaces
		for (int x = 0 ; x < line.length () ; x++)
		{
			if (line.charAt (x) == ' ')
			{
				String newLine = line.substring (0, x) + line.substring (x+1);
				return removeSpaces (newLine);
			}
		}
		return line;
	}
}
@@ -1,8 +1,612 @@
+/*%0A * Copyright (C) 2014 Priyesh Patel%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A %0A
 package 
@@ -3368,8 +3368,9 @@
 ne;%0A%09%7D%0A%7D
+%0A

ADd license header4