Shared.java revision 55c00e7356d9f76e7378cf7c701b9a41cb7be6da
1fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay/*
2fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * Copyright (C) 2015 The Android Open Source Project
3fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay *
4fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * Licensed under the Apache License, Version 2.0 (the "License");
5fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * you may not use this file except in compliance with the License.
6fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * You may obtain a copy of the License at
7fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay *
8fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay *      http://www.apache.org/licenses/LICENSE-2.0
9fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay *
10fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * Unless required by applicable law or agreed to in writing, software
11fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * distributed under the License is distributed on an "AS IS" BASIS,
12fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * See the License for the specific language governing permissions and
14fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay * limitations under the License.
15fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay */
16fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay
17fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKaypackage com.android.documentsui;
18fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay
19919231857d2add3afe51f06aaf41663a252c3e0eBen Kwaimport android.content.Context;
2055c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKayimport android.text.TextUtils;
218e3fd7676023738c04d099f2940a635ff0699717Ben Kwaimport android.text.format.DateUtils;
228e3fd7676023738c04d099f2940a635ff0699717Ben Kwaimport android.text.format.Time;
23919231857d2add3afe51f06aaf41663a252c3e0eBen Kwa
2455c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKayimport java.text.Collator;
25c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKayimport java.util.ArrayList;
26c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKayimport java.util.List;
27c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay
284d0255f79cc92a5675d14b20f9cdf06ecb8d7109Steve McKay/** @hide */
29fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKaypublic final class Shared {
3055c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
31ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa    /** Intent action name to pick a copy destination. */
32ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa    public static final String ACTION_PICK_COPY_DESTINATION =
33ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa            "com.android.documentsui.PICK_COPY_DESTINATION";
34ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa
35ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa    /**
36ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa     * Extra boolean flag for {@link ACTION_PICK_COPY_DESTINATION}, which
37ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa     * specifies if the destination directory needs to create new directory or not.
38ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa     */
39ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa    public static final String EXTRA_DIRECTORY_COPY = "com.android.documentsui.DIRECTORY_COPY";
40ae96780a8909476dca709b88dbae5ec9a2f0632cBen Kwa
41459bc2b19ae24e3ad09b2945f21c8397177d5720Steve McKay    public static final boolean DEBUG = true;
42fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay    public static final String TAG = "Documents";
434d0255f79cc92a5675d14b20f9cdf06ecb8d7109Steve McKay    public static final String EXTRA_STACK = "com.android.documentsui.STACK";
44919231857d2add3afe51f06aaf41663a252c3e0eBen Kwa
4555c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
4655c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay    /**
4755c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay     * String prefix used to indicate the document is a directory.
4855c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay     */
4955c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay    public static final char DIR_PREFIX = '\001';
5055c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
5155c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay    private static final Collator sCollator;
5255c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
5355c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay    static {
5455c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        sCollator = Collator.getInstance();
5555c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        sCollator.setStrength(Collator.SECONDARY);
5655c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay    }
5755c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
58919231857d2add3afe51f06aaf41663a252c3e0eBen Kwa    /**
59919231857d2add3afe51f06aaf41663a252c3e0eBen Kwa     * Generates a formatted quantity string.
60919231857d2add3afe51f06aaf41663a252c3e0eBen Kwa     */
61919231857d2add3afe51f06aaf41663a252c3e0eBen Kwa    public static final String getQuantityString(Context context, int resourceId, int quantity) {
62919231857d2add3afe51f06aaf41663a252c3e0eBen Kwa        return context.getResources().getQuantityString(resourceId, quantity, quantity);
63919231857d2add3afe51f06aaf41663a252c3e0eBen Kwa    }
648e3fd7676023738c04d099f2940a635ff0699717Ben Kwa
658e3fd7676023738c04d099f2940a635ff0699717Ben Kwa    public static String formatTime(Context context, long when) {
668e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        // TODO: DateUtils should make this easier
678e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        Time then = new Time();
688e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        then.set(when);
698e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        Time now = new Time();
708e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        now.setToNow();
718e3fd7676023738c04d099f2940a635ff0699717Ben Kwa
728e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
738e3fd7676023738c04d099f2940a635ff0699717Ben Kwa                | DateUtils.FORMAT_ABBREV_ALL;
748e3fd7676023738c04d099f2940a635ff0699717Ben Kwa
758e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        if (then.year != now.year) {
768e3fd7676023738c04d099f2940a635ff0699717Ben Kwa            flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
778e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        } else if (then.yearDay != now.yearDay) {
788e3fd7676023738c04d099f2940a635ff0699717Ben Kwa            flags |= DateUtils.FORMAT_SHOW_DATE;
798e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        } else {
808e3fd7676023738c04d099f2940a635ff0699717Ben Kwa            flags |= DateUtils.FORMAT_SHOW_TIME;
818e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        }
828e3fd7676023738c04d099f2940a635ff0699717Ben Kwa
838e3fd7676023738c04d099f2940a635ff0699717Ben Kwa        return DateUtils.formatDateTime(context, when, flags);
848e3fd7676023738c04d099f2940a635ff0699717Ben Kwa    }
858e3fd7676023738c04d099f2940a635ff0699717Ben Kwa
86c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay    /**
87c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay     * A convenient way to transform any list into a (parcelable) ArrayList.
88c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay     * Uses cast if possible, else creates a new list with entries from {@code list}.
89c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay     */
90c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay    public static <T> ArrayList<T> asArrayList(List<T> list) {
91c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay        return list instanceof ArrayList
92c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay            ? (ArrayList<T>) list
93c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay            : new ArrayList<T>(list);
94c83baa0574ee9e34c0e06bda1ff08928d880ee36Steve McKay    }
9555c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
9655c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay    /**
9755c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay     * Compare two strings against each other using system default collator in a
9855c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay     * case-insensitive mode. Clusters strings prefixed with {@link DIR_PREFIX}
9955c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay     * before other items.
10055c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay     */
10155c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay    public static int compareToIgnoreCaseNullable(String lhs, String rhs) {
10255c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        final boolean leftEmpty = TextUtils.isEmpty(lhs);
10355c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        final boolean rightEmpty = TextUtils.isEmpty(rhs);
10455c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
10555c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        if (leftEmpty && rightEmpty) return 0;
10655c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        if (leftEmpty) return -1;
10755c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        if (rightEmpty) return 1;
10855c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
10955c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        final boolean leftDir = (lhs.charAt(0) == DIR_PREFIX);
11055c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        final boolean rightDir = (rhs.charAt(0) == DIR_PREFIX);
11155c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
11255c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        if (leftDir && !rightDir) return -1;
11355c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        if (rightDir && !leftDir) return 1;
11455c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay
11555c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay        return sCollator.compare(lhs, rhs);
11655c00e7356d9f76e7378cf7c701b9a41cb7be6daSteve McKay    }
117fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay}
118