mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-07 01:28:52 -06:00
Throw exception when slow methods are executed on the main thread
This commit is contained in:
@@ -23,6 +23,7 @@ import android.content.Context;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.graphics.Typeface;
|
import android.graphics.Typeface;
|
||||||
|
import android.os.Looper;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
@@ -110,4 +111,19 @@ public abstract class UIHelper
|
|||||||
DisplayMetrics metrics = resources.getDisplayMetrics();
|
DisplayMetrics metrics = resources.getDisplayMetrics();
|
||||||
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, metrics);
|
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, metrics);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throws a runtime exception if called from the main thread. Useful to make sure that
|
||||||
|
* slow methods never accidentally slow the application down.
|
||||||
|
*
|
||||||
|
* @throws RuntimeException when run from main thread
|
||||||
|
*/
|
||||||
|
public static void throwIfMainThread() throws RuntimeException
|
||||||
|
{
|
||||||
|
Looper looper = Looper.myLooper();
|
||||||
|
if(looper == null) return;
|
||||||
|
|
||||||
|
if(looper == Looper.getMainLooper())
|
||||||
|
throw new RuntimeException("This method should never be called from the main thread");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import com.activeandroid.query.Delete;
|
|||||||
import com.activeandroid.query.Select;
|
import com.activeandroid.query.Select;
|
||||||
|
|
||||||
import org.isoron.uhabits.helpers.DateHelper;
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.helpers.UIHelper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
@@ -151,6 +152,8 @@ public class CheckmarkList
|
|||||||
*/
|
*/
|
||||||
protected void compute(long from, final long to)
|
protected void compute(long from, final long to)
|
||||||
{
|
{
|
||||||
|
UIHelper.throwIfMainThread();
|
||||||
|
|
||||||
final long day = DateHelper.millisecondsInOneDay;
|
final long day = DateHelper.millisecondsInOneDay;
|
||||||
|
|
||||||
Checkmark newestCheckmark = findNewest();
|
Checkmark newestCheckmark = findNewest();
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import com.activeandroid.util.SQLiteUtils;
|
|||||||
|
|
||||||
import org.isoron.uhabits.helpers.DatabaseHelper;
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
import org.isoron.uhabits.helpers.DateHelper;
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.helpers.UIHelper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Writer;
|
import java.io.Writer;
|
||||||
@@ -102,6 +103,8 @@ public class ScoreList
|
|||||||
*/
|
*/
|
||||||
protected void compute(long from, long to)
|
protected void compute(long from, long to)
|
||||||
{
|
{
|
||||||
|
UIHelper.throwIfMainThread();
|
||||||
|
|
||||||
final long day = DateHelper.millisecondsInOneDay;
|
final long day = DateHelper.millisecondsInOneDay;
|
||||||
final double freq = ((double) habit.freqNum) / habit.freqDen;
|
final double freq = ((double) habit.freqNum) / habit.freqDen;
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import com.activeandroid.query.Delete;
|
|||||||
import com.activeandroid.query.Select;
|
import com.activeandroid.query.Select;
|
||||||
|
|
||||||
import org.isoron.uhabits.helpers.DateHelper;
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.helpers.UIHelper;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
@@ -86,6 +87,8 @@ public class StreakList
|
|||||||
|
|
||||||
public void rebuild()
|
public void rebuild()
|
||||||
{
|
{
|
||||||
|
UIHelper.throwIfMainThread();
|
||||||
|
|
||||||
long beginning;
|
long beginning;
|
||||||
long today = DateHelper.getStartOfToday();
|
long today = DateHelper.getStartOfToday();
|
||||||
long day = DateHelper.millisecondsInOneDay;
|
long day = DateHelper.millisecondsInOneDay;
|
||||||
|
|||||||
Reference in New Issue
Block a user