Skip to content

Commit

Permalink
Merge pull request #81 from pravinyo/bug_play_downloaded_file_if_there
Browse files Browse the repository at this point in the history
Bug play downloaded file if there
  • Loading branch information
pravinyo authored Apr 25, 2020
2 parents 664e38d + e3f083b commit cefcf21
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 244 deletions.
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
<application
android:name=".ApplicationController"
android:allowBackup="true"
android:requestLegacyExternalStorage="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:targetApi="q">

<activity android:name=".presentation.MainActivity"
android:configChanges="uiMode"
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/allsoftdroid/audiobook/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.allsoftdroid.audiobook.di

import android.app.Activity
import android.content.Context
import com.allsoftdroid.audiobook.domain.usecase.GetLastPlayedUsecase
import com.allsoftdroid.audiobook.feature_downloader.data.Downloader
Expand Down Expand Up @@ -58,7 +59,7 @@ object AppModule {
}

single {
(ctx:Context) ->
(ctx:Activity) ->
Downloader(
ctx,
get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class MainActivity : BaseActivity() {
"bookName" to lastPlayedTrack.bookName,
"trackNumber" to lastPlayedTrack.position)

connectionListener.value?.let { connected->
if(connected){
connectionListener.value?.let { connectedEvent->
if(connectedEvent.peekContent()){
Navigation.findNavController(this,R.id.navHostFragment)
.navigate(com.allsoftdroid.feature_book.R.id.action_AudioBookListFragment_to_AudioBookDetailsFragment,bundle)
mainActivityViewModel.clearSharedPref()
Expand All @@ -121,7 +121,7 @@ class MainActivity : BaseActivity() {
mainActivityViewModel.bindAudioService()

connectionListener.observe(this, Observer {isConnected ->
showNetworkMessage(isConnected)
showNetworkMessage(isConnected.peekContent())
})

Timber.d("Main Activity start")
Expand All @@ -134,11 +134,15 @@ class MainActivity : BaseActivity() {

mainActivityViewModel.playerEvent.observeForever {
it.getContentIfNotHandled()?.let {audioPlayerEvent ->
connectionListener.value?.let { isConnected ->
Timber.d("Event is new and is being handled")
if(!isConnected) Toast.makeText(this,"Please Connect to Internet",Toast.LENGTH_SHORT).show()
performAction(audioPlayerEvent)
}

// connectionListener.value?.let { isConnectedEvent ->
// isConnectedEvent.getContentIfNotHandled()?.let {isConnected ->
// if(!isConnected) Toast.makeText(this,"Please Connect to Internet",Toast.LENGTH_SHORT).show()
// }
// }

Timber.d("Event is new and is being handled")
performAction(audioPlayerEvent)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.allsoftdroid.common.base.network

import android.app.Application
import android.content.Context
import android.os.Environment
import com.allsoftdroid.common.R

class ArchiveUtils {
companion object{

Expand All @@ -17,6 +22,21 @@ class ArchiveUtils {

fun getThumbnail(imageId: String?) = "$BASE_IMAGE_URL/$imageId/"

fun getLocalSavePath(path:String) = "/AudioBooks/$path/"
fun getLocalSavePath(bookId:String) = "/AudioBooks/$bookId/"

fun setDownloadsRootFolder(context: Application, root:String){
val sharedPref = context.getSharedPreferences(context.getString(R.string.downloads_path),Context.MODE_PRIVATE) ?: return
with (sharedPref.edit()) {
putString(context.getString(R.string.downloads_root_directory_key), root)
commit()
}
}

fun getDownloadsRootFolder(context: Application): String {
val default = Environment.DIRECTORY_DOWNLOADS
val sharedPref = context.getSharedPreferences(context.getString(R.string.downloads_path),Context.MODE_PRIVATE)

return sharedPref.getString(context.getString(R.string.downloads_root_directory_key),default)?:default
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import android.net.NetworkRequest
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.lifecycle.LiveData
import com.allsoftdroid.common.base.extension.Event

class ConnectionLiveData(private val context: Context) : LiveData<Boolean>(){
class ConnectionLiveData(private val context: Context) : LiveData<Event<Boolean>>(){

private var intentFilter = IntentFilter(CONNECTIVITY_ACTION)
private var connectivityManager = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
Expand Down Expand Up @@ -64,17 +65,17 @@ class ConnectionLiveData(private val context: Context) : LiveData<Boolean>(){

fun updateConnection() {
val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
postValue(activeNetwork?.isConnectedOrConnecting == true)
postValue(Event(activeNetwork?.isConnectedOrConnecting == true))
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class NetworkCallback(private val liveData : ConnectionLiveData) : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network?) {
liveData.postValue(true)
liveData.postValue(Event(true))
}

override fun onLost(network: Network?) {
liveData.postValue(false)
liveData.postValue(Event(false))
}
}
}
2 changes: 2 additions & 0 deletions common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<string name="app_name">common</string>
<string name="no_connection_found">No Internet Connection!</string>
<string name="downloads_root_directory_key">dowloads_root_directory_for_downloads</string>
<string name="downloads_path">local_downloads_path</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,15 @@ class BookDetailsViewModel(
track?.let {
val album = it.trackAlbum?:getMetadataUsecase.getBookIdentifier()
val desc = "Downloading: chapter ${track.trackNumber} from $album"
val id = getMetadataUsecase.getBookIdentifier()
download(
Download(
bookId = getMetadataUsecase.getBookIdentifier(),
url = ArchiveUtils.getRemoteFilePath(filename = track.filename,identifier = getMetadataUsecase.getBookIdentifier()),
bookId = id,
url = ArchiveUtils.getRemoteFilePath(filename = track.filename,identifier = id),
name = track.filename,
chapter = track.title?:"",
description = desc,
subPath = ArchiveUtils.getLocalSavePath(album),
subPath = ArchiveUtils.getLocalSavePath(id),
chapterIndex = track.trackNumber?:0
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ fun setTrackDownloadStatus(imageView: ImageView,item :AudioBookTrackDomainModel?
Timber.d("Download image icon updated")
}

@BindingAdapter("trackDownloadProgress")
fun setTrackDownloadProgress(view: ProgressBar, holder:TrackItemViewHolder?){

holder?.let {
Timber.d("Download event received with progress for $holder")
}
}

/**
Handle visibility of progress bar
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.allsoftdroid.audiobook.feature_downloader.data;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.ClipData;
import android.content.ClipboardManager;
Expand All @@ -22,6 +23,7 @@
import com.allsoftdroid.audiobook.feature_downloader.utils.DownloadObserver;
import com.allsoftdroid.audiobook.feature_downloader.utils.downloadUtils;
import com.allsoftdroid.common.base.extension.Event;
import com.allsoftdroid.common.base.network.ArchiveUtils;
import com.allsoftdroid.common.base.store.downloader.Cancel;
import com.allsoftdroid.common.base.store.downloader.Cancelled;
import com.allsoftdroid.common.base.store.downloader.Download;
Expand Down Expand Up @@ -49,20 +51,20 @@

public class Downloader implements IDownloader {

private Context mContext;
private Activity mContext;
private DownloadManager downloadManager;
private DownloadEventStore mDownloadEventStore;
private static HashMap<String,Integer> keyStrokeCount = new HashMap<>();
private DownloadObserver mDownloadObserver = null;
private boolean isDownloading = false;
private LinkedHashMap<String,Download> mDownloadQueue = new LinkedHashMap<>();

public Downloader(Context context) {
public Downloader(Activity context) {
mContext = context;
downloadManager = (DownloadManager) mContext.getSystemService(DOWNLOAD_SERVICE);
}

public Downloader(Context context, DownloadEventStore eventStore){
public Downloader(Activity context, DownloadEventStore eventStore){
mContext = context;
downloadManager = (DownloadManager) mContext.getSystemService(DOWNLOAD_SERVICE);
mDownloadEventStore = eventStore;
Expand Down Expand Up @@ -189,6 +191,8 @@ public void updateProgress(String mUrl,String mBookId,int mChapterIndex,long pro

private long download(String URL, String name, String description, String subPath){

String downloadFolder = ArchiveUtils.Companion.getDownloadsRootFolder(mContext.getApplication());

//store downloadId to database for own reference
long downloadId= downloadUtils.getDownloadIdIfIsDownloading(mContext,URL);
if(downloadId==DOWNLOADER_NOT_DOWNLOADING){
Expand All @@ -199,6 +203,7 @@ private long download(String URL, String name, String description, String subPat
uri,
name,
description,
downloadFolder,
subPath
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public static long DownloadData(DownloadManager downloadManager,
Uri uri,
String name,
String description,
String root,
String subPath) {

long downloadReference;
Expand All @@ -140,7 +141,7 @@ public static long DownloadData(DownloadManager downloadManager,
request.setDescription(description);

//Set the local destination for the downloaded file to a path within the application's external files directory
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,subPath+name);
request.setDestinationInExternalPublicDir(root,subPath+name);

//Show notification visibility
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import com.allsoftdroid.audiobook.feature_mini_player.R
import com.allsoftdroid.audiobook.feature_mini_player.databinding.FragmentMiniPlayerBinding
import com.allsoftdroid.audiobook.feature_mini_player.di.FeatureMiniPlayerModule
import com.allsoftdroid.audiobook.feature_mini_player.presentation.viewModel.MiniPlayerViewModel
import com.allsoftdroid.common.base.extension.Event
import com.allsoftdroid.common.base.extension.PlayingState
import com.allsoftdroid.common.base.fragment.BaseContainerFragment
import com.allsoftdroid.common.base.store.audioPlayer.*
import io.reactivex.disposables.Disposable
import org.koin.android.ext.android.inject
import timber.log.Timber

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class MiniPlayerViewModel(
)
))
}
}else{
Timber.d("Player is not ready")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ class AudioService : Service(),KoinComponent{
private fun buildNotification( isItFirst:Boolean = false) {
sendNotification(
trackTitle = audioServiceBinder.getCurrentTrackTitle(),
bookId = audioServiceBinder.getBookId(),
bookName = audioServiceBinder.getBookName(),
applicationContext = applicationContext,
service = this,
Expand Down
Loading

0 comments on commit cefcf21

Please sign in to comment.