/* * Copyright (C) 2014 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.google.android.exoplayer.util; import com.google.android.exoplayer.upstream.DataSource; import java.io.IOException; import java.net.URL; import java.util.Locale; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; /** * Miscellaneous utility functions. */ public final class Util { /** * Like {@link android.os.Build.VERSION#SDK_INT}, but in a place where it can be conveniently * overridden for local testing. */ public static final int SDK_INT = android.os.Build.VERSION.SDK_INT; private Util() {} /** * Returns true if the URL points to a file on the local device * * @param url The URL to test */ public static boolean isUrlLocalFile(URL url) { return url.getProtocol().equals("file"); } /** * Tests two objects for {@link Object#equals(Object)} equality, handling the case where one or * both may be null. * * @param o1 The first object. * @param o2 The second object. * @return {@code o1 == null ? o2 == null : o1.equals(o2)}. */ public static boolean areEqual(Object o1, Object o2) { return o1 == null ? o2 == null : o1.equals(o2); } /** * Instantiates a new single threaded executor whose thread has the specified name. * * @param threadName The name of the thread. * @return The executor. */ public static ExecutorService newSingleThreadExecutor(final String threadName) { return Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, threadName); } }); } /** * Instantiates a new single threaded scheduled executor whose thread has the specified name. * * @param threadName The name of the thread. * @return The executor. */ public static ScheduledExecutorService newSingleThreadScheduledExecutor(final String threadName) { return Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, threadName); } }); } /** * Closes a {@link DataSource}, suppressing any {@link IOException} that may occur. * * @param dataSource The {@link DataSource} to close. */ public static void closeQuietly(DataSource dataSource) { try { dataSource.close(); } catch (IOException e) { // Ignore. } } /** * Converts text to lower case using {@link Locale#US}. * * @param text The text to convert. * @return The lower case text, or null if {@code text} is null. */ public static String toLowerInvariant(String text) { return text == null ? null : text.toLowerCase(Locale.US); } }