fedilab-Android-App/app/src/main/java/app/fedilab/android/services/BackupStatusService.java

196 lines
9.0 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.services;
2018-01-06 15:14:11 +01:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-01-06 15:14:11 +01:00
*
* 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
2018-01-06 15:14:11 +01:00
* 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,
2018-01-06 15:14:11 +01:00
* see <http://www.gnu.org/licenses>. */
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
2018-01-06 17:13:18 +01:00
import android.os.Handler;
import android.os.Looper;
2019-08-26 16:17:06 +02:00
import android.os.SystemClock;
2018-01-06 15:14:11 +01:00
import android.text.Html;
2018-01-06 17:13:18 +01:00
import android.widget.Toast;
2019-11-15 16:32:25 +01:00
import androidx.annotation.Nullable;
2018-01-06 15:14:11 +01:00
import java.io.File;
2018-01-06 17:13:18 +01:00
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
2019-11-15 16:32:25 +01:00
import java.nio.charset.StandardCharsets;
2018-01-06 15:14:11 +01:00
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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.Attachment;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2018-01-06 15:14:11 +01:00
/**
* Created by Thomas on 06/01/2018.
* Manage service for owner status backup
*/
public class BackupStatusService extends IntentService {
2018-01-06 17:13:18 +01:00
private static int instanceRunning = 0;
2019-09-06 17:55:14 +02:00
2018-01-06 15:14:11 +01:00
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public BackupStatusService(String name) {
super(name);
}
2019-09-06 17:55:14 +02:00
2018-01-06 15:14:11 +01:00
public BackupStatusService() {
super("BackupStatusService");
}
public void onCreate() {
super.onCreate();
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
2019-09-06 17:55:14 +02:00
if (instanceRunning == 0) {
2020-05-16 11:32:09 +02:00
new Handler(Looper.getMainLooper()).post(() -> Toasty.info(BackupStatusService.this, getString(R.string.data_export_start), Toast.LENGTH_LONG).show());
2019-09-06 17:55:14 +02:00
} else {
2020-05-16 11:32:09 +02:00
new Handler(Looper.getMainLooper()).post(() -> Toasty.info(BackupStatusService.this, getString(R.string.data_export_running), Toast.LENGTH_LONG).show());
2018-01-06 17:13:18 +01:00
return;
}
instanceRunning++;
String message;
2018-01-06 15:14:11 +01:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
2019-06-05 14:35:42 +02:00
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-04-08 12:42:15 +02:00
Account account = new AccountDAO(BackupStatusService.this, db).getUniqAccount(userId, instance);
API api = new API(BackupStatusService.this, account.getInstance(), account.getToken());
2018-01-06 15:14:11 +01:00
try {
2018-01-06 17:13:18 +01:00
String fullPath;
Intent intentOpen;
String max_id = null;
int statusToBackUp = account.getStatuses_count();
List<Status> backupStatus = new ArrayList<>();
do {
APIResponse apiResponse = api.getStatus(userId, max_id);
max_id = apiResponse.getMax_id();
List<Status> statuses = apiResponse.getStatuses();
if (statuses.size() > 0)
backupStatus.addAll(statuses);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
SystemClock.sleep(2000);
}
2019-09-06 17:55:14 +02:00
} while (max_id != null);
2018-01-06 17:13:18 +01:00
2020-04-08 12:42:15 +02:00
String fileName = account.getAcct() + "@" + account.getInstance() + Helper.dateFileToString(BackupStatusService.this, new Date()) + ".csv";
2018-01-06 17:13:18 +01:00
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
2019-09-06 17:55:14 +02:00
fullPath = filePath + "/" + fileName;
2019-11-15 16:32:25 +01:00
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(fullPath)), StandardCharsets.UTF_8));
2018-01-06 17:13:18 +01:00
StringBuilder builder = new StringBuilder();
builder.append("id").append(',');
builder.append("uri").append(',');
builder.append("url").append(',');
builder.append("account").append(',');
builder.append("in_reply_to_id").append(',');
builder.append("in_reply_to_account_id").append(',');
builder.append("content").append(',');
builder.append("created_at").append(',');
builder.append("reblogs_count").append(',');
builder.append("favourites_count").append(',');
builder.append("sensitive").append(',');
builder.append("spoiler_text").append(',');
builder.append("visibility").append(',');
builder.append("media_attachments");
builder.append('\n');
2019-09-06 17:55:14 +02:00
for (Status status : backupStatus) {
2018-01-06 17:13:18 +01:00
//excludes reblog
2019-09-06 17:55:14 +02:00
if (status.getReblog() != null) {
2018-01-06 17:13:18 +01:00
statusToBackUp = statusToBackUp - 1;
continue;
}
builder.append("\"").append(status.getId()).append("\"").append(',');
builder.append("\"").append(status.getUri()).append("\"").append(',');
builder.append("\"").append(status.getUrl()).append("\"").append(',');
builder.append("\"").append(status.getAccount().getAcct()).append("\"").append(',');
builder.append("\"").append(status.getIn_reply_to_id()).append("\"").append(',');
builder.append("\"").append(status.getIn_reply_to_account_id()).append("\"").append(',');
2018-01-06 15:14:11 +01:00
String content;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
2018-01-06 17:13:18 +01:00
content = Html.fromHtml(status.getContent(), Html.FROM_HTML_MODE_LEGACY).toString();
2018-01-06 15:14:11 +01:00
else
2018-01-06 17:13:18 +01:00
content = Html.fromHtml(status.getContent()).toString();
2019-09-06 17:55:14 +02:00
builder.append("\"").append(content.replace("\"", "'").replace("\n", " ")).append("\"").append(',');
2020-04-08 12:42:15 +02:00
builder.append("\"").append(Helper.shortDateTime(BackupStatusService.this, status.getCreated_at())).append("\"").append(',');
2019-11-15 16:32:25 +01:00
builder.append("\"").append(status.getReblogs_count()).append("\"").append(',');
builder.append("\"").append(status.getFavourites_count()).append("\"").append(',');
builder.append("\"").append(status.isSensitive()).append("\"").append(',');
2019-09-06 17:55:14 +02:00
builder.append("\"").append(status.getSpoiler_text() != null ? status.getSpoiler_text() : "").append("\"").append(',');
2018-01-06 17:13:18 +01:00
builder.append("\"").append(status.getVisibility()).append("\"").append(',');
2019-09-06 17:55:14 +02:00
if (status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0) {
2018-01-06 17:13:18 +01:00
builder.append("\"");
2019-09-06 17:55:14 +02:00
for (Attachment attachment : status.getMedia_attachments()) {
2018-01-06 17:13:18 +01:00
builder.append(attachment.getUrl()).append(" ");
2018-01-06 15:14:11 +01:00
}
2018-01-06 17:13:18 +01:00
builder.append("\"");
2019-09-06 17:55:14 +02:00
} else {
2018-01-06 17:13:18 +01:00
builder.append("\"\"");
2018-01-06 15:14:11 +01:00
}
2018-01-06 17:13:18 +01:00
builder.append('\n');
2018-01-06 15:14:11 +01:00
}
2018-01-06 17:13:18 +01:00
pw.write(builder.toString());
pw.close();
message = getString(R.string.data_export_success, String.valueOf(statusToBackUp), String.valueOf(backupStatus.size()));
intentOpen = new Intent();
intentOpen.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + fullPath);
intentOpen.setDataAndType(uri, "text/csv");
String title = getString(R.string.data_export_toots, account.getAcct());
2020-04-08 12:42:15 +02:00
Helper.notify_user(BackupStatusService.this, account, intentOpen, BitmapFactory.decodeResource(getResources(),
Helper.getMainLogo(BackupStatusService.this)), Helper.NotifType.BACKUP, title, message);
2018-01-06 17:13:18 +01:00
} catch (Exception e) {
2018-01-06 15:14:11 +01:00
e.printStackTrace();
message = getString(R.string.data_export_error, account.getAcct());
2018-01-06 17:13:18 +01:00
final String finalMessage = message;
2020-05-16 11:32:09 +02:00
new Handler(Looper.getMainLooper()).post(() -> Toasty.error(BackupStatusService.this, finalMessage, Toast.LENGTH_LONG).show());
2018-01-06 15:14:11 +01:00
}
2018-01-06 17:13:18 +01:00
instanceRunning--;
2018-01-06 15:14:11 +01:00
}
}