@josuelmm/capacitor-background-geolocation 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/JosuelmmCapacitorBackgroundGeolocation.podspec +34 -0
- package/LICENSE +17 -0
- package/NOTICE.md +32 -0
- package/Package.swift +45 -0
- package/README.md +402 -0
- package/android/build.gradle +79 -0
- package/android/proguard-rules.pro +1 -0
- package/android/src/main/AndroidManifest.xml +83 -0
- package/android/src/main/java/com/evgenii/jsevaluator/HandlerWrapper.java +18 -0
- package/android/src/main/java/com/evgenii/jsevaluator/JavaScriptInterface.java +22 -0
- package/android/src/main/java/com/evgenii/jsevaluator/JsEvaluator.java +133 -0
- package/android/src/main/java/com/evgenii/jsevaluator/JsFunctionCallFormatter.java +37 -0
- package/android/src/main/java/com/evgenii/jsevaluator/WebViewWrapper.java +71 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/CallJavaResultInterface.java +8 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/HandlerWrapperInterface.java +5 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/JsCallback.java +10 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/JsEvaluatorInterface.java +18 -0
- package/android/src/main/java/com/evgenii/jsevaluator/interfaces/WebViewWrapperInterface.java +14 -0
- package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/BackgroundGeolocationPlugin.java +898 -0
- package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/ConfigMapper.java +303 -0
- package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/HeadlessTaskRegistry.java +34 -0
- package/android/src/main/java/com/josuelmm/capacitor/backgroundgeolocation/JsEvaluatorTaskRunner.java +63 -0
- package/android/src/main/java/com/marianhello/bgloc/BackgroundGeolocationFacade.java +699 -0
- package/android/src/main/java/com/marianhello/bgloc/BootCompletedReceiver.java +103 -0
- package/android/src/main/java/com/marianhello/bgloc/Config.java +1155 -0
- package/android/src/main/java/com/marianhello/bgloc/ConnectivityListener.java +5 -0
- package/android/src/main/java/com/marianhello/bgloc/HttpPostService.java +362 -0
- package/android/src/main/java/com/marianhello/bgloc/LocationManager.java +138 -0
- package/android/src/main/java/com/marianhello/bgloc/PluginDelegate.java +45 -0
- package/android/src/main/java/com/marianhello/bgloc/PluginException.java +38 -0
- package/android/src/main/java/com/marianhello/bgloc/PostLocationTask.java +238 -0
- package/android/src/main/java/com/marianhello/bgloc/ResourceResolver.java +55 -0
- package/android/src/main/java/com/marianhello/bgloc/data/AbstractLocationTemplate.java +69 -0
- package/android/src/main/java/com/marianhello/bgloc/data/ArrayListLocationTemplate.java +88 -0
- package/android/src/main/java/com/marianhello/bgloc/data/BackgroundActivity.java +108 -0
- package/android/src/main/java/com/marianhello/bgloc/data/BackgroundLocation.java +1088 -0
- package/android/src/main/java/com/marianhello/bgloc/data/ConfigJsonMapper.java +211 -0
- package/android/src/main/java/com/marianhello/bgloc/data/ConfigurationDAO.java +13 -0
- package/android/src/main/java/com/marianhello/bgloc/data/DAOFactory.java +17 -0
- package/android/src/main/java/com/marianhello/bgloc/data/HashMapLocationTemplate.java +82 -0
- package/android/src/main/java/com/marianhello/bgloc/data/LocationDAO.java +27 -0
- package/android/src/main/java/com/marianhello/bgloc/data/LocationTemplate.java +12 -0
- package/android/src/main/java/com/marianhello/bgloc/data/LocationTemplateFactory.java +71 -0
- package/android/src/main/java/com/marianhello/bgloc/data/LocationTransform.java +19 -0
- package/android/src/main/java/com/marianhello/bgloc/data/SessionLocationDAO.java +18 -0
- package/android/src/main/java/com/marianhello/bgloc/data/provider/ContentProviderLocationDAO.java +406 -0
- package/android/src/main/java/com/marianhello/bgloc/data/provider/LocationContentProvider.java +321 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationContract.java +94 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteConfigurationDAO.java +227 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationContract.java +122 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteLocationDAO.java +550 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteOpenHelper.java +189 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteSessionContract.java +74 -0
- package/android/src/main/java/com/marianhello/bgloc/data/sqlite/SQLiteSessionLocationDAO.java +169 -0
- package/android/src/main/java/com/marianhello/bgloc/driving/DrivingEventsDetector.java +265 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/AbstractTaskRunner.java +15 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/ActivityTask.java +48 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/JsCallback.java +10 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/LocationTask.java +60 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/StationaryTask.java +25 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/Task.java +8 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/TaskRunner.java +5 -0
- package/android/src/main/java/com/marianhello/bgloc/headless/TaskRunnerFactory.java +8 -0
- package/android/src/main/java/com/marianhello/bgloc/http/UrlTemplateResolver.java +115 -0
- package/android/src/main/java/com/marianhello/bgloc/oem/BatteryOemHelper.java +214 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/AbstractLocationProvider.java +218 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/ActivityRecognitionLocationProvider.java +385 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/DistanceFilterLocationProvider.java +685 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/LocationProvider.java +32 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/LocationProviderFactory.java +47 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/ProviderDelegate.java +12 -0
- package/android/src/main/java/com/marianhello/bgloc/provider/RawLocationProvider.java +175 -0
- package/android/src/main/java/com/marianhello/bgloc/sensor/SensorFusionDetector.java +199 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationService.java +16 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceImpl.java +1531 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceInfo.java +6 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceInfoImpl.java +41 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceIntentBuilder.java +203 -0
- package/android/src/main/java/com/marianhello/bgloc/service/LocationServiceProxy.java +156 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/AccountHelper.java +39 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/Authenticator.java +68 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/AuthenticatorService.java +28 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/BatchManager.java +311 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/NotificationHelper.java +148 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/SyncAdapter.java +301 -0
- package/android/src/main/java/com/marianhello/bgloc/sync/SyncService.java +68 -0
- package/android/src/main/java/com/marianhello/logging/DBLogReader.java +208 -0
- package/android/src/main/java/com/marianhello/logging/LogEntry.java +99 -0
- package/android/src/main/java/com/marianhello/logging/LoggerManager.java +70 -0
- package/android/src/main/java/com/marianhello/logging/UncaughtExceptionLogger.java +36 -0
- package/android/src/main/java/com/marianhello/utils/CloneHelper.java +22 -0
- package/android/src/main/java/com/marianhello/utils/Convert.java +56 -0
- package/android/src/main/java/com/marianhello/utils/TextUtils.java +72 -0
- package/android/src/main/java/com/marianhello/utils/ToneGenerator.java +68 -0
- package/android/src/main/java/org/apache/commons/io/Charsets.java +153 -0
- package/android/src/main/java/org/apache/commons/io/input/ReversedLinesFileReader.java +344 -0
- package/android/src/main/java/org/chromium/content/browser/ThreadUtils.java +134 -0
- package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlExpression.java +398 -0
- package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlSelectStatement.java +671 -0
- package/android/src/main/java/ru/andremoniy/sqlbuilder/SqlStatement.java +29 -0
- package/android/src/main/java/ru/andremoniy/utils/TextUtils.java +61 -0
- package/android/src/main/res/mipmap-hdpi/ic_launcher.png +0 -0
- package/android/src/main/res/mipmap-mdpi/ic_launcher.png +0 -0
- package/android/src/main/res/mipmap-xhdpi/ic_launcher.png +0 -0
- package/android/src/main/res/mipmap-xxhdpi/ic_launcher.png +0 -0
- package/android/src/main/res/mipmap-xxxhdpi/ic_launcher.png +0 -0
- package/android/src/main/res/values/strings.xml +15 -0
- package/android/src/main/res/xml/authenticator.xml +7 -0
- package/android/src/main/res/xml/syncadapter.xml +9 -0
- package/dist/esm/definitions.d.ts +1052 -0
- package/dist/esm/definitions.js +142 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.js +23 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +92 -0
- package/dist/esm/web.js +242 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +415 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +418 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin-Bridging-Header.h +18 -0
- package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin.m +52 -0
- package/ios/Sources/BackgroundGeolocationPlugin/BackgroundGeolocationPlugin.swift +750 -0
- package/ios/Tests/BackgroundGeolocationPluginTests/BackgroundGeolocationPluginTests.swift +12 -0
- package/ios/common/BackgroundGeolocation/CocoaLumberjack.h +1945 -0
- package/ios/common/BackgroundGeolocation/CocoaLumberjack.m +5255 -0
- package/ios/common/BackgroundGeolocation/FMDB.h +2357 -0
- package/ios/common/BackgroundGeolocation/FMDB.m +2672 -0
- package/ios/common/BackgroundGeolocation/FMDBLogger.h +42 -0
- package/ios/common/BackgroundGeolocation/FMDBLogger.m +264 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTUHeadingRequest.h +41 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTUHeadingRequest.m +68 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager+Internal.h +33 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager.h +178 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationManager.m +1025 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequest.h +103 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequest.m +238 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTULocationRequestDefines.h +163 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTURequestIDGenerator.h +39 -0
- package/ios/common/BackgroundGeolocation/INTULocationManager/INTURequestIDGenerator.m +37 -0
- package/ios/common/BackgroundGeolocation/MAURAbstractLocationProvider.h +51 -0
- package/ios/common/BackgroundGeolocation/MAURAbstractLocationProvider.m +53 -0
- package/ios/common/BackgroundGeolocation/MAURActivity.h +23 -0
- package/ios/common/BackgroundGeolocation/MAURActivity.m +52 -0
- package/ios/common/BackgroundGeolocation/MAURActivityLocationProvider.h +18 -0
- package/ios/common/BackgroundGeolocation/MAURActivityLocationProvider.m +340 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.h +88 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundGeolocationFacade.m +1193 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundSync.h +46 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundSync.m +283 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundTaskManager.h +25 -0
- package/ios/common/BackgroundGeolocation/MAURBackgroundTaskManager.m +105 -0
- package/ios/common/BackgroundGeolocation/MAURConfig.h +99 -0
- package/ios/common/BackgroundGeolocation/MAURConfig.m +636 -0
- package/ios/common/BackgroundGeolocation/MAURConfigurationContract.h +53 -0
- package/ios/common/BackgroundGeolocation/MAURConfigurationContract.m +54 -0
- package/ios/common/BackgroundGeolocation/MAURDistanceFilterLocationProvider.h +20 -0
- package/ios/common/BackgroundGeolocation/MAURDistanceFilterLocationProvider.m +550 -0
- package/ios/common/BackgroundGeolocation/MAURGeolocationOpenHelper.h +17 -0
- package/ios/common/BackgroundGeolocation/MAURGeolocationOpenHelper.m +124 -0
- package/ios/common/BackgroundGeolocation/MAURLocation.h +73 -0
- package/ios/common/BackgroundGeolocation/MAURLocation.m +392 -0
- package/ios/common/BackgroundGeolocation/MAURLocationContract.h +38 -0
- package/ios/common/BackgroundGeolocation/MAURLocationContract.m +39 -0
- package/ios/common/BackgroundGeolocation/MAURLocationManager.h +53 -0
- package/ios/common/BackgroundGeolocation/MAURLocationManager.m +305 -0
- package/ios/common/BackgroundGeolocation/MAURLogReader.h +26 -0
- package/ios/common/BackgroundGeolocation/MAURLogReader.m +122 -0
- package/ios/common/BackgroundGeolocation/MAURLogging.h +19 -0
- package/ios/common/BackgroundGeolocation/MAURPostLocationTask.h +53 -0
- package/ios/common/BackgroundGeolocation/MAURPostLocationTask.m +367 -0
- package/ios/common/BackgroundGeolocation/MAURProviderDelegate.h +52 -0
- package/ios/common/BackgroundGeolocation/MAURRawLocationProvider.h +18 -0
- package/ios/common/BackgroundGeolocation/MAURRawLocationProvider.m +138 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteConfigurationDAO.h +26 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteConfigurationDAO.m +335 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteHelper.h +57 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteHelper.m +93 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.h +52 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteLocationDAO.m +520 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteOpenHelper.h +32 -0
- package/ios/common/BackgroundGeolocation/MAURSQLiteOpenHelper.m +276 -0
- package/ios/common/BackgroundGeolocation/MAURSensorFusionDetector.h +41 -0
- package/ios/common/BackgroundGeolocation/MAURSensorFusionDetector.m +137 -0
- package/ios/common/BackgroundGeolocation/MAURSessionLocationContract.h +29 -0
- package/ios/common/BackgroundGeolocation/MAURSessionLocationContract.m +31 -0
- package/ios/common/BackgroundGeolocation/MAURSessionLocationDAO.h +25 -0
- package/ios/common/BackgroundGeolocation/MAURSessionLocationDAO.m +153 -0
- package/ios/common/BackgroundGeolocation/MAURUncaughtExceptionLogger.h +20 -0
- package/ios/common/BackgroundGeolocation/MAURUncaughtExceptionLogger.m +62 -0
- package/ios/common/BackgroundGeolocation/MAURUrlTemplateResolver.h +31 -0
- package/ios/common/BackgroundGeolocation/MAURUrlTemplateResolver.m +107 -0
- package/ios/common/BackgroundGeolocation/Reachability.h +102 -0
- package/ios/common/BackgroundGeolocation/Reachability.m +475 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/README.md +170 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/ext/NSString+ZIMString.h +55 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/ext/NSString+ZIMString.m +47 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlDataManipulationCommand.h +27 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlExpression.h +250 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlExpression.m +259 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlSelectStatement.h +360 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlSelectStatement.m +427 -0
- package/ios/common/BackgroundGeolocation/SQLQueryBuilder/sql/ZIMSqlStatement.h +37 -0
- package/ios/common/BackgroundGeolocation/module.modulemap +16 -0
- package/package.json +82 -0
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
package org.apache.commons.io.input;
|
|
18
|
+
|
|
19
|
+
import java.io.Closeable;
|
|
20
|
+
import java.io.File;
|
|
21
|
+
import java.io.IOException;
|
|
22
|
+
import java.io.RandomAccessFile;
|
|
23
|
+
import java.io.UnsupportedEncodingException;
|
|
24
|
+
import java.nio.charset.Charset;
|
|
25
|
+
import java.nio.charset.CharsetEncoder;
|
|
26
|
+
import java.nio.charset.UnsupportedCharsetException;
|
|
27
|
+
|
|
28
|
+
import org.apache.commons.io.Charsets;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Reads lines in a file reversely (similar to a BufferedReader, but starting at
|
|
32
|
+
* the last line). Useful for e.g. searching in log files.
|
|
33
|
+
*
|
|
34
|
+
* @since 2.2
|
|
35
|
+
*/
|
|
36
|
+
public class ReversedLinesFileReader implements Closeable {
|
|
37
|
+
|
|
38
|
+
private final int blockSize;
|
|
39
|
+
private final Charset encoding;
|
|
40
|
+
|
|
41
|
+
private final RandomAccessFile randomAccessFile;
|
|
42
|
+
|
|
43
|
+
private final long totalByteLength;
|
|
44
|
+
private final long totalBlockCount;
|
|
45
|
+
|
|
46
|
+
private final byte[][] newLineSequences;
|
|
47
|
+
private final int avoidNewlineSplitBufferSize;
|
|
48
|
+
private final int byteDecrement;
|
|
49
|
+
|
|
50
|
+
private FilePart currentFilePart;
|
|
51
|
+
|
|
52
|
+
private boolean trailingNewlineOfFileSkipped = false;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Creates a ReversedLinesFileReader with default block size of 4KB and the
|
|
56
|
+
* platform's default encoding.
|
|
57
|
+
*
|
|
58
|
+
* @param file
|
|
59
|
+
* the file to be read
|
|
60
|
+
* @throws IOException if an I/O error occurs
|
|
61
|
+
*/
|
|
62
|
+
public ReversedLinesFileReader(final File file) throws IOException {
|
|
63
|
+
this(file, 4096, Charset.defaultCharset().toString());
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Creates a ReversedLinesFileReader with the given block size and encoding.
|
|
68
|
+
*
|
|
69
|
+
* @param file
|
|
70
|
+
* the file to be read
|
|
71
|
+
* @param blockSize
|
|
72
|
+
* size of the internal buffer (for ideal performance this should
|
|
73
|
+
* match with the block size of the underlying file system).
|
|
74
|
+
* @param encoding
|
|
75
|
+
* the encoding of the file
|
|
76
|
+
* @throws IOException if an I/O error occurs
|
|
77
|
+
* @since 2.3
|
|
78
|
+
*/
|
|
79
|
+
public ReversedLinesFileReader(final File file, final int blockSize, final Charset encoding) throws IOException {
|
|
80
|
+
this.blockSize = blockSize;
|
|
81
|
+
this.encoding = encoding;
|
|
82
|
+
|
|
83
|
+
randomAccessFile = new RandomAccessFile(file, "r");
|
|
84
|
+
totalByteLength = randomAccessFile.length();
|
|
85
|
+
int lastBlockLength = (int) (totalByteLength % blockSize);
|
|
86
|
+
if (lastBlockLength > 0) {
|
|
87
|
+
totalBlockCount = totalByteLength / blockSize + 1;
|
|
88
|
+
} else {
|
|
89
|
+
totalBlockCount = totalByteLength / blockSize;
|
|
90
|
+
if (totalByteLength > 0) {
|
|
91
|
+
lastBlockLength = blockSize;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
currentFilePart = new FilePart(totalBlockCount, lastBlockLength, null);
|
|
95
|
+
|
|
96
|
+
// --- check & prepare encoding ---
|
|
97
|
+
Charset charset = Charsets.toCharset(encoding);
|
|
98
|
+
CharsetEncoder charsetEncoder = charset.newEncoder();
|
|
99
|
+
float maxBytesPerChar = charsetEncoder.maxBytesPerChar();
|
|
100
|
+
if(maxBytesPerChar==1f) {
|
|
101
|
+
// all one byte encodings are no problem
|
|
102
|
+
byteDecrement = 1;
|
|
103
|
+
} else if(charset == Charset.forName("UTF-8")) {
|
|
104
|
+
// UTF-8 works fine out of the box, for multibyte sequences a second UTF-8 byte can never be a newline byte
|
|
105
|
+
// http://en.wikipedia.org/wiki/UTF-8
|
|
106
|
+
byteDecrement = 1;
|
|
107
|
+
} else if(charset == Charset.forName("Shift_JIS")) {
|
|
108
|
+
// Same as for UTF-8
|
|
109
|
+
// http://www.herongyang.com/Unicode/JIS-Shift-JIS-Encoding.html
|
|
110
|
+
byteDecrement = 1;
|
|
111
|
+
} else if(charset == Charset.forName("UTF-16BE") || charset == Charset.forName("UTF-16LE")) {
|
|
112
|
+
// UTF-16 new line sequences are not allowed as second tuple of four byte sequences,
|
|
113
|
+
// however byte order has to be specified
|
|
114
|
+
byteDecrement = 2;
|
|
115
|
+
} else if(charset == Charset.forName("UTF-16")) {
|
|
116
|
+
throw new UnsupportedEncodingException(
|
|
117
|
+
"For UTF-16, you need to specify the byte order (use UTF-16BE or UTF-16LE)");
|
|
118
|
+
} else {
|
|
119
|
+
throw new UnsupportedEncodingException(
|
|
120
|
+
"Encoding "+encoding+" is not supported yet (feel free to submit a patch)");
|
|
121
|
+
}
|
|
122
|
+
// NOTE: The new line sequences are matched in the order given, so it is important that \r\n is BEFORE \n
|
|
123
|
+
newLineSequences = new byte[][] { "\r\n".getBytes(encoding), "\n".getBytes(encoding), "\r".getBytes(encoding) };
|
|
124
|
+
|
|
125
|
+
avoidNewlineSplitBufferSize = newLineSequences[0].length;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Creates a ReversedLinesFileReader with the given block size and encoding.
|
|
130
|
+
*
|
|
131
|
+
* @param file
|
|
132
|
+
* the file to be read
|
|
133
|
+
* @param blockSize
|
|
134
|
+
* size of the internal buffer (for ideal performance this should
|
|
135
|
+
* match with the block size of the underlying file system).
|
|
136
|
+
* @param encoding
|
|
137
|
+
* the encoding of the file
|
|
138
|
+
* @throws IOException if an I/O error occurs
|
|
139
|
+
* @throws UnsupportedCharsetException
|
|
140
|
+
* thrown instead of {@link UnsupportedEncodingException} in version 2.2 if the encoding is not
|
|
141
|
+
* supported.
|
|
142
|
+
*/
|
|
143
|
+
public ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException {
|
|
144
|
+
this(file, blockSize, Charsets.toCharset(encoding));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Returns the lines of the file from bottom to top.
|
|
149
|
+
*
|
|
150
|
+
* @return the next line or null if the start of the file is reached
|
|
151
|
+
* @throws IOException if an I/O error occurs
|
|
152
|
+
*/
|
|
153
|
+
public String readLine() throws IOException {
|
|
154
|
+
|
|
155
|
+
String line = currentFilePart.readLine();
|
|
156
|
+
while (line == null) {
|
|
157
|
+
currentFilePart = currentFilePart.rollOver();
|
|
158
|
+
if (currentFilePart != null) {
|
|
159
|
+
line = currentFilePart.readLine();
|
|
160
|
+
} else {
|
|
161
|
+
// no more fileparts: we're done, leave line set to null
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// aligned behaviour wiht BufferedReader that doesn't return a last, emtpy line
|
|
167
|
+
if("".equals(line) && !trailingNewlineOfFileSkipped) {
|
|
168
|
+
trailingNewlineOfFileSkipped = true;
|
|
169
|
+
line = readLine();
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return line;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Closes underlying resources.
|
|
177
|
+
*
|
|
178
|
+
* @throws IOException if an I/O error occurs
|
|
179
|
+
*/
|
|
180
|
+
public void close() throws IOException {
|
|
181
|
+
randomAccessFile.close();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
private class FilePart {
|
|
185
|
+
private final long no;
|
|
186
|
+
|
|
187
|
+
private final byte[] data;
|
|
188
|
+
|
|
189
|
+
private byte[] leftOver;
|
|
190
|
+
|
|
191
|
+
private int currentLastBytePos;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* ctor
|
|
195
|
+
* @param no the part number
|
|
196
|
+
* @param length its length
|
|
197
|
+
* @param leftOverOfLastFilePart remainder
|
|
198
|
+
* @throws IOException if there is a problem reading the file
|
|
199
|
+
*/
|
|
200
|
+
private FilePart(final long no, final int length, final byte[] leftOverOfLastFilePart) throws IOException {
|
|
201
|
+
this.no = no;
|
|
202
|
+
int dataLength = length + (leftOverOfLastFilePart != null ? leftOverOfLastFilePart.length : 0);
|
|
203
|
+
this.data = new byte[dataLength];
|
|
204
|
+
final long off = (no - 1) * blockSize;
|
|
205
|
+
|
|
206
|
+
// read data
|
|
207
|
+
if (no > 0 /* file not empty */) {
|
|
208
|
+
randomAccessFile.seek(off);
|
|
209
|
+
final int countRead = randomAccessFile.read(data, 0, length);
|
|
210
|
+
if (countRead != length) {
|
|
211
|
+
throw new IllegalStateException("Count of requested bytes and actually read bytes don't match");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// copy left over part into data arr
|
|
215
|
+
if (leftOverOfLastFilePart != null) {
|
|
216
|
+
System.arraycopy(leftOverOfLastFilePart, 0, data, length, leftOverOfLastFilePart.length);
|
|
217
|
+
}
|
|
218
|
+
this.currentLastBytePos = data.length - 1;
|
|
219
|
+
this.leftOver = null;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Handles block rollover
|
|
224
|
+
*
|
|
225
|
+
* @return the new FilePart or null
|
|
226
|
+
* @throws IOException if there was a problem reading the file
|
|
227
|
+
*/
|
|
228
|
+
private FilePart rollOver() throws IOException {
|
|
229
|
+
|
|
230
|
+
if (currentLastBytePos > -1) {
|
|
231
|
+
throw new IllegalStateException("Current currentLastCharPos unexpectedly positive... "
|
|
232
|
+
+ "last readLine() should have returned something! currentLastCharPos=" + currentLastBytePos);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (no > 1) {
|
|
236
|
+
return new FilePart(no - 1, blockSize, leftOver);
|
|
237
|
+
} else {
|
|
238
|
+
// NO 1 was the last FilePart, we're finished
|
|
239
|
+
if (leftOver != null) {
|
|
240
|
+
throw new IllegalStateException("Unexpected leftover of the last block: leftOverOfThisFilePart="
|
|
241
|
+
+ new String(leftOver, encoding));
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Reads a line.
|
|
249
|
+
*
|
|
250
|
+
* @return the line or null
|
|
251
|
+
* @throws IOException if there is an error reading from the file
|
|
252
|
+
*/
|
|
253
|
+
private String readLine() throws IOException {
|
|
254
|
+
|
|
255
|
+
String line = null;
|
|
256
|
+
int newLineMatchByteCount;
|
|
257
|
+
|
|
258
|
+
boolean isLastFilePart = no == 1;
|
|
259
|
+
|
|
260
|
+
int i = currentLastBytePos;
|
|
261
|
+
while (i > -1) {
|
|
262
|
+
|
|
263
|
+
if (!isLastFilePart && i < avoidNewlineSplitBufferSize) {
|
|
264
|
+
// avoidNewlineSplitBuffer: for all except the last file part we
|
|
265
|
+
// take a few bytes to the next file part to avoid splitting of newlines
|
|
266
|
+
createLeftOver();
|
|
267
|
+
break; // skip last few bytes and leave it to the next file part
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// --- check for newline ---
|
|
271
|
+
if ((newLineMatchByteCount = getNewLineMatchByteCount(data, i)) > 0 /* found newline */) {
|
|
272
|
+
final int lineStart = i + 1;
|
|
273
|
+
int lineLengthBytes = currentLastBytePos - lineStart + 1;
|
|
274
|
+
|
|
275
|
+
if (lineLengthBytes < 0) {
|
|
276
|
+
throw new IllegalStateException("Unexpected negative line length="+lineLengthBytes);
|
|
277
|
+
}
|
|
278
|
+
byte[] lineData = new byte[lineLengthBytes];
|
|
279
|
+
System.arraycopy(data, lineStart, lineData, 0, lineLengthBytes);
|
|
280
|
+
|
|
281
|
+
line = new String(lineData, encoding);
|
|
282
|
+
|
|
283
|
+
currentLastBytePos = i - newLineMatchByteCount;
|
|
284
|
+
break; // found line
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// --- move cursor ---
|
|
288
|
+
i -= byteDecrement;
|
|
289
|
+
|
|
290
|
+
// --- end of file part handling ---
|
|
291
|
+
if (i < 0) {
|
|
292
|
+
createLeftOver();
|
|
293
|
+
break; // end of file part
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// --- last file part handling ---
|
|
298
|
+
if (isLastFilePart && leftOver != null) {
|
|
299
|
+
// there will be no line break anymore, this is the first line of the file
|
|
300
|
+
line = new String(leftOver, encoding);
|
|
301
|
+
leftOver = null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return line;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Creates the buffer containing any left over bytes.
|
|
309
|
+
*/
|
|
310
|
+
private void createLeftOver() {
|
|
311
|
+
int lineLengthBytes = currentLastBytePos + 1;
|
|
312
|
+
if (lineLengthBytes > 0) {
|
|
313
|
+
// create left over for next block
|
|
314
|
+
leftOver = new byte[lineLengthBytes];
|
|
315
|
+
System.arraycopy(data, 0, leftOver, 0, lineLengthBytes);
|
|
316
|
+
} else {
|
|
317
|
+
leftOver = null;
|
|
318
|
+
}
|
|
319
|
+
currentLastBytePos = -1;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Finds the new-line sequence and return its length.
|
|
324
|
+
*
|
|
325
|
+
* @param data buffer to scan
|
|
326
|
+
* @param i start offset in buffer
|
|
327
|
+
* @return length of newline sequence or 0 if none found
|
|
328
|
+
*/
|
|
329
|
+
private int getNewLineMatchByteCount(byte[] data, int i) {
|
|
330
|
+
for (byte[] newLineSequence : newLineSequences) {
|
|
331
|
+
boolean match = true;
|
|
332
|
+
for (int j = newLineSequence.length - 1; j >= 0; j--) {
|
|
333
|
+
int k = i + j - (newLineSequence.length - 1);
|
|
334
|
+
match &= k >= 0 && data[k] == newLineSequence[j];
|
|
335
|
+
}
|
|
336
|
+
if (match) {
|
|
337
|
+
return newLineSequence.length;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return 0;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
|
2
|
+
// Use of this source code is governed by a BSD-style license that can be
|
|
3
|
+
// found in the LICENSE file.
|
|
4
|
+
|
|
5
|
+
package org.chromium.content.browser;
|
|
6
|
+
|
|
7
|
+
import android.os.Handler;
|
|
8
|
+
import android.os.Looper;
|
|
9
|
+
|
|
10
|
+
import java.util.concurrent.Callable;
|
|
11
|
+
import java.util.concurrent.ExecutionException;
|
|
12
|
+
import java.util.concurrent.FutureTask;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Helper methods to deal with threading related tasks.
|
|
16
|
+
*/
|
|
17
|
+
public class ThreadUtils {
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Run the supplied Runnable on the main thread. The method will block until
|
|
21
|
+
* the Runnable completes.
|
|
22
|
+
*
|
|
23
|
+
* @param r The Runnable to run.
|
|
24
|
+
*/
|
|
25
|
+
public static void runOnUiThreadBlocking(final Runnable r) {
|
|
26
|
+
if (runningOnUiThread()) {
|
|
27
|
+
r.run();
|
|
28
|
+
} else {
|
|
29
|
+
FutureTask<Void> task = new FutureTask<Void>(r, null);
|
|
30
|
+
postOnUiThread(task);
|
|
31
|
+
try {
|
|
32
|
+
task.get();
|
|
33
|
+
} catch (Exception e) {
|
|
34
|
+
throw new RuntimeException("Exception occured while waiting for runnable", e);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Run the supplied Callable on the main thread, wrapping any exceptions in
|
|
41
|
+
* a RuntimeException. The method will block until the Callable completes.
|
|
42
|
+
*
|
|
43
|
+
* @param c The Callable to run
|
|
44
|
+
* @return The result of the callable
|
|
45
|
+
*/
|
|
46
|
+
public static <T> T runOnUiThreadBlockingNoException(Callable<T> c) {
|
|
47
|
+
try {
|
|
48
|
+
return runOnUiThreadBlocking(c);
|
|
49
|
+
} catch (ExecutionException e) {
|
|
50
|
+
throw new RuntimeException("Error occured waiting for callable", e);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Run the supplied Callable on the main thread, The method will block until
|
|
56
|
+
* the Callable completes.
|
|
57
|
+
*
|
|
58
|
+
* @param c The Callable to run
|
|
59
|
+
* @return The result of the callable
|
|
60
|
+
* @throws ExecutionException c's exception
|
|
61
|
+
*/
|
|
62
|
+
public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException {
|
|
63
|
+
FutureTask<T> task = new FutureTask<T>(c);
|
|
64
|
+
runOnUiThread(task);
|
|
65
|
+
try {
|
|
66
|
+
return task.get();
|
|
67
|
+
} catch (InterruptedException e) {
|
|
68
|
+
throw new RuntimeException("Interrupted waiting for callable", e);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Run the supplied FutureTask on the main thread. The method will block
|
|
74
|
+
* only if the current thread is the main thread.
|
|
75
|
+
*
|
|
76
|
+
* @param task The FutureTask to run
|
|
77
|
+
* @return The queried task (to aid inline construction)
|
|
78
|
+
*/
|
|
79
|
+
public static <T> FutureTask<T> runOnUiThread(FutureTask<T> task) {
|
|
80
|
+
if (runningOnUiThread()) {
|
|
81
|
+
task.run();
|
|
82
|
+
} else {
|
|
83
|
+
postOnUiThread(task);
|
|
84
|
+
}
|
|
85
|
+
return task;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Run the supplied Callable on the main thread. The method will block
|
|
90
|
+
* only if the current thread is the main thread.
|
|
91
|
+
*
|
|
92
|
+
* @param c The Callable to run
|
|
93
|
+
* @return A FutureTask wrapping the callable to retrieve results
|
|
94
|
+
*/
|
|
95
|
+
public static <T> FutureTask<T> runOnUiThread(Callable<T> c) {
|
|
96
|
+
return runOnUiThread(new FutureTask<T>(c));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Run the supplied Runnable on the main thread. The method will block
|
|
101
|
+
* only if the current thread is the main thread.
|
|
102
|
+
*
|
|
103
|
+
* @param r The Runnable to run
|
|
104
|
+
*/
|
|
105
|
+
public static void runOnUiThread(Runnable r) {
|
|
106
|
+
runOnUiThread(new FutureTask<Void>(r, null));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Post the supplied FutureTask to run on the main thread. The method will
|
|
111
|
+
* not block, even if called on the UI thread.
|
|
112
|
+
*
|
|
113
|
+
* @param task The FutureTask to run
|
|
114
|
+
* @return The queried task (to aid inline construction)
|
|
115
|
+
*/
|
|
116
|
+
public static <T> FutureTask<T> postOnUiThread(FutureTask<T> task) {
|
|
117
|
+
new Handler(Looper.getMainLooper()).post(task);
|
|
118
|
+
return task;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Asserts that the current thread is running on the main thread.
|
|
123
|
+
*/
|
|
124
|
+
public static void assertOnUiThread() {
|
|
125
|
+
assert runningOnUiThread();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @return true iff the current thread is the main (UI) thread.
|
|
130
|
+
*/
|
|
131
|
+
public static boolean runningOnUiThread() {
|
|
132
|
+
return Looper.getMainLooper() == Looper.myLooper();
|
|
133
|
+
}
|
|
134
|
+
}
|