blob: 655359a70c241c066eacd8badb91031b4bf24888 [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;
Aga Wronska02c532f2016-03-02 16:00:22 -080020import android.content.res.Configuration;
Steve McKay008e9482016-02-18 15:32:16 -080021import android.text.TextUtils;
Ben Kwad8391492015-12-17 10:37:00 -080022import android.text.format.DateUtils;
23import android.text.format.Time;
Aga Wronska02c532f2016-03-02 16:00:22 -080024import android.view.WindowManager;
Aga Wronska973168c2016-03-28 17:27:02 -070025
26import com.android.documentsui.State.ActionType;
27
28import static com.android.documentsui.State.ACTION_OPEN_TREE;
29
Aga Wronska02c532f2016-03-02 16:00:22 -080030import android.app.AlertDialog;
Ben Kwa304895a2015-08-27 16:06:33 -070031
Steve McKay008e9482016-02-18 15:32:16 -080032import java.text.Collator;
Steve McKay14e827a2016-01-06 18:32:13 -080033import java.util.ArrayList;
34import java.util.List;
35
Steve McKay323ee3e2015-09-25 16:02:56 -070036/** @hide */
Steve McKay58efce32015-08-20 16:19:38 +000037public final class Shared {
Steve McKay008e9482016-02-18 15:32:16 -080038
Steve McKaya1f76802016-02-25 13:34:03 -080039 public static final String TAG = "Documents";
40
Ben Kwaffa829f2016-03-22 11:11:46 -070041 public static final boolean DEBUG = false;
Steve McKaya1f76802016-02-25 13:34:03 -080042
Ben Kwa84cebbe2015-09-25 14:48:29 -070043 /** 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 Wronska893390b2016-02-17 13:50:42 -080052 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 Kwa84cebbe2015-09-25 14:48:29 -070093
Tomasz Mikolajewskie46d7f92016-03-15 17:41:31 +090094 /**
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 McKay008e9482016-02-18 15:32:16 -080099 private static final Collator sCollator;
100
101 static {
102 sCollator = Collator.getInstance();
103 sCollator.setStrength(Collator.SECONDARY);
104 }
105
Ben Kwa304895a2015-08-27 16:06:33 -0700106 /**
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 Kwad8391492015-12-17 10:37:00 -0800112
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 McKay14e827a2016-01-06 18:32:13 -0800134 /**
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 McKay008e9482016-02-18 15:32:16 -0800143
144 /**
145 * Compare two strings against each other using system default collator in a
Tomasz Mikolajewski5cd060a2016-03-16 11:02:06 +0900146 * case-insensitive mode.
Steve McKay008e9482016-02-18 15:32:16 -0800147 */
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 McKay008e9482016-02-18 15:32:16 -0800156 return sCollator.compare(lhs, rhs);
157 }
Aga Wronska02c532f2016-03-02 16:00:22 -0800158
Tomasz Mikolajewski2e4e1472016-03-15 15:38:54 +0900159 /**
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 Wronska02c532f2016-03-02 16:00:22 -0800167 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 Wronska4e627162016-03-22 14:18:43 -0700177 /*
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
184 /*
185 * Indicates if the advanced roots should be hidden.
186 */
Aga Wronska973168c2016-03-28 17:27:02 -0700187 public static boolean areAdvancedRootsHidden(Context context, State state) {
188 return context.getResources().getBoolean(R.bool.advanced_roots_hidden)
189 && state.action != ACTION_OPEN_TREE;
Aga Wronska4e627162016-03-22 14:18:43 -0700190 }
191
Steve McKay58efce32015-08-20 16:19:38 +0000192}