From c9d51365b9c9d7c7e292a6240d657362b02d0177 Mon Sep 17 00:00:00 2001 From: professional-lalit Date: Tue, 4 Feb 2020 13:53:45 +0530 Subject: [PATCH] files retrieved via android SAF --- .idea/sonarIssues.xml | 55 ++++ app/build.gradle | 8 +- app/src/main/AndroidManifest.xml | 12 +- .../lib/activities/FileChooserActivity.kt | 39 ++- .../lib/activities/HomeActivity.kt | 63 ++++ .../lib/activities/ui/main/PageViewModel.kt | 44 +++ .../activities/ui/main/PlaceholderFragment.kt | 298 ++++++++++++++++++ .../ui/main/SectionsPagerAdapter.kt | 36 +++ .../lib/adapters/LibFileAdapter.kt | 42 +++ .../com/filehandling/lib/models/LibFile.kt | 23 ++ .../com/filehandling/lib/utils/Extensions.kt | 9 + .../lib/utils/FileIconProvider.kt | 59 ++++ .../lib/viewholders/FileViewHolder.kt | 63 +--- .../lib/viewholders/LibFileViewHolder.kt | 54 ++++ app/src/main/res/layout/activity_home.xml | 24 ++ app/src/main/res/layout/fragment_home.xml | 15 + app/src/main/res/values-w820dp/dimens.xml | 6 + app/src/main/res/values/dimens.xml | 2 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 9 + 20 files changed, 779 insertions(+), 85 deletions(-) create mode 100644 app/src/main/java/com/filehandling/lib/activities/HomeActivity.kt create mode 100644 app/src/main/java/com/filehandling/lib/activities/ui/main/PageViewModel.kt create mode 100644 app/src/main/java/com/filehandling/lib/activities/ui/main/PlaceholderFragment.kt create mode 100644 app/src/main/java/com/filehandling/lib/activities/ui/main/SectionsPagerAdapter.kt create mode 100644 app/src/main/java/com/filehandling/lib/adapters/LibFileAdapter.kt create mode 100644 app/src/main/java/com/filehandling/lib/models/LibFile.kt create mode 100644 app/src/main/java/com/filehandling/lib/utils/Extensions.kt create mode 100644 app/src/main/java/com/filehandling/lib/utils/FileIconProvider.kt create mode 100644 app/src/main/java/com/filehandling/lib/viewholders/LibFileViewHolder.kt create mode 100644 app/src/main/res/layout/activity_home.xml create mode 100644 app/src/main/res/layout/fragment_home.xml create mode 100644 app/src/main/res/values-w820dp/dimens.xml diff --git a/.idea/sonarIssues.xml b/.idea/sonarIssues.xml index bc7e6a4..22eea94 100644 --- a/.idea/sonarIssues.xml +++ b/.idea/sonarIssues.xml @@ -43,16 +43,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + @@ -68,21 +93,41 @@ + + + + + + + + + + + + + + + + + + + + @@ -113,6 +158,11 @@ + + + + + @@ -123,6 +173,11 @@ + + + + + diff --git a/app/build.gradle b/app/build.gradle index 8e82b76..58dab04 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,5 +1,5 @@ -apply plugin: 'com.android.library' -//apply plugin: 'com.android.application' +//apply plugin: 'com.android.library' +apply plugin: 'com.android.application' apply plugin: 'kotlin-android' @@ -10,7 +10,7 @@ android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { -// applicationId "com.filehandling.lib" + applicationId "com.filehandling.lib" minSdkVersion 21 targetSdkVersion 29 versionCode 1 @@ -43,6 +43,8 @@ dependencies { implementation 'androidx.core:core-ktx:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' + implementation 'com.google.android.material:material:1.0.0' + implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ac93702..8ce9439 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,19 +12,19 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - - + - + + - - + android:exported="true"> + if (currentDir.isDirectory) + addFragment(currentDir) + }) + } + private fun getPerms() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED @@ -65,18 +76,16 @@ class FileChooserActivity : AppCompatActivity() { ), REQ_FILE_ACCESS ) } else { - observeCurrentDir() + setRootFileDir() } } else { - observeCurrentDir() + setRootFileDir() } } - private fun observeCurrentDir() { - mFolderViewModel.mCurrentDir.observe(this, Observer { currentDir -> - if (currentDir.isDirectory) - addFragment(currentDir) - }) + + private fun setRootFileDir() { + mFolderViewModel.mCurrentDir.postValue(CustomFileModel(rootDir.parentFile, rootDir.name)) } @@ -89,7 +98,7 @@ class FileChooserActivity : AppCompatActivity() { if (permissions.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED ) { - observeCurrentDir() + setRootFileDir() } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions( diff --git a/app/src/main/java/com/filehandling/lib/activities/HomeActivity.kt b/app/src/main/java/com/filehandling/lib/activities/HomeActivity.kt new file mode 100644 index 0000000..c099770 --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/activities/HomeActivity.kt @@ -0,0 +1,63 @@ +package com.filehandling.lib.activities + +import android.app.Activity +import android.content.Intent +import android.os.Bundle +import com.google.android.material.floatingactionbutton.FloatingActionButton +import com.google.android.material.snackbar.Snackbar +import com.google.android.material.tabs.TabLayout +import androidx.viewpager.widget.ViewPager +import androidx.appcompat.app.AppCompatActivity +import android.view.Menu +import android.view.MenuItem +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProvider +import com.filehandling.lib.FolderViewModel +import com.filehandling.lib.R +import com.filehandling.lib.activities.ui.main.PageViewModel +import com.filehandling.lib.activities.ui.main.SectionsPagerAdapter + +class HomeActivity : AppCompatActivity() { + + lateinit var mPageViewModel: PageViewModel + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_home) + + mPageViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application) + .create(PageViewModel::class.java) + + initActionBar() + val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager) + val viewPager: ViewPager = findViewById(R.id.view_pager) + viewPager.adapter = sectionsPagerAdapter + val tabs: TabLayout = findViewById(R.id.tabs) + tabs.setupWithViewPager(viewPager) + + } + + private fun initActionBar() { + supportActionBar?.apply { + setHomeAsUpIndicator(R.drawable.ic_left_arrow) + setHomeButtonEnabled(true) + setDisplayHomeAsUpEnabled(true) + setDisplayShowCustomEnabled(true) + setDisplayShowTitleEnabled(true) + } + } + + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + if (item.itemId == android.R.id.home) { + val bundle = Bundle() + bundle.putSerializable("file-list", mPageViewModel.fileList.value) + val intent = Intent() + intent.putExtra("file-bundle", bundle) + setResult(Activity.RESULT_OK, intent) + finish() + return true + } + return super.onOptionsItemSelected(item) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/filehandling/lib/activities/ui/main/PageViewModel.kt b/app/src/main/java/com/filehandling/lib/activities/ui/main/PageViewModel.kt new file mode 100644 index 0000000..afa0f34 --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/activities/ui/main/PageViewModel.kt @@ -0,0 +1,44 @@ +package com.filehandling.lib.activities.ui.main + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.Transformations +import androidx.lifecycle.Transformations.map +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import com.filehandling.lib.models.CustomFileModel +import com.filehandling.lib.models.LibFile + +class PageViewModel : ViewModel() { + + private val _index = MutableLiveData() + val text: LiveData = map(_index) { + "Hello world from section: $it" + } + + fun setIndex(index: Int) { + _index.value = index + } + + private val mChosenFileList = MutableLiveData>() + val fileList: LiveData> = mChosenFileList + + fun addFile(file: LibFile) { + if (mChosenFileList.value == null) { + mChosenFileList.value = ArrayList() + } + if (!mChosenFileList.value!!.contains(file)) { + val list = mChosenFileList.value + list!!.add(file) + mChosenFileList.postValue(list) + } + } + + fun removeFile(file: LibFile) { + if (mChosenFileList.value != null && mChosenFileList.value!!.contains(file)) { + val list = mChosenFileList.value + list!!.remove(file) + mChosenFileList.postValue(list) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/filehandling/lib/activities/ui/main/PlaceholderFragment.kt b/app/src/main/java/com/filehandling/lib/activities/ui/main/PlaceholderFragment.kt new file mode 100644 index 0000000..70670df --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/activities/ui/main/PlaceholderFragment.kt @@ -0,0 +1,298 @@ +package com.filehandling.lib.activities.ui.main + +import android.content.ContentUris +import android.database.Cursor +import android.net.Uri +import android.os.Bundle +import android.provider.MediaStore +import android.util.Log +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.lifecycle.ViewModelProviders +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import com.filehandling.lib.R +import com.filehandling.lib.activities.HomeActivity +import com.filehandling.lib.adapters.LibFileAdapter +import com.filehandling.lib.fragments.DirectoryFragment +import com.filehandling.lib.models.LibFile +import com.filehandling.lib.utils.getClassName + + +/** + * A placeholder fragment containing a simple view. + */ +class PlaceholderFragment : Fragment() { + + + private lateinit var pageViewModel: PageViewModel + + enum class FragmentIndices(var index: Int) { + IMAGES(1), VIDEOS(2), AUDIOS(3), DOCUMENTS(4); + + fun getValue(): Int { + return index + } + } + + private lateinit var mAdapter: LibFileAdapter + private lateinit var recyclerFiles: RecyclerView + private val mFileList = arrayListOf() + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + pageViewModel = (activity as HomeActivity).mPageViewModel + } + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val root = inflater.inflate(R.layout.fragment_home, container, false) + return root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + setAdapter(view) + when (arguments?.getInt(ARG_SECTION_NUMBER)) { + FragmentIndices.IMAGES.getValue() -> { + getImageFiles(null) + } + FragmentIndices.VIDEOS.getValue() -> { + getVideoFiles(null) + } + FragmentIndices.AUDIOS.getValue() -> { + getAudioFiles(null) + } + FragmentIndices.DOCUMENTS.getValue() -> { + getDocFiles(null) + } + } + } + + private fun setAdapter(view: View) { + recyclerFiles = view.findViewById(R.id.recycler_files) + recyclerFiles.layoutManager = LinearLayoutManager(context) + mAdapter = LibFileAdapter(mFileList) { file, ops -> + + when (ops.ordinal) { + DirectoryFragment.Ops.ADD.ordinal -> { + file.isSelected = true + pageViewModel.addFile(file) + } + DirectoryFragment.Ops.REMOVE.ordinal -> { + file.isSelected = false + pageViewModel.removeFile(file) + } + } + + mAdapter.notifyItemChanged(mFileList.indexOf(file)) + } + recyclerFiles.adapter = mAdapter + } + + + private fun getImageFiles(filter: String?) { + val projection = arrayOf( + MediaStore.Images.Media._ID, + MediaStore.Images.Media.DISPLAY_NAME, + MediaStore.Images.Media.SIZE + ) + val query = activity!!.contentResolver.query( + MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + projection, + null, + null, + null + ) + query?.use { cursor -> + // Cache column indices. + val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) + val nameColumn = + cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME) + val sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE) + + while (cursor.moveToNext()) { + // Get values of columns for a given video. + val id = cursor.getLong(idColumn) + val name = cursor.getString(nameColumn) + val size = cursor.getInt(sizeColumn) + + val contentUri: Uri = ContentUris.withAppendedId( + MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + id + ) + + val libFile = LibFile(contentUri, false, name, 0, size) + libFile.isSelected = checkIfSelected(libFile) + mFileList += libFile + } + } + mAdapter.notifyDataSetChanged() + } + + private fun checkIfSelected(file: LibFile): Boolean { + if (pageViewModel.fileList.value != null) + return pageViewModel.fileList.value!!.contains(file) + return false + } + + private fun getVideoFiles(filter: String?) { + val projection = arrayOf( + MediaStore.Video.Media._ID, + MediaStore.Video.Media.DISPLAY_NAME, + MediaStore.Video.Media.SIZE + ) + val query = activity!!.contentResolver.query( + MediaStore.Video.Media.EXTERNAL_CONTENT_URI, + projection, + null, + null, + null + ) + query?.use { cursor -> + // Cache column indices. + val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID) + val nameColumn = + cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME) + val sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE) + + while (cursor.moveToNext()) { + // Get values of columns for a given video. + val id = cursor.getLong(idColumn) + val name = cursor.getString(nameColumn) + val size = cursor.getInt(sizeColumn) + + val contentUri: Uri = ContentUris.withAppendedId( + MediaStore.Video.Media.EXTERNAL_CONTENT_URI, + id + ) + Log.d(getClassName(), " FILE URI : $contentUri") + mFileList += LibFile(contentUri, false, name, 0, size) + } + } + mAdapter.notifyDataSetChanged() + } + + private fun getAudioFiles(filter: String?) { + val projection = arrayOf( + MediaStore.Audio.Media._ID, + MediaStore.Audio.Media.DISPLAY_NAME, + MediaStore.Audio.Media.SIZE + ) + val query = activity!!.contentResolver.query( + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, + projection, + null, + null, + null + ) + query?.use { cursor -> + // Cache column indices. + val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID) + val nameColumn = + cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME) + val sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE) + + while (cursor.moveToNext()) { + // Get values of columns for a given video. + val id = cursor.getLong(idColumn) + val name = cursor.getString(nameColumn) + val size = cursor.getInt(sizeColumn) + + val contentUri: Uri = ContentUris.withAppendedId( + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, + id + ) + Log.d(getClassName(), " FILE URI : $contentUri") + mFileList += LibFile(contentUri, false, name, 0, size) + } + } + mAdapter.notifyDataSetChanged() + } + + private fun getDocFiles(filter: String?) { + val projection = arrayOf( + MediaStore.Files.FileColumns._ID, + MediaStore.Files.FileColumns.MIME_TYPE, + MediaStore.Files.FileColumns.DATE_ADDED, + MediaStore.Files.FileColumns.DATE_MODIFIED, + MediaStore.Files.FileColumns.DISPLAY_NAME, + MediaStore.Files.FileColumns.TITLE, + MediaStore.Files.FileColumns.SIZE + ) + + val mimeTypes = + "'application/pdf','text/plain','application/msword','application/powerpoint'" + + val whereClause = + MediaStore.Files.FileColumns.MIME_TYPE + " IN (" + mimeTypes + ")" + val orderBy = MediaStore.Files.FileColumns.SIZE + " DESC" + val query: Cursor = activity!!.contentResolver.query( + MediaStore.Files.getContentUri("external"), + projection, + whereClause, + null, + orderBy + )!! + + query.use { cursor -> + // Cache column indices. + val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID) + val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE) + val mimeColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE) + val sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE) + + while (cursor.moveToNext()) { + // Get values of columns for a given video. + val id = cursor.getLong(idColumn) + var name = cursor.getString(nameColumn) + var mime = cursor.getString(mimeColumn) + val size = cursor.getInt(sizeColumn) + + val contentUri: Uri = ContentUris.withAppendedId( + MediaStore.Files.getContentUri("external"), + id + ) + Log.d(getClassName(), " FILE URI : $contentUri") + name = name ?: "" + + if (!mime.isNullOrEmpty()) + name += ".$mime".replace("application/", "") + .replace("video/", "") + .replace("image/", "") + .replace("text/", "") + + mFileList += LibFile(contentUri, false, name, 0, size) + } + } + query.close() + + mAdapter.notifyDataSetChanged() + } + + companion object { + /** + * The fragment argument representing the section number for this + * fragment. + */ + private const val ARG_SECTION_NUMBER = "section_number" + + /** + * Returns a new instance of this fragment for the given section + * number. + */ + @JvmStatic + fun newInstance(sectionNumber: Int): PlaceholderFragment { + return PlaceholderFragment().apply { + arguments = Bundle().apply { + putInt(ARG_SECTION_NUMBER, sectionNumber) + } + } + } + } + +} diff --git a/app/src/main/java/com/filehandling/lib/activities/ui/main/SectionsPagerAdapter.kt b/app/src/main/java/com/filehandling/lib/activities/ui/main/SectionsPagerAdapter.kt new file mode 100644 index 0000000..59fd251 --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/activities/ui/main/SectionsPagerAdapter.kt @@ -0,0 +1,36 @@ +package com.filehandling.lib.activities.ui.main + +import android.content.Context +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager +import androidx.fragment.app.FragmentPagerAdapter +import com.filehandling.lib.R + +private val TAB_TITLES = arrayOf( + "Images", + "Videos", + "Audios", + "Documents" +) + +/** + * A [FragmentPagerAdapter] that returns a fragment corresponding to + * one of the sections/tabs/pages. + */ +class SectionsPagerAdapter(private val context: Context, fm: FragmentManager) : + FragmentPagerAdapter(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { + + override fun getItem(position: Int): Fragment { + // getItem is called to instantiate the fragment for the given page. + // Return a PlaceholderFragment (defined as a static inner class below). + return PlaceholderFragment.newInstance(position + 1) + } + + override fun getPageTitle(position: Int): CharSequence? { + return TAB_TITLES[position] + } + + override fun getCount(): Int { + return TAB_TITLES.size + } +} \ No newline at end of file diff --git a/app/src/main/java/com/filehandling/lib/adapters/LibFileAdapter.kt b/app/src/main/java/com/filehandling/lib/adapters/LibFileAdapter.kt new file mode 100644 index 0000000..d8e7607 --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/adapters/LibFileAdapter.kt @@ -0,0 +1,42 @@ +package com.filehandling.lib.adapters + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.filehandling.lib.R +import com.filehandling.lib.fragments.DirectoryFragment +import com.filehandling.lib.models.CustomFileModel +import com.filehandling.lib.models.LibFile +import com.filehandling.lib.viewholders.FileViewHolder +import com.filehandling.lib.viewholders.LibFileViewHolder +import com.filehandling.lib.viewholders.ViewHolder + +/** + * This file is created by Lalit N. Hajare on 2/4/2020. + */ +class LibFileAdapter( + private var mList: ArrayList, + private var mCallback: (LibFile, DirectoryFragment.Ops) -> Unit +) : + RecyclerView.Adapter() { + + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return LibFileViewHolder( + LayoutInflater.from(parent.context).inflate( + R.layout.item_file_layout, + null, + false + ), mCallback + ) + } + + override fun getItemCount(): Int { + return mList.size + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.onBindView(mList[position]) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/filehandling/lib/models/LibFile.kt b/app/src/main/java/com/filehandling/lib/models/LibFile.kt new file mode 100644 index 0000000..e52eef8 --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/models/LibFile.kt @@ -0,0 +1,23 @@ +package com.filehandling.lib.models + +import android.net.Uri + +/** + * This file is created by Lalit N. Hajare on 2/4/2020. + */ + +data class LibFile( + val uri: Uri, + var isSelected: Boolean, + val name: String, + val duration: Int, + val size: Int +) { + override fun equals(other: Any?): Boolean { + return this.uri == (other as LibFile).uri + } + + override fun hashCode(): Int { + return uri.hashCode() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/filehandling/lib/utils/Extensions.kt b/app/src/main/java/com/filehandling/lib/utils/Extensions.kt new file mode 100644 index 0000000..ee251fa --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/utils/Extensions.kt @@ -0,0 +1,9 @@ +package com.filehandling.lib.utils + +/** + * This file is created by Lalit N. Hajare on 2/4/2020. + */ + +fun Any?.getClassName(): String { + return this!!::class.java.simpleName +} \ No newline at end of file diff --git a/app/src/main/java/com/filehandling/lib/utils/FileIconProvider.kt b/app/src/main/java/com/filehandling/lib/utils/FileIconProvider.kt new file mode 100644 index 0000000..86b0b3c --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/utils/FileIconProvider.kt @@ -0,0 +1,59 @@ +package com.filehandling.lib.utils + +import android.content.Context +import android.graphics.drawable.Drawable +import androidx.core.content.ContextCompat +import com.filehandling.lib.R + +/** + * This file is created by Lalit N. Hajare on 2/4/2020. + */ + +object FileIconProvider { + + fun getFileIcon(context: Context, name: String): Drawable { + if (name.endsWith(".txt") || name.endsWith(".plain")) { + return ContextCompat.getDrawable( + context, + R.drawable.ic_text + )!! + } else if (name.endsWith(".ppt")) { + return ContextCompat.getDrawable( + context, + R.drawable.ic_ppt + )!! + } else if (name.endsWith(".pdf")) { + return ContextCompat.getDrawable( + context, + R.drawable.ic_pdf + )!! + } else if (name.endsWith(".cfg") || name.endsWith(".config")) { + return ContextCompat.getDrawable( + context, + R.drawable.ic_config + )!! + } else if (name.endsWith(".mp3") || name.endsWith(".wav") || name.endsWith(".mp4") + || name.endsWith(".3gp") + ) { + return ContextCompat.getDrawable( + context, + R.drawable.ic_music_note + )!! + } else if (name.endsWith(".png") || name.endsWith(".jpeg") || name.endsWith(".jpg") + || name.endsWith(".giff") + ) { + return ContextCompat.getDrawable( + context, + R.drawable.ic_photo + )!! + + } else { + return ContextCompat.getDrawable( + context, + R.drawable.ic_unknown + )!! + + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/filehandling/lib/viewholders/FileViewHolder.kt b/app/src/main/java/com/filehandling/lib/viewholders/FileViewHolder.kt index 2cc0577..05bcf12 100644 --- a/app/src/main/java/com/filehandling/lib/viewholders/FileViewHolder.kt +++ b/app/src/main/java/com/filehandling/lib/viewholders/FileViewHolder.kt @@ -8,6 +8,7 @@ import com.filehandling.lib.R import com.filehandling.lib.fragments.DirectoryFragment import com.filehandling.lib.models.CustomFileModel import com.filehandling.lib.utils.DateFormatter +import com.filehandling.lib.utils.FileIconProvider import java.util.* /** @@ -33,67 +34,7 @@ class FileViewHolder( imgTick.visibility = View.INVISIBLE } - if (file.name.endsWith(".txt")) { - imgFile.setImageDrawable( - ContextCompat.getDrawable( - itemView.context, - R.drawable.ic_text - ) - ) - } else if (file.name.endsWith(".ppt")) { - imgFile.setImageDrawable( - ContextCompat.getDrawable( - itemView.context, - R.drawable.ic_ppt - ) - ) - } else if (file.name.endsWith(".pdf")) { - imgFile.setImageDrawable( - ContextCompat.getDrawable( - itemView.context, - R.drawable.ic_pdf - ) - ) - } else if (file.name.endsWith(".cfg") || file.name.endsWith(".config")) { - imgFile.setImageDrawable( - ContextCompat.getDrawable( - itemView.context, - R.drawable.ic_config - ) - ) - } else if (file.name.endsWith(".mp3") || file.name.endsWith(".wav") || file.name.endsWith(".mp4") - || file.name.endsWith(".3gp") - ) { - imgFile.setImageDrawable( - ContextCompat.getDrawable( - itemView.context, - R.drawable.ic_music_note - ) - ) - } else if (file.name.endsWith(".png") || file.name.endsWith(".jpeg") || file.name.endsWith(".jpg") - || file.name.endsWith(".giff") - ) { - imgFile.setImageDrawable( - ContextCompat.getDrawable( - itemView.context, - R.drawable.ic_photo - ) - ) - } else if (!file.isDirectory) { - imgFile.setImageDrawable( - ContextCompat.getDrawable( - itemView.context, - R.drawable.ic_unknown - ) - ) - }else{ - imgFile.setImageDrawable( - ContextCompat.getDrawable( - itemView.context, - R.drawable.ic_folder - ) - ) - } + imgFile.setImageDrawable(FileIconProvider.getFileIcon(itemView.context, file.name)) if (!file.name.isNullOrEmpty()) { diff --git a/app/src/main/java/com/filehandling/lib/viewholders/LibFileViewHolder.kt b/app/src/main/java/com/filehandling/lib/viewholders/LibFileViewHolder.kt new file mode 100644 index 0000000..3203f5b --- /dev/null +++ b/app/src/main/java/com/filehandling/lib/viewholders/LibFileViewHolder.kt @@ -0,0 +1,54 @@ +package com.filehandling.lib.viewholders + +/** + * This file is created by Lalit N. Hajare on 2/4/2020. + */ + +import android.view.View +import android.widget.ImageView +import android.widget.TextView +import androidx.core.content.ContextCompat +import com.filehandling.lib.R +import com.filehandling.lib.fragments.DirectoryFragment +import com.filehandling.lib.models.LibFile +import com.filehandling.lib.utils.DateFormatter +import com.filehandling.lib.utils.FileIconProvider +import java.util.* + +class LibFileViewHolder( + itemView: View, + private var mCallback: (LibFile, DirectoryFragment.Ops) -> Unit +) : + ViewHolder(itemView) { + + private val imgFile = itemView.findViewById(R.id.img_file) + private val txtFileName = itemView.findViewById(R.id.txt_file_name) + private val txtFileDesc = itemView.findViewById(R.id.txt_file_desc) + private val imgTick = itemView.findViewById(R.id.img_tick) + + override fun onBindView(model: Any) { + val file = model as LibFile + + if (file.isSelected) { + imgTick.visibility = View.VISIBLE + } else { + imgTick.visibility = View.INVISIBLE + } + + imgFile.setImageDrawable(FileIconProvider.getFileIcon(itemView.context, file.name)) + + + if (!file.name.isNullOrEmpty()) { + txtFileName.text = file.name + } + txtFileDesc.text = file.size.toString() + + itemView.setOnClickListener { + if (imgTick.visibility == View.INVISIBLE || imgTick.visibility == View.GONE) + mCallback(file, DirectoryFragment.Ops.ADD) + else + mCallback(file, DirectoryFragment.Ops.REMOVE) + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_home.xml b/app/src/main/res/layout/activity_home.xml new file mode 100644 index 0000000..e023d20 --- /dev/null +++ b/app/src/main/res/layout/activity_home.xml @@ -0,0 +1,24 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml new file mode 100644 index 0000000..2b5a5ab --- /dev/null +++ b/app/src/main/res/layout/fragment_home.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-w820dp/dimens.xml b/app/src/main/res/values-w820dp/dimens.xml new file mode 100644 index 0000000..63fc816 --- /dev/null +++ b/app/src/main/res/values-w820dp/dimens.xml @@ -0,0 +1,6 @@ + + + 64dp + diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 0de42fa..b7b6756 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -35,5 +35,7 @@ 8dp 176dp 16dp + 16dp + 8dp \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5a68001..d105eb6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,4 +3,7 @@ Hello blank fragment + HomeActivity + Tab 1 + Tab 2 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 3a1505c..6f8607c 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -44,5 +44,14 @@ @font/belgrano_regular + + +