提交 253bebfa 编写于 作者: B Blankj

see 07/29 log

上级 58513446
* `19/07/29` [fix] BusUtils post father class useless. KeyboardUtils#hideSoft bug. Publish v1.25.4.
* `19/07/28` [add] NetworkUtils#(un)registerNetworkStatusChangedListener. Publish v1.25.3.
* `19/07/27` [fix] ThreadUtils memory leak.
* `19/07/26` [add] ContainerUtils. Publish v1.25.2.
......
......@@ -45,7 +45,7 @@
[frame]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/auc_frame.png
[aucSvg]: https://img.shields.io/badge/AndroidUtilCode-v1.25.3-brightgreen.svg
[aucSvg]: https://img.shields.io/badge/AndroidUtilCode-v1.25.4-brightgreen.svg
[auc]: https://github.com/Blankj/AndroidUtilCode
[apiSvg]: https://img.shields.io/badge/API-14+-brightgreen.svg
......
......@@ -45,7 +45,7 @@ If this project helps you a lot and you want to support the project's developmen
[frame]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/auc_frame.png
[aucSvg]: https://img.shields.io/badge/AndroidUtilCode-v1.25.3-brightgreen.svg
[aucSvg]: https://img.shields.io/badge/AndroidUtilCode-v1.25.4-brightgreen.svg
[auc]: https://github.com/Blankj/AndroidUtilCode
[apiSvg]: https://img.shields.io/badge/API-14+-brightgreen.svg
......
......@@ -11,15 +11,15 @@ class Config {
static applicationId = 'com.blankj.androidutilcode'
static appName = 'Util'
static compileSdkVersion = 27
static compileSdkVersion = 28
static minSdkVersion = 14
static targetSdkVersion = 27
static versionCode = 1_025_003
static versionName = '1.25.3'// E.g. 1.9.72 => 1,009,072
static targetSdkVersion = 28
static versionCode = 1_025_004
static versionName = '1.25.4'// E.g. 1.9.72 => 1,009,072
// lib version
static kotlin_version = '1.3.10'
static support_version = '27.1.1'
static support_version = '28.0.0'
static leakcanary_version = '1.6.3'
// appConfig 配置的是可以跑 app 的模块,git 提交务必只包含 launcher
......
......@@ -10,6 +10,7 @@ import com.blankj.utilcode.pkg.R
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.Utils
import kotlinx.android.synthetic.main.activity_bus.*
import kotlin.random.Random
/**
* ```
......@@ -21,6 +22,11 @@ import kotlinx.android.synthetic.main.activity_bus.*
*/
class BusActivity : CommonTitleActivity() {
@BusUtils.Bus(tag = TAG_BASIC_TYPE)
fun test(param: Int) {
busAboutTv.text = param.toString()
}
@BusUtils.Bus(tag = TAG_BUS)
fun test(param: String) {
busAboutTv.text = param
......@@ -40,6 +46,7 @@ class BusActivity : CommonTitleActivity() {
}
companion object {
const val TAG_BASIC_TYPE = "tag_basic_type"
const val TAG_BUS = "tag_bus"
const val TAG_STICKY_BUS = "tag_sticky_bus"
const val TAG_IO = "tag_io"
......@@ -65,6 +72,7 @@ class BusActivity : CommonTitleActivity() {
busRegister,
busUnregister,
busPost,
busPostBasicType,
busPostSticky,
busPost2IoThread,
busRemoveSticky,
......@@ -88,6 +96,9 @@ class BusActivity : CommonTitleActivity() {
R.id.busPost -> {
BusUtils.post(TAG_BUS, TAG_BUS)
}
R.id.busPostBasicType -> {
BusUtils.post(TAG_BASIC_TYPE, Random(System.currentTimeMillis()).nextInt())
}
R.id.busPostSticky -> {
BusUtils.postSticky(TAG_STICKY_BUS, object : Callback {
override fun call(): String {
......
......@@ -9,12 +9,11 @@ import android.os.Bundle
import android.support.annotation.StringRes
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import com.blankj.base.rv.BaseAdapter
import com.blankj.base.rv.BaseCell
import com.blankj.base.rv.BaseViewHolder
import com.blankj.base.rv.BaseItem
import com.blankj.base.rv.BaseItemAdapter
import com.blankj.base.rv.ItemViewHolder
import com.blankj.common.CommonTaskActivity
import com.blankj.utilcode.pkg.Config
import com.blankj.utilcode.pkg.R
......@@ -31,7 +30,7 @@ import kotlinx.android.synthetic.main.activity_image.*
* desc : demo about ImageUtils
* ```
*/
class ImageActivity : CommonTaskActivity<List<BaseCell>>() {
class ImageActivity : CommonTaskActivity<List<ImageCell>>() {
companion object {
fun start(context: Context) {
......@@ -42,7 +41,7 @@ class ImageActivity : CommonTaskActivity<List<BaseCell>>() {
private lateinit var src: Bitmap
override fun doInBackground(): List<BaseCell> {
override fun doInBackground(): List<ImageCell> {
src = ImageUtils.getBitmap(R.drawable.image_lena)
val round = ImageUtils.getBitmap(R.drawable.main_avatar_round)
val watermark = ImageUtils.getBitmap(R.mipmap.ic_launcher)
......@@ -50,8 +49,8 @@ class ImageActivity : CommonTaskActivity<List<BaseCell>>() {
val width = src.width
val height = src.height
return ArrayList<BaseCell>().apply {
add(HeaderCell(src))
return ArrayList<ImageCell>().apply {
add(ImageCell(src))
add(ImageCell(R.string.image_src, src))
add(ImageCell(R.string.image_add_color, ImageUtils.drawColor(src, Color.parseColor("#8000FF00"))))
add(ImageCell(R.string.image_scale, ImageUtils.scale(src, width / 2, height / 2)))
......@@ -80,9 +79,10 @@ class ImageActivity : CommonTaskActivity<List<BaseCell>>() {
}
}
override fun runOnUiThread(data: List<BaseCell>) {
val imageAdapter = BaseAdapter<BaseCell>()
imageAdapter.data = data
override fun runOnUiThread(data: List<ImageCell>) {
val imageAdapter = BaseItemAdapter<ImageCell>()
imageAdapter.setHasStableIds(true)
imageAdapter.items = data
imageRv.adapter = imageAdapter
imageRv.layoutManager = LinearLayoutManager(this@ImageActivity)
}
......@@ -105,20 +105,33 @@ class ImageActivity : CommonTaskActivity<List<BaseCell>>() {
override fun onDebouncingClick(view: View) {}
}
class ImageCell(@StringRes val resId: Int, private val image: Bitmap) : BaseCell(R.layout.item_image) {
class ImageCell : BaseItem<ImageCell> {
override fun bind(holder: BaseViewHolder, position: Int) {
holder.findViewById<TextView>(R.id.imageItemNameTv).text = StringUtils.getString(resId)
holder.findViewById<ImageView>(R.id.imageItemIv).setImageBitmap(image)
private lateinit var mSrc: Bitmap
private var mResId: Int = 0
private lateinit var mImage: Bitmap
constructor(src: Bitmap) : super(R.layout.item_image_header) {
mSrc = src
}
}
class HeaderCell(private val src: Bitmap) : BaseCell(R.layout.item_image_header) {
constructor(@StringRes resId: Int, image: Bitmap) : super(R.layout.item_image) {
mResId = resId
mImage = image
}
override fun bind(holder: BaseViewHolder, position: Int) {
holder.findViewById<Button>(R.id.imageSaveBtn).setOnClickListener {
val save = ImageUtils.save(src, Config.CACHE_PATH + "lena.jpg", Bitmap.CompressFormat.JPEG)
ToastUtils.showLong(if (save) "successful" else "failed")
override fun bind(holder: ItemViewHolder, position: Int) {
if (isViewType(R.layout.item_image_header)) {
holder.setOnClickListener(R.id.imageSaveBtn) {
val save = ImageUtils.save(mSrc, Config.CACHE_PATH + "lena.jpg", Bitmap.CompressFormat.JPEG)
ToastUtils.showLong(if (save) "successful" else "failed")
}
return
}
if (isViewType(R.layout.item_image)) {
holder.findViewById<TextView>(R.id.imageItemNameTv).text = StringUtils.getString(mResId)
holder.findViewById<ImageView>(R.id.imageItemIv).setImageBitmap(mImage)
return
}
}
}
\ No newline at end of file
......@@ -33,6 +33,13 @@
android:layout_height="wrap_content"
android:text="@string/bus_post" />
<Button
android:id="@+id/busPostBasicType"
style="@style/WideBtnStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bus_post_basic_type" />
<Button
android:id="@+id/busPostSticky"
style="@style/WideBtnStyle"
......
......@@ -140,6 +140,7 @@
<string name="bus_register">Register</string>
<string name="bus_unregister">Unregister</string>
<string name="bus_post">Post</string>
<string name="bus_post_basic_type">Post Basic Type</string>
<string name="bus_post_sticky">Post Sticky</string>
<string name="bus_post_to_io_thread">Post To IO Thread</string>
<string name="bus_remove_sticky">Remove Sticky</string>
......
package com.blankj.base.rv;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.List;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/08/22
* desc :
* </pre>
*/
public class BaseAdapter<Cell extends BaseCell> extends RecyclerView.Adapter<BaseViewHolder> {
public BaseCell mEmptyCell;
public List<BaseCell> mHeaders;
public List<Cell> mData;
public List<BaseCell> mFooters;
public Context mContext;
public LayoutInflater mInflater;
@Override
public final int getItemViewType(int position) {
int headerSize = getHeaderSize();
if (headerSize > position) {
return mHeaders.get(position).viewType;
}
return mData.get(position).viewType;
}
@NonNull
@Override
public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
BaseViewHolder baseViewHolder = Cell.onCreateViewHolder(parent, viewType);
return baseViewHolder;
}
@Override
public final void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
mData.get(position).bind(holder, position);
}
@Override
public void onViewRecycled(@NonNull BaseViewHolder holder) {
super.onViewRecycled(holder);
int position = holder.getAdapterPosition();
mData.get(position).onViewRecycled(holder, position);
}
@Override
public int getItemCount() {
return getHeaderSize() + getDataSize() + getFooterSize();
}
public void setEmptyCell(BaseCell emptyCell) {
mEmptyCell = emptyCell;
}
private int getHeaderSize() {
if (mHeaders == null) return 0;
return mHeaders.size();
}
private int getDataSize() {
if (mData == null) return 0;
return mData.size();
}
private int getFooterSize() {
if (mFooters == null) return 0;
return mFooters.size();
}
public void setData(@NonNull final List<Cell> data) {
mData = data;
}
public List<Cell> getData() {
return mData;
}
}
package com.blankj.base.rv;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
......@@ -15,45 +17,65 @@ import android.view.ViewGroup;
* desc :
* </pre>
*/
public abstract class BaseCell {
public abstract class BaseItem<T extends BaseItem> {
private static final SparseIntArray LAYOUT_SPARSE_ARRAY = new SparseIntArray();
private static final SparseArray<View> VIEW_SPARSE_ARRAY = new SparseArray<>();
static int getLayoutByType(int type) {
return LAYOUT_SPARSE_ARRAY.get(type, -1);
static ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
int layoutByType = LAYOUT_SPARSE_ARRAY.get(viewType, -1);
if (layoutByType != -1) {
return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(layoutByType, parent, false));
}
View viewByType = VIEW_SPARSE_ARRAY.get(viewType);
if (viewByType != null) {
return new ItemViewHolder(viewByType);
}
throw new RuntimeException("onCreateViewHolder: get holder from view type failed.");
}
static View getViewByType(int type) {
return VIEW_SPARSE_ARRAY.get(type);
}
public abstract void bind(@NonNull final ItemViewHolder holder, final int position);
public abstract void bind(@NonNull final BaseViewHolder holder, final int position);
public void onViewRecycled(@NonNull final ItemViewHolder holder, final int position) {/**/}
public void onViewRecycled(@NonNull final BaseViewHolder holder, final int position) {
public long getItemId() {
return RecyclerView.NO_ID;
}
protected int viewType;
private int viewType;
BaseItemAdapter<T> mAdapter;
public BaseCell(int layoutId) {
viewType = layoutId + getClass().hashCode();
public BaseItem(@LayoutRes int layoutId) {
viewType = getViewTypeByLayoutId(layoutId);
LAYOUT_SPARSE_ARRAY.put(viewType, layoutId);
}
public BaseCell(View view) {
viewType = view.hashCode() + getClass().hashCode();
public BaseItem(@NonNull View view) {
viewType = getViewTypeByView(view);
VIEW_SPARSE_ARRAY.put(viewType, view);
}
public static BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
int layoutByType = getLayoutByType(viewType);
if (layoutByType != -1) {
return new BaseViewHolder(LayoutInflater.from(parent.getContext()).inflate(layoutByType, parent, false));
}
View viewByType = getViewByType(viewType);
if (viewByType != null) {
return new BaseViewHolder(viewByType);
}
throw new RuntimeException("onCreateViewHolder: get holder from view type failed.");
public int getViewType() {
return viewType;
}
public BaseItemAdapter<T> getAdapter() {
return mAdapter;
}
public boolean isViewType(@LayoutRes int layoutId) {
return viewType == getViewTypeByLayoutId(layoutId);
}
public boolean isViewType(@NonNull View view) {
return viewType == getViewTypeByView(view);
}
private int getViewTypeByLayoutId(@LayoutRes int layoutId) {
return layoutId + getClass().hashCode();
}
private int getViewTypeByView(@NonNull View view) {
return view.hashCode() + getClass().hashCode();
}
}
package com.blankj.base.rv;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/08/22
* desc :
* </pre>
*/
public class BaseItemAdapter<Item extends BaseItem> extends RecyclerView.Adapter<ItemViewHolder> {
public List<Item> mItems;
public BaseItemAdapter() {
this(false);
}
public BaseItemAdapter( boolean hasStableIds) {
setHasStableIds(hasStableIds);
}
@Override
public final int getItemViewType(int position) {
Item item = mItems.get(position);
item.mAdapter = this;
return item.getViewType();
}
@Override
public long getItemId(int position) {
return mItems.get(position).getItemId();
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return Item.onCreateViewHolder(parent, viewType);
}
@Override
public final void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
mItems.get(position).bind(holder, position);
}
@Override
public int getItemCount() {
return mItems.size();
}
@Override
public void onViewRecycled(@NonNull ItemViewHolder holder) {
super.onViewRecycled(holder);
int position = holder.getAdapterPosition();
if (position < 0 || position >= mItems.size()) {
return;
}
mItems.get(position).onViewRecycled(holder, position);
}
public void setItems(@NonNull final List<Item> items) {
mItems = items;
}
public List<Item> getItems() {
return Collections.unmodifiableList(mItems);
}
public Item getItem(@IntRange(from = 0) final int position) {
return mItems.get(position);
}
public boolean isEmpty() {
return mItems.isEmpty();
}
///////////////////////////////////////////////////////////////////////////
// id
///////////////////////////////////////////////////////////////////////////
public Item getItemById(final long id) {
int itemIndex = getItemIndexById(id);
if (itemIndex != -1) {
return mItems.get(itemIndex);
} else {
return null;
}
}
public int getItemIndexById(final long id) {
for (int i = 0; i < mItems.size(); i++) {
if (getItemId(i) == id) {
return i;
}
}
return -1;
}
public boolean hasItemWithId(final long id) {
return getItemIndexById(id) != -1;
}
public int replaceItemById(final long id, @NonNull final Item item) {
return replaceItemById(id, item, false);
}
public int replaceItemById(final long id, @NonNull final Item item, boolean notifyChanged) {
int itemIndex = getItemIndexById(id);
if (itemIndex != -1) {
replaceItem(itemIndex, item, notifyChanged);
}
return itemIndex;
}
public int removeItemById(final long id) {
return removeItemById(id, false);
}
public int removeItemById(final long id, boolean notifyRemoved) {
for (int i = 0; i < mItems.size(); i++) {
if (getItemId(i) == id) {
removeItem(i, notifyRemoved);
return i;
}
}
return -1;
}
///////////////////////////////////////////////////////////////////////////
// operate
///////////////////////////////////////////////////////////////////////////
public void addItem(@NonNull final Item item) {
addItem(item, false);
}
public void addItem(@NonNull final Item item, boolean notifyInserted) {
mItems.add(item);
if (notifyInserted) notifyItemInserted(mItems.size() - 1);
}
public void addItem(@IntRange(from = 0) final int index, @NonNull final Item item) {
addItem(index, item, false);
}
public void addItem(@IntRange(from = 0) final int index, @NonNull final Item item, boolean notifyInserted) {
mItems.add(index, item);
if (notifyInserted) notifyItemInserted(index);
}
public void addItems(@NonNull final List<Item> items) {
addItems(items, false);
}
public void addItems(@NonNull final List<Item> items, boolean notifyInserted) {
mItems.addAll(items);
if (notifyInserted) notifyItemRangeInserted(mItems.size() - items.size() - 1, items.size());
}
public void addItems(@IntRange(from = 0) final int index, @NonNull final List<Item> items) {
addItems(index, items, false);
}
public void addItems(@IntRange(from = 0) final int index, @NonNull final List<Item> items, boolean notifyInserted) {
mItems.addAll(index, items);
if (notifyInserted) notifyItemRangeInserted(index, items.size());
}
public void swapItem(@IntRange(from = 0) final int firstIndex, @IntRange(from = 0) final int secondIndex) {
swapItem(firstIndex, secondIndex, false);
}
public void swapItem(@IntRange(from = 0) final int firstIndex,
@IntRange(from = 0) final int secondIndex, boolean notifyMoved) {
Collections.swap(mItems, firstIndex, secondIndex);
if (notifyMoved) notifyItemMoved(firstIndex, secondIndex);
}
public Item replaceItem(@IntRange(from = 0) final int index, @NonNull final Item item) {
return replaceItem(index, item, false);
}
public Item replaceItem(@IntRange(from = 0) final int index, @NonNull final Item item, boolean notifyChanged) {
Item prevItem = mItems.set(index, item);
if (notifyChanged) notifyItemChanged(index);
return prevItem;
}
public boolean replaceItems(@NonNull final List<Item> items) {
return replaceItems(items, false);
}
public boolean replaceItems(@NonNull final List<Item> items, boolean notifyDataSetChanged) {
mItems.clear();
boolean added = mItems.addAll(items);
if (notifyDataSetChanged) notifyDataSetChanged();
return added;
}
public Item removeItem(@IntRange(from = 0) final int index) {
return removeItem(index, false);
}
public Item removeItem(@IntRange(from = 0) final int index, boolean notifyRemoved) {
Item removedItem = mItems.remove(index);
if (notifyRemoved) notifyItemRemoved(index);
return removedItem;
}
public int removeItem(@NonNull final Item object) {
return removeItem(object, false);
}
public int removeItem(@NonNull final Item object, boolean notifyRemoved) {
int itemIndex = mItems.indexOf(object);
if (itemIndex != -1) {
mItems.remove(itemIndex);
if (notifyRemoved) notifyItemRemoved(itemIndex);
}
return itemIndex;
}
public void clear() {
clear(false);
}
public void clear(boolean notifyDataSetChanged) {
mItems.clear();
if (notifyDataSetChanged) notifyDataSetChanged();
}
public void sortItems(@NonNull final Comparator<Item> comparator) {
sortItems(comparator, false);
}
public void sortItems(@NonNull final Comparator<Item> comparator, boolean notifyDataSetChanged) {
Collections.sort(mItems, comparator);
if (notifyDataSetChanged) notifyDataSetChanged();
}
}
......@@ -5,6 +5,8 @@ import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import java.util.List;
/**
* <pre>
* author: Blankj
......@@ -13,11 +15,11 @@ import android.view.View;
* desc :
* </pre>
*/
public class BaseViewHolder extends RecyclerView.ViewHolder {
public class ItemViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> viewArray = new SparseArray<>();
public BaseViewHolder(View itemView) {
public ItemViewHolder(View itemView) {
super(itemView);
}
......@@ -30,4 +32,12 @@ public class BaseViewHolder extends RecyclerView.ViewHolder {
}
return (T) view;
}
public void setOnClickListener(@IdRes final int viewId, View.OnClickListener listener) {
findViewById(viewId).setOnClickListener(listener);
}
public void setOnLongClickListener(@IdRes final int viewId, View.OnLongClickListener listener) {
findViewById(viewId).setOnLongClickListener(listener);
}
}
......@@ -2,10 +2,10 @@
Gradle:
```groovy
implementation 'com.blankj:utilcode:1.25.3'
implementation 'com.blankj:utilcode:1.25.4'
// if u use AndroidX, use the following
implementation 'com.blankj:utilcodex:1.25.3'
implementation 'com.blankj:utilcodex:1.25.4'
```
......
......@@ -2,10 +2,10 @@
Gradle:
```groovy
implementation 'com.blankj:utilcode:1.25.3'
implementation 'com.blankj:utilcode:1.25.4'
// if u use AndroidX, use the following
implementation 'com.blankj:utilcodex:1.25.3'
implementation 'com.blankj:utilcodex:1.25.4'
```
......
......@@ -8,10 +8,14 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
/**
......@@ -27,8 +31,10 @@ public final class BusUtils {
private static final Object NULL = "nULl";
private static final String TAG = "BusUtils";
private final Map<String, BusInfo> mTag_BusInfoMap = new HashMap<>();
private final Map<String, Set<Object>> mClassName_BusesMap = new ConcurrentHashMap<>();
private final Map<String, BusInfo> mTag_BusInfoMap = new HashMap<>();
private final Map<String, List<String>> mClassName_TagsMap = new HashMap<>();
private final Map<String, Map<String, Object>> mClassName_Tag_Arg4StickyMap = new ConcurrentHashMap<>();
private BusUtils() {
......@@ -90,7 +96,8 @@ public final class BusUtils {
private void registerInner(final Object bus) {
if (bus == null) return;
String className = bus.getClass().getName();
Class aClass = bus.getClass();
String className = aClass.getName();
synchronized (mClassName_BusesMap) {
Set<Object> buses = mClassName_BusesMap.get(className);
if (buses == null) {
......@@ -99,6 +106,27 @@ public final class BusUtils {
}
buses.add(bus);
}
List<String> tags = mClassName_TagsMap.get(className);
if (tags == null) {
synchronized (mClassName_TagsMap) {
tags = mClassName_TagsMap.get(className);
if (tags == null) {
tags = new ArrayList<>();
for (Map.Entry<String, BusInfo> entry : mTag_BusInfoMap.entrySet()) {
BusInfo value = entry.getValue();
try {
if (Class.forName(value.className).isAssignableFrom(aClass)) {
tags.add(entry.getKey());
value.classNames.add(className);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
mClassName_TagsMap.put(className, tags);
}
}
}
processSticky(bus);
}
......@@ -150,7 +178,7 @@ public final class BusUtils {
if ("".equals(busInfo.paramType)) {
return Class.forName(busInfo.className).getDeclaredMethod(busInfo.funName);
} else {
return Class.forName(busInfo.className).getDeclaredMethod(busInfo.funName, Class.forName(busInfo.paramType));
return Class.forName(busInfo.className).getDeclaredMethod(busInfo.funName, getClassName(busInfo.paramType));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
......@@ -160,6 +188,29 @@ public final class BusUtils {
return null;
}
private Class getClassName(String paramType) throws ClassNotFoundException {
switch (paramType) {
case "boolean":
return boolean.class;
case "int":
return int.class;
case "long":
return long.class;
case "short":
return short.class;
case "byte":
return byte.class;
case "double":
return double.class;
case "float":
return float.class;
case "char":
return char.class;
default:
return Class.forName(paramType);
}
}
private void invokeMethod(final String tag, final Object arg, final BusInfo busInfo, final boolean sticky) {
Runnable runnable = new Runnable() {
@Override
......@@ -189,8 +240,14 @@ public final class BusUtils {
}
private void realInvokeMethod(final String tag, Object arg, BusInfo busInfo, boolean sticky) {
Set<Object> buses = mClassName_BusesMap.get(busInfo.className);
if (buses == null || buses.size() == 0) {
Set<Object> buses = new HashSet<>();
for (String className : busInfo.classNames) {
Set<Object> subBuses = mClassName_BusesMap.get(className);
if (subBuses != null && !subBuses.isEmpty()) {
buses.addAll(subBuses);
}
}
if (buses.size() == 0) {
if (!sticky) {
Log.e(TAG, "The bus of tag <" + tag + "> was not registered before.");
return;
......@@ -258,13 +315,14 @@ public final class BusUtils {
private static final class BusInfo {
String className;
String funName;
String paramType;
String paramName;
boolean sticky;
String threadMode;
Method method;
String className;
String funName;
String paramType;
String paramName;
boolean sticky;
String threadMode;
Method method;
List<String> classNames;
BusInfo(String className, String funName, String paramType, String paramName,
boolean sticky, String threadMode) {
......@@ -274,6 +332,7 @@ public final class BusUtils {
this.paramName = paramName;
this.sticky = sticky;
this.threadMode = threadMode;
this.classNames = new CopyOnWriteArrayList<>();
}
@Override
......
......@@ -85,14 +85,15 @@ public final class KeyboardUtils {
public static void hideSoftInput(@NonNull final Activity activity) {
View view = activity.getCurrentFocus();
if (view == null) {
View focusView = activity.getWindow().getDecorView().findViewWithTag("keyboardTagView");
View decorView = activity.getWindow().getDecorView();
View focusView = decorView.findViewWithTag("keyboardTagView");
if (focusView == null) {
view = new EditText(activity);
view.setTag("keyboardTagView");
((ViewGroup) decorView).addView(view, 0, 0);
} else {
view = focusView;
}
((ViewGroup) activity.getWindow().getDecorView()).addView(view, 0, 0);
view.requestFocus();
}
hideSoftInput(view);
......
......@@ -1178,7 +1178,7 @@ public final class ThreadUtils {
@Override
public void run() {
onSuccess(result);
cancelTimerTask(Task.this);
removeTask(Task.this);
}
});
}
......@@ -1192,7 +1192,7 @@ public final class ThreadUtils {
@Override
public void run() {
onFail(throwable);
cancelTimerTask(Task.this);
removeTask(Task.this);
}
});
}
......@@ -1218,7 +1218,7 @@ public final class ThreadUtils {
@Override
public void run() {
onCancel();
cancelTimerTask(Task.this);
removeTask(Task.this);
}
});
}
......@@ -1262,11 +1262,8 @@ public final class ThreadUtils {
return sDeliver;
}
private static void cancelTimerTask(final Task task) {
TaskInfo timerTask = TASK_TASKINFO_MAP.get(task);
if (timerTask != null) {
TASK_TASKINFO_MAP.remove(task);
}
private static void removeTask(final Task task) {
TASK_TASKINFO_MAP.remove(task);
}
private static class TaskInfo {
......
......@@ -26,13 +26,6 @@ import android.widget.TextView;
import android.widget.Toast;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;
/**
* <pre>
......@@ -454,6 +447,16 @@ public final class ToastUtils {
@Override
public void show() {
Utils.runOnUiThreadDelayed(new Runnable() {
@Override
public void run() {
realShow();
}
}, 300);
}
private void realShow() {
if (mToast == null) return;
mView = mToast.getView();
if (mView == null) return;
final Context context = mToast.getView().getContext();
......
......@@ -24,6 +24,11 @@ import java.util.concurrent.Executor;
@Config(manifest = Config.NONE, shadows = {ShadowLog.class})
public class BaseTest {
@BusUtils.Bus(tag = "base")
public void noParamFun(int i) {
System.out.println("base" + i);
}
public BaseTest() {
ShadowLog.stream = System.out;
ThreadUtils.setDeliver(new Executor() {
......@@ -33,10 +38,12 @@ public class BaseTest {
}
});
Utils.init(RuntimeEnvironment.application);
ReflectUtils getInstance = ReflectUtils.reflect(BusUtils.class).method("getInstance");
getInstance.method("registerBus", "base", BaseTest.class.getName(), "noParamFun", "int", "i", false, "POSTING");
}
@Test
public void test() throws Exception {
}
}
}
\ No newline at end of file
......@@ -239,6 +239,14 @@ public class BusUtilsTest extends BaseTest {
System.out.println("BusUtils.toString_() = " + BusUtils.toString_());
}
@Test
public void testBase() {
BaseTest t = new BusUtilsTest();
BusUtils.register(t);
BusUtils.post("base", 1);
BusUtils.unregister(t);
}
public interface Callback {
String call();
}
......
......@@ -84,6 +84,7 @@ public class BusClassVisitor extends ClassVisitor {
if ("this".equals(name)) {
return;
}
System.out.println("funParamDesc: " + funParamDesc + ", desc: " + desc);
funParamDesc = funParamDesc.substring(desc.length());// 每次去除参数直到为 "",那么之后的就不是参数了
busInfo.paramsInfo.add(new BusInfo.ParamsInfo(Type.getType(desc).getClassName(), name));
if (busInfo.isParamSizeNoMoreThanOne && busInfo.paramsInfo.size() > 1) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册