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

206 lines
9.3 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;
2018-01-06 15:14:11 +01:00
import android.support.annotation.Nullable;
import android.text.Html;
2018-01-06 17:13:18 +01:00
import android.widget.Toast;
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;
2018-01-06 15:14:11 +01:00
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
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;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
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;
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.
*/
@SuppressWarnings("unused")
public BackupStatusService(String name) {
super(name);
}
@SuppressWarnings("unused")
public BackupStatusService() {
super("BackupStatusService");
}
public void onCreate() {
super.onCreate();
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
2018-01-06 17:13:18 +01:00
if( instanceRunning == 0 ){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
2018-11-25 10:45:16 +01:00
Toasty.info(getApplicationContext(), getString(R.string.data_export_start), Toast.LENGTH_LONG).show();
2018-01-06 17:13:18 +01:00
}
});
}else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
2018-11-25 10:45:16 +01:00
Toasty.info(getApplicationContext(), 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);
SQLiteDatabase db = Sqlite.getInstance(BackupStatusService.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(getApplicationContext(), db).getAccountByID(userId);
2018-01-06 17:13:18 +01:00
API api = new API(getApplicationContext(), 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);
}while (max_id != null);
String fileName = account.getAcct()+"@"+account.getInstance()+ Helper.dateFileToString(getApplicationContext(), new Date())+".csv";
String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
fullPath = filePath+"/"+fileName;
PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(fullPath)), "UTF-8"));
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');
2018-01-06 15:14:11 +01:00
for( Status status: backupStatus){
2018-01-06 17:13:18 +01:00
//excludes reblog
if( status.getReblog() != null){
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
//noinspection deprecation
2018-01-06 17:13:18 +01:00
content = Html.fromHtml(status.getContent()).toString();
builder.append("\"").append(content.replace("\"", "'").replace("\n"," ")).append("\"").append(',');
builder.append("\"").append(Helper.shortDateTime(getApplicationContext(), status.getCreated_at())).append("\"").append(',');
builder.append("\"").append(String.valueOf(status.getReblogs_count())).append("\"").append(',');
builder.append("\"").append(String.valueOf(status.getFavourites_count())).append("\"").append(',');
builder.append("\"").append(String.valueOf(status.isSensitive())).append("\"").append(',');
builder.append("\"").append(status.getSpoiler_text() !=null?status.getSpoiler_text():"").append("\"").append(',');
builder.append("\"").append(status.getVisibility()).append("\"").append(',');
2018-01-06 15:14:11 +01:00
if( status.getMedia_attachments() != null && status.getMedia_attachments().size() > 0){
2018-01-06 17:13:18 +01:00
builder.append("\"");
2018-01-06 15:14:11 +01: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("\"");
2018-01-06 15:14:11 +01: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");
long notif_id = Long.parseLong(account.getId());
int notificationId = ((notif_id + 3) > 2147483647) ? (int) (2147483647 - notif_id - 3) : (int) (notif_id + 3);
String title = getString(R.string.data_export_toots, account.getAcct());
2019-05-18 11:10:30 +02:00
Helper.notify_user(getApplicationContext(), intentOpen, notificationId, BitmapFactory.decodeResource(getResources(),
2018-09-16 18:03:24 +02:00
R.drawable.mastodonlogo),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;
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
2018-11-25 10:45:16 +01:00
Toasty.error(getApplicationContext(), finalMessage, Toast.LENGTH_LONG).show();
2018-01-06 17:13:18 +01:00
}
});
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
}
}