| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import { Injectable, InjectionToken, inject, signal } from '@angular/core';
- import { registerPlugin } from '@capacitor/core';
- import { isNativeApp } from './api/platform-proxy.service';
- import { UiMToastService } from '../ui/mobile/components/toast/toast';
- export type SettingsTarget = 'application' | 'notifications' | 'battery' | 'autostart';
- /** 系统页没直达时的兜底提示(按目标区分,别让用户在一堆列表里瞎找) */
- const FALLBACK_HINT: Record<SettingsTarget, string> = {
- notifications: '请在应用信息中打开「通知管理」',
- battery: '请在应用信息中找到「省电策略」,选择「无限制」',
- autostart: '请在应用信息中找到「自启动」并允许;不同品牌位置不同',
- application: '请在应用信息中检查相关权限',
- };
- export interface AppSettingsStatus {
- notificationsEnabled: boolean;
- manufacturer: string;
- /** 仅 Android 标准省电白名单;不能据此推断厂商的后台策略。 */
- batteryOptimizationsIgnored: boolean;
- }
- export interface SettingsOpenResult { destination: SettingsTarget; fallback: boolean; }
- export interface AppSettingsPlugin {
- open(options?: { target: SettingsTarget }): Promise<SettingsOpenResult>;
- getStatus(): Promise<AppSettingsStatus>;
- }
- export const APP_SETTINGS_PLUGIN = new InjectionToken<AppSettingsPlugin>('APP_SETTINGS_PLUGIN', {
- providedIn: 'root', factory: () => registerPlugin<AppSettingsPlugin>('AppSettings'),
- });
- export const APP_SETTINGS_NATIVE = new InjectionToken<() => boolean>('APP_SETTINGS_NATIVE', {
- providedIn: 'root', factory: () => isNativeApp,
- });
- @Injectable({ providedIn: 'root' })
- export class AppSettingsService {
- private readonly plugin = inject(APP_SETTINGS_PLUGIN);
- private readonly native = inject(APP_SETTINGS_NATIVE);
- private readonly toast = inject(UiMToastService);
- readonly status = signal<AppSettingsStatus | null>(null);
- async refresh(): Promise<AppSettingsStatus | null> {
- if (!this.native()) return null;
- try { const status = await this.plugin.getStatus(); this.status.set(status); return status; }
- catch { this.status.set(null); return null; }
- }
- async open(target: SettingsTarget): Promise<boolean> {
- if (!this.native()) return false;
- try {
- const result = await this.plugin.open({ target });
- if (result.fallback) this.toast.info(FALLBACK_HINT[target] ?? FALLBACK_HINT.application);
- return true;
- } catch {
- this.toast.error('未能打开设置,请长按 Fmode 图标进入「应用信息」');
- return false;
- }
- }
- }
|