@norcy/react-native-toolkit 0.1.140 → 0.1.141

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.
@@ -0,0 +1,205 @@
1
+ package com.norcy.reactnativetoolkit;
2
+
3
+ import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
4
+ import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
5
+
6
+ import android.app.Activity;
7
+ import android.content.Context;
8
+ import android.content.Intent;
9
+ import android.content.SharedPreferences;
10
+ import android.content.pm.PackageManager;
11
+ import android.net.Uri;
12
+ import android.os.Build;
13
+ import android.os.Environment;
14
+ import android.provider.Settings;
15
+ import android.webkit.MimeTypeMap;
16
+ import androidx.core.content.FileProvider;
17
+
18
+ import com.facebook.react.bridge.ActivityEventListener;
19
+ import com.facebook.react.bridge.Promise;
20
+ import com.facebook.react.bridge.ReactApplicationContext;
21
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
22
+ import com.facebook.react.bridge.ReactMethod;
23
+ import com.facebook.react.bridge.ReadableMap;
24
+ import com.umeng.commonsdk.UMConfigure;
25
+
26
+ import androidx.core.app.ActivityCompat;
27
+ import androidx.core.content.ContextCompat;
28
+
29
+ import java.io.File;
30
+
31
+ public class NCYAPI extends ReactContextBaseJavaModule implements ActivityEventListener {
32
+ private static final String PREFS_NAME = "NCYAPIPrefs"; // 偏好设置文件名
33
+ private static final int REQUEST_CODE = 228228;
34
+ public static final String NAME = "ManageExternalStorage";
35
+ Promise promise;
36
+
37
+ public NCYAPI(ReactApplicationContext reactContext) {
38
+ super(reactContext);
39
+ reactContext.addActivityEventListener(this);
40
+ }
41
+
42
+ @Override
43
+ public String getName() {
44
+ return "NCYAPI";
45
+ }
46
+
47
+ @ReactMethod
48
+ public void onAgree() {
49
+ SharedPreferences.Editor editor = getSharedPreferences().edit();
50
+ editor.putBoolean("umeng_agreed", true);
51
+ editor.apply();
52
+ UMConfigure.init(getReactApplicationContext(), "638196f005844627b58c1db6", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
53
+ }
54
+
55
+ @ReactMethod(isBlockingSynchronousMethod = true)
56
+ public void setData(String key, String value) {
57
+ SharedPreferences.Editor editor = getSharedPreferences().edit();
58
+ editor.putString(key, value);
59
+ editor.apply();
60
+ }
61
+
62
+ @ReactMethod(isBlockingSynchronousMethod = true)
63
+ public String getData(String key) {
64
+ SharedPreferences preferences = getSharedPreferences();
65
+ return preferences.getString(key, "");
66
+ }
67
+
68
+ private SharedPreferences getSharedPreferences() {
69
+ Context context = getReactApplicationContext();
70
+ return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
71
+ }
72
+
73
+ @ReactMethod
74
+ public void checkManagePermission(Promise promise) {
75
+ if (Build.VERSION.SDK_INT >= 30) {
76
+ promise.resolve(Environment.isExternalStorageManager());
77
+ } else {
78
+ int result = ContextCompat.checkSelfPermission(getReactApplicationContext(), READ_EXTERNAL_STORAGE);
79
+ int result1 = ContextCompat.checkSelfPermission(getReactApplicationContext(), WRITE_EXTERNAL_STORAGE);
80
+ promise.resolve(result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED);
81
+ }
82
+ }
83
+
84
+
85
+ @ReactMethod
86
+ public void requestManagePermission(Promise promise) {
87
+ this.promise = promise;
88
+ if (Build.VERSION.SDK_INT >= 30) {
89
+ try {
90
+ Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
91
+ intent.addCategory("android.intent.category.DEFAULT");
92
+ intent.setData(Uri.parse(String.format("package:%s",getReactApplicationContext().getPackageName())));
93
+ getReactApplicationContext().startActivityForResult(intent, REQUEST_CODE, null);
94
+ } catch (Exception e) {
95
+ Intent intent = new Intent();
96
+ intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
97
+ getReactApplicationContext().startActivityForResult(intent, REQUEST_CODE, null);
98
+ }
99
+ } else {
100
+ if (ActivityCompat.shouldShowRequestPermissionRationale(getCurrentActivity(), WRITE_EXTERNAL_STORAGE)) {
101
+ ActivityCompat.requestPermissions(getCurrentActivity(), new String[]{WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
102
+ } else {
103
+ int result = ContextCompat.checkSelfPermission(getReactApplicationContext(), READ_EXTERNAL_STORAGE);
104
+ int result1 = ContextCompat.checkSelfPermission(getReactApplicationContext(), WRITE_EXTERNAL_STORAGE);
105
+ this.promise.resolve(result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED);
106
+ }
107
+ }
108
+ }
109
+
110
+ /**
111
+ * 分享指定的文件
112
+ * @param filePath 文件路径
113
+ * @param options 可选参数,如标题、MIME类型等
114
+ * @param promise Promise对象返回结果
115
+ */
116
+ @ReactMethod
117
+ public void shareFile(String filePath, ReadableMap options, Promise promise) {
118
+ try {
119
+ // 检查文件是否存在
120
+ File file = new File(filePath);
121
+ if (!file.exists()) {
122
+ promise.reject("FILE_NOT_FOUND", "文件不存在: " + filePath);
123
+ return;
124
+ }
125
+
126
+ // 获取当前活动的Activity
127
+ Activity currentActivity = getCurrentActivity();
128
+ if (currentActivity == null) {
129
+ promise.reject("ACTIVITY_NOT_FOUND", "无法获取当前Activity");
130
+ return;
131
+ }
132
+
133
+ // 创建分享 Intent
134
+ Intent intent = new Intent(Intent.ACTION_SEND);
135
+ Uri fileUri;
136
+
137
+ // 适配 Android 7.0 及以上版本
138
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
139
+ fileUri = FileProvider.getUriForFile(
140
+ getReactApplicationContext(),
141
+ getReactApplicationContext().getPackageName() + ".provider",
142
+ file
143
+ );
144
+ intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
145
+ } else {
146
+ fileUri = Uri.fromFile(file);
147
+ }
148
+
149
+ // 根据文件扩展名确定MIME类型
150
+ String mimeType = null;
151
+ if (options != null && options.hasKey("mimeType")) {
152
+ mimeType = options.getString("mimeType");
153
+ }
154
+
155
+ if (mimeType == null) {
156
+ String extension = MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());
157
+ if (extension != null) {
158
+ mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
159
+ }
160
+ }
161
+
162
+ // 如果无法确定MIME类型,使用通用类型
163
+ if (mimeType == null) {
164
+ mimeType = "*/*";
165
+ }
166
+
167
+ intent.setType(mimeType);
168
+ intent.putExtra(Intent.EXTRA_STREAM, fileUri);
169
+
170
+ // 设置标题(如果提供)
171
+ String title = "分享文件";
172
+ if (options != null && options.hasKey("title")) {
173
+ title = options.getString("title");
174
+ }
175
+
176
+ // 通过当前Activity启动
177
+ currentActivity.startActivity(Intent.createChooser(intent, title));
178
+
179
+ // 返回成功结果
180
+ promise.resolve(true);
181
+ } catch (Exception e) {
182
+ promise.reject("SHARE_ERROR", e.getMessage(), e);
183
+ }
184
+ }
185
+
186
+ @Override
187
+ public void onNewIntent(Intent intent) {
188
+ }
189
+
190
+
191
+ @Override
192
+ public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent intent) {
193
+ if (REQUEST_CODE != requestCode) {
194
+ return;
195
+ }
196
+
197
+ if (Build.VERSION.SDK_INT >= 30) {
198
+ this.promise.resolve(Environment.isExternalStorageManager());
199
+ } else {
200
+ int result = ContextCompat.checkSelfPermission(getReactApplicationContext(), READ_EXTERNAL_STORAGE);
201
+ int result1 = ContextCompat.checkSelfPermission(getReactApplicationContext(), WRITE_EXTERNAL_STORAGE);
202
+ this.promise.resolve(result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED);
203
+ }
204
+ }
205
+ }
@@ -13,7 +13,8 @@ class ReactNativeToolkitPackage : ReactPackage {
13
13
  override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
14
14
  return Arrays.asList<NativeModule>(
15
15
  ReactNativeToolkitModule(reactContext),
16
- NCYReport(reactContext)
16
+ NCYReport(reactContext),
17
+ NCYAPI(reactContext)
17
18
  )
18
19
  }
19
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@norcy/react-native-toolkit",
3
- "version": "0.1.140",
3
+ "version": "0.1.141",
4
4
  "description": "My Toolkit",
5
5
  "main": "lib/commonjs/index",
6
6
  "module": "lib/module/index",