android: Fix display of remediation instructions with support library
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / RemediationInstructionFragment.java
1 /*
2 * Copyright (C) 2013-2016 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.ui;
17
18 import android.os.Bundle;
19 import android.support.v4.app.ListFragment;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 import android.view.ViewGroup;
23 import android.widget.ArrayAdapter;
24 import android.widget.FrameLayout;
25 import android.widget.TextView;
26
27 import org.strongswan.android.R;
28 import org.strongswan.android.logic.imc.RemediationInstruction;
29
30 public class RemediationInstructionFragment extends ListFragment
31 {
32 public static final String ARG_REMEDIATION_INSTRUCTION = "instruction";
33 private RemediationInstruction mInstruction = null;
34 private TextView mTitle;
35 private TextView mDescription;
36 private TextView mHeader;
37
38 @Override
39 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
40 {
41 /* while the documentation recommends to include "@android:layout/list_content" to retain
42 * the default functionality, this does not actually work with the ListFragment provided by
43 * the support library as it builds the view manually and uses different IDs */
44 View layout = inflater.inflate(R.layout.remediation_instruction, container, false);
45 FrameLayout list = (FrameLayout)layout.findViewById(R.id.list_container);
46 list.addView(super.onCreateView(inflater, list, savedInstanceState));
47 return layout;
48 }
49
50 @Override
51 public void onActivityCreated(Bundle savedInstanceState)
52 {
53 super.onActivityCreated(savedInstanceState);
54
55 if (savedInstanceState != null)
56 {
57 mInstruction = savedInstanceState.getParcelable(ARG_REMEDIATION_INSTRUCTION);
58 }
59 /* show dividers only between list items */
60 getListView().setHeaderDividersEnabled(false);
61 getListView().setFooterDividersEnabled(false);
62 /* don't show loader while adapter is not set */
63 setListShown(true);
64 mTitle = (TextView)getView().findViewById(R.id.title);
65 mDescription = (TextView)getView().findViewById(R.id.description);
66 mHeader = (TextView)getView().findViewById(R.id.list_header);
67 }
68
69 @Override
70 public void onSaveInstanceState(Bundle outState)
71 {
72 super.onSaveInstanceState(outState);
73 outState.putParcelable(ARG_REMEDIATION_INSTRUCTION, mInstruction);
74 }
75
76 @Override
77 public void onStart()
78 {
79 super.onStart();
80
81 Bundle args = getArguments();
82 if (args != null)
83 {
84 mInstruction = args.getParcelable(ARG_REMEDIATION_INSTRUCTION);
85 }
86 updateView(mInstruction);
87 }
88
89 public void updateView(RemediationInstruction instruction)
90 {
91 mInstruction = instruction;
92 if (mInstruction != null)
93 {
94 mTitle.setText(mInstruction.getTitle());
95 mDescription.setText(mInstruction.getDescription());
96 if (mInstruction.getHeader() != null)
97 {
98 mHeader.setText(mInstruction.getHeader());
99 setListAdapter(new ArrayAdapter<String>(getActivity(),
100 android.R.layout.simple_list_item_1, mInstruction.getItems()));
101 }
102 else
103 {
104 mHeader.setText("");
105 setListAdapter(null);
106 }
107 }
108 else
109 {
110 mTitle.setText("");
111 mDescription.setText("");
112 mHeader.setText("");
113 setListAdapter(null);
114 }
115 }
116 }