import {Inject, Injectable, QueryList, ViewChildren} from '@angular/core'; import {AppVersion} from '@ionic-native/app-version/ngx'; import {ResultModel} from '../../models/result-model'; import {ScreenOrientation} from '@ionic-native/screen-orientation/ngx'; import {SplashScreen} from '@ionic-native/splash-screen/ngx'; import {StatusBar} from '@ionic-native/status-bar/ngx'; import {ActionSheetController, Config, IonRouterOutlet, MenuController, ModalController, Platform, PopoverController, ToastController} from '@ionic/angular'; import {ApiAnon} from '../../core/services/http/apiAnon'; import {M2Logger} from '../../core/services/m2.logger'; import { Device } from '@ionic-native/device/ngx'; import {map} from 'rxjs/operators'; import {Router} from '@angular/router'; import {M2Toast} from '../../layout/setting/m2.toast'; import {Location} from '@angular/common'; import {TabsUtilsService} from '../../core/services/menu/tabs-utils.service'; import {M2_DEVICE_INFO_PROVIDER} from '../../core/tokens/m2.tokens'; import {DeviceInfoInterface} from '../../core/interfaces/device-info.interface'; import {PLATFORM} from '../../core/enum/platform'; @Injectable() export class NativeService { // 对android手机回退按键的处理 lastTimeBackPress = 0; timePeriodToExit = 3000; constructor(private platform: Platform, private m2log: M2Logger, private apiAnon: ApiAnon, private m2Toast: M2Toast, private config: Config, private splashScreen: SplashScreen, private screenOrientation: ScreenOrientation, private statusBar: StatusBar, private device: Device, private modalCtrl: ModalController, private menu: MenuController, private actionSheetCtrl: ActionSheetController, private popoverCtrl: PopoverController, private router: Router, private tabsUtilsService: TabsUtilsService, @Inject(M2_DEVICE_INFO_PROVIDER) private deviceInfoInterface: DeviceInfoInterface, private location: Location) { } async appSetting() { await this.platform.ready(); const item = await this.deviceInfoInterface.ready(); if (item.platform !== PLATFORM.Browser && item.platform !== PLATFORM.Other) { // 真机运行才设置状态等信息 // this.statusBar.styleDefault(); // this.statusBar.styleLightContent(); // this.statusBar.overlaysWebView(true); // this.statusBar.show(); // this.statusBar.hide(); // this.statusBar.backgroundColorByHexString('#3171e0'); // this.statusBar.overlaysWebView(true); // 安卓机型实现沉浸式效果, if (this.platform.is('android')) { setTimeout(() => { this.statusBar.overlaysWebView(true); this.statusBar.hide(); this.statusBar.show(); this.statusBar.overlaysWebView(true); }, 1000); } this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT) .then(value => { }) .catch(reason => console.log(reason)); // 对于android机型设置回退按键事件 if (this.platform.is('android')) { this._exitApp(); } } // 设置ionic全局行为 this.config.set('backButtonText', '返回'); } async hideSplash() { const item = await this.deviceInfoInterface.ready(); if (item.platform !== PLATFORM.Browser && item.platform !== PLATFORM.Other) { this.splashScreen.hide(); window.localStorage.setItem('_DF_splashScreenTime' , new Date().toISOString()); } } private _exitApp() { // this.platform.backButton.subscribe(() => { // // code that is executed when the user pressed the back button // console.log("backButton.subscribe"); // }); this.platform.backButton.subscribeWithPriority(0, async () => { // close action sheet try { const element = await this.actionSheetCtrl.getTop(); if (element) { element.dismiss(); return; } } catch (error) { } // close popover try { const element = await this.popoverCtrl.getTop(); if (element) { element.dismiss(); return; } } catch (error) { } // close modal // 为了保证升级模态页面不被安卓的回退键关闭,暂时不处理modal页面 // try { // const element = await this.modalCtrl.getTop(); // console.log(element); // if (element) { // element.dismiss().then( info => console.log(info)); // return; // } // } catch (error) { // console.log(error); // // } // close side menua try { const element = await this.menu.getOpen(); if (element) { this.menu.close(); return; } } catch (error) { } if (!this._checkRootPage(this.location.path())) { this.location.back(); } else { if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) { // this.platform.exitApp(); // Exit from app (navigator as any).app.exitApp(); // work in ionic 4 } else { this.m2Toast.presentToast('再按一下退出应用!').then( () => { this.lastTimeBackPress = new Date().getTime(); }); } } // routerOutlets.forEach((outlet: IonRouterOutlet) => { // console.log(outlet); // console.log(outlet.canGoBack() ); // if (outlet && outlet.canGoBack()) { // outlet.pop(); // // } else if (this.router.url === '/home') { // if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) { // // this.platform.exitApp(); // Exit from app // navigator['app'].exitApp(); // work in ionic 4 // // } else { // this.m2Toast.presentToast("再按一下退出应用!").then( () => { // this.lastTimeBackPress = new Date().getTime(); // }); // } // } // }); }); } private _checkRootPage(url: string): boolean { const index = this.tabsUtilsService.getTabs().findIndex( menu => '/m2/' + menu.navUrl === url); return index !== -1 ? true : false; } }