Skip to content

Commit

Permalink
Allow user-selected backup time.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholas-signal authored and greyson-signal committed Jan 25, 2023
1 parent a7d9bd9 commit 8dd1d3b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public final class SettingsValues extends SignalStoreValues {
public static final String PREFER_SYSTEM_EMOJI = "settings.use.system.emoji";
public static final String ENTER_KEY_SENDS = "settings.enter.key.sends";
public static final String BACKUPS_ENABLED = "settings.backups.enabled";
public static final String BACKUPS_SCHEDULE_HOUR = "settings.backups.schedule.hour";
public static final String BACKUPS_SCHEDULE_MINUTE = "settings.backups.schedule.minute";
public static final String SMS_DELIVERY_REPORTS_ENABLED = "settings.sms.delivery.reports.enabled";
public static final String WIFI_CALLING_COMPATIBILITY_MODE_ENABLED = "settings.wifi.calling.compatibility.mode.enabled";
public static final String MESSAGE_NOTIFICATIONS_ENABLED = "settings.message.notifications.enabled";
Expand Down Expand Up @@ -263,6 +265,19 @@ public void setBackupEnabled(boolean backupEnabled) {
putBoolean(BACKUPS_ENABLED, backupEnabled);
}

public int getBackupHour() {
return getInteger(BACKUPS_SCHEDULE_HOUR, 2);
}

public int getBackupMinute() {
return getInteger(BACKUPS_SCHEDULE_MINUTE, 0);
}

public void setBackupSchedule(int hour, int minute) {
putInteger(BACKUPS_SCHEDULE_HOUR, hour);
putInteger(BACKUPS_SCHEDULE_MINUTE, minute);
}

public boolean isSmsDeliveryReportsEnabled() {
return getBoolean(SMS_DELIVERY_REPORTS_ENABLED, TextSecurePreferences.isSmsDeliveryReportsEnabled(ApplicationDependencies.getApplication()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.method.LinkMovementMethod;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,6 +22,9 @@
import androidx.core.text.HtmlCompat;
import androidx.fragment.app.Fragment;

import com.google.android.material.timepicker.MaterialTimePicker;
import com.google.android.material.timepicker.TimeFormat;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
Expand All @@ -32,10 +38,14 @@
import org.thoughtcrime.securesms.jobs.LocalBackupJob;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.permissions.Permissions;
import org.thoughtcrime.securesms.service.LocalBackupListener;
import org.thoughtcrime.securesms.util.BackupUtil;
import org.thoughtcrime.securesms.util.JavaTimeExtensionsKt;
import org.thoughtcrime.securesms.util.StorageUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;

import java.text.NumberFormat;
import java.time.LocalTime;
import java.util.Locale;
import java.util.Objects;

Expand All @@ -48,6 +58,8 @@ public class BackupsPreferenceFragment extends Fragment {
private View create;
private View folder;
private View verify;
private View timer;
private TextView timeLabel;
private TextView toggle;
private TextView info;
private TextView summary;
Expand All @@ -67,6 +79,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
create = view.findViewById(R.id.fragment_backup_create);
folder = view.findViewById(R.id.fragment_backup_folder);
verify = view.findViewById(R.id.fragment_backup_verify);
timer = view.findViewById(R.id.fragment_backup_time);
timeLabel = view.findViewById(R.id.fragment_backup_time_value);
toggle = view.findViewById(R.id.fragment_backup_toggle);
info = view.findViewById(R.id.fragment_backup_info);
summary = view.findViewById(R.id.fragment_backup_create_summary);
Expand All @@ -77,6 +91,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
toggle.setOnClickListener(unused -> onToggleClicked());
create.setOnClickListener(unused -> onCreateClicked());
verify.setOnClickListener(unused -> BackupDialog.showVerifyBackupPassphraseDialog(requireContext()));
timer.setOnClickListener(unused -> pickTime());

formatter.setMinimumFractionDigits(1);
formatter.setMaximumFractionDigits(1);
Expand Down Expand Up @@ -243,6 +258,25 @@ private void onCreateClickedApi29() {
LocalBackupJob.enqueue(true);
}

private void pickTime() {
int timeFormat = DateFormat.is24HourFormat(requireContext()) ? TimeFormat.CLOCK_24H : TimeFormat.CLOCK_12H;
final MaterialTimePicker timePickerFragment = new MaterialTimePicker.Builder()
.setTimeFormat(timeFormat)
.setHour(SignalStore.settings().getBackupHour())
.setMinute(SignalStore.settings().getBackupMinute())
.setTitleText("Set Backup Time")
.build();
timePickerFragment.addOnPositiveButtonClickListener(v -> {
int hour = timePickerFragment.getHour();
int minute = timePickerFragment.getMinute();
SignalStore.settings().setBackupSchedule(hour, minute);
updateTimeLabel();
TextSecurePreferences.setNextBackupTime(requireContext(), 0);
LocalBackupListener.schedule(requireContext());
});
timePickerFragment.show(getChildFragmentManager(), "TIME_PICKER");
}

private void onCreateClickedLegacy() {
Permissions.with(this)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
Expand All @@ -255,10 +289,19 @@ private void onCreateClickedLegacy() {
.execute();
}

private void updateTimeLabel() {
final int backupHour = SignalStore.settings().getBackupHour();
final int backupMinute = SignalStore.settings().getBackupMinute();
LocalTime time = LocalTime.of(backupHour, backupMinute);
timeLabel.setText(JavaTimeExtensionsKt.formatHours(time, requireContext()));
}

private void setBackupsEnabled() {
toggle.setText(R.string.BackupsPreferenceFragment__turn_off);
create.setVisibility(View.VISIBLE);
verify.setVisibility(View.VISIBLE);
timer.setVisibility(View.VISIBLE);
updateTimeLabel();
setBackupFolderName();
}

Expand All @@ -267,6 +310,7 @@ private void setBackupsDisabled() {
create.setVisibility(View.GONE);
folder.setVisibility(View.GONE);
verify.setVisibility(View.GONE);
timer.setVisibility(View.GONE);
ApplicationDependencies.getJobManager().cancelAllInQueue(LocalBackupJob.QUEUE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public static long setNextBackupTimeToIntervalFromNow(@NonNull Context context)
nextTime = System.currentTimeMillis() + INTERVAL;
} else {
LocalDateTime now = LocalDateTime.now();
LocalDateTime next = now.withHour(2).withMinute(0).withSecond(0);
int hour = SignalStore.settings().getBackupHour();
int minute = SignalStore.settings().getBackupMinute();
LocalDateTime next = now.withHour(hour).withMinute(minute).withSecond(0);
if (now.getHour() >= 2) {
next = next.plusDays(1);
}
Expand Down
31 changes: 31 additions & 0 deletions app/src/main/res/layout/fragment_backups.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,37 @@
android:textColor="@color/signal_text_secondary" />
</LinearLayout>


<LinearLayout
android:id="@+id/fragment_backup_time"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeightLarge"
android:background="?attr/selectableItemBackground"
android:gravity="center_vertical"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="@string/BackupsPreferenceFragment__backup_time"
android:textAppearance="@style/Signal.Text.Body"
android:textColor="@color/signal_text_primary" />

<TextView
android:id="@+id/fragment_backup_time_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:textAppearance="@style/Signal.Text.Preview"
android:textColor="@color/signal_text_secondary"
tools:text="3:00" />
</LinearLayout>

<LinearLayout
android:id="@+id/fragment_backup_verify"
android:layout_width="match_parent"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@
<string name="BackupsPreferenceFragment__create_backup">Create backup</string>
<string name="BackupsPreferenceFragment__last_backup">Last backup: %1$s</string>
<string name="BackupsPreferenceFragment__backup_folder">Backup folder</string>
<!-- Title for a preference item allowing the user to selected the hour of the day when their chats are backed up. -->
<string name="BackupsPreferenceFragment__backup_time">Backup time</string>
<string name="BackupsPreferenceFragment__verify_backup_passphrase">Verify backup passphrase</string>
<string name="BackupsPreferenceFragment__test_your_backup_passphrase">Test your backup passphrase and verify that it matches</string>
<string name="BackupsPreferenceFragment__turn_on">Turn on</string>
Expand Down

0 comments on commit 8dd1d3b

Please sign in to comment.