// Path: app/src/main/java/org/mars3142/android/toaster/table/ToasterTable.java
// public class ToasterTable
//         implements BaseColumns {
// 
//     private static final String TAG = ToasterTable.class.getSimpleName();
//     
//     public static final String TABLE_NAME = "toaster";
//     public static final String TIMESTAMP = "timestamp";
//     public static final String PACKAGE = "package";
//     public static final String MESSAGE = "message";
//     public static final String VERSION_CODE = "version_code";
//     public static final String VERSION_NAME = "version_name";
//     public static final Uri TOASTER_URI = Uri.parse("content://" + ToasterProvider.AUTHORITY + "/toaster");
//     public static final Uri PACKAGE_URI = Uri.parse("content://" + ToasterProvider.AUTHORITY + "/packages");
//     public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.mars3142.content.toaster";
//     public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.mars3142.content.toaster";
// 
//     public static void onCreate(SQLiteDatabase db) {
//         db.execSQL("CREATE TABLE " + TABLE_NAME +
//                 " (" +
//                 _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
//                 TIMESTAMP + " LONG, " +
//                 PACKAGE + " TEXT, " +
//                 MESSAGE + " TEXT, " +
//                 VERSION_CODE + " INTEGER, " +
//                 VERSION_NAME + " TEXT " +
//                 ");");
//     }
// 
//     public static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//         if (newVersion > oldVersion) {
//             switch (oldVersion) {
//                 case 1:
//                     try {
//                         db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + VERSION_CODE + " INTEGER;");
//                         db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + VERSION_NAME + " TEXT;");
//                     } catch (SQLException ex) {
//                         // upgrade already done
//                     }
//                     break;
// 
//                 default:
//                     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
//                     onCreate(db);
//             }
//         }
//     }
// 
//     public static void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//         // nothing
//     }
// }
// 
// Path: app/src/main/java/org/mars3142/android/toaster/task/AsyncInsert.java
// public class AsyncInsert extends AsyncTask<Void, Void, Uri>{
// 
//     private Context mContext;
//     private Uri mUri;
//     private ContentValues mValues;
// 
//     public AsyncInsert(Context context, Uri uri, ContentValues values) {
//         mContext = context;
//         mUri = uri;
//         mValues = values;
//     }
// 
//     @Override
//     protected Uri doInBackground(Void... params) {
//         Uri result = null;
//         try {
//             result = mContext.getContentResolver().insert(mUri, mValues);
//         } catch (Exception ex) {
//             Timber.e("Error while insert");
//         }
// 
//         return result;
//     }
// }


import java.util.Calendar;
import timber.log.Timber;
import android.accessibilityservice.AccessibilityService;
import android.app.Notification;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Parcelable;
import android.view.accessibility.AccessibilityEvent;
import org.mars3142.android.toaster.BuildConfig;
import org.mars3142.android.toaster.table.ToasterTable;
import org.mars3142.android.toaster.task.AsyncInsert;

/*
 * This file is part of Toaster
 *
 * Copyright (c) 2014, 2017 Peter Siegmund
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.mars3142.android.toaster.service;





/**
 * Accessibility service who fetches the notification
 * <p/>
 * <p>Sends the data to the database provider and informs every widget to update</p>
 *
 * @author mars3142
 */
