Skip to content

Commit

Permalink
Fix downloading and opening files on Nougat
Browse files Browse the repository at this point in the history
  • Loading branch information
DSteve595 committed Dec 27, 2016
1 parent 10b510b commit 5a48f64
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 256 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ android {
applicationId 'com.stevenschoen.putionew'
minSdkVersion 19
targetSdkVersion 25
versionCode 107
versionName '4.0.5'
versionCode 108
versionName '4.0.6'
multiDexEnabled true
}
buildTypes {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@
android:name="android.app.default_searchable"
android:value=".activities.Putio"/>
</activity>
<activity android:name=".activities.LoginActivity"/>
<activity android:name=".LoginActivity"/>
<activity
android:name=".activities.FileFinished"
android:label="@string/downloadfinishedopentitle"
android:name=".files.FileFinishedActivity"
android:theme="@style/Putio.Dialog"/>
<activity
android:name=".transfers.AddTransferActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.stevenschoen.putionew.activities;
package com.stevenschoen.putionew;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
Expand All @@ -15,9 +15,6 @@
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.stevenschoen.putionew.PutioActivity;
import com.stevenschoen.putionew.R;

import org.json.JSONException;
import org.json.JSONObject;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.stevenschoen.putionew.activities.LoginActivity;
import com.stevenschoen.putionew.cast.BaseCastActivity;
import com.stevenschoen.putionew.files.FilesFragment;
import com.stevenschoen.putionew.fragments.Account;
Expand Down

This file was deleted.

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
}
}
12 changes: 6 additions & 6 deletions app/src/main/java/com/stevenschoen/putionew/PutioUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -199,14 +200,13 @@ public long[] call(Object... args) {
public void call(long[] downloadIds) {
switch (actionWhenDone) {
case ACTION_OPEN:
if (files.length > 1) {
throw new IllegalArgumentException("Download started with ACTION_OPEN but more than one file: " + Arrays.toString(files));
}
Intent serviceOpenIntent = new Intent(activity, PutioOpenFileService.class);
serviceOpenIntent.putExtra("downloadIds", downloadIds);
serviceOpenIntent.putExtra("id", files[0].id);
serviceOpenIntent.putExtra("filename", files[0].name);
serviceOpenIntent.putExtra("mode", actionWhenDone);
serviceOpenIntent.putExtra(PutioOpenFileService.EXTRA_DOWNLOAD_ID, downloadIds[0]);
activity.startService(serviceOpenIntent);
Toast.makeText(activity, activity.getString(R.string.downloadwillopen),
Toast.LENGTH_LONG).show();
Toast.makeText(activity, activity.getString(R.string.downloadwillopen), Toast.LENGTH_LONG).show();
break;
case ACTION_NOTHING:
Toast.makeText(activity, activity.getString(R.string.downloadstarted), Toast.LENGTH_SHORT).show();
Expand Down

This file was deleted.

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() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ abstract class FileListFragment<CallbacksClass: FileListFragment.Callbacks> : Rx
val downloadFragment = Fragment.instantiate(context, DownloadIndividualOrZipFragment::class.java.name) as DownloadIndividualOrZipFragment
downloadFragment.show(childFragmentManager, FolderFragment.FRAGTAG_DOWNLOAD_INDIVIDUALORZIP)
} else if (checkedFiles.size == 1) {
PutioApplication.get(context).putioUtils.downloadFiles(activity, PutioUtils.ACTION_NOTHING, *checkedFiles.toTypedArray())
PutioApplication.get(context).putioUtils.downloadFiles(activity, PutioUtils.ACTION_NOTHING, checkedFiles.first())
} else {
throw IllegalStateException("Download started with no file IDs!")
}
Expand Down Expand Up @@ -198,7 +198,6 @@ abstract class FileListFragment<CallbacksClass: FileListFragment.Callbacks> : Rx
}
override fun onDownloadSelected() {
selectionDownloadFiles()
filesAdapter!!.clearChecked()
}
override fun onCopyLinkSelected() {
selectionCopyLinks()
Expand Down Expand Up @@ -243,6 +242,7 @@ abstract class FileListFragment<CallbacksClass: FileListFragment.Callbacks> : Rx
childFragment.callbacks = object : DownloadIndividualOrZipFragment.Callbacks {
override fun onIndividualSelected() {
PutioApplication.get(context).putioUtils.downloadFiles(activity, PutioUtils.ACTION_NOTHING, *getCheckedFiles().toTypedArray())
filesAdapter!!.clearChecked()
}
override fun onZipSelected() {
val checkedFiles = getCheckedFiles()
Expand All @@ -255,6 +255,7 @@ abstract class FileListFragment<CallbacksClass: FileListFragment.Callbacks> : Rx
error.printStackTrace()
Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT).show()
})
filesAdapter!!.clearChecked()
}
override fun onCanceled() { }
}
Expand Down
Loading

0 comments on commit 5a48f64

Please sign in to comment.