|
| 1 | +package com.wxq.commonlibrary.weiget; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.Intent; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.Rect; |
| 7 | +import android.util.AttributeSet; |
| 8 | +import android.view.Gravity; |
| 9 | +import android.view.MotionEvent; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.FrameLayout; |
| 12 | +import android.widget.ImageView; |
| 13 | +import androidx.annotation.NonNull; |
| 14 | +import androidx.annotation.Nullable; |
| 15 | +import androidx.appcompat.app.AppCompatActivity; |
| 16 | +import androidx.customview.widget.ViewDragHelper; |
| 17 | +import androidx.fragment.app.Fragment; |
| 18 | +import androidx.fragment.app.FragmentManager; |
| 19 | + |
| 20 | +import com.hulab.debugkit.DebugFunction; |
| 21 | +import com.hulab.debugkit.DevTool; |
| 22 | +import com.hulab.debugkit.DevToolFragment; |
| 23 | +import com.wxq.commonlibrary.R; |
| 24 | +import com.wxq.commonlibrary.util.DensityUtil; |
| 25 | +import com.wxq.commonlibrary.util.ScreenUtils; |
| 26 | +import com.wxq.commonlibrary.util.StringUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author ArcherYc |
| 30 | + * @date on 2018/9/27 上午9:16 |
| 31 | + * @mail 247067345@qq.com |
| 32 | + */ |
| 33 | +public class DebugView extends FrameLayout { |
| 34 | + |
| 35 | + private ViewDragHelper mViewDragHelper; |
| 36 | + |
| 37 | + private Context mContext; |
| 38 | + |
| 39 | + private AppCompatActivity mActivity; |
| 40 | + |
| 41 | + private ImageView imvDebug; |
| 42 | + |
| 43 | + private DevTool.Builder builder; |
| 44 | + |
| 45 | + |
| 46 | + public DebugView(AppCompatActivity activity) { |
| 47 | + this(activity, null); |
| 48 | + this.mContext = activity; |
| 49 | + this.mActivity = activity; |
| 50 | + builder = new DevTool.Builder(activity); |
| 51 | + setBackgroundColor(Color.TRANSPARENT); |
| 52 | + init(); |
| 53 | + } |
| 54 | + |
| 55 | + private DebugView(Context context) { |
| 56 | + this(context, null); |
| 57 | + } |
| 58 | + |
| 59 | + private DebugView(Context context, @Nullable AttributeSet attrs) { |
| 60 | + this(context, attrs, 0); |
| 61 | + } |
| 62 | + |
| 63 | + private DebugView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 64 | + super(context, attrs, defStyleAttr); |
| 65 | + } |
| 66 | + |
| 67 | + private void init() { |
| 68 | + initDebugBuilder(); |
| 69 | + |
| 70 | + initFloatButton(); |
| 71 | + } |
| 72 | + |
| 73 | + private void initDebugBuilder() { |
| 74 | + int screenWidth = ScreenUtils.getScreenWidth(); |
| 75 | + int screenHeight = ScreenUtils.getScreenHeight(); |
| 76 | + int consoleWidth = screenWidth / 3 * 2; |
| 77 | + int consoleHeight = screenHeight / 2; |
| 78 | + builder.getTool().setConsoleWidth(DensityUtil.px2dip(mContext, consoleWidth)); |
| 79 | + builder.getTool().setConsoleHeight(DensityUtil.px2dip(mContext, consoleHeight)); |
| 80 | +// builder.addFunction(new DebugFunction("跳转调试页面") { |
| 81 | +// @Override |
| 82 | +// public String call() throws Exception { |
| 83 | +// mContext.startActivity(new Intent(mContext, ChooseCityActivity.class)); |
| 84 | +// return null; |
| 85 | +// } |
| 86 | +// }); |
| 87 | +// builder.addFunction(new DebugFunction("测试js") { |
| 88 | +// @Override |
| 89 | +// public String call() throws Exception { |
| 90 | +// |
| 91 | +// return null; |
| 92 | +// } |
| 93 | +// }); |
| 94 | + builder.addFunction(new DebugFunction("activity") { |
| 95 | + @Override |
| 96 | + public String call() throws Exception { |
| 97 | + return "\n" + mActivity.getClass().getSimpleName(); |
| 98 | + } |
| 99 | + }).addFunction(new DebugFunction("fragment") { |
| 100 | + @Override |
| 101 | + public String call() throws Exception { |
| 102 | + FragmentManager fragmentManager = mActivity.getSupportFragmentManager(); |
| 103 | + StringBuilder sb = new StringBuilder("\n"); |
| 104 | + addFragmentStr(sb, fragmentManager); |
| 105 | + if (StringUtils.isEmpty(sb.toString())) { |
| 106 | + return "no fragment"; |
| 107 | + } else { |
| 108 | + return sb.toString(); |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + builder.setTheme(DevToolFragment.DevToolTheme.DARK) |
| 113 | + .displayAt(screenWidth / 2 - consoleWidth / 2, screenHeight / 2 - consoleHeight / 2); |
| 114 | + } |
| 115 | + |
| 116 | + private void addFragmentStr(StringBuilder sb, FragmentManager activityFragmentManager) { |
| 117 | + for (Fragment fragment : activityFragmentManager.getFragments()) { |
| 118 | + View view = fragment.getView(); |
| 119 | + Rect rect = new Rect(); |
| 120 | + boolean visible = false; |
| 121 | + if (view != null) { |
| 122 | + visible = view.getGlobalVisibleRect(rect); |
| 123 | + } |
| 124 | + if (visible && fragment.isVisible() && fragment.getChildFragmentManager().getFragments().size() == 0) { |
| 125 | + sb.append(fragment.getClass().getSimpleName() + "--->visible" + "\n"); |
| 126 | + } else { |
| 127 | + sb.append(fragment.getClass().getSimpleName() + "\n"); |
| 128 | + } |
| 129 | + if (fragment.isVisible() && fragment.getChildFragmentManager().getFragments().size() > 0) { |
| 130 | + addFragmentStr(sb, fragment.getChildFragmentManager()); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private void initFloatButton() { |
| 136 | + imvDebug = new ImageView(mContext); |
| 137 | + imvDebug.setImageResource(R.mipmap.icon_debug); |
| 138 | + LayoutParams lp = new LayoutParams(DensityUtil.dip2px(mContext, 45), |
| 139 | + DensityUtil.dip2px(mContext, 45)); |
| 140 | + lp.gravity = Gravity.END | Gravity.CENTER_VERTICAL; |
| 141 | + imvDebug.setLayoutParams(lp); |
| 142 | + imvDebug.setOnClickListener(new OnClickListener() { |
| 143 | + @Override |
| 144 | + public void onClick(View v) { |
| 145 | + if (!builder.getTool().isAdded()) { |
| 146 | + builder.build(); |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + addView(imvDebug); |
| 152 | + mViewDragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() { |
| 153 | + @Override |
| 154 | + public boolean tryCaptureView(@NonNull View view, int i) { |
| 155 | + if (view.equals(imvDebug)) { |
| 156 | + return true; |
| 157 | + } else { |
| 158 | + return false; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public int clampViewPositionHorizontal(@NonNull View child, int left, int dx) { |
| 164 | + return left; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public int clampViewPositionVertical(@NonNull View child, int top, int dy) { |
| 169 | + return top; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public int getViewVerticalDragRange(@NonNull View child) { |
| 174 | + return getContext().getResources().getDisplayMetrics().heightPixels; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public int getViewHorizontalDragRange(@NonNull View child) { |
| 179 | + return getContext().getResources().getDisplayMetrics().widthPixels; |
| 180 | + } |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 186 | + if (mViewDragHelper != null) { |
| 187 | + return mViewDragHelper.shouldInterceptTouchEvent(ev); |
| 188 | + } |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public boolean onTouchEvent(MotionEvent event) { |
| 194 | + if (mViewDragHelper != null) { |
| 195 | + mViewDragHelper.processTouchEvent(event); |
| 196 | + } |
| 197 | + boolean inX = event.getX() > imvDebug.getX() && event.getX() < imvDebug.getX() + imvDebug.getWidth(); |
| 198 | + boolean inY = event.getY() > imvDebug.getY() && event.getY() < imvDebug.getY() + imvDebug.getHeight(); |
| 199 | + return inX && inY; |
| 200 | + } |
| 201 | + |
| 202 | + public void dismissDebugView() { |
| 203 | + if (builder != null) { |
| 204 | + if (builder.getTool().isAdded()) { |
| 205 | + try { |
| 206 | + mActivity.getFragmentManager() |
| 207 | + .beginTransaction() |
| 208 | + .remove(builder.getTool()) |
| 209 | + .commit(); |
| 210 | + } catch (Exception e) { |
| 211 | + e.printStackTrace(); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + public DevTool.Builder getDebugTool() { |
| 218 | + return builder; |
| 219 | + } |
| 220 | + |
| 221 | +} |
0 commit comments