public class ToasterService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Timber.d("Event: %s", event);

        if (event.getEventType() != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
            Timber.d("Unexpected event type - ignoring");
            return; // event is not a notification
        }

        long timestamp = Calendar.getInstance().getTimeInMillis();
        String sourcePackageName = (String) event.getPackageName();
        String message = "";
        for (CharSequence text : event.getText()) {
            message += text + "\n";
        }
        if (message.length() > 0) {
            message = message.substring(0, message.length() - 1);
        }

        Parcelable parcelable = event.getParcelableData();
        if (!(parcelable instanceof Notification)) {
            ContentValues cv = new ContentValues();

// Path: app/src/main/java/org/mars3142/android/toaster/table/ToasterTable.java
// public class ToasterTable
//         implements BaseColumns {
// 
//     private static final String TAG = ToasterTable.class.getSimpleName();
//     
//     public static final String TABLE_NAME = "toaster";
//     public static final String TIMESTAMP = "timestamp";
//     public static final String PACKAGE = "package";
//     public static final String MESSAGE = "message";
//     public static final String VERSION_CODE = "version_code";
//     public static final String VERSION_NAME = "version_name";
//     public static final Uri TOASTER_URI = Uri.parse("content://" + ToasterProvider.AUTHORITY + "/toaster");
//     public static final Uri PACKAGE_URI = Uri.parse("content://" + ToasterProvider.AUTHORITY + "/packages");
//     public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.mars3142.content.toaster";
//     public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.mars3142.content.toaster";
// 
//     public static void onCreate(SQLiteDatabase db) {
//         db.execSQL("CREATE TABLE " + TABLE_NAME +
//                 " (" +
//                 _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
//                 TIMESTAMP + " LONG, " +
//                 PACKAGE + " TEXT, " +
//                 MESSAGE + " TEXT, " +
//                 VERSION_CODE + " INTEGER, " +
//                 VERSION_NAME + " TEXT " +
//                 ");");
//     }
// 
//     public static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//         if (newVersion > oldVersion) {
//             switch (oldVersion) {
//                 case 1:
//                     try {
//                         db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + VERSION_CODE + " INTEGER;");
//                         db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + VERSION_NAME + " TEXT;");
//                     } catch (SQLException ex) {
//                         // upgrade already done
//                     }
//                     break;
// 
//                 default:
//                     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
//                     onCreate(db);
//             }
//         }
//     }
// 
//     public static void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//         // nothing
//     }
// }
// 
// Path: app/src/main/java/org/mars3142/android/toaster/task/AsyncInsert.java
// public class AsyncInsert extends AsyncTask<Void, Void, Uri>{
// 
//     private Context mContext;
//     private Uri mUri;
//     private ContentValues mValues;
// 
//     public AsyncInsert(Context context, Uri uri, ContentValues values) {
//         mContext = context;
//         mUri = uri;
//         mValues = values;
//     }
// 
//     @Override
//     protected Uri doInBackground(Void... params) {
//         Uri result = null;
//         try {
//             result = mContext.getContentResolver().insert(mUri, mValues);
//         } catch (Exception ex) {
//             Timber.e("Error while insert");
//         }
// 
//         return result;
//     }
// }


// Path: app/src/main/java/org/mars3142/android/toaster/service/ToasterService.java
import java.util.Calendar;
import timber.log.Timber;
import android.accessibilityservice.AccessibilityService;
import android.app.Notification;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Parcelable;
import android.view.accessibility.AccessibilityEvent;
import org.mars3142.android.toaster.BuildConfig;
import org.mars3142.android.toaster.table.ToasterTable;
import org.mars3142.android.toaster.task.AsyncInsert;

/*
 * This file is part of Toaster
 *
 * Copyright (c) 2014, 2017 Peter Siegmund
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.mars3142.android.toaster.service;





/**
 * Accessibility service who fetches the notification
 * <p/>
 * <p>Sends the data to the database provider and informs every widget to update</p>
 *
 * @author mars3142
 */
public class ToasterService extends AccessibilityService {

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        Timber.d("Event: %s", event);

        if (event.getEventType() != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
            Timber.d("Unexpected event type - ignoring");
            return; // event is not a notification
        }

        long timestamp = Calendar.getInstance().getTimeInMillis();
        String sourcePackageName = (String) event.getPackageName();
        String message = "";
        for (CharSequence text : event.getText()) {
            message += text + "\n";
        }
        if (message.length() > 0) {
            message = message.substring(0, message.length() - 1);
        }

        Parcelable parcelable = event.getParcelableData();
        if (!(parcelable instanceof Notification)) {
            ContentValues cv = new ContentValues();

            cv.put(ToasterTable.PACKAGE, sourcePackageName);
