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; 17 18 import static com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled; 19 20 import static java.lang.annotation.ElementType.FIELD; 21 import static java.lang.annotation.RetentionPolicy.SOURCE; 22 23 import android.view.MenuItem; 24 25 import androidx.annotation.Nullable; 26 import androidx.annotation.VisibleForTesting; 27 import androidx.recyclerview.selection.SelectionTracker; 28 import androidx.recyclerview.widget.RecyclerView; 29 30 import com.android.documentsui.MenuManager.SelectionDetails; 31 import com.android.documentsui.base.DebugHelper; 32 import com.android.documentsui.base.EventHandler; 33 import com.android.documentsui.base.Features; 34 import com.android.documentsui.base.Lookup; 35 import com.android.documentsui.base.RootInfo; 36 import com.android.documentsui.dirlist.AppsRowManager; 37 import com.android.documentsui.picker.PickResult; 38 import com.android.documentsui.queries.SearchViewManager; 39 import com.android.documentsui.ui.DialogController; 40 import com.android.documentsui.ui.MessageBuilder; 41 42 import java.lang.annotation.Retention; 43 import java.lang.annotation.Target; 44 import java.util.Collection; 45 import java.util.function.Consumer; 46 47 /** 48 * Provides access to runtime dependencies. 49 */ 50 public class Injector<T extends ActionHandler> { 51 52 public final Features features; 53 public final ActivityConfig config; 54 public final MessageBuilder messages; 55 public final Lookup<String, String> fileTypeLookup; 56 public final Consumer<Collection<RootInfo>> shortcutsUpdater; 57 58 public MenuManager menuManager; 59 public DialogController dialogs; 60 public SearchViewManager searchManager; 61 public AppsRowManager appsRowManager; 62 63 public PickResult pickResult; 64 65 public final DebugHelper debugHelper; 66 67 @ContentScoped 68 public ActionModeController actionModeController; 69 70 @ContentScoped 71 public ProfileTabsController profileTabsController; 72 73 @ContentScoped 74 public T actions; 75 76 @ContentScoped 77 public FocusManager focusManager; 78 79 @ContentScoped 80 public DocsSelectionHelper selectionMgr; 81 82 private final Model mModel; 83 84 // must be initialized before calling super.onCreate because prefs 85 // are used in State initialization. Injector( Features features, ActivityConfig config, MessageBuilder messages, DialogController dialogs, Lookup<String, String> fileTypeLookup, Consumer<Collection<RootInfo>> shortcutsUpdater)86 public Injector( 87 Features features, 88 ActivityConfig config, 89 MessageBuilder messages, 90 DialogController dialogs, 91 Lookup<String, String> fileTypeLookup, 92 Consumer<Collection<RootInfo>> shortcutsUpdater) { 93 this(features, config, messages, dialogs, fileTypeLookup, 94 shortcutsUpdater, new Model(features)); 95 } 96 97 @VisibleForTesting Injector( Features features, ActivityConfig config, MessageBuilder messages, DialogController dialogs, Lookup<String, String> fileTypeLookup, Consumer<Collection<RootInfo>> shortcutsUpdater, Model model)98 public Injector( 99 Features features, 100 ActivityConfig config, 101 MessageBuilder messages, 102 DialogController dialogs, 103 Lookup<String, String> fileTypeLookup, 104 Consumer<Collection<RootInfo>> shortcutsUpdater, 105 Model model) { 106 107 this.features = features; 108 this.config = config; 109 this.messages = messages; 110 this.dialogs = dialogs; 111 this.fileTypeLookup = fileTypeLookup; 112 this.shortcutsUpdater = shortcutsUpdater; 113 this.mModel = model; 114 this.debugHelper = new DebugHelper(this); 115 } 116 getModel()117 public Model getModel() { 118 return mModel; 119 } 120 getFocusManager(RecyclerView view, Model model)121 public FocusManager getFocusManager(RecyclerView view, Model model) { 122 assert (focusManager != null); 123 return focusManager.reset(view, model); 124 } 125 updateSharedSelectionTracker(SelectionTracker<String> selectionTracker)126 public void updateSharedSelectionTracker(SelectionTracker<String> selectionTracker) { 127 selectionMgr.reset(selectionTracker); 128 } 129 getActionModeController( SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker)130 public final ActionModeController getActionModeController( 131 SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker) { 132 if (isUseMaterial3FlagEnabled()) { 133 return null; 134 } 135 return actionModeController.reset(selectionDetails, menuItemClicker); 136 } 137 138 /** 139 * Obtains action handler and resets it if necessary. 140 * 141 * @param contentLock the lock held by 142 * {@link com.android.documentsui.selection.BandSelectionHelper} and 143 * {@link com.android.documentsui.selection.GestureSelectionHelper} to prevent 144 * loader from updating result during band/gesture selection. May be {@code null} if 145 * called from {@link com.android.documentsui.sidebar.RootsFragment}. 146 * @return the action handler 147 */ getActionHandler(@ullable ContentLock contentLock)148 public T getActionHandler(@Nullable ContentLock contentLock) { 149 150 // provide our friend, RootsFragment, early access to this special feature! 151 if (contentLock == null) { 152 return actions; 153 } 154 155 return actions.reset(contentLock); 156 } 157 158 /** 159 * Decorates a field that that is injected. 160 */ 161 @Retention(SOURCE) 162 @Target(FIELD) 163 public @interface Injected { 164 165 } 166 167 /** 168 * Decorates a field that holds an object that must be reset in the current content scope 169 * (i.e. DirectoryFragment). Fields decorated with this must have an associated 170 * accessor on Injector that, when call, reset the object for the calling context. 171 */ 172 @Retention(SOURCE) 173 @Target(FIELD) 174 public @interface ContentScoped { 175 176 } 177 } 178