mirror of
				https://github.com/KokaKiwi/BarInsta
				synced 2025-10-30 19:15:35 +00:00 
			
		
		
		
	more efficient way to search subdirs
This commit is contained in:
		
							parent
							
								
									e8fd989558
								
							
						
					
					
						commit
						a6b2031950
					
				| @ -180,6 +180,7 @@ dependencies { | |||||||
|     implementation "androidx.constraintlayout:constraintlayout:2.0.4" |     implementation "androidx.constraintlayout:constraintlayout:2.0.4" | ||||||
|     implementation "androidx.preference:preference:1.1.1" |     implementation "androidx.preference:preference:1.1.1" | ||||||
|     implementation 'androidx.palette:palette:1.0.0' |     implementation 'androidx.palette:palette:1.0.0' | ||||||
|  |     implementation 'androidx.documentfile:documentfile:1.0.1' | ||||||
| 
 | 
 | ||||||
|     implementation 'com.google.guava:guava:27.0.1-android' |     implementation 'com.google.guava:guava:27.0.1-android' | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -52,7 +52,7 @@ class CameraActivity : BaseLanguageActivity() { | |||||||
|         setContentView(binding.root) |         setContentView(binding.root) | ||||||
|         Utils.transparentStatusBar(this, true, false) |         Utils.transparentStatusBar(this, true, false) | ||||||
|         displayManager = getSystemService(DISPLAY_SERVICE) as DisplayManager |         displayManager = getSystemService(DISPLAY_SERVICE) as DisplayManager | ||||||
|         outputDirectory = DownloadUtils.getCameraDir() |         outputDirectory = DownloadUtils.cameraDir | ||||||
|         cameraExecutor = Executors.newSingleThreadExecutor() |         cameraExecutor = Executors.newSingleThreadExecutor() | ||||||
|         displayManager.registerDisplayListener(displayListener, null) |         displayManager.registerDisplayListener(displayListener, null) | ||||||
|         binding.viewFinder.post { |         binding.viewFinder.post { | ||||||
|  | |||||||
| @ -36,6 +36,7 @@ object DownloadUtils { | |||||||
|     private const val DIR_RECORDINGS = "Sent Recordings" |     private const val DIR_RECORDINGS = "Sent Recordings" | ||||||
|     private const val DIR_TEMP = "Temp" |     private const val DIR_TEMP = "Temp" | ||||||
|     private const val DIR_BACKUPS = "Backups" |     private const val DIR_BACKUPS = "Backups" | ||||||
|  |     private val dirMap: MutableMap<String, DocumentFile?> = mutableMapOf() | ||||||
|     private var root: DocumentFile? = null |     private var root: DocumentFile? = null | ||||||
|     @JvmStatic |     @JvmStatic | ||||||
|     @Throws(ReselectDocumentTreeException::class) |     @Throws(ReselectDocumentTreeException::class) | ||||||
| @ -48,18 +49,15 @@ object DownloadUtils { | |||||||
|         } |         } | ||||||
|         val uri = Uri.parse(barinstaDirUri) |         val uri = Uri.parse(barinstaDirUri) | ||||||
|         if (!barinstaDirUri!!.startsWith("content://com.android.externalstorage.documents")) { |         if (!barinstaDirUri!!.startsWith("content://com.android.externalstorage.documents")) { | ||||||
|             // reselect the folder in selector view |  | ||||||
|             throw ReselectDocumentTreeException(uri) |             throw ReselectDocumentTreeException(uri) | ||||||
|         } |         } | ||||||
|         val existingPermissions = context.contentResolver.persistedUriPermissions |         val existingPermissions = context.contentResolver.persistedUriPermissions | ||||||
|         if (existingPermissions.isEmpty()) { |         if (existingPermissions.isEmpty()) { | ||||||
|             // reselect the folder in selector view |  | ||||||
|             throw ReselectDocumentTreeException(uri) |             throw ReselectDocumentTreeException(uri) | ||||||
|         } |         } | ||||||
|         val anyMatch = existingPermissions.stream() |         val anyMatch = existingPermissions.stream() | ||||||
|             .anyMatch { uriPermission: UriPermission -> uriPermission.uri == uri } |             .anyMatch { uriPermission: UriPermission -> uriPermission.uri == uri } | ||||||
|         if (!anyMatch) { |         if (!anyMatch) { | ||||||
|             // reselect the folder in selector view |  | ||||||
|             throw ReselectDocumentTreeException(uri) |             throw ReselectDocumentTreeException(uri) | ||||||
|         } |         } | ||||||
|         root = DocumentFile.fromTreeUri(context, uri) |         root = DocumentFile.fromTreeUri(context, uri) | ||||||
| @ -68,24 +66,59 @@ object DownloadUtils { | |||||||
|             throw ReselectDocumentTreeException(uri) |             throw ReselectDocumentTreeException(uri) | ||||||
|         } |         } | ||||||
|         Utils.settingsHelper.putString(PreferenceKeys.PREF_BARINSTA_DIR_URI, uri.toString()) |         Utils.settingsHelper.putString(PreferenceKeys.PREF_BARINSTA_DIR_URI, uri.toString()) | ||||||
|  |         // set up directories | ||||||
|  |         val dirKeys = listOf(DIR_DOWNLOADS, DIR_CAMERA, DIR_EDIT, DIR_RECORDINGS, DIR_TEMP, DIR_BACKUPS) | ||||||
|  |         dirMap.putAll(checkSubDirs(context, root, dirKeys, true)) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun destroy() { |     fun destroy() { | ||||||
|         root = null |         root = null | ||||||
|  |         dirMap.clear() | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     fun getDownloadDir(vararg dirs: String?): DocumentFile? { |     fun checkSubDirs(context: Context, | ||||||
|  |                      parent: DocumentFile?, | ||||||
|  |                      queries: List<String>, | ||||||
|  |                      create: Boolean): Map<String, DocumentFile?> { | ||||||
|  |         // first we'll find existing ones | ||||||
|  |         val result: MutableMap<String, DocumentFile?> = mutableMapOf() | ||||||
|  |         if (parent == null) return result.toMap() | ||||||
|  |         val parentUri = parent.uri | ||||||
|  |         val docId = DocumentsContract.getTreeDocumentId(parentUri) | ||||||
|  |         val docUri = DocumentsContract.buildChildDocumentsUriUsingTree(parentUri, docId) | ||||||
|  |         val docCursor = context.contentResolver.query( | ||||||
|  |             docUri, arrayOf( | ||||||
|  |                 DocumentsContract.Document.COLUMN_DISPLAY_NAME, | ||||||
|  |                 DocumentsContract.Document.COLUMN_DOCUMENT_ID, | ||||||
|  |                 DocumentsContract.Document.COLUMN_MIME_TYPE | ||||||
|  |             ), null, null, null | ||||||
|  |         ) | ||||||
|  |         if (docCursor == null) return result.toMap() | ||||||
|  |         while (docCursor.moveToNext()) { | ||||||
|  |             if (!DocumentsContract.Document.MIME_TYPE_DIR.equals(docCursor.getString(2)) || !queries.contains(docCursor.getString(0))) | ||||||
|  |                 continue | ||||||
|  |             val userFolderUri = DocumentsContract.buildDocumentUriUsingTree(parentUri, docCursor.getString(1)) | ||||||
|  |             val dir = DocumentFile.fromTreeUri(context, userFolderUri) | ||||||
|  |             Log.d("austin_debug", userFolderUri.toString() + " " + dir!!.uri.toString()) | ||||||
|  |             result.put(docCursor.getString(0), dir) | ||||||
|  |         } | ||||||
|  |         docCursor.close() | ||||||
|  |         // next we'll create inexistent ones | ||||||
|  |         if (create) { | ||||||
|  |             for (k in queries) { | ||||||
|  |                 if (result.get(k) == null) { | ||||||
|  |                     result.put(k, parent.createDirectory(k)) | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return result.toMap() | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     fun getDownloadDir(dir: String): DocumentFile? { | ||||||
|         if (root == null) { |         if (root == null) { | ||||||
|             return null |             return null | ||||||
|         } |         } | ||||||
|         var subDir = root |         return dirMap.get(dir) | ||||||
|         for (dir in dirs) { |  | ||||||
|             if (subDir == null || isEmpty(dir)) continue |  | ||||||
|             val subDirFile = subDir.findFile(dir!!) |  | ||||||
|             val exists = subDirFile != null && subDirFile.exists() |  | ||||||
|             subDir = if (exists) subDirFile else subDir.createDirectory(dir) |  | ||||||
|         } |  | ||||||
|         return subDir |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @JvmStatic |     @JvmStatic | ||||||
| @ -93,23 +126,24 @@ object DownloadUtils { | |||||||
|         get() = getDownloadDir(DIR_DOWNLOADS) |         get() = getDownloadDir(DIR_DOWNLOADS) | ||||||
| 
 | 
 | ||||||
|     @JvmStatic |     @JvmStatic | ||||||
|     fun getCameraDir(): DocumentFile? { |     val cameraDir: DocumentFile? | ||||||
|         return getDownloadDir(DIR_CAMERA) |         get() = getDownloadDir(DIR_CAMERA) | ||||||
|  | 
 | ||||||
|  |     @JvmStatic | ||||||
|  |     fun getImageEditDir(sessionId: String?, context: Context): DocumentFile? { | ||||||
|  |         val editRoot = getDownloadDir(DIR_EDIT) | ||||||
|  |         if (sessionId == null) return editRoot | ||||||
|  |         val result = checkSubDirs(context, editRoot, listOf(sessionId), true) | ||||||
|  |         return result.get(sessionId) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @JvmStatic |     @JvmStatic | ||||||
|     fun getImageEditDir(sessionId: String?): DocumentFile? { |     val recordingsDir: DocumentFile? | ||||||
|         return getDownloadDir(DIR_EDIT, sessionId) |         get() = getDownloadDir(DIR_RECORDINGS) | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     fun getRecordingsDir(): DocumentFile? { |  | ||||||
|         return getDownloadDir(DIR_RECORDINGS) |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     @JvmStatic |     @JvmStatic | ||||||
|     fun getBackupsDir(): DocumentFile? { |     val backupsDir: DocumentFile? | ||||||
|         return getDownloadDir(DIR_BACKUPS) |         get() = getDownloadDir(DIR_BACKUPS) | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     // @Nullable |     // @Nullable | ||||||
|     // private static DocumentFile getDownloadDir(@NonNull final Context context, @Nullable final String username) { |     // private static DocumentFile getDownloadDir(@NonNull final Context context, @Nullable final String username) { | ||||||
|  | |||||||
| @ -34,7 +34,7 @@ class DirectThreadViewModel( | |||||||
| 
 | 
 | ||||||
|     // private static final String ERROR_INVALID_THREAD = "Invalid thread"; |     // private static final String ERROR_INVALID_THREAD = "Invalid thread"; | ||||||
|     private val contentResolver: ContentResolver = application.contentResolver |     private val contentResolver: ContentResolver = application.contentResolver | ||||||
|     private val recordingsDir: DocumentFile? = DownloadUtils.getRecordingsDir() |     private val recordingsDir: DocumentFile? = DownloadUtils.recordingsDir | ||||||
|     private var voiceRecorder: VoiceRecorder? = null |     private var voiceRecorder: VoiceRecorder? = null | ||||||
|     private lateinit var threadManager: ThreadManager |     private lateinit var threadManager: ThreadManager | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -59,7 +59,7 @@ public class ImageEditViewModel extends AndroidViewModel { | |||||||
|     public ImageEditViewModel(final Application application) { |     public ImageEditViewModel(final Application application) { | ||||||
|         super(application); |         super(application); | ||||||
|         sessionId = LocalDateTime.now().format(SIMPLE_DATE_FORMAT); |         sessionId = LocalDateTime.now().format(SIMPLE_DATE_FORMAT); | ||||||
|         outputDir = DownloadUtils.getImageEditDir(sessionId); |         outputDir = DownloadUtils.getImageEditDir(sessionId, application); | ||||||
|         destinationFile = outputDir.createFile(MIME_TYPE, RESULT + ".jpg"); |         destinationFile = outputDir.createFile(MIME_TYPE, RESULT + ".jpg"); | ||||||
|         destinationUri = destinationFile.getUri(); |         destinationUri = destinationFile.getUri(); | ||||||
|         cropDestinationUri = outputDir.createFile(MIME_TYPE, CROP + ".jpg").getUri(); |         cropDestinationUri = outputDir.createFile(MIME_TYPE, CROP + ".jpg").getUri(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user