Cache Images before showing with Glide on Android

    private fun cacheFiles(files: List<MediaFile>?) {
        if (files != null) {

            val measuredWidth = Statics.getScreenWidth() / 4
            //val requestOption = RequestOptions.overrideOf(width, width)

            for (f in files) {
                if (f.mimeType.startsWith("image")) {
                    GlideApp.with(CachePupli.context)
                            .load(File(f.path))
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .override(measuredWidth, measuredWidth)
                            .centerCrop()
                            .submit(measuredWidth, measuredWidth)
                }
            }
        }
    }

References
https://github.com/bumptech/glide/wiki/Loading-and-Caching-on-Background-Threads

Monitor File and Directory changes with Apache Commons IO

FileAlterationObserver observer = new FileAlterationObserver("pathToDir");
FileAlterationMonitor monitor = new FileAlterationMonitor(POLL_INTERVAL);
FileAlterationListener listener = new FileAlterationListenerAdaptor() {
    @Override
    public void onFileCreate(File file) {
        // code for processing creation event
    }
 
    @Override
    public void onFileDelete(File file) {
        // code for processing deletion event
    }
 
    @Override
    public void onFileChange(File file) {
        // code for processing change event
    }
};
observer.addListener(listener);
monitor.addObserver(observer);
monitor.start();

References
http://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library

Start Sticky Service on Boot in Android

BroadcastReceiverOnBootComplete.java

public class BroadcastReceiverOnBootComplete extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
            Intent serviceIntent = new Intent(context, AndroidServiceStartOnBoot.class);
            context.startService(serviceIntent);
        }
    }
}

AndroidServiceStartOnBoot.java

public class AndroidServiceStartOnBoot extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
       // here you can add whatever you want this service to do
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javacodegeeks.androidserviceonbootexample">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

    <receiver
        android:name="com.javacodegeeks.androidserviceonbootexample.BroadcastReceiverOnBootComplete"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_REPLACED" />
            <data android:scheme="package" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <data android:scheme="package" />
        </intent-filter>
    </receiver>

    <service android:name="com.javacodegeeks.androidserviceonbootexample.AndroidServiceStartOnBoot"></service>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name="com.javacodegeeks.androidserviceonbootexample.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest
>

if we want to be sure our service is sticky

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        if (workerThread == null) {
            workerThread = Thread(run)
            workerThread!!.start()

            Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        }

        return Service.START_STICKY
    }

and

    override fun onTaskRemoved(rootIntent: Intent?) {
        val restartService = Intent(applicationContext,
                this.javaClass)
        restartService.`package` = packageName
        val restartServicePI = PendingIntent.getService(
                applicationContext, 1, restartService,
                PendingIntent.FLAG_ONE_SHOT)
        val alarmService = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000, restartServicePI)
    }

References
http://blog.vogella.com/2011/12/11/automatically-starting-services-in-android-after-booting/
https://examples.javacodegeeks.com/android/core/activity/android-start-service-boot-example/
http://www.framentos.com/en/android-tutorial/2012/08/01/how-to-start-a-service-at-boot-in-android/

Working with FileObserver on Android

private class PathFileObserver extends FileObserver{
        static final String TAG="FILEOBSERVER";
        /**
         * should be end with File.separator
         */
    	String rootPath;
    	static final int mask = (FileObserver.CREATE | 
    			FileObserver.DELETE | 
    			FileObserver.DELETE_SELF |
    			FileObserver.MODIFY |
    			FileObserver.MOVED_FROM |
    			FileObserver.MOVED_TO |
    			FileObserver.MOVE_SELF); 
    	
    	public PathFileObserver(String root){
    		super(root, mask);

    		if (! root.endsWith(File.separator)){
    			root += File.separator;
    		}
    		rootPath = root;
    	}

    	public void onEvent(int event, String path) {
    		
    		switch(event){
    		case FileObserver.CREATE:
    			Log.d(TAG, "CREATE:" + rootPath + path);
    			break;
    		case FileObserver.DELETE:
    			Log.d(TAG, "DELETE:" + rootPath + path);
    			break;
    		case FileObserver.DELETE_SELF:
    			Log.d(TAG, "DELETE_SELF:" + rootPath + path);
    			break;
    		case FileObserver.MODIFY:
    			Log.d(TAG, "MODIFY:" + rootPath + path);
    			break;
    		case FileObserver.MOVED_FROM:
    			Log.d(TAG, "MOVED_FROM:" + rootPath + path);
    			break;
    		case FileObserver.MOVED_TO:
    			Log.d(TAG, "MOVED_TO:" + path);
    			break;
    		case FileObserver.MOVE_SELF:
    			Log.d(TAG, "MOVE_SELF:" + path);
    			break;
    		default:
    			// just ignore
    			break;
    		}
    	}

    	public void close(){
    		super.finalize();
    	}
    }

References
https://gist.github.com/shirou/659180