]> git.ipfire.org Git - thirdparty/strongswan.git/blob
0f74bfd4180e49edadeba3a35e108033e9dd1cc7
[thirdparty/strongswan.git] /
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 package org.strongswan.android.ui;
18
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.view.View;
22 import android.widget.ListView;
23
24 import org.strongswan.android.R;
25 import org.strongswan.android.logic.imc.RemediationInstruction;
26 import org.strongswan.android.ui.adapter.RemediationInstructionAdapter;
27
28 import java.util.ArrayList;
29
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 import androidx.fragment.app.ListFragment;
33
34 public class RemediationInstructionsFragment extends ListFragment
35 {
36 public static final String EXTRA_REMEDIATION_INSTRUCTIONS = "instructions";
37 private static final String KEY_POSITION = "position";
38 private ArrayList<RemediationInstruction> mInstructions = null;
39 private OnRemediationInstructionSelectedListener mListener;
40 private RemediationInstructionAdapter mAdapter;
41 private int mCurrentPosition = -1;
42
43 /**
44 * The activity containing this fragment should implement this interface
45 */
46 public interface OnRemediationInstructionSelectedListener
47 {
48 public void onRemediationInstructionSelected(RemediationInstruction instruction);
49 }
50
51 @Override
52 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
53 {
54 super.onViewCreated(view, savedInstanceState);
55
56 if (savedInstanceState != null)
57 {
58 mInstructions = savedInstanceState.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
59 mCurrentPosition = savedInstanceState.getInt(KEY_POSITION);
60 }
61 }
62
63 @Override
64 public void onSaveInstanceState(Bundle outState)
65 {
66 super.onSaveInstanceState(outState);
67 outState.putParcelableArrayList(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS, mInstructions);
68 outState.putInt(KEY_POSITION, mCurrentPosition);
69 }
70
71 @Override
72 public void onAttach(Context context)
73 {
74 super.onAttach(context);
75
76 if (context instanceof OnRemediationInstructionSelectedListener)
77 {
78 mListener = (OnRemediationInstructionSelectedListener)context;
79 }
80 }
81
82 @Override
83 public void onStart()
84 {
85 super.onStart();
86
87 boolean two_pane = getParentFragmentManager().findFragmentById(R.id.remediation_instruction_fragment) != null;
88 if (two_pane)
89 { /* two-pane layout, make list items selectable */
90 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
91 }
92
93 Bundle args = getArguments();
94 if (mInstructions == null && args != null)
95 {
96 mInstructions = args.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
97 }
98 updateView(mInstructions);
99
100 if (two_pane && mCurrentPosition == -1 && mInstructions.size() > 0)
101 { /* two-pane layout, select first instruction */
102 mCurrentPosition = 0;
103 mListener.onRemediationInstructionSelected(mInstructions.get(0));
104 }
105 getListView().setItemChecked(mCurrentPosition, true);
106 }
107
108 @Override
109 public void onListItemClick(ListView l, View v, int position, long id)
110 {
111 mCurrentPosition = position;
112 mListener.onRemediationInstructionSelected(mInstructions.get(position));
113 getListView().setItemChecked(position, true);
114 }
115
116 public void updateView(ArrayList<RemediationInstruction> instructions)
117 {
118 if (mAdapter == null)
119 {
120 mAdapter = new RemediationInstructionAdapter(getActivity());
121 setListAdapter(mAdapter);
122 }
123 mInstructions = instructions;
124 mAdapter.setData(mInstructions);
125 }
126 }