blob: 0948ab1e75718c70999eca047e7a985ea40c859b [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
Steve McKay3eb2d072016-01-25 19:00:22 -080046 public static final int ACTION_OPEN = 1;
47 public static final int ACTION_CREATE = 2;
48 public static final int ACTION_GET_CONTENT = 3;
49 public static final int ACTION_OPEN_TREE = 4;
50 public static final int ACTION_MANAGE = 5;
51 public static final int ACTION_BROWSE = 6;
52 public static final int ACTION_PICK_COPY_DESTINATION = 8;
53
54 @IntDef(flag = true, value = {
55 MODE_UNKNOWN,
56 MODE_LIST,
57 MODE_GRID
58 })
59 @Retention(RetentionPolicy.SOURCE)
60 public @interface ViewMode {}
61 public static final int MODE_UNKNOWN = 0;
62 public static final int MODE_LIST = 1;
63 public static final int MODE_GRID = 2;
64
65 public static final int SORT_ORDER_UNKNOWN = 0;
66 public static final int SORT_ORDER_DISPLAY_NAME = 1;
67 public static final int SORT_ORDER_LAST_MODIFIED = 2;
68 public static final int SORT_ORDER_SIZE = 3;
69
Steve McKay7a3b88c2015-09-23 17:21:40 -070070 public int action;
71 public String[] acceptMimes;
72
Steve McKay3eb2d072016-01-25 19:00:22 -080073 /** Derived from local preferences */
74 public @ViewMode int derivedMode = MODE_GRID;
Steve McKay7a3b88c2015-09-23 17:21:40 -070075
76 /** Explicit user choice */
77 public int userSortOrder = SORT_ORDER_UNKNOWN;
78 /** Derived after loader */
79 public int derivedSortOrder = SORT_ORDER_DISPLAY_NAME;
80
81 public boolean allowMultiple;
Steve McKay9f9d5b42015-09-23 15:44:24 -070082 public boolean forceSize;
Steve McKay7a3b88c2015-09-23 17:21:40 -070083 public boolean showSize;
Steve McKay9f9d5b42015-09-23 15:44:24 -070084 public boolean localOnly;
85 public boolean forceAdvanced;
86 public boolean showAdvanced;
Steve McKay9f9d5b42015-09-23 15:44:24 -070087 public boolean restored;
Steve McKaya6bbeab2016-02-17 15:02:01 -080088
89 // Indicates that a copy operation (or move) includes a directory.
90 // Why? Directory creation isn't supported by some roots (like Downloads).
91 // This allows us to restrict available roots to just those with support.
Steve McKay9f9d5b42015-09-23 15:44:24 -070092 public boolean directoryCopy;
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +090093 public boolean openableOnly;
Steve McKaya6bbeab2016-02-17 15:02:01 -080094
95 /**
96 * This is basically a sub-type for the copy operation. It can be either COPY or MOVE.
Steve McKayb83976072016-02-18 11:32:26 -080097 * The only legal values, if set, are: OPERATION_COPY, OPERATION_MOVE. Other pick
98 * operations don't use this. In those cases OPERATION_UNKNOWN is also legal.
Steve McKaya6bbeab2016-02-17 15:02:01 -080099 */
Steve McKayb83976072016-02-18 11:32:26 -0800100 public @OpType int copyOperationSubType = FileOperationService.OPERATION_UNKNOWN;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700101
102 /** Current user navigation stack; empty implies recents. */
103 public DocumentStack stack = new DocumentStack();
Steve McKay3eb2d072016-01-25 19:00:22 -0800104 private boolean mStackTouched;
Steve McKayaa15dae2016-02-09 16:17:24 -0800105 private boolean mInitialRootChanged;
106 private boolean mInitialDocChanged;
Steve McKay3eb2d072016-01-25 19:00:22 -0800107
Steve McKay7a3b88c2015-09-23 17:21:40 -0700108 /** Currently active search, overriding any stack. */
109 public String currentSearch;
110
111 /** Instance state for every shown directory */
112 public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>();
113
Steve McKaye852d932016-02-08 19:09:42 -0800114 /** UI selection */
115 public Selection selectedDocuments = new Selection();
116
Steve McKay7a3b88c2015-09-23 17:21:40 -0700117 /** Currently copying file */
Steve McKaye852d932016-02-08 19:09:42 -0800118 public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700119
120 /** Name of the package that started DocsUI */
121 public List<String> excludedAuthorities = new ArrayList<>();
122
Steve McKay7a3b88c2015-09-23 17:21:40 -0700123 public void initAcceptMimes(Intent intent) {
124 if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) {
125 acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES);
126 } else {
127 String glob = intent.getType();
128 acceptMimes = new String[] { glob != null ? glob : "*/*" };
129 }
130 }
131
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900132 public void onRootChanged(RootInfo root) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800133 if (DEBUG) Log.d(TAG, "Root changed to: " + root);
134 if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) {
135 mInitialRootChanged = true;
136 }
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900137 stack.root = root;
138 stack.clear();
139 mStackTouched = true;
140 }
141
142 public void pushDocument(DocumentInfo info) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800143 if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info);
144 if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) {
145 mInitialDocChanged = true;
146 }
Daichi Hirono7f34b202016-01-08 12:22:09 +0900147 stack.push(info);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900148 mStackTouched = true;
149 }
150
151 public void popDocument() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800152 if (DEBUG) Log.d(TAG, "Popping doc off stack.");
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900153 stack.pop();
154 mStackTouched = true;
155 }
156
157 public void setStack(DocumentStack stack) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800158 if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900159 this.stack = stack;
160 mStackTouched = true;
161 }
162
163 public boolean hasLocationChanged() {
164 return mStackTouched;
165 }
166
Aga Wronskad4b17532016-02-12 09:15:32 -0800167 public boolean initialLocationHasChanged() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800168 return mInitialRootChanged || mInitialDocChanged;
169 }
170
Steve McKay7a3b88c2015-09-23 17:21:40 -0700171 @Override
172 public int describeContents() {
173 return 0;
174 }
175
176 @Override
177 public void writeToParcel(Parcel out, int flags) {
178 out.writeInt(action);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700179 out.writeStringArray(acceptMimes);
180 out.writeInt(userSortOrder);
181 out.writeInt(allowMultiple ? 1 : 0);
182 out.writeInt(forceSize ? 1 : 0);
183 out.writeInt(showSize ? 1 : 0);
184 out.writeInt(localOnly ? 1 : 0);
185 out.writeInt(forceAdvanced ? 1 : 0);
186 out.writeInt(showAdvanced ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700187 out.writeInt(restored ? 1 : 0);
188 DurableUtils.writeToParcel(out, stack);
189 out.writeString(currentSearch);
190 out.writeMap(dirState);
Steve McKaye852d932016-02-08 19:09:42 -0800191 out.writeParcelable(selectedDocuments, 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700192 out.writeList(selectedDocumentsForCopy);
193 out.writeList(excludedAuthorities);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900194 out.writeInt(openableOnly ? 1 : 0);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900195 out.writeInt(mStackTouched ? 1 : 0);
Steve McKayaa15dae2016-02-09 16:17:24 -0800196 out.writeInt(mInitialRootChanged ? 1 : 0);
197 out.writeInt(mInitialDocChanged ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700198 }
199
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700200 public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700201 @Override
202 public State createFromParcel(Parcel in) {
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700203 return createFromParcel(in, null);
204 }
205
206 @Override
207 public State createFromParcel(Parcel in, ClassLoader loader) {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700208 final State state = new State();
209 state.action = in.readInt();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700210 state.acceptMimes = in.readStringArray();
211 state.userSortOrder = in.readInt();
212 state.allowMultiple = in.readInt() != 0;
213 state.forceSize = in.readInt() != 0;
214 state.showSize = in.readInt() != 0;
215 state.localOnly = in.readInt() != 0;
216 state.forceAdvanced = in.readInt() != 0;
217 state.showAdvanced = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700218 state.restored = in.readInt() != 0;
219 DurableUtils.readFromParcel(in, state.stack);
220 state.currentSearch = in.readString();
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700221 in.readMap(state.dirState, loader);
Steve McKaye852d932016-02-08 19:09:42 -0800222 state.selectedDocuments = in.readParcelable(loader);
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700223 in.readList(state.selectedDocumentsForCopy, loader);
224 in.readList(state.excludedAuthorities, loader);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900225 state.openableOnly = in.readInt() != 0;
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900226 state.mStackTouched = in.readInt() != 0;
Steve McKayaa15dae2016-02-09 16:17:24 -0800227 state.mInitialRootChanged = in.readInt() != 0;
228 state.mInitialDocChanged = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700229 return state;
230 }
231
232 @Override
233 public State[] newArray(int size) {
234 return new State[size];
235 }
236 };
Ben Kwa84cebbe2015-09-25 14:48:29 -0700237}