2 * Copyright 2017 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.fragments;
22 import android.database.Cursor;
23 import android.os.Bundle;
24 // We have to use `android.support.v4.app.Fragment` until minimum API >= 23. Otherwise we cannot call `getContext()`.
25 import android.support.v4.app.Fragment;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.EditText;
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
34 public class DomainSettingsFragment extends Fragment {
35 // `DATABASE_ID` is used by activities calling this fragment.
36 public static final String DATABASE_ID = "database_id";
38 // `databaseId` is used in `onCreate()` and `onCreateView()`.
39 private int databaseId;
42 public void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
45 // Store the database id in `databaseId`.
46 databaseId = getArguments().getInt(DATABASE_ID);
50 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
51 // Inflate `domain_settings`. `false` does not attach it to the root `container`.
52 View domainSettingsView = inflater.inflate(R.layout.domain_settings, container, false);
54 // Initialize the database handler. `this` specifies the context. The two `nulls` do not specify the database name or a `CursorFactory`.
55 // The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
56 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(getContext(), null, null, 0);
58 // Get the database `Cursor` for this ID and move it to the first row.
59 Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
60 domainCursor.moveToFirst();
62 // Get handles for the `EditTexts`.
63 EditText domainNameEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_name_edittext);
65 // Set the text from the database cursor.
66 domainNameEditText.setText(domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN)));
68 return domainSettingsView;