Skip to content

Commit

Permalink
Initial sample with local provider
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalonm committed Aug 29, 2017
1 parent ce137f7 commit 412963a
Show file tree
Hide file tree
Showing 44 changed files with 747 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
29 changes: 29 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.lalosoft.downloadablefontsdemo"
minSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'
}
25 changes: 25 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/gonzalo/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
21 changes: 21 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lalosoft.downloadablefontsdemo">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.lalosoft.downloadablefontsdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lalosoft.downloadablefontsdemo.font.FontHandler;
import com.lalosoft.downloadablefontsdemo.font.LocalFontProvider;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final TextView title = findViewById(R.id.title);
final TextView text = findViewById(R.id.text);
Button applyFont = findViewById(R.id.apply_local_font_button);

applyFont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FontHandler fontHandler = new FontHandler.Builder()
.provider(new LocalFontProvider(LocalFontProvider.DROID_SERIF))
.addTextView(title, FontHandler.FontStyle.BOLD)
.addTextView(text)
.build();

fontHandler.applyFont(new FontHandler.FontHandlerCallback() {
@Override
public void onFontApplySuccess() {
Toast.makeText(MainActivity.this,
R.string.applied_font,
Toast.LENGTH_SHORT)
.show();
}

@Override
public void onFontApplyError(Throwable e) {
Toast.makeText(MainActivity.this,
R.string.font_not_applied,
Toast.LENGTH_SHORT)
.show();
}
});
}
});

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.lalosoft.downloadablefontsdemo.font;

import android.graphics.Typeface;
import android.os.Handler;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class FontHandler {

private FontProvider provider;
private List<FontItem> fontItemList;

public enum FontStyle {
BOLD, ITALIC, REGULAR
}

public interface FontHandlerCallback {
void onFontApplySuccess();

void onFontApplyError(Throwable e);
}

private FontHandler(List<FontItem> fontItemList, FontProvider provider) {
this.fontItemList = fontItemList;
this.provider = provider;
}

public void applyFont(final FontHandlerCallback callback) {
new Handler().post(new Runnable() {
@Override
public void run() {
try {
for (FontItem item : fontItemList) {
Typeface typeface = provider.getTypeface(item.textView.getContext(),
getStyleName(item.fontStyle));
item.textView.setTypeface(typeface);
}
callback.onFontApplySuccess();
} catch (Exception e) {
callback.onFontApplyError(e);
}
}
});
}

private String getStyleName(FontStyle fontStyle) {
return fontStyle.name().toLowerCase();
}

public static class Builder {
private FontProvider provider;
private List<FontItem> fontItemList;

public Builder() {
this.fontItemList = new ArrayList<>();
}

public Builder provider(FontProvider provider) {
this.provider = provider;
return this;
}

public Builder addTextView(TextView textView, FontStyle fontStyle) {
fontItemList.add(new FontItem(textView, fontStyle));
return this;
}

public Builder addTextView(TextView textView) {
fontItemList.add(new FontItem(textView));
return this;
}

public FontHandler build() {
return new FontHandler(fontItemList, provider);
}

}

private static class FontItem {
public TextView textView;
public FontStyle fontStyle;

public FontItem(TextView textView, FontStyle fontStyle) {
this.textView = textView;
this.fontStyle = fontStyle;
}

public FontItem(TextView textView) {
this.textView = textView;
this.fontStyle = FontStyle.REGULAR;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.lalosoft.downloadablefontsdemo.font;

import android.content.Context;
import android.graphics.Typeface;

public interface FontProvider {
Typeface getTypeface(Context context, String fontStyleName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lalosoft.downloadablefontsdemo.font;

import android.content.Context;
import android.graphics.Typeface;

public class LocalFontProvider implements FontProvider {

// Droid Serif Font
private static final String DROID_SERIF_FOLDER = "fonts/droid_serif";
public static final String DROID_SERIF = DROID_SERIF_FOLDER + "/droid_serif-";

// Droid Sans Font
private static final String DROID_SANS_FOLDER = "fonts/droid_sans";
public static final String DROID_SANS = DROID_SANS_FOLDER + "/droid_sans-";

private String fontName;

public LocalFontProvider(String fontName) {
this.fontName = fontName;
}

@Override
public Typeface getTypeface(Context context, String fontStyleName) {
String name = fontName + fontStyleName + ".ttf";
return Typeface.createFromAsset(context.getAssets(), name);
}
}
113 changes: 113 additions & 0 deletions app/src/main/res/drawable/ic_launcher_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="108.0"
android:viewportWidth="108.0">
<path
android:fillColor="#26A69A"
android:pathData="M0,0h108v108h-108z"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,89L108,89"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,99L108,99"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,69L108,69"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,79L108,79"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,59L108,59"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeColor="#33FFFFFF"
android:strokeWidth="0.8" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeColor="#66FFFFFF"
android:strokeWidth="0.8" />
</vector>

Loading

0 comments on commit 412963a

Please sign in to comment.