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 | |
Aga Wronska | 6d50bcc | 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 | 15b9278 | 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 | 15b9278 | 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 | 7776aa5 | 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 | 6d50bcc | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 81 | public @ActionType int action; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 82 | public String[] acceptMimes; |
| 83 | |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 84 | /** Derived from local preferences */ |
| 85 | public @ViewMode int derivedMode = MODE_GRID; |
Steve McKay | f8a5e08 | 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 | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 93 | public boolean forceSize; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 94 | public boolean showSize; |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 95 | public boolean localOnly; |
Aga Wronska | ceb990e | 2016-03-30 18:07:59 -0700 | [diff] [blame] | 96 | public boolean showAdvancedOption; |
| 97 | public boolean showAdvanced; |
Steve McKay | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 98 | public boolean restored; |
Aga Wronska | a4cb878 | 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 | f171934 | 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 | f2c8b0d | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 107 | public boolean directoryCopy; |
Tomasz Mikolajewski | 95d9c25 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 108 | public boolean openableOnly; |
Steve McKay | f171934 | 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 | c8bdc7f | 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 | f171934 | 2016-02-17 15:02:01 -0800 | [diff] [blame] | 114 | */ |
Steve McKay | c8bdc7f | 2016-02-18 11:32:26 -0800 | [diff] [blame] | 115 | public @OpType int copyOperationSubType = FileOperationService.OPERATION_UNKNOWN; |
Steve McKay | f8a5e08 | 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 | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 119 | private boolean mStackTouched; |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 120 | private boolean mInitialRootChanged; |
| 121 | private boolean mInitialDocChanged; |
Steve McKay | 7776aa5 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 122 | |
Steve McKay | f8a5e08 | 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 | |
| 126 | /** Currently copying file */ |
Steve McKay | 84fa371 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 127 | public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>(); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 128 | |
| 129 | /** Name of the package that started DocsUI */ |
| 130 | public List<String> excludedAuthorities = new ArrayList<>(); |
| 131 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 132 | public void initAcceptMimes(Intent intent) { |
| 133 | if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) { |
| 134 | acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES); |
| 135 | } else { |
| 136 | String glob = intent.getType(); |
| 137 | acceptMimes = new String[] { glob != null ? glob : "*/*" }; |
| 138 | } |
| 139 | } |
| 140 | |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 141 | public void onRootChanged(RootInfo root) { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 142 | if (DEBUG) Log.d(TAG, "Root changed to: " + root); |
| 143 | if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) { |
| 144 | mInitialRootChanged = true; |
| 145 | } |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 146 | stack.root = root; |
| 147 | stack.clear(); |
| 148 | mStackTouched = true; |
| 149 | } |
| 150 | |
| 151 | public void pushDocument(DocumentInfo info) { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 152 | if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info); |
| 153 | if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) { |
| 154 | mInitialDocChanged = true; |
| 155 | } |
Daichi Hirono | 97c9c1a | 2016-01-08 12:22:09 +0900 | [diff] [blame] | 156 | stack.push(info); |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 157 | mStackTouched = true; |
| 158 | } |
| 159 | |
| 160 | public void popDocument() { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 161 | if (DEBUG) Log.d(TAG, "Popping doc off stack."); |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 162 | stack.pop(); |
| 163 | mStackTouched = true; |
| 164 | } |
| 165 | |
| 166 | public void setStack(DocumentStack stack) { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 167 | if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack); |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 168 | this.stack = stack; |
| 169 | mStackTouched = true; |
| 170 | } |
| 171 | |
Steve McKay | fd8425a | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 172 | // This will return true even when the initial location is set. |
| 173 | // 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] | 174 | public boolean hasLocationChanged() { |
| 175 | return mStackTouched; |
| 176 | } |
| 177 | |
Steve McKay | fd8425a | 2016-02-23 14:34:50 -0800 | [diff] [blame] | 178 | public boolean hasInitialLocationChanged() { |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 179 | return mInitialRootChanged || mInitialDocChanged; |
| 180 | } |
| 181 | |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 182 | @Override |
| 183 | public int describeContents() { |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void writeToParcel(Parcel out, int flags) { |
| 189 | out.writeInt(action); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 190 | out.writeStringArray(acceptMimes); |
| 191 | out.writeInt(userSortOrder); |
| 192 | out.writeInt(allowMultiple ? 1 : 0); |
| 193 | out.writeInt(forceSize ? 1 : 0); |
| 194 | out.writeInt(showSize ? 1 : 0); |
| 195 | out.writeInt(localOnly ? 1 : 0); |
Aga Wronska | ceb990e | 2016-03-30 18:07:59 -0700 | [diff] [blame] | 196 | out.writeInt(showAdvancedOption ? 1 : 0); |
| 197 | out.writeInt(showAdvanced ? 1 : 0); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 198 | out.writeInt(restored ? 1 : 0); |
Aga Wronska | a4cb878 | 2016-03-22 17:15:18 -0700 | [diff] [blame] | 199 | out.writeInt(external ? 1 : 0); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 200 | DurableUtils.writeToParcel(out, stack); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 201 | out.writeMap(dirState); |
| 202 | out.writeList(selectedDocumentsForCopy); |
| 203 | out.writeList(excludedAuthorities); |
Tomasz Mikolajewski | 95d9c25 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 204 | out.writeInt(openableOnly ? 1 : 0); |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 205 | out.writeInt(mStackTouched ? 1 : 0); |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 206 | out.writeInt(mInitialRootChanged ? 1 : 0); |
| 207 | out.writeInt(mInitialDocChanged ? 1 : 0); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Jeff Sharkey | 2584c82 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 210 | public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() { |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 211 | @Override |
| 212 | public State createFromParcel(Parcel in) { |
Jeff Sharkey | 2584c82 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 213 | return createFromParcel(in, null); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public State createFromParcel(Parcel in, ClassLoader loader) { |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 218 | final State state = new State(); |
| 219 | state.action = in.readInt(); |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 220 | state.acceptMimes = in.readStringArray(); |
| 221 | state.userSortOrder = in.readInt(); |
| 222 | state.allowMultiple = in.readInt() != 0; |
| 223 | state.forceSize = in.readInt() != 0; |
| 224 | state.showSize = in.readInt() != 0; |
| 225 | state.localOnly = in.readInt() != 0; |
Aga Wronska | ceb990e | 2016-03-30 18:07:59 -0700 | [diff] [blame] | 226 | state.showAdvancedOption = in.readInt() != 0; |
| 227 | state.showAdvanced = in.readInt() != 0; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 228 | state.restored = in.readInt() != 0; |
Aga Wronska | a4cb878 | 2016-03-22 17:15:18 -0700 | [diff] [blame] | 229 | state.external = in.readInt() != 0; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 230 | DurableUtils.readFromParcel(in, state.stack); |
Jeff Sharkey | 2584c82 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 231 | in.readMap(state.dirState, loader); |
| 232 | in.readList(state.selectedDocumentsForCopy, loader); |
| 233 | in.readList(state.excludedAuthorities, loader); |
Tomasz Mikolajewski | 95d9c25 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 234 | state.openableOnly = in.readInt() != 0; |
Daichi Hirono | 2806beb | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 235 | state.mStackTouched = in.readInt() != 0; |
Steve McKay | cb9eb42 | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 236 | state.mInitialRootChanged = in.readInt() != 0; |
| 237 | state.mInitialDocChanged = in.readInt() != 0; |
Steve McKay | f8a5e08 | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 238 | return state; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public State[] newArray(int size) { |
| 243 | return new State[size]; |
| 244 | } |
| 245 | }; |
Ben Kwa | ae96780 | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 246 | } |