mirror of https://codeberg.org/tom79/Fedilab
parent
a5c3da1491
commit
5521a14fdd
@ -0,0 +1,127 @@
|
||||
package app.fedilab.android.client.entities.app;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
|
||||
import app.fedilab.android.exception.DBException;
|
||||
import app.fedilab.android.helper.Helper;
|
||||
import app.fedilab.android.sqlite.Sqlite;
|
||||
|
||||
|
||||
public class DomainsBlock {
|
||||
|
||||
public static final String LAST_DATE_OF_UPDATE = "LAST_DATE_OF_UPDATE";
|
||||
public static List<String> trackingDomains = null;
|
||||
|
||||
private static void getDomains(Context context) {
|
||||
if (trackingDomains == null) {
|
||||
try {
|
||||
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
Cursor c = db.query(Sqlite.TABLE_DOMAINS_TRACKING, null, null, null, null, null, null, null);
|
||||
trackingDomains = cursorToDomain(c);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateDomains(Context _mContext) {
|
||||
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(_mContext);
|
||||
String last_date = sharedpreferences.getString(LAST_DATE_OF_UPDATE, null);
|
||||
Date dateUpdate = new Date(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(10));
|
||||
Date dateLastUpdate = Helper.stringToDate(_mContext, last_date);
|
||||
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
if (last_date == null || dateUpdate.after(dateLastUpdate)) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://hosts.fedilab.app/hosts").openConnection();
|
||||
if (connection.getResponseCode() > HttpsURLConnection.HTTP_MOVED_TEMP) {
|
||||
return;
|
||||
}
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String line;
|
||||
List<String> domains = new ArrayList<>();
|
||||
while ((line = br.readLine()) != null) {
|
||||
if (line.startsWith("0.0.0.0 ")) {
|
||||
domains.add(line.replace("0.0.0.0 ", "").trim());
|
||||
}
|
||||
}
|
||||
br.close();
|
||||
connection.disconnect();
|
||||
insertDomains(db, domains);
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(LAST_DATE_OF_UPDATE, Helper.dateToString(new Date()));
|
||||
editor.apply();
|
||||
} catch (IOException | DBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
} else {
|
||||
getDomains(_mContext);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a domains in db
|
||||
*
|
||||
* @param domains {@link List<String>}
|
||||
* @throws DBException exception with database
|
||||
*/
|
||||
private static void insertDomains(SQLiteDatabase db, List<String> domains) throws DBException {
|
||||
|
||||
if (db == null) {
|
||||
throw new DBException("db is null. Wrong initialization.");
|
||||
}
|
||||
db.delete(Sqlite.TABLE_DOMAINS_TRACKING, null, null);
|
||||
DomainsBlock.trackingDomains = new ArrayList<>();
|
||||
for (String domain : domains) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(Sqlite.COL_DOMAIN, domain);
|
||||
//Inserts token
|
||||
try {
|
||||
db.insertOrThrow(Sqlite.TABLE_DOMAINS_TRACKING, null, values);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
DomainsBlock.trackingDomains.add(domain);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* Method to hydrate domain from database
|
||||
* @param c Cursor
|
||||
* @return List<String>
|
||||
*/
|
||||
private static List<String> cursorToDomain(Cursor c) {
|
||||
//No element found
|
||||
if (c.getCount() == 0) {
|
||||
c.close();
|
||||
return null;
|
||||
}
|
||||
List<String> domains = new ArrayList<>();
|
||||
while (c.moveToNext()) {
|
||||
domains.add(c.getString(c.getColumnIndexOrThrow(Sqlite.COL_DOMAIN)));
|
||||
}
|
||||
//Close the cursor
|
||||
c.close();
|
||||
//domains list is returned
|
||||
return domains;
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
package app.fedilab.android.helper;
|
||||
/* Copyright 2022 Thomas Schneider
|
||||
*
|
||||
* This file is a part of Fedilab
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import app.fedilab.android.R;
|
||||
|
||||
|
||||
public class CountDrawable extends Drawable {
|
||||
|
||||
private final Paint mBadgePaint;
|
||||
private final Paint mTextPaint;
|
||||
private final Rect mTxtRect = new Rect();
|
||||
|
||||
private String mCount = "";
|
||||
private boolean mWillDraw;
|
||||
|
||||
public CountDrawable(Context context) {
|
||||
float mTextSize = context.getResources().getDimension(R.dimen.badge_count_textsize);
|
||||
|
||||
mBadgePaint = new Paint();
|
||||
mBadgePaint.setColor(ContextCompat.getColor(context, R.color.red_1));
|
||||
mBadgePaint.setAntiAlias(true);
|
||||
mBadgePaint.setStyle(Paint.Style.FILL);
|
||||
|
||||
mTextPaint = new Paint();
|
||||
mTextPaint.setColor(Color.WHITE);
|
||||
mTextPaint.setTypeface(Typeface.DEFAULT);
|
||||
mTextPaint.setTextSize(mTextSize);
|
||||
mTextPaint.setAntiAlias(true);
|
||||
mTextPaint.setTextAlign(Paint.Align.CENTER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(@NonNull Canvas canvas) {
|
||||
|
||||
if (!mWillDraw) {
|
||||
return;
|
||||
}
|
||||
Rect bounds = getBounds();
|
||||
float width = bounds.right - bounds.left;
|
||||
float height = bounds.bottom - bounds.top;
|
||||
|
||||
// Position the badge in the top-right quadrant of the icon.
|
||||
|
||||
/*Using Math.max rather than Math.min */
|
||||
|
||||
float radius = ((Math.max(width, height) / 2)) / 2;
|
||||
float centerX = (width - radius - 1) + 5;
|
||||
float centerY = radius - 5;
|
||||
if (mCount.length() <= 2) {
|
||||
// Draw badge circle.
|
||||
canvas.drawCircle(centerX, centerY, (int) (radius + 5.5), mBadgePaint);
|
||||
} else {
|
||||
canvas.drawCircle(centerX, centerY, (int) (radius + 6.5), mBadgePaint);
|
||||
}
|
||||
// Draw badge count text inside the circle.
|
||||
mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
|
||||
float textHeight = mTxtRect.bottom - mTxtRect.top;
|
||||
float textY = centerY + (textHeight / 2f);
|
||||
if (mCount.length() > 2)
|
||||
canvas.drawText("99+", centerX, textY, mTextPaint);
|
||||
else
|
||||
canvas.drawText(mCount, centerX, textY, mTextPaint);
|
||||
}
|
||||
|
||||
/*
|
||||
Sets the count (i.e notifications) to display.
|
||||
*/
|
||||
public void setCount(String count) {
|
||||
mCount = count;
|
||||
|
||||
// Only draw a badge if there are notifications.
|
||||
mWillDraw = !count.equalsIgnoreCase("0");
|
||||
invalidateSelf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlpha(int alpha) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColorFilter(ColorFilter cf) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOpacity() {
|
||||
return PixelFormat.UNKNOWN;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item
|
||||
android:drawable="@drawable/ic_block_script"
|
||||
android:gravity="center" />
|
||||
|
||||
<item
|
||||
android:id="@+id/ic_block_count"
|
||||
android:drawable="@color/transparent" />
|
||||
|
||||
</layer-list>
|
@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM4,12c0,-4.42 3.58,-8 8,-8 1.85,0 3.55,0.63 4.9,1.69L5.69,16.9C4.63,15.55 4,13.85 4,12zM12,20c-1.85,0 -3.55,-0.63 -4.9,-1.69L18.31,7.1C19.37,8.45 20,10.15 20,12c0,4.42 -3.58,8 -8,8z" />
|
||||
</vector>
|
Loading…
Reference in new issue