Skip to content

Commit

Permalink
files retrieved via android SAF
Browse files Browse the repository at this point in the history
  • Loading branch information
professional-lalit committed Feb 4, 2020
1 parent a261853 commit c9d5136
Show file tree
Hide file tree
Showing 20 changed files with 779 additions and 85 deletions.
55 changes: 55 additions & 0 deletions .idea/sonarIssues.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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
Expand Down Expand Up @@ -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'
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".activities.TestActivity">
<activity
android:name=".activities.HomeActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>

<activity android:name=".activities.TestActivity">
</activity>
<activity
android:name=".activities.FileChooserActivity"
android:exported="true">

</activity>
android:exported="true"></activity>

<provider
android:name=".GenericFileProvider"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.filehandling.lib.activities

import android.annotation.SuppressLint
import android.app.Activity
import android.content.ContentResolver
import android.content.ContentUris
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.provider.MediaStore
import android.util.Log
import android.view.MenuItem
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -25,22 +31,17 @@ class FileChooserActivity : AppCompatActivity() {
private lateinit var rootDir: File
lateinit var mFolderViewModel: FolderViewModel

@SuppressLint("NewApi")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_file_chooser)

rootDir = Environment.getExternalStorageDirectory()
initActionBar()
setViews()
getPerms()
}

private fun setViews() {
fileContainer = findViewById(R.id.file_container)
mFolderViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application)
.create(FolderViewModel::class.java)
mFolderViewModel.mCurrentDir.postValue(CustomFileModel(rootDir.parentFile, rootDir.name))
}


private fun initActionBar() {
supportActionBar?.apply {
Expand All @@ -53,6 +54,16 @@ class FileChooserActivity : AppCompatActivity() {
}


private fun setViews() {
fileContainer = findViewById(R.id.file_container)
mFolderViewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(application)
.create(FolderViewModel::class.java)
mFolderViewModel.mCurrentDir.observe(this, Observer { currentDir ->
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
Expand All @@ -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))
}


Expand All @@ -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(
Expand Down
63 changes: 63 additions & 0 deletions app/src/main/java/com/filehandling/lib/activities/HomeActivity.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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<Int>()
val text: LiveData<String> = map(_index) {
"Hello world from section: $it"
}

fun setIndex(index: Int) {
_index.value = index
}

private val mChosenFileList = MutableLiveData<ArrayList<LibFile>>()
val fileList: LiveData<ArrayList<LibFile>> = 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)
}
}
}
Loading

0 comments on commit c9d5136

Please sign in to comment.