calculate automatic timeout labels dynamically

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/ui/SettingsActivity.java  | 20 +
src/main/java/eu/siacs/conversations/utils/TimeframeUtils.java | 75 ++++
src/main/res/values/arrays.xml                                 | 11 
src/main/res/values/strings.xml                                | 28 +
src/main/res/xml/preferences.xml                               |  4 
5 files changed, 122 insertions(+), 16 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/ui/SettingsActivity.java 🔗

@@ -35,6 +35,7 @@ import eu.siacs.conversations.entities.Account;
 import eu.siacs.conversations.services.ExportLogsService;
 import eu.siacs.conversations.services.MemorizingTrustManager;
 import eu.siacs.conversations.ui.util.Color;
+import eu.siacs.conversations.utils.TimeframeUtils;
 import eu.siacs.conversations.xmpp.XmppConnection;
 import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 import eu.siacs.conversations.xmpp.jid.Jid;
@@ -109,6 +110,25 @@ public class SettingsActivity extends XmppActivity implements
 			}
 		}
 
+		ListPreference automaticMessageDeletionList = (ListPreference) mSettingsFragment.findPreference(AUTOMATIC_MESSAGE_DELETION);
+		if (automaticMessageDeletionList != null) {
+			final int[] choices = getResources().getIntArray(R.array.automatic_message_deletion_values);
+			CharSequence[] entries = new CharSequence[choices.length];
+			CharSequence[] entryValues = new CharSequence[choices.length];
+			for (int i = 0; i < choices.length; ++i) {
+				Log.d(Config.LOGTAG,"resolving choice "+choices[i]);
+				entryValues[i] = String.valueOf(choices[i]);
+				if (choices[i] == 0) {
+					entries[i] = getString(R.string.never);
+				} else {
+					entries[i] = TimeframeUtils.resolve(this, 1000L * choices[i]);
+				}
+			}
+			automaticMessageDeletionList.setEntries(entries);
+			automaticMessageDeletionList.setEntryValues(entryValues);
+		}
+
+
 		boolean removeLocation = new Intent("eu.siacs.conversations.location.request").resolveActivity(getPackageManager()) == null;
 		boolean removeVoice = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null;
 

src/main/java/eu/siacs/conversations/utils/TimeframeUtils.java 🔗

@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2018, Daniel Gultsch All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation and/or
+ * other materials provided with the distribution.
+ *
+ * 3. Neither the name of the copyright holder nor the names of its contributors
+ * may be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package eu.siacs.conversations.utils;
+
+import android.content.Context;
+import android.support.annotation.PluralsRes;
+
+import eu.siacs.conversations.R;
+
+public class TimeframeUtils {
+
+	private static final Timeframe[] TIMEFRAMES;
+
+	static {
+		TIMEFRAMES = new Timeframe[]{
+				new Timeframe(1000L, R.plurals.seconds),
+				new Timeframe(60L * 1000, R.plurals.minutes),
+				new Timeframe(60L * 60 * 1000, R.plurals.hours),
+				new Timeframe(24L * 60 * 60 * 1000, R.plurals.days),
+				new Timeframe(7L * 24 * 60 * 60 * 1000, R.plurals.weeks),
+				new Timeframe(30L * 24 * 60 * 60 * 1000, R.plurals.months),
+		};
+	}
+
+	public static String resolve(Context context, long timeframe) {
+		for(int i = TIMEFRAMES.length -1 ; i >= 0; --i) {
+			long duration = TIMEFRAMES[i].duration;
+			long threshold = i > 0 ? (TIMEFRAMES[i-1].duration / 2) : 0;
+			if (timeframe >= duration - threshold) {
+				int count = (int) (timeframe / duration + ((timeframe%duration)>(duration/2)?1:0));
+				return context.getResources().getQuantityString(TIMEFRAMES[i].name,count,count);
+			}
+		}
+		return context.getResources().getQuantityString(TIMEFRAMES[0].name,0,0);
+	}
+
+
+	private static class Timeframe {
+		public final long duration;
+		public final int name;
+
+		private Timeframe(long duration, @PluralsRes int name) {
+			this.duration = duration;
+			this.name = name;
+		}
+	}
+
+}

src/main/res/values/arrays.xml 🔗

@@ -98,18 +98,11 @@
 		<item>610</item>
 		<item>2584</item>
 	</string-array>
-	<string-array name="automatic_message_deletion_values">
+	<integer-array name="automatic_message_deletion_values">
 		<item>0</item>
 		<item>86400</item>
 		<item>604800</item>
 		<item>2592000</item>
 		<item>15811200</item>
-	</string-array>
-	<string-array name="automatic_message_deletion">
-		<item>@string/never</item>
-		<item>@string/timeout_24_hours</item>
-		<item>@string/timeout_7_days</item>
-		<item>@string/timeout_30_days</item>
-		<item>@string/timeout_6_months</item>
-	</string-array>
+	</integer-array>
 </resources>

src/main/res/values/strings.xml 🔗

@@ -658,10 +658,30 @@
 	<string name="hide_inactive_devices">Hide inactive</string>
 	<string name="distrust_omemo_key">Distrust device</string>
 	<string name="distrust_omemo_key_text">Are you sure you want to remove the verification for this device?\nThis device and messages coming from that device will be marked as untrusted.</string>
-	<string name="timeout_24_hours">24 hours</string>
-	<string name="timeout_7_days">7 days</string>
-	<string name="timeout_30_days">30 days</string>
-	<string name="timeout_6_months">6 months</string>
+	<plurals name="seconds">
+		<item quantity="one">%d second</item>
+		<item quantity="other">%d seconds</item>
+	</plurals>
+	<plurals name="minutes">
+		<item quantity="one">%d minute</item>
+		<item quantity="other">%d minutes</item>
+	</plurals>
+	<plurals name="hours">
+		<item quantity="one">%d hour</item>
+		<item quantity="other">%d hours</item>
+	</plurals>
+	<plurals name="days">
+		<item quantity="one">%d day</item>
+		<item quantity="other">%d days</item>
+	</plurals>
+	<plurals name="weeks">
+		<item quantity="one">%d week</item>
+		<item quantity="other">%d weeks</item>
+	</plurals>
+	<plurals name="months">
+		<item quantity="one">%d month</item>
+		<item quantity="other">%d months</item>
+	</plurals>
 	<string name="pref_automatically_delete_messages">Automatic message deletion</string>
 	<string name="pref_automatically_delete_messages_description">Automatically delete messages from this device that are older than the configured time frame.</string>
 	<string name="encrypting_message">Encrypting message</string>

src/main/res/xml/preferences.xml 🔗

@@ -192,9 +192,7 @@
                     android:key="automatic_message_deletion"
                     android:title="@string/pref_automatically_delete_messages"
                     android:summary="@string/pref_automatically_delete_messages_description"
-                    android:defaultValue="@integer/automatic_message_deletion"
-                    android:entries="@array/automatic_message_deletion"
-                    android:entryValues="@array/automatic_message_deletion_values" />
+                    android:defaultValue="@integer/automatic_message_deletion"/>
                 <CheckBoxPreference
                     android:defaultValue="@bool/dont_trust_system_cas"
                     android:key="dont_trust_system_cas"