1 /* 2 * Copyright (C) 2018 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.example.sampleleanbacklauncher.notifications; 18 19 import android.app.NotificationManager; 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.database.DatabaseUtils; 23 import androidx.recyclerview.widget.RecyclerView; 24 import android.text.TextUtils; 25 import android.util.Log; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 30 import com.example.sampleleanbacklauncher.R; 31 32 /** 33 * Adapter for the {@link RecyclerView} in the notifications side panel which displayed 34 * both dismissible and non-dismissible notifications. 35 */ 36 public class NotificationsPanelAdapter<VH extends RecyclerView.ViewHolder> 37 extends RecyclerView.Adapter<NotificationsPanelAdapter.NotificationPanelViewHolder> { 38 private static final String TAG = "NotifsPanelAdapter"; 39 private static final boolean DEBUG = false; 40 41 private static final int TYPE_DISMISSIBLE = 0; 42 private static final int TYPE_PERSISTENT = 1; 43 44 private Cursor mCursor; 45 NotificationsPanelAdapter(Context context, Cursor cursor)46 public NotificationsPanelAdapter(Context context, Cursor cursor) { 47 mCursor = cursor; 48 setHasStableIds(true); 49 } 50 getCursor()51 public Cursor getCursor() { 52 return mCursor; 53 } 54 55 @Override getItemCount()56 public int getItemCount() { 57 if (mCursor != null) { 58 return mCursor.getCount(); 59 } 60 return 0; 61 } 62 63 @Override onCreateViewHolder( ViewGroup parent, int viewType)64 public NotificationPanelViewHolder onCreateViewHolder( 65 ViewGroup parent, int viewType) { 66 LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 67 68 View trayItem; 69 if (viewType == TYPE_DISMISSIBLE) { 70 trayItem = inflater.inflate(R.layout.notification_panel_item_dismissible, 71 parent, false); 72 } else { 73 trayItem = inflater.inflate(R.layout.notification_panel_item, 74 parent, false); 75 } 76 77 return new NotificationPanelViewHolder(trayItem); 78 } 79 80 @Override onBindViewHolder(NotificationPanelViewHolder holder, int position)81 public void onBindViewHolder(NotificationPanelViewHolder holder, 82 int position) { 83 if (!mCursor.moveToPosition(position)) { 84 throw new IllegalStateException("Can't move cursor to position " + position); 85 } 86 onBindViewHolder(holder, mCursor); 87 } 88 89 @Override getItemViewType(int position)90 public int getItemViewType(int position) { 91 if (!mCursor.moveToPosition(position)) { 92 throw new IllegalStateException("Can't move cursor to position " + position); 93 } 94 95 boolean dismissible = mCursor.getInt(TvNotification.COLUMN_INDEX_DISMISSIBLE) != 0; 96 boolean ongoing = mCursor.getInt(TvNotification.COLUMN_INDEX_ONGOING) != 0; 97 if (ongoing || !dismissible) { 98 return TYPE_PERSISTENT; 99 } else { 100 return TYPE_DISMISSIBLE; 101 } 102 } 103 104 @Override getItemId(int position)105 public long getItemId(int position) { 106 if (!mCursor.moveToPosition(position)) { 107 Log.wtf(TAG, "Can't move cursor to position " + position); 108 return View.NO_ID; 109 } 110 111 String key = mCursor.getString(TvNotification.COLUMN_INDEX_KEY); 112 return key.hashCode(); 113 } 114 onBindViewHolder(NotificationPanelViewHolder viewHolder, Cursor cursor)115 public void onBindViewHolder(NotificationPanelViewHolder viewHolder, Cursor cursor) { 116 TvNotification notif = TvNotification.fromCursor(cursor); 117 viewHolder.setNotification(notif); 118 } 119 120 public static class NotificationPanelViewHolder extends RecyclerView.ViewHolder { NotificationPanelViewHolder(View itemView)121 public NotificationPanelViewHolder(View itemView) { 122 super(itemView); 123 } 124 setNotification(TvNotification notification)125 public void setNotification(TvNotification notification) { 126 ((NotificationPanelItemView) itemView).setNotification(notification); 127 } 128 } 129 130 /** 131 * Swap in a new Cursor, and close the old Cursor. 132 * 133 * @param newCursor The new cursor to be used. 134 */ changeCursor(Cursor newCursor)135 public void changeCursor(Cursor newCursor) { 136 if (DEBUG) { 137 Log.d(TAG, "changeCursor() called with: " + "newCursor = [" + 138 DatabaseUtils.dumpCursorToString(newCursor) + "]"); 139 } 140 141 mCursor = newCursor; 142 notifyDataSetChanged(); 143 } 144 }