]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/fragments/DomainSettingsFragment.java
Implement a working two-paned mode for `DomainsActivity`.
[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.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;
30
31 import com.stoutner.privacybrowser.R;
32 import com.stoutner.privacybrowser.helpers.DomainsDatabaseHelper;
33
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";
37
38     // `databaseId` is used in `onCreate()` and `onCreateView()`.
39     private int databaseId;
40
41     @Override
42     public void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44
45         // Store the database id in `databaseId`.
46         databaseId = getArguments().getInt(DATABASE_ID);
47     }
48
49     @Override
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);
53
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);
57
58         // Get the database `Cursor` for this ID and move it to the first row.
59         Cursor domainCursor = domainsDatabaseHelper.getCursorForId(databaseId);
60         domainCursor.moveToFirst();
61
62         // Get handles for the `EditTexts`.
63         EditText domainNameEditText = (EditText) domainSettingsView.findViewById(R.id.domain_settings_name_edittext);
64
65         // Set the text from the database cursor.
66         domainNameEditText.setText(domainCursor.getString(domainCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN)));
67
68         return domainSettingsView;
69     }
70 }