blob: 2967a908678d7549fdfeaa0b22e01f90b177bc15 [file] [log] [blame]
Ben Kwad8391492015-12-17 10:37:00 -08001/*
2 * Copyright (C) 2015 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
17package com.android.documentsui.dirlist;
18
Ben Kwa0436a752016-01-15 10:43:24 -080019import static com.android.internal.util.Preconditions.checkNotNull;
Ben Kwad8391492015-12-17 10:37:00 -080020import static com.android.internal.util.Preconditions.checkState;
21
22import android.content.Context;
23import android.database.Cursor;
Ben Kwa0436a752016-01-15 10:43:24 -080024import android.graphics.Rect;
Ben Kwad8391492015-12-17 10:37:00 -080025import android.support.annotation.Nullable;
26import android.support.v7.widget.RecyclerView;
27import android.view.KeyEvent;
28import android.view.LayoutInflater;
Ben Kwa0436a752016-01-15 10:43:24 -080029import android.view.MotionEvent;
Ben Kwad8391492015-12-17 10:37:00 -080030import android.view.View;
31import android.view.ViewGroup;
32
Ben Kwa0436a752016-01-15 10:43:24 -080033import com.android.documentsui.Events;
Ben Kwad8391492015-12-17 10:37:00 -080034import com.android.documentsui.R;
35import com.android.documentsui.State;
36
37public abstract class DocumentHolder
38 extends RecyclerView.ViewHolder
39 implements View.OnKeyListener {
40
41 public @Nullable String modelId;
42
43 final int mSelectedItemColor;
44 final int mDefaultItemColor;
45 final boolean mAlwaysShowSummary;
46 final Context mContext;
Ben Kwad8391492015-12-17 10:37:00 -080047
Ben Kwa0436a752016-01-15 10:43:24 -080048 DocumentHolder.EventListener mEventListener;
Ben Kwad8391492015-12-17 10:37:00 -080049 private View.OnKeyListener mKeyListener;
Ben Kwa0436a752016-01-15 10:43:24 -080050 private View mSelectionHotspot;
Ben Kwad8391492015-12-17 10:37:00 -080051
Ben Kwaf8a51f62015-12-22 14:03:30 -080052 public DocumentHolder(Context context, ViewGroup parent, int layout) {
53 this(context, inflateLayout(context, parent, layout));
Ben Kwad8391492015-12-17 10:37:00 -080054 }
55
Ben Kwaf8a51f62015-12-22 14:03:30 -080056 public DocumentHolder(Context context, View item) {
Ben Kwad8391492015-12-17 10:37:00 -080057 super(item);
58
59 itemView.setOnKeyListener(this);
60
61 mContext = context;
62
63 mDefaultItemColor = context.getColor(R.color.item_doc_background);
64 mSelectedItemColor = context.getColor(R.color.item_doc_background_selected);
65 mAlwaysShowSummary = context.getResources().getBoolean(R.bool.always_show_summary);
Ben Kwa0436a752016-01-15 10:43:24 -080066
67 mSelectionHotspot = itemView.findViewById(R.id.icon_check);
Ben Kwad8391492015-12-17 10:37:00 -080068 }
69
70 /**
71 * Binds the view to the given item data.
72 * @param cursor
73 * @param modelId
74 * @param state
75 */
76 public abstract void bind(Cursor cursor, String modelId, State state);
77
Ben Kwae3dfebf2016-02-18 16:45:45 -080078 /**
79 * Makes the associated item view appear selected. Note that this merely affects the appearance
80 * of the view, it doesn't actually select the item.
81 *
82 * @param selected
83 */
Ben Kwad8391492015-12-17 10:37:00 -080084 public void setSelected(boolean selected) {
85 itemView.setActivated(selected);
86 itemView.setBackgroundColor(selected ? mSelectedItemColor : mDefaultItemColor);
87 }
88
Ben Kwae3dfebf2016-02-18 16:45:45 -080089 /**
90 * Highlights the associated item view.
91 * @param highlighted
92 */
93 public void setHighlighted(boolean highlighted) {
94 itemView.setBackgroundColor(highlighted ? mSelectedItemColor : mDefaultItemColor);
95 }
96
Ben Kwad8391492015-12-17 10:37:00 -080097 @Override
98 public boolean onKey(View v, int keyCode, KeyEvent event) {
Ben Kwa0436a752016-01-15 10:43:24 -080099 // Event listener should always be set.
100 checkNotNull(mEventListener);
Ben Kwa67924892016-01-27 09:58:36 -0800101 return mEventListener.onKey(this, keyCode, event);
Ben Kwad8391492015-12-17 10:37:00 -0800102 }
103
Ben Kwa0436a752016-01-15 10:43:24 -0800104 public void addEventListener(DocumentHolder.EventListener listener) {
Ben Kwad8391492015-12-17 10:37:00 -0800105 // Just handle one for now; switch to a list if necessary.
Ben Kwa0436a752016-01-15 10:43:24 -0800106 checkState(mEventListener == null);
107 mEventListener = listener;
Ben Kwad8391492015-12-17 10:37:00 -0800108 }
109
110 public void addOnKeyListener(View.OnKeyListener listener) {
111 // Just handle one for now; switch to a list if necessary.
112 checkState(mKeyListener == null);
113 mKeyListener = listener;
114 }
115
116 public void setEnabled(boolean enabled) {
117 setEnabledRecursive(itemView, enabled);
118 }
119
Ben Kwa0436a752016-01-15 10:43:24 -0800120 public boolean onSingleTapUp(MotionEvent event) {
121 if (Events.isMouseEvent(event)) {
122 // Mouse clicks select.
123 // TODO: && input.isPrimaryButtonPressed(), but it is returning false.
124 if (mEventListener != null) {
125 return mEventListener.onSelect(this);
126 }
127 } else if (Events.isTouchEvent(event)) {
128 // Touch events select if they occur in the selection hotspot, otherwise they activate.
129 if (mEventListener == null) {
130 return false;
131 }
132
133 // Do everything in global coordinates - it makes things simpler.
134 Rect rect = new Rect();
135 mSelectionHotspot.getGlobalVisibleRect(rect);
136
137 // If the tap occurred within the icon rect, consider it a selection.
138 if (rect.contains((int)event.getRawX(), (int)event.getRawY())) {
139 return mEventListener.onSelect(this);
140 } else {
141 return mEventListener.onActivate(this);
142 }
143 }
144 return false;
145 }
146
Ben Kwad8391492015-12-17 10:37:00 -0800147 static void setEnabledRecursive(View itemView, boolean enabled) {
148 if (itemView == null) return;
149 if (itemView.isEnabled() == enabled) return;
150 itemView.setEnabled(enabled);
151
152 if (itemView instanceof ViewGroup) {
153 final ViewGroup vg = (ViewGroup) itemView;
154 for (int i = vg.getChildCount() - 1; i >= 0; i--) {
155 setEnabledRecursive(vg.getChildAt(i), enabled);
156 }
157 }
158 }
159
160 private static View inflateLayout(Context context, ViewGroup parent, int layout) {
161 final LayoutInflater inflater = LayoutInflater.from(context);
162 return inflater.inflate(layout, parent, false);
163 }
164
Ben Kwa0436a752016-01-15 10:43:24 -0800165 /**
166 * Implement this in order to be able to respond to events coming from DocumentHolders.
167 */
168 interface EventListener {
169 /**
Ben Kwa67924892016-01-27 09:58:36 -0800170 * Handles activation events on the document holder.
171 *
Ben Kwa0436a752016-01-15 10:43:24 -0800172 * @param doc The target DocumentHolder
173 * @return Whether the event was handled.
174 */
175 public boolean onActivate(DocumentHolder doc);
176
177 /**
Ben Kwa67924892016-01-27 09:58:36 -0800178 * Handles selection events on the document holder.
179 *
Ben Kwa0436a752016-01-15 10:43:24 -0800180 * @param doc The target DocumentHolder
181 * @return Whether the event was handled.
182 */
183 public boolean onSelect(DocumentHolder doc);
Ben Kwa67924892016-01-27 09:58:36 -0800184
185 /**
186 * Handles key events on the document holder.
187 *
188 * @param doc The target DocumentHolder.
189 * @param keyCode Key code for the event.
190 * @param event KeyEvent for the event.
191 * @return Whether the event was handled.
192 */
193 public boolean onKey(DocumentHolder doc, int keyCode, KeyEvent event);
Ben Kwad8391492015-12-17 10:37:00 -0800194 }
195}