Skip to content

Commit 84faf16

Browse files
committed
配置为本地开发用,同时修复资源回收的bug
1 parent dbd71b8 commit 84faf16

File tree

10 files changed

+232
-60
lines changed

10 files changed

+232
-60
lines changed
1 Byte
Binary file not shown.

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 0 additions & 7 deletions
This file was deleted.

app/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ android {
8181
buildConfigField "String", "CHANNEL", '"coolapk"'
8282
}
8383
}
84-
84+
// 避免压缩TensorFlow
85+
aaptOptions {
86+
noCompress "tflite"
87+
}
8588
}
8689

8790

@@ -95,6 +98,9 @@ repositories {
9598
}
9699

97100
dependencies {
101+
102+
// TensorFlow-lite
103+
implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
98104
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.1-alpha01', {
99105
exclude group: 'com.android.support', module: 'support-annotations'
100106
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.autojs.autojs.tensorflow;
2+
3+
public class Demo {
4+
public String getDemoResult() {
5+
return "Hello Demo.";
6+
}
7+
}

common/release/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":455,"versionName":"4.1.0 Alpha5","enabled":true,"outputFile":"commonRelease-4.1.0 Alpha5.apk","fullName":"commonRelease","baseName":"common-release"},"path":"commonRelease-4.1.0 Alpha5.apk","properties":{}}]
1+
[{"outputType":{"type":"APK"},"apkInfo":{"type":"FULL_SPLIT","splits":[{"filterType":"ABI","value":"armeabi-v7a"}],"versionCode":461,"versionName":"4.1.1 Alpha2","enabled":true,"filterName":"armeabi-v7a","outputFile":"app-common-armeabi-v7a-release.apk","fullName":"commonArmeabi-v7aRelease","baseName":"common-armeabi-v7a-release"},"path":"app-common-armeabi-v7a-release.apk","properties":{}},{"outputType":{"type":"APK"},"apkInfo":{"type":"FULL_SPLIT","splits":[{"filterType":"ABI","value":"x86"}],"versionCode":461,"versionName":"4.1.1 Alpha2","enabled":true,"filterName":"x86","outputFile":"app-common-x86-release.apk","fullName":"commonX86Release","baseName":"common-x86-release"},"path":"app-common-x86-release.apk","properties":{}}]

common/src/main/java/com/stardust/util/ResourceMonitor.java

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,45 @@
88
import com.stardust.BuildConfig;
99

1010
import java.util.concurrent.ConcurrentHashMap;
11+
import java.util.concurrent.locks.ReentrantLock;
1112

1213

1314
public final class ResourceMonitor {
1415

1516
private static final String LOG_TAG = "ResourceMonitor";
1617

17-
private static final ConcurrentHashMap<Class<?>, SparseArray<Exception>> mResources = new ConcurrentHashMap<>();
18+
private static class LockedResource {
19+
private ReentrantLock lock;
20+
private SparseArray<Exception> resource;
21+
22+
public LockedResource() {
23+
this.resource = new SparseArray<>();
24+
this.lock = new ReentrantLock();
25+
}
26+
27+
public LockedResource(SparseArray<Exception> resource) {
28+
this.resource = resource;
29+
this.lock = new ReentrantLock();
30+
}
31+
32+
public ReentrantLock getLock() {
33+
return lock;
34+
}
35+
36+
public void setLock(ReentrantLock lock) {
37+
this.lock = lock;
38+
}
39+
40+
public SparseArray<Exception> getResource() {
41+
return resource;
42+
}
43+
44+
public void setResource(SparseArray<Exception> resource) {
45+
this.resource = resource;
46+
}
47+
}
48+
49+
private static final ConcurrentHashMap<Class<?>, LockedResource> mResources = new ConcurrentHashMap<>();
1850
private static Handler sHandler;
1951
private static boolean sEnabled = BuildConfig.DEBUG;
2052
private static ExceptionCreator sExceptionCreator;
@@ -32,9 +64,9 @@ public static void onOpen(ResourceMonitor.Resource resource) {
3264
if (!sEnabled) {
3365
return;
3466
}
35-
SparseArray<Exception> map = mResources.get(resource.getClass());
67+
LockedResource map = mResources.get(resource.getClass());
3668
if (map == null) {
37-
map = new SparseArray<>();
69+
map = new LockedResource();
3870
mResources.put(resource.getClass(), map);
3971
}
4072
int resourceId = resource.getResourceId();
@@ -45,29 +77,35 @@ public static void onOpen(ResourceMonitor.Resource resource) {
4577
} else {
4678
exception = sExceptionCreator.create(resource);
4779
}
48-
map.put(resourceId, exception);
80+
map.getLock().lock();
81+
map.getResource().put(resourceId, exception);
82+
map.getLock().unlock();
4983
}
5084

5185
public static void onClose(ResourceMonitor.Resource resource) {
5286
if (!sEnabled) {
5387
return;
5488
}
55-
SparseArray map = mResources.get(resource.getClass());
89+
LockedResource map = mResources.get(resource.getClass());
5690
if (map != null) {
57-
map.remove(resource.getResourceId());
91+
map.getLock().lock();
92+
map.getResource().remove(resource.getResourceId());
93+
map.getLock().unlock();
5894
}
5995
}
6096

6197
public static void onFinalize(ResourceMonitor.Resource resource) {
6298
if (!sEnabled) {
6399
return;
64100
}
65-
SparseArray<Exception> map = mResources.get(resource.getClass());
101+
LockedResource map = mResources.get(resource.getClass());
66102
if (map != null) {
67-
int indexOfKey = map.indexOfKey(resource.getResourceId());
103+
map.getLock().lock();
104+
int indexOfKey = map.getResource().indexOfKey(resource.getResourceId());
68105
if (indexOfKey >= 0) {
69-
final Exception exception = map.valueAt(indexOfKey);
70-
map.removeAt(indexOfKey);
106+
final Exception exception = map.getResource().valueAt(indexOfKey);
107+
map.getResource().removeAt(indexOfKey);
108+
map.getLock().unlock();
71109
if (sHandler == null) {
72110
sHandler = new Handler(Looper.getMainLooper());
73111
}
@@ -84,6 +122,7 @@ public final void run() {
84122
}
85123
});
86124
}
125+
87126
}
88127
}
89128

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.stardust.util;
2+
3+
import android.os.Handler;
4+
import android.os.Looper;
5+
import android.util.Log;
6+
import android.util.SparseArray;
7+
8+
import com.stardust.BuildConfig;
9+
10+
import java.util.concurrent.ConcurrentHashMap;
11+
import java.util.concurrent.locks.ReentrantLock;
12+
13+
14+
public final class ResourceMonitorFixed {
15+
16+
private static final String LOG_TAG = "ResourceMonitor";
17+
18+
private static class LockedResource {
19+
private ReentrantLock lock;
20+
private SparseArray<Exception> resource;
21+
22+
public LockedResource() {
23+
this.resource = new SparseArray<>();
24+
this.lock = new ReentrantLock();
25+
}
26+
27+
public LockedResource(SparseArray<Exception> resource) {
28+
this.resource = resource;
29+
this.lock = new ReentrantLock();
30+
}
31+
32+
public ReentrantLock getLock() {
33+
return lock;
34+
}
35+
36+
public void setLock(ReentrantLock lock) {
37+
this.lock = lock;
38+
}
39+
40+
public SparseArray<Exception> getResource() {
41+
return resource;
42+
}
43+
44+
public void setResource(SparseArray<Exception> resource) {
45+
this.resource = resource;
46+
}
47+
}
48+
49+
private static final ConcurrentHashMap<Class<?>, LockedResource> mResources = new ConcurrentHashMap<>();
50+
private static Handler sHandler;
51+
private static boolean sEnabled = BuildConfig.DEBUG;
52+
private static ExceptionCreator sExceptionCreator;
53+
private static UnclosedResourceDetectedHandler sUnclosedResourceDetectedHandler;
54+
55+
public static void setExceptionCreator(ExceptionCreator exceptionCreator) {
56+
sExceptionCreator = exceptionCreator;
57+
}
58+
59+
public static void setUnclosedResourceDetectedHandler(UnclosedResourceDetectedHandler unclosedResourceDetectedHandler) {
60+
sUnclosedResourceDetectedHandler = unclosedResourceDetectedHandler;
61+
}
62+
63+
public static void onOpen(ResourceMonitorFixed.Resource resource) {
64+
if (!sEnabled) {
65+
return;
66+
}
67+
LockedResource map = mResources.get(resource.getClass());
68+
if (map == null) {
69+
map = new LockedResource();
70+
mResources.put(resource.getClass(), map);
71+
}
72+
int resourceId = resource.getResourceId();
73+
Exception exception;
74+
if (sExceptionCreator == null) {
75+
exception = new ResourceMonitorFixed.UnclosedResourceException(resource);
76+
exception.fillInStackTrace();
77+
} else {
78+
exception = sExceptionCreator.create(resource);
79+
}
80+
map.getLock().lock();
81+
map.getResource().put(resourceId, exception);
82+
map.getLock().unlock();
83+
}
84+
85+
public static void onClose(ResourceMonitorFixed.Resource resource) {
86+
if (!sEnabled) {
87+
return;
88+
}
89+
LockedResource map = mResources.get(resource.getClass());
90+
if (map != null) {
91+
map.getLock().lock();
92+
map.getResource().remove(resource.getResourceId());
93+
map.getLock().unlock();
94+
}
95+
}
96+
97+
public static void onFinalize(ResourceMonitorFixed.Resource resource) {
98+
if (!sEnabled) {
99+
return;
100+
}
101+
LockedResource map = mResources.get(resource.getClass());
102+
if (map != null) {
103+
map.getLock().lock();
104+
int indexOfKey = map.getResource().indexOfKey(resource.getResourceId());
105+
if (indexOfKey >= 0) {
106+
final Exception exception = map.getResource().valueAt(indexOfKey);
107+
map.getResource().removeAt(indexOfKey);
108+
map.getLock().unlock();
109+
if (sHandler == null) {
110+
sHandler = new Handler(Looper.getMainLooper());
111+
}
112+
sHandler.post(new Runnable() {
113+
public final void run() {
114+
UnclosedResourceDetectedException detectedException = new UnclosedResourceDetectedException(exception);
115+
detectedException.fillInStackTrace();
116+
Log.w(LOG_TAG, "UnclosedResourceDetected", detectedException);
117+
if (sUnclosedResourceDetectedHandler != null) {
118+
sUnclosedResourceDetectedHandler.onUnclosedResourceDetected(detectedException);
119+
} else {
120+
throw detectedException;
121+
}
122+
}
123+
});
124+
}
125+
126+
}
127+
}
128+
129+
public static boolean isEnabled() {
130+
return sEnabled;
131+
}
132+
133+
public static void setEnabled(boolean mEnabled) {
134+
ResourceMonitorFixed.sEnabled = mEnabled;
135+
}
136+
137+
public static final class UnclosedResourceException extends RuntimeException {
138+
public UnclosedResourceException(Resource resource) {
139+
super("id = " + resource.getResourceId() + ", resource = " + resource);
140+
}
141+
142+
}
143+
144+
145+
public static final class UnclosedResourceDetectedException extends RuntimeException {
146+
public UnclosedResourceDetectedException(Throwable cause) {
147+
super(cause);
148+
}
149+
}
150+
151+
public interface Resource {
152+
int getResourceId();
153+
}
154+
155+
public interface ExceptionCreator {
156+
Exception create(Resource resource);
157+
}
158+
159+
public interface UnclosedResourceDetectedHandler {
160+
161+
void onUnclosedResourceDetected(UnclosedResourceDetectedException detectedException);
162+
}
163+
}

