Skip to content

Commit

Permalink
add download
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Oct 4, 2017
1 parent 7faf009 commit bb4a10d
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 25 deletions.
4 changes: 2 additions & 2 deletions api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
compileSdkVersion rootProject.ext.targetSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion


defaultConfig {
Expand Down
57 changes: 53 additions & 4 deletions api/src/main/java/moe/shizuku/fontprovider/FontProviderClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@ public class FontProviderClient {
private static final String PACKAGE = "moe.shizuku.fontprovider";

public interface Callback {
void onServiceConnected(FontProviderClient client);
/**
* Called after ServiceConnection#onServiceConnected.
*
* @param client FontProviderClient
* @return true for unbindService automatically, false for keep ServiceConnection.
*/
boolean onServiceConnected(FontProviderClient client);
}

/**
* Create service connection.
*
* @param context Context
* @param callback Callback
*/
public static void create(Context context, Callback callback) {
context = context.getApplicationContext();

Expand All @@ -54,11 +66,33 @@ public static void create(Context context, Callback callback) {
private static Map<String, ByteBuffer> sBufferCache = new HashMap<>();

private IFontProvider mFontProvider;
private ServiceConnection mServiceConnection;

public FontProviderClient(IFontProvider fontProvider) {
public FontProviderClient(ServiceConnection serviceConnection, IFontProvider fontProvider) {
mServiceConnection = serviceConnection;
mFontProvider = fontProvider;
}

/**
* Unbind service, only need call this if false is returned in onServiceConnected
*
* @param context Context
*/
public void unbindService(Context context) {
try {
context.getApplicationContext().unbindService(mServiceConnection);
} catch (Exception e) {
Log.i(TAG, "can't unbindService", e);
}
}

/**=*
* @return IFontProvider
*/
public IFontProvider getFontProvider() {
return mFontProvider;
}

private static int resolveWeight(String name) {
if (TextUtils.isEmpty(name)) {
return 400;
Expand All @@ -81,18 +115,33 @@ private static int resolveWeight(String name) {
}
}

private static boolean resolveSerif(String name) {
private static boolean resolveIsSerif(String name) {
if (TextUtils.isEmpty(name)) {
return false;
}

return name.startsWith("serif");
}

/**
* Replace font family with specified font.
*
* @param name font family name
* @param fontName font name, such as "Noto Sans CJK"
* @return Typeface using to replace.
*/
public Typeface replace(String name, String fontName) {
return replace(name, fontName, resolveSerif(name) ? FontRequest.NOTO_SERIF : FontRequest.DEFAULT);
return replace(name, fontName, resolveIsSerif(name) ? FontRequest.NOTO_SERIF : FontRequest.DEFAULT);
}

/**
* Replace font family with specified font.
*
* @param name font family name
* @param fontName font name, such as "Noto Sans CJK"
* @param defaultFont first font
* @return Typeface using to replace.
*/
public Typeface replace(String name, String fontName, FontRequest defaultFont) {
return replace(name, FontRequests.create(resolveWeight(name), defaultFont, fontName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class FontProviderServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName componentName, IBinder binder) {
IFontProvider fontProvider = IFontProvider.Stub.asInterface(binder);

mCallback.onServiceConnected(new FontProviderClient(fontProvider));
mContext.unbindService(this);
FontProviderClient client = new FontProviderClient(this, fontProvider);
if (mCallback.onServiceConnected(client)) {
client.unbindService(mContext);
}
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import java.util.function.Consumer
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
compileSdkVersion rootProject.ext.targetSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "moe.shizuku.fontprovider"
minSdkVersion rootProject.ext.minSdkVersion
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/moe/shizuku/fontprovider/FontInfo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package moe.shizuku.fontprovider;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by rikka on 2017/9/29.
Expand All @@ -24,7 +25,7 @@ public class FontInfo implements Parcelable {
private final String url_prefix;
private final Style[] style;

private final Typeface[] typefaces;
private static final Map<String, Typeface> sCache = new HashMap<>();

public String getName() {
return name;
Expand Down Expand Up @@ -82,11 +83,11 @@ public Style[] getStyle(int... weight) {
}

public Typeface getTypeface(int style) {
return typefaces[style];
return sCache.get(name + style);
}

public void setTypeface(Typeface typeface, int style) {
typefaces[style] = typeface;
sCache.put(name + style, typeface);
}

public static class Style implements Parcelable {
Expand Down Expand Up @@ -199,7 +200,6 @@ private FontInfo(Parcel in) {
this.preview_text = in.createStringArray();
this.url_prefix = in.readString();
this.style = in.createTypedArray(Style.CREATOR);
this.typefaces = new Typeface[style.length];
}

public static final Parcelable.Creator<FontInfo> CREATOR = new Parcelable.Creator<FontInfo>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static List<Long> download(FontInfo font, List<String> files, Context con
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(font.getUrlPrefix() + f))
.setDestinationInExternalFilesDir(context, null, f)
.setVisibleInDownloadsUi(false)
.setAllowedOverMetered(false)
.setAllowedOverMetered(false || BuildConfig.DEBUG)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
ids.add(downloadManager.enqueue(request));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
int localeIndex = getIntent().getIntExtra(Intents.EXTRA_LOCALE_INDEX, 0);
Context context = null;
if (mFontInfo.getLanguage()[0] != null) {
String locale = mFontInfo.getLanguage()[localeIndex];
Locale locale = Locale.forLanguageTag(mFontInfo.getLanguage()[localeIndex]);

Configuration configuration = new Configuration(getResources().getConfiguration());
configuration.setLocale(Locale.forLanguageTag(locale));
configuration.setLocale(locale);
context = createConfigurationContext(configuration);

if (getActionBar() != null) {
getActionBar().setSubtitle(locale.getDisplayName());
}
}

RecyclerView recyclerView = findViewById(android.R.id.list_container);
Expand Down Expand Up @@ -83,7 +87,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
String[] language = mFontInfo.getLanguage();
if (language[0] != null
&& id >= MENU_ITEM_ID_START && id < MENU_ITEM_ID_START + language.length) {
//
getIntent().putExtra(Intents.EXTRA_LOCALE_INDEX, id - MENU_ITEM_ID_START);
recreate();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public class FontManageAdapter extends BaseRecyclerViewAdapter {
public FontManageAdapter(List<?> items) {
super(items);

setHasStableIds(true);

putRule(FontInfo.class, FontViewHolder.CREATOR);
}

@Override
public long getItemId(int position) {
return position;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void onBind() {
if (typeface == null) {
FontProviderClient.create(context, new FontProviderClient.Callback() {
@Override
public void onServiceConnected(FontProviderClient client) {
public boolean onServiceConnected(FontProviderClient client) {
Typeface typeface;

if (font.getLanguage()[localeIndex] == null) {
Expand All @@ -80,6 +80,7 @@ public void onServiceConnected(FontProviderClient client) {
text.setTypeface(typeface);

font.setTypeface(typeface, getAdapterPosition());
return true;
}
});
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package moe.shizuku.fontprovider.viewholder;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

Expand Down Expand Up @@ -43,17 +49,40 @@ public FontViewHolder(View itemView) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setEnabled(false);

List<String> files = FontManager.getFiles(getData(), v.getContext(), true);
FontManager.download(getData(), files, v.getContext());
final List<Long> ids = FontManager.download(getData(), files, v.getContext());

v.getContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (id != -1) {
ids.remove(id);

if (ids.isEmpty()) {
getAdapter().notifyItemChanged(getAdapterPosition(), new Object());
context.unregisterReceiver(this);
}
}
}
}, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
});

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.getContext().startActivity(FontPreviewActivity.intent(v.getContext(), getData()));
if (!FontManager.getFiles(getData(), v.getContext(), true).isEmpty()) {
Toast.makeText(v.getContext(), R.string.font_cannot_preview, Toast.LENGTH_SHORT).show();
} else {
v.getContext().startActivity(FontPreviewActivity.intent(v.getContext(), getData()));
}
}
});

setIsRecyclable(false);
}

@Override
Expand All @@ -77,4 +106,12 @@ public void onBind() {
summary.setText(context.getString(R.string.font_summary_no_language, font.getStyle().length));
}
}

@Override
public void onBind(@NonNull List<Object> payloads) {
if (FontManager.getFiles(getData(), itemView.getContext(), true).isEmpty()) {
button.setVisibility(View.GONE);
size.setVisibility(View.GONE);
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/item_font.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="?android:selectableItemBackground"
android:paddingStart="?android:listPreferredItemPaddingStart"
android:paddingEnd="?android:listPreferredItemPaddingEnd"
android:paddingTop="16dp"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/menu/preview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<item
android:id="@+id/action_language"
android:icon="@drawable/ic_language_24dp"
android:title="@string/action_language"
android:showAsAction="always">

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<string name="font_summary">%1$d styles · %2$d languages</string>
<string name="font_summary_no_language">%1$d styles</string>
<string name="font_size">%1$d styles · %2$d languages</string>
<string name="preview_text">This is a sample text. 骨曜将葛,地玄系、片海示。</string>
<string name="font_cannot_preview">You can\'t preview this font before downloading it.</string>
<string name="action_language">Change language</string>
</resources>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta6'
classpath 'com.android.tools.build:gradle:3.0.0-beta7'

classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
Expand All @@ -33,7 +33,7 @@ ext {
supportLibraryVersion = '26.1.0'
minSdkVersion = 24
targetSdkVersion = 26
buildToolsVersion = "26.0.1"
buildToolsVersion = "26.0.2"
versionCode = 1
versionName = '1.1.2'
}

0 comments on commit bb4a10d

Please sign in to comment.