Skip to content

Commit

Permalink
Move save location to Documents/Novel2Go
Browse files Browse the repository at this point in the history
Ask for permission to storage.
If granted save in Documents/Novel2Go otherwise fall back to Android/data/com.kaiserpudding.Novel2Go/files/Documents
Relies on legacy external storage
  • Loading branch information
XiangRongLin committed Nov 30, 2019
1 parent 4d68efb commit 14a9b1f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
all {
buildConfigField("int", "EXTERNAL_STORAGE_PERMISSION", "1")
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
android:requestLegacyExternalStorage="true"
tools:ignore="GoogleAppIndexingWarning"
tools:targetApi="q">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package com.kaiserpudding.novel2go.download

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.kaiserpudding.novel2go.BuildConfig.EXTERNAL_STORAGE_PERMISSION
import com.kaiserpudding.novel2go.R
import com.kaiserpudding.novel2go.download.service.DownloadService

Expand All @@ -26,12 +30,26 @@ class NewDownloadFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

ActivityCompat.requestPermissions(
activity!!,
arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE),
EXTERNAL_STORAGE_PERMISSION
)

val button = view.findViewById<Button>(R.id.button_start)
button.setOnClickListener {
val urlEditText = view.findViewById<EditText>(R.id.edit_text_url)
val url = urlEditText.text.toString()
val storagePermission = ContextCompat.checkSelfPermission(
context!!, android.Manifest.permission.WRITE_EXTERNAL_STORAGE
) != PackageManager.PERMISSION_DENIED

Intent(context, DownloadService::class.java).also {
it.putExtra(DownloadService.DOWNLOAD_URL_INTENT_EXTRA, url)
it.putExtra(
DownloadService.STORAGE_PERMISSION_INTENT_EXTRA,
storagePermission
)
activity?.startService(it)
}
listener!!.onStartDownload()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ class DownloadService : IntentService("DownloadService") {
override fun onHandleIntent(intent: Intent?) {
scope.launch {
val url = intent!!.getStringExtra(DOWNLOAD_URL_INTENT_EXTRA)
val storagePermission = intent.getBooleanExtra(STORAGE_PERMISSION_INTENT_EXTRA, false)

if (DEBUG) Log.d(LOG_TAG, "onHandleIntent() called with $url")

val file = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)
val file =
if (storagePermission) File("/storage/emulated/0/Documents/Novel2Go")
else baseContext.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS)

val fileName = extractor.extractSingle(url, file!!)
insertDownload(
Download(
Expand All @@ -46,5 +50,6 @@ class DownloadService : IntentService("DownloadService") {
companion object {
private const val LOG_TAG = "DownloadService"
const val DOWNLOAD_URL_INTENT_EXTRA = "download_url"
const val STORAGE_PERMISSION_INTENT_EXTRA = "external_storage_persmisison"
}
}

0 comments on commit 14a9b1f

Please sign in to comment.