WaitingView.uts 10.0 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
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 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';

DCloud-yyl's avatar
DCloud-yyl 已提交
29 30

export class WaitingView{
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

	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;
DCloud-yyl's avatar
DCloud-yyl 已提交
60 61 62 63 64 65 66
	
	
	private mpopWindow?: PopupWindow = null
	
	private currentHandler: Handler
	
		//{"back":"transmit","height":"112px","loading":{"display":"block","height":"55px","icon":"__uniapperror.png","jSONArray":false},"padding":"10px","size":"16px","width":"140px"}
67 68 69 70 71 72 73 74 75


	constructor(context?: Activity, style: UTSJSONObject) {
		
		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
DCloud-yyl's avatar
DCloud-yyl 已提交
76
		this.waitingView = LayoutInflater.from(context).inflate(R.layout.uni_prompt_loadingview, null, false) as ViewGroup
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		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)

DCloud-yyl's avatar
DCloud-yyl 已提交
92
		this.currentHandler = new Handler(Looper.getMainLooper())
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
		this.initView()
		this.makeBitmap()
		
	}

	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())
		}

DCloud-yyl's avatar
DCloud-yyl 已提交
146
		let drawable = this.context?.getResources()!!.getDrawable(R.drawable.uni_prompt_circle_white_progress)
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
		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())
	}


	/**
	 * 决定是否要透传点击事件
	 */
	passThrough(ev: MotionEvent): Boolean{
		if(this.context == null){
		    return false
		}
		return this.context!.dispatchTouchEvent(ev)
	}
	
	
	showWaiting() {
DCloud-yyl's avatar
DCloud-yyl 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
		if(this.context != null && !this.context!.isFinishing()){
			let width = -2;
			let height = -2
			if (Build.VERSION.SDK_INT >= 23 && this.mask) {
				this.mask = false
				width = -1
				height = -1
			}
			let mpop = new PopupWindow(this.waitingView, width.toInt(), height.toInt(), this.mask)
			this.mpopWindow = mpop
			mpop.showAtLocation((this.context?.findViewById<ViewGroup>(android.R.id.content))?.getChildAt(0), Gravity.CENTER, 0, 0)
			mpop.setOutsideTouchable(true)
			mpop.setOnDismissListener(new WaitingDismissListener())
			mpop.setTouchInterceptor(new TouchInterceptorListener(this.mask))
		}
187 188 189
	}

	close() {
DCloud-yyl's avatar
DCloud-yyl 已提交
190 191 192
		if (this.mpopWindow != null && (this.mpopWindow as PopupWindow).isShowing()) {
			this.currentHandler.post(new MainThreadRunnable((this.mpopWindow as PopupWindow)))
		}
193 194 195 196 197 198 199 200 201 202
		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") {
DCloud-yyl's avatar
DCloud-yyl 已提交
203
				imageBytes = this.inputStreamToArray(this.context?.getResources()!!.getAssets().open("uni-uts/uni-prompt/toast_success.png"))
204
			} else if (this.LoadingIcon == "errorIcon") {
DCloud-yyl's avatar
DCloud-yyl 已提交
205
				imageBytes = this.inputStreamToArray(this.context?.getResources()!!.getAssets().open("uni-uts/uni-prompt/toast_error.png"))
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 314 315 316 317 318
			} 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
	}
}