2 * Copyright (C) 2013 Tobias Brunner
4 * Copyright (C) secunet Security Networks AG
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>.
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
17 package org.strongswan.android.ui;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.view.View;
22 import android.widget.ListView;
24 import org.strongswan.android.R;
25 import org.strongswan.android.logic.imc.RemediationInstruction;
26 import org.strongswan.android.ui.adapter.RemediationInstructionAdapter;
28 import java.util.ArrayList;
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 import androidx.fragment.app.ListFragment;
34 public class RemediationInstructionsFragment extends ListFragment
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;
44 * The activity containing this fragment should implement this interface
46 public interface OnRemediationInstructionSelectedListener
48 public void onRemediationInstructionSelected(RemediationInstruction instruction);
52 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
54 super.onViewCreated(view, savedInstanceState);
56 if (savedInstanceState != null)
58 mInstructions = savedInstanceState.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
59 mCurrentPosition = savedInstanceState.getInt(KEY_POSITION);
64 public void onSaveInstanceState(Bundle outState)
66 super.onSaveInstanceState(outState);
67 outState.putParcelableArrayList(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS, mInstructions);
68 outState.putInt(KEY_POSITION, mCurrentPosition);
72 public void onAttach(Context context)
74 super.onAttach(context);
76 if (context instanceof OnRemediationInstructionSelectedListener)
78 mListener = (OnRemediationInstructionSelectedListener)context;
87 boolean two_pane = getParentFragmentManager().findFragmentById(R.id.remediation_instruction_fragment) != null;
89 { /* two-pane layout, make list items selectable */
90 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
93 Bundle args = getArguments();
94 if (mInstructions == null && args != null)
96 mInstructions = args.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
98 updateView(mInstructions);
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));
105 getListView().setItemChecked(mCurrentPosition, true);
109 public void onListItemClick(ListView l, View v, int position, long id)
111 mCurrentPosition = position;
112 mListener.onRemediationInstructionSelected(mInstructions.get(position));
113 getListView().setItemChecked(position, true);
116 public void updateView(ArrayList<RemediationInstruction> instructions)
118 if (mAdapter == null)
120 mAdapter = new RemediationInstructionAdapter(getActivity());
121 setListAdapter(mAdapter);
123 mInstructions = instructions;
124 mAdapter.setData(mInstructions);