blob: aa3d43c32672306eb95a637f06e7a3e8f572c6db [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;
24import android.view.View;
25
26import com.android.documentsui.MenuManager.SelectionDetails;
27import com.android.documentsui.base.EventHandler;
Steve McKay98f8c5f2017-03-03 13:52:14 -080028import com.android.documentsui.base.Features;
Steve McKay04718262016-11-08 11:01:35 -080029import com.android.documentsui.dirlist.DocumentsAdapter;
Jon Manneb1d11b2017-04-01 15:36:59 -070030import com.android.documentsui.base.DebugHelper;
Steve McKaye8b547f2016-12-19 14:42:55 -080031import com.android.documentsui.prefs.ScopedPreferences;
Garfield Tane9670332017-03-06 18:33:23 -080032import com.android.documentsui.queries.SearchViewManager;
Steve McKay04718262016-11-08 11:01:35 -080033import com.android.documentsui.selection.SelectionManager;
34import com.android.documentsui.selection.SelectionManager.SelectionPredicate;
35import com.android.documentsui.ui.DialogController;
Steve McKay92ae43d2016-11-08 12:06:58 -080036import com.android.documentsui.ui.MessageBuilder;
Garfield Tane9670332017-03-06 18:33:23 -080037import com.android.internal.annotations.VisibleForTesting;
Steve McKay92ae43d2016-11-08 12:06:58 -080038
39import java.lang.annotation.Retention;
40import java.lang.annotation.Target;
Steve McKay04718262016-11-08 11:01:35 -080041
42/**
43 * Provides access to runtime dependencies.
44 */
Steve McKay92ae43d2016-11-08 12:06:58 -080045public class Injector<T extends ActionHandler> {
Steve McKay04718262016-11-08 11:01:35 -080046
Steve McKay98f8c5f2017-03-03 13:52:14 -080047 public final Features features;
Steve McKay92ae43d2016-11-08 12:06:58 -080048 public final ActivityConfig config;
49 public final ScopedPreferences prefs;
50 public final MessageBuilder messages;
51
52 public MenuManager menuManager;
53 public DialogController dialogs;
Garfield Tane9670332017-03-06 18:33:23 -080054 public SearchViewManager searchManager;
Steve McKay92ae43d2016-11-08 12:06:58 -080055
Garfield Tanda2c0f02017-04-11 13:47:58 -070056 public final DebugHelper debugHelper;
57
Steve McKay92ae43d2016-11-08 12:06:58 -080058 @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 McKay98f8c5f2017-03-03 13:52:14 -080070 private final Model mModel;
Jon Mann30d8c792017-02-21 17:44:49 -080071
Steve McKay92ae43d2016-11-08 12:06:58 -080072 // must be initialized before calling super.onCreate because prefs
73 // are used in State initialization.
74 public Injector(
Steve McKay98f8c5f2017-03-03 13:52:14 -080075 Features features,
Steve McKay92ae43d2016-11-08 12:06:58 -080076 ActivityConfig config,
77 ScopedPreferences prefs,
78 MessageBuilder messages,
79 DialogController dialogs) {
Garfield Tane9670332017-03-06 18:33:23 -080080 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 McKay92ae43d2016-11-08 12:06:58 -080091
Steve McKay98f8c5f2017-03-03 13:52:14 -080092 this.features = features;
Steve McKay92ae43d2016-11-08 12:06:58 -080093 this.config = config;
94 this.prefs = prefs;
95 this.messages = messages;
96 this.dialogs = dialogs;
Garfield Tane9670332017-03-06 18:33:23 -080097 this.mModel = model;
Jon Manneb1d11b2017-04-01 15:36:59 -070098 this.debugHelper = new DebugHelper(this);
Steve McKay92ae43d2016-11-08 12:06:58 -080099 }
100
Jon Mann30d8c792017-02-21 17:44:49 -0800101 public Model getModel() {
102 return mModel;
103 }
104
Steve McKay92ae43d2016-11-08 12:06:58 -0800105 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 Tane9670332017-03-06 18:33:23 -0800120 /**
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 McKay92ae43d2016-11-08 12:06:58 -0800129
130 // provide our friend, RootsFragment, early access to this special feature!
Garfield Tane9670332017-03-06 18:33:23 -0800131 if (reloadLock == null) {
Steve McKay92ae43d2016-11-08 12:06:58 -0800132 return actions;
133 }
134
Garfield Tane9670332017-03-06 18:33:23 -0800135 return actions.reset(reloadLock);
Steve McKay92ae43d2016-11-08 12:06:58 -0800136 }
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 McKay04718262016-11-08 11:01:35 -0800157}