blob: a288fe8f30ab6057c1729e13446e537363402d68 [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
Ben Kwa84cebbe2015-09-25 14:48:29 -070031 /** 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 Wronska893390b2016-02-17 13:50:42 -080040 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 Kwa84cebbe2015-09-25 14:48:29 -070081
Steve McKay83df8c02015-09-16 15:07:31 -070082 public static final boolean DEBUG = true;
Steve McKay58efce32015-08-20 16:19:38 +000083 public static final String TAG = "Documents";
Ben Kwa304895a2015-08-27 16:06:33 -070084
Steve McKay008e9482016-02-18 15:32:16 -080085
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 Kwa304895a2015-08-27 16:06:33 -070098 /**
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 Kwad8391492015-12-17 10:37:00 -0800104
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 McKay14e827a2016-01-06 18:32:13 -0800126 /**
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 McKay008e9482016-02-18 15:32:16 -0800135
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 McKay58efce32015-08-20 16:19:38 +0000157}