Android Open Source - Thrift-box D B






From Project

Back to project page Thrift-box.

License

The source code is released under:

GNU General Public License

If you think the Android project Thrift-box 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 ru.sash0k.thriftbox.database;
//from  www  . ja v a  2  s. c o m
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.provider.BaseColumns;

import java.nio.channels.FileLock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ru.sash0k.thriftbox.Utils;

/**
 * Created with IntelliJ IDEA.
 * User: sash0k
 */
public class DB {

    /**
     * Created with IntelliJ IDEA.
     * User: sash0k
     */
    public static class DbOpenHelper extends SQLiteOpenHelper {
        private static final int DB_VERSION = 2;
        private static final String DB_NAME = "thriftbox.db";

        public DbOpenHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            final String expenses_table = "CREATE TABLE IF NOT EXISTS [" + EXPENSES_TABLE + "]" +
                    " ([" + BaseColumns._ID + "] INTEGER PRIMARY KEY AUTOINCREMENT," +
                    " [" + VALUE + "] INTEGER NOT NULL," +
                    " [" + CATEGORY + "] INTEGER NOT NULL DEFAULT(0)," +
                    " [" + COMMENT + "] TEXT," +
                    " [" + TIMESTAMP + "] INTEGER NOT NULL DEFAULT(strftime('%s','now')));";

            final String expenses_view = "CREATE VIEW IF NOT EXISTS [" + EXPENSES_VIEW + "] AS SELECT " +
                    BaseColumns._ID + ", " + VALUE + ", " + CATEGORY + ", "
                    + COMMENT + ", " + TIMESTAMP + ", " +
                    "strftime('%d.%m.%Y', " + TIMESTAMP + ", 'unixepoch', 'localtime') AS " + DATE +
                    " FROM " + EXPENSES_TABLE;

            final String insert_trigger = "CREATE TRIGGER IF NOT EXISTS " +
                    "insert_expenses instead of insert on " + EXPENSES_VIEW +
                    " BEGIN INSERT into " + EXPENSES_TABLE + "(" + VALUE + ", " + CATEGORY + ", " + COMMENT + ")" +
                    " values(new." + VALUE + ", new." + CATEGORY + ", new." + COMMENT + "); END;";

            final String delete_trigger = "CREATE TRIGGER IF NOT EXISTS " +
                    "delete_expenses instead of delete on " + EXPENSES_VIEW +
                    " BEGIN DELETE from " + EXPENSES_TABLE + " WHERE " + BaseColumns._ID + " = OLD." + BaseColumns._ID + "; END;";

            sqLiteDatabase.execSQL(expenses_table);
            sqLiteDatabase.execSQL(expenses_view);
            sqLiteDatabase.execSQL(insert_trigger);
            sqLiteDatabase.execSQL(delete_trigger);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
            Utils.log("db onUpgrade: " + oldVersion + " >> " + newVersion);
            sqLiteDatabase.execSQL("DROP TRIGGER IF EXISTS insert_expenses");
            sqLiteDatabase.execSQL("DROP TRIGGER IF EXISTS delete_expenses");
            sqLiteDatabase.execSQL("DROP VIEW IF EXISTS " + EXPENSES_VIEW);
            if (oldVersion == 1 && newVersion == 2) {
                // ??????? ????????? "????", ?????? ?????? ????????? "?????????????" ?? 9 ?? 11
                sqLiteDatabase.execSQL("UPDATE " + EXPENSES_TABLE + " SET " + CATEGORY + " = 11 WHERE " + CATEGORY + " = 9" );
                sqLiteDatabase.execSQL("ALTER TABLE " + EXPENSES_TABLE + " ADD COLUMN " + COMMENT + " TEXT");
            }
            else sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + EXPENSES_TABLE);
            onCreate(sqLiteDatabase);
        }
    }
    // ====================================================================

    // ??????? ???????
    public static final String EXPENSES_TABLE = "expenses";
    public static final String EXPENSES_VIEW = "expenses_view";

    // ????? ??????? EXPENSES_TABLE
    // BaseColumns._ID
    public static final String VALUE = "value";
    public static final String CATEGORY = "category";
    public static final String COMMENT = "comment";
    public static final String TIMESTAMP = "timestamp";

    // ????? ??????? EXPENSES_VIEW
    public static final String DATE = "date";
    // ====================================================================

    /**
     * ???????? URI ???? ?????? ?? ???????????
     *
     * @param table - ??????? ???????
     * @return
     */
    public static Uri getUri(String table) {
        return Uri.parse("content://" + DBProvider.AUTHORITY + "/" + table);
    }
    // ====================================================================

    /**
     * ??????? ???????
     */
    public static void clean(Context context, String table) {
        context.getContentResolver().delete(getUri(table), null, null);
    }
    // ====================================================================

    /**
     * ???????? ??????? ? ???????
     */
    public static void insertItem(Context context, int value, int category, String comment) {
        ContentValues values = new ContentValues();
        values.put(VALUE, value);
        values.put(CATEGORY, category);
        if (comment != null) values.put(COMMENT, comment.trim().isEmpty() ? null : comment);
        context.getContentResolver().insert(getUri(EXPENSES_VIEW), values);
    }
    // ====================================================================

    /**
     * ??????? ??????? ?? ???????
     */
    public static void deleteItem(Context context, int id) {
        context.getContentResolver().delete(getUri(EXPENSES_VIEW), BaseColumns._ID + "=?", new String[]{Integer.toString(id)});
    }
    // ====================================================================

    /**
     * ???????? ??????? ?? ????????????? ????
     */
    public static long getExpense(Context context, long timestamp) {
        final String[] columns = new String[]{"SUM(" + VALUE + ")"};
        final String where = TIMESTAMP + ">=?";
        Cursor mCursor = context.getContentResolver().query(getUri(EXPENSES_TABLE),
                columns, where, new String[]{Long.toString(timestamp)}, null);
        if (mCursor != null) {
            if (mCursor.moveToFirst()) {
                final long result = mCursor.getLong(0);
                mCursor.close();
                return result;
            } else mCursor.close();
        }
        return 0;
    }
    // ====================================================================


    /**
     * ???????? ???????? ?? ??????????? ?? ???????
     */
    public static Map<Integer, Float> getMonth(Context context, long timestamp) {
        final String[] columns = new String[]{CATEGORY, "SUM(" + VALUE + ") AS " + VALUE};
        final String where = TIMESTAMP + ">=? GROUP BY (" + CATEGORY + ")";
        Cursor mCursor = context.getContentResolver().query(getUri(EXPENSES_TABLE),
                columns, where, new String[]{Long.toString(timestamp)}, CATEGORY);

        if (mCursor == null) return null;
        Map<Integer, Float> result = new HashMap<>(mCursor.getCount());
        while (mCursor.moveToNext()) {
            result.put(mCursor.getInt(0), mCursor.getFloat(1) / 100.0f);
        }
        mCursor.close();
        return result;
    }
    // ====================================================================

}




