@norcy/react-native-toolkit 0.1.139 → 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.
- package/README.md +12 -0
- package/android/build.gradle +4 -0
- package/android/src/main/AndroidManifest.xml +1 -1
- package/android/src/main/java/com/norcy/reactnativetoolkit/NCYAPI.java +205 -0
- package/android/src/main/java/com/norcy/reactnativetoolkit/NCYReport.java +48 -0
- package/android/src/main/java/com/{norcyreactnativetoolkit → norcy/reactnativetoolkit}/ReactNativeToolkitModule.kt +2 -1
- package/android/src/main/java/com/{norcyreactnativetoolkit → norcy/reactnativetoolkit}/ReactNativeToolkitPackage.kt +7 -2
- package/package.json +1 -1
package/README.md
CHANGED
@@ -8,6 +8,18 @@ App 工厂核心组件
|
|
8
8
|
yarn add @norcy/react-native-toolkit
|
9
9
|
```
|
10
10
|
|
11
|
+
## Android 引入
|
12
|
+
|
13
|
+
App 端需要在 `android/app/build.gradle` 中添加:
|
14
|
+
|
15
|
+
```gradle
|
16
|
+
dependencies {
|
17
|
+
implementation 'com.umeng.umsdk:common:9.6.3'
|
18
|
+
implementation 'com.umeng.umsdk:asms:1.8.0'
|
19
|
+
}
|
20
|
+
```
|
21
|
+
|
22
|
+
|
11
23
|
## 维护
|
12
24
|
【重要】TS 校验和 ESLint 校验
|
13
25
|
|
package/android/build.gradle
CHANGED
@@ -127,4 +127,8 @@ dependencies {
|
|
127
127
|
// noinspection GradleDynamicVersion
|
128
128
|
api 'com.facebook.react:react-native:+'
|
129
129
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
130
|
+
|
131
|
+
// 友盟统计
|
132
|
+
compileOnly 'com.umeng.umsdk:common:9.6.3'
|
133
|
+
compileOnly 'com.umeng.umsdk:asms:1.8.0'
|
130
134
|
}
|
@@ -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
|
+
}
|
@@ -0,0 +1,48 @@
|
|
1
|
+
package com.norcy.reactnativetoolkit;
|
2
|
+
|
3
|
+
import com.facebook.react.bridge.Arguments;
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
5
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
6
|
+
import com.facebook.react.bridge.ReactMethod;
|
7
|
+
import com.facebook.react.bridge.ReadableMap;
|
8
|
+
import com.facebook.react.bridge.WritableMap;
|
9
|
+
import com.umeng.analytics.MobclickAgent;
|
10
|
+
|
11
|
+
public class NCYReport extends ReactContextBaseJavaModule {
|
12
|
+
public NCYReport(ReactApplicationContext reactContext) {
|
13
|
+
super(reactContext);
|
14
|
+
}
|
15
|
+
|
16
|
+
@Override
|
17
|
+
public String getName() {
|
18
|
+
return "NCYReport";
|
19
|
+
}
|
20
|
+
|
21
|
+
@ReactMethod
|
22
|
+
public void report(String key, ReadableMap params) {
|
23
|
+
WritableMap attributes = Arguments.createMap();
|
24
|
+
attributes.merge(params);
|
25
|
+
MobclickAgent.onEventObject(getReactApplicationContext(), key, attributes.toHashMap());
|
26
|
+
}
|
27
|
+
|
28
|
+
@ReactMethod
|
29
|
+
public void signIn(String userId) {
|
30
|
+
MobclickAgent.onProfileSignIn(userId);
|
31
|
+
}
|
32
|
+
|
33
|
+
@ReactMethod
|
34
|
+
public void signOut() {
|
35
|
+
MobclickAgent.onProfileSignOff();
|
36
|
+
}
|
37
|
+
|
38
|
+
@ReactMethod
|
39
|
+
public void enterPage(String pageName) {
|
40
|
+
MobclickAgent.onPageStart(pageName);
|
41
|
+
}
|
42
|
+
|
43
|
+
@ReactMethod
|
44
|
+
public void leavePage(String pageName) {
|
45
|
+
MobclickAgent.onPageEnd(pageName);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
@@ -1,4 +1,4 @@
|
|
1
|
-
package com.
|
1
|
+
package com.norcy.reactnativetoolkit
|
2
2
|
|
3
3
|
import com.facebook.react.bridge.ReactApplicationContext
|
4
4
|
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
@@ -22,3 +22,4 @@ class ReactNativeToolkitModule(reactContext: ReactApplicationContext) : ReactCon
|
|
22
22
|
|
23
23
|
|
24
24
|
}
|
25
|
+
|
@@ -1,4 +1,4 @@
|
|
1
|
-
package com.
|
1
|
+
package com.norcy.reactnativetoolkit
|
2
2
|
|
3
3
|
import java.util.Arrays
|
4
4
|
import java.util.Collections
|
@@ -11,10 +11,15 @@ import com.facebook.react.bridge.JavaScriptModule
|
|
11
11
|
|
12
12
|
class ReactNativeToolkitPackage : ReactPackage {
|
13
13
|
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
|
14
|
-
return Arrays.asList<NativeModule>(
|
14
|
+
return Arrays.asList<NativeModule>(
|
15
|
+
ReactNativeToolkitModule(reactContext),
|
16
|
+
NCYReport(reactContext),
|
17
|
+
NCYAPI(reactContext)
|
18
|
+
)
|
15
19
|
}
|
16
20
|
|
17
21
|
override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
|
18
22
|
return emptyList<ViewManager<*, *>>()
|
19
23
|
}
|
20
24
|
}
|
25
|
+
|