2 * Copyright (C) 2013 Tobias Brunner
3 * 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 java.util.ArrayList;
20 import org.strongswan.android.R;
21 import org.strongswan.android.logic.imc.RemediationInstruction;
22 import org.strongswan.android.ui.adapter.RemediationInstructionAdapter;
24 import android.app.Activity;
25 import android.app.ListFragment;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.widget.ListView;
30 public class RemediationInstructionsFragment extends ListFragment
32 public static final String EXTRA_REMEDIATION_INSTRUCTIONS = "instructions";
33 private static final String KEY_POSITION = "position";
34 private ArrayList<RemediationInstruction> mInstructions = null;
35 private OnRemediationInstructionSelectedListener mListener;
36 private RemediationInstructionAdapter mAdapter;
37 private int mCurrentPosition = -1;
40 * The activity containing this fragment should implement this interface
42 public interface OnRemediationInstructionSelectedListener
44 public void onRemediationInstructionSelected(RemediationInstruction instruction);
48 public void onActivityCreated(Bundle savedInstanceState)
50 super.onActivityCreated(savedInstanceState);
52 if (savedInstanceState != null)
54 mInstructions = savedInstanceState.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
55 mCurrentPosition = savedInstanceState.getInt(KEY_POSITION);
60 public void onSaveInstanceState(Bundle outState)
62 super.onSaveInstanceState(outState);
63 outState.putParcelableArrayList(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS, mInstructions);
64 outState.putInt(KEY_POSITION, mCurrentPosition);
68 public void onAttach(Activity activity)
70 super.onAttach(activity);
72 if (activity instanceof OnRemediationInstructionSelectedListener)
74 mListener = (OnRemediationInstructionSelectedListener)activity;
83 boolean two_pane = getFragmentManager().findFragmentById(R.id.remediation_instruction_fragment) != null;
85 { /* two-pane layout, make list items selectable */
86 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
89 Bundle args = getArguments();
90 if (mInstructions == null && args != null)
92 mInstructions = args.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
94 updateView(mInstructions);
96 if (two_pane && mCurrentPosition == -1 && mInstructions.size() > 0)
97 { /* two-pane layout, select first instruction */
99 mListener.onRemediationInstructionSelected(mInstructions.get(0));
101 getListView().setItemChecked(mCurrentPosition, true);
105 public void onListItemClick(ListView l, View v, int position, long id)
107 mCurrentPosition = position;
108 mListener.onRemediationInstructionSelected(mInstructions.get(position));
109 getListView().setItemChecked(position, true);
112 public void updateView(ArrayList<RemediationInstruction> instructions)
114 if (mAdapter == null)
116 mAdapter = new RemediationInstructionAdapter(getActivity());
117 setListAdapter(mAdapter);
119 mInstructions = instructions;
120 mAdapter.setData(mInstructions);