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