blob: 30d1d4b77481ad02b054fb002da678417bc776b5 [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.support.annotation.Nullable;
22import android.support.v7.widget.RecyclerView;
23import android.view.MenuItem;
Steve McKay04718262016-11-08 11:01:35 -080024
25import com.android.documentsui.MenuManager.SelectionDetails;
Steve McKay0ce04b72017-06-05 13:23:54 -070026import com.android.documentsui.base.DebugHelper;
Steve McKay04718262016-11-08 11:01:35 -080027import com.android.documentsui.base.EventHandler;
Steve McKay98f8c5f2017-03-03 13:52:14 -080028import com.android.documentsui.base.Features;
Garfield Tanb47b4b52017-05-16 12:48:53 -070029import com.android.documentsui.base.Lookup;
Steve McKay0ce04b72017-06-05 13:23:54 -070030import com.android.documentsui.base.RootInfo;
Steve McKay04718262016-11-08 11:01:35 -080031import com.android.documentsui.dirlist.DocumentsAdapter;
Steve McKaye8b547f2016-12-19 14:42:55 -080032import com.android.documentsui.prefs.ScopedPreferences;
Garfield Tane9670332017-03-06 18:33:23 -080033import com.android.documentsui.queries.SearchViewManager;
Steve McKay04718262016-11-08 11:01:35 -080034import com.android.documentsui.selection.SelectionManager;
35import com.android.documentsui.selection.SelectionManager.SelectionPredicate;
36import com.android.documentsui.ui.DialogController;
Steve McKay92ae43d2016-11-08 12:06:58 -080037import com.android.documentsui.ui.MessageBuilder;
Garfield Tane9670332017-03-06 18:33:23 -080038import com.android.internal.annotations.VisibleForTesting;
Steve McKay92ae43d2016-11-08 12:06:58 -080039
40import java.lang.annotation.Retention;
41import java.lang.annotation.Target;
Steve McKay0ce04b72017-06-05 13:23:54 -070042import java.util.Collection;
43import java.util.function.Consumer;
Steve McKay04718262016-11-08 11:01:35 -080044
45/**
46 * Provides access to runtime dependencies.
47 */
Steve McKay92ae43d2016-11-08 12:06:58 -080048public class Injector<T extends ActionHandler> {
Steve McKay04718262016-11-08 11:01:35 -080049
Steve McKay98f8c5f2017-03-03 13:52:14 -080050 public final Features features;
Steve McKay92ae43d2016-11-08 12:06:58 -080051 public final ActivityConfig config;
52 public final ScopedPreferences prefs;
53 public final MessageBuilder messages;
Garfield Tanb47b4b52017-05-16 12:48:53 -070054 public final Lookup<String, String> fileTypeLookup;
Steve McKay0ce04b72017-06-05 13:23:54 -070055 public final Consumer<Collection<RootInfo>> shortcutsUpdater;
Steve McKay92ae43d2016-11-08 12:06:58 -080056
57 public MenuManager menuManager;
58 public DialogController dialogs;
Garfield Tane9670332017-03-06 18:33:23 -080059 public SearchViewManager searchManager;
Steve McKay92ae43d2016-11-08 12:06:58 -080060
Garfield Tanda2c0f02017-04-11 13:47:58 -070061 public final DebugHelper debugHelper;
62
Steve McKay92ae43d2016-11-08 12:06:58 -080063 @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 McKay98f8c5f2017-03-03 13:52:14 -080075 private final Model mModel;
Jon Mann30d8c792017-02-21 17:44:49 -080076
Steve McKay92ae43d2016-11-08 12:06:58 -080077 // must be initialized before calling super.onCreate because prefs
78 // are used in State initialization.
79 public Injector(
Steve McKay98f8c5f2017-03-03 13:52:14 -080080 Features features,
Steve McKay92ae43d2016-11-08 12:06:58 -080081 ActivityConfig config,
82 ScopedPreferences prefs,
83 MessageBuilder messages,
Garfield Tanb47b4b52017-05-16 12:48:53 -070084 DialogController dialogs,
Steve McKay0ce04b72017-06-05 13:23:54 -070085 Lookup<String, String> fileTypeLookup,
86 Consumer<Collection<RootInfo>> shortcutsUpdater) {
87 this(features, config, prefs, messages, dialogs, fileTypeLookup,
88 shortcutsUpdater, new Model(features));
Garfield Tane9670332017-03-06 18:33:23 -080089 }
90
91 @VisibleForTesting
92 public Injector(
93 Features features,
94 ActivityConfig config,
95 ScopedPreferences prefs,
96 MessageBuilder messages,
97 DialogController dialogs,
Garfield Tanb47b4b52017-05-16 12:48:53 -070098 Lookup<String, String> fileTypeLookup,
Steve McKay0ce04b72017-06-05 13:23:54 -070099 Consumer<Collection<RootInfo>> shortcutsUpdater,
Garfield Tane9670332017-03-06 18:33:23 -0800100 Model model) {
Steve McKay92ae43d2016-11-08 12:06:58 -0800101
Steve McKay98f8c5f2017-03-03 13:52:14 -0800102 this.features = features;
Steve McKay92ae43d2016-11-08 12:06:58 -0800103 this.config = config;
104 this.prefs = prefs;
105 this.messages = messages;
106 this.dialogs = dialogs;
Garfield Tanb47b4b52017-05-16 12:48:53 -0700107 this.fileTypeLookup = fileTypeLookup;
Steve McKay0ce04b72017-06-05 13:23:54 -0700108 this.shortcutsUpdater = shortcutsUpdater;
Garfield Tane9670332017-03-06 18:33:23 -0800109 this.mModel = model;
Jon Manneb1d11b2017-04-01 15:36:59 -0700110 this.debugHelper = new DebugHelper(this);
Steve McKay92ae43d2016-11-08 12:06:58 -0800111 }
112
Jon Mann30d8c792017-02-21 17:44:49 -0800113 public Model getModel() {
114 return mModel;
115 }
116
Steve McKay92ae43d2016-11-08 12:06:58 -0800117 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 Lin11a2c132017-05-03 18:46:37 -0700128 SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker) {
129 return actionModeController.reset(selectionDetails, menuItemClicker);
Steve McKay92ae43d2016-11-08 12:06:58 -0800130 }
131
Garfield Tane9670332017-03-06 18:33:23 -0800132 /**
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 McKay92ae43d2016-11-08 12:06:58 -0800141
142 // provide our friend, RootsFragment, early access to this special feature!
Garfield Tane9670332017-03-06 18:33:23 -0800143 if (reloadLock == null) {
Steve McKay92ae43d2016-11-08 12:06:58 -0800144 return actions;
145 }
146
Garfield Tane9670332017-03-06 18:33:23 -0800147 return actions.reset(reloadLock);
Steve McKay92ae43d2016-11-08 12:06:58 -0800148 }
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 McKay04718262016-11-08 11:01:35 -0800169}