Android Open Source - SenqmAndroid Database Helper






From Project

Back to project page SenqmAndroid.

License

The source code is released under:

GNU General Public License

If you think the Android project SenqmAndroid 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  2014 Alban GRUIN//from  ww  w  . java  2s  .  co  m
 *
 *      This file is part of SenqmAndroid.
 *
 *      SenqmAndroid 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.
 *
 *      SenqmAndroid 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 SenqmAndroid.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.agrn.senqm.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.agrn.senqm.database.entities.Computer;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.ArrayList;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper
{
    private static DatabaseHelper instance = null;

    private static final String DBNAME = "senqm.sqlite";
    private static final int DBVERSION = 1;

    private Dao<Computer, Integer> computerDao;

    public static DatabaseHelper getInstance()
    {
        return instance;
    }

    public static DatabaseHelper getInstance(Context context)
    {
        if(instance == null || !instance.isOpen())
            instance = new DatabaseHelper(context);

        return instance;
    }

    private DatabaseHelper(Context context)
    {
        super(context, DBNAME, null, DBVERSION);

        getDaos();
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) throws RuntimeException
    {
        try
        {
            TableUtils.createTableIfNotExists(connectionSource, Computer.class);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion)
    {
    }

    public ArrayList<Computer> getComputerList()
    {
        ArrayList<Computer> computerList = new ArrayList<>();

        for(Computer computer: computerDao.getWrappedIterable())
            computerList.add(computer);

        return computerList;
    }

    private void getComputerDao()
    {
        try
        {
            computerDao = getDao(Computer.class);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private void getDaos()
    {
        if(computerDao == null)
            getComputerDao();
    }
}




Java Source Code List

com.agrn.senqm.MainActivity.java
com.agrn.senqm.database.DatabaseHelper.java
com.agrn.senqm.database.entities.Computer.java
com.agrn.senqm.network.Message.java
com.agrn.senqm.network.WifiLockContainer.java
com.agrn.senqm.network.connection.ConnectionHandler.java
com.agrn.senqm.network.connection.Connection.java
com.agrn.senqm.network.service.ConnectionManagerServiceBoundListener.java
com.agrn.senqm.network.service.ConnectionManagerServiceConnection.java
com.agrn.senqm.network.service.ConnectionManagerService.java
com.agrn.senqm.telephony.PhoneBroadcaster.java
com.agrn.senqm.telephony.sms.SMSBroadcaster.java
com.agrn.senqm.telephony.sms.SendSMS.java