]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/BookmarksActivity.java
Initial bookmarks implementation. Fix for Custom User Agent not working. https...
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / BookmarksActivity.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;
21
22 import android.app.Activity;
23 import android.app.DialogFragment;
24 import android.database.Cursor;
25 import android.graphics.Bitmap;
26 import android.graphics.BitmapFactory;
27 import android.os.Bundle;
28 import android.support.design.widget.FloatingActionButton;
29 import android.support.v4.app.NavUtils;
30 import android.support.v7.app.ActionBar;
31 import android.support.v7.app.AppCompatActivity;
32 import android.support.v7.widget.Toolbar;
33 import android.view.View;
34 import android.widget.AdapterView;
35 import android.widget.EditText;
36 import android.widget.ImageView;
37 import android.widget.ListView;
38 import android.widget.SimpleCursorAdapter;
39
40 import java.io.ByteArrayOutputStream;
41
42 public class BookmarksActivity extends AppCompatActivity implements CreateBookmark.CreateBookmarkListener {
43     private BookmarksDatabaseHandler bookmarksDatabaseHandler;
44     private ListView bookmarksListView;
45
46     @Override
47     protected void onCreate(Bundle savedInstanceState) {
48         super.onCreate(savedInstanceState);
49         setContentView(R.layout.bookmarks_coordinatorlayout);
50
51         // We need to use the SupportActionBar from android.support.v7.app.ActionBar until the minimum API is >= 21.
52         Toolbar bookmarksAppBar = (Toolbar) findViewById(R.id.bookmarks_toolbar);
53         setSupportActionBar(bookmarksAppBar);
54
55         // Display the home arrow on supportAppBar.
56         final ActionBar appBar = getSupportActionBar();
57         assert appBar != null;// This assert removes the incorrect warning in Android Studio on the following line that appBar might be null.
58         appBar.setDisplayHomeAsUpEnabled(true);
59
60         // Initialize the database handler and the ListView.
61         bookmarksDatabaseHandler = new BookmarksDatabaseHandler(this, null, null, 0);
62         bookmarksListView = (ListView) findViewById(R.id.bookmarks_listview);
63
64         // Display the bookmarks in the ListView.
65         updateBookmarksListView();
66
67         // Set a listener so that tapping a list item loads the URL.  We need to store the activity in a variable so that we can return to the parent activity after loading the URL.
68         final Activity bookmarksActivity = this;
69         bookmarksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
70             @Override
71             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
72                 // Convert the id from long to int to match the format of the bookmarks database.
73                 int databaseID = (int) id;
74
75                 // Get the bookmark URL and assign it to formattedUrlString.
76                 MainWebViewActivity.formattedUrlString = bookmarksDatabaseHandler.getBookmarkURL(databaseID);
77
78                 //  Load formattedUrlString and return to the main activity.
79                 MainWebViewActivity.mainWebView.loadUrl(MainWebViewActivity.formattedUrlString);
80                 NavUtils.navigateUpFromSameTask(bookmarksActivity);
81             }
82         });
83
84         // Set a FloatingActionButton for creating new bookmarks.
85         FloatingActionButton createBookmarkFAB = (FloatingActionButton) findViewById(R.id.create_bookmark_fab);
86         assert createBookmarkFAB != null;
87         createBookmarkFAB.setOnClickListener(new View.OnClickListener() {
88             @Override
89             public void onClick(View view) {
90                 // Show the CreateBookmark AlertDialog and name the instance "@string/create_bookmark".
91                 DialogFragment createBookmarkDialog = new CreateBookmark();
92                 createBookmarkDialog.show(getFragmentManager(), "@string/create_bookmark");
93             }
94         });
95     }
96
97     @Override
98     public void onCreateBookmarkCancel(DialogFragment createBookmarkDialogFragment) {
99         // Do nothing because the user selected "Cancel".
100     }
101
102     @Override
103     public void onCreateBookmarkCreate(DialogFragment createBookmarkDialogFragment) {
104         // Get the EditTexts from the DialogFragment and extract the strings.
105         EditText createBookmarkNameEditText = (EditText) createBookmarkDialogFragment.getDialog().findViewById(R.id.create_bookmark_name_edittext);
106         String bookmarkNameString = createBookmarkNameEditText.getText().toString();
107         EditText createBookmarkURLEditText = (EditText) createBookmarkDialogFragment.getDialog().findViewById(R.id.create_bookmark_url_edittext);
108         String bookmarkURLString = createBookmarkURLEditText.getText().toString();
109
110         // Convert the favoriteIcon Bitmap to a byte array.
111         ByteArrayOutputStream favoriteIconByteArrayOutputStream = new ByteArrayOutputStream();
112         MainWebViewActivity.favoriteIcon.compress(Bitmap.CompressFormat.PNG, 0, favoriteIconByteArrayOutputStream);
113         byte[] favoriteIconByteArray = favoriteIconByteArrayOutputStream.toByteArray();
114
115         // Create the bookmark.
116         bookmarksDatabaseHandler.createBookmark(bookmarkNameString, bookmarkURLString, favoriteIconByteArray);
117
118         // Refresh the ListView.
119         updateBookmarksListView();
120     }
121
122     private void updateBookmarksListView() {
123         // Get a Cursor with the current contents of the bookmarks database.
124         final Cursor bookmarksCursor = bookmarksDatabaseHandler.getBookmarksCursor();
125
126         // The last argument is 0 because no special behavior is required.
127         SimpleCursorAdapter bookmarksAdapter = new SimpleCursorAdapter(this,
128                 R.layout.bookmarks_item_linearlayout,
129                 bookmarksCursor,
130                 new String[] { BookmarksDatabaseHandler.FAVORITEICON, BookmarksDatabaseHandler.BOOKMARK_NAME },
131                 new int[] { R.id.bookmark_favorite_icon, R.id.bookmark_name },
132                 0);
133
134         // Override the handling of R.id.bookmark_favorite_icon to load an image instead of a string.
135         bookmarksAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
136             public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
137                 if (view.getId() == R.id.bookmark_favorite_icon) {
138                     // Get the byte array from the database.
139                     byte[] favoriteIconByteArray = cursor.getBlob(columnIndex);
140
141                     // Convert the byte array to a Bitmap beginning at the first byte and ending at the last.
142                     Bitmap favoriteIconBitmap = BitmapFactory.decodeByteArray(favoriteIconByteArray, 0, favoriteIconByteArray.length);
143
144                     // Set the favoriteIconBitmap.
145                     ImageView bookmarkFavoriteIcon = (ImageView) view;
146                     bookmarkFavoriteIcon.setImageBitmap(favoriteIconBitmap);
147                     return true;
148                 } else {  // Process the rest of the bookmarksAdapter with default settings.
149                     return false;
150                 }
151             }
152         });
153
154         // Update the ListView.
155         bookmarksListView.setAdapter(bookmarksAdapter);
156     }
157 }