]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java
Add icons to `DomainSettingsFragment`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / fragments / DomainSettingsFragment.java
1 /*
2  * Copyright 2017 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.fragments;
21
22 import android.content.Context;
23 import android.database.Cursor;
24 import android.os.Bundle;
25 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23.  Otherwise we cannot call `getContext()`.
26 import android.support.v4.app.Fragment;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.ViewGroup;
30 import android.widget.ArrayAdapter;
31 import android.widget.EditText;
32 import android.widget.Spinner;
33
34 import com.stoutner.privacybrowser.R;
35 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
36
37 public class DomainSettingsFragment extends Fragment {
38     // `DATABASE_ID` is used by activities calling this fragment.
39     public static final String DATABASE_ID = "database_id";
40
41     // `databaseId` is used in `onCreate()` and `onCreateView()`.
42     private int databaseId;
43
44     @Override
45     public void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47
48         // Store the database id in `databaseId`.
49         databaseId = getArguments().getInt(DATABASE_ID);
50     }
51
52     @Override
53     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
54         // Inflate `domain_settings`.  `false` does not attach it to the root `container`.
55         View domainSettingsView = inflater.inflate(R.layout.domain_settings, container, false);
56
57         // Get a handle for the `Context`.
58         Context context = getContext();
59
60         // Get handles for the views in the fragment.
61         EditText domainNameEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_name_edittext);
62         Spinner userAgentSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_user_agent_spinner);
63         Spinner fontSizeSpinner = (Spinner) domainSettingsView.findViewById(R.id.domain_settings_font_size_spinner);
64
65         // Initialize the database handler.  `this` specifies the context.  The two `nulls` do not specify the database name or a `CursorFactory`.
66         // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
67         DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
68
69         // Get the database `Cursor` for this ID and move it to the first row.
70         Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
71         domainCursor.moveToFirst();
72
73         // Save the `Cursor` entries as variables.
74         String domainNameString = domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN));
75         int fontSizeInt = domainCursor.getInt(domainCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE));
76
77         // Create `ArrayAdapters` for the `Spinners`and their `entry values`.
78         ArrayAdapter<CharSequence> userAgentArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entries, android.R.layout.simple_spinner_item);
79         ArrayAdapter<CharSequence> userAgentEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.user_agent_entry_values, android.R.layout.simple_spinner_item);
80         ArrayAdapter<CharSequence> fontSizeArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entries, android.R.layout.simple_spinner_item);
81         ArrayAdapter<CharSequence> fontSizeEntryValuesArrayAdapter = ArrayAdapter.createFromResource(context, R.array.default_font_size_entry_values, android.R.layout.simple_spinner_item);
82
83         // Set the drop down style for the `ArrayAdapters`.
84         userAgentArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
85         fontSizeArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
86
87         // Set the `ArrayAdapters` for the `Spinners`.
88         userAgentSpinner.setAdapter(userAgentArrayAdapter);
89         fontSizeSpinner.setAdapter(fontSizeArrayAdapter);
90
91         //
92         // int userAgentArrayPosition =
93
94         // Set the selected font size.
95         int fontSizeArrayPosition = fontSizeEntryValuesArrayAdapter.getPosition(String.valueOf(fontSizeInt));
96         fontSizeSpinner.setSelection(fontSizeArrayPosition);
97
98
99         // Set the text from the database cursor.
100         domainNameEditText.setText(domainNameString);
101
102         return domainSettingsView;
103     }
104 }