externalize time passed utils

This commit is contained in:
Daniel Gultsch 2020-05-09 09:42:33 +02:00
parent 072edc5a62
commit 6daaca496b
6 changed files with 108 additions and 92 deletions

View File

@ -84,7 +84,6 @@ import eu.siacs.conversations.entities.Transferable;
import eu.siacs.conversations.entities.TransferablePlaceholder;
import eu.siacs.conversations.http.HttpDownloadConnection;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.AppRTCAudioManager;
import eu.siacs.conversations.services.MessageArchiveService;
import eu.siacs.conversations.services.QuickConversationsService;
import eu.siacs.conversations.services.XmppConnectionService;
@ -112,10 +111,9 @@ import eu.siacs.conversations.utils.GeoHelper;
import eu.siacs.conversations.utils.MessageUtils;
import eu.siacs.conversations.utils.NickValidityChecker;
import eu.siacs.conversations.utils.Patterns;
import eu.siacs.conversations.utils.PermissionUtils;
import eu.siacs.conversations.utils.QuickLoader;
import eu.siacs.conversations.utils.StylingHelper;
import eu.siacs.conversations.utils.TimeframeUtils;
import eu.siacs.conversations.utils.TimeFrameUtils;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.chatstate.ChatState;
@ -1568,7 +1566,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
if (durations[i] == -1) {
labels[i] = getString(R.string.until_further_notice);
} else {
labels[i] = TimeframeUtils.resolve(activity, 1000L * durations[i]);
labels[i] = TimeFrameUtils.resolve(activity, 1000L * durations[i]);
}
}
builder.setItems(labels, (dialog, which) -> {

View File

@ -29,6 +29,7 @@ import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.ActivityRecordingBinding;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.utils.ThemeHelper;
import eu.siacs.conversations.utils.TimeFrameUtils;
public class RecordingActivity extends Activity implements View.OnClickListener {
@ -135,7 +136,7 @@ public class RecordingActivity extends Activity implements View.OnClickListener
Log.d(Config.LOGTAG, "time out waiting for output file to be written");
}
} catch (InterruptedException e) {
Log.d(Config.LOGTAG, "interrupted while waiting for output file to be written" ,e);
Log.d(Config.LOGTAG, "interrupted while waiting for output file to be written", e);
}
runOnUiThread(() -> {
setResult(Activity.RESULT_OK, new Intent().setData(Uri.fromFile(mOutputFile)));
@ -181,14 +182,9 @@ public class RecordingActivity extends Activity implements View.OnClickListener
};
mFileObserver.startWatching();
}
@SuppressLint("SetTextI18n")
private void tick() {
long time = (mStartTime < 0) ? 0 : (SystemClock.elapsedRealtime() - mStartTime);
int minutes = (int) (time / 60000);
int seconds = (int) (time / 1000) % 60;
int milliseconds = (int) (time / 100) % 10;
this.binding.timer.setText(minutes + ":" + (seconds < 10 ? "0" + seconds : seconds) + "." + milliseconds);
this.binding.timer.setText(TimeFrameUtils.formatTimePassed(mStartTime, true));
}
@Override

View File

@ -40,7 +40,7 @@ import eu.siacs.conversations.services.MemorizingTrustManager;
import eu.siacs.conversations.services.QuickConversationsService;
import eu.siacs.conversations.ui.util.StyledAttributes;
import eu.siacs.conversations.utils.GeoHelper;
import eu.siacs.conversations.utils.TimeframeUtils;
import eu.siacs.conversations.utils.TimeFrameUtils;
import rocks.xmpp.addr.Jid;
public class SettingsActivity extends XmppActivity implements
@ -140,7 +140,7 @@ public class SettingsActivity extends XmppActivity implements
if (choices[i] == 0) {
entries[i] = getString(R.string.never);
} else {
entries[i] = TimeframeUtils.resolve(this, 1000L * choices[i]);
entries[i] = TimeFrameUtils.resolve(this, 1000L * choices[i]);
}
}
automaticMessageDeletionList.setEntries(entries);

View File

@ -73,7 +73,7 @@ import eu.siacs.conversations.utils.Emoticons;
import eu.siacs.conversations.utils.GeoHelper;
import eu.siacs.conversations.utils.MessageUtils;
import eu.siacs.conversations.utils.StylingHelper;
import eu.siacs.conversations.utils.TimeframeUtils;
import eu.siacs.conversations.utils.TimeFrameUtils;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.mam.MamReference;
import rocks.xmpp.addr.Jid;
@ -703,7 +703,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
final long duration = rtpSessionStatus.duration;
if (received) {
if (duration > 0) {
viewHolder.status_message.setText(activity.getString(R.string.incoming_call_duration, TimeframeUtils.resolve(activity, duration)));
viewHolder.status_message.setText(activity.getString(R.string.incoming_call_duration, TimeFrameUtils.resolve(activity, duration)));
} else if (rtpSessionStatus.successful) {
viewHolder.status_message.setText(R.string.incoming_call);
} else {
@ -711,7 +711,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie
}
} else {
if (duration > 0) {
viewHolder.status_message.setText(activity.getString(R.string.outgoing_call_duration, TimeframeUtils.resolve(activity, duration)));
viewHolder.status_message.setText(activity.getString(R.string.outgoing_call_duration, TimeFrameUtils.resolve(activity, duration)));
} else {
viewHolder.status_message.setText(R.string.outgoing_call);
}

View File

@ -0,0 +1,97 @@
/*
* 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.os.SystemClock;
import android.support.annotation.PluralsRes;
import java.util.Locale;
import eu.siacs.conversations.R;
public class TimeFrameUtils {
private static final TimeFrame[] TIME_FRAMES;
static {
TIME_FRAMES = 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 = TIME_FRAMES.length - 1; i >= 0; --i) {
long duration = TIME_FRAMES[i].duration;
long threshold = i > 0 ? (TIME_FRAMES[i - 1].duration / 2) : 0;
if (timeFrame >= duration - threshold) {
int count = (int) (timeFrame / duration + ((timeFrame % duration) > (duration / 2) ? 1 : 0));
return context.getResources().getQuantityString(TIME_FRAMES[i].name, count, count);
}
}
return context.getResources().getQuantityString(TIME_FRAMES[0].name, 0, 0);
}
public static String formatTimePassed(final long since, final boolean withMilliseconds) {
return formatTimePassed(since, SystemClock.elapsedRealtime(), withMilliseconds);
}
public static String formatTimePassed(final long since, final long to, final boolean withMilliseconds) {
final long passed = (since < 0) ? 0 : (to - since);
final int hours = (int) (passed / 3600000);
final int minutes = (int) (passed / 60000) % 60;
final int seconds = (int) (passed / 1000) % 60;
final int milliseconds = (int) (passed / 100) % 10;
if (hours > 0) {
return String.format(Locale.ENGLISH, "%d:%02d:%02d", hours, minutes, seconds);
} else if (withMilliseconds) {
return String.format(Locale.ENGLISH, "%d:%02d.%d", minutes, seconds, milliseconds);
} else {
return String.format(Locale.ENGLISH, "%d:%02d", minutes, seconds);
}
}
private static class TimeFrame {
final long duration;
public final int name;
private TimeFrame(long duration, @PluralsRes int name) {
this.duration = duration;
this.name = name;
}
}
}

View File

@ -1,75 +0,0 @@
/*
* 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;
}
}
}