1 /* 2 * Copyright (C) 2016 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 package com.android.documentsui.testing; 17 18 import android.provider.DocumentsContract.Root; 19 import com.android.documentsui.base.Providers; 20 import com.android.documentsui.base.RootInfo; 21 import com.android.documentsui.base.State; 22 import com.android.documentsui.roots.ProvidersAccess; 23 24 import java.util.ArrayList; 25 import java.util.Collection; 26 import java.util.HashMap; 27 import java.util.List; 28 import java.util.Map; 29 30 import javax.annotation.Nullable; 31 32 public class TestProvidersAccess implements ProvidersAccess { 33 34 public static final RootInfo DOWNLOADS; 35 public static final RootInfo HOME; 36 public static final RootInfo HAMMY; 37 public static final RootInfo PICKLES; 38 public static final RootInfo RECENTS; 39 40 static { 41 DOWNLOADS = new RootInfo() {{ 42 flags = Root.FLAG_SUPPORTS_CREATE; 43 }}; 44 DOWNLOADS.authority = Providers.AUTHORITY_DOWNLOADS; 45 DOWNLOADS.rootId = Providers.ROOT_ID_DOWNLOADS; 46 47 HOME = new RootInfo(); 48 HOME.authority = Providers.AUTHORITY_STORAGE; 49 HOME.rootId = Providers.ROOT_ID_HOME; 50 51 HAMMY = new RootInfo(); 52 HAMMY.authority = "yummies"; 53 HAMMY.rootId = "hamsandwich"; 54 55 PICKLES = new RootInfo(); 56 PICKLES.authority = "yummies"; 57 PICKLES.rootId = "pickles"; 58 59 RECENTS = new RootInfo() {{ 60 // Special root for recents 61 derivedType = RootInfo.TYPE_RECENTS; 62 flags = Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_IS_CHILD; 63 availableBytes = -1; 64 }}; 65 } 66 67 public final Map<String, Collection<RootInfo>> roots = new HashMap<>(); 68 private @Nullable RootInfo nextRoot; 69 70 public TestProvidersAccess() { 71 add(DOWNLOADS); 72 add(HOME); 73 add(HAMMY); 74 add(PICKLES); 75 } 76 77 private void add(RootInfo root) { 78 if (!roots.containsKey(root.authority)) { 79 roots.put(root.authority, new ArrayList<>()); 80 } 81 roots.get(root.authority).add(root); 82 } 83 84 public void configurePm(TestPackageManager pm) { 85 pm.addStubContentProviderForRoot(TestProvidersAccess.DOWNLOADS); 86 pm.addStubContentProviderForRoot(TestProvidersAccess.HOME); 87 pm.addStubContentProviderForRoot(TestProvidersAccess.HAMMY); 88 pm.addStubContentProviderForRoot(TestProvidersAccess.PICKLES); 89 } 90 91 @Override 92 public RootInfo getRootOneshot(String authority, String rootId) { 93 if (roots.containsKey(authority)) { 94 for (RootInfo root : roots.get(authority)) { 95 if (rootId.equals(root.rootId)) { 96 return root; 97 } 98 } 99 } 100 return null; 101 } 102 103 @Override 104 public Collection<RootInfo> getMatchingRootsBlocking(State state) { 105 List<RootInfo> allRoots = new ArrayList<>(); 106 for (String authority : roots.keySet()) { 107 allRoots.addAll(roots.get(authority)); 108 } 109 return ProvidersAccess.getMatchingRoots(allRoots, state); 110 } 111 112 @Override 113 public Collection<RootInfo> getRootsForAuthorityBlocking(String authority) { 114 return roots.get(authority); 115 } 116 117 @Override 118 public Collection<RootInfo> getRootsBlocking() { 119 List<RootInfo> result = new ArrayList<>(); 120 for (Collection<RootInfo> vals : roots.values()) { 121 result.addAll(vals); 122 } 123 return result; 124 } 125 126 @Override 127 public RootInfo getDefaultRootBlocking(State state) { 128 return DOWNLOADS; 129 } 130 131 @Override 132 public RootInfo getRecentsRoot() { 133 return RECENTS; 134 } 135 136 @Override 137 public String getApplicationName(String authority) { 138 return "Test Application"; 139 } 140 141 @Override 142 public String getPackageName(String authority) { 143 return "com.android.documentsui"; 144 } 145 } 146