// Path: wear/src/main/java/ooo/oxo/apps/earth/dao/Earth.java
// public class Earth {
// 
//     public String file;
// 
//     public Date fetchedAt = new Date();
// 
//     public Earth() {
//     }
// 
//     public Earth(String file) {
//         this.file = file;
//     }
// 
//     @Nullable
//     public static Earth fromCursor(@NonNull Cursor cursor) {
//         if (!cursor.moveToNext()) {
//             return null;
//         }
// 
//         Earth earth = new Earth();
//         earth.file = cursor.getString(cursor.getColumnIndexOrThrow(EarthsContract.Columns.FILE));
//         earth.fetchedAt = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(EarthsContract.Columns.FETCHED_AT)));
// 
//         if (TextUtils.isEmpty(earth.file)) {
//             return null;
//         }
// 
//         return earth;
//     }
// 
//     public ContentValues toContentValues() {
//         ContentValues values = new ContentValues();
//         values.put(EarthsContract.Columns.FILE, file);
//         values.put(EarthsContract.Columns.FETCHED_AT, fetchedAt.getTime());
//         return values;
//     }
// 
// }


import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.concurrent.TimeUnit;
import ooo.oxo.apps.earth.dao.Earth;

        try {
            int deleted = trim(db);
            Log.d(TAG, "cleaned " + deleted + " outdated earths");
        } catch (Exception e) {
            Log.e(TAG, "failed cleaning outdated earths", e);
        }

        //noinspection ConstantConditions
        getContext().getContentResolver().notifyChange(uri, null, false);

        return ContentUris.withAppendedId(uri, row);
    }

    private int trim(SQLiteDatabase db) {
        long window = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2);

        String selection = EarthsContract.Columns.FETCHED_AT + " < ?";
        String[] selectionArgs = new String[]{String.valueOf(window)};

        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(EarthsContract.TABLE);

        Cursor cursor = builder.query(db, null, selection, selectionArgs, null, null, null, null);

        if (cursor == null) {
            return 0;
        }

        while (cursor.moveToNext()) {
// Path: wear/src/main/java/ooo/oxo/apps/earth/dao/Earth.java
// public class Earth {
// 
//     public String file;
// 
//     public Date fetchedAt = new Date();
// 
//     public Earth() {
//     }
// 
//     public Earth(String file) {
//         this.file = file;
//     }
// 
//     @Nullable
//     public static Earth fromCursor(@NonNull Cursor cursor) {
//         if (!cursor.moveToNext()) {
//             return null;
//         }
// 
//         Earth earth = new Earth();
//         earth.file = cursor.getString(cursor.getColumnIndexOrThrow(EarthsContract.Columns.FILE));
//         earth.fetchedAt = new Date(cursor.getLong(cursor.getColumnIndexOrThrow(EarthsContract.Columns.FETCHED_AT)));
// 
//         if (TextUtils.isEmpty(earth.file)) {
//             return null;
//         }
// 
//         return earth;
//     }
// 
//     public ContentValues toContentValues() {
//         ContentValues values = new ContentValues();
//         values.put(EarthsContract.Columns.FILE, file);
//         values.put(EarthsContract.Columns.FETCHED_AT, fetchedAt.getTime());
//         return values;
//     }
// 
// }


// Path: wear/src/main/java/ooo/oxo/apps/earth/provider/EarthsProvider.java
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.concurrent.TimeUnit;
import ooo.oxo.apps.earth.dao.Earth;


        try {
            int deleted = trim(db);
            Log.d(TAG, "cleaned " + deleted + " outdated earths");
        } catch (Exception e) {
            Log.e(TAG, "failed cleaning outdated earths", e);
        }

        //noinspection ConstantConditions
        getContext().getContentResolver().notifyChange(uri, null, false);

        return ContentUris.withAppendedId(uri, row);
    }

    private int trim(SQLiteDatabase db) {
        long window = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2);

        String selection = EarthsContract.Columns.FETCHED_AT + " < ?";
        String[] selectionArgs = new String[]{String.valueOf(window)};

        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(EarthsContract.TABLE);

        Cursor cursor = builder.query(db, null, selection, selectionArgs, null, null, null, null);

        if (cursor == null) {
            return 0;
        }

        while (cursor.moveToNext()) {
            Earth earth = Earth.fromCursor(cursor);