package com.rehivetech.beeeon.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;

/**
 * Improved version of ProgressDialog which supports calling show/dismiss on UI thread and disables screen orientation during showing.
 */
public class BetterProgressDialog extends ProgressDialog {
	private final Activity mActivity;

	public BetterProgressDialog(final Activity activity) {
		super(activity);
		mActivity = activity;
	}

	/**
	 * Show progress dialog and disable orientation change of activity.
	 * This method works on UI thread.
	 */
	@Override
	public void show() {
		// Disable orientation change
		int currentOrientation = mActivity.getResources().getConfiguration().orientation;
		mActivity.setRequestedOrientation(currentOrientation == Configuration.ORIENTATION_LANDSCAPE
				? ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
				: ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				BetterProgressDialog.super.show();
			}
		});
	}

	/**
	 * Dismiss progress dialog and enable again orientation change of activity.
	 * This method works on UI thread (by default).
	 */
	@Override
	public void dismiss() {
		super.dismiss();

		// Enable orientation change again
		mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
	}

	/**
	 * Change progress message to string from resources.
	 * This method works on UI thread.
	 * @param resourceId
	 */
	public void setMessageResource(int resourceId) {
		CharSequence message = resourceId > 0 ? mActivity.getString(resourceId) : "";

		// Call our setMessage that works on UI thread
		setMessage(message);
	}

	/**
	 * Change progress message.
	 * This method works on UI thread.
	 * @param message
	 */
	@Override
	public void setMessage(final CharSequence message) {
		mActivity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				BetterProgressDialog.super.setMessage(message);
			}
		});
	}
}

@@ -1291,24 +1291,234 @@
 smiss() %7B%0A%09%09
+// For older versions of Android we must call it on UI thread, newer devices probably handles that itself%0A%09%09mActivity.runOnUiThread(new Runnable() %7B%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09%09%09BetterProgressDialog.
 super.dismis
@@ -1521,16 +1521,27 @@
 smiss();
+%0A%09%09%09%7D%0A%09%09%7D);
 %0A%0A%09%09// E

Fix dismissing BetterProgressDialog on older Android devices (fixes #525)