Java Source Code List

android.support.v4.preference.PreferenceFragment.java
android.support.v4.preference.PreferenceManagerCompat.java
android.widget_fixed.CursorFilter.java
android.widget_fixed.CursorTreeAdapter.java
com.github.mikephil.charting.charts.Chart.java
com.github.mikephil.charting.charts.PieChart.java
com.github.mikephil.charting.charts.PieRadarChartBase.java
com.github.mikephil.charting.data.ChartData.java
com.github.mikephil.charting.data.DataSet.java
com.github.mikephil.charting.data.Entry.java
com.github.mikephil.charting.data.PieDataSet.java
com.github.mikephil.charting.data.PieData.java
com.github.mikephil.charting.data.filter.Approximator.java
com.github.mikephil.charting.exception.DrawingDataSetNotCreatedException.java
com.github.mikephil.charting.interfaces.ChartInterface.java
com.github.mikephil.charting.interfaces.OnChartGestureListener.java
com.github.mikephil.charting.interfaces.OnChartValueSelectedListener.java
com.github.mikephil.charting.interfaces.OnDrawLineChartTouchListener.java
com.github.mikephil.charting.interfaces.OnDrawListener.java
com.github.mikephil.charting.listener.PieRadarChartTouchListener.java
com.github.mikephil.charting.matrix.Vector3.java
com.github.mikephil.charting.renderer.Transformer.java
com.github.mikephil.charting.utils.ColorFormatter.java
com.github.mikephil.charting.utils.ColorTemplate.java
com.github.mikephil.charting.utils.Highlight.java
com.github.mikephil.charting.utils.LabelBase.java
com.github.mikephil.charting.utils.LargeValueFormatter.java
com.github.mikephil.charting.utils.Legend.java
com.github.mikephil.charting.utils.LimitLine.java
com.github.mikephil.charting.utils.MarkerView.java
com.github.mikephil.charting.utils.PointD.java
com.github.mikephil.charting.utils.SelInfo.java
com.github.mikephil.charting.utils.Utils.java
com.github.mikephil.charting.utils.ValueFormatter.java
com.github.mikephil.charting.utils.XLabels.java
com.github.mikephil.charting.utils.YLabels.java
net.margaritov.preference.colorpicker.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.ColorPickerDialog.java
net.margaritov.preference.colorpicker.ColorPickerPanelView.java
net.margaritov.preference.colorpicker.ColorPickerPreference.java
net.margaritov.preference.colorpicker.ColorPickerView.java
ru.sash0k.thriftbox.AdapterExpenses.java
ru.sash0k.thriftbox.MainActivity.java
ru.sash0k.thriftbox.TypefaceSpan2.java
ru.sash0k.thriftbox.Utils.java
ru.sash0k.thriftbox.Widget.java
ru.sash0k.thriftbox.categories.Categories.java
ru.sash0k.thriftbox.categories.InterceptingHorizontalScrollView.java
ru.sash0k.thriftbox.database.DBProvider.java
ru.sash0k.thriftbox.database.DB.java
ru.sash0k.thriftbox.fragments.CommentDialog.java
ru.sash0k.thriftbox.fragments.DeleteConfirmDialog.java
ru.sash0k.thriftbox.fragments.ExpandableListFragment.java
ru.sash0k.thriftbox.fragments.ExpensesFragment.java
ru.sash0k.thriftbox.fragments.InputFragment.java
ru.sash0k.thriftbox.fragments.PieChartFragment.java
ru.sash0k.thriftbox.fragments.SettingsFragment.java