WaitingView.uts 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
// import Context from 'android.content.Context';
import LayoutInflater from 'android.view.LayoutInflater';
import R from 'io.dcloud.uts.prompt.R';
import ViewGroup from 'android.view.ViewGroup';
import LinearLayout from 'android.widget.LinearLayout';
import Activity from 'android.app.Activity';
import TextView from 'android.widget.TextView';
import PopupWindow from 'android.widget.PopupWindow';
import Build from 'android.os.Build';
import Gravity from 'android.view.Gravity';
import Color from 'android.graphics.Color';
import Handler from 'android.os.Handler';
import Looper from 'android.os.Looper';
import string from 'android.R.string';
import View from 'android.view.View';
import MotionEvent from 'android.view.MotionEvent';
import JSONObject from 'com.alibaba.fastjson.JSONObject';
import ProgressBar from 'android.widget.ProgressBar';
import BitmapFactory from 'android.graphics.BitmapFactory';
import bool from 'android.R.bool';
import Bitmap from 'android.graphics.Bitmap';
import ImageView from 'android.widget.ImageView';
import { UTSAndroid } from "io.dcloud.uts";
import FileInputStream from 'java.io.FileInputStream';
import File from 'java.io.File';
import ByteArrayOutputStream from 'java.io.ByteArrayOutputStream';
import InputStream from 'java.io.InputStream';
import array from 'android.R.array';
import Dialog from 'android.app.Dialog';

export class WaitingView extends Dialog{

	private context?: Activity
	private style: JSONObject

	private waitingView: ViewGroup
	private waitingRootView: LinearLayout
	private textView: TextView
	private mProgressBar: ProgressBar
	private seaparatorView: View
	private mImageView: ImageView

	private height: Int = -2
	private width: Int = -2
	private mask: boolean = false
	private title: string = ""

	screenWidth: Int
	ScreenHeight: Int
	density: number = 0

	// json数据
	private padding: Int = 0
	private textSize: Int = 0


	private loadingdDisplay = ""
	private loadingHeight = 0
	private LoadingIcon: string = ""
	private mBitmap: Bitmap | null = null;


	constructor(context?: Activity, style: UTSJSONObject) {
		
		super(context!,R.style.loadingDialog);
		
		this.context = context;
		this.style = style.toJSONObject() as JSONObject
		this.screenWidth = context?.getResources()?.getDisplayMetrics()!.widthPixels
		this.ScreenHeight = context?.getResources()?.getDisplayMetrics()!.heightPixels
		this.density = context?.getResources()?.getDisplayMetrics()!.density
		this.waitingView = LayoutInflater.from(context).inflate(R.layout.dcloud_loadingview, null, false) as ViewGroup
		this.waitingRootView = this.waitingView.findViewById<LinearLayout>(R.id.dcloud_pd_root)
		this.mProgressBar = this.waitingView.findViewById<ProgressBar>(R.id.dcloud_pb_loading)
		this.textView = this.waitingView.findViewById<TextView>(R.id.dcloud_tv_loading)
		this.seaparatorView = this.waitingView.findViewById<View>(R.id.dcloud_view_seaparator)
		this.mImageView = this.waitingView.findViewById<ImageView>(R.id.dcloud_iv_loading)
		
		this.textView.setMaxLines(2)

		this.handlerArguments()

		let llp = (this.waitingRootView).getLayoutParams() as LinearLayout.LayoutParams
		llp.width = this.width > 0 ? (this.width * this.density).toInt() : this.width
		llp.height = -2
		this.waitingRootView.setLayoutParams(llp)

		this.initView()
		this.makeBitmap()
		
		setContentView(waitingView)
	}

	handlerArguments() {
		if (this.style["height"] != null) {
			this.height = parseInt(this.style["height"] as string).toInt()
		}
		if (this.style["width"] != null) {
			this.width = parseInt(this.style["width"] as string).toInt()
		}
		if (this.style["modal"] != null) {
			this.mask = this.style["modal"] as boolean
		}
		this.title = this.style.get("name") as string
		if (this.style["loading"] != null) {
			let loading = this.style["loading"] as JSONObject
			this.loadingdDisplay = loading["display"] as string
			if (loading["icon"] != null) {
				this.LoadingIcon = loading["icon"] as string
			}
			if (loading["height"] != null) {
				this.loadingHeight = parseInt(loading["height"] as string).toInt()
			}
		}
		if ("block" == this.loadingdDisplay) {
			this.waitingRootView.setOrientation(LinearLayout.VERTICAL)
		} else if ("inline" == this.loadingdDisplay) {
			this.waitingRootView.setOrientation(LinearLayout.HORIZONTAL)
		} else if ("none" == this.loadingdDisplay) {
			this.seaparatorView.setVisibility(View.GONE)
			this.mProgressBar.setVisibility(View.GONE)
		}
	}

