blob: 534a483474d30dfa631ba2236d9ce3e8a5ebd755 [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;
Steve McKay9f9d5b42015-09-23 15:44:24 -070096 public boolean restored;
Aga Wronska67c76b92016-03-22 17:15:18 -070097 /*
98 * Indicates handler was an external app, like photos.
99 */
100 public boolean external;
Steve McKaya6bbeab2016-02-17 15:02:01 -0800101
102 // Indicates that a copy operation (or move) includes a directory.
103 // Why? Directory creation isn't supported by some roots (like Downloads).
104 // This allows us to restrict available roots to just those with support.
Steve McKay9f9d5b42015-09-23 15:44:24 -0700105 public boolean directoryCopy;
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900106 public boolean openableOnly;
Steve McKaya6bbeab2016-02-17 15:02:01 -0800107
108 /**
109 * This is basically a sub-type for the copy operation. It can be either COPY or MOVE.
Steve McKayb83976072016-02-18 11:32:26 -0800110 * The only legal values, if set, are: OPERATION_COPY, OPERATION_MOVE. Other pick
111 * operations don't use this. In those cases OPERATION_UNKNOWN is also legal.
Steve McKaya6bbeab2016-02-17 15:02:01 -0800112 */
Steve McKayb83976072016-02-18 11:32:26 -0800113 public @OpType int copyOperationSubType = FileOperationService.OPERATION_UNKNOWN;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700114
115 /** Current user navigation stack; empty implies recents. */
116 public DocumentStack stack = new DocumentStack();
Steve McKay3eb2d072016-01-25 19:00:22 -0800117 private boolean mStackTouched;
Steve McKayaa15dae2016-02-09 16:17:24 -0800118 private boolean mInitialRootChanged;
119 private boolean mInitialDocChanged;
Steve McKay3eb2d072016-01-25 19:00:22 -0800120
Steve McKay7a3b88c2015-09-23 17:21:40 -0700121 /** Instance state for every shown directory */
122 public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>();
123
Steve McKaye852d932016-02-08 19:09:42 -0800124 /** UI selection */
125 public Selection selectedDocuments = new Selection();
126
Steve McKay7a3b88c2015-09-23 17:21:40 -0700127 /** Currently copying file */
Steve McKaye852d932016-02-08 19:09:42 -0800128 public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700129
130 /** Name of the package that started DocsUI */
131 public List<String> excludedAuthorities = new ArrayList<>();
132
Steve McKay7a3b88c2015-09-23 17:21:40 -0700133 public void initAcceptMimes(Intent intent) {
134 if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) {
135 acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES);
136 } else {
137 String glob = intent.getType();
138 acceptMimes = new String[] { glob != null ? glob : "*/*" };
139 }
140 }
141
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900142 public void onRootChanged(RootInfo root) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800143 if (DEBUG) Log.d(TAG, "Root changed to: " + root);
144 if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) {
145 mInitialRootChanged = true;
146 }
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900147 stack.root = root;
148 stack.clear();
149 mStackTouched = true;
150 }
151
152 public void pushDocument(DocumentInfo info) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800153 if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info);
154 if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) {
155 mInitialDocChanged = true;
156 }
Daichi Hirono7f34b202016-01-08 12:22:09 +0900157 stack.push(info);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900158 mStackTouched = true;
159 }
160
161 public void popDocument() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800162 if (DEBUG) Log.d(TAG, "Popping doc off stack.");
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900163 stack.pop();
164 mStackTouched = true;
165 }
166
167 public void setStack(DocumentStack stack) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800168 if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900169 this.stack = stack;
170 mStackTouched = true;
171 }
172
Steve McKayc95d87c2016-02-23 14:34:50 -0800173 // This will return true even when the initial location is set.
174 // To get a read on if the user has changed something, use #hasInitialLocationChanged.
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900175 public boolean hasLocationChanged() {
176 return mStackTouched;
177 }
178
Steve McKayc95d87c2016-02-23 14:34:50 -0800179 public boolean hasInitialLocationChanged() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800180 return mInitialRootChanged || mInitialDocChanged;
181 }
182
Steve McKay7a3b88c2015-09-23 17:21:40 -0700183 @Override
184 public int describeContents() {
185 return 0;
186 }
187
188 @Override
189 public void writeToParcel(Parcel out, int flags) {
190 out.writeInt(action);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700191 out.writeStringArray(acceptMimes);
192 out.writeInt(userSortOrder);
193 out.writeInt(allowMultiple ? 1 : 0);
194 out.writeInt(forceSize ? 1 : 0);
195 out.writeInt(showSize ? 1 : 0);
196 out.writeInt(localOnly ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700197 out.writeInt(restored ? 1 : 0);
Aga Wronska67c76b92016-03-22 17:15:18 -0700198 out.writeInt(external ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700199 DurableUtils.writeToParcel(out, stack);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700200 out.writeMap(dirState);
Steve McKaye852d932016-02-08 19:09:42 -0800201 out.writeParcelable(selectedDocuments, 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700202 out.writeList(selectedDocumentsForCopy);
203 out.writeList(excludedAuthorities);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900204 out.writeInt(openableOnly ? 1 : 0);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900205 out.writeInt(mStackTouched ? 1 : 0);
Steve McKayaa15dae2016-02-09 16:17:24 -0800206 out.writeInt(mInitialRootChanged ? 1 : 0);
207 out.writeInt(mInitialDocChanged ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700208 }
209
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700210 public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700211 @Override
212 public State createFromParcel(Parcel in) {
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700213 return createFromParcel(in, null);
214 }
215
216 @Override
217 public State createFromParcel(Parcel in, ClassLoader loader) {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700218 final State state = new State();
219 state.action = in.readInt();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700220 state.acceptMimes = in.readStringArray();
221 state.userSortOrder = in.readInt();
222 state.allowMultiple = in.readInt() != 0;
223 state.forceSize = in.readInt() != 0;
224 state.showSize = in.readInt() != 0;
225 state.localOnly = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700226 state.restored = in.readInt() != 0;
Aga Wronska67c76b92016-03-22 17:15:18 -0700227 state.external = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700228 DurableUtils.readFromParcel(in, state.stack);
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700229 in.readMap(state.dirState, loader);
Steve McKaye852d932016-02-08 19:09:42 -0800230 state.selectedDocuments = in.readParcelable(loader);
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700231 in.readList(state.selectedDocumentsForCopy, loader);
232 in.readList(state.excludedAuthorities, loader);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900233 state.openableOnly = in.readInt() != 0;
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900234 state.mStackTouched = in.readInt() != 0;
Steve McKayaa15dae2016-02-09 16:17:24 -0800235 state.mInitialRootChanged = in.readInt() != 0;
236 state.mInitialDocChanged = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700237 return state;
238 }
239
240 @Override
241 public State[] newArray(int size) {
242 return new State[size];
243 }
244 };
Ben Kwa84cebbe2015-09-25 14:48:29 -0700245}