android: Use Fragment class from the support library to avoid deprecation warnings
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / RemediationInstructionsFragment.java
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 package org.strongswan.android.ui;
17
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.support.v4.app.ListFragment;
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 public class RemediationInstructionsFragment extends ListFragment
31 {
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;
38
39 /**
40 * The activity containing this fragment should implement this interface
41 */
42 public interface OnRemediationInstructionSelectedListener
43 {
44 public void onRemediationInstructionSelected(RemediationInstruction instruction);
45 }
46
47 @Override
48 public void onActivityCreated(Bundle savedInstanceState)
49 {
50 super.onActivityCreated(savedInstanceState);
51
52 if (savedInstanceState != null)
53 {
54 mInstructions = savedInstanceState.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
55 mCurrentPosition = savedInstanceState.getInt(KEY_POSITION);
56 }
57 }
58
59 @Override
60 public void onSaveInstanceState(Bundle outState)
61 {
62 super.onSaveInstanceState(outState);
63 outState.putParcelableArrayList(RemediationInstructionsFragment.EXTRA_REMEDIATION_INSTRUCTIONS, mInstructions);
64 outState.putInt(KEY_POSITION, mCurrentPosition);
65 }
66
67 @Override
68 public void onAttach(Context context)
69 {
70 super.onAttach(context);
71
72 if (context instanceof OnRemediationInstructionSelectedListener)
73 {
74 mListener = (OnRemediationInstructionSelectedListener)context;
75 }
76 }
77
78 @Override
79 public void onStart()
80 {
81 super.onStart();
82
83 boolean two_pane = getFragmentManager().findFragmentById(R.id.remediation_instruction_fragment) != null;
84 if (two_pane)
85 { /* two-pane layout, make list items selectable */
86 getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
87 }
88
89 Bundle args = getArguments();
90 if (mInstructions == null && args != null)
91 {
92 mInstructions = args.getParcelableArrayList(EXTRA_REMEDIATION_INSTRUCTIONS);
93 }
94 updateView(mInstructions);
95
96 if (two_pane && mCurrentPosition == -1 && mInstructions.size() > 0)
97 { /* two-pane layout, select first instruction */
98 mCurrentPosition = 0;
99 mListener.onRemediationInstructionSelected(mInstructions.get(0));
100 }
101 getListView().setItemChecked(mCurrentPosition, true);
102 }
103
104 @Override
105 public void onListItemClick(ListView l, View v, int position, long id)
106 {
107 mCurrentPosition = position;
108 mListener.onRemediationInstructionSelected(mInstructions.get(position));
109 getListView().setItemChecked(position, true);
110 }
111
112 public void updateView(ArrayList<RemediationInstruction> instructions)
113 {
114 if (mAdapter == null)
115 {
116 mAdapter = new RemediationInstructionAdapter(getActivity());
117 setListAdapter(mAdapter);
118 }
119 mInstructions = instructions;
120 mAdapter.setData(mInstructions);
121 }
122 }