]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/adapters/HistoryArrayAdapter.java
1135638c7058945f6c771a15abd828a32418ae40
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / adapters / HistoryArrayAdapter.java
1 /**
2  * Copyright 2016 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.adapters;
21
22 import android.content.Context;
23 import android.graphics.Typeface;
24 import android.support.annotation.NonNull;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.ArrayAdapter;
29 import android.widget.ImageView;
30 import android.widget.TextView;
31
32 import com.stoutner.privacybrowser.R;
33 import com.stoutner.privacybrowser.definitions.History;
34
35 import java.util.ArrayList;
36
37 public class HistoryArrayAdapter extends ArrayAdapter<History> {
38
39     // `currentPage` is used in `HistoryArrayAdapter` and `getView()`.
40     private int currentPage;
41
42     public HistoryArrayAdapter(Context context, ArrayList<History> historyArrayList, int currentPageId) {
43         // We need to call `super` from the base `ArrayAdapter`.  `0` is the `textViewResourceId`.
44         super(context, 0, historyArrayList);
45
46         // Store `currentPageId` in the class variable.
47         currentPage = currentPageId;
48     }
49
50     @Override
51     @NonNull
52     public View getView(int position, View convertView, @NonNull ViewGroup parent) {
53         // Inflate the view if it is `null`.
54         if (convertView == null) {
55             // `false` does not attach `url_history_item_linearlayout` to `parent`.
56             convertView = LayoutInflater.from(getContext()).inflate(R.layout.url_history_item_linearlayout, parent, false);
57         }
58
59         // Get handles for `favoriteIconImageView` and `urlTextView`.
60         ImageView favoriteIconImageView = (ImageView) convertView.findViewById(R.id.history_favorite_icon_imageview);
61         TextView urlTextView = (TextView) convertView.findViewById(R.id.history_url_textview);
62
63         // Get the URL history for this position.
64         History history = getItem(position);
65
66         // Remove the lint warning below that `history` might be `null`.
67         assert history != null;
68
69         // Set `favoriteIconImageView` and `urlTextView`.
70         favoriteIconImageView.setImageBitmap(history.entryFavoriteIcon);
71         urlTextView.setText(history.entryUrl);
72
73         // Set the URL text for `currentPage` to be bold.
74         if (position == currentPage) {
75             urlTextView.setTypeface(Typeface.DEFAULT_BOLD);
76         } else {  // Set the default typeface for all the other entries.
77             urlTextView.setTypeface(Typeface.DEFAULT);
78         }
79
80         // Return the modified `convertView`.
81         return convertView;
82     }
83 }