cl.mmoscoso.geocomm.GeoCommMainActivity.java Source code

Java tutorial

Introduction

Here is the source code for cl.mmoscoso.geocomm.GeoCommMainActivity.java

Source

/**
Copyright 2014 Fabian Ramirez Barrios
This file is part of GeoComm.
    
GeoComm 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.
    
GeoComm 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 GeoComm.  If not, see <http://www.gnu.org/licenses/>.
    
**/
package cl.mmoscoso.geocomm;

/**
 * @author Fabian Ramirez
 * @author mmoscoso
 */
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.view.MenuItemCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toast;
import cl.mmoscoso.geocomm.entity.GeoCommGlobalClass;
import cl.mmoscoso.geocomm.entity.GeoCommPoint;
import cl.mmoscoso.geocomm.entity.GeoCommRoute;
import cl.mmoscoso.geocomm.entity.GeoCommUser;
import cl.mmoscoso.geocomm.gui.GeoCommListRoutesAdapter;
import cl.mmoscoso.geocomm.gui.GeoCommLogInActivity;
import cl.mmoscoso.geocomm.gui.GeoCommMapViewActivity;
import cl.mmoscoso.geocomm.gui.GeoCommOpcionUserActivity;
import cl.mmoscoso.geocomm.gui.GeoCommRegisterActivity;
import cl.mmoscoso.geocomm.sync.GeoCommLogInAsyncTask;
import cl.mmoscoso.geocomm.sync.GeoCommRegisterAsyncTask;
import cl.mmoscoso.geocomm.sync.GeoCommGetPointsAsyncTask;
import cl.mmoscoso.geocomm.sync.GeoCommGetRoutesAsyncTask;

public class GeoCommMainActivity extends Activity implements OnItemClickListener, OnQueryTextListener {

    private ListView list_routes;
    public LocationManager locationManager;
    private final String TAGNAME = "GeoCommMainActivity";
    List<GeoCommRoute> routes;
    List<GeoCommPoint> points;
    TextView textview_name_user;
    Button button_myroutes;
    MenuItem item;
    GeoCommUser user;
    SearchView searchView;
    List<GeoCommRoute> aux_routes = new ArrayList<GeoCommRoute>();

    private static int MAPVIEW_ACTIVITY = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_geo_comm_main);

        //this.textview_name_user = (TextView) findViewById(R.id.text_view_status);
        //this.button_myroutes = (Button) findViewById(R.id.button_myroutes);        
        this.list_routes = (ListView) findViewById(R.id.list_routes);

        //locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, this);
        list_routes.setClickable(true);
        list_routes.setOnItemClickListener(this);

        SharedPreferences preferences = getSharedPreferences("userInformation", MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean("is_login", false);
        edit.commit();

        getRoutes(new View(this));
    }

    public void getRoutes(View view) {
        routes = new ArrayList<GeoCommRoute>();
        GeoCommGlobalClass gc = new GeoCommGlobalClass();
        String host = gc.getHostRoutes();
        GeoCommGetRoutesAsyncTask get = new GeoCommGetRoutesAsyncTask(this, host, routes, this.list_routes);
        get.execute();
    }

    public void storage() {
        for (int j = 0; j < this.routes.size(); j++) {
            aux_routes.add(this.routes.get(j));
        }
    }

    public void Connected(String name, int id) {
        Intent intent = new Intent(this, GeoCommOpcionUserActivity.class);
        intent.putExtra("IDUSER", id);
        intent.putExtra("NAMEUSER", name);
        intent.putExtra("FROMLOGIN", true);
        startActivity(intent);
    }

    /**
     * Method for execute action when item is clicked
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        Log.i(TAGNAME, "click on list item " + aux_routes.get(position).getId());
        Intent intent = new Intent(this, GeoCommMapViewActivity.class);
        intent.putExtra("IDROUTE", aux_routes.get(position).getId());
        startActivityForResult(intent, MAPVIEW_ACTIVITY);
        //startActivityForResult(intent,GeoCommMainActivity.class);
        //startActivitky(intent);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == MAPVIEW_ACTIVITY) {
            if (resultCode == RESULT_OK) {
                //MAP VIEW END OK
            } else {
                Log.e(TAGNAME, "Error with MAPVIEW");
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.map_view_main, menu);
        //SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        //menu.findItem(R.id.action_search).getActionView();
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        // Assumes current activity is the searchable activity
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(this);
        return true;
    }

    public void ErrorLogin() {
        Intent intent = new Intent(this, GeoCommLogInActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.register) {
            //Log.i(TAGNAME, "CLICK!!!");
            Intent intent = new Intent(this, GeoCommRegisterActivity.class);
            startActivity(intent);
        }
        if (id == R.id.log_in) {
            SharedPreferences preferences = getSharedPreferences("userInformation", MODE_PRIVATE);
            String peference_name = preferences.getString("name", null);
            String peference_pass = preferences.getString("pass", null);
            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
            Boolean bool_longin = sharedPref.getBoolean("log_in", false);
            //Log.i(TAGNAME, "LOG: "+bool_longin);
            //Log.i(TAGNAME, "peference_pass: "+peference_pass);
            //Log.i(TAGNAME, "peference_name: "+peference_name);
            if (bool_longin && peference_pass != null && peference_name != null) {
                GeoCommGlobalClass gc = new GeoCommGlobalClass();
                String host = gc.getHostLogin();
                GeoCommLogInAsyncTask get = new GeoCommLogInAsyncTask(this, host, peference_name, peference_pass,
                        user);
                get.execute();
                //Log.i(TAGNAME, "Entro!");
            } else {
                Intent intent = new Intent(this, GeoCommLogInActivity.class);
                startActivity(intent);
            }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        //Log.i(TAGNAME, "tamao: "+this.routes.size());
        try {
            aux_routes.clear();
            for (int j = 0; j < this.routes.size(); j++) {
                aux_routes.add(this.routes.get(j));
            }
            //Log.i(TAGNAME, "CAMBIOOO!!!"+newText);
            int i = 0;
            while (i < aux_routes.size()) {
                if (this.aux_routes.get(i).getName().toUpperCase().contains(newText.toUpperCase())) {// || this.aux_routes.get(i).getName().contains(newText.toLowerCase())){// \b123wood\
                    //if (this.aux_routes.get(i).getName().contains(newText)){
                    //Log.i(TAGNAME, aux_routes.get(i).getName());   
                } else {
                    aux_routes.remove(i);
                    i = -1;
                }
                i++;
            }
            GeoCommListRoutesAdapter adapter = new GeoCommListRoutesAdapter(this, aux_routes);
            this.list_routes.setAdapter(adapter);
        } catch (NullPointerException e) {
            // TODO: handle exception
            Toast.makeText(this, R.string.error_notelementListview, Toast.LENGTH_SHORT).show();
        }

        return false;
    }

}