fr.eoit.activity.LocationManagementActivity.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.LocationManagementActivity.java

Source

/**
 * Copyright (C) 2012 Picon software
 *
 * 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.
 *
 * 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.
    
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package fr.eoit.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.ListView;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import fr.eoit.EOITConst;
import fr.eoit.R;
import fr.eoit.activity.fragment.util.NoStationDialog;
import fr.eoit.activity.listener.StationRoleOnItemClickListener;
import fr.eoit.activity.util.StationListViewBinder;
import fr.eoit.activity.util.task.UpdateStandingAsyncTask;
import fr.eoit.db.bean.Station;
import fr.eoit.db.dto.ColumnsNames.Region;
import fr.eoit.parameter.station.Stations;
import fr.piconsoft.db.util.DbUtil;

/**
 * @author picon.software
 *
 */
public class LocationManagementActivity extends LoaderActivity<Cursor> {

    private SimpleCursorAdapter adapter;
    private boolean standingUpdated = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            standingUpdated = savedInstanceState.getBoolean("standingUpdated");
        }

        setContentView(R.layout.location_management);

        Intent intent = getIntent();
        if (intent.getData() == null) {
            intent.setData(Station.CONTENT_URI);
        }

        // The names of the cursor columns to display in the view, initialized to the title column
        String[] dataColumns = { Station.COLUMN_NAME_STATION_TYPE_ID, Station.COLUMN_NAME_NAME,
                Region.COLUMN_NAME_NAME_ALIAS, Station.COLUMN_NAME_ROLE, Station.COLUMN_NAME_ROLE,
                Station.COLUMN_NAME_STANDING };

        // The view IDs that will display the cursor columns, initialized to the TextView in
        // noteslist_item.xml
        int[] viewIDs = { R.id.station_icon, R.id.station_name, R.id.location_name, R.id.station_prod_icon,
                R.id.station_trade_icon, R.id.corp_standing };

        // Creates the backing adapter for the ListView.
        adapter = new SimpleCursorAdapter(this, R.layout.station_row, null, dataColumns, viewIDs,
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        adapter.setViewBinder(new StationListViewBinder());

        ListView listView = (ListView) findViewById(R.id.location_list);
        //listView.setOnItemClickListener(new ItemOnItemListClickListener());

        // Sets the ListView's adapter to be the cursor adapter that was just created.
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new StationRoleOnItemClickListener());

        initOrRestart();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putBoolean("standingUpdated", standingUpdated);
    }

    @Override
    protected void onReload() {
        super.onReload();

        initOrRestart();
    }

    @Override
    public Loader<Cursor> getCursorLoader(int id, Bundle args) {
        return new CursorLoader(this, getIntent().getData(),
                new String[] { Station._ID, Station.COLUMN_NAME_STATION_TYPE_ID, Station.COLUMN_NAME_NAME,
                        "r." + Region.COLUMN_NAME_NAME + " AS " + Region.COLUMN_NAME_NAME_ALIAS,
                        Station.COLUMN_NAME_ROLE, Station.COLUMN_NAME_STANDING },
                Station.COLUMN_NAME_FAVORITE + " = 1", null, null);
    }

    @Override
    protected void onPause() {
        super.onPause();

        standingUpdated = false;
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (!standingUpdated) {
            ProgressDialog waitDialog = new ProgressDialog(this);
            waitDialog.setCancelable(true);
            waitDialog.setMessage(getString(R.string.retreiving_standing));
            waitDialog.show();

            new UpdateStandingAsyncTask(waitDialog).execute(this);
            standingUpdated = true;
        }
    }

    @Override
    public void onLoadFinished(Cursor data) {
        adapter.swapCursor(data);
        if (DbUtil.hasAtLeastOneRow(data)) {
            findViewById(R.id.location_list).setVisibility(View.VISIBLE);
            findViewById(android.R.id.empty).setVisibility(View.GONE);
        } else {
            findViewById(R.id.location_list).setVisibility(View.GONE);
            findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.location_management_menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.ADD_REMOVE_OPTION:
            final Intent paramIntent = new Intent(this, StationAddRemoveActivity.class);
            startActivity(paramIntent);
            return true;

        default:
            return false;
        }
    }

    @Override
    protected void onDestroy() {
        int role = -1;

        if (Stations.getProductionStation() == null && Stations.getTradeStation() == null) {
            role = EOITConst.Stations.BOTH_ROLES;
        } else if (Stations.getTradeStation() == null) {
            role = EOITConst.Stations.TRADE_ROLE;
        } else if (Stations.getProductionStation() == null) {
            role = EOITConst.Stations.PRODUCTION_ROLE;
        }

        if (role >= 0) {
            NoStationDialog dialog = new NoStationDialog(role);
            dialog.show(getSupportFragmentManager(), "no_station_dialog");
        }

        super.onDestroy();
    }
}