Added option to set auto download size

This commit is contained in:
M66B 2018-11-04 14:37:52 +00:00
parent 4eb17b603e
commit cb0b074ffa
4 changed files with 80 additions and 7 deletions

View File

@ -25,7 +25,9 @@ import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.Spinner;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -42,6 +44,7 @@ public class FragmentOptions extends FragmentEx {
private SwitchCompat swSwipe;
private SwitchCompat swNav;
private SwitchCompat swInsecure;
private Spinner spDownload;
private SwitchCompat swDebug;
@Override
@ -62,6 +65,7 @@ public class FragmentOptions extends FragmentEx {
swSwipe = view.findViewById(R.id.swSwipe);
swNav = view.findViewById(R.id.swNav);
swInsecure = view.findViewById(R.id.swInsecure);
spDownload = view.findViewById(R.id.spDownload);
swDebug = view.findViewById(R.id.swDebug);
// Wire controls
@ -152,6 +156,25 @@ public class FragmentOptions extends FragmentEx {
}
});
int download = prefs.getInt("download", 32768);
final int[] values = getResources().getIntArray(R.array.downloadValues);
for (int i = 0; i < values.length; i++)
if (values[i] == download) {
spDownload.setSelection(i);
break;
}
spDownload.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
prefs.edit().putInt("download", values[position]).apply();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
prefs.edit().remove("download").apply();
}
});
swDebug.setChecked(prefs.getBoolean("debug", false));
swDebug.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override

View File

@ -133,8 +133,6 @@ public class ServiceSynchronize extends LifecycleService {
private static final int CONNECT_BACKOFF_MAX = 1024; // seconds (1024 sec ~ 17 min)
private static final int SYNC_BATCH_SIZE = 20;
private static final int DOWNLOAD_BATCH_SIZE = 20;
private static final int MESSAGE_AUTO_DOWNLOAD_SIZE = 32 * 1024; // bytes
private static final int ATTACHMENT_AUTO_DOWNLOAD_SIZE = 32 * 1024; // bytes
private static final long RECONNECT_BACKOFF = 90 * 1000L; // milliseconds
static final int PI_CLEAR = 1;
@ -1848,6 +1846,11 @@ public class ServiceSynchronize extends LifecycleService {
}
private static void downloadMessage(Context context, EntityFolder folder, IMAPFolder ifolder, IMAPMessage imessage, long id) throws MessagingException, IOException {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
long download = prefs.getInt("download", 32768);
if (download == 0)
download = Long.MAX_VALUE;
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
if (message == null)
@ -1860,13 +1863,13 @@ public class ServiceSynchronize extends LifecycleService {
boolean fetch = false;
if (!message.content)
if (!metered || (message.size != null && message.size < MESSAGE_AUTO_DOWNLOAD_SIZE))
if (!metered || (message.size != null && message.size < download))
fetch = true;
if (!fetch)
for (EntityAttachment attachment : attachments)
if (!attachment.available)
if (!metered || (attachment.size != null && attachment.size < ATTACHMENT_AUTO_DOWNLOAD_SIZE)) {
if (!metered || (attachment.size != null && attachment.size < download)) {
fetch = true;
break;
}
@ -1886,7 +1889,7 @@ public class ServiceSynchronize extends LifecycleService {
}
if (!message.content)
if (!metered || (message.size != null && message.size < MESSAGE_AUTO_DOWNLOAD_SIZE)) {
if (!metered || (message.size != null && message.size < download)) {
message.write(context, helper.getHtml());
db.message().setMessageContent(message.id, true);
Log.i(Helper.TAG, folder.name + " downloaded message id=" + message.id + " size=" + message.size);
@ -1896,7 +1899,7 @@ public class ServiceSynchronize extends LifecycleService {
for (int i = 0; i < attachments.size(); i++) {
EntityAttachment attachment = attachments.get(i);
if (!attachment.available)
if (!metered || (attachment.size != null && attachment.size < ATTACHMENT_AUTO_DOWNLOAD_SIZE)) {
if (!metered || (attachment.size != null && attachment.size < download)) {
if (iattachments == null)
iattachments = helper.getAttachments();
attachment.part = iattachments.get(i).part;

View File

@ -101,6 +101,28 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swNav" />
<TextView
android:id="@+id/tvDownload"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_advanced_download"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:textColor="?android:attr/textColorPrimary"
app:layout_constraintEnd_toStartOf="@+id/spDownload"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swInsecure" />
<Spinner
android:id="@+id/spDownload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:entries="@array/downloadNames"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvDownload"
app:layout_constraintTop_toTopOf="@id/tvDownload" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/swDebug"
android:layout_width="match_parent"
@ -108,6 +130,6 @@
android:layout_marginTop="12dp"
android:text="@string/title_advanced_debug"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/swInsecure" />
app:layout_constraintTop_toBottomOf="@id/tvDownload" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

View File

@ -90,6 +90,7 @@
<string name="title_advanced_browse">Browse messages on the server</string>
<string name="title_advanced_swipe">Swipe actions</string>
<string name="title_advanced_nav">Previous/next navigation</string>
<string name="title_advanced_download">Automatically download messages and attachments on a metered connection up to</string>
<string name="title_advanced_debug">Debug mode</string>
<string name="title_select">Select &#8230;</string>
@ -307,6 +308,30 @@
<string name="title_debug_info_remark">Please describe the problem and indicate the time of the problem:</string>
<string name="title_crash_info_remark">Please describe what you were doing when the app crashed:</string>
<string-array name="downloadNames">
<item>16 KB</item>
<item>32 KB</item>
<item>64 KB</item>
<item>128 KB</item>
<item>256 KB</item>
<item>512 KB</item>
<item>1 MB</item>
<item>2 MB</item>
<item>&#8734;</item>
</string-array>
<integer-array name="downloadValues" translatable="false">
<item>16384</item>
<item>32768</item>
<item>65536</item>
<item>131072</item>
<item>262144</item>
<item>524288</item>
<item>1048576</item>
<item>2097152</item>
<item>0</item>
</integer-array>
<array name="colorPicker">
<item>@color/red</item>
<item>@color/pink</item>