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

Commit 9fad6594 authored by Tony Huang's avatar Tony Huang
Browse files

Refactor some codes for unbundle (3/N)

1. Implement DateUtils.formatDuration by MeasureFormat in DocsUIi
   as a util api.
2. Copy ExitInterface.convertRationalLatLonToFloat to DocsUI
   because this function is not used by other apps or components.
   And it seems metadata bundle still cannot be quried by other
   apps, so copy these code for DocsUI used only.

Bug: 118794189
Test: atest DocumentsUITests
Change-Id: I7be622ee37648e00943e1617c419106399d6813e
parent 0a0321ac
Loading
Loading
Loading
Loading
+30 −2
Original line number Diff line number Diff line
@@ -76,8 +76,7 @@ final class MetadataUtils {
        String lonRef = exif.getString(ExifInterface.TAG_GPS_LONGITUDE_REF);

        return new float[] {
            ExifInterface.convertRationalLatLonToFloat(lat, latRef),
            ExifInterface.convertRationalLatLonToFloat(lon, lonRef)
            convertRationalLatLonToFloat(lat, latRef), convertRationalLatLonToFloat(lon, lonRef)
        };
    }

@@ -88,4 +87,33 @@ final class MetadataUtils {
                data.getFloat(Shared.METADATA_VIDEO_LONGITUTE)
        };
    }

    /** This founction is copied from {@link ExifInterface} */
    private static float convertRationalLatLonToFloat(String rationalString, String ref) {
        try {
            String [] parts = rationalString.split(",");

            String [] pair;
            pair = parts[0].split("/");
            double degrees = Double.parseDouble(pair[0].trim())
                    / Double.parseDouble(pair[1].trim());

            pair = parts[1].split("/");
            double minutes = Double.parseDouble(pair[0].trim())
                    / Double.parseDouble(pair[1].trim());

            pair = parts[2].split("/");
            double seconds = Double.parseDouble(pair[0].trim())
                    / Double.parseDouble(pair[1].trim());

            double result = degrees + (minutes / 60.0) + (seconds / 3600.0);
            if ((ref.equals("S") || ref.equals("W"))) {
                return (float) -result;
            }
            return (float) result;
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
            // Not valid
            throw new IllegalArgumentException();
        }
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -58,7 +58,6 @@ import android.system.ErrnoException;
import android.system.Int64Ref;
import android.system.Os;
import android.system.OsConstants;
import android.text.format.DateUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;

@@ -72,6 +71,7 @@ import com.android.documentsui.base.RootInfo;
import com.android.documentsui.clipping.UrisSupplier;
import com.android.documentsui.roots.ProvidersCache;
import com.android.documentsui.services.FileOperationService.OpType;
import com.android.documentsui.util.FormatUtils;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
@@ -134,7 +134,7 @@ class CopyJob extends ResolvedResourcesJob {

    Notification getProgressNotification(@StringRes int msgId) {
        mProgressTracker.update(mProgressBuilder, (remainingTime) -> service.getString(msgId,
                DateUtils.formatDuration(remainingTime)));
                FormatUtils.formatDuration(remainingTime)));
        return mProgressBuilder.build();
    }

+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui.util;

import android.icu.text.MeasureFormat;
import android.icu.text.MeasureFormat.FormatWidth;
import android.icu.util.Measure;
import android.icu.util.MeasureUnit;
import android.text.format.DateUtils;

import java.util.Locale;

/**
 * A utility class for formating different type of values to strings.
 */
public class FormatUtils {
    private FormatUtils() {}

    /**
     * Returns the given duration in a human-friendly format. For example,
     * "4 minutes" or "1 second". Returns only the largest meaningful unit of time,
     * and the result duration will round to that unit.
     * For example, 500 milliseconds round to 1 second,
     * 90000 milliseconds (90 seconds or 1.5 minutes) round to 2 minutes.
     * The returned unit of time is from seconds up to hours.
     * This founction is copied from {@link DateUtils}
     * @param millis the duration time in milliseconds.
     * @return String of the duration.
     */
    public static String formatDuration(long millis) {
        MeasureFormat formatter = MeasureFormat.getInstance(Locale.getDefault(), FormatWidth.WIDE);
        if (millis >= DateUtils.HOUR_IN_MILLIS) {
            final int hours =
                    (int) ((millis + DateUtils.HOUR_IN_MILLIS / 2) / DateUtils.HOUR_IN_MILLIS);
            return formatter.format(new Measure(hours, MeasureUnit.HOUR));
        } else if (millis >= DateUtils.MINUTE_IN_MILLIS) {
            final int minutes =
                    (int) ((millis + DateUtils.MINUTE_IN_MILLIS / 2) / DateUtils.MINUTE_IN_MILLIS);
            return formatter.format(new Measure(minutes, MeasureUnit.MINUTE));
        } else {
            final int seconds =
                    (int) ((millis + DateUtils.SECOND_IN_MILLIS / 2) / DateUtils.SECOND_IN_MILLIS);
            return formatter.format(new Measure(seconds, MeasureUnit.SECOND));
        }
    }
}
+53 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui.util;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class FormatUtilsTest {
    @Test
    public void testFormatDuration_seconds() {
        assertEquals("0 seconds", FormatUtils.formatDuration(0));
        assertEquals("0 seconds", FormatUtils.formatDuration(1));
        assertEquals("0 seconds", FormatUtils.formatDuration(499));
        assertEquals("1 second", FormatUtils.formatDuration(500));
        assertEquals("1 second", FormatUtils.formatDuration(1000));
        assertEquals("2 seconds", FormatUtils.formatDuration(1500));
    }

    @Test
    public void testFormatDuration_Minutes() {
        assertEquals("59 seconds", FormatUtils.formatDuration(59000));
        assertEquals("60 seconds", FormatUtils.formatDuration(59500));
        assertEquals("1 minute", FormatUtils.formatDuration(60000));
        assertEquals("1 minute", FormatUtils.formatDuration(65000));
        assertEquals("2 minutes", FormatUtils.formatDuration(90000));
        assertEquals("2 minutes", FormatUtils.formatDuration(120000));
    }

    @Test
    public void testFormatDuration_Hours() {
        assertEquals("59 minutes", FormatUtils.formatDuration(3540000));
        assertEquals("1 hour", FormatUtils.formatDuration(3600000));
        assertEquals("1 hour", FormatUtils.formatDuration(3660000));
        assertEquals("2 hours", FormatUtils.formatDuration(5400000));
        assertEquals("48 hours", FormatUtils.formatDuration(172800000));
    }

}