index.uts 3.7 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7

import Rect from "android.graphics.Rect";
import Color from "android.graphics.Color";
import TextView from "android.widget.TextView";
import FrameLayout from "android.widget.FrameLayout";
import ViewGroup from "android.view.ViewGroup";
import Gravity from "android.view.Gravity";
杜庆泉's avatar
杜庆泉 已提交
8 9 10
import Runnable from 'java.lang.Runnable';
import Exception from 'java.lang.Exception';
import MediaPlayer from 'android.media.MediaPlayer';
杜庆泉's avatar
杜庆泉 已提交
11

杜庆泉's avatar
杜庆泉 已提交
12 13 14 15 16 17 18 19 20 21 22
import logo from "../../static/logo.png";

import {
    onAppActivityDestroy,
    onAppActivityPause,
    onAppActivityResume,
    onAppActivityBack,
	getUniActivity,
	getAppContext
} from "io.dcloud.uts.android";

杜庆泉's avatar
杜庆泉 已提交
23 24 25
/**
 * 定时任务参数封装
 */
杜庆泉's avatar
杜庆泉 已提交
26 27 28 29 30 31
type TimerOptions = {
  start: (res: string) => void;
  work: (res: string) => void;
};


杜庆泉's avatar
杜庆泉 已提交
32 33 34
/**
 * 执行延时任务
 */
杜庆泉's avatar
杜庆泉 已提交
35 36 37 38 39 40 41 42 43
export function doTimerTask(opts:TimerOptions) {
	opts.start('doTimerTask start');
	setTimeout(function() {
		opts.work("doTimerTask work");
	}, 2000);
  
  return { name: "doTimerTask" };
}

杜庆泉's avatar
杜庆泉 已提交
44 45 46
/**
 * 执行周期任务
 */
杜庆泉's avatar
杜庆泉 已提交
47 48
export function doIntervalTask(opts:TimerOptions) {
	
杜庆泉's avatar
杜庆泉 已提交
49
	let taskRet = setInterval(function() {
杜庆泉's avatar
杜庆泉 已提交
50 51 52 53 54 55 56
		opts.work("doIntervalTask work");
	}, 2000);
	opts.start('doIntervalTask start');
  
  return { name: "doIntervalTask",taskId:taskRet};
}

杜庆泉's avatar
杜庆泉 已提交
57 58 59
/**
 * 清除周期任务
 */
杜庆泉's avatar
杜庆泉 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72
export function clearIntervalTask(taskId:number) {
	
	clearInterval(taskId);
  
  return { name: "clearIntervalTask"};
}



class AddUIRunnable extends Runnable {

    override run():void {
		
杜庆泉's avatar
杜庆泉 已提交
73 74 75
        let textView = new TextView(getUniActivity())
        textView.setText("HELLO WORLD");
        textView.textSize = 30.0.toFloat();
杜庆泉's avatar
杜庆泉 已提交
76
        textView.setBackgroundColor(Color.RED)
杜庆泉's avatar
杜庆泉 已提交
77 78
        textView.setTag("helloText")
        textView.setGravity(Gravity.CENTER)
杜庆泉's avatar
杜庆泉 已提交
79

杜庆泉's avatar
杜庆泉 已提交
80
        let decorView = getUniActivity()!.window.decorView;
杜庆泉's avatar
杜庆泉 已提交
81 82


杜庆泉's avatar
杜庆泉 已提交
83
        let frameContent = decorView.findViewById<FrameLayout>(android.R.id.content)
杜庆泉's avatar
杜庆泉 已提交
84
        let layoutParam = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
杜庆泉's avatar
杜庆泉 已提交
85 86
        layoutParam.topMargin = 200;

杜庆泉's avatar
杜庆泉 已提交
87
        frameContent.addView(textView,layoutParam)
杜庆泉's avatar
杜庆泉 已提交
88 89 90 91 92 93 94 95

    }
};

class RemoveUIRunnable extends Runnable {

    override run():void {

杜庆泉's avatar
杜庆泉 已提交
96
        let decorView = getUniActivity()!.window.decorView;
杜庆泉's avatar
杜庆泉 已提交
97
        let frameContent = decorView.findViewById<FrameLayout>(android.R.id.content)
杜庆泉's avatar
杜庆泉 已提交
98 99 100
		
		let targetTV = frameContent.findViewWithTag<TextView>("helloText")
		frameContent.removeView(targetTV)
杜庆泉's avatar
杜庆泉 已提交
101 102 103 104 105 106

    }
};


export function addViewToDecorView() {
杜庆泉's avatar
杜庆泉 已提交
107
    let uiRunable = new AddUIRunnable();
杜庆泉's avatar
杜庆泉 已提交
108 109 110 111 112
    getUniActivity()!.runOnUiThread(uiRunable)

}

export function removeViewToDecorView() {
杜庆泉's avatar
杜庆泉 已提交
113
    var uiRunable = new RemoveUIRunnable();
杜庆泉's avatar
杜庆泉 已提交
114 115 116 117
    getUniActivity()!.runOnUiThread(uiRunable)
}


杜庆泉's avatar
杜庆泉 已提交
118 119 120 121 122 123 124 125



export function getLogoPath(): string {
  return logo;
}


杜庆泉's avatar
杜庆泉 已提交
126
export function playAssetAudio() {
杜庆泉's avatar
杜庆泉 已提交
127
	
杜庆泉's avatar
杜庆泉 已提交
128 129 130
	let assetManager = getAppContext()!.getAssets();
	let afd = assetManager.openFd("free.mp3");
	let mediaPlayer = new MediaPlayer();
杜庆泉's avatar
杜庆泉 已提交
131 132 133 134 135 136
	mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
	mediaPlayer.prepare();
	mediaPlayer.start();
	
}

杜庆泉's avatar
杜庆泉 已提交
137 138


杜庆泉's avatar
杜庆泉 已提交
139 140


杜庆泉's avatar
杜庆泉 已提交
141
export function initAppLifecycle(onLifecycleChange: (event:string) => void) {
杜庆泉's avatar
杜庆泉 已提交
142 143

    onAppActivityDestroy(() => {
杜庆泉's avatar
杜庆泉 已提交
144 145 146
		let eventName = "onAppActivityDestroy - " + Date.now();
		onLifecycleChange(eventName);
        console.log(eventName);
杜庆泉's avatar
杜庆泉 已提交
147 148
    });
    onAppActivityPause(() => {
杜庆泉's avatar
杜庆泉 已提交
149 150 151
		let eventName = "onAppActivityPause - " + Date.now();
		onLifecycleChange(eventName);
        console.log(eventName);
杜庆泉's avatar
杜庆泉 已提交
152 153
    });
    onAppActivityResume(() => {
杜庆泉's avatar
杜庆泉 已提交
154 155 156
		let eventName = "onAppActivityResume - " + Date.now();
		onLifecycleChange(eventName);
        console.log(eventName);
杜庆泉's avatar
杜庆泉 已提交
157 158
    });
    onAppActivityBack(() => {
杜庆泉's avatar
杜庆泉 已提交
159 160 161
		let eventName = "onAppActivityBack - " + Date.now();
		onLifecycleChange(eventName);
        console.log(eventName);
杜庆泉's avatar
杜庆泉 已提交
162 163 164 165
    });

}