fedilab-Android-App/app/src/main/java/app/fedilab/android/asynctasks/PostStatusAsyncTask.java

137 lines
6.2 KiB
Java
Raw Normal View History

/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* 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.
*
2019-05-18 11:10:30 +02:00
* 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.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.asynctasks;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
2021-01-19 17:43:51 +01:00
import android.os.Handler;
import android.os.Looper;
2017-10-27 15:09:26 +02:00
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.regex.Matcher;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Error;
import app.fedilab.android.client.GNUAPI;
import app.fedilab.android.helper.Helper;
2021-03-01 18:19:08 +01:00
import app.fedilab.android.helper.PushHelper;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.interfaces.OnPostStatusActionInterface;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.TagsCacheDAO;
/**
* Created by Thomas on 21/07/2017.
* Posts status (live version) - scheduled toots are sent via classic post feature in api
*/
2021-01-19 17:43:51 +01:00
public class PostStatusAsyncTask {
2021-01-19 17:43:51 +01:00
private final OnPostStatusActionInterface listener;
private final app.fedilab.android.client.Entities.Status status;
private final Account account;
private final WeakReference<Context> contextReference;
private final UpdateAccountInfoAsyncTask.SOCIAL social;
private APIResponse apiResponse;
2017-10-27 15:09:26 +02:00
2019-09-06 17:55:14 +02:00
public PostStatusAsyncTask(Context context, UpdateAccountInfoAsyncTask.SOCIAL social, Account account, app.fedilab.android.client.Entities.Status status, OnPostStatusActionInterface onPostStatusActionInterface) {
2017-10-27 15:09:26 +02:00
this.contextReference = new WeakReference<>(context);
this.listener = onPostStatusActionInterface;
this.status = status;
2017-10-04 15:57:53 +02:00
this.account = account;
2019-07-31 19:14:42 +02:00
this.social = social;
2021-01-19 17:43:51 +01:00
doInBackground();
}
2021-01-19 17:43:51 +01:00
protected void doInBackground() {
2019-01-25 16:54:48 +01:00
2021-01-19 17:43:51 +01:00
new Thread(() -> {
boolean isconnected = Helper.isConnectedToInternet(contextReference.get(), Helper.getLiveInstance(contextReference.get()));
if (social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
if (isconnected) {
if (account == null) {
apiResponse = new API(this.contextReference.get()).postStatusAction(status);
} else {
apiResponse = new API(this.contextReference.get(), account.getInstance(), account.getToken()).postStatusAction(status);
}
2020-08-09 15:23:55 +02:00
} else {
2021-01-19 17:43:51 +01:00
apiResponse = new APIResponse();
Error error = new Error();
error.setError(contextReference.get().getString(R.string.no_internet));
error.setStatusCode(-33);
apiResponse.setError(error);
2020-08-09 15:23:55 +02:00
}
2019-09-06 17:55:14 +02:00
} else {
2021-01-19 17:43:51 +01:00
if (isconnected) {
if (account == null) {
apiResponse = new GNUAPI(this.contextReference.get()).postStatusAction(status);
} else {
apiResponse = new GNUAPI(this.contextReference.get(), account.getInstance(), account.getToken()).postStatusAction(status);
}
2020-08-09 15:23:55 +02:00
} else {
2021-01-19 17:43:51 +01:00
apiResponse = new APIResponse();
Error error = new Error();
error.setError(contextReference.get().getString(R.string.no_internet));
error.setStatusCode(-33);
apiResponse.setError(error);
2020-08-09 15:23:55 +02:00
}
2019-02-03 18:29:34 +01:00
}
2021-01-19 17:43:51 +01:00
if (status.getIn_reply_to_id() != null) {
apiResponse.setTargetedId(status.getIn_reply_to_id());
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> {
listener.onPostStatusAction(apiResponse);
//Search for tag with upper cases to store them locally
Thread thread = new Thread() {
@Override
public void run() {
String content = status.getContent();
if (content != null && content.length() > 0) {
SQLiteDatabase db = Sqlite.getInstance(contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Matcher matcher = Helper.hashtagPattern.matcher(content);
while (matcher.find()) {
int matchStart = matcher.start(1);
int matchEnd = matcher.end();
//Get cached tags
List<String> cachedTag = new TagsCacheDAO(contextReference.get(), db).getAll();
String tag = content.substring(matchStart, matchEnd);
tag = tag.replace("#", "");
if (cachedTag == null) {
new TagsCacheDAO(contextReference.get(), db).insert(tag);
} else {
//If cache doesn't contain the tag and the tag has upper case
if (!cachedTag.contains(tag) && !tag.toLowerCase().equals(tag)) {
new TagsCacheDAO(contextReference.get(), db).insert(tag);
}
}
}
}
}
2021-01-19 17:43:51 +01:00
};
thread.start();
};
mainHandler.post(myRunnable);
}).start();
}
}