blob: d6c631247cb49d0334710beff8a2a1fa0a64ae5f [file] [log] [blame]
Steve McKay04718262016-11-08 11:01:35 -08001/*
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 */
16package com.android.documentsui;
17
Steve McKay92ae43d2016-11-08 12:06:58 -080018import static java.lang.annotation.ElementType.FIELD;
19import static java.lang.annotation.RetentionPolicy.SOURCE;
20
Steve McKay04718262016-11-08 11:01:35 -080021import android.view.MenuItem;
Steve McKay04718262016-11-08 11:01:35 -080022
Riddle Hsu0c375982018-06-21 22:06:43 +080023import androidx.annotation.Nullable;
24import androidx.recyclerview.selection.SelectionTracker;
25import androidx.recyclerview.widget.RecyclerView;
26
Steve McKay04718262016-11-08 11:01:35 -080027import com.android.documentsui.MenuManager.SelectionDetails;
Steve McKay0ce04b72017-06-05 13:23:54 -070028import com.android.documentsui.base.DebugHelper;
Steve McKay04718262016-11-08 11:01:35 -080029import com.android.documentsui.base.EventHandler;
Steve McKay98f8c5f2017-03-03 13:52:14 -080030import com.android.documentsui.base.Features;
Garfield Tanb47b4b52017-05-16 12:48:53 -070031import com.android.documentsui.base.Lookup;
Steve McKay0ce04b72017-06-05 13:23:54 -070032import com.android.documentsui.base.RootInfo;
Tony Huange092d2d2018-12-26 19:02:51 +080033import com.android.documentsui.dirlist.AppsRowManager;
shawnline0ba46d2019-01-25 15:21:26 +080034import com.android.documentsui.picker.PickResult;
Steve McKaye8b547f2016-12-19 14:42:55 -080035import com.android.documentsui.prefs.ScopedPreferences;
Garfield Tane9670332017-03-06 18:33:23 -080036import com.android.documentsui.queries.SearchViewManager;
Steve McKay04718262016-11-08 11:01:35 -080037import com.android.documentsui.ui.DialogController;
Steve McKay92ae43d2016-11-08 12:06:58 -080038import com.android.documentsui.ui.MessageBuilder;
Jeff Sharkeya4ff00f2018-07-09 14:57:51 -060039import androidx.annotation.VisibleForTesting;
Steve McKay92ae43d2016-11-08 12:06:58 -080040
41import java.lang.annotation.Retention;
42import java.lang.annotation.Target;
Steve McKay0ce04b72017-06-05 13:23:54 -070043import java.util.Collection;
44import java.util.function.Consumer;
Steve McKay04718262016-11-08 11:01:35 -080045
46/**
47 * Provides access to runtime dependencies.
48 */
Steve McKay92ae43d2016-11-08 12:06:58 -080049public class Injector<T extends ActionHandler> {
Steve McKay04718262016-11-08 11:01:35 -080050
Steve McKay98f8c5f2017-03-03 13:52:14 -080051 public final Features features;
Steve McKay92ae43d2016-11-08 12:06:58 -080052 public final ActivityConfig config;
53 public final ScopedPreferences prefs;
54 public final MessageBuilder messages;
Garfield Tanb47b4b52017-05-16 12:48:53 -070055 public final Lookup<String, String> fileTypeLookup;
Steve McKay0ce04b72017-06-05 13:23:54 -070056 public final Consumer<Collection<RootInfo>> shortcutsUpdater;
Steve McKay92ae43d2016-11-08 12:06:58 -080057
58 public MenuManager menuManager;
59 public DialogController dialogs;
Garfield Tane9670332017-03-06 18:33:23 -080060 public SearchViewManager searchManager;
Tony Huange092d2d2018-12-26 19:02:51 +080061 public AppsRowManager appsRowManager;
Steve McKay92ae43d2016-11-08 12:06:58 -080062
shawnline0ba46d2019-01-25 15:21:26 +080063 public PickResult pickResult;
64
Garfield Tanda2c0f02017-04-11 13:47:58 -070065 public final DebugHelper debugHelper;
66
Steve McKay92ae43d2016-11-08 12:06:58 -080067 @ContentScoped
68 public ActionModeController actionModeController;
69
70 @ContentScoped
71 public T actions;
72
73 @ContentScoped
74 public FocusManager focusManager;
75
76 @ContentScoped
Steve McKay365e3cb2017-08-31 10:27:08 -070077 public DocsSelectionHelper selectionMgr;
Steve McKay92ae43d2016-11-08 12:06:58 -080078
Steve McKay98f8c5f2017-03-03 13:52:14 -080079 private final Model mModel;
Jon Mann30d8c792017-02-21 17:44:49 -080080
Steve McKay92ae43d2016-11-08 12:06:58 -080081 // must be initialized before calling super.onCreate because prefs
82 // are used in State initialization.
83 public Injector(
Steve McKay98f8c5f2017-03-03 13:52:14 -080084 Features features,
Steve McKay92ae43d2016-11-08 12:06:58 -080085 ActivityConfig config,
86 ScopedPreferences prefs,
87 MessageBuilder messages,
Garfield Tanb47b4b52017-05-16 12:48:53 -070088 DialogController dialogs,
Steve McKay0ce04b72017-06-05 13:23:54 -070089 Lookup<String, String> fileTypeLookup,
90 Consumer<Collection<RootInfo>> shortcutsUpdater) {
91 this(features, config, prefs, messages, dialogs, fileTypeLookup,
92 shortcutsUpdater, new Model(features));
Garfield Tane9670332017-03-06 18:33:23 -080093 }
94
95 @VisibleForTesting
96 public Injector(
97 Features features,
98 ActivityConfig config,
99 ScopedPreferences prefs,
100 MessageBuilder messages,
101 DialogController dialogs,
Garfield Tanb47b4b52017-05-16 12:48:53 -0700102 Lookup<String, String> fileTypeLookup,
Steve McKay0ce04b72017-06-05 13:23:54 -0700103 Consumer<Collection<RootInfo>> shortcutsUpdater,
Garfield Tane9670332017-03-06 18:33:23 -0800104 Model model) {
Steve McKay92ae43d2016-11-08 12:06:58 -0800105
Steve McKay98f8c5f2017-03-03 13:52:14 -0800106 this.features = features;
Steve McKay92ae43d2016-11-08 12:06:58 -0800107 this.config = config;
108 this.prefs = prefs;
109 this.messages = messages;
110 this.dialogs = dialogs;
Garfield Tanb47b4b52017-05-16 12:48:53 -0700111 this.fileTypeLookup = fileTypeLookup;
Steve McKay0ce04b72017-06-05 13:23:54 -0700112 this.shortcutsUpdater = shortcutsUpdater;
Garfield Tane9670332017-03-06 18:33:23 -0800113 this.mModel = model;
Jon Manneb1d11b2017-04-01 15:36:59 -0700114 this.debugHelper = new DebugHelper(this);
Steve McKay92ae43d2016-11-08 12:06:58 -0800115 }
116
Jon Mann30d8c792017-02-21 17:44:49 -0800117 public Model getModel() {
118 return mModel;
119 }
120
Steve McKay92ae43d2016-11-08 12:06:58 -0800121 public FocusManager getFocusManager(RecyclerView view, Model model) {
122 assert (focusManager != null);
123 return focusManager.reset(view, model);
124 }
125
Riddle Hsu0c375982018-06-21 22:06:43 +0800126 public void updateSharedSelectionTracker(SelectionTracker<String> selectionTracker) {
127 selectionMgr.reset(selectionTracker);
Steve McKay92ae43d2016-11-08 12:06:58 -0800128 }
129
130 public final ActionModeController getActionModeController(
Ben Lin11a2c132017-05-03 18:46:37 -0700131 SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker) {
132 return actionModeController.reset(selectionDetails, menuItemClicker);
Steve McKay92ae43d2016-11-08 12:06:58 -0800133 }
134
Garfield Tane9670332017-03-06 18:33:23 -0800135 /**
136 * Obtains action handler and resets it if necessary.
Steve McKay88456592017-08-24 10:09:01 -0700137 *
Steve McKay12394522017-08-24 14:14:10 -0700138 * @param contentLock the lock held by
Steve McKay0aa72072017-09-29 09:43:40 -0700139 * {@link com.android.documentsui.selection.BandSelectionHelper} and
140 * {@link com.android.documentsui.selection.GestureSelectionHelper} to prevent
Steve McKay365e3cb2017-08-31 10:27:08 -0700141 * loader from updating result during band/gesture selection. May be {@code null} if
142 * called from {@link com.android.documentsui.sidebar.RootsFragment}.
Garfield Tane9670332017-03-06 18:33:23 -0800143 * @return the action handler
144 */
Steve McKay12394522017-08-24 14:14:10 -0700145 public T getActionHandler(@Nullable ContentLock contentLock) {
Steve McKay92ae43d2016-11-08 12:06:58 -0800146
147 // provide our friend, RootsFragment, early access to this special feature!
Steve McKay12394522017-08-24 14:14:10 -0700148 if (contentLock == null) {
Steve McKay92ae43d2016-11-08 12:06:58 -0800149 return actions;
150 }
151
Steve McKay12394522017-08-24 14:14:10 -0700152 return actions.reset(contentLock);
Steve McKay92ae43d2016-11-08 12:06:58 -0800153 }
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 McKay04718262016-11-08 11:01:35 -0800174}