blob: c7d60e3d1133f98074cf078a84881048695bcd54 [file] [log] [blame]
Steve McKay7a3b88c2015-09-23 17:21:40 -07001/*
2 * Copyright (C) 2013 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
Steve McKayaa15dae2016-02-09 16:17:24 -080019import static com.android.documentsui.Shared.DEBUG;
20
Steve McKay3eb2d072016-01-25 19:00:22 -080021import android.annotation.IntDef;
Steve McKay7a3b88c2015-09-23 17:21:40 -070022import android.content.Intent;
23import android.os.Parcel;
24import android.os.Parcelable;
Steve McKayaa15dae2016-02-09 16:17:24 -080025import android.util.Log;
Steve McKay7a3b88c2015-09-23 17:21:40 -070026import android.util.SparseArray;
27
Steve McKaye852d932016-02-08 19:09:42 -080028import com.android.documentsui.dirlist.MultiSelectManager.Selection;
Steve McKay7a3b88c2015-09-23 17:21:40 -070029import com.android.documentsui.model.DocumentInfo;
30import com.android.documentsui.model.DocumentStack;
31import com.android.documentsui.model.DurableUtils;
Daichi Hirono3b36c5a2016-01-07 15:29:12 +090032import com.android.documentsui.model.RootInfo;
Steve McKayb83976072016-02-18 11:32:26 -080033import com.android.documentsui.services.FileOperationService;
Steve McKaya6bbeab2016-02-17 15:02:01 -080034import com.android.documentsui.services.FileOperationService.OpType;
Steve McKay7a3b88c2015-09-23 17:21:40 -070035
Steve McKay3eb2d072016-01-25 19:00:22 -080036import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
Steve McKay7a3b88c2015-09-23 17:21:40 -070038import java.util.ArrayList;
39import java.util.HashMap;
40import java.util.List;
41
42public class State implements android.os.Parcelable {
Steve McKay3eb2d072016-01-25 19:00:22 -080043
Steve McKayaa15dae2016-02-09 16:17:24 -080044 private static final String TAG = "State";
45
Aga Wronska973168c2016-03-28 17:27:02 -070046 @IntDef(flag = true, value = {
47 ACTION_BROWSE,
48 ACTION_PICK_COPY_DESTINATION,
49 ACTION_OPEN,
50 ACTION_CREATE,
51 ACTION_GET_CONTENT,
52 ACTION_OPEN_TREE
53 })
54 @Retention(RetentionPolicy.SOURCE)
55 public @interface ActionType {}
Steve McKay6ce903d2016-03-09 15:20:00 -080056 // File manager and related private picking activity.
57 public static final int ACTION_BROWSE = 1;
58 public static final int ACTION_PICK_COPY_DESTINATION = 2;
Steve McKay6ce903d2016-03-09 15:20:00 -080059 // All public picking activities
60 public static final int ACTION_OPEN = 3;
61 public static final int ACTION_CREATE = 4;
62 public static final int ACTION_GET_CONTENT = 5;
63 public static final int ACTION_OPEN_TREE = 6;
Steve McKay3eb2d072016-01-25 19:00:22 -080064
65 @IntDef(flag = true, value = {
66 MODE_UNKNOWN,
67 MODE_LIST,
68 MODE_GRID
69 })
70 @Retention(RetentionPolicy.SOURCE)
71 public @interface ViewMode {}
72 public static final int MODE_UNKNOWN = 0;
73 public static final int MODE_LIST = 1;
74 public static final int MODE_GRID = 2;
75
76 public static final int SORT_ORDER_UNKNOWN = 0;
77 public static final int SORT_ORDER_DISPLAY_NAME = 1;
78 public static final int SORT_ORDER_LAST_MODIFIED = 2;
79 public static final int SORT_ORDER_SIZE = 3;
80
Aga Wronska973168c2016-03-28 17:27:02 -070081 public @ActionType int action;
Steve McKay7a3b88c2015-09-23 17:21:40 -070082 public String[] acceptMimes;
83
Steve McKay3eb2d072016-01-25 19:00:22 -080084 /** Derived from local preferences */
85 public @ViewMode int derivedMode = MODE_GRID;
Steve McKay7a3b88c2015-09-23 17:21:40 -070086
87 /** Explicit user choice */
88 public int userSortOrder = SORT_ORDER_UNKNOWN;
89 /** Derived after loader */
90 public int derivedSortOrder = SORT_ORDER_DISPLAY_NAME;
91
92 public boolean allowMultiple;
Steve McKay9f9d5b42015-09-23 15:44:24 -070093 public boolean forceSize;
Steve McKay7a3b88c2015-09-23 17:21:40 -070094 public boolean showSize;
Steve McKay9f9d5b42015-09-23 15:44:24 -070095 public boolean localOnly;
Aga Wronska774cc932016-03-30 18:07:59 -070096 public boolean showAdvancedOption;
97 public boolean showAdvanced;
Steve McKay9f9d5b42015-09-23 15:44:24 -070098 public boolean restored;
Aga Wronska67c76b92016-03-22 17:15:18 -070099 /*
100 * Indicates handler was an external app, like photos.
101 */
102 public boolean external;
Steve McKaya6bbeab2016-02-17 15:02:01 -0800103
104 // Indicates that a copy operation (or move) includes a directory.
105 // Why? Directory creation isn't supported by some roots (like Downloads).
106 // This allows us to restrict available roots to just those with support.
Steve McKay9f9d5b42015-09-23 15:44:24 -0700107 public boolean directoryCopy;
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900108 public boolean openableOnly;
Steve McKaya6bbeab2016-02-17 15:02:01 -0800109
110 /**
111 * This is basically a sub-type for the copy operation. It can be either COPY or MOVE.
Steve McKayb83976072016-02-18 11:32:26 -0800112 * The only legal values, if set, are: OPERATION_COPY, OPERATION_MOVE. Other pick
113 * operations don't use this. In those cases OPERATION_UNKNOWN is also legal.
Steve McKaya6bbeab2016-02-17 15:02:01 -0800114 */
Steve McKayb83976072016-02-18 11:32:26 -0800115 public @OpType int copyOperationSubType = FileOperationService.OPERATION_UNKNOWN;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700116
117 /** Current user navigation stack; empty implies recents. */
118 public DocumentStack stack = new DocumentStack();
Steve McKay3eb2d072016-01-25 19:00:22 -0800119 private boolean mStackTouched;
Steve McKayaa15dae2016-02-09 16:17:24 -0800120 private boolean mInitialRootChanged;
121 private boolean mInitialDocChanged;
Steve McKay3eb2d072016-01-25 19:00:22 -0800122
Steve McKay7a3b88c2015-09-23 17:21:40 -0700123 /** Instance state for every shown directory */
124 public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>();
125
Steve McKaye852d932016-02-08 19:09:42 -0800126 /** UI selection */
127 public Selection selectedDocuments = new Selection();
128
Steve McKay7a3b88c2015-09-23 17:21:40 -0700129 /** Currently copying file */
Steve McKaye852d932016-02-08 19:09:42 -0800130 public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700131
132 /** Name of the package that started DocsUI */
133 public List<String> excludedAuthorities = new ArrayList<>();
134
Steve McKay7a3b88c2015-09-23 17:21:40 -0700135 public void initAcceptMimes(Intent intent) {
136 if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) {
137 acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES);
138 } else {
139 String glob = intent.getType();
140 acceptMimes = new String[] { glob != null ? glob : "*/*" };
141 }
142 }
143
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900144 public void onRootChanged(RootInfo root) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800145 if (DEBUG) Log.d(TAG, "Root changed to: " + root);
146 if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) {
147 mInitialRootChanged = true;
148 }
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900149 stack.root = root;
150 stack.clear();
151 mStackTouched = true;
152 }
153
154 public void pushDocument(DocumentInfo info) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800155 if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info);
156 if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) {
157 mInitialDocChanged = true;
158 }
Daichi Hirono7f34b202016-01-08 12:22:09 +0900159 stack.push(info);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900160 mStackTouched = true;
161 }
162
163 public void popDocument() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800164 if (DEBUG) Log.d(TAG, "Popping doc off stack.");
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900165 stack.pop();
166 mStackTouched = true;
167 }
168
169 public void setStack(DocumentStack stack) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800170 if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900171 this.stack = stack;
172 mStackTouched = true;
173 }
174
Steve McKayc95d87c2016-02-23 14:34:50 -0800175 // This will return true even when the initial location is set.
176 // To get a read on if the user has changed something, use #hasInitialLocationChanged.
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900177 public boolean hasLocationChanged() {
178 return mStackTouched;
179 }
180
Steve McKayc95d87c2016-02-23 14:34:50 -0800181 public boolean hasInitialLocationChanged() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800182 return mInitialRootChanged || mInitialDocChanged;
183 }
184
Steve McKay7a3b88c2015-09-23 17:21:40 -0700185 @Override
186 public int describeContents() {
187 return 0;
188 }
189
190 @Override
191 public void writeToParcel(Parcel out, int flags) {
192 out.writeInt(action);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700193 out.writeStringArray(acceptMimes);
194 out.writeInt(userSortOrder);
195 out.writeInt(allowMultiple ? 1 : 0);
196 out.writeInt(forceSize ? 1 : 0);
197 out.writeInt(showSize ? 1 : 0);
198 out.writeInt(localOnly ? 1 : 0);
Aga Wronska774cc932016-03-30 18:07:59 -0700199 out.writeInt(showAdvancedOption ? 1 : 0);
200 out.writeInt(showAdvanced ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700201 out.writeInt(restored ? 1 : 0);
Aga Wronska67c76b92016-03-22 17:15:18 -0700202 out.writeInt(external ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700203 DurableUtils.writeToParcel(out, stack);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700204 out.writeMap(dirState);
Steve McKaye852d932016-02-08 19:09:42 -0800205 out.writeParcelable(selectedDocuments, 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700206 out.writeList(selectedDocumentsForCopy);
207 out.writeList(excludedAuthorities);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900208 out.writeInt(openableOnly ? 1 : 0);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900209 out.writeInt(mStackTouched ? 1 : 0);
Steve McKayaa15dae2016-02-09 16:17:24 -0800210 out.writeInt(mInitialRootChanged ? 1 : 0);
211 out.writeInt(mInitialDocChanged ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700212 }
213
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700214 public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700215 @Override
216 public State createFromParcel(Parcel in) {
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700217 return createFromParcel(in, null);
218 }
219
220 @Override
221 public State createFromParcel(Parcel in, ClassLoader loader) {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700222 final State state = new State();
223 state.action = in.readInt();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700224 state.acceptMimes = in.readStringArray();
225 state.userSortOrder = in.readInt();
226 state.allowMultiple = in.readInt() != 0;
227 state.forceSize = in.readInt() != 0;
228 state.showSize = in.readInt() != 0;
229 state.localOnly = in.readInt() != 0;
Aga Wronska774cc932016-03-30 18:07:59 -0700230 state.showAdvancedOption = in.readInt() != 0;
231 state.showAdvanced = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700232 state.restored = in.readInt() != 0;
Aga Wronska67c76b92016-03-22 17:15:18 -0700233 state.external = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700234 DurableUtils.readFromParcel(in, state.stack);
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700235 in.readMap(state.dirState, loader);
Steve McKaye852d932016-02-08 19:09:42 -0800236 state.selectedDocuments = in.readParcelable(loader);
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700237 in.readList(state.selectedDocumentsForCopy, loader);
238 in.readList(state.excludedAuthorities, loader);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900239 state.openableOnly = in.readInt() != 0;
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900240 state.mStackTouched = in.readInt() != 0;
Steve McKayaa15dae2016-02-09 16:17:24 -0800241 state.mInitialRootChanged = in.readInt() != 0;
242 state.mInitialDocChanged = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700243 return state;
244 }
245
246 @Override
247 public State[] newArray(int size) {
248 return new State[size];
249 }
250 };
Ben Kwa84cebbe2015-09-25 14:48:29 -0700251}