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
7c62d103
Commit
7c62d103
authored
Jul 22, 2024
by
leichao.gao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
ac80202e
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 @
7c62d103
...
...
@@ -4,6 +4,7 @@ import android.net.Uri
data class
RecycleBinBean
(
val
path
:
String
=
""
,
val
backupPath
:
String
=
""
,
var
deleteTime
:
Long
=
0
,
val
size
:
Long
=
0
,
val
mimeType
:
String
=
""
,
...
...
app/src/main/java/com/base/filerecoveryrecyclebin/service/BackUpUtils.kt
View file @
7c62d103
package
com.base.filerecoveryrecyclebin.service
import
android.Manifest
import
android.content.pm.PackageManager
import
android.os.Build
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.bean.RecycleBinBean
import
com.base.filerecoveryrecyclebin.help.BaseApplication
import
com.base.filerecoveryrecyclebin.utils.AppPreferences
import
com.google.gson.Gson
import
com.google.gson.reflect.TypeToken
import
com.ironsource.da
import
java.io.File
import
java.lang.Exception
import
java.util.stream.Collectors
...
...
@@ -17,10 +24,18 @@ object BackUpUtils {
fun
backupFile
(
src
:
File
,
path
:
String
)
{
val
recycleBinFile
=
File
(
getRecycleBinDir
(),
".\$name"
)
src
.
copyTo
(
recycleBinFile
,
true
)
val
binBean
=
RecycleBinBean
(
src
.
path
,
0
,
src
.
length
(),
getFileExtension
(
src
),
false
)
saveData
(
binBean
,
path
)
if
(!
hasStorageReadWritePermission
())
{
return
}
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
{
...
...
@@ -35,6 +50,7 @@ object BackUpUtils {
fun
saveData
(
binBean
:
RecycleBinBean
,
path
:
String
)
{
val
hashMap
=
queryBackUpSp
()
hashMap
.
put
(
path
,
binBean
)
saveBackUpSp
(
hashMap
)
}
...
...
@@ -53,17 +69,19 @@ object BackUpUtils {
}
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
>?
{
val
hashMap
=
queryBackUpSp
()
if
(
hashMap
.
size
>
0
)
{
return
null
}
else
{
val
values
:
MutableList
<
RecycleBinBean
>?
=
hashMap
.
values
.
stream
().
collect
(
Collectors
.
toList
())
return
values
}
else
{
return
null
}
}
...
...
@@ -76,14 +94,39 @@ object BackUpUtils {
fun
deleteFile
(
path
:
String
)
{
val
isExists
=
queryFileExists
(
path
)
if
(
isExists
)
{
val
hashMap
=
queryBackUpSp
()
val
data
=
hashMap
[
path
]
val
hashMap
=
queryBackUpSp
()
val
data
=
hashMap
[
path
]
if
(
data
!=
null
)
{
data
?.
deleted
=
true
data
?.
deleteTime
=
System
.
currentTimeMillis
()
data
?.
deleted
=
true
data
?.
deleteTime
=
System
.
currentTimeMillis
()
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 @
7c62d103
package
com
.
base
.
filerecoveryrecyclebin
.
service
;
import
android.os.Build
;
import
android.os.Environment
;
import
android.os.FileObserver
;
import
android.os.Handler
;
...
...
@@ -52,14 +53,15 @@ public class FileObserverExample {
fileObserver
=
new
FileObserver
(
watchList
,
FileObserver
.
CREATE
|
FileObserver
.
DELETE
)
{
@Override
public
void
onEvent
(
int
event
,
String
path
)
{
Log
.
d
(
TAG
,
"onEvent "
+
event
+
" "
+
path
);
if
(!
TextUtils
.
isEmpty
(
path
))
{
Log
.
d
(
TAG
,
"path: "
+
path
);
Log
.
d
(
TAG
,
"onEvent "
+
event
+
" "
+
path
);
if
(!
TextUtils
.
isEmpty
(
path
))
{
Log
.
d
(
TAG
,
"path: "
+
path
);
handleFileEvent
(
path
,
event
);
}
}
};
fileObserver
.
startWatching
();
// 启动观察
Log
.
d
(
TAG
,
"startObserving"
);
}
public
void
stopObserving
()
{
...
...
@@ -71,22 +73,23 @@ public class FileObserverExample {
// 处理文件事件
private
void
handleFileEvent
(
String
path
,
int
event
)
{
Log
.
d
(
TAG
,
path
);
Log
.
d
(
TAG
,
path
);
// 检查文件扩展名是否在监听范围内
if
(
FILE_EXTENSIONS
.
contains
(
getFileExtension
(
path
)))
{
if
(
event
==
FileObserver
.
CREATE
)
{
Log
.
d
(
TAG
,
"CREATE"
);
Log
.
d
(
TAG
,
"CREATE"
);
fileHandler
.
post
(()
->
handleFileCreation
(
path
));
}
else
if
(
event
==
FileObserver
.
DELETE
)
{
Log
.
d
(
TAG
,
"DELETE:"
+
path
);
BackUpUtils
.
INSTANCE
.
deleteFile
(
path
);
}
}
}
// 获取文件扩展名
private
String
getFileExtension
(
String
fileName
)
{
private
String
getFileExtension
(
String
fileName
)
{
if
(
fileName
.
lastIndexOf
(
"."
)
!=
-
1
&&
fileName
.
lastIndexOf
(
"."
)
!=
0
)
{
return
"."
+
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
);
return
"."
+
fileName
.
substring
(
fileName
.
lastIndexOf
(
"."
)
+
1
);
}
else
{
return
""
;
}
...
...
app/src/main/java/com/base/filerecoveryrecyclebin/service/StayNotificationService.kt
View file @
7c62d103
...
...
@@ -65,8 +65,8 @@ class StayNotificationService : Service() {
override
fun
onStartCommand
(
intent
:
Intent
?,
flags
:
Int
,
startId
:
Int
):
Int
{
if
(
fileObserver
==
null
){
fileObserver
=
FileObserverExample
()
fileObserver
?.
startObserving
()
}
fileObserver
?.
startObserving
()
if
(!
isRunning
)
{
val
notification
=
createPermanentNotification
(
applicationContext
)
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