Android Open Source - blekit-android Beacons D B






From Project

Back to project page blekit-android.

License

The source code is released under:

Copyright (C) 2014 Upnext Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software wi...

If you think the Android project blekit-android 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

/*
 * Copyright (c) 2014 UP-NEXT. All rights reserved.
 * http://www.up-next.com/*from  w w  w .  j av  a 2s . c om*/
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package com.upnext.blekit.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.upnext.blekit.BeaconEvent;
import com.upnext.blekit.EventOccurenceUnit;

import java.util.Date;

/**
 * Local database used for storing BLEKit entities like events for counting their occurences.
 *
 * @author Roman Wozniak (roman@up-next.com)
 */
public class BeaconsDB extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "BeaconsDB";

    public BeaconsDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        final String CREATE_BEACON_EVENTS_TABLE = "CREATE TABLE " + TABLE_BEACON_EVENTS + " ( " +
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_EVENT + " TEXT, "+
                KEY_BEACON_ID + " TEXT, "+
                KEY_DATE + " INTEGER )";
        db.execSQL(CREATE_BEACON_EVENTS_TABLE);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BEACON_EVENTS);
        this.onCreate(db);
    }

    private static final String TABLE_BEACON_EVENTS = "beacon_events";

    private static final String KEY_ID = "id";
    private static final String KEY_EVENT = "event";
    private static final String KEY_BEACON_ID = "beacon_id";
    private static final String KEY_DATE = "date";

    private static final String[] COLUMNS = {KEY_ID,KEY_EVENT,KEY_BEACON_ID,KEY_DATE};


    /**
     * Adds an event to the events database
     *
     * @param event beacon event
     * @param beaconId beacon identifier
     */
    public void addBeaconEvent( BeaconEvent event, String beaconId ) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_EVENT, event.name());
        values.put(KEY_BEACON_ID, beaconId);
        values.put(KEY_DATE, new Date().getTime());
        db.insert(TABLE_BEACON_EVENTS, null, values);
        db.close();
    }

    /**
     * Returns the number of occurences of event for given beacon id in unit of time.
     *
     * @param event beacon event
     * @param beaconId beacon id
     * @param occurenceUnit occurence unit
     * @return number of occurences
     */
    public int getNumOccurencesForBeaconInTime( BeaconEvent event, String beaconId, EventOccurenceUnit occurenceUnit ) {
        if( occurenceUnit==null ) return 0;

        String query = "SELECT count(*) FROM " + TABLE_BEACON_EVENTS + " WHERE " +
                KEY_BEACON_ID + "='" + beaconId + "' AND " +
                KEY_EVENT + "='" + event.name() + "' AND " +
                KEY_DATE + " BETWEEN " + getStartDateForOccurence(occurenceUnit) + " AND " + new Date().getTime();

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();

        return count;
    }

    public void listAll() {
        String query = "SELECT * FROM " + TABLE_BEACON_EVENTS;
        L.d(query);
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        if( cursor.moveToFirst() ) {
            do {
                L.d( Integer.parseInt(cursor.getString(0)) +
                        " : " + cursor.getString(1) +
                        " : " + cursor.getString(2) +
                        " : " + cursor.getLong(3)
                );
            } while (cursor.moveToNext());
        }

    }


    private static final long MILLIS_HOUR = 60*60*1000;
    private static final long MILLIS_DAY = MILLIS_HOUR*24;
    private static final long MILLIS_MONTH = MILLIS_DAY*30;
    private static final long MILLIS_YEAR = MILLIS_DAY*365;

    private long getStartDateForOccurence( EventOccurenceUnit occurenceUnit ) {
        long now = new Date().getTime();
        switch (occurenceUnit) {
            case HOUR:
                return now - MILLIS_HOUR;

            case DAY:
                return now - MILLIS_DAY;

            case MONTH:
                return now - MILLIS_MONTH;

            case YEAR:
                return now - MILLIS_YEAR;

            case TOTAL:
                return 0;
        }
        return 0;
    }

}




Java Source Code List

com.upnext.blekit.ActionsFactory.java
com.upnext.blekit.AverageIBeacon.java
com.upnext.blekit.BLEKitClient.java
com.upnext.blekit.BLEKitIntentProcessor.java
com.upnext.blekit.BLEKitService.java
com.upnext.blekit.BLEKit.java
com.upnext.blekit.BackgroundMode.java
com.upnext.blekit.BeaconEvent.java
com.upnext.blekit.ConditionsFactory.java
com.upnext.blekit.Config.java
com.upnext.blekit.EventOccurenceUnit.java
com.upnext.blekit.Proximity.java
com.upnext.blekit.StartupReceiver.java
com.upnext.blekit.actions.AlertActionParams.java
com.upnext.blekit.actions.AlertAction.java
com.upnext.blekit.actions.BLEAction.java
com.upnext.blekit.actions.BaseNotificationParams.java
com.upnext.blekit.actions.ContentActionParams.java
com.upnext.blekit.actions.ContentAction.java
com.upnext.blekit.actions.NotificationActionParams.java
com.upnext.blekit.actions.NotificationAction.java
com.upnext.blekit.actions.YelpActionParams.java
com.upnext.blekit.actions.YelpAction.java
com.upnext.blekit.actions.facebook.FacebookCheckinActionParams.java
com.upnext.blekit.actions.facebook.FacebookCheckinAction.java
com.upnext.blekit.actions.facebook.FacebookCheckinActivity.java
com.upnext.blekit.actions.foursquare.FoursquareCheckinActionParams.java
com.upnext.blekit.actions.foursquare.FoursquareCheckinAction.java
com.upnext.blekit.actions.foursquare.FoursquareCheckinActivity.java
com.upnext.blekit.conditions.BLECondition.java
com.upnext.blekit.conditions.CameFarCondition.java
com.upnext.blekit.conditions.CameImmediateCondition.java
com.upnext.blekit.conditions.CameNearCondition.java
com.upnext.blekit.conditions.EnterCondition.java
com.upnext.blekit.conditions.HttpOkCondition.java
com.upnext.blekit.conditions.HttpOkParams.java
com.upnext.blekit.conditions.LeaveCondition.java
com.upnext.blekit.conditions.OccurenceCondition.java
com.upnext.blekit.conditions.OccurenceParams.java
com.upnext.blekit.conditions.StaysCondition.java
com.upnext.blekit.conditions.StaysParams.java
com.upnext.blekit.listeners.BLEKitStateListener.java
com.upnext.blekit.listeners.BeaconEventListener.java
com.upnext.blekit.listeners.ZoneUpdateListener.java
com.upnext.blekit.model.Action.java
com.upnext.blekit.model.Beacon.java
com.upnext.blekit.model.Condition.java
com.upnext.blekit.model.CurrentBeaconProximity.java
com.upnext.blekit.model.Location.java
com.upnext.blekit.model.Trigger.java
com.upnext.blekit.model.Zone.java
com.upnext.blekit.receiver.LoggingReceiver.java
com.upnext.blekit.util.BeaconPreferences.java
com.upnext.blekit.util.BeaconsDB.java
com.upnext.blekit.util.ExpressionEvaluator.java
com.upnext.blekit.util.JsonParser.java
com.upnext.blekit.util.L.java
com.upnext.blekit.util.Rand.java
com.upnext.blekit.util.http.Error.java
com.upnext.blekit.util.http.HttpClient.java
com.upnext.blekit.util.http.HttpUtils.java
com.upnext.blekit.util.http.Response.java