package com.efolab.t326;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Main extends Activity {
Context context;
LinearLayout base;
View view;
ListView lv;
private final int resouces = R.layout.listlayout;
private final String[] from = {"_id" , "lat" , "lng" , "update_date" };
private final int[] to = {R.id.textView1 , R.id.textView2 , R.id.textView3 , R.id.textView4};
private HashMap<String , Object> map;
private List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
private SimpleAdapter adapter;
private static final String CLASSNAME = Main.class.getSimpleName();
private static final String DB_NAME = "DB_"+CLASSNAME;
private static final String TABLE_NAME ="T_"+CLASSNAME;
private static final int DB_VER = 1;
private static final String CREATE_TABLE = "CREATE TABLE "+ TABLE_NAME +" ("
+ "seq INTEGER primary key autoincrement"
+ ",lat REAL not null "
+ ",lng REAL not null"
+ ",send_flg integer DEFAULT 0"
+ ",del_flg integer DEFAULT 0"
+ ",update_date TIMESTAMP DEFAULT (DATETIME('now','localtime'))"
+ ");";
private SQLiteDatabase db;
private SQLiteOpenHelper helper;
private String sql = null;
private int db_count = 0;
LocationManager lm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
base = new LinearLayout(context);
base.setOrientation(LinearLayout.VERTICAL);
setContentView(base);
helper = new SQLiteOpenHelper(context, DB_NAME, null, DB_VER) {
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(CREATE_TABLE);
}catch(SQLException e){
Log.e(CLASSNAME,"e:"+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
TODO
}
};
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lv = new ListView(context);
adapter = new SimpleAdapter(context, data, resouces, from, to){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 == 0) {
view.setBackgroundColor(Color.BLACK);
} else {
view.setBackgroundColor(Color.DKGRAY);
}
return view;
}
};
lv.setAdapter(adapter);
base.addView(lv);
sql = "select seq as _id , lat , lng , update_date from " + TABLE_NAME + " where del_flg = 0 and send_flg = 0;";
db = helper.getReadableDatabase();
Cursor c = db.rawQuery(sql, null);
db_count = c.getCount();
c.moveToFirst();
for(int i=0;i<c.getCount();i++){
map = new HashMap<String , Object>();
map.put("_id", c.getInt(0));
map.put("lat", c.getDouble(1));
map.put("lng", c.getDouble(2));
map.put("update_date", c.getString(3));
data.add(map);
c.moveToNext();
}
db.close();
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
TODO
super.onResume();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 10,l);
}
@Override
protected void onPause() {
TODO
super.onPause();
lm.removeUpdates(l);
}
LocationListener l = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
TODO
}
@Override
public void onProviderEnabled(String provider) {
TODO
}
@Override
public void onProviderDisabled(String provider) {
TODO
}
@Override
public void onLocationChanged(Location location) {
TODO
sql = "insert into " + TABLE_NAME + " (lat,lng) values (" + location.getLatitude() +" , " + location.getLongitude() + " );";
db = helper.getWritableDatabase();
db.execSQL(sql);
db.close();
sql = "select seq as _id , lat , lng , update_date from " + TABLE_NAME + " where del_flg = 0 and send_flg = 0;";
db = helper.getReadableDatabase();
Cursor c = db.rawQuery(sql, null);
Toast.makeText(context, "db_count"+db_count+",c.getCount():"+c.getCount(), Toast.LENGTH_SHORT).show();
c.moveToPosition(db_count);
for(int i=db_count;i<c.getCount();i++){
Toast.makeText(context, "i"+i, Toast.LENGTH_SHORT).show();
map = new HashMap<String , Object>();
map.put("_id", c.getInt(0));
map.put("lat", c.getDouble(1));
map.put("lng", c.getDouble(2));
map.put("update_date", c.getString(3));
data.add(map);
c.moveToNext();
}
db_count = c.getCount();
db.close();
adapter.notifyDataSetChanged();
}
};
}