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