Last active
December 9, 2015 14:33
-
-
Save JoshuaRedmond/7be9c4eecc6365521adc to your computer and use it in GitHub Desktop.
An adapter for use with a RecyclerView that uses the FontManager class created by George Yunaev under Apache 2.0 (http://www.ulduzsoft.com/2012/01/enumerating-the-fonts-on-android-platform/) and based on his FontPreference (also Apache 2.0) (http://www.ulduzsoft.com/2012/01/fontpreference-dialog-for-android/). Used in Approxitock (https://play.g…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2015, Joshua Redmond | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.approxitock.android; | |
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.os.AsyncTask; | |
import android.support.v7.util.SortedList; | |
import android.support.v7.widget.RecyclerView; | |
import android.support.v7.widget.util.SortedListAdapterCallback; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import java.io.File; | |
public class FontPickerAdapter extends RecyclerView.Adapter | |
{ | |
private LayoutInflater inflater; | |
SortedList<FontHolder> sortedFonts = new SortedList<>(FontHolder.class, new FontSorterAdapter<>(this)); | |
public FontPickerAdapter(Context c) | |
{ | |
inflater = LayoutInflater.from(c); | |
new FindFontsTask(this).execute(); | |
} | |
private class FindFontsTask extends AsyncTask<Void, FontHolder, Void> | |
{ | |
FontPickerAdapter adapter; | |
@Override | |
protected Void doInBackground(Void... params) | |
{ | |
String[] fontdirs = {"/system/fonts", "/system/font", "/data/fonts"}; | |
TTFAnalyzer analyzer = new TTFAnalyzer(); | |
for (String fontdir : fontdirs) | |
{ | |
File dir = new File(fontdir); | |
if (!dir.exists()) | |
continue; | |
File[] files = dir.listFiles(); | |
if (files == null) | |
continue; | |
for (File file : files) | |
{ | |
String fontName = analyzer.getTtfFontName(file.getAbsolutePath()); | |
//Remove AndroidClock fonts as only the numbers and the letters A,P and M are styled which looks really weird in Approxitock | |
if (fontName != null && !fontName.startsWith("AndroidClock")) | |
{ | |
FontHolder tempHolder = new FontHolder(fontName, file.getAbsolutePath()); | |
publishProgress(tempHolder); | |
} | |
} | |
} | |
return null; | |
} | |
public FindFontsTask(FontPickerAdapter adapter) | |
{ | |
this.adapter = adapter; | |
} | |
@Override | |
protected void onProgressUpdate(FontHolder... values) | |
{ | |
super.onProgressUpdate(values); | |
for (FontHolder value : values) | |
{ | |
sortedFonts.add(value); | |
} | |
} | |
@Override | |
protected void onPostExecute(Void aVoid) | |
{ | |
super.onPostExecute(aVoid); | |
} | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) | |
{ | |
return new ItemViewHolder(inflater.inflate(R.layout.font_list_item,null)); | |
} | |
public static class ItemViewHolder extends RecyclerView.ViewHolder { | |
private TextView fontView; | |
public ItemViewHolder(View view) | |
{ | |
super(view); | |
fontView = (TextView) view.findViewById(R.id.fontText); | |
} | |
} | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) | |
{ | |
ItemViewHolder itemViewHolder = (ItemViewHolder) holder; | |
TextView view = itemViewHolder.fontView; | |
// Set text to be font name and written in font style | |
Typeface tface = Typeface.createFromFile(sortedFonts.get(position).getFontPath()); | |
view.setTypeface(tface); | |
view.setText(sortedFonts.get(position).getFontName()); | |
holder.itemView.setTag(sortedFonts.get(position)); | |
} | |
@Override | |
public int getItemCount() | |
{ | |
return sortedFonts.size(); | |
} | |
public class FontHolder | |
{ | |
private String fontName; | |
private String fontPath; | |
public FontHolder(String fontName, String fontPath) | |
{ | |
this.fontName = fontName; | |
this.fontPath = fontPath; | |
} | |
public String getFontName() | |
{ | |
return fontName; | |
} | |
public void setFontName(String fontName) | |
{ | |
this.fontName = fontName; | |
} | |
public String getFontPath() | |
{ | |
return fontPath; | |
} | |
public void setFontPath(String fontPath) | |
{ | |
this.fontPath = fontPath; | |
} | |
} | |
private class FontSorterAdapter<T extends FontHolder> extends SortedListAdapterCallback<T> | |
{ | |
/** | |
* Creates a {@link SortedList.Callback} that will forward data change events to the provided | |
* Adapter. | |
* | |
* @param adapter The Adapter instance which should receive events from the SortedList. | |
*/ | |
public FontSorterAdapter(RecyclerView.Adapter adapter) | |
{ | |
super(adapter); | |
} | |
@Override | |
public int compare(FontHolder o1, FontHolder o2) | |
{ | |
return o1.getFontName().compareToIgnoreCase(o2.getFontName()); | |
} | |
@Override | |
public boolean areContentsTheSame(FontHolder oldItem, FontHolder newItem) | |
{ | |
return oldItem.getFontName().equals(newItem.getFontName()) && | |
oldItem.getFontPath().equals(newItem.getFontPath()); | |
} | |
@Override | |
public boolean areItemsTheSame(FontHolder item1, FontHolder item2) | |
{ | |
return item1.getFontName().equals(item2.getFontName()) && | |
item1.getFontPath().equals(item2.getFontPath()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment