Steve McKay | f8a5e08 | 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 | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
| 20 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Steve McKay | f8a5e08 | 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 | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 25 | import android.util.Log; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 26 | import android.util.SparseArray; |
| 27 | |
Steve McKay | 84fa371 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 28 | import com.android.documentsui.dirlist.MultiSelectManager.Selection; |
Steve McKay | f8a5e08 | 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 | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 32 | import com.android.documentsui.model.RootInfo; |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 33 | import com.android.documentsui.services.FileOperationService; |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 34 | import com.android.documentsui.services.FileOperationService.OpType; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 35 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 36 | import java.lang.annotation.Retention; |
| 37 | import java.lang.annotation.RetentionPolicy; |
Steve McKay | f8a5e08 | 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 | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 43 | |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 44 | private static final String TAG = "State"; |
| 45 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 46 | 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 McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 70 | public int action; |
| 71 | public String[] acceptMimes; |
| 72 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 73 | /** Derived from local preferences */ |
| 74 | public @ViewMode int derivedMode = MODE_GRID; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 75 | |
| 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 McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 82 | public boolean forceSize; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 83 | public boolean showSize; |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 84 | public boolean localOnly; |
| 85 | public boolean forceAdvanced; |
| 86 | public boolean showAdvanced; |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 87 | public boolean restored; |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 88 | |
| 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 McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 92 | public boolean directoryCopy; |
Tomasz Mikolajewski | 95d9c25 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 93 | public boolean openableOnly; |
Steve McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * This is basically a sub-type for the copy operation. It can be either COPY or MOVE. |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 97 | * 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 McKay | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 99 | */ |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 100 | public @OpType int copyOperationSubType = FileOperationService.OPERATION_UNKNOWN; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 101 | |
| 102 | /** Current user navigation stack; empty implies recents. */ |
| 103 | public DocumentStack stack = new DocumentStack(); |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 104 | private boolean mStackTouched; |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 105 | private boolean mInitialRootChanged; |
| 106 | private boolean mInitialDocChanged; |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 107 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 108 | /** Instance state for every shown directory */ |
| 109 | public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>(); |
| 110 | |
Steve McKay | 84fa371 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 111 | /** UI selection */ |
| 112 | public Selection selectedDocuments = new Selection(); |
| 113 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 114 | /** Currently copying file */ |
Steve McKay | 84fa371 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 115 | public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>(); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 116 | |
| 117 | /** Name of the package that started DocsUI */ |
| 118 | public List<String> excludedAuthorities = new ArrayList<>(); |
| 119 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 120 | public void initAcceptMimes(Intent intent) { |
| 121 | if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) { |
| 122 | acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES); |
| 123 | } else { |
| 124 | String glob = intent.getType(); |
| 125 | acceptMimes = new String[] { glob != null ? glob : "*/*" }; |
| 126 | } |
| 127 | } |
| 128 | |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 129 | public void onRootChanged(RootInfo root) { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 130 | if (DEBUG) Log.d(TAG, "Root changed to: " + root); |
| 131 | if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) { |
| 132 | mInitialRootChanged = true; |
| 133 | } |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 134 | stack.root = root; |
| 135 | stack.clear(); |
| 136 | mStackTouched = true; |
| 137 | } |
| 138 | |
| 139 | public void pushDocument(DocumentInfo info) { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 140 | if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info); |
| 141 | if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) { |
| 142 | mInitialDocChanged = true; |
| 143 | } |
Daichi Hirono | 97c9c1a | 2016-01-08 12:22:09 +0900 | [diff] [blame] | 144 | stack.push(info); |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 145 | mStackTouched = true; |
| 146 | } |
| 147 | |
| 148 | public void popDocument() { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 149 | if (DEBUG) Log.d(TAG, "Popping doc off stack."); |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 150 | stack.pop(); |
| 151 | mStackTouched = true; |
| 152 | } |
| 153 | |
| 154 | public void setStack(DocumentStack stack) { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 155 | if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack); |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 156 | this.stack = stack; |
| 157 | mStackTouched = true; |
| 158 | } |
| 159 | |
Steve McKay | fd8425a | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 160 | // This will return true even when the initial location is set. |
| 161 | // To get a read on if the user has changed something, use #hasInitialLocationChanged. |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 162 | public boolean hasLocationChanged() { |
| 163 | return mStackTouched; |
| 164 | } |
| 165 | |
Steve McKay | fd8425a | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 166 | public boolean hasInitialLocationChanged() { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 167 | return mInitialRootChanged || mInitialDocChanged; |
| 168 | } |
| 169 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public int describeContents() { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void writeToParcel(Parcel out, int flags) { |
| 177 | out.writeInt(action); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 178 | out.writeStringArray(acceptMimes); |
| 179 | out.writeInt(userSortOrder); |
| 180 | out.writeInt(allowMultiple ? 1 : 0); |
| 181 | out.writeInt(forceSize ? 1 : 0); |
| 182 | out.writeInt(showSize ? 1 : 0); |
| 183 | out.writeInt(localOnly ? 1 : 0); |
| 184 | out.writeInt(forceAdvanced ? 1 : 0); |
| 185 | out.writeInt(showAdvanced ? 1 : 0); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 186 | out.writeInt(restored ? 1 : 0); |
| 187 | DurableUtils.writeToParcel(out, stack); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 188 | out.writeMap(dirState); |
Steve McKay | 84fa371 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 189 | out.writeParcelable(selectedDocuments, 0); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 190 | out.writeList(selectedDocumentsForCopy); |
| 191 | out.writeList(excludedAuthorities); |
Tomasz Mikolajewski | 95d9c25 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 192 | out.writeInt(openableOnly ? 1 : 0); |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 193 | out.writeInt(mStackTouched ? 1 : 0); |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 194 | out.writeInt(mInitialRootChanged ? 1 : 0); |
| 195 | out.writeInt(mInitialDocChanged ? 1 : 0); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Jeff Sharkey | 2584c82 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 198 | public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() { |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 199 | @Override |
| 200 | public State createFromParcel(Parcel in) { |
Jeff Sharkey | 2584c82 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 201 | return createFromParcel(in, null); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public State createFromParcel(Parcel in, ClassLoader loader) { |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 206 | final State state = new State(); |
| 207 | state.action = in.readInt(); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 208 | 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 McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 216 | state.restored = in.readInt() != 0; |
| 217 | DurableUtils.readFromParcel(in, state.stack); |
Jeff Sharkey | 2584c82 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 218 | in.readMap(state.dirState, loader); |
Steve McKay | 84fa371 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 219 | state.selectedDocuments = in.readParcelable(loader); |
Jeff Sharkey | 2584c82 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 220 | in.readList(state.selectedDocumentsForCopy, loader); |
| 221 | in.readList(state.excludedAuthorities, loader); |
Tomasz Mikolajewski | 95d9c25 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 222 | state.openableOnly = in.readInt() != 0; |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 223 | state.mStackTouched = in.readInt() != 0; |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 224 | state.mInitialRootChanged = in.readInt() != 0; |
| 225 | state.mInitialDocChanged = in.readInt() != 0; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 226 | return state; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public State[] newArray(int size) { |
| 231 | return new State[size]; |
| 232 | } |
| 233 | }; |
Ben Kwa | ae96780 | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 234 | } |