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 | |
| 28 | import com.android.documentsui.model.DocumentInfo; |
| 29 | import com.android.documentsui.model.DocumentStack; |
| 30 | import com.android.documentsui.model.DurableUtils; |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 31 | import com.android.documentsui.model.RootInfo; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 32 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 33 | import java.lang.annotation.Retention; |
| 34 | import java.lang.annotation.RetentionPolicy; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.List; |
| 38 | |
| 39 | public class State implements android.os.Parcelable { |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 40 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 41 | private static final String TAG = "State"; |
| 42 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 43 | 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 McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 67 | public int action; |
| 68 | public String[] acceptMimes; |
| 69 | |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 70 | /** Derived from local preferences */ |
| 71 | public @ViewMode int derivedMode = MODE_GRID; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 72 | |
| 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 McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 79 | public boolean forceSize; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 80 | public boolean showSize; |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 81 | public boolean localOnly; |
| 82 | public boolean forceAdvanced; |
| 83 | public boolean showAdvanced; |
Steve McKay | 9f9d5b4 | 2015-09-23 15:44:24 -0700 | [diff] [blame] | 84 | public boolean restored; |
| 85 | public boolean directoryCopy; |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 86 | public boolean openableOnly; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 87 | /** 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 McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 92 | private boolean mStackTouched; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 93 | private boolean mInitialRootChanged; |
| 94 | private boolean mInitialDocChanged; |
Steve McKay | 3eb2d07 | 2016-01-25 19:00:22 -0800 | [diff] [blame] | 95 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 96 | /** 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 McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 108 | 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 Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 117 | public void onRootChanged(RootInfo root) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 118 | if (DEBUG) Log.d(TAG, "Root changed to: " + root); |
| 119 | if (!mInitialRootChanged && stack.root != null && !root.equals(stack.root)) { |
| 120 | mInitialRootChanged = true; |
| 121 | } |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 122 | stack.root = root; |
| 123 | stack.clear(); |
| 124 | mStackTouched = true; |
| 125 | } |
| 126 | |
| 127 | public void pushDocument(DocumentInfo info) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 128 | 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 Hirono | 7f34b20 | 2016-01-08 12:22:09 +0900 | [diff] [blame] | 132 | stack.push(info); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 133 | mStackTouched = true; |
| 134 | } |
| 135 | |
| 136 | public void popDocument() { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 137 | if (DEBUG) Log.d(TAG, "Popping doc off stack."); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 138 | stack.pop(); |
| 139 | mStackTouched = true; |
| 140 | } |
| 141 | |
| 142 | public void setStack(DocumentStack stack) { |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 143 | if (DEBUG) Log.d(TAG, "Setting the whole darn stack to: " + stack); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 144 | this.stack = stack; |
| 145 | mStackTouched = true; |
| 146 | } |
| 147 | |
| 148 | public boolean hasLocationChanged() { |
| 149 | return mStackTouched; |
| 150 | } |
| 151 | |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 152 | public boolean initialiLocationHasChanged() { |
| 153 | return mInitialRootChanged || mInitialDocChanged; |
| 154 | } |
| 155 | |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 156 | @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 McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 164 | 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 McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 172 | 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 Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 178 | out.writeInt(openableOnly ? 1 : 0); |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 179 | out.writeInt(mStackTouched ? 1 : 0); |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 180 | out.writeInt(mInitialRootChanged ? 1 : 0); |
| 181 | out.writeInt(mInitialDocChanged ? 1 : 0); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 184 | public static final ClassLoaderCreator<State> CREATOR = new ClassLoaderCreator<State>() { |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 185 | @Override |
| 186 | public State createFromParcel(Parcel in) { |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 187 | return createFromParcel(in, null); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public State createFromParcel(Parcel in, ClassLoader loader) { |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 192 | final State state = new State(); |
| 193 | state.action = in.readInt(); |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 194 | 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 McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 202 | state.restored = in.readInt() != 0; |
| 203 | DurableUtils.readFromParcel(in, state.stack); |
| 204 | state.currentSearch = in.readString(); |
Jeff Sharkey | ba9a4b3 | 2016-02-05 17:13:16 -0700 | [diff] [blame] | 205 | in.readMap(state.dirState, loader); |
| 206 | in.readList(state.selectedDocumentsForCopy, loader); |
| 207 | in.readList(state.excludedAuthorities, loader); |
Tomasz Mikolajewski | a8057a9 | 2015-11-16 11:41:28 +0900 | [diff] [blame] | 208 | state.openableOnly = in.readInt() != 0; |
Daichi Hirono | 3b36c5a | 2016-01-07 15:29:12 +0900 | [diff] [blame] | 209 | state.mStackTouched = in.readInt() != 0; |
Steve McKay | aa15dae | 2016-02-09 16:17:24 -0800 | [diff] [blame^] | 210 | state.mInitialRootChanged = in.readInt() != 0; |
| 211 | state.mInitialDocChanged = in.readInt() != 0; |
Steve McKay | 7a3b88c | 2015-09-23 17:21:40 -0700 | [diff] [blame] | 212 | return state; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public State[] newArray(int size) { |
| 217 | return new State[size]; |
| 218 | } |
| 219 | }; |
Ben Kwa | 84cebbe | 2015-09-25 14:48:29 -0700 | [diff] [blame] | 220 | } |