diff --git a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java index 16f46675c..611e84105 100644 --- a/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java +++ b/app/src/main/java/app/fedilab/android/fragments/ContentSettingsFragment.java @@ -311,6 +311,19 @@ public class ContentSettingsFragment extends Fragment implements ScreenShotable editor.apply(); } }); + + boolean auto_backup_notifications = sharedpreferences.getBoolean(Helper.SET_AUTO_BACKUP_NOTIFICATIONS+userId+instance, false); + final CheckBox set_auto_backup_notifications = rootView.findViewById(R.id.set_auto_backup_notifications); + set_auto_backup_notifications.setChecked(auto_backup_notifications); + set_auto_backup_notifications.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putBoolean(Helper.SET_AUTO_BACKUP_NOTIFICATIONS+userId+instance, set_auto_backup_notifications.isChecked()); + editor.apply(); + } + }); + final LinearLayout set_auto_backup_text = rootView.findViewById(R.id.set_auto_backup_text); set_auto_backup_text.setOnClickListener(view -> set_auto_backup.performClick()); diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index 9bb5cf898..bc7761267 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -372,6 +372,7 @@ public class Helper { public static final String SET_DISPLAY_ADMIN_STATUSES = "set_display_admin_statuses"; public static final String SET_DISPLAY_FEDILAB_FEATURES_BUTTON = "set_display_fedilab_features_button"; public static final String SET_AUTO_BACKUP_STATUSES = "set_auto_backup_statuses"; + public static final String SET_AUTO_BACKUP_NOTIFICATIONS = "set_auto_backup_notifications"; public static final int S_NO = 0; static final int S_512KO = 1; diff --git a/app/src/main/java/app/fedilab/android/jobs/ApplicationJob.java b/app/src/main/java/app/fedilab/android/jobs/ApplicationJob.java index 63e373f74..91e203ec1 100644 --- a/app/src/main/java/app/fedilab/android/jobs/ApplicationJob.java +++ b/app/src/main/java/app/fedilab/android/jobs/ApplicationJob.java @@ -36,6 +36,8 @@ public class ApplicationJob implements JobCreator { return new ScheduledBoostsSyncJob(); case BackupStatusesSyncJob.BACKUP_SYNC: return new BackupStatusesSyncJob(); + case BackupNotificationsSyncJob.BACKUP_NOTIFICATIONS_SYNC: + return new BackupNotificationsSyncJob(); default: return null; } diff --git a/app/src/main/java/app/fedilab/android/jobs/BackupNotificationsSyncJob.java b/app/src/main/java/app/fedilab/android/jobs/BackupNotificationsSyncJob.java new file mode 100644 index 000000000..883c33280 --- /dev/null +++ b/app/src/main/java/app/fedilab/android/jobs/BackupNotificationsSyncJob.java @@ -0,0 +1,103 @@ +package app.fedilab.android.jobs; +/* 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 . */ + +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; + +import androidx.annotation.NonNull; + +import com.evernote.android.job.Job; +import com.evernote.android.job.JobManager; +import com.evernote.android.job.JobRequest; + +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +import app.fedilab.android.client.Entities.Account; +import app.fedilab.android.helper.Helper; +import app.fedilab.android.services.BackupNotificationInDataBaseService; +import app.fedilab.android.sqlite.AccountDAO; +import app.fedilab.android.sqlite.Sqlite; + + +/** + * Created by Thomas on 24/08/2019. + * backup notifications + */ + +public class BackupNotificationsSyncJob extends Job { + + static final String BACKUP_NOTIFICATIONS_SYNC = "job_backup_notification"; + static { + Helper.installProvider(); + } + + @NonNull + @Override + protected Result onRunJob(@NonNull Params params) { + //Code refresh here + + backupService(); + return Result.SUCCESS; + } + + + public static int schedule(boolean updateCurrent) { + + Set jobRequests = JobManager.instance().getAllJobRequestsForTag(BACKUP_NOTIFICATIONS_SYNC); + if (!jobRequests.isEmpty() && !updateCurrent) { + return jobRequests.iterator().next().getJobId(); + } + + int jobRequestschedule = -1; + try { + jobRequestschedule = new JobRequest.Builder(BackupNotificationsSyncJob.BACKUP_NOTIFICATIONS_SYNC) + .setPeriodic(TimeUnit.MINUTES.toMillis(Helper.MINUTES_BETWEEN_BACKUP), TimeUnit.MINUTES.toMillis(5)) + .setUpdateCurrent(updateCurrent) + .setRequiredNetworkType(JobRequest.NetworkType.METERED) + .setRequirementsEnforced(false) + .build() + .schedule(); + }catch (Exception ignored){} + + return jobRequestschedule; + } + + + /** + * Task in background starts here. + */ + private void backupService() { + SQLiteDatabase db = Sqlite.getInstance(getContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + final List accounts = new AccountDAO(getContext(), db).getAllAccount(); + SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + for(Account account: accounts) { + boolean autobackup = sharedpreferences.getBoolean(Helper.SET_AUTO_BACKUP_NOTIFICATIONS + account.getId() + account.getInstance(), false); + if( autobackup) { + try { + Intent backupIntent = new Intent(getContext(), BackupNotificationInDataBaseService.class); + backupIntent.putExtra("userid", account.getId()); + backupIntent.putExtra("instance", account.getInstance()); + getContext().startService(backupIntent); + }catch (Exception ignored){} + } + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_settings_reveal.xml b/app/src/main/res/layout/fragment_settings_reveal.xml index 845878608..21cafc784 100644 --- a/app/src/main/res/layout/fragment_settings_reveal.xml +++ b/app/src/main/res/layout/fragment_settings_reveal.xml @@ -981,7 +981,35 @@ android:layout_height="wrap_content" /> - + + + + + + + Backup Auto backup statuses This option is per account. It will launch a service that will automatically store your statuses locally in the database. That allows to get statistics and charts + Auto backup notifications + This option is per account. It will launch a service that will automatically store your notifications locally in the database. That allows to get statistics and charts Report account Send an invitation Your instance does not allow to register a new account!