]> git.ipfire.org Git - thirdparty/strongswan.git/blob
d00f561e0a740b7013655cc14dcecba6047fe83a
[thirdparty/strongswan.git] /
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * HSR 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.content.Context;
19 import android.os.Bundle;
20 import android.view.View;
21 import android.widget.ListView;
22
23 import org.strongswan.android.R;
24 import org.strongswan.android.logic.imc.RemediationInstruction;
25 import org.strongswan.android.ui.adapter.RemediationInstructionAdapter;
26
27 import java.util.ArrayList;
28
29 import androidx.fragment.app.ListFragment;
30
31 public class RemediationInstructionsFragment extends ListFragment
32 {
33 public static final String EXTRA_REMEDIATION_INSTRUCTIONS = "instructions";
34 private static final String KEY_POSITION = "position";
35 private ArrayList<RemediationInstruction> mInstructions = null;
36 private OnRemediationInstructionSelectedListener mListener;
37 private RemediationInstructionAdapter mAdapter;
38 private int mCurrentPosition = -1;
39
40 /**
41 * The activity containing this fragment should implement this interface
42 */
43 public interface OnRemediationInstructionSelectedListener
44 {
45 public void onRemediationInstructionSelected(RemediationInstruction instruction);
46 }
47
48 @Override
49 public void onActivityCreated(Bundle savedInstanceState)
50 {
51 super.onActivityCreated(savedInstanceState);
52
53 if (savedInstanceState != null)
54 {
55 mInstructions = savedInstanceState.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
56 mCurrentPosition = savedInstanceState.getInt(KEY_POSITION);
57 }
58 }
59
60 @Override
61 public void onSaveInstanceState(Bundle outState)
62 {
63 super.onSaveInstanceState(outState);
64 outState.putParcelableArrayList(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS, mInstructions);
65 outState.putInt(KEY_POSITION, mCurrentPosition);
66 }
67
68 @Override
69 public void onAttach(Context context)
70 {
71 super.onAttach(context);
72
73 if (context instanceof OnRemediationInstructionSelectedListener)
74 {
75 mListener = (OnRemediationInstructionSelectedListener)context;
76 }
77 }
78
79 @Override
80 public void onStart()
81 {
82 super.onStart();
83
84 boolean two_pane = getFragmentManager().findFragmentById(R.id.remediation_instruction_fragment) != null;
85 if (two_pane)
86 { /* two-pane layout, make list items selectable */
87 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
88 }
89
90 Bundle args = getArguments();
91 if (mInstructions == null && args != null)
92 {
93 mInstructions = args.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
94 }
95 updateView(mInstructions);
96
97 if (two_pane && mCurrentPosition == -1 && mInstructions.size() > 0)
98 { /* two-pane layout, select first instruction */
99 mCurrentPosition = 0;
100 mListener.onRemediationInstructionSelected(mInstructions.get(0));
101 }
102 getListView().setItemChecked(mCurrentPosition, true);
103 }
104
105 @Override
106 public void onListItemClick(ListView l, View v, int position, long id)
107 {
108 mCurrentPosition = position;
109 mListener.onRemediationInstructionSelected(mInstructions.get(position));
110 getListView().setItemChecked(position, true);
111 }
112
113 public void updateView(ArrayList<RemediationInstruction> instructions)
114 {
115 if (mAdapter == null)
116 {
117 mAdapter = new RemediationInstructionAdapter(getActivity());
118 setListAdapter(mAdapter);
119 }
120 mInstructions = instructions;
121 mAdapter.setData(mInstructions);
122 }
123 }