-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/seafile/seadroid2/util/DeviceIdManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.seafile.seadroid2.util; | ||
|
||
import android.bluetooth.le.AdvertiseData; | ||
import android.bluetooth.le.AdvertiseSettings; | ||
|
||
import com.blankj.utilcode.util.FileUtils; | ||
import com.seafile.seadroid2.SeadroidApplication; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.util.UUID; | ||
|
||
public class DeviceIdManager { | ||
private static volatile DeviceIdManager mSingleton = null; | ||
public static final String PREFIX = ".sd-"; | ||
public final File LOCAL_FOLDER = SeadroidApplication.getAppContext().getExternalCacheDir(); | ||
|
||
private DeviceIdManager() { | ||
|
||
} | ||
|
||
public static DeviceIdManager getInstance() { | ||
if (mSingleton == null) { | ||
synchronized (DeviceIdManager.class) { | ||
if (mSingleton == null) { | ||
mSingleton = new DeviceIdManager(); | ||
} | ||
} | ||
} | ||
return mSingleton; | ||
} | ||
|
||
public String getOrSet() { | ||
File stFile = checkLocalDeviceIdFileExistsState(); | ||
if (stFile != null) { | ||
String name = stFile.getName().replace(PREFIX, ""); | ||
return name.substring(0, 16); | ||
} | ||
|
||
String uid = UUID.randomUUID().toString().replace("-", ""); | ||
File file = new File(LOCAL_FOLDER.getAbsolutePath() + "/" + PREFIX + uid); | ||
FileUtils.createOrExistsFile(file); | ||
return uid.substring(0, 16); | ||
} | ||
|
||
public File checkLocalDeviceIdFileExistsState() { | ||
if (LOCAL_FOLDER == null) { | ||
return null; | ||
} | ||
|
||
File[] temp = LOCAL_FOLDER.listFiles(new FilenameFilter() { | ||
@Override | ||
public boolean accept(File dir, String name) { | ||
return name.startsWith(PREFIX); | ||
} | ||
}); | ||
|
||
if (temp == null) { | ||
return null; | ||
} | ||
|
||
if (temp.length == 1) { | ||
return temp[0]; | ||
} | ||
|
||
for (File file : temp) { | ||
file.delete(); | ||
} | ||
return null; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/seafile/seadroid2/util/FileExports.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.seafile.seadroid2.util; | ||
|
||
import android.content.ContentResolver; | ||
import android.content.ContentValues; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.os.Environment; | ||
import android.os.ParcelFileDescriptor; | ||
import android.provider.MediaStore; | ||
import android.support.annotation.RequiresApi; | ||
|
||
import com.blankj.utilcode.util.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class FileExports { | ||
@RequiresApi(api = Build.VERSION_CODES.Q) | ||
public static void exportFileAndroid10AndAbove(String fileName, String mimeType, ContentResolver contentResolver, File file) throws IOException { | ||
ContentValues cv = new ContentValues(); | ||
cv.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName); | ||
cv.put(MediaStore.MediaColumns.MIME_TYPE, mimeType); | ||
cv.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS); | ||
|
||
Uri uri = contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, cv); | ||
if (uri != null) { | ||
ParcelFileDescriptor descriptor = contentResolver.openFileDescriptor(uri, "w"); | ||
if (descriptor != null) { | ||
FileOutputStream fos = new FileOutputStream(descriptor.getFileDescriptor()); | ||
FileInputStream fis = new FileInputStream(file); | ||
copyStream(fis, fos); | ||
} | ||
} else { | ||
File downloadFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); | ||
// | ||
File dest = new File(downloadFolder.getPath() + "/" + fileName); | ||
if (!dest.exists()) { | ||
FileUtils.copy(file, dest); | ||
} | ||
} | ||
} | ||
|
||
private static void copyStream(InputStream inputStream, FileOutputStream outputStream) throws IOException { | ||
byte[] buffer = new byte[1024]; | ||
int len; | ||
while ((len = inputStream.read(buffer)) != -1) { | ||
outputStream.write(buffer, 0, len); | ||
} | ||
} | ||
|
||
} |