utsAndroid.uvue 11.4 KB
Newer Older
lizhongyi_'s avatar
lizhongyi_ 已提交
1
<template>
2
<!--  #ifdef APP-ANDROID -->
DCloud-WZF's avatar
DCloud-WZF 已提交
3 4 5 6 7 8 9 10
  <scroll-view style="flex: 1">
    <view>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-hello-text"> 逐一点击执行,观察测试反馈 </view>
      </view>

      <button @click="getAppContextClick">getAppContext</button>
      <button @click="getUniActivityClick">getUniActivity</button>
杜庆泉's avatar
杜庆泉 已提交
11
      <button @click="getJavaClassClick">getJavaClass</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
12 13 14 15 16 17 18
      <button @click="getAppTempPathClick">getAppTempPath</button>
      <button @click="typeofClick">typeof</button>
      <button @click="arrayPermissionFlowClick">组权限申请流程测试</button>
      <button @click="singlePermissionFlowClick">单权限申请流程测试</button>
      <button @click="dispatchAsyncClick">任务分发测试</button>
      <button @click="pathTestClick">路径转换测试</button>
      <button @click="privacyStateClick">隐私协议状态测试</button>
M
mahaifeng 已提交
19
      <button @click="privacyStateCallBackClick">隐私协议回调测试</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-hello-text">
          1. 当前页面已通过initAppLifecycle函数注册了生命周期监听。
        </view>
        <view class="uni-hello-text">
          2. 手动切换其他APP再返回,可在控制台和界面观察事件日志
        </view>
      </view>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="text-box" scroll-y="true">
          <text>{{ text }}</text>
        </view>
      </view>
      <button @click="gotoSystemPermissionActivityClick">
        手动申请权限测试
      </button>
      <button @tap="testGoOtherActivity">跳转拍照界面</button>
      <button @tap="testUnRegLifecycle">取消注册周期函数</button>
      <image :src="selectImage" v-if="selectImage"></image>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-hello-text"> 获取设备信息,观察是否符合预期 </view>
      </view>
      <button @tap="getDeviceInfoClick">获取设备基础信息</button>
43
      <button @tap="getFileProviderUriClick">使用外部应用访问私有文件</button>
44 45 46 47 48

      <button @tap="activityCallback">注册activity 回调方法</button>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="uni-hello-text">
          点击注册activity 回调方法后,可以手动切换其他APP再返回,可在控制台和界面观察事件日志
M
mahaifeng 已提交
49
        </view>
50 51 52 53
      </view>
      <view class="uni-padding-wrap uni-common-mt">
        <view class="text-box" scroll-y="true">
          <text>{{ cbText }}</text>
M
mahaifeng 已提交
54
        </view>
55 56
      </view>
      <button @tap="unRegActivityCallback">取消注册activity 回调方法</button>
DCloud-WZF's avatar
DCloud-WZF 已提交
57 58 59
    </view>
  </scroll-view>
  <!-- #endif -->
lizhongyi_'s avatar
lizhongyi_ 已提交
60 61 62
</template>

<script>
63
  // #ifdef APP-ANDROID
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  import {
    getAppContextTest,
    getUniActivityTest,
    getJavaClassTest,
    getAppTempPathTest,
    typeofClickTest,
    gotoSystemPermissionActivityTest,
    arrayPermissionFlowTest,
    singlePermissionFlowTest,
    dispatchAsyncTest,
    convert2AbsFullPathTest,
    unRegLifecycle,
    initAppLifecycle,
    gotoCameraTake,
    getDeviceInfoTest,
    privacyStateTest
  } from '@/uni_modules/uts-platform-api'
81 82
  // #endif
  
83 84
  // #ifdef APP-ANDROID
  import {
M
mahaifeng 已提交
85 86 87 88 89
    UTSAcvitiyLifeCycleCallback,
    UTSAcvitiyKeyEventCallback,
    UTSActivityWindowCallback,
    UTSActivityCallback,
    UTSActivityComponentCallback,
90 91 92 93 94 95
    onCallbackChange
  } from '@/uni_modules/uts-syntaxcase'


  import File from 'java.io.File';
  import Intent from 'android.content.Intent';
96
 // #endif
M
mahaifeng 已提交
97

98 99
  /**
   * 测试在页面生命周期之外,使用api
100
   */   
