Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
| 20 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 25 | import android.util.Log; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 26 | import android.util.SparseArray; |
| 27 | |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 28 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 29 | import com.android.documentsui.model.DocumentInfo; |
| 30 | import com.android.documentsui.model.DocumentStack; |
| 31 | import com.android.documentsui.model.DurableUtils; |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 32 | import com.android.documentsui.model.RootInfo; |
Steve McKay | b8397607 | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 33 | import com.android.documentsui.services.FileOperationService; |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 34 | import com.android.documentsui.services.FileOperationService.OpType; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 35 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 36 | import java.lang.annotation.Retention; |
| 37 | import java.lang.annotation.RetentionPolicy; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
| 39 | import java.util.HashMap; |
| 40 | import java.util.List; |
| 41 | |
| 42 | public class State implements android.os.Parcelable { |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 43 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 44 | private static final String TAG = "State"; |
| 45 | |
Aga Wronska | 973168c | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 46 | @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 McKay | 6ce903d | 2016-03-09 15:20:00 -0800 | [diff] [blame] | 56 | // 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 McKay | 6ce903d | 2016-03-09 15:20:00 -0800 | [diff] [blame] | 59 | // 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 McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 64 | |
| 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 Wronska | 973168c | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 81 | public @ActionType int action; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 82 | public String[] acceptMimes; |
| 83 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 84 | /** Derived from local preferences */ |
| 85 | public @ViewMode int derivedMode = MODE_GRID; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 86 | |
| 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 McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 93 | public boolean forceSize; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 94 | public boolean showSize; |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 95 | public boolean localOnly; |
Aga Wronska | 774cc93 | 2016-03-30 18:07:59 -0700 | [diff] [blame] | 96 | public boolean showAdvancedOption; |
| 97 | public boolean showAdvanced; |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 98 | public boolean restored; |
Aga Wronska | 67c76b9 | 2016-03-22 17:15:18 -0700 | [diff] [blame] | 99 | /* |
| 100 | * Indicates handler was an external app, like photos. |
| 101 | */ |
| 102 | public boolean external; |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 103 | |
| 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 McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 107 | public boolean directoryCopy; |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 108 | public boolean openableOnly; |
Steve McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * This is basically a sub-type for the copy operation. It can be either COPY or MOVE. |
Steve McKay | b8397607 | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 112 | * 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 McKay | a6bbeab | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 114 | */ |
Steve McKay | b8397607 | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 115 | public @OpType int copyOperationSubType = FileOperationService.OPERATION_UNKNOWN; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 116 | |
| 117 | /** Current user navigation stack; empty implies recents. */ |
| 118 | public DocumentStack stack = new DocumentStack(); |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 119 | private boolean mStackTouched; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 120 | private boolean mInitialRootChanged; |
| 121 | private boolean mInitialDocChanged; |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 122 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 123 | /** Instance state for every shown directory */ |
| 124 | public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>(); |
| 125 | |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 126 | /** UI selection */ |
| 127 | public Selection selectedDocuments = new Selection(); |
| 128 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 129 | /** Currently copying file */ |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 130 | public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>(); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 131 | |
| 132 | /** Name of the package that started DocsUI */ |
| 133 | public List<String> excludedAuthorities = new ArrayList<>(); |
| 134 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 135 | public void initAcceptMimes(Intent intent) { |
| 136 | if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) { |
| 137 | acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES); |
| 138 | } else { |
| 139 | String glob = intent.getType(); |
| 140 | acceptMimes = new String[] { glob != null ? glob : "*/*" }; |
| 141 | } |
| 142 | } |
| 143 | |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 144 | public void onRootChanged(RootInfo root) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 145 | if (DEBUG) Log.d(TAG, "Root changed to: " + root); |
| 146 | if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) { |
| 147 | mInitialRootChanged = true; |
| 148 | } |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 149 | stack.root = root; |
| 150 | stack.clear(); |
| 151 | mStackTouched = true; |
| 152 | } |
| 153 | |
| 154 | public void pushDocument(DocumentInfo info) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 155 | if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info); |
| 156 | if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) { |
| 157 | mInitialDocChanged = true; |
| 158 | } |
Daichi Hirono | 7f34b20 | 2016-01-08 12:22:09 +0900 | [diff] [blame] | 159 | stack.push(info); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 160 | mStackTouched = true; |
| 161 | } |
| 162 | |
| 163 | public void popDocument() { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 164 | if (DEBUG) Log.d(TAG, "Popping doc off stack."); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 165 | stack.pop(); |
| 166 | mStackTouched = true; |
| 167 | } |
| 168 | |
| 169 | public void setStack(DocumentStack stack) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 170 | if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 171 | this.stack = stack; |
| 172 | mStackTouched = true; |
| 173 | } |
| 174 | |
Steve McKay | c95d87c | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 175 | // This will return true even when the initial location is set. |
| 176 | // To get a read on if the user has changed something, use #hasInitialLocationChanged. |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 177 | public boolean hasLocationChanged() { |
| 178 | return mStackTouched; |
| 179 | } |
| 180 | |
Steve McKay | c95d87c | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 181 | public boolean hasInitialLocationChanged() { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 182 | return mInitialRootChanged || mInitialDocChanged; |
| 183 | } |
| 184 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public int describeContents() { |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void writeToParcel(Parcel out, int flags) { |
| 192 | out.writeInt(action); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 193 | out.writeStringArray(acceptMimes); |
| 194 | out.writeInt(userSortOrder); |
| 195 | out.writeInt(allowMultiple ? 1 : 0); |
| 196 | out.writeInt(forceSize ? 1 : 0); |
| 197 | out.writeInt(showSize ? 1 : 0); |
| 198 | out.writeInt(localOnly ? 1 : 0); |
Aga Wronska | 774cc93 | 2016-03-30 18:07:59 -0700 | [diff] [blame] | 199 | out.writeInt(showAdvancedOption ? 1 : 0); |
| 200 | out.writeInt(showAdvanced ? 1 : 0); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 201 | out.writeInt(restored ? 1 : 0); |
Aga Wronska | 67c76b9 | 2016-03-22 17:15:18 -0700 | [diff] [blame] | 202 | out.writeInt(external ? 1 : 0); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 203 | DurableUtils.writeToParcel(out, stack); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 204 | out.writeMap(dirState); |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 205 | out.writeParcelable(selectedDocuments, 0); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 206 | out.writeList(selectedDocumentsForCopy); |
| 207 | out.writeList(excludedAuthorities); |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 208 | out.writeInt(openableOnly ? 1 : 0); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 209 | out.writeInt(mStackTouched ? 1 : 0); |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 210 | out.writeInt(mInitialRootChanged ? 1 : 0); |
| 211 | out.writeInt(mInitialDocChanged ? 1 : 0); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 214 | public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() { |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 215 | @Override |
| 216 | public State createFromParcel(Parcel in) { |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 217 | return createFromParcel(in, null); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public State createFromParcel(Parcel in, ClassLoader loader) { |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 222 | final State state = new State(); |
| 223 | state.action = in.readInt(); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 224 | state.acceptMimes = in.readStringArray(); |
| 225 | state.userSortOrder = in.readInt(); |
| 226 | state.allowMultiple = in.readInt() != 0; |
| 227 | state.forceSize = in.readInt() != 0; |
| 228 | state.showSize = in.readInt() != 0; |
| 229 | state.localOnly = in.readInt() != 0; |
Aga Wronska | 774cc93 | 2016-03-30 18:07:59 -0700 | [diff] [blame] | 230 | state.showAdvancedOption = in.readInt() != 0; |
| 231 | state.showAdvanced = in.readInt() != 0; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 232 | state.restored = in.readInt() != 0; |
Aga Wronska | 67c76b9 | 2016-03-22 17:15:18 -0700 | [diff] [blame] | 233 | state.external = in.readInt() != 0; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 234 | DurableUtils.readFromParcel(in, state.stack); |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 235 | in.readMap(state.dirState, loader); |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 236 | state.selectedDocuments = in.readParcelable(loader); |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 237 | in.readList(state.selectedDocumentsForCopy, loader); |
| 238 | in.readList(state.excludedAuthorities, loader); |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 239 | state.openableOnly = in.readInt() != 0; |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 240 | state.mStackTouched = in.readInt() != 0; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 241 | state.mInitialRootChanged = in.readInt() != 0; |
| 242 | state.mInitialDocChanged = in.readInt() != 0; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 243 | return state; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public State[] newArray(int size) { |
| 248 | return new State[size]; |
| 249 | } |
| 250 | }; |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 251 | } |