Clean up code
This commit is contained in:
@@ -21,17 +21,13 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
public class NotificationsHelper {
|
||||
private static final int NOTIFICATION_ID_BASE = 3921337;
|
||||
private static final String NOTIFICATION_CHANNEL_ID = "tgss_chan_v5";
|
||||
|
||||
private HashMap<TelegramStickersPack, Integer> downloadingPacksNotificationsIds;
|
||||
|
||||
private Context c;
|
||||
private NotificationManagerCompat manager;
|
||||
private int currentNotificationN = 0;
|
||||
|
||||
private static final int MAX_UPDATE_NOTIFICATIONS_PER_SECOND = 3;
|
||||
private static final int MIN_TIME_BETWEEN_UPDATE_NOTIFICATIONS = 1000 / MAX_UPDATE_NOTIFICATIONS_PER_SECOND;
|
||||
|
||||
private AtomicLong lastNotificationTime = new AtomicLong(0);
|
||||
private final HashMap<TelegramStickersPack, Integer> downloadingPacksNotificationsIds;
|
||||
private final Context c;
|
||||
private final NotificationManagerCompat manager;
|
||||
private int currentNotificationN = 0;
|
||||
private final AtomicLong lastNotificationTime = new AtomicLong(0);
|
||||
|
||||
public NotificationsHelper(Context c) {
|
||||
this.c = c;
|
||||
|
||||
@@ -11,7 +11,7 @@ import java.io.File;
|
||||
class PackDeletionTask extends AsyncTask<Void, Void, Boolean> {
|
||||
private static final String TAG = "TGSSAnnihilator";
|
||||
|
||||
private TelegramStickersPack pack;
|
||||
private final TelegramStickersPack pack;
|
||||
|
||||
PackDeletionTask(TelegramStickersPack pack) {
|
||||
this.pack = pack;
|
||||
|
||||
@@ -101,7 +101,7 @@ public class TelegramStickersPack extends CustomStickersPack {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof TelegramStickersPack && ((TelegramStickersPack) obj).id.toLowerCase().equals(id.toLowerCase());
|
||||
return obj instanceof TelegramStickersPack && ((TelegramStickersPack) obj).id.equalsIgnoreCase(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.aefyr.tsg.g2;
|
||||
|
||||
import static ru.vtosters.lite.utils.Helper.*;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
@@ -27,29 +29,25 @@ public class TelegramStickersService {
|
||||
private static final String TAG = "TGStickersService";
|
||||
private static TelegramStickersService instance;
|
||||
|
||||
private Context c;
|
||||
private final Context c;
|
||||
|
||||
private ArrayList<TelegramStickersPack> packs;
|
||||
private ArrayList<TelegramStickersPack> activePacks;
|
||||
private ArrayList<TelegramStickersPack> inactivePacks;
|
||||
private ArrayList<StickersEventsListener> listeners;
|
||||
private HashSet<String> currentlyDownloading;
|
||||
private final ArrayList<TelegramStickersPack> packs;
|
||||
private final ArrayList<TelegramStickersPack> activePacks;
|
||||
private final ArrayList<TelegramStickersPack> inactivePacks;
|
||||
private final ArrayList<StickersEventsListener> listeners;
|
||||
private final HashSet<String> currentlyDownloading;
|
||||
|
||||
private Handler uiThreadHandler;
|
||||
private final Handler uiThreadHandler;
|
||||
|
||||
private TelegramStickersGrabber grabber;
|
||||
private final TelegramStickersGrabber grabber;
|
||||
|
||||
private TelegramStickersDbHelper dbHelper;
|
||||
private final TelegramStickersDbHelper dbHelper;
|
||||
private boolean ready = false;
|
||||
private ArrayList<Runnable> queuedTasks;
|
||||
private final ArrayList<Runnable> queuedTasks;
|
||||
|
||||
private ThreadPoolExecutor executor;
|
||||
private final ThreadPoolExecutor executor;
|
||||
|
||||
private NotificationsHelper notificationsHelper;
|
||||
|
||||
public static TelegramStickersService getInstance(Context c) {
|
||||
return instance == null ? new TelegramStickersService(c) : instance;
|
||||
}
|
||||
private final NotificationsHelper notificationsHelper;
|
||||
|
||||
private TelegramStickersService(Context context) {
|
||||
instance = this;
|
||||
@@ -72,6 +70,10 @@ public class TelegramStickersService {
|
||||
updatePacks(true);
|
||||
}
|
||||
|
||||
public static TelegramStickersService getInstance(Context c) {
|
||||
return instance == null ? new TelegramStickersService(c) : instance;
|
||||
}
|
||||
|
||||
private void updatePacks(final boolean notify) {
|
||||
packs.clear();
|
||||
dbHelper.getAllPacks(new TelegramStickersDbHelper.PacksLoadingListener() {
|
||||
@@ -105,20 +107,6 @@ public class TelegramStickersService {
|
||||
});
|
||||
}
|
||||
|
||||
interface StickersEventsListener {
|
||||
void onPackAdded(TelegramStickersPack pack, int atIndex);
|
||||
|
||||
void onPackRemoved(TelegramStickersPack pack, int atIndex);
|
||||
|
||||
void onPackChanged(TelegramStickersPack pack, int atIndex);
|
||||
|
||||
void onPackDownloadError(TelegramStickersPack pack, Exception error);
|
||||
|
||||
void onActivePacksListChanged();
|
||||
|
||||
void onInactivePacksListChanged();
|
||||
}
|
||||
|
||||
public void addStickersEventsListener(StickersEventsListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
@@ -128,7 +116,7 @@ public class TelegramStickersService {
|
||||
}
|
||||
|
||||
private void notifyPackAdded(TelegramStickersPack pack, int atIndex) {
|
||||
Helper.GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
if (listeners.isEmpty())
|
||||
return;
|
||||
|
||||
@@ -138,7 +126,7 @@ public class TelegramStickersService {
|
||||
}
|
||||
|
||||
private void notifyPackChanged(TelegramStickersPack pack, int atIndex) {
|
||||
Helper.GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
if (listeners.isEmpty())
|
||||
return;
|
||||
|
||||
@@ -148,7 +136,7 @@ public class TelegramStickersService {
|
||||
}
|
||||
|
||||
private void notifyPackRemoved(TelegramStickersPack pack, int atIndex) {
|
||||
Helper.GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
if (listeners.isEmpty())
|
||||
return;
|
||||
|
||||
@@ -158,7 +146,7 @@ public class TelegramStickersService {
|
||||
}
|
||||
|
||||
private void notifyPackDownloadError(TelegramStickersPack pack, Exception error) {
|
||||
Helper.GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
if (listeners.isEmpty())
|
||||
return;
|
||||
|
||||
@@ -168,7 +156,7 @@ public class TelegramStickersService {
|
||||
}
|
||||
|
||||
private void notifyActivePacksListChanged() {
|
||||
Helper.GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
if (listeners.isEmpty())
|
||||
return;
|
||||
|
||||
@@ -178,7 +166,7 @@ public class TelegramStickersService {
|
||||
}
|
||||
|
||||
private void notifyInactivePacksListChanged() {
|
||||
Helper.GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
GetContext().sendBroadcast(new Intent(StickersFragment.ACTION_RELOAD));
|
||||
if (listeners.isEmpty())
|
||||
return;
|
||||
|
||||
@@ -398,5 +386,19 @@ public class TelegramStickersService {
|
||||
getPacksListReference().set(li1, p2);
|
||||
}
|
||||
|
||||
interface StickersEventsListener {
|
||||
void onPackAdded(TelegramStickersPack pack, int atIndex);
|
||||
|
||||
void onPackRemoved(TelegramStickersPack pack, int atIndex);
|
||||
|
||||
void onPackChanged(TelegramStickersPack pack, int atIndex);
|
||||
|
||||
void onPackDownloadError(TelegramStickersPack pack, Exception error);
|
||||
|
||||
void onActivePacksListChanged();
|
||||
|
||||
void onInactivePacksListChanged();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,10 @@ import android.provider.BaseColumns;
|
||||
*/
|
||||
public final class TelegramStickersContract {
|
||||
|
||||
public static final String SQL_CREATE_ENTRIES = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s INTEGER AUTO_INCREMENT, %s TEXT, %s TEXT, %s TEXT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT)",
|
||||
StickersTableEntry.TABLE_NAME, StickersTableEntry._ID, StickersTableEntry.COLUMN_NAME_INDEX, StickersTableEntry.COLUMN_NAME_ID, StickersTableEntry.COLUMN_NAME_NAME,
|
||||
StickersTableEntry.COLUMN_NAME_FOLDER, StickersTableEntry.COLUMN_NAME_STICKERS_COUNT, StickersTableEntry.COLUMN_NAME_ENABLED, StickersTableEntry.COLUMN_NAME_VERSION, StickersTableEntry.COLUMN_NAME_EMOJIS);
|
||||
|
||||
private TelegramStickersContract() {
|
||||
}
|
||||
|
||||
@@ -23,9 +27,5 @@ public final class TelegramStickersContract {
|
||||
public static final String COLUMN_NAME_EMOJIS = "emojis";
|
||||
}
|
||||
|
||||
public static final String SQL_CREATE_ENTRIES = String.format("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s INTEGER AUTO_INCREMENT, %s TEXT, %s TEXT, %s TEXT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT)",
|
||||
StickersTableEntry.TABLE_NAME, StickersTableEntry._ID, StickersTableEntry.COLUMN_NAME_INDEX, StickersTableEntry.COLUMN_NAME_ID, StickersTableEntry.COLUMN_NAME_NAME,
|
||||
StickersTableEntry.COLUMN_NAME_FOLDER, StickersTableEntry.COLUMN_NAME_STICKERS_COUNT, StickersTableEntry.COLUMN_NAME_ENABLED, StickersTableEntry.COLUMN_NAME_VERSION, StickersTableEntry.COLUMN_NAME_EMOJIS);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -27,12 +27,6 @@ public class TelegramStickersDbHelper extends SQLiteOpenHelper {
|
||||
private SQLiteDatabase writableDb;
|
||||
private SQLiteDatabase readableDb;
|
||||
|
||||
public interface PacksLoadingListener {
|
||||
void onPackLoaded(TelegramStickersPack pack);
|
||||
|
||||
void onAllPacksLoaded(ArrayList<TelegramStickersPack> packs);
|
||||
}
|
||||
|
||||
public TelegramStickersDbHelper(Context context) {
|
||||
super(context, DB_NAME, null, DB_VERSION);
|
||||
}
|
||||
@@ -105,6 +99,12 @@ public class TelegramStickersDbHelper extends SQLiteOpenHelper {
|
||||
new PacksLoadingTask(listener).execute();
|
||||
}
|
||||
|
||||
public interface PacksLoadingListener {
|
||||
void onPackLoaded(TelegramStickersPack pack);
|
||||
|
||||
void onAllPacksLoaded(ArrayList<TelegramStickersPack> packs);
|
||||
}
|
||||
|
||||
private class PacksLoadingTask extends AsyncTask<Void, Void, ArrayList<TelegramStickersPack>> {
|
||||
|
||||
PacksLoadingListener listener;
|
||||
|
||||
@@ -38,12 +38,6 @@ import ru.f0x1d.net.NetResponse;
|
||||
*/
|
||||
public class TelegramStickersGrabber {
|
||||
private static final String TAG = "TSG";
|
||||
|
||||
private static String BOT_API_BASE_URL;
|
||||
private static String GET_STICKER_SET_URL;
|
||||
private static String GET_FILE_URL;
|
||||
private static String BOT_API_BASE_FILE_URL;
|
||||
|
||||
public static String REAL_TG_IP = null;
|
||||
public static String PROXY_IP = null;
|
||||
public static int PROXY_PORT = -1;
|
||||
@@ -52,11 +46,32 @@ public class TelegramStickersGrabber {
|
||||
public static boolean PROXY_SOCKS = true;
|
||||
public static boolean HAS_PASS = false;
|
||||
public static boolean USE_PROXY = false;
|
||||
private static String BOT_API_BASE_URL;
|
||||
private static String GET_STICKER_SET_URL;
|
||||
private static String GET_FILE_URL;
|
||||
private static String BOT_API_BASE_FILE_URL;
|
||||
|
||||
static {
|
||||
updateURLs();
|
||||
}
|
||||
|
||||
private final MessageDigest sha256;
|
||||
private final Handler uiThreadHandler;
|
||||
private String botApiKey = "";
|
||||
private NetClient httpClient;
|
||||
public TelegramStickersGrabber(String botApiKey) {
|
||||
this.botApiKey = botApiKey;
|
||||
httpClient = new NetClient.Builder().connectTimeout(5, TimeUnit.SECONDS).build();
|
||||
uiThreadHandler = new Handler(Looper.getMainLooper());
|
||||
try {
|
||||
sha256 = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
Log.wtf(TAG, "No support for SHA-256");
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateURLs() {
|
||||
BOT_API_BASE_FILE_URL = "https://api.telegram.org/file/bot%s/%s";
|
||||
|
||||
@@ -66,11 +81,6 @@ public class TelegramStickersGrabber {
|
||||
GET_FILE_URL = BOT_API_BASE_URL + "getFile?file_id=%s";
|
||||
}
|
||||
|
||||
private String botApiKey = "";
|
||||
private NetClient httpClient;
|
||||
private final MessageDigest sha256;
|
||||
private final Handler uiThreadHandler;
|
||||
|
||||
public static boolean isTelegramBlocked() {
|
||||
NetClient client = new NetClient.Builder().connectTimeout(5, TimeUnit.SECONDS).build();
|
||||
try {
|
||||
@@ -101,43 +111,6 @@ public class TelegramStickersGrabber {
|
||||
return false;
|
||||
}
|
||||
|
||||
public interface PackDownloadListener {
|
||||
void onPackDownloaded(TelegramStickersPackInfo pack, boolean newVersionFound);
|
||||
|
||||
void onPackDownloadError(Exception e);
|
||||
|
||||
void onGotPackInfo(TelegramStickersPackInfo packInfo);
|
||||
|
||||
void onStickerDownloaded(String pack, File sticker, String boundEmoji, int stickerIndex, int downloadedStickersCount, int totalStickerCount);
|
||||
}
|
||||
|
||||
public interface KeyCheckListener {
|
||||
void onKeyChecked(boolean ok);
|
||||
|
||||
void onNetError();
|
||||
}
|
||||
|
||||
public class TSGException extends Exception {
|
||||
private static final long serialVersionUID = 7866915509988422944L;
|
||||
|
||||
private TSGException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
public TelegramStickersGrabber(String botApiKey) {
|
||||
this.botApiKey = botApiKey;
|
||||
httpClient = new NetClient.Builder().connectTimeout(5, TimeUnit.SECONDS).build();
|
||||
uiThreadHandler = new Handler(Looper.getMainLooper());
|
||||
try {
|
||||
sha256 = MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
Log.wtf(TAG, "No support for SHA-256");
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void resetProxy() {
|
||||
httpClient = new NetClient.Builder().connectTimeout(5, TimeUnit.SECONDS).build();
|
||||
}
|
||||
@@ -201,40 +174,6 @@ public class TelegramStickersGrabber {
|
||||
});
|
||||
}
|
||||
|
||||
private class StickerSet {
|
||||
String id;
|
||||
String name;
|
||||
String version;
|
||||
ArrayList<Sticker> stickers;
|
||||
|
||||
StickerSet(String id, String name, String version, ArrayList<Sticker> stickers) {
|
||||
this.name = name;
|
||||
this.stickers = stickers;
|
||||
this.id = id;
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
|
||||
private class Sticker {
|
||||
String emoji;
|
||||
String fileId;
|
||||
|
||||
Sticker(String fileId, String emoji) {
|
||||
this.fileId = fileId;
|
||||
this.emoji = emoji;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof Sticker && ((Sticker) obj).fileId.equals(fileId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (17 * emoji.hashCode()) << 32 + fileId.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
private void getPackInfo(final String packName, final File packFolder, final String installedVersion, final PackDownloadListener listener) {
|
||||
NetRequest packInfoRequest = new NetRequest.Builder().get().url(String.format(GET_STICKER_SET_URL, botApiKey, packName)).build();
|
||||
httpClient.newCall(packInfoRequest).enqueue(new NetCallback() {
|
||||
@@ -478,4 +417,62 @@ public class TelegramStickersGrabber {
|
||||
private void runOnUiThread(Runnable r) {
|
||||
uiThreadHandler.post(r);
|
||||
}
|
||||
|
||||
public interface PackDownloadListener {
|
||||
void onPackDownloaded(TelegramStickersPackInfo pack, boolean newVersionFound);
|
||||
|
||||
void onPackDownloadError(Exception e);
|
||||
|
||||
void onGotPackInfo(TelegramStickersPackInfo packInfo);
|
||||
|
||||
void onStickerDownloaded(String pack, File sticker, String boundEmoji, int stickerIndex, int downloadedStickersCount, int totalStickerCount);
|
||||
}
|
||||
|
||||
public interface KeyCheckListener {
|
||||
void onKeyChecked(boolean ok);
|
||||
|
||||
void onNetError();
|
||||
}
|
||||
|
||||
public class TSGException extends Exception {
|
||||
private static final long serialVersionUID = 7866915509988422944L;
|
||||
|
||||
private TSGException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
private class StickerSet {
|
||||
String id;
|
||||
String name;
|
||||
String version;
|
||||
ArrayList<Sticker> stickers;
|
||||
|
||||
StickerSet(String id, String name, String version, ArrayList<Sticker> stickers) {
|
||||
this.name = name;
|
||||
this.stickers = stickers;
|
||||
this.id = id;
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
|
||||
private class Sticker {
|
||||
String emoji;
|
||||
String fileId;
|
||||
|
||||
Sticker(String fileId, String emoji) {
|
||||
this.fileId = fileId;
|
||||
this.emoji = emoji;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof Sticker && ((Sticker) obj).fileId.equals(fileId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (17 * emoji.hashCode()) << 32 + fileId.hashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,9 @@ import android.util.Log;
|
||||
*/
|
||||
public class GoalCounter {
|
||||
|
||||
public interface OnGoalReachListener {
|
||||
void onGoalReached();
|
||||
}
|
||||
|
||||
private int goal;
|
||||
private final int goal;
|
||||
private int i = 0;
|
||||
|
||||
private OnGoalReachListener listener;
|
||||
private final OnGoalReachListener listener;
|
||||
|
||||
public GoalCounter(int goal, OnGoalReachListener listener) {
|
||||
this.goal = goal;
|
||||
@@ -36,4 +31,8 @@ public class GoalCounter {
|
||||
public synchronized int value() {
|
||||
return i;
|
||||
}
|
||||
|
||||
public interface OnGoalReachListener {
|
||||
void onGoalReached();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user