Commit 7c62d103 authored by leichao.gao's avatar leichao.gao

update

parent ac80202e
......@@ -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 = "",
......
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
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 "";
}
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment