package com.xlythe.textmanager.text.concurrency;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;

/**
 * Represents a variable that needs to be loaded.
 */
public abstract class FutureImpl<T> implements Future<T> {
    private static final HandlerThread sBackgroundThread = new HandlerThread("FutureBackgroundThread");

    static {
        sBackgroundThread.start();
    }

    // This handler posts to the thread it's created on.
    private final Handler mForegroundHandler = new Handler();
    private final Handler mBackgroundHandler = new Handler(sBackgroundThread.getLooper());

    @Override
    public void get(final Callback<T> callback) {
        mBackgroundHandler.post(new Runnable() {
            @Override
            public void run() {
                // We got the result, but we're still on the background thread.
                final T result = get();

                // Post to get back to the calling thread.
                mForegroundHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.get(result);
                    }
                });
            }
        });
    }

    @Override
    public String toString() {
        return get().toString();
    }
}

@@ -512,26 +512,10 @@
 dler
- = new Handler()
 ;%0A
+
     
@@ -598,24 +598,212 @@
 Looper());%0A%0A
+    public FutureImpl() %7B%0A        if (Looper.myLooper() != null) %7B%0A            mForegroundHandler = new Handler();%0A        %7D else %7B%0A            mForegroundHandler = null;%0A        %7D%0A    %7D%0A%0A
     @Overrid
@@ -1101,9 +1101,28 @@
  // 
-P
+If we know how to, p
 ost 
@@ -1148,32 +1148,86 @@
 calling thread.%0A
+                if (mForegroundHandler != null) %7B%0A    
                 
@@ -1259,32 +1259,36 @@
 ew Runnable() %7B%0A
+    
                 
@@ -1313,32 +1313,36 @@
                 
+    
 public void run(
@@ -1337,32 +1337,36 @@
 ic void run() %7B%0A
+    
                 
@@ -1407,34 +1407,41 @@
                 
-%7D%0A
+    %7D%0A   
                 
@@ -1436,28 +1436,183 @@
             
+ 
 %7D);%0A
+                %7D else %7B%0A                    // Otherwise, return from our background thread.%0A                    callback.get(result);%0A                %7D%0A
             

add future support from bg threads