2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.services;
|
2018-02-17 08:44:13 +01:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2018-02-17 08:44:13 +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-02-17 08:44:13 +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-02-17 08:44:13 +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.os.Handler;
|
|
|
|
import android.os.Looper;
|
2019-11-15 16:32:25 +01:00
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.widget.Toast;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2019-06-11 19:38:26 +02:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2019-08-26 16:17:06 +02:00
|
|
|
|
2018-02-17 08:44:13 +01:00
|
|
|
import java.util.ArrayList;
|
2019-08-07 10:24:35 +02:00
|
|
|
import java.util.Date;
|
2018-02-17 08:44:13 +01:00
|
|
|
import java.util.List;
|
2018-11-25 10:45:16 +01:00
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.activities.MainActivity;
|
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.Status;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.sqlite.AccountDAO;
|
|
|
|
import app.fedilab.android.sqlite.Sqlite;
|
|
|
|
import app.fedilab.android.sqlite.StatusCacheDAO;
|
2018-11-25 10:45:16 +01:00
|
|
|
import es.dmoral.toasty.Toasty;
|
2018-02-17 08:44:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-17 09:30:44 +01:00
|
|
|
* Created by Thomas on 17/02/2018.
|
2018-02-17 08:44:13 +01:00
|
|
|
* Manage service for owner status backup in database
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class BackupStatusInDataBaseService extends IntentService {
|
|
|
|
|
|
|
|
|
|
|
|
private static int instanceRunning = 0;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2018-02-17 08:44:13 +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 BackupStatusInDataBaseService(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2018-02-17 08:44:13 +01:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public BackupStatusInDataBaseService() {
|
2018-02-17 09:30:44 +01:00
|
|
|
super("BackupStatusInDataBaseService");
|
2018-02-17 08:44:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onHandleIntent(@Nullable Intent intent) {
|
2019-08-07 09:47:16 +02:00
|
|
|
boolean toastMessage = true;
|
2019-08-07 08:53:15 +02:00
|
|
|
String userId = null;
|
|
|
|
String instance = null;
|
2019-08-07 10:24:35 +02:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (intent != null && intent.hasExtra("userid") && intent.hasExtra("instance")) {
|
2019-08-07 10:24:35 +02:00
|
|
|
userId = intent.getStringExtra("userid");
|
2019-08-07 08:53:15 +02:00
|
|
|
instance = intent.getStringExtra("instance");
|
2019-08-07 09:47:16 +02:00
|
|
|
toastMessage = false;
|
2019-08-07 08:53:15 +02:00
|
|
|
}
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (userId == null || instance == null) {
|
2019-08-07 08:53:15 +02:00
|
|
|
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
|
|
|
|
}
|
|
|
|
boolean finalToastMessage = toastMessage;
|
2019-09-06 17:55:14 +02:00
|
|
|
if (instanceRunning == 0) {
|
2020-05-16 11:32:09 +02:00
|
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
|
|
if (finalToastMessage) {
|
|
|
|
Toasty.info(BackupStatusInDataBaseService.this, getString(R.string.data_export_start), Toast.LENGTH_LONG).show();
|
2018-02-17 08:44:13 +01:00
|
|
|
}
|
|
|
|
});
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2020-05-16 11:32:09 +02:00
|
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
|
|
if (finalToastMessage) {
|
|
|
|
Toasty.info(BackupStatusInDataBaseService.this, getString(R.string.data_export_running), Toast.LENGTH_LONG).show();
|
2018-02-17 08:44:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
instanceRunning++;
|
|
|
|
String message;
|
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(BackupStatusInDataBaseService.this, db).getUniqAccount(userId, instance);
|
|
|
|
API api = new API(BackupStatusInDataBaseService.this, account.getInstance(), account.getToken());
|
2018-02-17 08:44:13 +01:00
|
|
|
try {
|
2018-02-17 09:30:44 +01:00
|
|
|
//Starts from the last recorded ID
|
2019-08-07 13:50:53 +02:00
|
|
|
Date sinceDate = new StatusCacheDAO(BackupStatusInDataBaseService.this, db).getLastTootDateCache(StatusCacheDAO.ARCHIVE_CACHE, userId, instance);
|
2018-02-17 08:44:13 +01:00
|
|
|
String max_id = null;
|
|
|
|
List<Status> backupStatus = new ArrayList<>();
|
2018-02-17 09:30:44 +01:00
|
|
|
boolean canContinue = true;
|
2018-02-17 08:44:13 +01:00
|
|
|
do {
|
|
|
|
APIResponse apiResponse = api.getStatus(userId, max_id);
|
|
|
|
max_id = apiResponse.getMax_id();
|
|
|
|
List<Status> statuses = apiResponse.getStatuses();
|
2019-09-06 17:55:14 +02:00
|
|
|
for (Status tmpStatus : statuses) {
|
|
|
|
if (sinceDate != null && max_id != null && tmpStatus.getCreated_at().before(sinceDate)) {
|
2018-02-17 09:30:44 +01:00
|
|
|
canContinue = false;
|
|
|
|
break;
|
2018-02-17 08:44:13 +01:00
|
|
|
}
|
2019-08-07 13:50:53 +02:00
|
|
|
new StatusCacheDAO(BackupStatusInDataBaseService.this, db).insertStatus(StatusCacheDAO.ARCHIVE_CACHE, tmpStatus, userId, instance);
|
2018-02-17 09:30:44 +01:00
|
|
|
backupStatus.add(tmpStatus);
|
2018-02-17 08:44:13 +01:00
|
|
|
}
|
2019-10-20 16:21:04 +02:00
|
|
|
try {
|
|
|
|
Thread.sleep(2000);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
SystemClock.sleep(2000);
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
} while (max_id != null && canContinue);
|
2018-02-17 09:30:44 +01:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (backupStatus.size() > 0) {
|
2018-02-17 16:09:06 +01:00
|
|
|
Intent backupIntent = new Intent(Helper.INTENT_BACKUP_FINISH);
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(backupIntent);
|
|
|
|
}
|
2018-02-17 15:42:09 +01:00
|
|
|
message = getString(R.string.data_backup_success, String.valueOf(backupStatus.size()));
|
2018-02-17 15:51:01 +01:00
|
|
|
Intent mainActivity = new Intent(BackupStatusInDataBaseService.this, MainActivity.class);
|
|
|
|
mainActivity.putExtra(Helper.INTENT_ACTION, Helper.BACKUP_INTENT);
|
2018-02-17 15:42:09 +01:00
|
|
|
String title = getString(R.string.data_backup_toots, account.getAcct());
|
2019-09-06 17:55:14 +02:00
|
|
|
if (finalToastMessage) {
|
2020-04-08 12:42:15 +02:00
|
|
|
Helper.notify_user(BackupStatusInDataBaseService.this, account, mainActivity, BitmapFactory.decodeResource(getResources(),
|
|
|
|
Helper.getMainLogo(BackupStatusInDataBaseService.this)), Helper.NotifType.BACKUP, title, message);
|
2019-08-07 08:53:15 +02:00
|
|
|
}
|
2018-02-17 08:44:13 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
message = getString(R.string.data_export_error, account.getAcct());
|
|
|
|
final String finalMessage = message;
|
2020-05-16 11:32:09 +02:00
|
|
|
new Handler(Looper.getMainLooper()).post(() -> {
|
|
|
|
if (finalToastMessage) {
|
|
|
|
Toasty.error(BackupStatusInDataBaseService.this, finalMessage, Toast.LENGTH_LONG).show();
|
2018-02-17 08:44:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
instanceRunning--;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|