2019-08-25 12:08:18 +02:00
|
|
|
package app.fedilab.android.services;
|
|
|
|
/* Copyright 2019 Thomas Schneider
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
|
|
|
* 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-08-26 16:17:06 +02:00
|
|
|
import android.os.SystemClock;
|
2019-08-25 12:08:18 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import app.fedilab.android.R;
|
|
|
|
import app.fedilab.android.activities.MainActivity;
|
|
|
|
import app.fedilab.android.client.API;
|
|
|
|
import app.fedilab.android.client.APIResponse;
|
|
|
|
import app.fedilab.android.client.Entities.Account;
|
2019-08-25 14:49:20 +02:00
|
|
|
import app.fedilab.android.client.Entities.Notification;
|
2019-08-25 12:08:18 +02:00
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.sqlite.AccountDAO;
|
2019-08-25 14:49:20 +02:00
|
|
|
import app.fedilab.android.sqlite.NotificationCacheDAO;
|
2019-08-25 12:08:18 +02:00
|
|
|
import app.fedilab.android.sqlite.Sqlite;
|
|
|
|
import es.dmoral.toasty.Toasty;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 24/08/2019.
|
|
|
|
* Manage service for owner notification backup in database
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class BackupNotificationInDataBaseService extends IntentService {
|
|
|
|
|
|
|
|
|
|
|
|
private static int instanceRunning = 0;
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2019-08-25 12:08:18 +02: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 BackupNotificationInDataBaseService(String name) {
|
|
|
|
super(name);
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2019-08-25 12:08:18 +02:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
public BackupNotificationInDataBaseService() {
|
|
|
|
super("BackupNotificationInDataBaseService");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onHandleIntent(@Nullable Intent intent) {
|
|
|
|
boolean toastMessage = true;
|
|
|
|
String userId = null;
|
|
|
|
String instance = null;
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (intent != null && intent.hasExtra("userid") && intent.hasExtra("instance")) {
|
2019-08-25 12:08:18 +02:00
|
|
|
userId = intent.getStringExtra("userid");
|
|
|
|
instance = intent.getStringExtra("instance");
|
|
|
|
toastMessage = false;
|
|
|
|
}
|
|
|
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (userId == null || instance == null) {
|
2019-08-25 12:08:18 +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) {
|
2019-08-25 12:08:18 +02:00
|
|
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (finalToastMessage) {
|
2019-08-25 12:08:18 +02:00
|
|
|
Toasty.info(getApplicationContext(), getString(R.string.data_export_start), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-09-06 17:55:14 +02:00
|
|
|
} else {
|
2019-08-25 12:08:18 +02:00
|
|
|
new Handler(Looper.getMainLooper()).post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2019-09-06 17:55:14 +02:00
|
|
|
if (finalToastMessage) {
|
2019-08-25 12:08:18 +02:00
|
|
|
Toasty.info(getApplicationContext(), getString(R.string.data_export_running), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
instanceRunning++;
|
|
|
|
String message;
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(BackupNotificationInDataBaseService.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
Account account = new AccountDAO(getApplicationContext(), db).getUniqAccount(userId, instance);
|
|
|
|
API api = new API(getApplicationContext(), account.getInstance(), account.getToken());
|
|
|
|
try {
|
|
|
|
//Starts from the last recorded ID
|
2019-08-26 16:17:06 +02:00
|
|
|
String lastId = new NotificationCacheDAO(BackupNotificationInDataBaseService.this, db).getLastNotificationIDCache(userId, instance);
|
2019-08-25 12:08:18 +02:00
|
|
|
String max_id = null;
|
2019-08-25 14:49:20 +02:00
|
|
|
List<Notification> backupNotifications = new ArrayList<>();
|
2019-08-25 12:08:18 +02:00
|
|
|
boolean canContinue = true;
|
|
|
|
do {
|
2019-08-25 14:49:20 +02:00
|
|
|
APIResponse apiResponse = api.getNotifications(max_id);
|
2019-08-26 16:17:06 +02:00
|
|
|
|
2019-08-25 12:08:18 +02:00
|
|
|
max_id = apiResponse.getMax_id();
|
2019-08-25 14:49:20 +02:00
|
|
|
List<Notification> notifications = apiResponse.getNotifications();
|
2019-09-06 17:55:14 +02:00
|
|
|
for (Notification tmpNotification : notifications) {
|
|
|
|
if (lastId != null && tmpNotification.getId().compareTo(lastId) <= 0) {
|
2019-08-25 12:08:18 +02:00
|
|
|
canContinue = false;
|
|
|
|
break;
|
|
|
|
}
|
2019-08-26 16:17:06 +02:00
|
|
|
|
2019-08-25 14:49:20 +02:00
|
|
|
new NotificationCacheDAO(BackupNotificationInDataBaseService.this, db).insertNotification(tmpNotification, userId, instance);
|
|
|
|
backupNotifications.add(tmpNotification);
|
2019-08-25 12:08:18 +02: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);
|
2019-08-25 12:08:18 +02:00
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
if (backupNotifications.size() > 0) {
|
2019-08-25 12:08:18 +02:00
|
|
|
Intent backupIntent = new Intent(Helper.INTENT_BACKUP_FINISH);
|
|
|
|
LocalBroadcastManager.getInstance(this).sendBroadcast(backupIntent);
|
|
|
|
}
|
2019-08-26 17:47:00 +02:00
|
|
|
message = getString(R.string.data_backup_notification_success, String.valueOf(backupNotifications.size()));
|
2019-08-25 12:08:18 +02:00
|
|
|
Intent mainActivity = new Intent(BackupNotificationInDataBaseService.this, MainActivity.class);
|
2019-08-25 17:21:34 +02:00
|
|
|
mainActivity.putExtra(Helper.INTENT_ACTION, Helper.BACKUP_NOTIFICATION_INTENT);
|
2019-08-25 12:08:18 +02:00
|
|
|
String title = getString(R.string.data_backup_toots, account.getAcct());
|
2019-09-06 17:55:14 +02:00
|
|
|
if (finalToastMessage) {
|
2019-09-25 15:18:41 +02:00
|
|
|
|
2019-08-25 12:08:18 +02:00
|
|
|
Helper.notify_user(getApplicationContext(), account, mainActivity, BitmapFactory.decodeResource(getResources(),
|
2019-09-25 15:44:52 +02:00
|
|
|
Helper.getMainLogo(getApplicationContext())), Helper.NotifType.BACKUP, title, message);
|
2019-08-25 12:08:18 +02: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-09-06 17:55:14 +02:00
|
|
|
if (finalToastMessage) {
|
2019-08-25 12:08:18 +02:00
|
|
|
Toasty.error(getApplicationContext(), finalMessage, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
instanceRunning--;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|