Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 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 | |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 18 | import static java.lang.annotation.ElementType.FIELD; |
| 19 | import static java.lang.annotation.RetentionPolicy.SOURCE; |
| 20 | |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 21 | import android.support.annotation.Nullable; |
| 22 | import android.support.v7.widget.RecyclerView; |
| 23 | import android.view.MenuItem; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 24 | |
| 25 | import com.android.documentsui.MenuManager.SelectionDetails; |
Steve McKay | 0ce04b7 | 2017-06-05 13:23:54 -0700 | [diff] [blame^] | 26 | import com.android.documentsui.base.DebugHelper; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 27 | import com.android.documentsui.base.EventHandler; |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 28 | import com.android.documentsui.base.Features; |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 29 | import com.android.documentsui.base.Lookup; |
Steve McKay | 0ce04b7 | 2017-06-05 13:23:54 -0700 | [diff] [blame^] | 30 | import com.android.documentsui.base.RootInfo; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 31 | import com.android.documentsui.dirlist.DocumentsAdapter; |
Steve McKay | e8b547f | 2016-12-19 14:42:55 -0800 | [diff] [blame] | 32 | import com.android.documentsui.prefs.ScopedPreferences; |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 33 | import com.android.documentsui.queries.SearchViewManager; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 34 | import com.android.documentsui.selection.SelectionManager; |
| 35 | import com.android.documentsui.selection.SelectionManager.SelectionPredicate; |
| 36 | import com.android.documentsui.ui.DialogController; |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 37 | import com.android.documentsui.ui.MessageBuilder; |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 38 | import com.android.internal.annotations.VisibleForTesting; |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 39 | |
| 40 | import java.lang.annotation.Retention; |
| 41 | import java.lang.annotation.Target; |
Steve McKay | 0ce04b7 | 2017-06-05 13:23:54 -0700 | [diff] [blame^] | 42 | import java.util.Collection; |
| 43 | import java.util.function.Consumer; |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Provides access to runtime dependencies. |
| 47 | */ |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 48 | public class Injector<T extends ActionHandler> { |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 49 | |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 50 | public final Features features; |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 51 | public final ActivityConfig config; |
| 52 | public final ScopedPreferences prefs; |
| 53 | public final MessageBuilder messages; |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 54 | public final Lookup<String, String> fileTypeLookup; |
Steve McKay | 0ce04b7 | 2017-06-05 13:23:54 -0700 | [diff] [blame^] | 55 | public final Consumer<Collection<RootInfo>> shortcutsUpdater; |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 56 | |
| 57 | public MenuManager menuManager; |
| 58 | public DialogController dialogs; |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 59 | public SearchViewManager searchManager; |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 60 | |
Garfield Tan | da2c0f0 | 2017-04-11 13:47:58 -0700 | [diff] [blame] | 61 | public final DebugHelper debugHelper; |
| 62 | |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 63 | @ContentScoped |
| 64 | public ActionModeController actionModeController; |
| 65 | |
| 66 | @ContentScoped |
| 67 | public T actions; |
| 68 | |
| 69 | @ContentScoped |
| 70 | public FocusManager focusManager; |
| 71 | |
| 72 | @ContentScoped |
| 73 | public SelectionManager selectionMgr; |
| 74 | |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 75 | private final Model mModel; |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 76 | |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 77 | // must be initialized before calling super.onCreate because prefs |
| 78 | // are used in State initialization. |
| 79 | public Injector( |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 80 | Features features, |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 81 | ActivityConfig config, |
| 82 | ScopedPreferences prefs, |
| 83 | MessageBuilder messages, |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 84 | DialogController dialogs, |
Steve McKay | 0ce04b7 | 2017-06-05 13:23:54 -0700 | [diff] [blame^] | 85 | Lookup<String, String> fileTypeLookup, |
| 86 | Consumer<Collection<RootInfo>> shortcutsUpdater) { |
| 87 | this(features, config, prefs, messages, dialogs, fileTypeLookup, |
| 88 | shortcutsUpdater, new Model(features)); |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @VisibleForTesting |
| 92 | public Injector( |
| 93 | Features features, |
| 94 | ActivityConfig config, |
| 95 | ScopedPreferences prefs, |
| 96 | MessageBuilder messages, |
| 97 | DialogController dialogs, |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 98 | Lookup<String, String> fileTypeLookup, |
Steve McKay | 0ce04b7 | 2017-06-05 13:23:54 -0700 | [diff] [blame^] | 99 | Consumer<Collection<RootInfo>> shortcutsUpdater, |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 100 | Model model) { |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 101 | |
Steve McKay | 98f8c5f | 2017-03-03 13:52:14 -0800 | [diff] [blame] | 102 | this.features = features; |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 103 | this.config = config; |
| 104 | this.prefs = prefs; |
| 105 | this.messages = messages; |
| 106 | this.dialogs = dialogs; |
Garfield Tan | b47b4b5 | 2017-05-16 12:48:53 -0700 | [diff] [blame] | 107 | this.fileTypeLookup = fileTypeLookup; |
Steve McKay | 0ce04b7 | 2017-06-05 13:23:54 -0700 | [diff] [blame^] | 108 | this.shortcutsUpdater = shortcutsUpdater; |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 109 | this.mModel = model; |
Jon Mann | eb1d11b | 2017-04-01 15:36:59 -0700 | [diff] [blame] | 110 | this.debugHelper = new DebugHelper(this); |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Jon Mann | 30d8c79 | 2017-02-21 17:44:49 -0800 | [diff] [blame] | 113 | public Model getModel() { |
| 114 | return mModel; |
| 115 | } |
| 116 | |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 117 | public FocusManager getFocusManager(RecyclerView view, Model model) { |
| 118 | assert (focusManager != null); |
| 119 | return focusManager.reset(view, model); |
| 120 | } |
| 121 | |
| 122 | public SelectionManager getSelectionManager( |
| 123 | DocumentsAdapter adapter, SelectionPredicate canSetState) { |
| 124 | return selectionMgr.reset(adapter, canSetState); |
| 125 | } |
| 126 | |
| 127 | public final ActionModeController getActionModeController( |
Ben Lin | 11a2c13 | 2017-05-03 18:46:37 -0700 | [diff] [blame] | 128 | SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker) { |
| 129 | return actionModeController.reset(selectionDetails, menuItemClicker); |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Obtains action handler and resets it if necessary. |
| 134 | * @param reloadLock the lock held by {@link com.android.documentsui.selection.BandController} |
| 135 | * to prevent loader from updating result during band selection. May be |
| 136 | * {@code null} if called from |
| 137 | * {@link com.android.documentsui.sidebar.RootsFragment}. |
| 138 | * @return the action handler |
| 139 | */ |
| 140 | public T getActionHandler(@Nullable DirectoryReloadLock reloadLock) { |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 141 | |
| 142 | // provide our friend, RootsFragment, early access to this special feature! |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 143 | if (reloadLock == null) { |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 144 | return actions; |
| 145 | } |
| 146 | |
Garfield Tan | e967033 | 2017-03-06 18:33:23 -0800 | [diff] [blame] | 147 | return actions.reset(reloadLock); |
Steve McKay | 92ae43d | 2016-11-08 12:06:58 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Decorates a field that that is injected. |
| 152 | */ |
| 153 | @Retention(SOURCE) |
| 154 | @Target(FIELD) |
| 155 | public @interface Injected { |
| 156 | |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Decorates a field that holds an object that must be reset in the current content scope |
| 161 | * (i.e. DirectoryFragment). Fields decorated with this must have an associated |
| 162 | * accessor on Injector that, when call, reset the object for the calling context. |
| 163 | */ |
| 164 | @Retention(SOURCE) |
| 165 | @Target(FIELD) |
| 166 | public @interface ContentScoped { |
| 167 | |
| 168 | } |
Steve McKay | 0471826 | 2016-11-08 11:01:35 -0800 | [diff] [blame] | 169 | } |