private View getDirentView(SeafDirent dirent, View convertView, ViewGroup parent, int position) { View view = convertView; Viewholder viewHolder; if (convertView == null) { view = LayoutInflater.from(mActivity).inflate(R.layout.list_item_entry, null); TextView title = (TextView) view.findViewById(R.id.list_item_title); TextView subtitle = (TextView) view.findViewById(R.id.list_item_subtitle); ImageView icon = (ImageView) view.findViewById(R.id.list_item_icon); ImageView action = (ImageView) view.findViewById(R.id.list_item_action); viewHolder = new Viewholder(title, subtitle, icon, action); view.setTag(viewHolder); } else { viewHolder = (Viewholder) convertView.getTag(); } viewHolder.title.setText(dirent.getTitle()); if (dirent.isDir()) { viewHolder.subtitle.setText(""); viewHolder.icon.setImageResource(dirent.getIcon()); viewHolder.action.setVisibility(View.VISIBLE); setDirAction(dirent, viewHolder, position); } else { setFileView(dirent, viewHolder, position); } return view; }
private void setFileView(SeafDirent dirent, Viewholder viewHolder, int position) { NavContext nav = mActivity.getNavContext(); DataManager dataManager = mActivity.getDataManager(); String repoName = nav.getRepoName(); String repoID = nav.getRepoID(); String filePath = Utils.pathJoin(nav.getDirPath(), dirent.name); File file = dataManager.getLocalRepoFile(repoName, repoID, filePath); boolean cacheExists = false; if (file.exists()) { SeafCachedFile cf = dataManager.getCachedFile(repoName, repoID, filePath); String subtitle = null; long size = file.length(); if (size == dirent.size) { subtitle = dirent.getSubtitle(); } else { subtitle = Utils.readableFileSize(size) + ", modified"; } if (cf != null) { cacheExists = true; if (dirent.id.equals(cf.fileID) && dirent.size == size) { subtitle += ", cached"; } } viewHolder.subtitle.setText(subtitle); if (Utils.isViewableImage(file.getName())) { setImageThumbNail(file, dirent, dataManager, viewHolder); } else viewHolder.icon.setImageResource(dirent.getIcon()); } else { viewHolder.subtitle.setText(dirent.getSubtitle()); viewHolder.icon.setImageResource(dirent.getIcon()); } setFileAction(dirent, viewHolder, position, cacheExists); }
private void setImageThumbNail( File file, SeafDirent dirent, DataManager dataManager, Viewholder viewHolder) { if (file.length() < DataManager.MAX_DIRECT_SHOW_THUMB) { Bitmap imageBitmap = dataManager.getThumbnail(file); if (imageBitmap != null) viewHolder.icon.setImageBitmap(imageBitmap); else viewHolder.icon.setImageResource(dirent.getIcon()); } else { File thumbFile = DataManager.getThumbFile(dirent.id); if (thumbFile.exists()) { Bitmap imageBitmap; try { // setImageURI does not work correctly under high screen density // viewHolder.icon.setScaleType(ImageView.ScaleType.FIT_XY); // viewHolder.icon.setImageURI(Uri.fromFile(thumbFile)); imageBitmap = BitmapFactory.decodeStream(new FileInputStream(thumbFile)); viewHolder.icon.setImageBitmap(imageBitmap); } catch (FileNotFoundException e) { viewHolder.icon.setImageResource(dirent.getIcon()); } } else { viewHolder.icon.setImageResource(dirent.getIcon()); } } }