Hello world

Signed-off-by: Henrique Silva <jhenrique09.mcz@hotmail.com>
diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..1997072
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,8 @@
+LOCAL_PATH := $(call my-dir)

+

+include $(CLEAR_VARS)

+LOCAL_SRC_FILES := $(call all-subdir-java-files)

+LOCAL_PACKAGE_NAME := AmbientPlayHistoryProvider

+LOCAL_MODULE_TAGS := optional

+LOCAL_SDK_VERSION := system_current

+include $(BUILD_PACKAGE)
\ No newline at end of file
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
new file mode 100644
index 0000000..c3511ce
--- /dev/null
+++ b/AndroidManifest.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>

+<manifest xmlns:android="http://schemas.android.com/apk/res/android"

+    package="org.pixelexperience.ambient.play.history"

+    android:versionCode="1"

+    android:versionName="1.0">

+

+    <permission

+        android:name="org.pixelexperience.ambient.play.history.MANAGE_HISTORY"

+        android:protectionLevel="signature|privileged" />

+

+    <uses-sdk

+        android:minSdkVersion="28"

+        android:targetSdkVersion="28" />

+

+    <application

+        android:allowBackup="false"

+        android:icon="@drawable/ic_launcher"

+        android:label="@string/app_name">

+        <provider

+            android:name=".AmbientPlayHistoryProvider"

+            android:authorities="org.pixelexperience.ambient.play.history.provider"

+            android:exported="true"

+            android:readPermission="org.pixelexperience.ambient.play.history.MANAGE_HISTORY" />

+    </application>

+

+</manifest>
\ No newline at end of file
diff --git a/res/drawable-hdpi/ic_launcher.png b/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 0000000..9e6ed31
--- /dev/null
+++ b/res/drawable-hdpi/ic_launcher.png
Binary files differ
diff --git a/res/drawable-mdpi/ic_launcher.png b/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..c589166
--- /dev/null
+++ b/res/drawable-mdpi/ic_launcher.png
Binary files differ
diff --git a/res/drawable-xhdpi/ic_launcher.png b/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..bd20a94
--- /dev/null
+++ b/res/drawable-xhdpi/ic_launcher.png
Binary files differ
diff --git a/res/drawable-xxhdpi/ic_launcher.png b/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..fef5de9
--- /dev/null
+++ b/res/drawable-xxhdpi/ic_launcher.png
Binary files differ
diff --git a/res/drawable-xxxhdpi/ic_launcher.png b/res/drawable-xxxhdpi/ic_launcher.png
new file mode 100644
index 0000000..576a921
--- /dev/null
+++ b/res/drawable-xxxhdpi/ic_launcher.png
Binary files differ
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
new file mode 100644
index 0000000..4ca6d30
--- /dev/null
+++ b/res/values/custom_strings.xml
@@ -0,0 +1,3 @@
+<resources>

+    <string name="app_name">Now Playing Provider</string>

+</resources>

diff --git a/src/org/pixelexperience/ambient/play/history/AmbientPlayHistoryDb.java b/src/org/pixelexperience/ambient/play/history/AmbientPlayHistoryDb.java
new file mode 100644
index 0000000..b3e60cd
--- /dev/null
+++ b/src/org/pixelexperience/ambient/play/history/AmbientPlayHistoryDb.java
@@ -0,0 +1,42 @@
+package org.pixelexperience.ambient.play.history;

+import android.content.Context;

+import android.database.sqlite.SQLiteDatabase;

+import android.database.sqlite.SQLiteOpenHelper;

+

+public class AmbientPlayHistoryDb extends SQLiteOpenHelper {

+

+    private static final String DATABASE_NAME = "AmbientPlayHistory";

+    private static final int DATABASE_VERSION = 1;

+

+    static final String KEY_ID = "_id";

+    static final String KEY_TIMESTAMP = "ts";

+    static final String KEY_ARTIST = "artist";

+    static final String KEY_SONG = "song";

+

+    static final String SQLITE_TABLE = "matched_songs";

+

+    private static final String DATABASE_CREATE =

+            "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +

+                    KEY_ID + " integer PRIMARY KEY autoincrement," +

+                    KEY_TIMESTAMP + "," +

+                    KEY_SONG + "," +

+                    KEY_ARTIST + ");";

+

+

+    AmbientPlayHistoryDb(Context context) {

+        super(context, DATABASE_NAME, null, DATABASE_VERSION);

+    }

+

+    @Override

+    public void onCreate(SQLiteDatabase db) {

+        db.execSQL(DATABASE_CREATE);

+    }

+

+    @Override

+    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

+        db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);

+        onCreate(db);

+    }

+

+

+}
\ No newline at end of file
diff --git a/src/org/pixelexperience/ambient/play/history/AmbientPlayHistoryProvider.java b/src/org/pixelexperience/ambient/play/history/AmbientPlayHistoryProvider.java
new file mode 100644
index 0000000..ae963eb
--- /dev/null
+++ b/src/org/pixelexperience/ambient/play/history/AmbientPlayHistoryProvider.java
@@ -0,0 +1,116 @@
+package org.pixelexperience.ambient.play.history;

