/*
This file is part of ZAX.
ZAX 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 3 of the License, or
(at your option) any later version.
ZAX 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.
You should have received a copy of the GNU General Public License
along with ZAX. If not, see <http://www.gnu.org/licenses/>.
*/
package com.inovex.zabbixmobile.activities.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.inovex.zabbixmobile.R;
import com.inovex.zabbixmobile.adapters.ScreensListAdapter;
import com.inovex.zabbixmobile.listeners.OnScreensItemSelectedListener;
/**
* Fragment showing a list of all screens specified in Zabbix.
*
*/
public class ScreensListFragment extends BaseServiceConnectedListFragment {
public static String TAG = ScreensListFragment.class.getSimpleName();
private static final String ARG_POSITION = "arg_position";
private static final String ARG_ITEM_ID = "arg_item_id";
private static final String ARG_SPINNER_VISIBLE = "arg_spinner_visible";
private int mCurrentPosition = 0;
private long mCurrentItemId = 0;
private boolean mLoadingSpinnerVisible = true;
private OnScreensItemSelectedListener mCallbackMain;
private ScreensListAdapter mListAdapter;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallbackMain = (OnScreensItemSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnScreensItemSelectedListener.");
}
}
public void setCurrentPosition(int currentPosition) {
this.mCurrentPosition = currentPosition;
}
public void setCurrentItemId(long currentItemId) {
this.mCurrentItemId = currentItemId;
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
setCurrentPosition(position);
setCurrentItemId(id);
mCallbackMain.onScreenSelected(mListAdapter.getItem(position));
}
@Override
protected void setupListAdapter() {
this.mListAdapter = mZabbixDataService.getScreensListAdapter();
setListAdapter(mListAdapter);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
mCurrentItemId = savedInstanceState.getLong(ARG_ITEM_ID);
mLoadingSpinnerVisible = savedInstanceState.getBoolean(
ARG_SPINNER_VISIBLE, false);
}
Log.d(TAG, "pos: " + mCurrentPosition + "; id: " + mCurrentItemId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_screens_list, null, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemChecked(mCurrentPosition, true);
getListView().setSelection(mCurrentPosition);
if (mLoadingSpinnerVisible)
showLoadingSpinner();
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt(ARG_POSITION, mCurrentPosition);
outState.putLong(ARG_ITEM_ID, mCurrentItemId);
super.onSaveInstanceState(outState);
}
/**
* Shows a loading spinner instead of this page's list view.
*/
public void showLoadingSpinner() {
mLoadingSpinnerVisible = true;
if (getView() != null) {
LinearLayout progressLayout = (LinearLayout) getView()
.findViewById(R.id.list_progress_layout);
if (progressLayout != null)
progressLayout.setVisibility(View.VISIBLE);
}
}
/**
* Dismisses the loading spinner view.
*
* If the view has not yet been created, the status is saved and when the
* view is created, the spinner will not be shown at all.
*/
public void dismissLoadingSpinner() {
mLoadingSpinnerVisible = false;
if (getView() != null) {
LinearLayout progressLayout = (LinearLayout) getView()
.findViewById(R.id.list_progress_layout);
if (progressLayout != null) {
progressLayout.setVisibility(View.GONE);
}
}
}
}