// Path: app/src/main/java/com/ToxicBakery/app/screenshot_redaction/ScreenshotApplication.java
// public class ScreenshotApplication extends Application {
// 
//     private static final String[] PERMISSIONS = {
//             Manifest.permission.READ_EXTERNAL_STORAGE
//     };
// 
//     private static final String TAG = "ScreenshotApplication";
//     private static final String REMOVE_OLD_DIR = "REMOVE_OLD_DIR";
// 
//     private ScreenShotNotifications screenShotNotifications;
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
// 
//         // Install Timber
//         if (BuildConfig.DEBUG) {
//             Timber.plant(new Timber.DebugTree());
//         }
// 
//         Once.initialise(this);
//         screenShotNotifications = new ScreenShotNotifications(this);
// 
//         Observable.just(true)
//                 .subscribeOn(AndroidSchedulers.mainThread())
//                 .subscribe(new Action1<Boolean>() {
//                     @Override
//                     public void call(Boolean aBoolean) {
//                         initializeResources();
//                     }
//                 });
//     }
// 
//     /**
//      * Gets the screenshot notification instance from the process application.
//      *
//      * @return screenshot notification instance
//      */
//     public ScreenShotNotifications getScreenShotNotifications() {
//         return screenShotNotifications;
//     }
// 
//     void initializeResources() {
//         if (!Once.beenDone(REMOVE_OLD_DIR)) {
//             Once.markDone(REMOVE_OLD_DIR);
// 
//             File externalFilesDir = getApplicationContext().getExternalFilesDir(null);
//             if (externalFilesDir != null
//                     && externalFilesDir.exists()
//                     && !externalFilesDir.delete()) {
// 
//                 Log.e(TAG, "Failed to delete external storage directory");
//             }
//         }
// 
//         if (Is.lessThan(MARSHMALLOW) || PermissionCheck.hasPermissions(getApplicationContext(), PERMISSIONS)) {
//             new CopyToSdCard()
//                     .copy(new TessDataRawResourceCopyConfiguration(getApplicationContext(), R.raw.eng));
// 
//             DictionaryEnglish.getInstance(getApplicationContext());
//             DictionaryEnglishNames.getInstance(getApplicationContext());
//             ScreenshotService.startScreenshotService(getApplicationContext());
//         }
//     }
// 
// }


import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import android.support.annotation.NonNull;
import android.util.Log;
import com.ToxicBakery.app.screenshot_redaction.ScreenshotApplication;
import java.io.File;
import java.util.Date;
import static android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        public ObserverImpl(@NonNull Context context,
                            @NonNull IOnScreenShotListener onScreenShotListener) {

            super(null);

            this.context = context;
            this.onScreenShotListener = onScreenShotListener;
            contentResolver = context.getContentResolver();
            lastDate = System.currentTimeMillis();
        }

        @Override
        public void onChange(boolean selfChange) {
            Cursor cursor = contentResolver.query(EXTERNAL_CONTENT_URI, PROJECTION, SELECTION, null, SORT_ORDER);
            try {
                if (cursor != null) {
                    int idxData = cursor.getColumnIndex(Media.DATA);
                    int idxDate = cursor.getColumnIndex(Media.DATE_ADDED);

                    if (cursor.moveToFirst()) {
                        String path = cursor.getString(idxData);
                        File file = new File(path);
                        Uri uri = Uri.fromFile(file);
                        long date = cursor.getInt(idxDate) * 1000L;

                        if (date < lastDate) {
                            // Date is before the most recent
                            Log.i(TAG, "Screenshot date " + new Date(date) + " is before " + new Date(lastDate));

                            // Likely a deletion
// Path: app/src/main/java/com/ToxicBakery/app/screenshot_redaction/ScreenshotApplication.java
// public class ScreenshotApplication extends Application {
// 
//     private static final String[] PERMISSIONS = {
//             Manifest.permission.READ_EXTERNAL_STORAGE
//     };
// 
//     private static final String TAG = "ScreenshotApplication";
//     private static final String REMOVE_OLD_DIR = "REMOVE_OLD_DIR";
// 
//     private ScreenShotNotifications screenShotNotifications;
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
// 
//         // Install Timber
//         if (BuildConfig.DEBUG) {
//             Timber.plant(new Timber.DebugTree());
//         }
// 
//         Once.initialise(this);
//         screenShotNotifications = new ScreenShotNotifications(this);
// 
//         Observable.just(true)
//                 .subscribeOn(AndroidSchedulers.mainThread())
//                 .subscribe(new Action1<Boolean>() {
//                     @Override
//                     public void call(Boolean aBoolean) {
//                         initializeResources();
//                     }
//                 });
//     }
// 
//     /**
//      * Gets the screenshot notification instance from the process application.
//      *
//      * @return screenshot notification instance
//      */
//     public ScreenShotNotifications getScreenShotNotifications() {
//         return screenShotNotifications;
//     }
// 
//     void initializeResources() {
//         if (!Once.beenDone(REMOVE_OLD_DIR)) {
//             Once.markDone(REMOVE_OLD_DIR);
// 
//             File externalFilesDir = getApplicationContext().getExternalFilesDir(null);
//             if (externalFilesDir != null
//                     && externalFilesDir.exists()
//                     && !externalFilesDir.delete()) {
// 
//                 Log.e(TAG, "Failed to delete external storage directory");
//             }
//         }
// 
//         if (Is.lessThan(MARSHMALLOW) || PermissionCheck.hasPermissions(getApplicationContext(), PERMISSIONS)) {
//             new CopyToSdCard()
//                     .copy(new TessDataRawResourceCopyConfiguration(getApplicationContext(), R.raw.eng));
// 
//             DictionaryEnglish.getInstance(getApplicationContext());
//             DictionaryEnglishNames.getInstance(getApplicationContext());
//             ScreenshotService.startScreenshotService(getApplicationContext());
//         }
//     }
// 
// }


// Path: app/src/main/java/com/ToxicBakery/app/screenshot_redaction/observer/ScreenshotObserver.java
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import android.support.annotation.NonNull;
import android.util.Log;
import com.ToxicBakery.app.screenshot_redaction.ScreenshotApplication;
import java.io.File;
import java.util.Date;
import static android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        public ObserverImpl(@NonNull Context context,
                            @NonNull IOnScreenShotListener onScreenShotListener) {

            super(null);

            this.context = context;
            this.onScreenShotListener = onScreenShotListener;
            contentResolver = context.getContentResolver();
            lastDate = System.currentTimeMillis();
        }

        @Override
        public void onChange(boolean selfChange) {
            Cursor cursor = contentResolver.query(EXTERNAL_CONTENT_URI, PROJECTION, SELECTION, null, SORT_ORDER);
            try {
                if (cursor != null) {
                    int idxData = cursor.getColumnIndex(Media.DATA);
                    int idxDate = cursor.getColumnIndex(Media.DATE_ADDED);

                    if (cursor.moveToFirst()) {
                        String path = cursor.getString(idxData);
                        File file = new File(path);
                        Uri uri = Uri.fromFile(file);
                        long date = cursor.getInt(idxDate) * 1000L;

                        if (date < lastDate) {
                            // Date is before the most recent
                            Log.i(TAG, "Screenshot date " + new Date(date) + " is before " + new Date(lastDate));

                            // Likely a deletion
                            ((ScreenshotApplication) context.getApplicationContext())