Adding some utility methods for thread verification

Change-Id: I3fb6a1b860bba8a6efe413dd2698f972dc3e2506
This commit is contained in:
Sunny Goyal
2016-04-15 18:03:27 -07:00
parent 534058492f
commit 6388b2c61b
4 changed files with 54 additions and 12 deletions
-9
View File
@@ -47,7 +47,6 @@ import android.graphics.drawable.PaintDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Process;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
@@ -62,7 +61,6 @@ import android.widget.Toast;
import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.config.ProviderConfig;
import com.android.launcher3.util.IconNormalizer;
import java.io.ByteArrayOutputStream;
@@ -728,13 +726,6 @@ public final class Utilities {
(res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
}
public static void assertWorkerThread() {
if (ProviderConfig.IS_DOGFOOD_BUILD &&
(LauncherModel.sWorkerThread.getThreadId() != Process.myTid())) {
throw new IllegalStateException();
}
}
/**
* Returns true if the intent is a valid launch intent for a launcher activity of an app.
* This is used to identify shortcuts which are different from the ones exposed by the
@@ -33,6 +33,7 @@ import com.android.launcher3.compat.UserHandleCompat;
import com.android.launcher3.compat.UserManagerCompat;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.Preconditions;
import com.android.launcher3.util.SQLiteCacheHelper;
import com.android.launcher3.util.Thunk;
import com.android.launcher3.widget.WidgetCell;
@@ -169,7 +170,7 @@ public class WidgetPreviewLoader {
* This ensures that we remove entries for packages which changed while the launcher was dead.
*/
public void removeObsoletePreviews(ArrayList<? extends ComponentKey> list) {
Utilities.assertWorkerThread();
Preconditions.assertWorkerThread();
LongSparseArray<HashSet<String>> validPackages = new LongSparseArray<>();
@@ -16,10 +16,10 @@ import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.ItemInfo;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.LauncherAppWidgetProviderInfo;
import com.android.launcher3.Utilities;
import com.android.launcher3.compat.AlphabeticIndexCompat;
import com.android.launcher3.compat.AppWidgetManagerCompat;
import com.android.launcher3.config.ProviderConfig;
import com.android.launcher3.util.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
@@ -101,7 +101,7 @@ public class WidgetsModel {
}
public WidgetsModel updateAndClone(Context context) {
Utilities.assertWorkerThread();
Preconditions.assertWorkerThread();
try {
final ArrayList<WidgetItem> widgetsAndShortcuts = new ArrayList<>();
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.util;
import android.os.Looper;
import com.android.launcher3.LauncherModel;
import com.android.launcher3.config.ProviderConfig;
/**
* A set of utility methods for thread verification.
*/
public class Preconditions {
public static void assertWorkerThread() {
if (ProviderConfig.IS_DOGFOOD_BUILD && !isSameLooper(LauncherModel.getWorkerLooper())) {
throw new IllegalStateException();
}
}
public static void assertUIThread() {
if (ProviderConfig.IS_DOGFOOD_BUILD && !isSameLooper(Looper.getMainLooper())) {
throw new IllegalStateException();
}
}
public static void assertNonUiThread() {
if (ProviderConfig.IS_DOGFOOD_BUILD && isSameLooper(Looper.getMainLooper())) {
throw new IllegalStateException();
}
}
private static boolean isSameLooper(Looper looper) {
return Looper.myLooper() == looper;
}
}