• 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.DocsStableIdProvider;
32 import com.android.documentsui.dirlist.DocumentsAdapter;
33 import com.android.documentsui.prefs.ScopedPreferences;
34 import com.android.documentsui.queries.SearchViewManager;
35 import com.android.documentsui.selection.ContentLock;
36 import com.android.documentsui.selection.SelectionHelper;
37 import com.android.documentsui.ui.DialogController;
38 import com.android.documentsui.ui.MessageBuilder;
39 import com.android.internal.annotations.VisibleForTesting;
40 
41 import java.lang.annotation.Retention;
42 import java.lang.annotation.Target;
43 import java.util.Collection;
44 import java.util.function.Consumer;
45 
46 /**
47  * Provides access to runtime dependencies.
48  */
49 public class Injector<T extends ActionHandler> {
50 
51     public final Features features;
52     public final ActivityConfig config;
53     public final ScopedPreferences prefs;
54     public final MessageBuilder messages;
55     public final Lookup<String, String> fileTypeLookup;
56     public final Consumer<Collection<RootInfo>> shortcutsUpdater;
57 
58     public MenuManager menuManager;
59     public DialogController dialogs;
60     public SearchViewManager searchManager;
61 
62     public final DebugHelper debugHelper;
63 
64     @ContentScoped
65     public ActionModeController actionModeController;
66 
67     @ContentScoped
68     public T actions;
69 
70     @ContentScoped
71     public FocusManager focusManager;
72 
73     @ContentScoped
74     public DocsSelectionHelper selectionMgr;
75 
76     private final Model mModel;
77 
78     // must be initialized before calling super.onCreate because prefs
79     // are used in State initialization.
Injector( Features features, ActivityConfig config, ScopedPreferences prefs, MessageBuilder messages, DialogController dialogs, Lookup<String, String> fileTypeLookup, Consumer<Collection<RootInfo>> shortcutsUpdater)80     public Injector(
81             Features features,
82             ActivityConfig config,
83             ScopedPreferences prefs,
84             MessageBuilder messages,
85             DialogController dialogs,
86             Lookup<String, String> fileTypeLookup,
87             Consumer<Collection<RootInfo>> shortcutsUpdater) {
88         this(features, config, prefs, messages, dialogs, fileTypeLookup,
89                 shortcutsUpdater, new Model(features));
90     }
91 
92     @VisibleForTesting
Injector( Features features, ActivityConfig config, ScopedPreferences prefs, MessageBuilder messages, DialogController dialogs, Lookup<String, String> fileTypeLookup, Consumer<Collection<RootInfo>> shortcutsUpdater, Model model)93     public Injector(
94             Features features,
95             ActivityConfig config,
96             ScopedPreferences prefs,
97             MessageBuilder messages,
98             DialogController dialogs,
99             Lookup<String, String> fileTypeLookup,
100             Consumer<Collection<RootInfo>> shortcutsUpdater,
101             Model model) {
102 
103         this.features = features;
104         this.config = config;
105         this.prefs = prefs;
106         this.messages = messages;
107         this.dialogs = dialogs;
108         this.fileTypeLookup = fileTypeLookup;
109         this.shortcutsUpdater = shortcutsUpdater;
110         this.mModel = model;
111         this.debugHelper = new DebugHelper(this);
112     }
113 
getModel()114     public Model getModel() {
115         return mModel;
116     }
117 
getFocusManager(RecyclerView view, Model model)118     public FocusManager getFocusManager(RecyclerView view, Model model) {
119         assert (focusManager != null);
120         return focusManager.reset(view, model);
121     }
122 
getSelectionManager( DocumentsAdapter adapter, SelectionHelper.SelectionPredicate canSetState)123     public SelectionHelper getSelectionManager(
124             DocumentsAdapter adapter, SelectionHelper.SelectionPredicate canSetState) {
125         return selectionMgr.reset(adapter, new DocsStableIdProvider(adapter), canSetState);
126     }
127 
getActionModeController( SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker)128     public final ActionModeController getActionModeController(
129             SelectionDetails selectionDetails, EventHandler<MenuItem> menuItemClicker) {
130         return actionModeController.reset(selectionDetails, menuItemClicker);
131     }
132 
133     /**
134      * Obtains action handler and resets it if necessary.
135      *
136      * @param contentLock the lock held by
137      *            {@link com.android.documentsui.selection.BandSelectionHelper} and
138      *            {@link com.android.documentsui.selection.GestureSelectionHelper} to prevent
139      *            loader from updating result during band/gesture selection. May be {@code null} if
140      *            called from {@link com.android.documentsui.sidebar.RootsFragment}.
141      * @return the action handler
142      */
getActionHandler(@ullable ContentLock contentLock)143     public T getActionHandler(@Nullable ContentLock contentLock) {
144 
145         // provide our friend, RootsFragment, early access to this special feature!
146         if (contentLock == null) {
147             return actions;
148         }
149 
150         return actions.reset(contentLock);
151     }
152 
153     /**
154      * Decorates a field that that is injected.
155      */
156     @Retention(SOURCE)
157     @Target(FIELD)
158     public @interface Injected {
159 
160     }
161 
162     /**
163      * Decorates a field that holds an object that must be reset in the current content scope
164      * (i.e. DirectoryFragment). Fields decorated with this must have an associated
165      * accessor on Injector that, when call, reset the object for the calling context.
166      */
167     @Retention(SOURCE)
168     @Target(FIELD)
169     public @interface ContentScoped {
170 
171     }
172 }
173