txa-settings-permission 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 TXA (txa@nrotxa.online)
4
+ GitHub: https://github.com/TXAVLOG/txa-settings-permission
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,391 @@
1
+ # txa-settings-permission
2
+
3
+ <div align="center">
4
+
5
+ ![npm](https://img.shields.io/npm/v/txa-settings-permission?color=6366f1&style=flat-square)
6
+ ![license](https://img.shields.io/npm/l/txa-settings-permission?color=22c55e&style=flat-square)
7
+ ![platform](https://img.shields.io/badge/platform-Android-3ddc84?style=flat-square)
8
+ ![capacitor](https://img.shields.io/badge/Capacitor-v4%20%7C%20v5%20%7C%20v6-119EFF?style=flat-square)
9
+
10
+ **Capacitor plugin by TXA** — Mở tất cả các màn hình cài đặt cấp quyền Android,
11
+ tự động tương thích theo Android version, log đẹp khi lỗi.
12
+
13
+ [📦 NPM](https://www.npmjs.com/package/txa-settings-permission) · [🐛 Issues](https://github.com/TXAVLOG/txa-settings-permission/issues) · [👤 TXA](mailto:txa@nrotxa.online)
14
+
15
+ </div>
16
+
17
+ ---
18
+
19
+ ## ✨ Tính năng
20
+
21
+ | Tính năng | Mô tả |
22
+ |-----------|-------|
23
+ | 📂 **All Files Access** | `MANAGE_EXTERNAL_STORAGE` (Android 11+) |
24
+ | 🪟 **Overlay** | Hiển thị trên ứng dụng khác (Android 6+) |
25
+ | 📦 **Install Unknown Apps** | Cài từ nguồn không rõ (Android 8+) |
26
+ | 🔋 **Battery Optimization** | Bỏ qua tối ưu hóa pin (Android 6+) |
27
+ | 🔔 **Notification Settings** | Cài đặt thông báo & DND |
28
+ | ♿ **Accessibility** | Quyền trợ năng |
29
+ | 📊 **Usage Stats** | Quyền dữ liệu sử dụng (Android 5.1+) |
30
+ | 📡 **System Settings** | WiFi, Location, Bluetooth, NFC, Sound, Display... |
31
+ | 🔍 **Runtime Check** | Check bất kỳ permission nào theo tên |
32
+ | 📋 **Bulk Check** | Check nhiều permission cùng lúc |
33
+ | 🔄 **Auto-compat** | Tự fallback theo Android version |
34
+ | 📝 **Đẹp Log** | Mọi kết quả đều kèm log object chi tiết |
35
+
36
+ ---
37
+
38
+ ## 📋 Yêu cầu
39
+
40
+ - **Capacitor** `>= 4.0.0`
41
+ - **Android SDK** `>= 22` (Android 5.0+)
42
+ - **Node.js** `>= 16`
43
+
44
+ > ⚠️ Plugin này **chỉ hoạt động trên Android**. Trên Web/iOS trả về mock (`granted: true`).
45
+
46
+ ---
47
+
48
+ ## 🚀 Cài đặt
49
+
50
+ ```bash
51
+ npm install txa-settings-permission
52
+ npx cap sync
53
+ ```
54
+
55
+ Sau khi cài, script `postinstall` tự kiểm tra `@capacitor/core`. Nếu thiếu sẽ báo:
56
+
57
+ ```
58
+ ❌ THIẾU DEPENDENCY BẮT BUỘC: @capacitor/core
59
+ npm install @capacitor/core @capacitor/android @capacitor/cli
60
+ ```
61
+
62
+ ---
63
+
64
+ ## ⚙️ Cấu hình Android
65
+
66
+ ### Thêm permissions vào `AndroidManifest.xml`
67
+
68
+ Chỉ thêm permission app bạn **thực sự cần**:
69
+
70
+ ```xml
71
+ <!-- android/app/src/main/AndroidManifest.xml -->
72
+ <manifest ...>
73
+
74
+ <!-- All Files Access (Android 11+) -->
75
+ <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
76
+
77
+ <!-- Overlay -->
78
+ <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
79
+
80
+ <!-- Bỏ qua tối ưu hóa pin -->
81
+ <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
82
+
83
+ <!-- Storage (Android < 13) -->
84
+ <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
85
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
86
+ android:maxSdkVersion="29" />
87
+
88
+ <!-- Media (Android 13+) -->
89
+ <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
90
+ <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
91
+ <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
92
+
93
+ <!-- Notifications (Android 13+) -->
94
+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
95
+
96
+ </manifest>
97
+ ```
98
+
99
+ ### Register plugin thủ công (nếu cần)
100
+
101
+ Capacitor 6+ tự auto-discovery. Nếu cần register thủ công trong `MainActivity.java`:
102
+
103
+ ```java
104
+ import online.txa.settingspermission.TxaSettingsPermissionPlugin;
105
+
106
+ public class MainActivity extends BridgeActivity {
107
+ @Override
108
+ public void onCreate(Bundle savedInstanceState) {
109
+ registerPlugin(TxaSettingsPermissionPlugin.class);
110
+ super.onCreate(savedInstanceState);
111
+ }
112
+ }
113
+ ```
114
+
115
+ ---
116
+
117
+ ## 📖 API Reference
118
+
119
+ ### `getDeviceInfo()`
120
+
121
+ Lấy thông tin thiết bị và feature flags theo Android version.
122
+
123
+ ```typescript
124
+ const info = await TxaSettingsPermission.getDeviceInfo();
125
+ // {
126
+ // androidSdk: 34, androidRelease: "14",
127
+ // manufacturer: "samsung", model: "SM-S918B",
128
+ // supportsManageAllFiles: true, // Android 11+
129
+ // supportsOverlay: true, // Android 6+
130
+ // supportsInstallUnknownApps: true, // Android 8+
131
+ // supportsBatteryOptimization: true,
132
+ // supportsMediaPermissions: true, // Android 13+
133
+ // ...
134
+ // }
135
+ ```
136
+
137
+ ---
138
+
139
+ ### `checkAllRequired()`
140
+
141
+ Check toàn bộ permission phổ biến cùng lúc. **Nên gọi khi app khởi động.**
142
+
143
+ ```typescript
144
+ const { checks, androidSdk } = await TxaSettingsPermission.checkAllRequired();
145
+
146
+ // checks.manageAllFiles → MANAGE_EXTERNAL_STORAGE
147
+ // checks.overlay → Draw Overlay
148
+ // checks.installUnknownApps
149
+ // checks.batteryOptimization
150
+ // checks.camera, microphone, location
151
+ // checks.readMediaVideo (Android 13+)
152
+ // checks.postNotifications (Android 13+)
153
+ // checks.readExternalStorage (Android < 13)
154
+ ```
155
+
156
+ ---
157
+
158
+ ### All Files Access (Android 11+)
159
+
160
+ ```typescript
161
+ // Check
162
+ const { granted, log } = await TxaSettingsPermission.checkManageAllFiles();
163
+ console.log(log.message); // "Đã có quyền MANAGE_EXTERNAL_STORAGE"
164
+
165
+ // Mở Settings
166
+ const result = await TxaSettingsPermission.openManageAllFiles();
167
+ // result.opened = true → Settings đã mở
168
+ // result.granted = true → Đã có quyền từ trước
169
+ // result.log.level = 'warn' nếu fallback sang màn hình chung
170
+ ```
171
+
172
+ > Android < 11: Tự động `{ granted: true }`, không mở gì.
173
+
174
+ ---
175
+
176
+ ### Overlay (Android 6+)
177
+
178
+ ```typescript
179
+ const { granted } = await TxaSettingsPermission.checkOverlay();
180
+ if (!granted) await TxaSettingsPermission.openOverlaySettings();
181
+ ```
182
+
183
+ ---
184
+
185
+ ### Install Unknown Apps (Android 8+)
186
+
187
+ ```typescript
188
+ const { granted } = await TxaSettingsPermission.checkInstallUnknownApps();
189
+ if (!granted) {
190
+ await TxaSettingsPermission.openInstallUnknownApps();
191
+ // Android 8+: màn hình riêng cho app
192
+ // Android 5-7: mở Security Settings (log.level = 'warn')
193
+ }
194
+ ```
195
+
196
+ ---
197
+
198
+ ### Battery Optimization (Android 6+)
199
+
200
+ ```typescript
201
+ const { ignoring } = await TxaSettingsPermission.checkBatteryOptimization();
202
+ if (!ignoring) {
203
+ const result = await TxaSettingsPermission.openBatteryOptimization();
204
+ // Tự fallback sang Battery Saver Settings nếu device không hỗ trợ
205
+ console.log(result.log.level, result.log.message);
206
+ }
207
+ ```
208
+
209
+ ---
210
+
211
+ ### Notifications
212
+
213
+ ```typescript
214
+ await TxaSettingsPermission.openNotificationSettings(); // Cài đặt thông báo
215
+ await TxaSettingsPermission.openNotificationPolicyAccess(); // DND / Không làm phiền
216
+ ```
217
+
218
+ ---
219
+
220
+ ### System Settings
221
+
222
+ ```typescript
223
+ await TxaSettingsPermission.openWifiSettings();
224
+ await TxaSettingsPermission.openLocationSettings();
225
+ await TxaSettingsPermission.openBluetoothSettings();
226
+ await TxaSettingsPermission.openNfcSettings();
227
+ await TxaSettingsPermission.openDateTimeSettings();
228
+ await TxaSettingsPermission.openLanguageSettings();
229
+ await TxaSettingsPermission.openStorageSettings();
230
+ await TxaSettingsPermission.openDisplaySettings();
231
+ await TxaSettingsPermission.openSoundSettings();
232
+ await TxaSettingsPermission.openAccessibilitySettings();
233
+ await TxaSettingsPermission.openUsageStatsSettings();
234
+ await TxaSettingsPermission.openDeviceAdminSettings();
235
+ await TxaSettingsPermission.openSettings(); // App Settings tổng quát
236
+ ```
237
+
238
+ ---
239
+
240
+ ### Runtime Permission Check
241
+
242
+ ```typescript
243
+ // Check 1 permission
244
+ const { granted, log } = await TxaSettingsPermission.checkRuntimePermission({
245
+ permission: 'android.permission.CAMERA',
246
+ });
247
+
248
+ // Check nhiều permission cùng lúc
249
+ const result = await TxaSettingsPermission.checkMultiplePermissions({
250
+ permissions: [
251
+ 'android.permission.CAMERA',
252
+ 'android.permission.RECORD_AUDIO',
253
+ 'android.permission.READ_MEDIA_VIDEO',
254
+ ],
255
+ });
256
+ console.log('Tất cả đã cấp:', result.allGranted);
257
+ console.log('Thiếu:', result.missing);
258
+ ```
259
+
260
+ ---
261
+
262
+ ## 📝 Log Object
263
+
264
+ Mọi method đều trả về `log` kèm theo:
265
+
266
+ ```typescript
267
+ {
268
+ level: 'info', // 'info' | 'warn' | 'error'
269
+ message: 'Đã mở...', // Mô tả bằng tiếng Việt
270
+ action: 'MANAGE_ALL_FILES', // Tên action
271
+ success: true,
272
+ androidVersion: 34, // Android SDK
273
+ androidRelease: '14', // Version string
274
+ timestamp: 1700000000000 // Unix ms
275
+ }
276
+ ```
277
+
278
+ | Level | Ý nghĩa |
279
+ |-------|---------|
280
+ | `info` | Thành công hoặc không cần thiết (Android version cũ) |
281
+ | `warn` | Fallback sang phương án dự phòng |
282
+ | `error` | Không thể thực hiện |
283
+
284
+ ---
285
+
286
+ ## 🔄 Ví dụ đầy đủ — Setup Permission khi khởi động
287
+
288
+ ```typescript
289
+ import { TxaSettingsPermission } from 'txa-settings-permission';
290
+ import { App } from '@capacitor/app';
291
+ import { Capacitor } from '@capacitor/core';
292
+
293
+ export async function setupPermissions() {
294
+ if (!Capacitor.isNativePlatform()) return;
295
+
296
+ const info = await TxaSettingsPermission.getDeviceInfo();
297
+ console.log(`Android ${info.androidRelease} (SDK ${info.androidSdk})`);
298
+
299
+ const { checks } = await TxaSettingsPermission.checkAllRequired();
300
+
301
+ if (!checks.manageAllFiles && info.supportsManageAllFiles) {
302
+ await TxaSettingsPermission.openManageAllFiles();
303
+ await waitForResume();
304
+ }
305
+
306
+ if (!checks.overlay && info.supportsOverlay) {
307
+ await TxaSettingsPermission.openOverlaySettings();
308
+ await waitForResume();
309
+ }
310
+
311
+ if (!checks.batteryOptimization && info.supportsBatteryOptimization) {
312
+ await TxaSettingsPermission.openBatteryOptimization();
313
+ await waitForResume();
314
+ }
315
+ }
316
+
317
+ function waitForResume(): Promise<void> {
318
+ return new Promise(resolve => {
319
+ const h = App.addListener('appStateChange', async ({ isActive }) => {
320
+ if (isActive) { (await h).remove(); resolve(); }
321
+ });
322
+ });
323
+ }
324
+ ```
325
+
326
+ ---
327
+
328
+ ## 🗺️ Android Version Compatibility
329
+
330
+ | Method | API 22-22 (5.1) | API 23 (6) | API 26 (8) | API 30 (11) | API 33 (13) |
331
+ |--------|:-:|:-:|:-:|:-:|:-:|
332
+ | `openManageAllFiles` | ✅ auto | ✅ auto | ✅ auto | ✅ Settings | ✅ Settings |
333
+ | `openOverlaySettings` | ✅ auto | ✅ Settings | ✅ Settings | ✅ Settings | ✅ Settings |
334
+ | `openInstallUnknownApps` | ✅ auto | ⚠️ Security | ⚠️ Security | ✅ Per-app | ✅ Per-app |
335
+ | `openBatteryOptimization` | ✅ auto | ✅ Settings | ✅ Settings | ✅ Settings | ✅ Settings |
336
+ | `openNotificationSettings` | ⚠️ App Details | ⚠️ App Details | ✅ Settings | ✅ Settings | ✅ Settings |
337
+ | `openUsageStatsSettings` | ✅ Settings | ✅ Settings | ✅ Settings | ✅ Settings | ✅ Settings |
338
+
339
+ > ✅ auto = không cần, tự trả `granted: true` · ⚠️ = fallback/log warn
340
+
341
+ ---
342
+
343
+ ## 🏗️ Build & Publish
344
+
345
+ ```bash
346
+ # Cài dev dependencies
347
+ npm install
348
+
349
+ # Build
350
+ npm run build
351
+
352
+ # Publish lên NPM
353
+ npm login
354
+ npm publish --access public
355
+
356
+ # Update version
357
+ npm version patch && npm publish --access public
358
+ ```
359
+
360
+ ---
361
+
362
+ ## 📂 Cấu trúc
363
+
364
+ ```
365
+ txa-settings-permission/
366
+ ├── android/
367
+ │ ├── build.gradle
368
+ │ └── src/main/java/online/txa/settingspermission/
369
+ │ └── TxaSettingsPermissionPlugin.java
370
+ ├── src/
371
+ │ ├── index.ts ← Entry point
372
+ │ ├── definitions.ts ← TypeScript types
373
+ │ └── web.ts ← Web mock
374
+ ├── scripts/
375
+ │ └── check-capacitor.js
376
+ ├── package.json
377
+ ├── tsconfig.json
378
+ └── rollup.config.js
379
+ ```
380
+
381
+ ---
382
+
383
+ ## 📄 License
384
+
385
+ MIT © [TXA](mailto:txa@nrotxa.online)
386
+
387
+ ---
388
+
389
+ <div align="center">
390
+ Made with ❤️ by <strong>TXA</strong> · <a href="https://github.com/TXAVLOG">github.com/TXAVLOG</a>
391
+ </div>
@@ -0,0 +1,57 @@
1
+ ext {
2
+ junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
3
+ androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.5'
4
+ androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.5.1'
5
+ }
6
+
7
+ buildscript {
8
+ repositories {
9
+ google()
10
+ mavenCentral()
11
+ }
12
+ dependencies {
13
+ classpath 'com.android.tools.build:gradle:8.2.1'
14
+ }
15
+ }
16
+
17
+ apply plugin: 'com.android.library'
18
+
19
+ android {
20
+ namespace "online.txa.settingspermission"
21
+ compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 34
22
+
23
+ defaultConfig {
24
+ minSdk project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 22
25
+ targetSdk project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 34
26
+ }
27
+
28
+ buildTypes {
29
+ release {
30
+ minifyEnabled false
31
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
32
+ }
33
+ }
34
+
35
+ lintOptions {
36
+ abortOnError false
37
+ }
38
+
39
+ compileOptions {
40
+ sourceCompatibility JavaVersion.VERSION_17
41
+ targetCompatibility JavaVersion.VERSION_17
42
+ }
43
+ }
44
+
45
+ repositories {
46
+ google()
47
+ mavenCentral()
48
+ }
49
+
50
+ dependencies {
51
+ implementation fileTree(dir: 'libs', include: ['*.jar'])
52
+ implementation project(':capacitor-android')
53
+
54
+ testImplementation "junit:junit:$junitVersion"
55
+ androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
56
+ androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
57
+ }
@@ -0,0 +1,42 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
+ <!--
4
+ Plugin: txa-settings-permission
5
+ Author: TXA (txa@nrotxa.online)
6
+
7
+ Các permission dưới đây cần được thêm vào AndroidManifest.xml của APP (không phải plugin).
8
+ Chỉ thêm những permission mà app của bạn thực sự cần dùng.
9
+
10
+ === THÊM VÀO android/app/src/main/AndroidManifest.xml ===
11
+
12
+ All Files Access (Android 11+):
13
+ <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
14
+
15
+ Overlay / Draw on top:
16
+ <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
17
+
18
+ Install Unknown Apps (Android 8+): không cần permission riêng, chỉ cần mở Settings
19
+
20
+ Storage (Android < 13):
21
+ <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
22
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29" />
23
+
24
+ Media (Android 13+):
25
+ <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
26
+ <uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
27
+ <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
28
+
29
+ Notifications (Android 13+):
30
+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
31
+
32
+ Camera:
33
+ <uses-permission android:name="android.permission.CAMERA" />
34
+
35
+ Location:
36
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
37
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
38
+
39
+ Request ignore battery optimizations:
40
+ <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
41
+ -->
42
+ </manifest>