blob: 81a063545691f6f0905745899faac9ec0b968033 [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 McKaya6bbeab2016-02-17 15:02:01 -080033import com.android.documentsui.services.FileOperationService.OpType;
Steve McKay7a3b88c2015-09-23 17:21:40 -070034
Steve McKay3eb2d072016-01-25 19:00:22 -080035import java.lang.annotation.Retention;
36import java.lang.annotation.RetentionPolicy;
Steve McKay7a3b88c2015-09-23 17:21:40 -070037import java.util.ArrayList;
38import java.util.HashMap;
39import java.util.List;
40
41public class State implements android.os.Parcelable {
Steve McKay3eb2d072016-01-25 19:00:22 -080042
Steve McKayaa15dae2016-02-09 16:17:24 -080043 private static final String TAG = "State";
44
Steve McKay3eb2d072016-01-25 19:00:22 -080045 public static final int ACTION_OPEN = 1;
46 public static final int ACTION_CREATE = 2;
47 public static final int ACTION_GET_CONTENT = 3;
48 public static final int ACTION_OPEN_TREE = 4;
49 public static final int ACTION_MANAGE = 5;
50 public static final int ACTION_BROWSE = 6;
51 public static final int ACTION_PICK_COPY_DESTINATION = 8;
52
53 @IntDef(flag = true, value = {
54 MODE_UNKNOWN,
55 MODE_LIST,
56 MODE_GRID
57 })
58 @Retention(RetentionPolicy.SOURCE)
59 public @interface ViewMode {}
60 public static final int MODE_UNKNOWN = 0;
61 public static final int MODE_LIST = 1;
62 public static final int MODE_GRID = 2;
63
64 public static final int SORT_ORDER_UNKNOWN = 0;
65 public static final int SORT_ORDER_DISPLAY_NAME = 1;
66 public static final int SORT_ORDER_LAST_MODIFIED = 2;
67 public static final int SORT_ORDER_SIZE = 3;
68
Steve McKay7a3b88c2015-09-23 17:21:40 -070069 public int action;
70 public String[] acceptMimes;
71
Steve McKay3eb2d072016-01-25 19:00:22 -080072 /** Derived from local preferences */
73 public @ViewMode int derivedMode = MODE_GRID;
Steve McKay7a3b88c2015-09-23 17:21:40 -070074
75 /** Explicit user choice */
76 public int userSortOrder = SORT_ORDER_UNKNOWN;
77 /** Derived after loader */
78 public int derivedSortOrder = SORT_ORDER_DISPLAY_NAME;
79
80 public boolean allowMultiple;
Steve McKay9f9d5b42015-09-23 15:44:24 -070081 public boolean forceSize;
Steve McKay7a3b88c2015-09-23 17:21:40 -070082 public boolean showSize;
Steve McKay9f9d5b42015-09-23 15:44:24 -070083 public boolean localOnly;
84 public boolean forceAdvanced;
85 public boolean showAdvanced;
Steve McKay9f9d5b42015-09-23 15:44:24 -070086 public boolean restored;
Steve McKaya6bbeab2016-02-17 15:02:01 -080087
88 // Indicates that a copy operation (or move) includes a directory.
89 // Why? Directory creation isn't supported by some roots (like Downloads).
90 // This allows us to restrict available roots to just those with support.
Steve McKay9f9d5b42015-09-23 15:44:24 -070091 public boolean directoryCopy;
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +090092 public boolean openableOnly;
Steve McKaya6bbeab2016-02-17 15:02:01 -080093
94 /**
95 * This is basically a sub-type for the copy operation. It can be either COPY or MOVE.
96 * The only legal values are: OPERATION_COPY, OPERATION_MOVE.
97 */
98 public @OpType int copyOperationSubType;
Steve McKay7a3b88c2015-09-23 17:21:40 -070099
100 /** Current user navigation stack; empty implies recents. */
101 public DocumentStack stack = new DocumentStack();
Steve McKay3eb2d072016-01-25 19:00:22 -0800102 private boolean mStackTouched;
Steve McKayaa15dae2016-02-09 16:17:24 -0800103 private boolean mInitialRootChanged;
104 private boolean mInitialDocChanged;
Steve McKay3eb2d072016-01-25 19:00:22 -0800105
Steve McKay7a3b88c2015-09-23 17:21:40 -0700106 /** Currently active search, overriding any stack. */
107 public String currentSearch;
108
109 /** Instance state for every shown directory */
110 public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>();
111
Steve McKaye852d932016-02-08 19:09:42 -0800112 /** UI selection */
113 public Selection selectedDocuments = new Selection();
114
Steve McKay7a3b88c2015-09-23 17:21:40 -0700115 /** Currently copying file */
Steve McKaye852d932016-02-08 19:09:42 -0800116 public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700117
118 /** Name of the package that started DocsUI */
119 public List<String> excludedAuthorities = new ArrayList<>();
120
Steve McKay7a3b88c2015-09-23 17:21:40 -0700121 public void initAcceptMimes(Intent intent) {
122 if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) {
123 acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES);
124 } else {
125 String glob = intent.getType();
126 acceptMimes = new String[] { glob != null ? glob : "*/*" };
127 }
128 }
129
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900130 public void onRootChanged(RootInfo root) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800131 if (DEBUG) Log.d(TAG, "Root changed to: " + root);
132 if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) {
133 mInitialRootChanged = true;
134 }
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900135 stack.root = root;
136 stack.clear();
137 mStackTouched = true;
138 }
139
140 public void pushDocument(DocumentInfo info) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800141 if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info);
142 if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) {
143 mInitialDocChanged = true;
144 }
Daichi Hirono7f34b202016-01-08 12:22:09 +0900145 stack.push(info);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900146 mStackTouched = true;
147 }
148
149 public void popDocument() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800150 if (DEBUG) Log.d(TAG, "Popping doc off stack.");
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900151 stack.pop();
152 mStackTouched = true;
153 }
154
155 public void setStack(DocumentStack stack) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800156 if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900157 this.stack = stack;
158 mStackTouched = true;
159 }
160
161 public boolean hasLocationChanged() {
162 return mStackTouched;
163 }
164
Aga Wronskad4b17532016-02-12 09:15:32 -0800165 public boolean initialLocationHasChanged() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800166 return mInitialRootChanged || mInitialDocChanged;
167 }
168
Steve McKay7a3b88c2015-09-23 17:21:40 -0700169 @Override
170 public int describeContents() {
171 return 0;
172 }
173
174 @Override
175 public void writeToParcel(Parcel out, int flags) {
176 out.writeInt(action);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700177 out.writeStringArray(acceptMimes);
178 out.writeInt(userSortOrder);
179 out.writeInt(allowMultiple ? 1 : 0);
180 out.writeInt(forceSize ? 1 : 0);
181 out.writeInt(showSize ? 1 : 0);
182 out.writeInt(localOnly ? 1 : 0);
183 out.writeInt(forceAdvanced ? 1 : 0);
184 out.writeInt(showAdvanced ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700185 out.writeInt(restored ? 1 : 0);
186 DurableUtils.writeToParcel(out, stack);
187 out.writeString(currentSearch);
188 out.writeMap(dirState);
Steve McKaye852d932016-02-08 19:09:42 -0800189 out.writeParcelable(selectedDocuments, 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700190 out.writeList(selectedDocumentsForCopy);
191 out.writeList(excludedAuthorities);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900192 out.writeInt(openableOnly ? 1 : 0);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900193 out.writeInt(mStackTouched ? 1 : 0);
Steve McKayaa15dae2016-02-09 16:17:24 -0800194 out.writeInt(mInitialRootChanged ? 1 : 0);
195 out.writeInt(mInitialDocChanged ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700196 }
197
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700198 public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700199 @Override
200 public State createFromParcel(Parcel in) {
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700201 return createFromParcel(in, null);
202 }
203
204 @Override
205 public State createFromParcel(Parcel in, ClassLoader loader) {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700206 final State state = new State();
207 state.action = in.readInt();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700208 state.acceptMimes = in.readStringArray();
209 state.userSortOrder = in.readInt();
210 state.allowMultiple = in.readInt() != 0;
211 state.forceSize = in.readInt() != 0;
212 state.showSize = in.readInt() != 0;
213 state.localOnly = in.readInt() != 0;
214 state.forceAdvanced = in.readInt() != 0;
215 state.showAdvanced = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700216 state.restored = in.readInt() != 0;
217 DurableUtils.readFromParcel(in, state.stack);
218 state.currentSearch = in.readString();
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700219 in.readMap(state.dirState, loader);
Steve McKaye852d932016-02-08 19:09:42 -0800220 state.selectedDocuments = in.readParcelable(loader);
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700221 in.readList(state.selectedDocumentsForCopy, loader);
222 in.readList(state.excludedAuthorities, loader);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900223 state.openableOnly = in.readInt() != 0;
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900224 state.mStackTouched = in.readInt() != 0;
Steve McKayaa15dae2016-02-09 16:17:24 -0800225 state.mInitialRootChanged = in.readInt() != 0;
226 state.mInitialDocChanged = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700227 return state;
228 }
229
230 @Override
231 public State[] newArray(int size) {
232 return new State[size];
233 }
234 };
Ben Kwa84cebbe2015-09-25 14:48:29 -0700235}