Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
F
File Recovery RecycleBin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
File Recovery RecycleBin
Commits
e4fae0e3
Commit
e4fae0e3
authored
Jul 22, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of gitlab.huolea.com:koko/file-recovery-recyclebin
parents
af3d4434
7c62d103
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
67 additions
and
20 deletions
+67
-20
RecycleBinBean.kt
...va/com/base/filerecoveryrecyclebin/bean/RecycleBinBean.kt
+1
-0
BackUpUtils.kt
...va/com/base/filerecoveryrecyclebin/service/BackUpUtils.kt
+55
-12
FileObserverExample.java
...e/filerecoveryrecyclebin/service/FileObserverExample.java
+10
-7
StayNotificationService.kt
...filerecoveryrecyclebin/service/StayNotificationService.kt
+1
-1
No files found.
app/src/main/java/com/base/filerecoveryrecyclebin/bean/RecycleBinBean.kt
View file @
e4fae0e3
...
@@ -4,6 +4,7 @@ import android.net.Uri
...
@@ -4,6 +4,7 @@ import android.net.Uri
data class
RecycleBinBean
(
data class
RecycleBinBean
(
val
path
:
String
=
""
,
val
path
:
String
=
""
,
val
backupPath
:
String
=
""
,
var
deleteTime
:
Long
=
0
,
var
deleteTime
:
Long
=
0
,
val
size
:
Long
=
0
,
val
size
:
Long
=
0
,
val
mimeType
:
String
=
""
,
val
mimeType
:
String
=
""
,
...
...
app/src/main/java/com/base/filerecoveryrecyclebin/service/BackUpUtils.kt
View file @
e4fae0e3
package
com.base.filerecoveryrecyclebin.service
package
com.base.filerecoveryrecyclebin.service
import
android.Manifest
import
android.content.pm.PackageManager
import
android.os.Build
import
android.text.TextUtils
import
android.text.TextUtils
import
android.util.Log
import
androidx.core.content.ContextCompat
import
com.base.filerecoveryrecyclebin.MyApplication
import
com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinFileEx.getRecycleBinDir
import
com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinFileEx.getRecycleBinDir
import
com.base.filerecoveryrecyclebin.bean.RecycleBinBean
import
com.base.filerecoveryrecyclebin.bean.RecycleBinBean
import
com.base.filerecoveryrecyclebin.help.BaseApplication
import
com.base.filerecoveryrecyclebin.utils.AppPreferences
import
com.base.filerecoveryrecyclebin.utils.AppPreferences
import
com.google.gson.Gson
import
com.google.gson.Gson
import
com.google.gson.reflect.TypeToken
import
com.google.gson.reflect.TypeToken
import
com.ironsource.da
import
java.io.File
import
java.io.File
import
java.lang.Exception
import
java.util.stream.Collectors
import
java.util.stream.Collectors
...
@@ -17,10 +24,18 @@ object BackUpUtils {
...
@@ -17,10 +24,18 @@ object BackUpUtils {
fun
backupFile
(
src
:
File
,
path
:
String
)
{
fun
backupFile
(
src
:
File
,
path
:
String
)
{
val
recycleBinFile
=
File
(
getRecycleBinDir
(),
".\$name"
)
if
(!
hasStorageReadWritePermission
())
{
src
.
copyTo
(
recycleBinFile
,
true
)
return
val
binBean
=
RecycleBinBean
(
src
.
path
,
0
,
src
.
length
(),
getFileExtension
(
src
),
false
)
}
saveData
(
binBean
,
path
)
try
{
val
recycleBinFile
=
File
(
getRecycleBinDir
(),
".\$name"
)
src
.
copyTo
(
recycleBinFile
,
true
)
val
binBean
=
RecycleBinBean
(
src
.
path
,
recycleBinFile
.
path
,
0
,
src
.
length
(),
getFileExtension
(
src
),
false
)
saveData
(
binBean
,
path
)
}
catch
(
e
:
Exception
)
{
}
}
}
private
fun
getFileExtension
(
file
:
File
):
String
{
private
fun
getFileExtension
(
file
:
File
):
String
{
...
@@ -35,6 +50,7 @@ object BackUpUtils {
...
@@ -35,6 +50,7 @@ object BackUpUtils {
fun
saveData
(
binBean
:
RecycleBinBean
,
path
:
String
)
{
fun
saveData
(
binBean
:
RecycleBinBean
,
path
:
String
)
{
val
hashMap
=
queryBackUpSp
()
val
hashMap
=
queryBackUpSp
()
hashMap
.
put
(
path
,
binBean
)
hashMap
.
put
(
path
,
binBean
)
saveBackUpSp
(
hashMap
)
saveBackUpSp
(
hashMap
)
}
}
...
@@ -53,17 +69,19 @@ object BackUpUtils {
...
@@ -53,17 +69,19 @@ object BackUpUtils {
}
}
fun
saveBackUpSp
(
hash
:
HashMap
<
String
,
RecycleBinBean
>)
{
fun
saveBackUpSp
(
hash
:
HashMap
<
String
,
RecycleBinBean
>)
{
AppPreferences
.
getInstance
().
put
(
KEY
,
Gson
().
toJson
(
hash
))
val
str
=
Gson
().
toJson
(
hash
)
Log
.
d
(
"FileObserverExample"
,
str
)
AppPreferences
.
getInstance
().
put
(
KEY
,
str
)
}
}
fun
getBackUpListObj
():
List
<
RecycleBinBean
>?
{
fun
getBackUpListObj
():
List
<
RecycleBinBean
>?
{
val
hashMap
=
queryBackUpSp
()
val
hashMap
=
queryBackUpSp
()
if
(
hashMap
.
size
>
0
)
{
if
(
hashMap
.
size
>
0
)
{
return
null
}
else
{
val
values
:
MutableList
<
RecycleBinBean
>?
=
val
values
:
MutableList
<
RecycleBinBean
>?
=
hashMap
.
values
.
stream
().
collect
(
Collectors
.
toList
())
hashMap
.
values
.
stream
().
collect
(
Collectors
.
toList
())
return
values
return
values
}
else
{
return
null
}
}
}
}
...
@@ -76,14 +94,39 @@ object BackUpUtils {
...
@@ -76,14 +94,39 @@ object BackUpUtils {
fun
deleteFile
(
path
:
String
)
{
fun
deleteFile
(
path
:
String
)
{
val
isExists
=
queryFileExists
(
path
)
val
isExists
=
queryFileExists
(
path
)
if
(
isExists
)
{
if
(
isExists
)
{
val
hashMap
=
queryBackUpSp
()
val
hashMap
=
queryBackUpSp
()
val
data
=
hashMap
[
path
]
val
data
=
hashMap
[
path
]
if
(
data
!=
null
)
{
if
(
data
!=
null
)
{
data
?.
deleted
=
true
data
?.
deleted
=
true
data
?.
deleteTime
=
System
.
currentTimeMillis
()
data
?.
deleteTime
=
System
.
currentTimeMillis
()
hashMap
[
path
]
=
data
hashMap
[
path
]
=
data
saveBackUpSp
(
hashMap
)
}
}
}
}
}
}
/**
* 检查应用是否具有存储读写权限
*
* @param context 应用上下文
* @return 如果应用有权限返回 true,否则返回 false
*/
fun
hasStorageReadWritePermission
():
Boolean
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
R
)
{
return
true
}
else
{
// 检查存储读写权限
val
readPermission
:
Int
=
ContextCompat
.
checkSelfPermission
(
BaseApplication
.
context
,
Manifest
.
permission
.
READ_EXTERNAL_STORAGE
)
val
writePermission
:
Int
=
ContextCompat
.
checkSelfPermission
(
BaseApplication
.
context
,
Manifest
.
permission
.
WRITE_EXTERNAL_STORAGE
)
return
readPermission
==
PackageManager
.
PERMISSION_GRANTED
&&
writePermission
==
PackageManager
.
PERMISSION_GRANTED
}
}
}
}
\ No newline at end of file
app/src/main/java/com/base/filerecoveryrecyclebin/service/FileObserverExample.java
View file @
e4fae0e3
package
com
.
base
.
filerecoveryrecyclebin
.
service
;
package
com
.
base
.
filerecoveryrecyclebin
.
service
;
import
android.os.Build
;
import
android.os.Environment
;
import
android.os.Environment
;
import
android.os.FileObserver
;
import
android.os.FileObserver
;
import
android.os.Handler
;
import
android.os.Handler
;
...
@@ -52,14 +53,15 @@ public class FileObserverExample {
...
@@ -52,14 +53,15 @@ public class FileObserverExample {
fileObserver
=
new
FileObserver
(
watchList
,
FileObserver
.
CREATE
|
FileObserver
.
DELETE
)
{
fileObserver
=
new
FileObserver
(
watchList
,
FileObserver
.
CREATE
|
FileObserver
.
DELETE
)
{
@Override
@Override
public
void
onEvent
(
int
event
,
String
path
)
{
public
void
onEvent
(
int
event
,
String
path
)
{
Log
.
d
(
TAG
,
"onEvent "
+
event
+
" "
+
path
);
Log
.
d
(
TAG
,
"onEvent "
+
event
+
" "
+
path
);
if
(!
TextUtils
.
isEmpty
(
path
))
{
if
(!
TextUtils
.
isEmpty
(
path
))
{
Log
.
d
(
TAG
,
"path: "
+
path
);
Log
.
d
(
TAG
,
"path: "
+
path
);
handleFileEvent
(
path
,
event
);
handleFileEvent
(
path
,
event
);
}
}
}
}
};
};
fileObserver
.
startWatching
();
// 启动观察
fileObserver
.
startWatching
();
// 启动观察
Log
.
d
(
TAG
,
"startObserving"
);
}
}
public
void
stopObserving
()
{
public
void
stopObserving
()
{
...
@@ -71,22 +73,23 @@ public class FileObserverExample {
...
@@ -71,22 +73,23 @@ public class FileObserverExample {
// 处理文件事件
// 处理文件事件
private
void
handleFileEvent
(
String
path
,
int
event
)
{
private
void
handleFileEvent
(
String
path
,
int
event
)
{
Log
.
d
(
TAG
,
path
);
Log
.
d
(
TAG
,
path
);
// 检查文件扩展名是否在监听范围内
// 检查文件扩展名是否在监听范围内
if
(
FILE_EXTENSIONS
.
contains
(
getFileExtension
(
path
)))
{
if
(
FILE_EXTENSIONS
.
contains
(
getFileExtension
(
path
)))
{
if
(
event
==
FileObserver
.
CREATE
)
{
if
(
event
==
FileObserver
.
CREATE
)
{
Log
.
d
(
TAG
,
"CREATE"
);
Log
.
d
(
TAG
,
"CREATE"
);
fileHandler
.
post
(()
->
handleFileCreation
(
path
));
fileHandler
.
post
(()
->
handleFileCreation
(
path
));
}
else
if
(
event
==
FileObserver
.
DELETE
)
{
}
else
if
(
event
==
FileObserver
.
DELETE
)
{
Log
.
d
(
TAG
,
"DELETE:"
+
path
);
BackUpUtils
.
INSTANCE
.
deleteFile
(
path
);
BackUpUtils
.
INSTANCE
.
deleteFile
(
path
);
}
}
}
}
}
}
// 获取文件扩展名
// 获取文件扩展名
private
String
getFileExtension
(
String
fileName
)
{
private
String
getFileExtension
(
String
fileName
)
{
if
(
fileName
.
lastIndexOf
(
"."
)
!=
-
1
&&
fileName
.
lastIndexOf
(
"."
)
!=
0
)
{
if
(
fileName
.
lastIndexOf
(
"."
)
!=
-
1
&&
fileName
.
lastIndexOf
(
"."
)
!=
0
)
{
return
"."
+
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
);
return
"."
+
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
);
}
else
{
}
else
{
return
""
;
return
""
;
}
}
...
...
app/src/main/java/com/base/filerecoveryrecyclebin/service/StayNotificationService.kt
View file @
e4fae0e3
...
@@ -65,8 +65,8 @@ class StayNotificationService : Service() {
...
@@ -65,8 +65,8 @@ class StayNotificationService : Service() {
override
fun
onStartCommand
(
intent
:
Intent
?,
flags
:
Int
,
startId
:
Int
):
Int
{
override
fun
onStartCommand
(
intent
:
Intent
?,
flags
:
Int
,
startId
:
Int
):
Int
{
if
(
fileObserver
==
null
){
if
(
fileObserver
==
null
){
fileObserver
=
FileObserverExample
()
fileObserver
=
FileObserverExample
()
fileObserver
?.
startObserving
()
}
}
fileObserver
?.
startObserving
()
if
(!
isRunning
)
{
if
(!
isRunning
)
{
val
notification
=
createPermanentNotification
(
applicationContext
)
val
notification
=
createPermanentNotification
(
applicationContext
)
startForeground
(
1
,
notification
)
startForeground
(
1
,
notification
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment