Auto update backup

This commit is contained in:
tom79 2019-08-07 08:53:15 +02:00
parent 32e5c08f2b
commit fc23a20910
3 changed files with 35 additions and 32 deletions

View File

@ -45,12 +45,14 @@ import java.util.concurrent.TimeUnit;
import app.fedilab.android.R;
import app.fedilab.android.activities.BaseMainActivity;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.activities.OwnerStatusActivity;
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.Notification;
import app.fedilab.android.fragments.DisplayNotificationsFragment;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.services.BackupStatusInDataBaseService;
import app.fedilab.android.services.BackupStatusService;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
@ -118,7 +120,7 @@ public class BackupStatusesSyncJob extends Job {
for(Account account: accounts) {
boolean autobackup = sharedpreferences.getBoolean(Helper.SET_AUTO_BACKUP_STATUSES + account.getId() + account.getInstance(), false);
if( autobackup) {
Intent backupIntent = new Intent(getContext(), BackupStatusService.class);
Intent backupIntent = new Intent(getContext(), BackupStatusInDataBaseService.class);
backupIntent.putExtra("userId", account.getId());
backupIntent.putExtra("instance", account.getInstance());
getContext().startService(backupIntent);

View File

@ -74,27 +74,42 @@ public class BackupStatusInDataBaseService extends IntentService {
@Override
protected void onHandleIntent(@Nullable Intent intent) {
boolean toastMessage = false;
String userId = null;
String instance = null;
if( intent != null){
userId = intent.getStringExtra("userId");
instance = intent.getStringExtra("instance");
toastMessage = true;
}
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;
if( instanceRunning == 0 ){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toasty.info(getApplicationContext(), getString(R.string.data_export_start), Toast.LENGTH_LONG).show();
if(finalToastMessage) {
Toasty.info(getApplicationContext(), getString(R.string.data_export_start), Toast.LENGTH_LONG).show();
}
}
});
}else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toasty.info(getApplicationContext(), getString(R.string.data_export_running), Toast.LENGTH_LONG).show();
if(finalToastMessage) {
Toasty.info(getApplicationContext(), getString(R.string.data_export_running), Toast.LENGTH_LONG).show();
}
}
});
return;
}
instanceRunning++;
String message;
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
SQLiteDatabase db = Sqlite.getInstance(BackupStatusInDataBaseService.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());
@ -126,8 +141,10 @@ public class BackupStatusInDataBaseService extends IntentService {
Intent mainActivity = new Intent(BackupStatusInDataBaseService.this, MainActivity.class);
mainActivity.putExtra(Helper.INTENT_ACTION, Helper.BACKUP_INTENT);
String title = getString(R.string.data_backup_toots, account.getAcct());
Helper.notify_user(getApplicationContext(),account, mainActivity, BitmapFactory.decodeResource(getResources(),
R.drawable.mastodonlogo), Helper.NotifType.BACKUP, title, message);
if(finalToastMessage) {
Helper.notify_user(getApplicationContext(), account, mainActivity, BitmapFactory.decodeResource(getResources(),
R.drawable.mastodonlogo), Helper.NotifType.BACKUP, title, message);
}
} catch (Exception e) {
e.printStackTrace();
message = getString(R.string.data_export_error, account.getAcct());
@ -135,7 +152,9 @@ public class BackupStatusInDataBaseService extends IntentService {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toasty.error(getApplicationContext(), finalMessage, Toast.LENGTH_LONG).show();
if(finalToastMessage) {
Toasty.error(getApplicationContext(), finalMessage, Toast.LENGTH_LONG).show();
}
}
});
}

View File

@ -81,32 +81,18 @@ public class BackupStatusService extends IntentService {
@Override
protected void onHandleIntent(@Nullable Intent intent) {
String userId = null;
String instance = null;
boolean toastMessage = false;
if( intent != null){
userId = intent.getStringExtra("userId");
instance = intent.getStringExtra("instance");
toastMessage = true;
}
boolean finalToastMessage = toastMessage;
if( instanceRunning == 0 ){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if(finalToastMessage) {
Toasty.info(getApplicationContext(), getString(R.string.data_export_start), Toast.LENGTH_LONG).show();
}
Toasty.info(getApplicationContext(), getString(R.string.data_export_start), Toast.LENGTH_LONG).show();
}
});
}else {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if(finalToastMessage) {
Toasty.info(getApplicationContext(), getString(R.string.data_export_running), Toast.LENGTH_LONG).show();
}
Toasty.info(getApplicationContext(), getString(R.string.data_export_running), Toast.LENGTH_LONG).show();
}
});
return;
@ -114,10 +100,8 @@ public class BackupStatusService extends IntentService {
instanceRunning++;
String message;
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);
}
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
SQLiteDatabase db = Sqlite.getInstance(BackupStatusService.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());
@ -208,9 +192,7 @@ public class BackupStatusService extends IntentService {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if( finalToastMessage) {
Toasty.error(getApplicationContext(), finalMessage, Toast.LENGTH_LONG).show();
}
Toasty.error(getApplicationContext(), finalMessage, Toast.LENGTH_LONG).show();
}
});
}