package com.nimbits.android;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.nimbits.android.database.DataBaseHelper;
import com.nimbits.android.object.DataPoint;
import com.nimbits.android.object.PointCategory;
import com.nimbits.android.object.RecordedValue;
import com.nimbits.google.DataClient;
import com.nimbits.google.GoogleAuthentication;
public class CategoryActivity extends ListActivity {
static final int PROGRESS_DIALOG = 0;
static final int POINT_DIALOG = 1;
static final int CHANGESERVER_DIALOG = 2;
ProgressThread progressThread;
PointThread pointThread;
ProgressDialog progressDialog;
ProgressDialog pointDialog;
private GoogleAuthentication G;
static final int New_Cataogy_ID = 0;
private String currentCategory=null;
public static final String hiddenCategory = "Nimbits_Unsorted";
// private static String DB_PATH = "/data/data/com.nimbits.android/databases/";
final static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss Z").serializeNulls().create();
// private static String DB_NAME = "nimbits";
/** Nested class that performs progress calculations (counting) */
private final String ServerPrefID = "SERVERURL";
private final String PREFS_NAME = "NimbitsPrefs";
public String getServerURL()
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String url = settings.getString(ServerPrefID,"nimbits1.appspot.com");
return url;
}
public void setServerURL(String url)
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(ServerPrefID,url);
editor.commit();
}
private SQLiteDatabase getDB(boolean writeable)
{
DataBaseHelper myDbHelper = new DataBaseHelper(CategoryActivity.this);
SQLiteDatabase db1 = null;
if (writeable)
{
db1 = myDbHelper.getWritableDatabase();
}
else
{
db1 = myDbHelper.getReadableDatabase();
}
db1.setVersion(1);
db1.setLocale(Locale.getDefault());
db1.setLockingEnabled(true);
myDbHelper = null;
return db1;
}
private class ProgressThread extends Thread {
Handler mHandler;
final static int STATE_DONE = 0;
final static int STATE_RUNNING = 1;
@SuppressWarnings("unused")
int mState;
@SuppressWarnings("unused")
int total;
ProgressThread(Handler h) {
mHandler = h;
}
private void update(int c, boolean loggedIn)
{
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("total", c);
b.putBoolean("loggedIn", loggedIn);
msg.setData(b);
mHandler.sendMessage(msg);
}
public void run() {
mState = STATE_RUNNING;
total = 0;
boolean loggedIn;
update(6, false);
boolean isAuth = isUserAuthenticated();
update(7, false);
if ( isAuth )
{
loggedIn = true;
SQLiteDatabase db1 = getDB(true);
Cursor c;
update(10, loggedIn);
boolean dbempty = false;
try {
c = db1.query(DataBaseHelper.POINT_TABLE_NAME, new String[] {"_id", "NAME", "DESCRIPTION", "DISPLAYTYPE"}, null , null, null, null, "DISPLAYTYPE");
dbempty = c.isAfterLast();
c.close();
//Log.v("dbtest","" +
} catch (Exception e) {
Log.v("dbtest",e.getMessage());
}
update(20, loggedIn);
List<PointCategory> Categories;
if (dbempty)
{
Categories =DataClient.getCategories(getServerURL());
update(25, loggedIn);
ContentValues values = new ContentValues();
ContentValues pvalues = new ContentValues();
int i = 30;
if (Categories != null)
{
update(i, loggedIn);
for (PointCategory ct : Categories)
{
i++;
update(i, loggedIn);
if (! ct.getName().equals(hiddenCategory))
{
values.put("NAME", ct.getName());
values.put("DISPLAYTYPE", 1);
values.put("JSONPOINTS", ct.getJsonPointCollection());
int pcount = 0;
if (ct.getPoints() != null)
{
pcount = ct.getPoints().size();
for (DataPoint p : ct.getPoints())
{
pvalues.put("CategorY",ct.getName());
pvalues.put("NAME",p.getName());
String unit = "";
String note = "";
String v = "";
if (p.getUnit() != null)
{
unit = p.getUnit();
}
if (v != null || note !=null)
{
pvalues.put("DESCRIPTION", v + " " + unit + " " + note);
}
pvalues.put("DISPLAYTYPE", 2);
pvalues.put("JSON",gson.toJson(p));
db1.insert(DataBaseHelper.POINT_TABLE_NAME, null,pvalues);
}
}
values.put("DESCRIPTION", pcount + " points");
db1.insert(DataBaseHelper.MAIN_TABLE_NAME, null,values);
}
else
{
List<DataPoint> pa = ct.getPoints();
if (pa != null)
{
for (DataPoint p : pa )
{
String unit = "";
String note = "";
RecordedValue currentValue;
currentValue = DataClient.getLastRecordedValue(p.getName(), getServerURL());
if (p.getUnit() != null)
{
unit = p.getUnit();
}
values.put("NAME",p.getName());
values.put("DISPLAYTYPE", 2);
if (currentValue.getNote() != null)
{
note = currentValue.getNote();
}
if (currentValue !=null)
{
values.put("DESCRIPTION", currentValue.getValue() + " " + unit + " " + note);
}
db1.insert(DataBaseHelper.MAIN_TABLE_NAME, null,values);
pvalues.put("CategorY",ct.getName());
pvalues.put("NAME",p.getName());
pvalues.put("DESCRIPTION",p.getDescription());
pvalues.put("DISPLAYTYPE", 2);
//pvalues.put("JSON",gson.toJson(p));
db1.insert(DataBaseHelper.POINT_TABLE_NAME, null,pvalues);
}
}
}
}
}
}
db1.close();
}
else
{
loggedIn = false;
Log.e("Not logged in","not logged in");
}
total = 100;
update(100, loggedIn);
mState = STATE_DONE;
}
/* sets the current state for the thread,
* used to stop the thread */
public void setState(int state) {
mState = state;
}
}
private class PointThread extends Thread {
Handler mHandler;
final static int STATE_DONE = 0;
final static int STATE_RUNNING = 1;
@SuppressWarnings("unused")
int mState;
@SuppressWarnings("unused")
int total;
String selectedCategory;
PointThread(Handler h, String CategoryName) {
selectedCategory = CategoryName;
mHandler = h;
}
/* sets the current state for the thread,
* used to stop the thread */
public void setState(int state) {
mState = state;
}
private void update(int c )
{
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
b.putInt("total", c);
msg.setData(b);
mHandler.sendMessage(msg);
}
public void run() {
mState = STATE_RUNNING;
RecordedValue v;
String pointName;
String json;
String note;
String unit;
Log.v("point thread", "start");
SQLiteDatabase db1 = getDB(true);
Cursor c;
c = db1.query(DataBaseHelper.POINT_TABLE_NAME, new String[] {"_id", "NAME", "DESCRIPTION", "DISPLAYTYPE","JSON"}, "CategorY='" + selectedCategory +"'" , null, null, null, "DISPLAYTYPE");
int count = c.getCount();
Log.v("point thread", "selected " + selectedCategory);
Log.v("point thread", "count " + count);
update(0);
if (count > 0)
{
int d = 100 / count;
int progress = 0;
//update(count);
c.moveToFirst();
while (! c.isAfterLast())
{
pointName = c.getString(c.getColumnIndex("NAME"));
json = c.getString(c.getColumnIndex("JSON"));
DataPoint p = gson.fromJson(json,DataPoint.class);
note = "";
unit = "";
v = DataClient.getLastRecordedValue(pointName, getServerURL());
if (p.getUnit() !=null)
{
unit= p.getUnit();
}
if (v.getNote() !=null)
{
note = v.getNote();
}
ContentValues u = new ContentValues();
u.put("DESCRIPTION", v.getValue() + " " + unit + " " + note);
db1.update(DataBaseHelper.POINT_TABLE_NAME, u, "NAME=?", new String[] {pointName});
progress += d;
update(progress);
c.moveToNext();
}
c.close();
db1.close();
update(100);
}
else
{
update(100);
}
update(100);
}
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case PROGRESS_DIALOG:
progressDialog = new ProgressDialog(CategoryActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressThread = new ProgressThread(handler);
progressThread.start();
return progressDialog;
case POINT_DIALOG:
pointDialog = new ProgressDialog(CategoryActivity.this);
pointDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pointDialog.setMessage("Loading Current Values...");
pointThread = new PointThread(pointHandler, currentCategory);
pointThread.start();
return pointDialog;
case CHANGESERVER_DIALOG:
// ProgressDialog dialog = ProgressDialog.show(CategoryActivity.this, "",
// "Loading. Please wait...", true);
//
// Context mContext = getApplicationContext();
final Dialog dialog1 = new Dialog(CategoryActivity.this);
//
dialog1.setOnDismissListener(new DialogInterface.OnDismissListener () {
public void onDismiss(DialogInterface dialog) {
EditText urlText = (EditText) dialog1.findViewById(R.id.new_value);
setServerURL(urlText.getText().toString());
}
});
dialog1.setContentView(R.layout.text_prompt);
dialog1.setTitle("Change Server");
//
TextView text = (TextView) dialog1.findViewById(R.id.text);
text.setText("You can point your android device to another Nimbits Server URL (i.e yourserver.appspot.com)");
EditText urlText = (EditText) dialog1.findViewById(R.id.new_value);
urlText.setText( getServerURL());
return dialog1;
default:
return null;
}
}
// Define the Handler that receives messages from the thread and update the progress
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
boolean loggedIn = msg.getData().getBoolean("loggedIn");
if (progressDialog != null)
{
progressDialog.setProgress(total);
Log.v("handler","" + total);
if (total >= 100){
progressDialog.setProgress(0);
dismissDialog(PROGRESS_DIALOG);
removeDialog(PROGRESS_DIALOG);
progressThread.setState(ProgressThread.STATE_DONE);
progressDialog = null;
if (loggedIn)
{
loadView();
}
else
{
Toast.makeText(CategoryActivity.this, "Could not log in. Your google account cannot be accessed or found on this phone.", Toast.LENGTH_LONG).show();
}
}
}
}
};
final Handler pointHandler = new Handler() {
public void handleMessage(Message msg) {
int total = msg.getData().getInt("total");
if (pointDialog != null)
{
pointDialog.setProgress(total);
if (total >= 100){
pointDialog.setProgress(0);
dismissDialog(POINT_DIALOG);
removeDialog(POINT_DIALOG);
pointThread.setState(PointThread.STATE_DONE);
pointDialog = null;
loadPointView(currentCategory);
}
}
}
};
private class OnNewCategoryListener implements CustomDialog.ReadyListener {
public void ready(String name) {
if (! name.trim().equals(""))
{
try {
DataClient.addCategory( getServerURL(), name);
SQLiteDatabase db1 = null;
db1 = getDB(true);
ContentValues values = new ContentValues();
values.put("NAME", name);
values.put("DISPLAYTYPE", 1);
values.put("DESCRIPTION", 0 + " points");
db1.insert(DataBaseHelper.MAIN_TABLE_NAME, null,values);
db1.close();
Toast.makeText(CategoryActivity.this, "Added Category " + name +". Click on the Category to add Data Points to it", Toast.LENGTH_LONG).show();
loadView();
} catch (IOException e) {
Toast.makeText(CategoryActivity.this, e.getMessage(),Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private class OnCreatePointListener implements CustomDialog.ReadyListener {
public void ready(String name) {
SQLiteDatabase db1 = getDB(true);
if (! name.trim().equals(""))
{
try {
String c = "";
if (currentCategory == null)
{
c = hiddenCategory;
}
else
{
c = currentCategory;
}
String json = DataClient.addPoint(getServerURL(), name, c) ;
Log.v("j",json);
ContentValues pvalues = new ContentValues();
ContentValues cvalues = new ContentValues();
if (currentCategory !=null)
{
pvalues.put("CategorY",currentCategory);
}
else
{
pvalues.put("CategorY",hiddenCategory);
cvalues.put("NAME",name);
cvalues.put("DISPLAYTYPE", 2);
cvalues.put("DESCRIPTION","");
db1.insert(DataBaseHelper.MAIN_TABLE_NAME, null,cvalues);
}
pvalues.put("NAME",name);
pvalues.put("DESCRIPTION","");
pvalues.put("DISPLAYTYPE", 2);
pvalues.put("JSON",json);
db1.insert(DataBaseHelper.POINT_TABLE_NAME, null,pvalues);
db1.close();
if (currentCategory !=null)
{
loadPointView(currentCategory);
}
else
{
loadView();
}
Toast.makeText(CategoryActivity.this, "Added Point " + name + ". Go to " + getServerURL() + " to configure advanced properties", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(CategoryActivity.this, e.getMessage(),Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void loadView()
{
DataBaseHelper myDbHelper = new DataBaseHelper(CategoryActivity.this);
currentCategory= null;
Log.v("loadview", "loading");
SQLiteDatabase db1 = getDB(false);
Cursor c;
c = db1.query(DataBaseHelper.MAIN_TABLE_NAME, new String[] {"_id", "NAME", "DESCRIPTION", "DISPLAYTYPE"}, null, null, null, null, "DISPLAYTYPE");
ListAdapter adapter = new ImageCursorAdapter(
this, // Context.
R.layout.main_list, // Specify the row template to use (here, two columns bound to the two retrieved cursor
c, // Pass in the cursor to bind to.
new String[] {"NAME","DESCRIPTION"}, // Array of cursor columns to bind to.
new int[] {R.id.text1, R.id.text2}); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(adapter);
}
public void loadPointView(String Category)
{
this.setTitle(Category);
currentCategory= Category;
SQLiteDatabase db1 = getDB(false);
Cursor c;
c = db1.query(DataBaseHelper.POINT_TABLE_NAME, new String[] {"_id", "NAME", "DESCRIPTION", "DISPLAYTYPE"}, "CategorY='" + Category +"'" , null, null, null, "DISPLAYTYPE");
ListAdapter adapter = new ImageCursorAdapter(
this, // Context.
R.layout.main_list, // Specify the row template to use (here, two columns bound to the two retrieved cursor
c, // Pass in the cursor to bind to.
new String[] {"NAME","DESCRIPTION"}, // Array of cursor columns to bind to.
new int[] {R.id.text1, R.id.text2}); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(adapter);
}
private String getSelectedPointJSON(String pointName)
{
SQLiteDatabase db1 = getDB(false);
Cursor c;
c = db1.query(DataBaseHelper.POINT_TABLE_NAME, new String[] {"_id", "JSON"}, "NAME='" + pointName +"'" , null, null, null, null);
c.moveToFirst();
String retVal = c.getString(c.getColumnIndex("JSON"));
c.close();
db1.close();
return retVal;
}
private boolean isUserAuthenticated()
{
Log.v("isUserAuthenticated", "Checking Auth");
boolean retVal = false;
String result = null;
G = GoogleAuthentication.getNewGoogleAuthentication();
if (G.ConnectClean(CategoryActivity.this, getServerURL()))
{
try {
Log.v("isUserAuthenticated", "call to get logged in");
result = G.GetLoggedIn(getServerURL());
Log.v("isUserAuthenticated", result);
} catch (IOException e1) {
Log.e("Login",result);
Log.e("Login", e1.getMessage());
}
}
else
{
//Toast.makeText(CategoryActivity.this, "Could not find your google account. Does this phone have a google account configured on it under accounts / security?", Toast.LENGTH_LONG).show();
}
// Log.e("Login",result);
if (result != null)
{
retVal = result.equals("true");
}
else
{
retVal = false;
}
return retVal;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.catagorylayout);
String reload = "true";
Bundle b = getIntent().getExtras();
String Category = null;
if (b !=null)
{
reload = b.getString("reload");
Category = b.getString("Category");
Log.v("Load Category", reload + " " + Category);
}
if (reload.equals("false"))
{
if (Category != null)
{
currentCategory = Category;
showDialog(POINT_DIALOG);
}
else
{
loadView();
}
}
else
{
try {
DataBaseHelper myDbHelper = new DataBaseHelper(CategoryActivity.this);
myDbHelper.createDataBase();
myDbHelper = null;
} catch (IOException e1) {
Log.e("DB", "create db error", e1);
}
showDialog(PROGRESS_DIALOG);
}
}
private void ViewMap()
{
if (this.currentCategory == null)
{
Toast.makeText(CategoryActivity.this,"Please select a Category to or point to view on the map", Toast.LENGTH_LONG).show();
}
else
{
SQLiteDatabase db1 = getDB(false);
Cursor c;
c = db1.query(DataBaseHelper.MAIN_TABLE_NAME, new String[] {"_id", "JSONPOINTS"}, "NAME='" + currentCategory +"'" , null, null, null, null);
c.moveToFirst();
String json = c.getString(c.getColumnIndex("JSONPOINTS"));
c.close();
Bundle b = new Bundle();
Intent intent = new Intent();
b.putString("type", "Category");
b.putString("Category", currentCategory);
b.putString("json",json);
intent.putExtras( b);
intent.setClass( CategoryActivity.this , MapViewActivity.class);
startActivity(intent);
c.close();
db1.close();
finish();
}
}
private void ViewChart()
{
if (this.currentCategory == null)
{
Toast.makeText(CategoryActivity.this,"Please select a Category to or point to view a chart", Toast.LENGTH_LONG).show();
}
else
{
SQLiteDatabase db1 = getDB(false);
Cursor c;
c= db1.query(DataBaseHelper.MAIN_TABLE_NAME, new String[] {"_id", "JSONPOINTS"}, "NAME='" + currentCategory +"'" , null, null, null, null);
c.moveToFirst();
String json = c.getString(c.getColumnIndex("JSONPOINTS"));
c.close();
db1.close();
Bundle b = new Bundle();
Intent intent = new Intent();
b.putString("type", "Category");
b.putString("Category", currentCategory);
b.putString("json",json);
intent.putExtras( b);
intent.setClass( CategoryActivity.this ,ChartActivity.class);
startActivity(intent);
finish();
}
}
private void createCategory()
{
//setContentView(0);
try {
CustomDialog myDialog = new CustomDialog(this, "","New Category Name:",
new OnNewCategoryListener());
myDialog.show();
// al.add(myDialog.getEntry());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Create Category",e.getMessage());
}
//dialog.show();
}
private void createPoint()
{
//setContentView(0);
try {
CustomDialog myDialog = new CustomDialog(this, "","New Point Name:",
new OnCreatePointListener());
myDialog.show();
// al.add(myDialog.getEntry());
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Create Category",e.getMessage());
}
//dialog.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.New_Point_Catagory:
createCategory();
return true;
case R.id.New_Data_Point:
createPoint();
return true;
case R.id.main_menu:
loadView();
return true;
case R.id.refresh:
SQLiteDatabase db1 = getDB(false);
db1.execSQL("delete from " + DataBaseHelper.MAIN_TABLE_NAME);
db1.execSQL("delete from " + DataBaseHelper.POINT_TABLE_NAME);
db1.close();
showDialog(PROGRESS_DIALOG);
return true;
case R.id.view_map:
ViewMap();
return true;
case R.id.view_chart:
ViewChart();
return true;
case R.id.exit:
onTerminate();
return true;
case R.id.ChangeServer:
changeServer();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void changeServer()
{
showDialog( CHANGESERVER_DIALOG);
//ImageView image = (ImageView) dialog.findViewById(R.id.image);
//image.setImageResource(R.drawable.android);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
@Override
protected void onListItemClick(ListView l, View v, int position,
long id) {
super.onListItemClick(l, v, position, id);
Context context = getApplicationContext();
ImageView icon = (ImageView) v.findViewById(R.id.icon1);
TextView d = (TextView) v.findViewById(R.id.text1);
if (icon.getTag().toString().equals("point"))
{
String json = getSelectedPointJSON((String) d.getText());
Bundle b = new Bundle();
Intent intent = new Intent();
b.putString("Category", this.currentCategory);
b.putString("point",(String) d.getText());
b.putString("json",json);
intent.putExtras( b);
intent.setClass(CategoryActivity.this, PointActivity.class );
startActivity(intent);
finish();
}
else
{
currentCategory = (String) d.getText();
Log.v("click", "Loading points thread");
showDialog(POINT_DIALOG);
//loadPointView();
}
}
@Override
public void onDestroy(){
Log.v("Category", "ondestroy");
super.onDestroy();
this.finish();
}
public void onTerminate() {
Log.v("Category", "ondestroy");
// clean up application global
super.onStop();
super.onDestroy();
this.finish(); // /// close the application
}
}
|