+

+import android.content.ContentProvider;

+import android.content.ContentValues;

+import android.content.UriMatcher;

+import android.database.Cursor;

+import android.database.sqlite.SQLiteDatabase;

+import android.database.sqlite.SQLiteQueryBuilder;

+import android.net.Uri;

+import android.text.TextUtils;

+

+public class AmbientPlayHistoryProvider extends ContentProvider {

+    private static final int SONGS = 1;

+    private static final int SONG = 2;

+    private static String MANAGE_HISTORY_PERMISSION = "org.pixelexperience.ambient.play.history.MANAGE_HISTORY";

+    private static final String AUTHORITY = "org.pixelexperience.ambient.play.history.provider";

+    private static final Uri CONTENT_URI =

+            Uri.parse("content://" + AUTHORITY + "/songs");

+    private static final String[] PROJECTION = {AmbientPlayHistoryDb.KEY_ID, AmbientPlayHistoryDb.KEY_TIMESTAMP, AmbientPlayHistoryDb.KEY_SONG, AmbientPlayHistoryDb.KEY_ARTIST};

+    private static final UriMatcher uriMatcher;

+

+    static {

+        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

+        uriMatcher.addURI(AUTHORITY, "songs", SONGS);

+        uriMatcher.addURI(AUTHORITY, "songs/#", SONG);

+    }

+

+    private AmbientPlayHistoryDb dbHelper;

+

+    @Override

+    public boolean onCreate() {

+        dbHelper = new AmbientPlayHistoryDb(getContext());

+        return false;

+    }

+

+    @Override

+    public String getType(Uri uri) {

+        return null;

+    }

+

+    @Override

+    public Uri insert(Uri uri, ContentValues values) {

+        SQLiteDatabase db = dbHelper.getWritableDatabase();

+        switch (uriMatcher.match(uri)) {

+            case SONGS:

+                //do nothing

+                break;

+            default:

+                throw new IllegalArgumentException("Unsupported URI: " + uri);

+        }

+        long isLastSongAlreadyMatched = isLastSongAlreadyMatched(values);

+        long id = isLastSongAlreadyMatched != -1 ? isLastSongAlreadyMatched : db.insert(AmbientPlayHistoryDb.SQLITE_TABLE, null, values);

+        return Uri.parse(CONTENT_URI + "/" + id);

+    }

+

+    private int isLastSongAlreadyMatched(ContentValues values) {

+        try (Cursor cursor = query(AmbientPlayHistoryProvider.CONTENT_URI, AmbientPlayHistoryProvider.PROJECTION, null, null, null)) {

+            if (cursor != null) {

+                if (cursor.moveToFirst()) {

+                    return values.get(AmbientPlayHistoryDb.KEY_SONG).equals(cursor.getString(2)) && values.get(AmbientPlayHistoryDb.KEY_ARTIST).equals(cursor.getString(3)) ? cursor.getInt(0) : -1;

+                }

+            }

+        }

+        return -1;

+    }

+

+    @Override

+    public Cursor query(Uri uri, String[] projection, String selection,

+                        String[] selectionArgs, String sortOrder) {

+

+        SQLiteDatabase db = dbHelper.getWritableDatabase();

+        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

+        queryBuilder.setTables(AmbientPlayHistoryDb.SQLITE_TABLE);

+

+        switch (uriMatcher.match(uri)) {

+            case SONGS:

+                break;

+            case SONG:

+                String id = uri.getPathSegments().get(1);

+                queryBuilder.appendWhere(AmbientPlayHistoryDb.KEY_ID + "=" + id);

+                break;

+            default:

+                throw new IllegalArgumentException("Unsupported URI: " + uri);

+        }

+

+        return queryBuilder.query(db, projection, selection,

+                selectionArgs, null, null, AmbientPlayHistoryDb.KEY_ID + " DESC");

+

+    }

+

+    @Override

+    public int delete(Uri uri, String selection, String[] selectionArgs) {

+

+        SQLiteDatabase db = dbHelper.getWritableDatabase();

+        switch (uriMatcher.match(uri)) {

+            case SONGS:

+                break;

+            case SONG:

+                String id = uri.getPathSegments().get(1);

+                selection = AmbientPlayHistoryDb.KEY_ID + "=" + id

+                        + (!TextUtils.isEmpty(selection) ?

+                        " AND (" + selection + ')' : "");

+                break;

+            default:

+                throw new IllegalArgumentException("Unsupported URI: " + uri);

+        }

+        return db.delete(AmbientPlayHistoryDb.SQLITE_TABLE, selection, selectionArgs);

+    }

+

+    @Override

+    public int update(Uri uri, ContentValues values, String selection,

+                      String[] selectionArgs) {

+        return 0;

+    }

+

+}