|
| 1 | +/** |
| 2 | + * Copyright 2025 The Ground Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an 'AS IS' BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import {Component, OnInit} from '@angular/core'; |
| 18 | +import {Router} from '@angular/router'; |
| 19 | +import {firstValueFrom} from 'rxjs'; |
| 20 | + |
| 21 | +import {AppConfigService} from 'app/services/app-config/app-config.service'; |
| 22 | + |
| 23 | +@Component({ |
| 24 | + selector: 'ground-android-page', |
| 25 | + template: ` |
| 26 | + <div *ngIf="playStoreId$ | async as playStoreId"> |
| 27 | + <a |
| 28 | + [href]="'https://play.google.com/store/apps/details?id=' + playStoreId" |
| 29 | + > |
| 30 | + Go to Play Store |
| 31 | + </a> |
| 32 | + </div> |
| 33 | + `, |
| 34 | +}) |
| 35 | +export class AndroidPageComponent implements OnInit { |
| 36 | + fullPath = ''; |
| 37 | + playStoreId$ = this.appConfigService.getPlayStoreId(); |
| 38 | + |
| 39 | + constructor( |
| 40 | + private appConfigService: AppConfigService, |
| 41 | + private router: Router |
| 42 | + ) {} |
| 43 | + |
| 44 | + async ngOnInit(): Promise<void> { |
| 45 | + this.fullPath = this.router.url; |
| 46 | + |
| 47 | + const playStoreId = await firstValueFrom(this.playStoreId$); |
| 48 | + |
| 49 | + if (!playStoreId) return; |
| 50 | + |
| 51 | + const timeout = 1500; // ms |
| 52 | + |
| 53 | + // Fallback: redirect to Play Store if app doesn't open |
| 54 | + const fallback = setTimeout(() => { |
| 55 | + window.location.href = `https://play.google.com/store/apps/details?id=${playStoreId}`; |
| 56 | + }, timeout); |
| 57 | + |
| 58 | + // Try opening the app via intent URL |
| 59 | + window.location.href = `intent://${this.fullPath}#Intent;scheme=https;package=${playStoreId};end`; |
| 60 | + |
| 61 | + // Cancel fallback if app is opened (browser loses focus) |
| 62 | + const blurHandler = () => { |
| 63 | + clearTimeout(fallback); |
| 64 | + window.removeEventListener('blur', blurHandler); |
| 65 | + }; |
| 66 | + window.addEventListener('blur', blurHandler); |
| 67 | + } |
| 68 | +} |
0 commit comments