2018-05-10 11:16:56 +02:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Build;
|
|
|
|
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;
|
|
|
|
|
2020-02-17 16:23:34 +01:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.preference.Preference;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2018-05-10 11:16:56 +02:00
|
|
|
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,
|
2019-06-06 21:51:56 +02:00
|
|
|
R.id.item_twemoji,
|
|
|
|
R.id.item_notoemoji};
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
private ArrayList<RadioButton> radioButtons = new ArrayList<>();
|
|
|
|
|
2019-06-06 21:51:56 +02:00
|
|
|
private boolean updated, currentNeedsUpdate;
|
|
|
|
|
2020-06-08 09:21:12 +02:00
|
|
|
public EmojiPreference(Context context) {
|
|
|
|
super(context);
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
}
|
2019-06-06 21:51:56 +02:00
|
|
|
|
2018-05-10 11:16:56 +02:00
|
|
|
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
|
|
|
|
2020-02-17 16:23:34 +01:00
|
|
|
for (int i = 0; i < viewIds.length; i++) {
|
2018-05-10 11:16:56 +02:00
|
|
|
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) ->
|
2020-02-17 16:23:34 +01:00
|
|
|
startDownload(font, container));
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
cancel.setOnClickListener((cancelButton) ->
|
2020-02-17 16:23:34 +01:00
|
|
|
cancelDownload(font, container));
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
radio.setOnClickListener((radioButton) ->
|
2020-02-17 16:23:34 +01:00
|
|
|
select(font, (RadioButton) radioButton));
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
container.setOnClickListener((containterView) ->
|
|
|
|
select(font,
|
|
|
|
containterView.findViewById(R.id.emojicompat_radio
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void startDownload(EmojiCompatFont font, View container) {
|
2020-02-17 16:23:34 +01:00
|
|
|
ImageButton download = container.findViewById(R.id.emojicompat_download);
|
|
|
|
TextView caption = container.findViewById(R.id.emojicompat_caption);
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
ProgressBar progressBar = container.findViewById(R.id.emojicompat_progress);
|
2020-02-17 16:23:34 +01:00
|
|
|
ImageButton cancel = container.findViewById(R.id.emojicompat_download_cancel);
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
// 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);
|
2020-02-17 16:23:34 +01:00
|
|
|
} else {
|
2018-05-10 11:16:56 +02:00
|
|
|
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);
|
2019-06-06 21:51:56 +02:00
|
|
|
// Set the flag to restart the app (because an update has been downloaded)
|
|
|
|
if (selected == original && currentNeedsUpdate) {
|
|
|
|
updated = true;
|
|
|
|
currentNeedsUpdate = false;
|
|
|
|
}
|
2018-05-10 11:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select a font both visually and logically
|
2020-02-17 16:23:34 +01:00
|
|
|
*
|
|
|
|
* @param font The font to be selected
|
2018-05-10 11:16:56 +02:00
|
|
|
* @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
|
2020-02-17 16:23:34 +01:00
|
|
|
for (RadioButton other : radioButtons) {
|
|
|
|
if (other != radio) {
|
2018-05-10 11:16:56 +02:00
|
|
|
other.setChecked(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
radio.setChecked(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a "consistent" state is reached, i.e. it's not downloading the font
|
2020-02-17 16:23:34 +01:00
|
|
|
*
|
|
|
|
* @param font The font to be displayed
|
2018-05-10 11:16:56 +02:00
|
|
|
* @param container The ConstraintLayout containing the item
|
|
|
|
*/
|
|
|
|
private void updateItem(EmojiCompatFont font, View container) {
|
|
|
|
// Assignments
|
2020-02-17 16:23:34 +01:00
|
|
|
ImageButton download = container.findViewById(R.id.emojicompat_download);
|
|
|
|
TextView caption = container.findViewById(R.id.emojicompat_caption);
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
ProgressBar progress = container.findViewById(R.id.emojicompat_progress);
|
2020-02-17 16:23:34 +01:00
|
|
|
ImageButton cancel = container.findViewById(R.id.emojicompat_download_cancel);
|
2018-05-10 11:16:56 +02:00
|
|
|
|
2020-02-17 16:23:34 +01:00
|
|
|
RadioButton radio = container.findViewById(R.id.emojicompat_radio);
|
2018-05-10 11:16:56 +02:00
|
|
|
|
|
|
|
// There's no download going on
|
|
|
|
progress.setVisibility(View.GONE);
|
|
|
|
cancel.setVisibility(View.GONE);
|
|
|
|
caption.setVisibility(View.VISIBLE);
|
|
|
|
|
2020-02-17 16:23:34 +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);
|
2020-02-17 16:23:34 +01:00
|
|
|
} else {
|
2018-05-10 11:16:56 +02:00
|
|
|
// Make it downloadable
|
|
|
|
download.setVisibility(View.VISIBLE);
|
|
|
|
radio.setVisibility(View.GONE);
|
|
|
|
container.setClickable(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select it if necessary
|
2020-02-17 16:23:34 +01:00
|
|
|
if (font == selected) {
|
2018-05-10 11:16:56 +02:00
|
|
|
radio.setChecked(true);
|
2019-06-06 21:51:56 +02:00
|
|
|
// Update available
|
|
|
|
if (!font.isDownloaded(getContext())) {
|
|
|
|
currentNeedsUpdate = true;
|
|
|
|
}
|
2020-02-17 16:23:34 +01:00
|
|
|
} else {
|
2018-05-10 11:16:56 +02:00
|
|
|
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() {
|
2020-02-17 16:23:34 +01:00
|
|
|
saveSelectedFont();
|
2019-06-06 21:51:56 +02:00
|
|
|
if (selected != original || updated) {
|
2020-02-17 16:23:34 +01:00
|
|
|
new AlertDialog.Builder(getContext())
|
|
|
|
.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
|
|
|
|
Intent launchIntent = new Intent(getContext(), SplashActivity.class);
|
|
|
|
PendingIntent mPendingIntent = PendingIntent.getActivity(
|
|
|
|
getContext(),
|
|
|
|
// This is the codepoint of the party face emoji :D
|
|
|
|
0x1f973,
|
|
|
|
launchIntent,
|
|
|
|
PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
|
AlarmManager mgr =
|
|
|
|
(AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|