blob: 99c2d80bdfd9975a765283a223c2e9ce7eb66e0e [file] [log] [blame]
Steve McKay58efce32015-08-20 16:19:38 +00001/*
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
17package com.android.documentsui;
18
Ben Kwa304895a2015-08-27 16:06:33 -070019import android.content.Context;
Steve McKay008e9482016-02-18 15:32:16 -080020import android.text.TextUtils;
Ben Kwad8391492015-12-17 10:37:00 -080021import android.text.format.DateUtils;
22import android.text.format.Time;
Ben Kwa304895a2015-08-27 16:06:33 -070023
Steve McKay008e9482016-02-18 15:32:16 -080024import java.text.Collator;
Steve McKay14e827a2016-01-06 18:32:13 -080025import java.util.ArrayList;
26import java.util.List;
27
Steve McKay323ee3e2015-09-25 16:02:56 -070028/** @hide */
Steve McKay58efce32015-08-20 16:19:38 +000029public final class Shared {
Steve McKay008e9482016-02-18 15:32:16 -080030
Steve McKaya1f76802016-02-25 13:34:03 -080031 public static final String TAG = "Documents";
32
33 public static final boolean DEBUG = true;
34
Ben Kwa84cebbe2015-09-25 14:48:29 -070035 /** Intent action name to pick a copy destination. */
36 public static final String ACTION_PICK_COPY_DESTINATION =
37 "com.android.documentsui.PICK_COPY_DESTINATION";
38
39 /**
40 * Extra boolean flag for {@link ACTION_PICK_COPY_DESTINATION}, which
41 * specifies if the destination directory needs to create new directory or not.
42 */
43 public static final String EXTRA_DIRECTORY_COPY = "com.android.documentsui.DIRECTORY_COPY";
Aga Wronska893390b2016-02-17 13:50:42 -080044 public static final String EXTRA_STACK = "com.android.documentsui.STACK";
45
46 /**
47 * Extra flag used to store query of type String in the bundle.
48 */
49 public static final String EXTRA_QUERY = "query";
50
51 /**
52 * Extra flag used to store state of type State in the bundle.
53 */
54 public static final String EXTRA_STATE = "state";
55
56 /**
57 * Extra flag used to store type of DirectoryFragment's type ResultType type in the bundle.
58 */
59 public static final String EXTRA_TYPE = "type";
60
61 /**
62 * Extra flag used to store root of type RootInfo in the bundle.
63 */
64 public static final String EXTRA_ROOT = "root";
65
66 /**
67 * Extra flag used to store document of DocumentInfo type in the bundle.
68 */
69 public static final String EXTRA_DOC = "document";
70
71 /**
72 * Extra flag used to store DirectoryFragment's selection of Selection type in the bundle.
73 */
74 public static final String EXTRA_SELECTION = "selection";
75
76 /**
77 * Extra flag used to store DirectoryFragment's search mode of boolean type in the bundle.
78 */
79 public static final String EXTRA_SEARCH_MODE = "searchMode";
80
81 /**
82 * Extra flag used to store DirectoryFragment's ignore state of boolean type in the bundle.
83 */
84 public static final String EXTRA_IGNORE_STATE = "ignoreState";
Ben Kwa84cebbe2015-09-25 14:48:29 -070085
Steve McKay008e9482016-02-18 15:32:16 -080086
87 /**
88 * String prefix used to indicate the document is a directory.
89 */
90 public static final char DIR_PREFIX = '\001';
91
92 private static final Collator sCollator;
93
94 static {
95 sCollator = Collator.getInstance();
96 sCollator.setStrength(Collator.SECONDARY);
97 }
98
Ben Kwa304895a2015-08-27 16:06:33 -070099 /**
100 * Generates a formatted quantity string.
101 */
102 public static final String getQuantityString(Context context, int resourceId, int quantity) {
103 return context.getResources().getQuantityString(resourceId, quantity, quantity);
104 }
Ben Kwad8391492015-12-17 10:37:00 -0800105
106 public static String formatTime(Context context, long when) {
107 // TODO: DateUtils should make this easier
108 Time then = new Time();
109 then.set(when);
110 Time now = new Time();
111 now.setToNow();
112
113 int flags = DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_NO_MIDNIGHT
114 | DateUtils.FORMAT_ABBREV_ALL;
115
116 if (then.year != now.year) {
117 flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;
118 } else if (then.yearDay != now.yearDay) {
119 flags |= DateUtils.FORMAT_SHOW_DATE;
120 } else {
121 flags |= DateUtils.FORMAT_SHOW_TIME;
122 }
123
124 return DateUtils.formatDateTime(context, when, flags);
125 }
126
Steve McKay14e827a2016-01-06 18:32:13 -0800127 /**
128 * A convenient way to transform any list into a (parcelable) ArrayList.
129 * Uses cast if possible, else creates a new list with entries from {@code list}.
130 */
131 public static <T> ArrayList<T> asArrayList(List<T> list) {
132 return list instanceof ArrayList
133 ? (ArrayList<T>) list
134 : new ArrayList<T>(list);
135 }
Steve McKay008e9482016-02-18 15:32:16 -0800136
137 /**
138 * Compare two strings against each other using system default collator in a
139 * case-insensitive mode. Clusters strings prefixed with {@link DIR_PREFIX}
140 * before other items.
141 */
142 public static int compareToIgnoreCaseNullable(String lhs, String rhs) {
143 final boolean leftEmpty = TextUtils.isEmpty(lhs);
144 final boolean rightEmpty = TextUtils.isEmpty(rhs);
145
146 if (leftEmpty && rightEmpty) return 0;
147 if (leftEmpty) return -1;
148 if (rightEmpty) return 1;
149
150 final boolean leftDir = (lhs.charAt(0) == DIR_PREFIX);
151 final boolean rightDir = (rhs.charAt(0) == DIR_PREFIX);
152
153 if (leftDir && !rightDir) return -1;
154 if (rightDir && !leftDir) return 1;
155
156 return sCollator.compare(lhs, rhs);
157 }
Steve McKay58efce32015-08-20 16:19:38 +0000158}