	initView() {
		// 初始化默认参数
		// this.textView.setTextColor(0xffffffff)
		this.textView.setTextColor(Color.WHITE)
		this.textView.setGravity(Gravity.CENTER)
		this.textView.setText(this.title)
		this.textView.setTextSize(0, (16 * this.density).toFloat())
		
		if(this.title.length < 1){
			// 没有文本需要展示
			this.textView.setVisibility(View.GONE)
			this.waitingRootView.setPadding((10 * this.density).toInt(), (40 * this.density).toInt(), (10 * this.density).toInt(), (30 * this.density).toInt())
		}else{
			// 既有loading,也有文本
			this.waitingRootView.setPadding((10 * this.density).toInt(), (20 * this.density).toInt(), (10 * this.density).toInt(), (20 * this.density).toInt())
		}

		let drawable = this.context?.getResources()!!.getDrawable(R.drawable.uts_dcloud_circle_white_progress)
		if (this.loadingHeight > 0) {
			this.mProgressBar.setLayoutParams(new LinearLayout.LayoutParams((this.loadingHeight * this.density).toInt(), (this.loadingHeight * this.density).toInt()))
		} else {
			let height = (drawable.getIntrinsicHeight() * 0.3).toInt()
			this.mProgressBar.setLayoutParams(new LinearLayout.LayoutParams(height, height))
		}

		this.mProgressBar.setIndeterminateDrawable(drawable)
		this.waitingRootView.setFocusable(true)
		this.waitingRootView.setAlpha(0.9.toFloat())
	}


	override onTouchEvent(ev: MotionEvent): Boolean{
		if(this.mask){
			/**
			 * 明确说明了要遮罩 拦截
			 */
			return super.onTouchEvent(ev)
		}
		 
		let event=MotionEvent.obtain(ev.downTime, ev.eventTime, ev.action,
			ev.rawX, ev.rawY,
			ev.pressure, ev.size, ev.metaState, ev.xPrecision, ev.yPrecision, ev.deviceId, ev.edgeFlags)

		return passThrough(event)
	}
	/**
	 * 决定是否要透传点击事件
	 */
	passThrough(ev: MotionEvent): Boolean{
		if(this.context == null){
		    return false
		}
		return this.context!.dispatchTouchEvent(ev)
	}
	
	
	showWaiting() {
		this.setCanceledOnTouchOutside(false)
		this.show()
	}

	close() {
		this.dismiss()
		if (this.mBitmap != null) {
			this.mBitmap = null;
		}
	}

	makeBitmap() {
		if (this.LoadingIcon != null) {
			// let mBitmap: Bitmap | null = null;
			let imageBytes: ByteArray | null = null
			if (this.LoadingIcon == "successIcon") {
				imageBytes = this.inputStreamToArray(this.context?.getResources()!!.getAssets().open("uniappsuccess_icon.png"))
			} else if (this.LoadingIcon == "errorIcon") {
				imageBytes = this.inputStreamToArray(this.context?.getResources()!!.getAssets().open("uniapperror_icon.png"))
			} else if (this.LoadingIcon.length > 0) {
				let path: string = ""
				if (this.LoadingIcon.startsWith("file://")) {
					path = this.LoadingIcon.replace("file://", "")
				} else {
					path = UTSAndroid.getResourcePath(this.LoadingIcon)
				}
				if (path != null && path.length > 0) {
					try {
						let fis: FileInputStream = new FileInputStream(new File(path));
						imageBytes = this.inputStreamToArray(fis);
						fis.close()
					} catch (e: Throwable) {
					}
				}
			} else {
				return
			}
			if (imageBytes == null || imageBytes.size == 0) {
				return
			}
			let option = new BitmapFactory.Options()
			option.inJustDecodeBounds = true
			BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size, option)
			let width = option.outWidth
			let height = option.outHeight

			option.inSampleSize = this.getBestScale(height)
			option.inJustDecodeBounds = false;
			this.mBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size, option)

			this.mProgressBar.setVisibility(View.GONE)
			this.mImageView.setVisibility(View.VISIBLE)
			this.mImageView.setImageBitmap(this.mBitmap)

			if (width % height != 0) {
				return
			}

			var lp: ViewGroup.LayoutParams = this.mImageView.getLayoutParams()
			if (lp != null) {
				if (this.loadingHeight > 0) {
					lp.height = (this.loadingHeight * this.density).toInt()
					lp.width = (this.loadingHeight * this.density).toInt()
				} else {
					lp.width = this.mBitmap!!.getHeight()
					lp.height = this.mBitmap!!.getHeight()
				}
				this.mImageView.setLayoutParams(lp)
			}
		}
	}

	getBestScale(height: Int): Int {
		let sampleSize: Int = 1;
		let maxEdge = Math.min(this.screenWidth, this.ScreenHeight) - 10 * 2 * this.density
		if (maxEdge > 0 && height > maxEdge) {
			sampleSize = (height / maxEdge).toInt()
		}
		return sampleSize
	}

	inputStreamToArray(inputStream: InputStream): ByteArray | null {
		try {
			let bos: ByteArrayOutputStream = new ByteArrayOutputStream()
			let bytes: ByteArray = new ByteArray(1024)

			do {
				let length = inputStream.read(bytes)
				if (length != -1) {
					bos.write(bytes, 0, length)
				} else {
					break
				}
			} while (true)
			bos.close()
			return bos.toByteArray()
		} catch (e: Throwable) {
			return null;
		}
	}
}

class MainThreadRunnable implements Runnable {

	pop: PopupWindow

	constructor(pop: PopupWindow) {
		this.pop = pop
	}

	override run() {
		this.pop.dismiss()
	}
}

class WaitingDismissListener implements PopupWindow.OnDismissListener {
	override onDismiss() {
	}
}

class TouchInterceptorListener implements View.OnTouchListener {

	interceptor: boolean

	constructor(interceptor: boolean) {
		this.interceptor = interceptor
	}

	override onTouch(v: View, event: MotionEvent): boolean {
		return this.interceptor
	}
}