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 
17 package com.android.dialer.callcomposer;
18 
19 import android.content.Context;
20 import android.database.Cursor;
21 import android.database.MatrixCursor;
22 import android.database.MergeCursor;
23 import android.support.annotation.NonNull;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.View.OnClickListener;
27 import android.view.ViewGroup;
28 import android.widget.CursorAdapter;
29 import com.android.dialer.common.Assert;
30 import com.android.dialer.common.LogUtil;
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 /** Bridges between the image cursor loaded by GalleryBoundCursorLoader and the GalleryGridView. */
35 public class GalleryGridAdapter extends CursorAdapter {
36 
37   @NonNull private final OnClickListener onClickListener;
38   @NonNull private final List<GalleryGridItemView> views = new ArrayList<>();
39   @NonNull private final Context context;
40 
41   private GalleryGridItemData selectedData;
42 
GalleryGridAdapter( @onNull Context context, Cursor cursor, @NonNull OnClickListener onClickListener)43   public GalleryGridAdapter(
44       @NonNull Context context, Cursor cursor, @NonNull OnClickListener onClickListener) {
45     super(context, cursor, 0);
46     this.onClickListener = Assert.isNotNull(onClickListener);
47     this.context = Assert.isNotNull(context);
48   }
49 
50   @Override
getCount()51   public int getCount() {
52     // Add one for the header.
53     return super.getCount() + 1;
54   }
55 
56   @Override
getView(int position, View convertView, ViewGroup parent)57   public View getView(int position, View convertView, ViewGroup parent) {
58     // At position 0, we want to insert a header. If position == 0, we don't need the cursor.
59     // If position != 0, then we need to move the cursor to position - 1 to account for the offset
60     // of the header.
61     if (position != 0 && !getCursor().moveToPosition(position - 1)) {
62       Assert.fail("couldn't move cursor to position " + (position - 1));
63     }
64     View view;
65     if (convertView == null) {
66       view = newView(context, getCursor(), parent);
67     } else {
68       view = convertView;
69     }
70     bindView(view, context, getCursor(), position);
71     return view;
72   }
73 
bindView(View view, Context context, Cursor cursor, int position)74   private void bindView(View view, Context context, Cursor cursor, int position) {
75     if (position == 0) {
76       GalleryGridItemView gridView = (GalleryGridItemView) view;
77       gridView.showGallery(true);
78     } else {
79       bindView(view, context, cursor);
80     }
81   }
82 
83   @Override
bindView(View view, Context context, Cursor cursor)84   public void bindView(View view, Context context, Cursor cursor) {
85     GalleryGridItemView gridView = (GalleryGridItemView) view;
86     gridView.bind(cursor);
87     gridView.setSelected(gridView.getData().equals(selectedData));
88   }
89 
90   @Override
newView(Context context, Cursor cursor, ViewGroup parent)91   public View newView(Context context, Cursor cursor, ViewGroup parent) {
92     GalleryGridItemView view =
93         (GalleryGridItemView)
94             LayoutInflater.from(context).inflate(R.layout.gallery_grid_item_view, parent, false);
95     view.setOnClickListener(onClickListener);
96     views.add(view);
97     return view;
98   }
99 
setSelected(GalleryGridItemData selectedData)100   public void setSelected(GalleryGridItemData selectedData) {
101     this.selectedData = selectedData;
102     for (GalleryGridItemView view : views) {
103       view.setSelected(view.getData().equals(selectedData));
104     }
105   }
106 
insertEntries(@onNull List<GalleryGridItemData> entries)107   public void insertEntries(@NonNull List<GalleryGridItemData> entries) {
108     Assert.checkArgument(entries.size() != 0);
109     LogUtil.i("GalleryGridAdapter.insertRows", "inserting %d rows", entries.size());
110     MatrixCursor extraRow = new MatrixCursor(GalleryGridItemData.IMAGE_PROJECTION);
111     for (GalleryGridItemData entry : entries) {
112       extraRow.addRow(new Object[] {0L, entry.getFilePath(), entry.getMimeType(), ""});
113     }
114     extraRow.moveToFirst();
115     Cursor extendedCursor = new MergeCursor(new Cursor[] {extraRow, getCursor()});
116     swapCursor(extendedCursor);
117   }
118 
insertEntry(String filePath, String mimeType)119   public GalleryGridItemData insertEntry(String filePath, String mimeType) {
120     LogUtil.i("GalleryGridAdapter.insertRow", mimeType + " " + filePath);
121 
122     MatrixCursor extraRow = new MatrixCursor(GalleryGridItemData.IMAGE_PROJECTION);
123     extraRow.addRow(new Object[] {0L, filePath, mimeType, ""});
124     extraRow.moveToFirst();
125     Cursor extendedCursor = new MergeCursor(new Cursor[] {extraRow, getCursor()});
126     swapCursor(extendedCursor);
127 
128     return new GalleryGridItemData(extraRow);
129   }
130 }
131