Avoid potential negative array index access to cached text.
[LibreOffice.git] / android / source / src / java / org / libreoffice / ui / RecentFilesAdapter.java
blobef00b9fb6cfd3889900ea7992bba5333e157d093
1 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 package org.libreoffice.ui;
12 import androidx.core.content.ContextCompat;
13 import androidx.recyclerview.widget.RecyclerView;
14 import android.view.LayoutInflater;
15 import android.view.View;
16 import android.view.ViewGroup;
17 import android.widget.ImageView;
18 import android.widget.TextView;
20 import org.libreoffice.R;
22 import java.util.List;
24 class RecentFilesAdapter extends RecyclerView.Adapter<RecentFilesAdapter.ViewHolder> {
26 private final LibreOfficeUIActivity mActivity;
27 private final List<RecentFile> recentFiles;
29 RecentFilesAdapter(LibreOfficeUIActivity activity, List<RecentFile> recentFiles) {
30 this.mActivity = activity;
31 this.recentFiles = recentFiles;
34 @Override
35 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
36 View item = LayoutInflater.from(parent.getContext())
37 .inflate(R.layout.item_recent_files, parent, false);
38 return new ViewHolder(item);
41 @Override
42 public void onBindViewHolder(ViewHolder holder, int position) {
43 final RecentFile entry = recentFiles.get(position);
45 holder.itemView.setOnClickListener(new View.OnClickListener() {
46 @Override
47 public void onClick(View view) {
48 mActivity.openDocument(entry.getUri());
50 });
52 final String filename = entry.getDisplayName();
53 holder.textView.setText(filename);
55 int compoundDrawableInt = 0;
57 switch (FileUtilities.getType(filename)) {
58 case FileUtilities.DOC:
59 compoundDrawableInt = R.drawable.writer;
60 break;
61 case FileUtilities.CALC:
62 compoundDrawableInt = R.drawable.calc;
63 break;
64 case FileUtilities.DRAWING:
65 compoundDrawableInt = R.drawable.draw;
66 break;
67 case FileUtilities.IMPRESS:
68 compoundDrawableInt = R.drawable.impress;
69 break;
72 // set icon if known filetype was detected
73 if (compoundDrawableInt != 0)
74 holder.imageView.setImageDrawable(ContextCompat.getDrawable(mActivity, compoundDrawableInt));
77 @Override
78 public int getItemCount() {
79 return recentFiles.size();
82 class ViewHolder extends RecyclerView.ViewHolder {
84 TextView textView;
85 ImageView imageView;
87 ViewHolder(View itemView) {
88 super(itemView);
89 this.textView = itemView.findViewById(R.id.textView);
90 this.imageView = itemView.findViewById(R.id.imageView);