101 102 103 104 105 106 107 108 109 110 111 112 113
  export default {
    data() {
      return {
        text: '',
        cbText: '',
        selectImage: '',
        callback: [] as Any[]
      }
    },
    unmounted() {
      // #ifdef APP-ANDROID
      this.unRegActivityCallback()
      // #endif
M
mahaifeng 已提交
114

115 116
    },
    onLoad: function () {
117
      // #ifdef APP-ANDROID
118 119 120 121 122 123 124 125
      let that = this
      initAppLifecycle(function (eventLog) {
        // 展示捕捉到的声明周期日志
        let nextLine = that.text + eventLog
        that.text = nextLine
        let nextLineFlag = that.text + '\n'
        that.text = nextLineFlag
      })
126
      // #endif
127 128
    },
    methods: {
129
      // #ifdef APP-ANDROID
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      privacyStateClick() {
        privacyStateTest(function (ret, desc) {
          if (ret) {
            uni.showToast({
              title: '测试通过',
            })
          } else {
            uni.showToast({
              icon: 'none',
              title: '失败:' + desc,
            })
          }
        })
      },
      privacyStateCallBackClick() {
145

146
        let isAgree : boolean = true
147

148 149
        const cb = (ret : PrivacyOption) => {
          console.log('privacyStateCallBackTest->' + ret.isAgree)
150
          if (ret.isAgree == isAgree) {
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
            uni.showToast({
              title: '测试通过'
            })
          } else {
            uni.showToast({
              icon: 'none',
              title: '失败'
            })
          }
        }
        // 先重置用户同意状态
        UTSAndroid.onPrivacyAgreeChange(cb)
        UTSAndroid.setPrivacyAgree(isAgree)
        UTSAndroid.offPrivacyAgreeChange(cb)
        setTimeout(function () {
166 167
          console.log('privacyStateCallBackTest->false')

168 169 170 171 172 173 174 175 176 177 178 179 180
          UTSAndroid.setPrivacyAgree(false)
        }, 5000);
      },
      getDeviceInfoClick() {
        this.text = getDeviceInfoTest()
      },
      testGoOtherActivity() {
        var that = this
        let ret = gotoCameraTake(function (file) {
          // 展示捕捉到的声明周期日志
          console.log(file)
          that.selectImage = 'file://' + file
        })
181

182
        if (!ret) {
DCloud-WZF's avatar
DCloud-WZF 已提交
183
          uni.showToast({
184 185 186 187 188 189 190 191 192 193 194 195 196
            icon: 'none',
            title: '测试失败',
          })
        }
      },
      testUnRegLifecycle() {
        // 取消注册生命周期
        unRegLifecycle()
      },
      getJavaClassClick() {
        if (getJavaClassTest()) {
          uni.showToast({
            title: '测试通过'
DCloud-WZF's avatar
DCloud-WZF 已提交
197 198 199
          })
        } else {
          uni.showToast({
200 201
            icon: 'error',
            title: '测试失败'
DCloud-WZF's avatar
DCloud-WZF 已提交
202 203
          })
        }
204 205 206
      },
      getAppContextClick() {
        if (getAppContextTest()) {
M
mahaifeng 已提交
207
          uni.showToast({
208
            title: '测试通过',
M
mahaifeng 已提交
209 210 211
          })
        } else {
          uni.showToast({
212 213
            icon: 'error',
            title: '测试失败',
M
mahaifeng 已提交
214 215
          })
        }
216
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
217

218 219
      getUniActivityClick() {
        if (getUniActivityTest()) {
DCloud-WZF's avatar
DCloud-WZF 已提交
220 221 222 223 224
          uni.showToast({
            title: '测试通过',
          })
        } else {
          uni.showToast({
225 226
            icon: 'error',
            title: '测试失败',
DCloud-WZF's avatar
DCloud-WZF 已提交
227 228
          })
        }
229 230 231
      },
      pathTestClick() {
        if (convert2AbsFullPathTest()) {
DCloud-WZF's avatar
DCloud-WZF 已提交
232 233 234 235 236
          uni.showToast({
            title: '测试通过',
          })
        } else {
          uni.showToast({
237 238
            icon: 'error',
            title: '测试失败',
DCloud-WZF's avatar
DCloud-WZF 已提交
239 240
          })
        }
241 242 243 244 245 246 247 248 249 250 251 252
      },
      getFileProviderUriClick() {
        let file = new File(UTSAndroid.getResourcePath("static/logo.png"))
        const uri = UTSAndroid.getFileProviderUri(file)
        console.log("uri", uri.toString())
        const intent = new Intent(Intent.ACTION_VIEW, uri)
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // 添加权限标志 
        const context = UTSAndroid.getUniActivity()!;
        context.startActivity(intent);
      },
      getAppTempPathClick() {
        if (getAppTempPathTest()) {
DCloud-WZF's avatar
DCloud-WZF 已提交
253 254 255 256 257
          uni.showToast({
            title: '测试通过',
          })
        } else {
          uni.showToast({
258 259
            icon: 'error',
            title: '测试失败',
DCloud-WZF's avatar
DCloud-WZF 已提交
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 319 320 321 322
      },
      dispatchAsyncClick() {
        dispatchAsyncTest(function (ret, desc) {
          if (ret) {
            uni.showToast({
              title: '测试通过',
            })
          } else {
            uni.showToast({
              icon: 'none',
              title: '失败:' + desc,
            })
          }
        })
      },
      typeofClick() {
        if (typeofClickTest()) {
          uni.showToast({
            title: '测试通过',
          })
        } else {
          uni.showToast({
            icon: 'error',
            title: '测试失败',
          })
        }
      },

      gotoSystemPermissionActivityClick() {
        gotoSystemPermissionActivityTest()
      },
      arrayPermissionFlowClick() {
        arrayPermissionFlowTest(function (ret, desc) {
          if (ret) {
            uni.showToast({
              icon: 'none',
              title: '测试通过',
            })
          } else {
            uni.showToast({
              icon: 'none',
              title: '失败:' + desc,
            })
          }
        })
      },
      singlePermissionFlowClick() {
        singlePermissionFlowTest(function (ret, desc) {
          if (ret) {
            uni.showToast({
              icon: 'none',
              title: '测试通过',
            })
          } else {
            uni.showToast({
              icon: 'none',
              title: '失败:' + desc,
            })
          }
        })
      },
323
       // #endif
324 325 326 327 328 329 330 331 332 333 334
      // #ifdef APP-ANDROID
      // #ifdef UNI-APP-X
      activityCallback() {
        var that = this
        onCallbackChange(function (eventLog : string) {
          // 展示捕捉到的声明周期日志
          let nextLine = that.cbText + eventLog
          that.cbText = nextLine
          let nextLineFlag = that.cbText + '\n'
          that.cbText = nextLineFlag
        })
335 336 337
        let index = getCurrentPages().length - 1
        let page = getCurrentPages()[index]
        console.log('page route=' + page.route)
338 339 340
        this.callback.push(new UTSAcvitiyLifeCycleCallback())
        this.callback.push(new UTSActivityWindowCallback())
        this.callback.push(new UTSAcvitiyKeyEventCallback())
341
        this.callback.push(new UTSActivityCallback(), page.route)
342 343 344
        this.callback.push(new UTSActivityComponentCallback())
        this.callback.forEach((value) => {
          if (value instanceof UTSAcvitiyLifeCycleCallback) {
345
            UTSAndroid.onActivityCallback(value,page.route)
346 347 348 349 350 351 352 353 354 355 356 357 358
          }
          if (value instanceof UTSActivityWindowCallback) {
            UTSAndroid.onActivityCallback(value)
          }
          if (value instanceof UTSAcvitiyKeyEventCallback) {
            UTSAndroid.onActivityCallback(value)
          }
          if (value instanceof UTSActivityCallback) {
            UTSAndroid.onActivityCallback(value)
          }
          if (value instanceof UTSActivityComponentCallback) {
            UTSAndroid.onActivityCallback(value)
          }
M
mahaifeng 已提交
359

360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
        })
      },
      unRegActivityCallback() {
        this.callback.forEach((value) => {

          if (value instanceof UTSAcvitiyLifeCycleCallback) {
            UTSAndroid.offActivityCallback(value)
          }
          if (value instanceof UTSActivityWindowCallback) {
            UTSAndroid.offActivityCallback(value)
          }
          if (value instanceof UTSAcvitiyKeyEventCallback) {
            UTSAndroid.offActivityCallback(value)
          }
          if (value instanceof UTSActivityCallback) {
            UTSAndroid.offActivityCallback(value)
          }
          if (value instanceof UTSActivityComponentCallback) {
            UTSAndroid.offActivityCallback(value)
          }
        })
      }
      // #endif
      // #endif

    },
  }
lizhongyi_'s avatar
lizhongyi_ 已提交
387 388 389
</script>

<style>
390 391 392 393
  .testButton {
    width: 100%;
  }
</style>