coolapk/release/output.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"outputType":{"type":"APK"},"apkInfo":{"type":"FULL_SPLIT","splits":[{"filterType":"ABI","value":"armeabi-v7a"}],"versionCode":461,"versionName":"4.1.1 Alpha2","enabled":true,"filterName":"armeabi-v7a","outputFile":"app-coolapk-armeabi-v7a-release.apk","fullName":"coolapkArmeabi-v7aRelease","baseName":"coolapk-armeabi-v7a-release"},"path":"app-coolapk-armeabi-v7a-release.apk","properties":{}},{"outputType":{"type":"APK"},"apkInfo":{"type":"FULL_SPLIT","splits":[{"filterType":"ABI","value":"x86"}],"versionCode":461,"versionName":"4.1.1 Alpha2","enabled":true,"filterName":"x86","outputFile":"app-coolapk-x86-release.apk","fullName":"coolapkX86Release","baseName":"coolapk-x86-release"},"path":"app-coolapk-x86-release.apk","properties":{}}]

gradle.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1414
# org.gradle.parallel=true
1515
#Mon Jan 23 10:16:46 CST 2017
16-
#systemProp.https.proxyPort=1080
17-
#systemProp.http.proxyHost=127.0.0.1
1816
org.gradle.jvmargs=-Xms8192m -Xmx8192m -Dfile.encoding=UTF-8
19-
#systemProp.https.proxyHost=127.0.0.1
20-
#systemProp.http.proxyPort=1080
17+
systemProp.https.proxyHost=127.0.0.1
18+
systemProp.https.proxyPort=7890
19+
systemProp.http.proxyHost=127.0.0.1
20+
systemProp.http.proxyPort=7890
2121
android.useAndroidX=true
2222
android.enableJetifier=true

0 commit comments

Comments
 (0)