blob: f239eb45552b5ee2e290f98c42ef1d98caee4433 [file] [log] [blame]
Steve McKayf8a5e082015-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 McKaycb9eb422016-02-09 16:17:24 -080019import static com.android.documentsui.Shared.DEBUG;
20
Steve McKay7776aa52016-01-25 19:00:22 -080021import android.annotation.IntDef;
Steve McKayf8a5e082015-09-23 17:21:40 -070022import android.content.Intent;
23import android.os.Parcel;
24import android.os.Parcelable;
Steve McKaycb9eb422016-02-09 16:17:24 -080025import android.util.Log;
Steve McKayf8a5e082015-09-23 17:21:40 -070026import android.util.SparseArray;
27
Steve McKay84fa3712016-02-08 19:09:42 -080028import com.android.documentsui.dirlist.MultiSelectManager.Selection;
Steve McKayf8a5e082015-09-23 17:21:40 -070029import com.android.documentsui.model.DocumentInfo;
30import com.android.documentsui.model.DocumentStack;
31import com.android.documentsui.model.DurableUtils;
Daichi Hirono2806beb2016-01-07 15:29:12 +090032import com.android.documentsui.model.RootInfo;
Steve McKayc8bdc7f2016-02-18 11:32:26 -080033import com.android.documentsui.services.FileOperationService;
Steve McKayf1719342016-02-17 15:02:01 -080034import com.android.documentsui.services.FileOperationService.OpType;
Steve McKayf8a5e082015-09-23 17:21:40 -070035
Steve McKay7776aa52016-01-25 19:00:22 -080036import java.lang.annotation.Retention;
37import java.lang.annotation.RetentionPolicy;
Steve McKayf8a5e082015-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 McKay7776aa52016-01-25 19:00:22 -080043
Steve McKaycb9eb422016-02-09 16:17:24 -080044 private static final String TAG = "State";
45
Aga Wronska6d50bcc2016-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 McKay15b92782016-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 McKay15b92782016-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 McKay7776aa52016-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 Wronska6d50bcc2016-03-28 17:27:02 -070081 public @ActionType int action;
Steve McKayf8a5e082015-09-23 17:21:40 -070082 public String[] acceptMimes;
83
Steve McKay7776aa52016-01-25 19:00:22 -080084 /** Derived from local preferences */
85 public @ViewMode int derivedMode = MODE_GRID;
Steve McKayf8a5e082015-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 McKayf2c8b0d2015-09-23 15:44:24 -070093 public boolean forceSize;
Steve McKayf8a5e082015-09-23 17:21:40 -070094 public boolean showSize;
Steve McKayf2c8b0d2015-09-23 15:44:24 -070095 public boolean localOnly;
Aga Wronskaceb990e2016-03-30 18:07:59 -070096 public boolean showAdvancedOption;
97 public boolean showAdvanced;
Steve McKayf2c8b0d2015-09-23 15:44:24 -070098 public boolean restored;
Aga Wronskaa4cb8782016-03-22 17:15:18 -070099 /*
100 * Indicates handler was an external app, like photos.
101 */
102 public boolean external;
Steve McKayf1719342016-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 McKayf2c8b0d2015-09-23 15:44:24 -0700107 public boolean directoryCopy;
Tomasz Mikolajewski95d9c252015-11-16 11:41:28 +0900108 public boolean openableOnly;
Steve McKayf1719342016-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 McKayc8bdc7f2016-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 McKayf1719342016-02-17 15:02:01 -0800114 */
Steve McKayc8bdc7f2016-02-18 11:32:26 -0800115 public @OpType int copyOperationSubType = FileOperationService.OPERATION_UNKNOWN;
Steve McKayf8a5e082015-09-23 17:21:40 -0700116
117 /** Current user navigation stack; empty implies recents. */
118 public DocumentStack stack = new DocumentStack();
Steve McKay7776aa52016-01-25 19:00:22 -0800119 private boolean mStackTouched;
Steve McKaycb9eb422016-02-09 16:17:24 -0800120 private boolean mInitialRootChanged;
121 private boolean mInitialDocChanged;
Steve McKay7776aa52016-01-25 19:00:22 -0800122
Steve McKayf8a5e082015-09-23 17:21:40 -0700123 /** Instance state for every shown directory */
124 public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>();
125
126 /** Currently copying file */
Steve McKay84fa3712016-02-08 19:09:42 -0800127 public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>();
Steve McKayf8a5e082015-09-23 17:21:40 -0700128
129 /** Name of the package that started DocsUI */
130 public List<String> excludedAuthorities = new ArrayList<>();
131
Steve McKayf8a5e082015-09-23 17:21:40 -0700132 public void initAcceptMimes(Intent intent) {
133 if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) {
134 acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES);
135 } else {
136 String glob = intent.getType();
137 acceptMimes = new String[] { glob != null ? glob : "*/*" };
138 }
139 }
140
Daichi Hirono2806beb2016-01-07 15:29:12 +0900141 public void onRootChanged(RootInfo root) {
Steve McKaycb9eb422016-02-09 16:17:24 -0800142 if (DEBUG) Log.d(TAG, "Root changed to: " + root);
143 if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) {
144 mInitialRootChanged = true;
145 }
Daichi Hirono2806beb2016-01-07 15:29:12 +0900146 stack.root = root;
147 stack.clear();
148 mStackTouched = true;
149 }
150
151 public void pushDocument(DocumentInfo info) {
Steve McKaycb9eb422016-02-09 16:17:24 -0800152 if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info);
153 if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) {
154 mInitialDocChanged = true;
155 }
Daichi Hirono97c9c1a2016-01-08 12:22:09 +0900156 stack.push(info);
Daichi Hirono2806beb2016-01-07 15:29:12 +0900157 mStackTouched = true;
158 }
159
160 public void popDocument() {
Steve McKaycb9eb422016-02-09 16:17:24 -0800161 if (DEBUG) Log.d(TAG, "Popping doc off stack.");
Daichi Hirono2806beb2016-01-07 15:29:12 +0900162 stack.pop();
163 mStackTouched = true;
164 }
165
166 public void setStack(DocumentStack stack) {
Steve McKaycb9eb422016-02-09 16:17:24 -0800167 if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack);
Daichi Hirono2806beb2016-01-07 15:29:12 +0900168 this.stack = stack;
169 mStackTouched = true;
170 }
171
Steve McKayfd8425a2016-02-23 14:34:50 -0800172 // This will return true even when the initial location is set.
173 // To get a read on if the user has changed something, use #hasInitialLocationChanged.
Daichi Hirono2806beb2016-01-07 15:29:12 +0900174 public boolean hasLocationChanged() {
175 return mStackTouched;
176 }
177
Steve McKayfd8425a2016-02-23 14:34:50 -0800178 public boolean hasInitialLocationChanged() {
Steve McKaycb9eb422016-02-09 16:17:24 -0800179 return mInitialRootChanged || mInitialDocChanged;
180 }
181
Steve McKayf8a5e082015-09-23 17:21:40 -0700182 @Override
183 public int describeContents() {
184 return 0;
185 }
186
187 @Override
188 public void writeToParcel(Parcel out, int flags) {
189 out.writeInt(action);
Steve McKayf8a5e082015-09-23 17:21:40 -0700190 out.writeStringArray(acceptMimes);
191 out.writeInt(userSortOrder);
192 out.writeInt(allowMultiple ? 1 : 0);
193 out.writeInt(forceSize ? 1 : 0);
194 out.writeInt(showSize ? 1 : 0);
195 out.writeInt(localOnly ? 1 : 0);
Aga Wronskaceb990e2016-03-30 18:07:59 -0700196 out.writeInt(showAdvancedOption ? 1 : 0);
197 out.writeInt(showAdvanced ? 1 : 0);
Steve McKayf8a5e082015-09-23 17:21:40 -0700198 out.writeInt(restored ? 1 : 0);
Aga Wronskaa4cb8782016-03-22 17:15:18 -0700199 out.writeInt(external ? 1 : 0);
Steve McKayf8a5e082015-09-23 17:21:40 -0700200 DurableUtils.writeToParcel(out, stack);
Steve McKayf8a5e082015-09-23 17:21:40 -0700201 out.writeMap(dirState);
202 out.writeList(selectedDocumentsForCopy);
203 out.writeList(excludedAuthorities);
Tomasz Mikolajewski95d9c252015-11-16 11:41:28 +0900204 out.writeInt(openableOnly ? 1 : 0);
Daichi Hirono2806beb2016-01-07 15:29:12 +0900205 out.writeInt(mStackTouched ? 1 : 0);
Steve McKaycb9eb422016-02-09 16:17:24 -0800206 out.writeInt(mInitialRootChanged ? 1 : 0);
207 out.writeInt(mInitialDocChanged ? 1 : 0);
Steve McKayf8a5e082015-09-23 17:21:40 -0700208 }
209
Jeff Sharkey2584c822016-02-05 17:13:16 -0700210 public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() {
Steve McKayf8a5e082015-09-23 17:21:40 -0700211 @Override
212 public State createFromParcel(Parcel in) {
Jeff Sharkey2584c822016-02-05 17:13:16 -0700213 return createFromParcel(in, null);
214 }
215
216 @Override
217 public State createFromParcel(Parcel in, ClassLoader loader) {
Steve McKayf8a5e082015-09-23 17:21:40 -0700218 final State state = new State();
219 state.action = in.readInt();
Steve McKayf8a5e082015-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;
Aga Wronskaceb990e2016-03-30 18:07:59 -0700226 state.showAdvancedOption = in.readInt() != 0;
227 state.showAdvanced = in.readInt() != 0;
Steve McKayf8a5e082015-09-23 17:21:40 -0700228 state.restored = in.readInt() != 0;
Aga Wronskaa4cb8782016-03-22 17:15:18 -0700229 state.external = in.readInt() != 0;
Steve McKayf8a5e082015-09-23 17:21:40 -0700230 DurableUtils.readFromParcel(in, state.stack);
Jeff Sharkey2584c822016-02-05 17:13:16 -0700231 in.readMap(state.dirState, loader);
232 in.readList(state.selectedDocumentsForCopy, loader);
233 in.readList(state.excludedAuthorities, loader);
Tomasz Mikolajewski95d9c252015-11-16 11:41:28 +0900234 state.openableOnly = in.readInt() != 0;
Daichi Hirono2806beb2016-01-07 15:29:12 +0900235 state.mStackTouched = in.readInt() != 0;
Steve McKaycb9eb422016-02-09 16:17:24 -0800236 state.mInitialRootChanged = in.readInt() != 0;
237 state.mInitialDocChanged = in.readInt() != 0;
Steve McKayf8a5e082015-09-23 17:21:40 -0700238 return state;
239 }
240
241 @Override
242 public State[] newArray(int size) {
243 return new State[size];
244 }
245 };
Ben Kwaae967802015-09-25 14:48:29 -0700246}