Android Open Source - SoundScheduler Rules Data Source






From Project

Back to project page SoundScheduler.

License

The source code is released under:

GNU General Public License

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

/*
 * Sound Scheduler// www  . j  a  v  a 2s .  c  o  m
 * Copyright (C) 2013 Victor Kifer
 */

package com.victorkifer.SoundScheduler.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.victorkifer.SoundScheduler.entities.Rule;

import java.util.ArrayList;
import java.util.List;

public class RulesDataSource {
    private SQLiteDatabase db;
    private RulesDatabaseHelper dbHelper;
    private String[] columns = {
            RulesDatabaseHelper.RULES_COLUMN_ID,
            RulesDatabaseHelper.RULES_COLUMN_START,
            RulesDatabaseHelper.RULES_COLUMN_END,
            RulesDatabaseHelper.RULES_COLUMN_ACTIVE,
            RulesDatabaseHelper.RULES_COLUMN_APPLIED,
            RulesDatabaseHelper.RULES_COLUMN_REPEAT,
            RulesDatabaseHelper.RULES_COLUMN_STATE_BEFORE,
            RulesDatabaseHelper.RULES_COLUMN_STATE_TO_APPLY
    };
    private final int[] DayValues = { 1, 2, 4, 8, 16, 32, 64 };

    public RulesDataSource(Context context) {
        dbHelper = new RulesDatabaseHelper(context);
    }

    public void open() throws SQLException {
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public long addRule(Rule rule) {
        ContentValues values = ruleToContentValues(rule);

        long insertID = db.insert(RulesDatabaseHelper.TABLE_RULES,
                null,
                values);

        return insertID;
    }

    public void deleteRule(Rule rule) {
        db.delete(RulesDatabaseHelper.TABLE_RULES,
                RulesDatabaseHelper.RULES_COLUMN_ID + " = " + rule.getId(),
                null);
    }

    public List<Rule> getAllRules() {
        List<Rule> rules = new ArrayList<Rule>();

        Cursor cursor = db.query(
                RulesDatabaseHelper.TABLE_RULES,
                columns,
                null,
                null,
                null,
                null,
                null
        );

        while(cursor.moveToNext()) {
            Rule rule = cursorToRule(cursor);
            rules.add(rule);
        }

        cursor.close();

        return rules;
    }

    public List<Rule> getActiveRules() {
        List<Rule> rules = new ArrayList<Rule>();

        Cursor cursor = db.query(
                RulesDatabaseHelper.TABLE_RULES,
                columns,
                RulesDatabaseHelper.RULES_COLUMN_ACTIVE + " = '1'",
                null,
                null,
                null,
                null
        );

        while(cursor.moveToNext()) {
            Rule rule = cursorToRule(cursor);
            rules.add(rule);
        }

        cursor.close();

        return rules;
    }

    public boolean hasAppliedRule() {
        Cursor c = db.rawQuery("SELECT COUNT(*) FROM "+ RulesDatabaseHelper.TABLE_RULES + " WHERE " +
                RulesDatabaseHelper.RULES_COLUMN_APPLIED + "='1'", null);
        c.moveToFirst();
        boolean res = c.getInt(0) > 0;
        c.close();
        return res;
    }

    public void updateRule(Rule rule) {
        ContentValues values = ruleToContentValues(rule);

        db.update(
                RulesDatabaseHelper.TABLE_RULES,
                values,
                RulesDatabaseHelper.RULES_COLUMN_ID + " =?",
                new String[] { String.valueOf(rule.getId()) }
        );
    }

    public Rule getRuleById(long ruleId) {
        Cursor cursor = db.query(
                RulesDatabaseHelper.TABLE_RULES,
                columns,
                RulesDatabaseHelper.RULES_COLUMN_ID + " = '" + ruleId + "'",
                null,
                null,
                null,
                null
        );
        if(cursor.moveToNext()) {
            return cursorToRule(cursor);
        }
        return null;
    }


    private Rule cursorToRule(Cursor cursor) {
        Rule rule = new Rule();
        rule.setId(cursor.getLong(cursor.getColumnIndex(RulesDatabaseHelper.RULES_COLUMN_ID)));
        rule.setStartTime(cursor.getLong(cursor.getColumnIndex(RulesDatabaseHelper.RULES_COLUMN_START)));
        rule.setEndTime(cursor.getLong(cursor.getColumnIndex(RulesDatabaseHelper.RULES_COLUMN_END)));
        rule.setActive(cursor.getInt(cursor.getColumnIndex(RulesDatabaseHelper.RULES_COLUMN_ACTIVE)) > 0);
        rule.setApplied(cursor.getInt(cursor.getColumnIndex(RulesDatabaseHelper.RULES_COLUMN_APPLIED)) > 0);

        int repeat = cursor.getInt(cursor.getColumnIndex(RulesDatabaseHelper.RULES_COLUMN_REPEAT));
        boolean[] arr = new boolean[7];
        for(int i = 6; i>=0; i--) {
            if(repeat >= DayValues[i]) {
                arr[i] = true;
                repeat -= DayValues[i];
            }
        }
        rule.setDaysRepeat(arr);

        rule.setStateBefore(cursor.getInt(cursor.getColumnIndex(RulesDatabaseHelper.RULES_COLUMN_STATE_BEFORE)));
        rule.setStateToApply(cursor.getInt(cursor.getColumnIndex(RulesDatabaseHelper.RULES_COLUMN_STATE_TO_APPLY)));

        return rule;
    }

    private ContentValues ruleToContentValues(Rule rule) {
        ContentValues values = new ContentValues();
        values.put(RulesDatabaseHelper.RULES_COLUMN_START, rule.getStartTime());
        values.put(RulesDatabaseHelper.RULES_COLUMN_END, rule.getEndTime());
        values.put(RulesDatabaseHelper.RULES_COLUMN_ACTIVE, rule.isActive());
        values.put(RulesDatabaseHelper.RULES_COLUMN_APPLIED, rule.isApplied());
        values.put(RulesDatabaseHelper.RULES_COLUMN_STATE_BEFORE, rule.getStateBefore());
        values.put(RulesDatabaseHelper.RULES_COLUMN_STATE_TO_APPLY, rule.getStateToApply());
        int repeat = 0;
        for(int i=0; i<7; i++) {
            repeat += DayValues[i] * (rule.isActiveOnDay(i) ? 1 : 0);
        }
        values.put(RulesDatabaseHelper.RULES_COLUMN_REPEAT, repeat);

        return values;
    }
}




Java Source Code List

com.victorkifer.SoundScheduler.AboutActivity.java
com.victorkifer.SoundScheduler.EditRuleActivity.java
com.victorkifer.SoundScheduler.MainActivity.java
com.victorkifer.SoundScheduler.adapters.RuleListAdapter.java
com.victorkifer.SoundScheduler.database.RulesDataSource.java
com.victorkifer.SoundScheduler.database.RulesDatabaseHelper.java
com.victorkifer.SoundScheduler.entities.Rule.java
com.victorkifer.SoundScheduler.listeners.RuleDeleteListener.java
com.victorkifer.SoundScheduler.listeners.RuleItemStateListener.java
com.victorkifer.SoundScheduler.managers.SoundManager.java
com.victorkifer.SoundScheduler.receivers.BootCompleteReceiver.java
com.victorkifer.SoundScheduler.receivers.TimeChangeReceiver.java