From 611d35e8e87ed0bbbcd5ab9ff90522f557ee6dd6 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Thu, 30 May 2013 11:47:01 +0200 Subject: android: Add fragment for a list of remediation instructions This fragment can later be used in one- or two-pane layouts. --- .../ui/RemediationInstructionsFragment.java | 121 +++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/frontends/android/src/org/strongswan/android/ui/RemediationInstructionsFragment.java (limited to 'src/frontends') diff --git a/src/frontends/android/src/org/strongswan/android/ui/RemediationInstructionsFragment.java b/src/frontends/android/src/org/strongswan/android/ui/RemediationInstructionsFragment.java new file mode 100644 index 000000000..9aa7ea140 --- /dev/null +++ b/src/frontends/android/src/org/strongswan/android/ui/RemediationInstructionsFragment.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2013 Tobias Brunner + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +package org.strongswan.android.ui; + +import java.util.ArrayList; + +import org.strongswan.android.logic.imc.RemediationInstruction; +import org.strongswan.android.ui.adapter.RemediationInstructionAdapter; + +import android.app.Activity; +import android.app.ListFragment; +import android.os.Bundle; +import android.view.View; +import android.widget.ListView; + +public class RemediationInstructionsFragment extends ListFragment +{ + public static final String EXTRA_REMEDIATION_INSTRUCTIONS = "instructions"; + private static final String KEY_POSITION = "position"; + private ArrayList mInstructions = null; + private OnRemediationInstructionSelectedListener mListener; + private RemediationInstructionAdapter mAdapter; + private int mCurrentPosition = -1; + + /** + * The activity containing this fragment should implement this interface + */ + public interface OnRemediationInstructionSelectedListener + { + public void onRemediationInstructionSelected(RemediationInstruction instruction); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) + { + super.onActivityCreated(savedInstanceState); + + if (savedInstanceState != null) + { + mInstructions = savedInstanceState.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS); + mCurrentPosition = savedInstanceState.getInt(KEY_POSITION); + } + } + + @Override + public void onSaveInstanceState(Bundle outState) + { + super.onSaveInstanceState(outState); + outState.putParcelableArrayList(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS, mInstructions); + outState.putInt(KEY_POSITION, mCurrentPosition); + } + + @Override + public void onAttach(Activity activity) + { + super.onAttach(activity); + + if (activity instanceof OnRemediationInstructionSelectedListener) + { + mListener = (OnRemediationInstructionSelectedListener)activity; + } + } + + @Override + public void onStart() + { + super.onStart(); + + boolean two_pane = false; + if (two_pane) + { /* two-pane layout, make list items selectable */ + getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); + } + + Bundle args = getArguments(); + if (mInstructions == null && args != null) + { + mInstructions = args.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS); + } + updateView(mInstructions); + + if (two_pane && mCurrentPosition == -1 && mInstructions.size() > 0) + { /* two-pane layout, select first instruction */ + mCurrentPosition = 0; + mListener.onRemediationInstructionSelected(mInstructions.get(0)); + } + getListView().setItemChecked(mCurrentPosition, true); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) + { + mCurrentPosition = position; + mListener.onRemediationInstructionSelected(mInstructions.get(position)); + getListView().setItemChecked(position, true); + } + + public void updateView(ArrayList instructions) + { + if (mAdapter == null) + { + mAdapter = new RemediationInstructionAdapter(getActivity()); + setListAdapter(mAdapter); + } + mInstructions = instructions; + mAdapter.setData(mInstructions); + } +} -- cgit v1.2.3