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