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 | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 33 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 34 | import java.lang.annotation.Retention; |
| 35 | import java.lang.annotation.RetentionPolicy; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.List; |
| 39 | |
| 40 | public class State implements android.os.Parcelable { |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 41 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 42 | private static final String TAG = "State"; |
| 43 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 44 | public static final int ACTION_OPEN = 1; |
| 45 | public static final int ACTION_CREATE = 2; |
| 46 | public static final int ACTION_GET_CONTENT = 3; |
| 47 | public static final int ACTION_OPEN_TREE = 4; |
| 48 | public static final int ACTION_MANAGE = 5; |
| 49 | public static final int ACTION_BROWSE = 6; |
| 50 | public static final int ACTION_PICK_COPY_DESTINATION = 8; |
| 51 | |
| 52 | @IntDef(flag = true, value = { |
| 53 | MODE_UNKNOWN, |
| 54 | MODE_LIST, |
| 55 | MODE_GRID |
| 56 | }) |
| 57 | @Retention(RetentionPolicy.SOURCE) |
| 58 | public @interface ViewMode {} |
| 59 | public static final int MODE_UNKNOWN = 0; |
| 60 | public static final int MODE_LIST = 1; |
| 61 | public static final int MODE_GRID = 2; |
| 62 | |
| 63 | public static final int SORT_ORDER_UNKNOWN = 0; |
| 64 | public static final int SORT_ORDER_DISPLAY_NAME = 1; |
| 65 | public static final int SORT_ORDER_LAST_MODIFIED = 2; |
| 66 | public static final int SORT_ORDER_SIZE = 3; |
| 67 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 68 | public int action; |
| 69 | public String[] acceptMimes; |
| 70 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 71 | /** Derived from local preferences */ |
| 72 | public @ViewMode int derivedMode = MODE_GRID; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 73 | |
| 74 | /** Explicit user choice */ |
| 75 | public int userSortOrder = SORT_ORDER_UNKNOWN; |
| 76 | /** Derived after loader */ |
| 77 | public int derivedSortOrder = SORT_ORDER_DISPLAY_NAME; |
| 78 | |
| 79 | public boolean allowMultiple; |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 80 | public boolean forceSize; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 81 | public boolean showSize; |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 82 | public boolean localOnly; |
| 83 | public boolean forceAdvanced; |
| 84 | public boolean showAdvanced; |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 85 | public boolean restored; |
| 86 | public boolean directoryCopy; |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 87 | public boolean openableOnly; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 88 | /** Transfer mode for file copy/move operations. */ |
| 89 | public int transferMode; |
| 90 | |
| 91 | /** Current user navigation stack; empty implies recents. */ |
| 92 | public DocumentStack stack = new DocumentStack(); |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 93 | private boolean mStackTouched; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 94 | private boolean mInitialRootChanged; |
| 95 | private boolean mInitialDocChanged; |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 96 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 97 | /** Currently active search, overriding any stack. */ |
| 98 | public String currentSearch; |
| 99 | |
| 100 | /** Instance state for every shown directory */ |
| 101 | public HashMap<String, SparseArray<Parcelable>> dirState = new HashMap<>(); |
| 102 | |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 103 | /** UI selection */ |
| 104 | public Selection selectedDocuments = new Selection(); |
| 105 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 106 | /** Currently copying file */ |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 107 | public List<DocumentInfo> selectedDocumentsForCopy = new ArrayList<>(); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 108 | |
| 109 | /** Name of the package that started DocsUI */ |
| 110 | public List<String> excludedAuthorities = new ArrayList<>(); |
| 111 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 112 | public void initAcceptMimes(Intent intent) { |
| 113 | if (intent.hasExtra(Intent.EXTRA_MIME_TYPES)) { |
| 114 | acceptMimes = intent.getStringArrayExtra(Intent.EXTRA_MIME_TYPES); |
| 115 | } else { |
| 116 | String glob = intent.getType(); |
| 117 | acceptMimes = new String[] { glob != null ? glob : "*/*" }; |
| 118 | } |
| 119 | } |
| 120 | |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 121 | public void onRootChanged(RootInfo root) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 122 | if (DEBUG) Log.d(TAG, "Root changed to: " + root); |
| 123 | if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) { |
| 124 | mInitialRootChanged = true; |
| 125 | } |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 126 | stack.root = root; |
| 127 | stack.clear(); |
| 128 | mStackTouched = true; |
| 129 | } |
| 130 | |
| 131 | public void pushDocument(DocumentInfo info) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 132 | if (DEBUG) Log.d(TAG, "Adding doc to stack: " + info); |
| 133 | if (!mInitialDocChanged && stack.size() > 0 && !info.equals(stack.peek())) { |
| 134 | mInitialDocChanged = true; |
| 135 | } |
Daichi Hirono | 7f34b20 | 2016-01-08 12:22:09 +0900 | [diff] [blame] | 136 | stack.push(info); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 137 | mStackTouched = true; |
| 138 | } |
| 139 | |
| 140 | public void popDocument() { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 141 | if (DEBUG) Log.d(TAG, "Popping doc off stack."); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 142 | stack.pop(); |
| 143 | mStackTouched = true; |
| 144 | } |
| 145 | |
| 146 | public void setStack(DocumentStack stack) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 147 | if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 148 | this.stack = stack; |
| 149 | mStackTouched = true; |
| 150 | } |
| 151 | |
| 152 | public boolean hasLocationChanged() { |
| 153 | return mStackTouched; |
| 154 | } |
| 155 | |
Aga Wronska | d4b1753 | 2016-02-12 09:15:32 -0800 | [diff] [blame^] | 156 | public boolean initialLocationHasChanged() { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 157 | return mInitialRootChanged || mInitialDocChanged; |
| 158 | } |
| 159 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 160 | @Override |
| 161 | public int describeContents() { |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public void writeToParcel(Parcel out, int flags) { |
| 167 | out.writeInt(action); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 168 | out.writeStringArray(acceptMimes); |
| 169 | out.writeInt(userSortOrder); |
| 170 | out.writeInt(allowMultiple ? 1 : 0); |
| 171 | out.writeInt(forceSize ? 1 : 0); |
| 172 | out.writeInt(showSize ? 1 : 0); |
| 173 | out.writeInt(localOnly ? 1 : 0); |
| 174 | out.writeInt(forceAdvanced ? 1 : 0); |
| 175 | out.writeInt(showAdvanced ? 1 : 0); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 176 | out.writeInt(restored ? 1 : 0); |
| 177 | DurableUtils.writeToParcel(out, stack); |
| 178 | out.writeString(currentSearch); |
| 179 | out.writeMap(dirState); |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 180 | out.writeParcelable(selectedDocuments, 0); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 181 | out.writeList(selectedDocumentsForCopy); |
| 182 | out.writeList(excludedAuthorities); |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 183 | out.writeInt(openableOnly ? 1 : 0); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 184 | out.writeInt(mStackTouched ? 1 : 0); |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 185 | out.writeInt(mInitialRootChanged ? 1 : 0); |
| 186 | out.writeInt(mInitialDocChanged ? 1 : 0); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 189 | public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() { |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 190 | @Override |
| 191 | public State createFromParcel(Parcel in) { |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 192 | return createFromParcel(in, null); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public State createFromParcel(Parcel in, ClassLoader loader) { |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 197 | final State state = new State(); |
| 198 | state.action = in.readInt(); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 199 | state.acceptMimes = in.readStringArray(); |
| 200 | state.userSortOrder = in.readInt(); |
| 201 | state.allowMultiple = in.readInt() != 0; |
| 202 | state.forceSize = in.readInt() != 0; |
| 203 | state.showSize = in.readInt() != 0; |
| 204 | state.localOnly = in.readInt() != 0; |
| 205 | state.forceAdvanced = in.readInt() != 0; |
| 206 | state.showAdvanced = in.readInt() != 0; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 207 | state.restored = in.readInt() != 0; |
| 208 | DurableUtils.readFromParcel(in, state.stack); |
| 209 | state.currentSearch = in.readString(); |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 210 | in.readMap(state.dirState, loader); |
Steve McKay | e852d93 | 2016-02-08 19:09:42 -0800 | [diff] [blame] | 211 | state.selectedDocuments = in.readParcelable(loader); |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 212 | in.readList(state.selectedDocumentsForCopy, loader); |
| 213 | in.readList(state.excludedAuthorities, loader); |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 214 | state.openableOnly = in.readInt() != 0; |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 215 | state.mStackTouched = in.readInt() != 0; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame] | 216 | state.mInitialRootChanged = in.readInt() != 0; |
| 217 | state.mInitialDocChanged = in.readInt() != 0; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 218 | return state; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public State[] newArray(int size) { |
| 223 | return new State[size]; |
| 224 | } |
| 225 | }; |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 226 | } |