2 * Copyright (C) 2013 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
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>.
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
16 package org.strongswan.android.ui;
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.view.View;
21 import android.widget.ListView;
23 import org.strongswan.android.R;
24 import org.strongswan.android.logic.imc.RemediationInstruction;
25 import org.strongswan.android.ui.adapter.RemediationInstructionAdapter;
27 import java.util.ArrayList;
29 import androidx.fragment.app.ListFragment;
31 public class RemediationInstructionsFragment extends ListFragment
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;
41 * The activity containing this fragment should implement this interface
43 public interface OnRemediationInstructionSelectedListener
45 public void onRemediationInstructionSelected(RemediationInstruction instruction);
49 public void onActivityCreated(Bundle savedInstanceState)
51 super.onActivityCreated(savedInstanceState);
53 if (savedInstanceState != null)
55 mInstructions = savedInstanceState.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
56 mCurrentPosition = savedInstanceState.getInt(KEY_POSITION);
61 public void onSaveInstanceState(Bundle outState)
63 super.onSaveInstanceState(outState);
64 outState.putParcelableArrayList(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS, mInstructions);
65 outState.putInt(KEY_POSITION, mCurrentPosition);
69 public void onAttach(Context context)
71 super.onAttach(context);
73 if (context instanceof OnRemediationInstructionSelectedListener)
75 mListener = (OnRemediationInstructionSelectedListener)context;
84 boolean two_pane = getFragmentManager().findFragmentById(R.id.remediation_instruction_fragment) != null;
86 { /* two-pane layout, make list items selectable */
87 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
90 Bundle args = getArguments();
91 if (mInstructions == null && args != null)
93 mInstructions = args.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
95 updateView(mInstructions);
97 if (two_pane && mCurrentPosition == -1 && mInstructions.size() > 0)
98 { /* two-pane layout, select first instruction */
100 mListener.onRemediationInstructionSelected(mInstructions.get(0));
102 getListView().setItemChecked(mCurrentPosition, true);
106 public void onListItemClick(ListView l, View v, int position, long id)
108 mCurrentPosition = position;
109 mListener.onRemediationInstructionSelected(mInstructions.get(position));
110 getListView().setItemChecked(position, true);
113 public void updateView(ArrayList<RemediationInstruction> instructions)
115 if (mAdapter == null)
117 mAdapter = new RemediationInstructionAdapter(getActivity());
118 setListAdapter(mAdapter);
120 mInstructions = instructions;
121 mAdapter.setData(mInstructions);