package cgeo.geocaching.utils;

import cgeo.geocaching.activity.AbstractActivity;
import cgeo.geocaching.activity.Progress;

import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;

import java.lang.ref.WeakReference;

public abstract class SimpleHandler extends Handler {
    public static final String MESSAGE_TEXT = "message_text";
    protected final WeakReference<AbstractActivity> activityRef;
    protected final WeakReference<Progress> progressDialogRef;

    public SimpleHandler(final AbstractActivity activity, final Progress progress) {
        this.activityRef = new WeakReference<AbstractActivity>(activity);
        this.progressDialogRef = new WeakReference<Progress>(progress);
    }

    @Override
    public void handleMessage(final Message msg) {
        AbstractActivity activity = activityRef.get();
        if (activity != null && msg.getData() != null && msg.getData().getString(MESSAGE_TEXT) != null) {
            activity.showToast(msg.getData().getString(MESSAGE_TEXT));
        }
        dismissProgress();
    }

    protected final void showToast(final int resId) {
        AbstractActivity activity = activityRef.get();
        if (activity != null) {
            Resources res = activity.getResources();
            activity.showToast(res.getText(resId).toString());
        }
    }

    protected final void dismissProgress() {
        Progress progressDialog = progressDialogRef.get();
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    protected final void setProgressMessage(final String txt) {
        Progress progressDialog = progressDialogRef.get();
        if (progressDialog != null) {
            progressDialog.setMessage(txt);
        }
    }

    protected final void finishActivity() {
        AbstractActivity activity = activityRef.get();
        if (activity != null) {
            activity.finish();
        }

    }

}

@@ -122,46 +122,8 @@
 s;%0A%0A
-import android.content.res.Resources;%0A
 impo
@@ -548,21 +548,16 @@
         
-this.
 activity
@@ -621,13 +621,8 @@
     
-this.
 prog
@@ -748,32 +748,38 @@
  msg) %7B%0A        
+final 
 AbstractActivity
@@ -1008,899 +1008,150 @@
     
-dismissProgress();%0A    %7D%0A%0A    protected final void showToast(final int resId) %7B%0A        AbstractActivity activity = activityRef.get();%0A        if (activity != null) %7B%0A            Resources res = activity.getResources();%0A            activity.showToast(res.getText(resId).toString());%0A        %7D%0A    %7D%0A%0A    protected final void dismissProgress() %7B%0A        Progress progressDialog = progressDialogRef.get();%0A        if (progressDialog != null) %7B%0A            progressDialog.dismiss();%0A        %7D%0A    %7D%0A%0A    protected final void setProgressMessage(final String txt) %7B%0A        Progress progressDialog = progressDialogRef.get();%0A        if (progressDialog != null) %7B%0A            progressDialog.setMessage(txt);%0A        %7D%0A    %7D%0A%0A    protected final void finishActivity() %7B%0A        AbstractActivity activity = activityRef.get();%0A        if (activity != null) %7B%0A            activity.finish();%0A        %7D%0A
+final Progress progressDialog = progressDialogRef.get();%0A        if (progressDialog != null) %7B%0A            progressDialog.dismiss();%0A        %7D
 %0A   

make SimpleHandler even simpler