Android Open Source - hackspacewidget Space Data Source






From Project

Back to project page hackspacewidget.

License

The source code is released under:

Apache License

If you think the Android project hackspacewidget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.ring0.hackspace.database;
//from  w  ww  .j  a va  2s . c  o m
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import de.ring0.hackspace.datatypes.Space;

public class SpaceDataSource {
  
  private static final String TABLE_NAME = "spaces";
  private static final String[] COLUMNS = { "id", "name", "url" };

  private InternalHelper helper;
  private SQLiteDatabase db;
  
  public SpaceDataSource(Context context) {
    helper = new InternalHelper(context, TABLE_NAME);
  }
  
  public void open() throws SQLException {
    db = helper.getWritableDatabase();
  }
  
  public void close() {
    helper.close();
  }
  
  /**
   * 
   * @param space
   */
  public void insertSpace(Space space) {
    ContentValues cv = new ContentValues();
    cv.put("name", space.space);
    cv.put("url", space.spaceapi);
    
    db.insert(TABLE_NAME, null, cv);
  }
  
  /**
   * 
   * @param spaces
   */
  public void populateTable(Space[] spaces) {
    db.beginTransaction();
    for(Space s : spaces) {
      ContentValues cv = new ContentValues();
      cv.put("name", s.space);
      cv.put("url", s.spaceapi);
      db.insert(TABLE_NAME, null, cv);
    }
    db.endTransaction();
  }
  
  /**
   * 
   * @param id
   * @return
   */
  public Space getSpace(int id) {
    Space s = new Space();
    
    Cursor c = db.query(TABLE_NAME, COLUMNS, String.format("id = %d", id), null, null, null, null);
    c.moveToFirst();
    s.id = c.getInt(0);
    s.space = c.getString(1);
    s.spaceapi = c.getString(2);
    c.close();
    
    return s;
  }
  
  /**
   * 
   * @param name
   * @return
   */
  public int findSpaceByName(String name) {
    Cursor c = db.query(TABLE_NAME, new String[] {"id"}, String.format("name LIKE \"%s\"", name), null, null, null, null);
    if(c.getCount() > 0) {
      c.moveToFirst();
      c.close();
      return c.getInt(0);
    }
    else {
      c.close();
      return -1;
    }
  }
  
  /**
   * 
   * @return
   */
  public Space[] getAllSpaces() {
    ArrayList<Space> spaces = new ArrayList<Space>();
    Cursor c = db.query(TABLE_NAME, COLUMNS, null, null, null, null, null);
    c.moveToFirst();
    while(!c.isAfterLast()) {
      Space s = new Space();
      s.id = c.getInt(0);
      s.space = c.getString(1);
      s.spaceapi = c.getString(2);
      spaces.add(s);
      c.moveToNext();
    }
    c.close();
    return (Space[]) spaces.toArray();
  }
}




Java Source Code List

de.ring0.hackspace.HackspaceInfoFragment.java
de.ring0.hackspace.HackspaceListFragment.java
de.ring0.hackspace.HackspaceStatusAPI.java
de.ring0.hackspace.HackspaceStatusProvider.java
de.ring0.hackspace.HackspaceViewActivity.java
de.ring0.hackspace.HackspaceWidgetConfig.java
de.ring0.hackspace.UpdateWidgetTask.java
de.ring0.hackspace.database.InternalHelper.java
de.ring0.hackspace.database.SpaceDataSource.java
de.ring0.hackspace.database.StatusDataSource.java
de.ring0.hackspace.datatypes.Space.java
de.ring0.hackspace.datatypes.field.SpaceCache.java
de.ring0.hackspace.datatypes.field.SpaceContact.java
de.ring0.hackspace.datatypes.field.SpaceEvent.java
de.ring0.hackspace.datatypes.field.SpaceFED.java
de.ring0.hackspace.datatypes.field.SpaceFeeds.java
de.ring0.hackspace.datatypes.field.SpaceIcon.java
de.ring0.hackspace.datatypes.field.SpaceKeymaster.java
de.ring0.hackspace.datatypes.field.SpaceLocation.java
de.ring0.hackspace.datatypes.field.SpaceRadioShow.java
de.ring0.hackspace.datatypes.field.SpaceSensors.java
de.ring0.hackspace.datatypes.field.SpaceState.java
de.ring0.hackspace.datatypes.sensors.AccountBalance.java
de.ring0.hackspace.datatypes.sensors.Barometer.java
de.ring0.hackspace.datatypes.sensors.BeverageSupply.java
de.ring0.hackspace.datatypes.sensors.DoorLocked.java
de.ring0.hackspace.datatypes.sensors.Humidity.java
de.ring0.hackspace.datatypes.sensors.NetworkConnections.java
de.ring0.hackspace.datatypes.sensors.PeopleNowPresent.java
de.ring0.hackspace.datatypes.sensors.PowerConsumption.java
de.ring0.hackspace.datatypes.sensors.Radiation.java
de.ring0.hackspace.datatypes.sensors.Sensor.java
de.ring0.hackspace.datatypes.sensors.Temperature.java
de.ring0.hackspace.datatypes.sensors.TotalMemberCount.java
de.ring0.hackspace.datatypes.sensors.Wind.java