2018-05-10 11:16:56 +02:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.app.AlarmManager;
|
2018-11-12 21:09:39 +01:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2018-05-10 11:16:56 +02:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Build;
|
2018-11-12 21:09:39 +01:00
|
|
|
|
|
|
|
import android.support.v7.preference.Preference;
|
|
|
|
import android.support.v7.preference.PreferenceManager;
|
2018-05-10 11:16:56 +02:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
2018-11-12 21:09:39 +01:00
|
|
|
import android.view.LayoutInflater;
|
2018-05-10 11:16:56 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.RadioButton;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.keylesspalace.tusky.util.EmojiCompatFont;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Preference lets the user select their preferred emoji font
|
|
|
|
*/
|
2018-11-12 21:09:39 +01:00
|
|
|
public class EmojiPreference extends Preference {
|
2018-05-10 11:16:56 +02:00
|
|
|
private static final String TAG = "EmojiPreference";
|
|
|
|
private EmojiCompatFont selected, original;
|
|
|
|
static final String FONT_PREFERENCE = "selected_emoji_font";
|
|
|
|
private static final EmojiCompatFont[] FONTS = EmojiCompatFont.FONTS;
|
|
|
|
// Please note that this array should be sorted in the same way as their fonts.
|
|
|
|
private static final int[] viewIds = {
|
|
|
|
R.id.item_nomoji,
|
|
|
|
R.id.item_blobmoji,
|
|
|
|
R.id.item_twemoji};
|
|
|
|
|
|
|
|
private ArrayList<RadioButton> radioButtons = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
public EmojiPreference(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
|
|
|
|
// Find out which font is currently active
|
|
|
|
this.selected = EmojiCompatFont.byId(PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(context)
|
|
|
|
.getInt(FONT_PREFERENCE, 0));
|
|
|
|
// We'll use this later to determine if anything has changed
|
|
|
|
this.original = this.selected;
|
|
|
|
|
|
|
|
setSummary(selected.getDisplay(context));
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
@Override
|
|
|
|
protected void onClick() {
|
2018-05-10 11:16:56 +02:00
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_emojicompat, null);
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
for(int i = 0; i < viewIds.length; i++) {
|
|
|
|
setupItem(view.findViewById(viewIds[i]), FONTS[i]);
|
|
|
|
}
|
2018-11-12 21:09:39 +01:00
|
|
|
|
|
|
|
new AlertDialog.Builder(getContext())
|
|
|
|
.setView(view)
|
|
|
|
.setPositiveButton(android.R.string.ok, (dialog, which) -> onDialogOk())
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.show();
|
2018-05-10 11:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setupItem(View container, EmojiCompatFont font) {
|
|
|
|
Context context = container.getContext();
|
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
TextView title = container.findViewById(R.id.emojicompat_name);
|
|
|
|
TextView caption = container.findViewById(R.id.emojicompat_caption);
|
|
|
|
ImageView thumb = container.findViewById(R.id.emojicompat_thumb);
|
|
|
|
ImageButton download = container.findViewById(R.id.emojicompat_download);
|
|
|
|
ImageButton cancel = container.findViewById(R.id.emojicompat_download_cancel);
|
|
|
|
RadioButton radio = container.findViewById(R.id.emojicompat_radio);
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
// Initialize all the views
|
|
|
|
title.setText(font.getDisplay(context));
|
|
|
|
caption.setText(font.getCaption(context));
|
|
|
|
thumb.setImageDrawable(font.getThumb(context));
|
|
|
|
|
|
|
|
// There needs to be a list of all the radio buttons in order to uncheck them when one is selected
|
|
|
|
radioButtons.add(radio);
|
|
|
|
|
|
|
|
updateItem(font, container);
|
|
|
|
|
|
|
|
// Set actions
|
|
|
|
download.setOnClickListener((downloadButton) ->
|
|
|
|
startDownload(font, container));
|
|
|
|
|
|
|
|
cancel.setOnClickListener((cancelButton) ->
|
|
|
|
cancelDownload(font, container));
|
|
|
|
|
|
|
|
radio.setOnClickListener((radioButton) ->
|
|
|
|
select(font, (RadioButton) radioButton));
|
|
|
|
|
|
|
|
container.setOnClickListener((containterView) ->
|
|
|
|
select(font,
|
|
|
|
containterView.findViewById(R.id.emojicompat_radio
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startDownload(EmojiCompatFont font, View container) {
|
|
|
|
ImageButton download = container.findViewById(R.id.emojicompat_download);
|
|
|
|
TextView caption = container.findViewById(R.id.emojicompat_caption);
|
|
|
|
|
|
|
|
ProgressBar progressBar = container.findViewById(R.id.emojicompat_progress);
|
|
|
|
ImageButton cancel = container.findViewById(R.id.emojicompat_download_cancel);
|
|
|
|
|
|
|
|
// Switch to downloading style
|
|
|
|
download.setVisibility(View.GONE);
|
2018-06-25 16:23:43 +02:00
|
|
|
caption.setVisibility(View.INVISIBLE);
|
2018-05-10 11:16:56 +02:00
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
|
|
|
cancel.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
font.downloadFont(getContext(), new EmojiCompatFont.Downloader.EmojiDownloadListener() {
|
2018-05-10 11:16:56 +02:00
|
|
|
@Override
|
|
|
|
public void onDownloaded(EmojiCompatFont font) {
|
|
|
|
finishDownload(font, container);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onProgress(float progress) {
|
|
|
|
// The progress is returned as a float between 0 and 1
|
|
|
|
progress *= progressBar.getMax();
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
progressBar.setProgress((int) progress, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
progressBar.setProgress((int) progress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailed() {
|
|
|
|
Toast.makeText(getContext(), R.string.download_failed, Toast.LENGTH_SHORT).show();
|
|
|
|
updateItem(font, container);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelDownload(EmojiCompatFont font, View container) {
|
|
|
|
font.cancelDownload();
|
|
|
|
updateItem(font, container);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void finishDownload(EmojiCompatFont font, View container) {
|
|
|
|
select(font, container.findViewById(R.id.emojicompat_radio));
|
|
|
|
updateItem(font, container);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a font both visually and logically
|
|
|
|
* @param font The font to be selected
|
|
|
|
* @param radio The radio button associated with it's visual item
|
|
|
|
*/
|
|
|
|
private void select(EmojiCompatFont font, RadioButton radio) {
|
|
|
|
selected = font;
|
|
|
|
// Uncheck all the other buttons
|
|
|
|
for(RadioButton other : radioButtons) {
|
|
|
|
if(other != radio) {
|
|
|
|
other.setChecked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
radio.setChecked(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a "consistent" state is reached, i.e. it's not downloading the font
|
|
|
|
* @param font The font to be displayed
|
|
|
|
* @param container The ConstraintLayout containing the item
|
|
|
|
*/
|
|
|
|
private void updateItem(EmojiCompatFont font, View container) {
|
|
|
|
// Assignments
|
|
|
|
ImageButton download = container.findViewById(R.id.emojicompat_download);
|
|
|
|
TextView caption = container.findViewById(R.id.emojicompat_caption);
|
|
|
|
|
|
|
|
ProgressBar progress = container.findViewById(R.id.emojicompat_progress);
|
|
|
|
ImageButton cancel = container.findViewById(R.id.emojicompat_download_cancel);
|
|
|
|
|
|
|
|
RadioButton radio = container.findViewById(R.id.emojicompat_radio);
|
|
|
|
|
|
|
|
// There's no download going on
|
|
|
|
progress.setVisibility(View.GONE);
|
|
|
|
cancel.setVisibility(View.GONE);
|
|
|
|
caption.setVisibility(View.VISIBLE);
|
|
|
|
|
2018-11-12 21:09:39 +01:00
|
|
|
if(font.isDownloaded(getContext())) {
|
2018-05-10 11:16:56 +02:00
|
|
|
// Make it selectable
|
|
|
|
download.setVisibility(View.GONE);
|
|
|
|
radio.setVisibility(View.VISIBLE);
|
|
|
|
container.setClickable(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Make it downloadable
|
|
|
|
download.setVisibility(View.VISIBLE);
|
|
|
|
radio.setVisibility(View.GONE);
|
|
|
|
container.setClickable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select it if necessary
|
|
|
|
if(font == selected) {
|
|
|
|
radio.setChecked(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
radio.setChecked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In order to be able to use this font later on, it needs to be saved first.
|
|
|
|
*/
|
|
|
|
private void saveSelectedFont() {
|
|
|
|
int index = selected.getId();
|
|
|
|
Log.i(TAG, "saveSelectedFont: Font ID: " + index);
|
|
|
|
// It's saved using the key FONT_PREFERENCE
|
|
|
|
PreferenceManager
|
2018-11-12 21:09:39 +01:00
|
|
|
.getDefaultSharedPreferences(getContext())
|
2018-05-10 11:16:56 +02:00
|
|
|
.edit()
|
|
|
|
.putInt(FONT_PREFERENCE, index)
|
|
|
|
.apply();
|
|
|
|
setSummary(selected.getDisplay(getContext()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* That's it. The user doesn't want to switch between these amazing radio buttons anymore!
|
|
|
|
* That means, the selected font can be saved (if the user hit OK)
|
|
|
|
*/
|
2018-11-12 21:09:39 +01:00
|
|
|
private void onDialogOk() {
|
2018-05-10 11:16:56 +02:00
|
|
|
saveSelectedFont();
|
|
|
|
if(selected != original) {
|
2018-11-12 21:09:39 +01:00
|
|
|
new AlertDialog.Builder(getContext())
|
2018-05-10 11:16:56 +02:00
|
|
|
.setTitle(R.string.restart_required)
|
|
|
|
.setMessage(R.string.restart_emoji)
|
|
|
|
.setNegativeButton(R.string.later, null)
|
|
|
|
.setPositiveButton(R.string.restart, ((dialog, which) -> {
|
|
|
|
// Restart the app
|
|
|
|
// From https://stackoverflow.com/a/17166729/5070653
|
2018-11-12 21:09:39 +01:00
|
|
|
Intent launchIntent = new Intent(getContext(), SplashActivity.class);
|
2018-05-10 11:16:56 +02:00
|
|
|
PendingIntent mPendingIntent = PendingIntent.getActivity(
|
2018-11-12 21:09:39 +01:00
|
|
|
getContext(),
|
2018-05-10 11:16:56 +02:00
|
|
|
// This is the codepoint of the party face emoji :D
|
|
|
|
0x1f973,
|
|
|
|
launchIntent,
|
|
|
|
PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
|
AlarmManager mgr =
|
2018-11-12 21:09:39 +01:00
|
|
|
(AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
|
2018-05-10 11:16:56 +02:00
|
|
|
if (mgr != null) {
|
|
|
|
mgr.set(
|
|
|
|
AlarmManager.RTC,
|
|
|
|
System.currentTimeMillis() + 100,
|
|
|
|
mPendingIntent);
|
|
|
|
}
|
|
|
|
System.exit(0);
|
|
|
|
})).show();
|
|
|
|
}
|
2018-11-12 21:09:39 +01:00
|
|
|
|
2018-05-10 11:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|