-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix downloading and opening files on Nougat
- Loading branch information
Showing
15 changed files
with
217 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 0 additions & 94 deletions
94
app/src/main/java/com/stevenschoen/putionew/PutioOpenFileService.java
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
app/src/main/java/com/stevenschoen/putionew/PutioOpenFileService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.stevenschoen.putionew | ||
|
||
import android.app.DownloadManager | ||
import android.app.Service | ||
import android.content.BroadcastReceiver | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.IntentFilter | ||
import android.net.Uri | ||
import android.os.IBinder | ||
import com.stevenschoen.putionew.files.FileFinishedActivity | ||
|
||
class PutioOpenFileService : Service() { | ||
|
||
companion object { | ||
const val EXTRA_DOWNLOAD_ID = "download_id" | ||
} | ||
|
||
lateinit var downloadManager: DownloadManager | ||
|
||
var downloadId: Long = -1 | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
|
||
downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager | ||
} | ||
|
||
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | ||
super.onStartCommand(intent, flags, startId) | ||
|
||
this.downloadId = intent.extras.getLong(EXTRA_DOWNLOAD_ID, -1) | ||
|
||
val intentFilter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE) | ||
registerReceiver(downloadReceiver, intentFilter) | ||
|
||
return Service.START_STICKY | ||
} | ||
|
||
private val downloadReceiver = object : BroadcastReceiver() { | ||
override fun onReceive(context: Context, intent: Intent) { | ||
val query = DownloadManager.Query().setFilterById(downloadId) | ||
val cursor = downloadManager.query(query) | ||
|
||
if (cursor.moveToFirst()) { | ||
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) | ||
when (status) { | ||
DownloadManager.STATUS_SUCCESSFUL -> { | ||
val title = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE)) | ||
val uri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))) | ||
val type = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE)) | ||
|
||
val finishedIntent = Intent(this@PutioOpenFileService, FileFinishedActivity::class.java).apply { | ||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
putExtra(FileFinishedActivity.EXTRA_NAME, title) | ||
putExtra(FileFinishedActivity.EXTRA_URI, uri) | ||
putExtra(FileFinishedActivity.EXTRA_MEDIA_TYPE, type) | ||
} | ||
startActivity(finishedIntent) | ||
|
||
stopSelf() | ||
} | ||
DownloadManager.STATUS_FAILED -> { } | ||
DownloadManager.STATUS_PAUSED -> { } | ||
DownloadManager.STATUS_PENDING -> { } | ||
DownloadManager.STATUS_RUNNING -> { } | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onDestroy() { | ||
unregisterReceiver(downloadReceiver) | ||
} | ||
|
||
override fun onBind(intent: Intent?): IBinder? { | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 0 additions & 57 deletions
57
app/src/main/java/com/stevenschoen/putionew/activities/FileFinished.java
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/stevenschoen/putionew/files/FileFinishedActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.stevenschoen.putionew.files | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.support.v7.app.AppCompatActivity | ||
import android.widget.Button | ||
import android.widget.TextView | ||
import com.stevenschoen.putionew.R | ||
|
||
class FileFinishedActivity : AppCompatActivity() { | ||
|
||
companion object { | ||
const val EXTRA_NAME = "name" | ||
const val EXTRA_URI = "uri" | ||
const val EXTRA_MEDIA_TYPE = "type" | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.dialog_filefinished) | ||
|
||
val name = intent.extras.getString(EXTRA_NAME) | ||
|
||
val messageView = findViewById(R.id.text_downloadfinished_body) as TextView | ||
messageView.text = String.format(getString(R.string.downloadfinishedbody), name) | ||
|
||
val openView = findViewById(R.id.button_filefinished_action) as Button | ||
openView.setOnClickListener { | ||
val uri = intent.extras.getParcelable<Uri>(EXTRA_URI) | ||
val type = intent.extras.getString(EXTRA_MEDIA_TYPE) | ||
val intent = Intent(Intent.ACTION_VIEW).apply { | ||
setDataAndType(uri, type) | ||
} | ||
startActivity(Intent.createChooser(intent, null)) | ||
|
||
finish() | ||
} | ||
|
||
val okView = findViewById(R.id.button_filefinished_ok) as Button | ||
okView.setOnClickListener { finish() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.