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

169 lines
6.8 KiB
Java
Raw Normal View History

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-06-11 19:38:26 +02:00
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2018-02-17 08:44:13 +01:00
import android.widget.Toast;
2018-11-25 10:45:16 +01: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-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;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
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;
/**
* 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);
}
@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
if( intent != null && intent.hasExtra("userid") && intent.hasExtra("instance")){
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);
if( userId == null || instance == null) {
userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
}
boolean finalToastMessage = toastMessage;
2018-02-17 08:44:13 +01:00
if( instanceRunning == 0 ){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
2019-08-07 08:53:15 +02:00
if(finalToastMessage) {
Toasty.info(getApplicationContext(), getString(R.string.data_export_start), Toast.LENGTH_LONG).show();
}
2018-02-17 08:44:13 +01:00
}
});
}else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
2019-08-07 08:53:15 +02:00
if(finalToastMessage) {
Toasty.info(getApplicationContext(), getString(R.string.data_export_running), Toast.LENGTH_LONG).show();
}
2018-02-17 08:44:13 +01:00
}
});
return;
}
instanceRunning++;
String message;
SQLiteDatabase db = Sqlite.getInstance(BackupStatusInDataBaseService.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-06-05 14:35:42 +02:00
Account account = new AccountDAO(getApplicationContext(), db).getUniqAccount(userId, instance);
2018-02-17 08:44:13 +01:00
API api = new API(getApplicationContext(), account.getInstance(), account.getToken());
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();
2018-02-17 09:30:44 +01:00
for(Status tmpStatus : statuses) {
2019-08-07 10:24:35 +02:00
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
}
2018-02-17 09:30:44 +01:00
}while (max_id != null && canContinue);
2018-02-17 16:09:06 +01:00
if(backupStatus.size() > 0){
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-08-07 08:53:15 +02:00
if(finalToastMessage) {
Helper.notify_user(getApplicationContext(), account, mainActivity, BitmapFactory.decodeResource(getResources(),
R.drawable.mastodonlogo), Helper.NotifType.BACKUP, title, message);
}
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;
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
2019-08-07 08:53:15 +02:00
if(finalToastMessage) {
Toasty.error(getApplicationContext(), finalMessage, Toast.LENGTH_LONG).show();
}
2018-02-17 08:44:13 +01:00
}
});
}
instanceRunning--;
}
}