15c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng/*
25c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * Copyright (C) 2012 The Android Open Source Project
35c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng *
45c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
55c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * you may not use this file except in compliance with the License.
65c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * You may obtain a copy of the License at
75c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng *
85c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
95c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng *
105c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * Unless required by applicable law or agreed to in writing, software
115c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
125c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * See the License for the specific language governing permissions and
145c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng * limitations under the License
155c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng */
165c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Chengpackage com.android.deskclock.widget.swipeablelistview;
175c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
185c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Chengimport android.text.TextUtils;
195c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Chengimport android.util.Log;
205c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
215c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Chengimport java.util.regex.Pattern;
225c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
235c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Chengpublic class LogUtils {
245c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
255c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static final String TAG = "AlarmClock";
265c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
275c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    // "GMT" + "+" or "-" + 4 digits
285c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    private static final Pattern DATE_CLEANUP_PATTERN_WRONG_TIMEZONE =
295c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            Pattern.compile("GMT([-+]\\d{4})$");
305c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
315c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
325c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Priority constant for the println method; use LogUtils.v.
335c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
345c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static final int VERBOSE = Log.VERBOSE;
355c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
365c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
375c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Priority constant for the println method; use LogUtils.d.
385c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
395c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static final int DEBUG = Log.DEBUG;
405c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
415c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
425c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Priority constant for the println method; use LogUtils.i.
435c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
445c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static final int INFO = Log.INFO;
455c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
465c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
475c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Priority constant for the println method; use LogUtils.w.
485c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
495c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static final int WARN = Log.WARN;
505c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
515c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
525c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Priority constant for the println method; use LogUtils.e.
535c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
545c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static final int ERROR = Log.ERROR;
555c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
565c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
575c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Used to enable/disable logging that we don't want included in
585c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * production releases.
595c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
605c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    private static final int MAX_ENABLED_LOG_LEVEL = ERROR;
615c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
625c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    private static Boolean sDebugLoggingEnabledForTests = null;
635c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
645c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
655c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Enable debug logging for unit tests.
665c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
675c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    //@VisibleForTesting
685c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    static void setDebugLoggingEnabledForTests(boolean enabled) {
695c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        sDebugLoggingEnabledForTests = Boolean.valueOf(enabled);
705c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
715c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
725c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
735c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Returns true if the build configuration prevents debug logging.
745c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
755c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    //@VisibleForTesting
765c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static boolean buildPreventsDebugLogging() {
775c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return MAX_ENABLED_LOG_LEVEL > VERBOSE;
785c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
795c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
805c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
815c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Returns a boolean indicating whether debug logging is enabled.
825c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
835c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    protected static boolean isDebugLoggingEnabled(String tag) {
845c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (buildPreventsDebugLogging()) {
855c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return false;
865c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
875c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (sDebugLoggingEnabledForTests != null) {
885c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return sDebugLoggingEnabledForTests.booleanValue();
895c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
905c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return Log.isLoggable(tag, Log.DEBUG);
915c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
925c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
935c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
945c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Checks to see whether or not a log for the specified tag is loggable at the specified level.
955c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
965c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static boolean isLoggable(String tag, int level) {
975c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (MAX_ENABLED_LOG_LEVEL > level) {
985c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return false;
995c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
1005c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return Log.isLoggable(tag, level);
1015c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
1025c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
1035c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
1045c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #VERBOSE} log message.
1055c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
1065c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
1075c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
1085c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
1095c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
1105c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
1115c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
1125c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
1135c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int v(String tag, String format, Object... args) {
1145c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, VERBOSE)) {
1155c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.v(tag, String.format(format, args));
1165c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
1175c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
1185c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
1195c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
1205c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
1215c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #VERBOSE} log message.
1225c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
1235c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
1245c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tr An exception to log
1255c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
1265c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
1275c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
1285c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
1295c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
1305c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
1315c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int v(String tag, Throwable tr, String format, Object... args) {
1325c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, VERBOSE)) {
1335c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.v(tag, String.format(format, args), tr);
1345c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
1355c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
1365c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
1375c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
1385c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
1395c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #DEBUG} log message.
1405c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
1415c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
1425c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
1435c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
1445c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
1455c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
1465c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
1475c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
1485c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int d(String tag, String format, Object... args) {
1495c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, DEBUG)) {
1505c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.d(tag, String.format(format, args));
1515c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
1525c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
1535c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
1545c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
1555c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
1565c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #DEBUG} log message.
1575c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
1585c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
1595c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tr An exception to log
1605c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
1615c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
1625c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
1635c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
1645c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
1655c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
1665c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int d(String tag, Throwable tr, String format, Object... args) {
1675c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, DEBUG)) {
1685c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.d(tag, String.format(format, args), tr);
1695c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
1705c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
1715c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
1725c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
1735c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
1745c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #INFO} log message.
1755c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
1765c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
1775c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
1785c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
1795c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
1805c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
1815c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
1825c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
1835c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int i(String tag, String format, Object... args) {
1845c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, INFO)) {
1855c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.i(tag, String.format(format, args));
1865c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
1875c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
1885c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
1895c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
1905c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
1915c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #INFO} log message.
1925c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
1935c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
1945c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tr An exception to log
1955c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
1965c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
1975c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
1985c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
1995c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
2005c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
2015c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int i(String tag, Throwable tr, String format, Object... args) {
2025c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, INFO)) {
2035c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.i(tag, String.format(format, args), tr);
2045c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
2055c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
2065c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
2075c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
2085c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
2095c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #WARN} log message.
2105c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
2115c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
2125c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
2135c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
2145c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
2155c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
2165c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
2175c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
2185c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int w(String tag, String format, Object... args) {
2195c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, WARN)) {
2205c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.w(tag, String.format(format, args));
2215c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
2225c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
2235c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
2245c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
2255c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
2265c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #WARN} log message.
2275c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
2285c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
2295c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tr An exception to log
2305c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
2315c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
2325c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
2335c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
2345c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
2355c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
2365c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int w(String tag, Throwable tr, String format, Object... args) {
2375c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, WARN)) {
2385c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.w(tag, String.format(format, args), tr);
2395c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
2405c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
2415c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
2425c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
2435c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
2445c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #ERROR} log message.
2455c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
2465c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
2475c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
2485c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
2495c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
2505c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
2515c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
2525c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
2535c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int e(String tag, String format, Object... args) {
2545c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, ERROR)) {
2555c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.e(tag, String.format(format, args));
2565c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
2575c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
2585c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
2595c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
2605c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
2615c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Send a {@link #ERROR} log message.
2625c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
2635c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
2645c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tr An exception to log
2655c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
2665c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
2675c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
2685c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
2695c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
2705c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
2715c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int e(String tag, Throwable tr, String format, Object... args) {
2725c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (isLoggable(tag, ERROR)) {
2735c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return Log.e(tag, String.format(format, args), tr);
2745c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
2755c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return 0;
2765c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
2775c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
2785c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
2795c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * What a Terrible Failure: Report a condition that should never happen.
2805c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * The error will always be logged at level ASSERT with the call stack.
2815c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Depending on system configuration, a report may be added to the
2825c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * {@link android.os.DropBoxManager} and/or the process may be terminated
2835c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * immediately with an error dialog.
2845c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
2855c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
2865c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
2875c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
2885c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
2895c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
2905c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
2915c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
2925c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int wtf(String tag, String format, Object... args) {
2935c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return Log.wtf(tag, String.format(format, args), new Error());
2945c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
2955c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
2965c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
2975c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * What a Terrible Failure: Report a condition that should never happen.
2985c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * The error will always be logged at level ASSERT with the call stack.
2995c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Depending on system configuration, a report may be added to the
3005c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * {@link android.os.DropBoxManager} and/or the process may be terminated
3015c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * immediately with an error dialog.
3025c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tag Used to identify the source of a log message.  It usually identifies
3035c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *        the class or activity where the log call occurs.
3045c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param tr An exception to log
3055c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param format the format string (see {@link java.util.Formatter#format})
3065c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * @param args
3075c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            the list of arguments passed to the formatter. If there are
3085c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            more arguments than required by {@code format},
3095c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *            additional arguments are ignored.
3105c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
3115c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static int wtf(String tag, Throwable tr, String format, Object... args) {
3125c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return Log.wtf(tag, String.format(format, args), tr);
3135c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
3145c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
3155c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
3165c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    /**
3175c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * Try to make a date MIME(RFC 2822/5322)-compliant.
3185c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *
3195c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * It fixes:
3205c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     * - "Thu, 10 Dec 09 15:08:08 GMT-0700" to "Thu, 10 Dec 09 15:08:08 -0700"
3215c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *   (4 digit zone value can't be preceded by "GMT")
3225c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     *   We got a report saying eBay sends a date in this format
3235c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng     */
3245c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static String cleanUpMimeDate(String date) {
3255c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        if (TextUtils.isEmpty(date)) {
3265c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng            return date;
3275c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        }
3285c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        date = DATE_CLEANUP_PATTERN_WRONG_TIMEZONE.matcher(date).replaceFirst("$1");
3295c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return date;
3305c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
3315c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
3325c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
3335c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static String byteToHex(int b) {
3345c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return byteToHex(new StringBuilder(), b).toString();
3355c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
3365c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
3375c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    public static StringBuilder byteToHex(StringBuilder sb, int b) {
3385c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        b &= 0xFF;
3395c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        sb.append("0123456789ABCDEF".charAt(b >> 4));
3405c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        sb.append("0123456789ABCDEF".charAt(b & 0xF));
3415c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng        return sb;
3425c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng    }
3435c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng
3445c3795c271bc6160f0ae2056627e9e96ffdc335cChiao Cheng}
345