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; |
Aga Wronska | 02c532f | 2016-03-02 16:00:22 -0800 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 21 | import android.text.TextUtils; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 22 | import android.text.format.DateUtils; |
| 23 | import android.text.format.Time; |
Aga Wronska | 02c532f | 2016-03-02 16:00:22 -0800 | [diff] [blame] | 24 | import android.view.WindowManager; |
Aga Wronska | 973168c | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 25 | |
| 26 | import com.android.documentsui.State.ActionType; |
| 27 | |
| 28 | import static com.android.documentsui.State.ACTION_OPEN_TREE; |
| 29 | |
Aga Wronska | 02c532f | 2016-03-02 16:00:22 -0800 | [diff] [blame] | 30 | import android.app.AlertDialog; |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 31 | |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 32 | import java.text.Collator; |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | |
Steve McKay | 323ee3e | 2015-09-25 16:02:56 -0700 | [diff] [blame] | 36 | /** @hide */ |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 37 | public final class Shared { |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 38 | |
Steve McKay | a1f7680 | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 39 | public static final String TAG = "Documents"; |
| 40 | |
Ben Kwa | ffa829f | 2016-03-22 11:11:46 -0700 | [diff] [blame] | 41 | public static final boolean DEBUG = false; |
Steve McKay | a1f7680 | 2016-02-25 13:34:03 -0800 | [diff] [blame] | 42 | |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 43 | /** Intent action name to pick a copy destination. */ |
| 44 | public static final String ACTION_PICK_COPY_DESTINATION = |
| 45 | "com.android.documentsui.PICK_COPY_DESTINATION"; |
| 46 | |
| 47 | /** |
| 48 | * Extra boolean flag for {@link ACTION_PICK_COPY_DESTINATION}, which |
| 49 | * specifies if the destination directory needs to create new directory or not. |
| 50 | */ |
| 51 | public static final String EXTRA_DIRECTORY_COPY = "com.android.documentsui.DIRECTORY_COPY"; |
Aga Wronska | 893390b | 2016-02-17 13:50:42 -0800 | [diff] [blame] | 52 | public static final String EXTRA_STACK = "com.android.documentsui.STACK"; |
| 53 | |
| 54 | /** |
| 55 | * Extra flag used to store query of type String in the bundle. |
| 56 | */ |
| 57 | public static final String EXTRA_QUERY = "query"; |
| 58 | |
| 59 | /** |
| 60 | * Extra flag used to store state of type State in the bundle. |
| 61 | */ |
| 62 | public static final String EXTRA_STATE = "state"; |
| 63 | |
| 64 | /** |
| 65 | * Extra flag used to store type of DirectoryFragment's type ResultType type in the bundle. |
| 66 | */ |
| 67 | public static final String EXTRA_TYPE = "type"; |
| 68 | |
| 69 | /** |
| 70 | * Extra flag used to store root of type RootInfo in the bundle. |
| 71 | */ |
| 72 | public static final String EXTRA_ROOT = "root"; |
| 73 | |
| 74 | /** |
| 75 | * Extra flag used to store document of DocumentInfo type in the bundle. |
| 76 | */ |
| 77 | public static final String EXTRA_DOC = "document"; |
| 78 | |
| 79 | /** |
| 80 | * Extra flag used to store DirectoryFragment's selection of Selection type in the bundle. |
| 81 | */ |
| 82 | public static final String EXTRA_SELECTION = "selection"; |
| 83 | |
| 84 | /** |
| 85 | * Extra flag used to store DirectoryFragment's search mode of boolean type in the bundle. |
| 86 | */ |
| 87 | public static final String EXTRA_SEARCH_MODE = "searchMode"; |
| 88 | |
| 89 | /** |
| 90 | * Extra flag used to store DirectoryFragment's ignore state of boolean type in the bundle. |
| 91 | */ |
| 92 | public static final String EXTRA_IGNORE_STATE = "ignoreState"; |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 93 | |
Tomasz Mikolajewski | e46d7f9 | 2016-03-15 17:41:31 +0900 | [diff] [blame] | 94 | /** |
| 95 | * Extra for an Intent for enabling performance benchmark. Used only by tests. |
| 96 | */ |
| 97 | public static final String EXTRA_BENCHMARK = "com.android.documentsui.benchmark"; |
| 98 | |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 99 | private static final Collator sCollator; |
| 100 | |
| 101 | static { |
| 102 | sCollator = Collator.getInstance(); |
| 103 | sCollator.setStrength(Collator.SECONDARY); |
| 104 | } |
| 105 | |
Ben Kwa | 304895a | 2015-08-27 16:06:33 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Generates a formatted quantity string. |
| 108 | */ |
| 109 | public static final String getQuantityString(Context context, int resourceId, int quantity) { |
| 110 | return context.getResources().getQuantityString(resourceId, quantity, quantity); |
| 111 | } |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 112 | |
| 113 | public static String formatTime(Context context, long when) { |
| 114 | // TODO: DateUtils should make this easier |
| 115 | Time then = new Time(); |
| 116 | then.set(when); |
| 117 | Time now = new Time(); |
| 118 | now.setToNow(); |
| 119 | |
| 120 | int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT |
| 121 | | DateUtils.FORMAT_ABBREV_ALL; |
| 122 | |
| 123 | if (then.year != now.year) { |
| 124 | flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE; |
| 125 | } else if (then.yearDay != now.yearDay) { |
| 126 | flags |= DateUtils.FORMAT_SHOW_DATE; |
| 127 | } else { |
| 128 | flags |= DateUtils.FORMAT_SHOW_TIME; |
| 129 | } |
| 130 | |
| 131 | return DateUtils.formatDateTime(context, when, flags); |
| 132 | } |
| 133 | |
Steve McKay | 14e827a | 2016-01-06 18:32:13 -0800 | [diff] [blame] | 134 | /** |
| 135 | * A convenient way to transform any list into a (parcelable) ArrayList. |
| 136 | * Uses cast if possible, else creates a new list with entries from {@code list}. |
| 137 | */ |
| 138 | public static <T> ArrayList<T> asArrayList(List<T> list) { |
| 139 | return list instanceof ArrayList |
| 140 | ? (ArrayList<T>) list |
| 141 | : new ArrayList<T>(list); |
| 142 | } |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Compare two strings against each other using system default collator in a |
Tomasz Mikolajewski | 5cd060a | 2016-03-16 11:02:06 +0900 | [diff] [blame] | 146 | * case-insensitive mode. |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 147 | */ |
| 148 | public static int compareToIgnoreCaseNullable(String lhs, String rhs) { |
| 149 | final boolean leftEmpty = TextUtils.isEmpty(lhs); |
| 150 | final boolean rightEmpty = TextUtils.isEmpty(rhs); |
| 151 | |
| 152 | if (leftEmpty && rightEmpty) return 0; |
| 153 | if (leftEmpty) return -1; |
| 154 | if (rightEmpty) return 1; |
| 155 | |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 156 | return sCollator.compare(lhs, rhs); |
| 157 | } |
Aga Wronska | 02c532f | 2016-03-02 16:00:22 -0800 | [diff] [blame] | 158 | |
Tomasz Mikolajewski | 2e4e147 | 2016-03-15 15:38:54 +0900 | [diff] [blame] | 159 | /** |
| 160 | * Compare two strings against each other using system default collator in a |
| 161 | * case-insensitive mode. |
| 162 | */ |
| 163 | public static int compareToIgnoreCase(String lhs, String rhs) { |
| 164 | return sCollator.compare(lhs, rhs); |
| 165 | } |
| 166 | |
Aga Wronska | 02c532f | 2016-03-02 16:00:22 -0800 | [diff] [blame] | 167 | public static boolean isHardwareKeyboardAvailable(Context context) { |
| 168 | return context.getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS; |
| 169 | } |
| 170 | |
| 171 | public static void ensureKeyboardPresent(Context context, AlertDialog dialog) { |
| 172 | if (!isHardwareKeyboardAvailable(context)) { |
| 173 | dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); |
| 174 | } |
| 175 | } |
| 176 | |
Aga Wronska | 4e62716 | 2016-03-22 14:18:43 -0700 | [diff] [blame] | 177 | /* |
| 178 | * Indicates if the home directory should be hidden in the roots list. |
| 179 | */ |
| 180 | public static boolean isHomeRootHidden(Context context) { |
| 181 | return context.getResources().getBoolean(R.bool.home_root_hidden); |
| 182 | } |
| 183 | |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 184 | } |