Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cb2d9bf1 authored by James.cf Lin's avatar James.cf Lin Committed by James Lin
Browse files

Use the local time instead of the timestamp which received from the network.

The PIDF data which receives from the network may contain incorrect timestamp (e.g. 1970-01-01T00:00:00).Therefore, always use the local time instead when parse the PIDF data

Bug: 192370112
Test: atest RcsUceAdapterTest PidfParserTest
Change-Id: Iebfece87d54cffb0d0e9368d27afa4ae32148b8f
parent e271cb2e
Loading
Loading
Loading
Loading
+26 −13
Original line number Diff line number Diff line
@@ -36,14 +36,13 @@ import com.android.ims.rcs.uce.presence.pidfparser.pidf.PidfConstant;
import com.android.ims.rcs.uce.presence.pidfparser.pidf.Presence;
import com.android.ims.rcs.uce.presence.pidfparser.pidf.Tuple;
import com.android.ims.rcs.uce.util.UceUtils;
import com.android.internal.annotations.VisibleForTesting;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -62,6 +61,29 @@ public class PidfParser {

    private static final Pattern PIDF_PATTERN = Pattern.compile("\t|\r|\n");

    /**
     * Testing interface used to get the timestamp.
     */
    @VisibleForTesting
    public interface TimestampProxy {
        Instant getTimestamp();
    }

    // The timestamp proxy to create the local timestamp.
    private static final TimestampProxy sLocalTimestampProxy = () -> Instant.now();

    // Override timestamp proxy for testing only.
    private static TimestampProxy sOverrideTimestampProxy;

    @VisibleForTesting
    public static void setTimestampProxy(TimestampProxy proxy) {
        sOverrideTimestampProxy = proxy;
    }

    private static TimestampProxy getTimestampProxy() {
        return (sOverrideTimestampProxy != null) ? sOverrideTimestampProxy : sLocalTimestampProxy;
    }

    /**
     * Convert the RcsContactUceCapability to the string of pidf.
     */
@@ -214,17 +236,8 @@ public class PidfParser {
            builder.setContactUri(Uri.parse(contact));
        }

        // Timestamp
        String timestamp = PidfParserUtils.getTupleTimestamp(tuple);
        if (!TextUtils.isEmpty(timestamp)) {
            try {
                Instant instant = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(
                        timestamp, Instant::from);
                builder.setTime(instant);
            } catch (DateTimeParseException e) {
                Log.w(LOG_TAG, "getRcsContactPresenceTuple: Parse timestamp failed " + e);
            }
        }
        // Use local time instead to prevent we receive the incorrect timestamp from the network.
        builder.setTime(getTimestampProxy().getTimestamp());

        // Set service description
        if (!TextUtils.isEmpty(serviceDescription)) {
+13 −9
Original line number Diff line number Diff line
@@ -47,14 +47,19 @@ import org.mockito.Mock;
@RunWith(AndroidJUnit4.class)
public class PidfParserTest extends ImsTestBase {

    // The timestamp of the PIDF
    private final Instant mPidfTimestamp = Instant.now().plusMillis(1);

    @Before
    public void setUp() throws Exception {
        super.setUp();
        PidfParser.setTimestampProxy(() -> mPidfTimestamp);
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
        PidfParser.setTimestampProxy(null);
    }

    @Test
@@ -104,7 +109,7 @@ public class PidfParserTest extends ImsTestBase {
        assertEquals("1.0", presenceTuple1.getServiceVersion());
        assertEquals(serviceDescription, presenceTuple1.getServiceDescription());
        assertEquals(Uri.parse(contact), presenceTuple1.getContactUri());
        assertEquals("2001-01-01T01:00:00Z", presenceTuple1.getTime().toString());
        assertEquals(mPidfTimestamp.toString(), presenceTuple1.getTime().toString());
        assertTrue(presenceTuple1.getServiceCapabilities().isAudioCapable());
        assertFalse(presenceTuple1.getServiceCapabilities().isVideoCapable());
    }
@@ -184,7 +189,7 @@ public class PidfParserTest extends ImsTestBase {
            assertEquals(expectedTuple.getStatus(), tuple.getStatus());
            assertEquals(expectedTuple.getServiceVersion(), tuple.getServiceVersion());
            assertEquals(expectedTuple.getServiceDescription(), tuple.getServiceDescription());
            assertEquals(expectedTuple.getTime(), tuple.getTime());
            assertEquals(mPidfTimestamp, tuple.getTime());
            assertEquals(expectedTuple.getContactUri(), tuple.getContactUri());

            ServiceCapabilities expectedCap = expectedTuple.getServiceCapabilities();
@@ -243,7 +248,7 @@ public class PidfParserTest extends ImsTestBase {
        assertEquals("1.0", presenceTuple1.getServiceVersion());
        assertEquals(serviceDescription1, presenceTuple1.getServiceDescription());
        assertEquals(Uri.parse(contact), presenceTuple1.getContactUri());
        assertEquals("2001-01-01T01:00:00Z", presenceTuple1.getTime().toString());
        assertEquals(mPidfTimestamp.toString(), presenceTuple1.getTime().toString());
        assertNull(presenceTuple1.getServiceCapabilities());

        // Verify the second tuple information
@@ -254,7 +259,7 @@ public class PidfParserTest extends ImsTestBase {
        assertFalse(presenceTuple2.getServiceCapabilities().isVideoCapable());
        assertEquals(serviceDescription2, presenceTuple2.getServiceDescription());
        assertEquals(Uri.parse(contact), presenceTuple2.getContactUri());
        assertEquals("2001-02-02T01:00:00Z", presenceTuple2.getTime().toString());
        assertEquals(mPidfTimestamp.toString(), presenceTuple2.getTime().toString());
        assertNotNull(presenceTuple2.getServiceCapabilities());
        assertEquals(isAudioSupported, presenceTuple2.getServiceCapabilities().isAudioCapable());
        assertEquals(isVideoSupported, presenceTuple2.getServiceCapabilities().isVideoCapable());
@@ -341,7 +346,7 @@ public class PidfParserTest extends ImsTestBase {
                .append("<caps:video>").append(isVideoSupported).append("</caps:video>")
                .append("</caps:servcaps>")
                .append("<contact>").append(contact).append("</contact>")
                .append("<timestamp>2001-01-01T01:00:00.00Z</timestamp>")
                .append("<timestamp>").append(mPidfTimestamp.toString()).append("</timestamp>")
                .append("</tuple></presence>");
        return pidfBuilder.toString();
    }
@@ -444,7 +449,7 @@ public class PidfParserTest extends ImsTestBase {
                + "<op:description>" + serviceDescription1 + "</op:description>"
                + "</op:service-description>"
                + "<contact>" + contact + "</contact>"
                + "<timestamp>2001-01-01T01:00:00.00Z</timestamp>"
                + "<timestamp>" + mPidfTimestamp.toString() + "</timestamp>"
                + "</tuple>"
                // tuple 2
                + "<tuple id=\"a1\">"
@@ -462,7 +467,7 @@ public class PidfParserTest extends ImsTestBase {
                + "<caps:video>" + videoSupported + "</caps:video>"
                + "</caps:servcaps>"
                + "<contact>" + contact + "</contact>"
                + "<timestamp>2001-02-02T01:00:00.00Z</timestamp>"
                + "<timestamp>" + mPidfTimestamp.toString() + "</timestamp>"
                + "</tuple>"
                + "</presence>";
    }
@@ -475,7 +480,6 @@ public class PidfParserTest extends ImsTestBase {
        final String basicStatus = RcsContactPresenceTuple.TUPLE_BASIC_STATUS_OPEN;
        final String version = "1.0";
        final String description = "description test";
        final Instant nowTime = Instant.now();

        // init the capabilities
        ServiceCapabilities.Builder servCapsBuilder =
@@ -487,7 +491,7 @@ public class PidfParserTest extends ImsTestBase {
                basicStatus, RcsContactPresenceTuple.SERVICE_ID_MMTEL, version);
        tupleBuilder.setContactUri(contact)
                .setServiceDescription(description)
                .setTime(nowTime)
                .setTime(mPidfTimestamp)
                .setServiceCapabilities(servCapsBuilder.build());

        PresenceBuilder presenceBuilder = new PresenceBuilder(contact,