blob: bd90eef2765bae8137d8ad8207e695ad2ac91fbe [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
28import com.android.documentsui.model.DocumentInfo;
29import com.android.documentsui.model.DocumentStack;
30import com.android.documentsui.model.DurableUtils;
Daichi Hirono3b36c5a2016-01-07 15:29:12 +090031import com.android.documentsui.model.RootInfo;
Steve McKay7a3b88c2015-09-23 17:21:40 -070032
Steve McKay3eb2d072016-01-25 19:00:22 -080033import java.lang.annotation.Retention;
34import java.lang.annotation.RetentionPolicy;
Steve McKay7a3b88c2015-09-23 17:21:40 -070035import java.util.ArrayList;
36import java.util.HashMap;
37import java.util.List;
38
39public class State implements android.os.Parcelable {
Steve McKay3eb2d072016-01-25 19:00:22 -080040
Steve McKayaa15dae2016-02-09 16:17:24 -080041 private static final String TAG = "State";
42
Steve McKay3eb2d072016-01-25 19:00:22 -080043 public static final int ACTION_OPEN = 1;
44 public static final int ACTION_CREATE = 2;
45 public static final int ACTION_GET_CONTENT = 3;
46 public static final int ACTION_OPEN_TREE = 4;
47 public static final int ACTION_MANAGE = 5;
48 public static final int ACTION_BROWSE = 6;
49 public static final int ACTION_PICK_COPY_DESTINATION = 8;
50
51 @IntDef(flag = true, value = {
52 MODE_UNKNOWN,
53 MODE_LIST,
54 MODE_GRID
55 })
56 @Retention(RetentionPolicy.SOURCE)
57 public @interface ViewMode {}
58 public static final int MODE_UNKNOWN = 0;
59 public static final int MODE_LIST = 1;
60 public static final int MODE_GRID = 2;
61
62 public static final int SORT_ORDER_UNKNOWN = 0;
63 public static final int SORT_ORDER_DISPLAY_NAME = 1;
64 public static final int SORT_ORDER_LAST_MODIFIED = 2;
65 public static final int SORT_ORDER_SIZE = 3;
66
Steve McKay7a3b88c2015-09-23 17:21:40 -070067 public int action;
68 public String[] acceptMimes;
69
Steve McKay3eb2d072016-01-25 19:00:22 -080070 /** Derived from local preferences */
71 public @ViewMode int derivedMode = MODE_GRID;
Steve McKay7a3b88c2015-09-23 17:21:40 -070072
73 /** Explicit user choice */
74 public int userSortOrder = SORT_ORDER_UNKNOWN;
75 /** Derived after loader */
76 public int derivedSortOrder = SORT_ORDER_DISPLAY_NAME;
77
78 public boolean allowMultiple;
Steve McKay9f9d5b42015-09-23 15:44:24 -070079 public boolean forceSize;
Steve McKay7a3b88c2015-09-23 17:21:40 -070080 public boolean showSize;
Steve McKay9f9d5b42015-09-23 15:44:24 -070081 public boolean localOnly;
82 public boolean forceAdvanced;
83 public boolean showAdvanced;
Steve McKay9f9d5b42015-09-23 15:44:24 -070084 public boolean restored;
85 public boolean directoryCopy;
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +090086 public boolean openableOnly;
Steve McKay7a3b88c2015-09-23 17:21:40 -070087 /** Transfer mode for file copy/move operations. */
88 public int transferMode;
89
90 /** Current user navigation stack; empty implies recents. */
91 public DocumentStack stack = new DocumentStack();
Steve McKay3eb2d072016-01-25 19:00:22 -080092 private boolean mStackTouched;
Steve McKayaa15dae2016-02-09 16:17:24 -080093 private boolean mInitialRootChanged;
94 private boolean mInitialDocChanged;
Steve McKay3eb2d072016-01-25 19:00:22 -080095
Steve McKay7a3b88c2015-09-23 17:21:40 -070096 /** Currently active search, overriding any stack. */
97 public String currentSearch;
98
99 /** Instance state for every shown directory */
100 public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>();
101
102 /** Currently copying file */
103 public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<DocumentInfo>();
104
105 /** Name of the package that started DocsUI */
106 public List<String> excludedAuthorities = new ArrayList<>();
107
Steve McKay7a3b88c2015-09-23 17:21:40 -0700108 public void initAcceptMimes(Intent intent) {
109 if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) {
110 acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES);
111 } else {
112 String glob = intent.getType();
113 acceptMimes = new String[] { glob != null ? glob : "*/*" };
114 }
115 }
116
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900117 public void onRootChanged(RootInfo root) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800118 if (DEBUG) Log.d(TAG, "Root changed to: " + root);
119 if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) {
120 mInitialRootChanged = true;
121 }
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900122 stack.root = root;
123 stack.clear();
124 mStackTouched = true;
125 }
126
127 public void pushDocument(DocumentInfo info) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800128 if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info);
129 if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) {
130 mInitialDocChanged = true;
131 }
Daichi Hirono7f34b202016-01-08 12:22:09 +0900132 stack.push(info);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900133 mStackTouched = true;
134 }
135
136 public void popDocument() {
Steve McKayaa15dae2016-02-09 16:17:24 -0800137 if (DEBUG) Log.d(TAG, "Popping doc off stack.");
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900138 stack.pop();
139 mStackTouched = true;
140 }
141
142 public void setStack(DocumentStack stack) {
Steve McKayaa15dae2016-02-09 16:17:24 -0800143 if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900144 this.stack = stack;
145 mStackTouched = true;
146 }
147
148 public boolean hasLocationChanged() {
149 return mStackTouched;
150 }
151
Steve McKayaa15dae2016-02-09 16:17:24 -0800152 public boolean initialiLocationHasChanged() {
153 return mInitialRootChanged || mInitialDocChanged;
154 }
155
Steve McKay7a3b88c2015-09-23 17:21:40 -0700156 @Override
157 public int describeContents() {
158 return 0;
159 }
160
161 @Override
162 public void writeToParcel(Parcel out, int flags) {
163 out.writeInt(action);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700164 out.writeStringArray(acceptMimes);
165 out.writeInt(userSortOrder);
166 out.writeInt(allowMultiple ? 1 : 0);
167 out.writeInt(forceSize ? 1 : 0);
168 out.writeInt(showSize ? 1 : 0);
169 out.writeInt(localOnly ? 1 : 0);
170 out.writeInt(forceAdvanced ? 1 : 0);
171 out.writeInt(showAdvanced ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700172 out.writeInt(restored ? 1 : 0);
173 DurableUtils.writeToParcel(out, stack);
174 out.writeString(currentSearch);
175 out.writeMap(dirState);
176 out.writeList(selectedDocumentsForCopy);
177 out.writeList(excludedAuthorities);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900178 out.writeInt(openableOnly ? 1 : 0);
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900179 out.writeInt(mStackTouched ? 1 : 0);
Steve McKayaa15dae2016-02-09 16:17:24 -0800180 out.writeInt(mInitialRootChanged ? 1 : 0);
181 out.writeInt(mInitialDocChanged ? 1 : 0);
Steve McKay7a3b88c2015-09-23 17:21:40 -0700182 }
183
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700184 public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700185 @Override
186 public State createFromParcel(Parcel in) {
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700187 return createFromParcel(in, null);
188 }
189
190 @Override
191 public State createFromParcel(Parcel in, ClassLoader loader) {
Steve McKay7a3b88c2015-09-23 17:21:40 -0700192 final State state = new State();
193 state.action = in.readInt();
Steve McKay7a3b88c2015-09-23 17:21:40 -0700194 state.acceptMimes = in.readStringArray();
195 state.userSortOrder = in.readInt();
196 state.allowMultiple = in.readInt() != 0;
197 state.forceSize = in.readInt() != 0;
198 state.showSize = in.readInt() != 0;
199 state.localOnly = in.readInt() != 0;
200 state.forceAdvanced = in.readInt() != 0;
201 state.showAdvanced = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700202 state.restored = in.readInt() != 0;
203 DurableUtils.readFromParcel(in, state.stack);
204 state.currentSearch = in.readString();
Jeff Sharkeyba9a4b32016-02-05 17:13:16 -0700205 in.readMap(state.dirState, loader);
206 in.readList(state.selectedDocumentsForCopy, loader);
207 in.readList(state.excludedAuthorities, loader);
Tomasz Mikolajewskia8057a92015-11-16 11:41:28 +0900208 state.openableOnly = in.readInt() != 0;
Daichi Hirono3b36c5a2016-01-07 15:29:12 +0900209 state.mStackTouched = in.readInt() != 0;
Steve McKayaa15dae2016-02-09 16:17:24 -0800210 state.mInitialRootChanged = in.readInt() != 0;
211 state.mInitialDocChanged = in.readInt() != 0;
Steve McKay7a3b88c2015-09-23 17:21:40 -0700212 return state;
213 }
214
215 @Override
216 public State[] newArray(int size) {
217 return new State[size];
218 }
219 };
Ben Kwa84cebbe2015-09-25 14:48:29 -0700220}