Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui; |
| 18 | |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 20 | import android.text.TextUtils; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 21 | import android.text.format.DateUtils; |
| 22 | import android.text.format.Time; |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 23 | |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 24 | import java.text.Collator; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 28 | /** @hide */ |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 29 | public final class Shared { |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 30 | |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 31 | /** Intent action name to pick a copy destination. */ |
| 32 | public static final String ACTION_PICK_COPY_DESTINATION = |
| 33 | "com.android.documentsui.PICK_COPY_DESTINATION"; |
| 34 | |
| 35 | /** |
| 36 | * Extra boolean flag for {@link ACTION_PICK_COPY_DESTINATION}, which |
| 37 | * specifies if the destination directory needs to create new directory or not. |
| 38 | */ |
| 39 | public static final String EXTRA_DIRECTORY_COPY = "com.android.documentsui.DIRECTORY_COPY"; |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 40 | public static final String EXTRA_STACK = "com.android.documentsui.STACK"; |
| 41 | |
| 42 | /** |
| 43 | * Extra flag used to store query of type String in the bundle. |
| 44 | */ |
| 45 | public static final String EXTRA_QUERY = "query"; |
| 46 | |
| 47 | /** |
| 48 | * Extra flag used to store state of type State in the bundle. |
| 49 | */ |
| 50 | public static final String EXTRA_STATE = "state"; |
| 51 | |
| 52 | /** |
| 53 | * Extra flag used to store type of DirectoryFragment's type ResultType type in the bundle. |
| 54 | */ |
| 55 | public static final String EXTRA_TYPE = "type"; |
| 56 | |
| 57 | /** |
| 58 | * Extra flag used to store root of type RootInfo in the bundle. |
| 59 | */ |
| 60 | public static final String EXTRA_ROOT = "root"; |
| 61 | |
| 62 | /** |
| 63 | * Extra flag used to store document of DocumentInfo type in the bundle. |
| 64 | */ |
| 65 | public static final String EXTRA_DOC = "document"; |
| 66 | |
| 67 | /** |
| 68 | * Extra flag used to store DirectoryFragment's selection of Selection type in the bundle. |
| 69 | */ |
| 70 | public static final String EXTRA_SELECTION = "selection"; |
| 71 | |
| 72 | /** |
| 73 | * Extra flag used to store DirectoryFragment's search mode of boolean type in the bundle. |
| 74 | */ |
| 75 | public static final String EXTRA_SEARCH_MODE = "searchMode"; |
| 76 | |
| 77 | /** |
| 78 | * Extra flag used to store DirectoryFragment's ignore state of boolean type in the bundle. |
| 79 | */ |
| 80 | public static final String EXTRA_IGNORE_STATE = "ignoreState"; |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 81 | |
Steve McKay | 83df8c0 | 2015-09-16 15:07:31 -0700 | [diff] [blame] | 82 | public static final boolean DEBUG = true; |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 83 | public static final String TAG = "Documents"; |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 84 | |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * String prefix used to indicate the document is a directory. |
| 88 | */ |
| 89 | public static final char DIR_PREFIX = '\001'; |
| 90 | |
| 91 | private static final Collator sCollator; |
| 92 | |
| 93 | static { |
| 94 | sCollator = Collator.getInstance(); |
| 95 | sCollator.setStrength(Collator.SECONDARY); |
| 96 | } |
| 97 | |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Generates a formatted quantity string. |
| 100 | */ |
| 101 | public static final String getQuantityString(Context context, int resourceId, int quantity) { |
| 102 | return context.getResources().getQuantityString(resourceId, quantity, quantity); |
| 103 | } |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 104 | |
| 105 | public static String formatTime(Context context, long when) { |
| 106 | // TODO: DateUtils should make this easier |
| 107 | Time then = new Time(); |
| 108 | then.set(when); |
| 109 | Time now = new Time(); |
| 110 | now.setToNow(); |
| 111 | |
| 112 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 113 | | DateUtils.FORMAT_ABBREV_ALL; |
| 114 | |
| 115 | if (then.year != now.year) { |
| 116 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 117 | } else if (then.yearDay != now.yearDay) { |
| 118 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 119 | } else { |
| 120 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 121 | } |
| 122 | |
| 123 | return DateUtils.formatDateTime(context, when, flags); |
| 124 | } |
| 125 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 126 | /** |
| 127 | * A convenient way to transform any list into a (parcelable) ArrayList. |
| 128 | * Uses cast if possible, else creates a new list with entries from {@code list}. |
| 129 | */ |
| 130 | public static <T> ArrayList<T> asArrayList(List<T> list) { |
| 131 | return list instanceof ArrayList |
| 132 | ? (ArrayList<T>) list |
| 133 | : new ArrayList<T>(list); |
| 134 | } |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Compare two strings against each other using system default collator in a |
| 138 | * case-insensitive mode. Clusters strings prefixed with {@link DIR_PREFIX} |
| 139 | * before other items. |
| 140 | */ |
| 141 | public static int compareToIgnoreCaseNullable(String lhs, String rhs) { |
| 142 | final boolean leftEmpty = TextUtils.isEmpty(lhs); |
| 143 | final boolean rightEmpty = TextUtils.isEmpty(rhs); |
| 144 | |
| 145 | if (leftEmpty && rightEmpty) return 0; |
| 146 | if (leftEmpty) return -1; |
| 147 | if (rightEmpty) return 1; |
| 148 | |
| 149 | final boolean leftDir = (lhs.charAt(0) == DIR_PREFIX); |
| 150 | final boolean rightDir = (rhs.charAt(0) == DIR_PREFIX); |
| 151 | |
| 152 | if (leftDir && !rightDir) return -1; |
| 153 | if (rightDir && !leftDir) return 1; |
| 154 | |
| 155 | return sCollator.compare(lhs, rhs); |
| 156 | } |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 157 | } |