Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui.dirlist; |
| 18 | |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkNotNull; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 20 | import static com.android.internal.util.Preconditions.checkState; |
| 21 | |
| 22 | import android.content.Context; |
| 23 | import android.database.Cursor; |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 24 | import android.graphics.Rect; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 25 | import android.support.annotation.Nullable; |
| 26 | import android.support.v7.widget.RecyclerView; |
| 27 | import android.view.KeyEvent; |
| 28 | import android.view.LayoutInflater; |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 29 | import android.view.MotionEvent; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
| 32 | |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 33 | import com.android.documentsui.Events; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 34 | import com.android.documentsui.R; |
| 35 | import com.android.documentsui.State; |
| 36 | |
| 37 | public 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 Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 47 | |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 48 | DocumentHolder.EventListener mEventListener; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 49 | private View.OnKeyListener mKeyListener; |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 50 | private View mSelectionHotspot; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 51 | |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 52 | public DocumentHolder(Context context, ViewGroup parent, int layout) { |
| 53 | this(context, inflateLayout(context, parent, layout)); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Ben Kwa | f8a51f6 | 2015-12-22 14:03:30 -0800 | [diff] [blame] | 56 | public DocumentHolder(Context context, View item) { |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 57 | 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 Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 66 | |
| 67 | mSelectionHotspot = itemView.findViewById(R.id.icon_check); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 68 | } |
| 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 Kwa | e3dfebf | 2016-02-18 16:45:45 -0800 | [diff] [blame] | 78 | /** |
| 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 Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 84 | public void setSelected(boolean selected) { |
| 85 | itemView.setActivated(selected); |
| 86 | itemView.setBackgroundColor(selected ? mSelectedItemColor : mDefaultItemColor); |
| 87 | } |
| 88 | |
Ben Kwa | e3dfebf | 2016-02-18 16:45:45 -0800 | [diff] [blame] | 89 | /** |
| 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 Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 97 | @Override |
| 98 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 99 | // Event listener should always be set. |
| 100 | checkNotNull(mEventListener); |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 101 | return mEventListener.onKey(this, keyCode, event); |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 104 | public void addEventListener(DocumentHolder.EventListener listener) { |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 105 | // Just handle one for now; switch to a list if necessary. |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 106 | checkState(mEventListener == null); |
| 107 | mEventListener = listener; |
Ben Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 108 | } |
| 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 Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 120 | 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 Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 147 | 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 Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Implement this in order to be able to respond to events coming from DocumentHolders. |
| 167 | */ |
| 168 | interface EventListener { |
| 169 | /** |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 170 | * Handles activation events on the document holder. |
| 171 | * |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 172 | * @param doc The target DocumentHolder |
| 173 | * @return Whether the event was handled. |
| 174 | */ |
| 175 | public boolean onActivate(DocumentHolder doc); |
| 176 | |
| 177 | /** |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 178 | * Handles selection events on the document holder. |
| 179 | * |
Ben Kwa | 0436a75 | 2016-01-15 10:43:24 -0800 | [diff] [blame] | 180 | * @param doc The target DocumentHolder |
| 181 | * @return Whether the event was handled. |
| 182 | */ |
| 183 | public boolean onSelect(DocumentHolder doc); |
Ben Kwa | 6792489 | 2016-01-27 09:58:36 -0800 | [diff] [blame] | 184 | |
| 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 Kwa | d839149 | 2015-12-17 10:37:00 -0800 | [diff] [blame] | 194 | } |
| 195 | } |