capacitor-camera-module 0.0.2 → 0.0.3

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.
@@ -55,4 +55,7 @@ dependencies {
55
55
  testImplementation "junit:junit:$junitVersion"
56
56
  androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
57
57
  androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
58
+
59
+ implementation 'androidx.activity:activity:1.7.0'
60
+ implementation 'androidx.fragment:fragment:1.5.7'
58
61
  }
@@ -1,2 +1,12 @@
1
1
  <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ package="com.eddieagvictoria.cameramodule">
3
+
4
+ <!-- Permiso de cámara -->
5
+ <uses-permission android:name="android.permission.CAMERA" />
6
+
7
+ <!-- Características opcionales -->
8
+ <uses-feature android:name="android.hardware.camera" android:required="false" />
9
+ <uses-feature android:name="android.hardware.camera.front" android:required="false" />
10
+ <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
11
+
2
12
  </manifest>
@@ -1,22 +1,151 @@
1
1
  package com.eddieagvictoria.cameramodule;
2
2
 
3
+ import android.Manifest;
4
+ import android.content.pm.PackageManager;
5
+ import android.os.Build;
6
+
7
+ import androidx.activity.result.ActivityResultCallback;
8
+ import androidx.activity.result.ActivityResultLauncher;
9
+ import androidx.activity.result.contract.ActivityResultContracts;
10
+ import androidx.appcompat.app.AppCompatActivity;
11
+ import androidx.core.content.ContextCompat;
12
+
3
13
  import com.getcapacitor.JSObject;
4
14
  import com.getcapacitor.Plugin;
5
15
  import com.getcapacitor.PluginCall;
6
16
  import com.getcapacitor.PluginMethod;
7
17
  import com.getcapacitor.annotation.CapacitorPlugin;
18
+ import com.getcapacitor.annotation.Permission;
8
19
 
9
- @CapacitorPlugin(name = "CameraModule")
20
+ @CapacitorPlugin(
21
+ name = "CameraModule",
22
+ permissions = {
23
+ @Permission(
24
+ alias = "camera",
25
+ strings = { Manifest.permission.CAMERA }
26
+ )
27
+ }
28
+ )
10
29
  public class CameraModulePlugin extends Plugin {
11
30
 
12
- private CameraModule implementation = new CameraModule();
31
+ private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;
32
+ private PluginCall pendingPermissionCall = null;
33
+
34
+ @Override
35
+ public void load() {
36
+ super.load();
37
+ }
13
38
 
14
39
  @PluginMethod
15
40
  public void echo(PluginCall call) {
16
- String value = call.getString("value");
41
+ String value = call.getString("value", "");
42
+ JSObject ret = new JSObject();
43
+ ret.put("value", value);
44
+ call.resolve(ret);
45
+ }
17
46
 
47
+ @PluginMethod
48
+ public void checkPermission(PluginCall call) {
18
49
  JSObject ret = new JSObject();
19
- ret.put("value", implementation.echo(value));
50
+
51
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
52
+ int permissionStatus = ContextCompat.checkSelfPermission(
53
+ getContext(),
54
+ CAMERA_PERMISSION
55
+ );
56
+
57
+ boolean granted = permissionStatus == PackageManager.PERMISSION_GRANTED;
58
+
59
+ ret.put("granted", granted);
60
+
61
+ if (granted) {
62
+ ret.put("status", "granted");
63
+ } else {
64
+ if (getActivity() instanceof AppCompatActivity) {
65
+ boolean shouldShowRationale = ((AppCompatActivity) getActivity())
66
+ .shouldShowRequestPermissionRationale(CAMERA_PERMISSION);
67
+
68
+ if (shouldShowRationale) {
69
+ ret.put("status", "prompt-with-rationale");
70
+ } else {
71
+ ret.put("status", "prompt");
72
+ }
73
+ } else {
74
+ ret.put("status", "denied");
75
+ }
76
+ }
77
+
78
+ ret.put("details", "Android SDK " + Build.VERSION.SDK_INT);
79
+
80
+ } else {
81
+ // Para versiones anteriores a Android 6.0
82
+ ret.put("granted", true);
83
+ ret.put("status", "granted");
84
+ ret.put("details", "Pre-Marshmallow, granted at install");
85
+ }
86
+
20
87
  call.resolve(ret);
21
88
  }
22
- }
89
+
90
+ @PluginMethod
91
+ public void requestPermission(PluginCall call) {
92
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
93
+ pendingPermissionCall = call;
94
+
95
+ // Registrar el callback para el permiso
96
+ ActivityResultLauncher<String> requestPermissionLauncher =
97
+ ((AppCompatActivity) getActivity())
98
+ .registerForActivityResult(
99
+ new ActivityResultContracts.RequestPermission(),
100
+ isGranted -> {
101
+ if (pendingPermissionCall != null) {
102
+ JSObject result = new JSObject();
103
+ result.put("granted", isGranted);
104
+ result.put("status", isGranted ? "granted" : "denied");
105
+ result.put("details", "Permission request completed");
106
+
107
+ pendingPermissionCall.resolve(result);
108
+ pendingPermissionCall = null;
109
+ }
110
+ }
111
+ );
112
+
113
+ // Solicitar el permiso
114
+ requestPermissionLauncher.launch(CAMERA_PERMISSION);
115
+
116
+ } else {
117
+ // Para versiones anteriores
118
+ JSObject ret = new JSObject();
119
+ ret.put("granted", true);
120
+ ret.put("status", "granted");
121
+ ret.put("details", "Pre-Marshmallow, auto-granted");
122
+ call.resolve(ret);
123
+ }
124
+ }
125
+
126
+ @PluginMethod
127
+ public void checkAndRequestPermission(PluginCall call) {
128
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
129
+ int permissionStatus = ContextCompat.checkSelfPermission(
130
+ getContext(),
131
+ CAMERA_PERMISSION
132
+ );
133
+
134
+ if (permissionStatus == PackageManager.PERMISSION_GRANTED) {
135
+ JSObject ret = new JSObject();
136
+ ret.put("granted", true);
137
+ ret.put("status", "granted");
138
+ ret.put("details", "Already granted");
139
+ call.resolve(ret);
140
+ } else {
141
+ requestPermission(call);
142
+ }
143
+ } else {
144
+ JSObject ret = new JSObject();
145
+ ret.put("granted", true);
146
+ ret.put("status", "granted");
147
+ ret.put("details", "Pre-Marshmallow, auto-granted");
148
+ call.resolve(ret);
149
+ }
150
+ }
151
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-camera-module",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "Plugin to request permissiones view camera take phots",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -1,11 +0,0 @@
1
- package com.eddieagvictoria.cameramodule;
2
-
3
- import com.getcapacitor.Logger;
4
-
5
- public class CameraModule {
6
-
7
- public String echo(String value) {
8
- Logger.info("Echo", value);
9
- return value;
10
- }
11
- }