• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 import static java.lang.annotation.ElementType.FIELD;
19 import static java.lang.annotation.RetentionPolicy.SOURCE;
20 
21 import android.support.annotation.Nullable;
22 import android.support.v7.widget.RecyclerView;
23 import android.view.MenuItem;
24 
25 import com.android.documentsui.MenuManager.SelectionDetails;
26 import com.android.documentsui.base.DebugHelper;
27 import com.android.documentsui.base.EventHandler;
28 import com.android.documentsui.base.Features;
29 import com.android.documentsui.base.Lookup;
30 import com.android.documentsui.base.RootInfo;
31 import com.android.documentsui.dirlist.DocumentsAdapter;
32 import com.android.documentsui.prefs.ScopedPreferences;
33 import com.android.documentsui.queries.SearchViewManager;
34 import com.android.documentsui.selection.SelectionManager;
35 import com.android.documentsui.selection.SelectionManager.SelectionPredicate;
36 import com.android.documentsui.ui.DialogController;
37 import com.android.documentsui.ui.MessageBuilder;
38 import com.android.internal.annotations.VisibleForTesting;
39 
40 import java.lang.annotation.Retention;
41 import java.lang.annotation.Target;
42 import java.util.Collection;
43 import java.util.function.Consumer;
44 
45 /**
46  * Provides access to runtime dependencies.
47  */
48 public class Injector<T extends ActionHandler> {
49 
50     public final Features features;
51     public final ActivityConfig config;
52     public final ScopedPreferences prefs;
53     public final MessageBuilder messages;
54     public final Lookup<String, String> fileTypeLookup;
55     public final Consumer<Collection<RootInfo>> shortcutsUpdater;
56 
57     public MenuManager menuManager;
58     public DialogController dialogs;
59     public SearchViewManager searchManager;
60 
61     public final DebugHelper debugHelper;
62 
63     @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 
75     private final Model mModel;
76 
77     // must be initialized before calling super.onCreate because prefs
78     // are used in State initialization.
Injector( Features features, ActivityConfig config, ScopedPreferences prefs, MessageBuilder messages, DialogController dialogs, Lookup<String, String> fileTypeLookup, Consumer<Collection<RootInfo>> shortcutsUpdater)79     public Injector(
80             Features features,
81             ActivityConfig config,
82             ScopedPreferences prefs,
83             MessageBuilder messages,
84             DialogController dialogs,
85             Lookup<String, String> fileTypeLookup,
86             Consumer<Collection<RootInfo>> shortcutsUpdater) {
87         this(features, config, prefs, messages, dialogs, fileTypeLookup,
88                 shortcutsUpdater, new Model(features));
89     }
90 
91     @VisibleForTesting
Injector( Features features, ActivityConfig config, ScopedPreferences prefs, MessageBuilder messages, DialogController dialogs, Lookup<String, String> fileTypeLookup, Consumer<Collection<RootInfo>> shortcutsUpdater, Model model)92     public Injector(
93             Features features,
94             ActivityConfig config,
95             ScopedPreferences prefs,
96             MessageBuilder messages,
97             DialogController dialogs,
98             Lookup<String, String> fileTypeLookup,
99             Consumer<Collection<RootInfo>> shortcutsUpdater,
100             Model model) {
101 
102         this.features = features;
103         this.config = config;
104         this.prefs = prefs;
105         this.messages = messages;
106         this.dialogs = dialogs;
107         this.fileTypeLookup = fileTypeLookup;
108         this.shortcutsUpdater = shortcutsUpdater;
109         this.mModel = model;
110         this.debugHelper = new DebugHelper(this);
111     }
112 
getModel()113     public Model getModel() {
114         return mModel;
115     }
116 
getFocusManager(RecyclerView view, Model model)117     public FocusManager getFocusManager(RecyclerView view, Model model) {
118         assert (focusManager != null);
119         return focusManager.reset(view, model);
120     }
121 
getSelectionManager( DocumentsAdapter adapter, SelectionPredicate canSetState)122     public SelectionManager getSelectionManager(
123             DocumentsAdapter adapter, SelectionPredicate canSetState) {
124         return selectionMgr.reset(adapter, canSetState);
125     }
126 
getActionModeController( SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker)127     public final ActionModeController getActionModeController(
128             SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker) {
129         return actionModeController.reset(selectionDetails, menuItemClicker);
130     }
131 
132     /**
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      */
getActionHandler(@ullable DirectoryReloadLock reloadLock)140     public T getActionHandler(@Nullable DirectoryReloadLock reloadLock) {
141 
142         // provide our friend, RootsFragment, early access to this special feature!
143         if (reloadLock == null) {
144             return actions;
145         }
146 
147         return actions.reset(reloadLock);
148     }
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     }
169 }
170