From 5a48f6408e235fd22b97d73f8e75078dc17b4abc Mon Sep 17 00:00:00 2001 From: Steven Schoen Date: Tue, 27 Dec 2016 03:57:29 -0500 Subject: [PATCH] Fix downloading and opening files on Nougat --- app/build.gradle | 4 +- app/src/main/AndroidManifest.xml | 5 +- .../{activities => }/LoginActivity.java | 5 +- .../stevenschoen/putionew/PutioActivity.java | 1 - .../putionew/PutioOpenFileService.java | 94 ------------------- .../putionew/PutioOpenFileService.kt | 79 ++++++++++++++++ .../com/stevenschoen/putionew/PutioUtils.java | 12 +-- .../putionew/activities/FileFinished.java | 57 ----------- .../putionew/files/FileFinishedActivity.kt | 43 +++++++++ .../putionew/files/FileListFragment.kt | 5 +- .../stevenschoen/putionew/tv/TvActivity.java | 2 +- .../main/res/layout/dialog_filefinished.xml | 82 +++++++--------- app/src/main/res/layout/dialog_redownload.xml | 73 +++++++------- .../res/layout/download_individualorzip.xml | 6 +- app/src/main/res/values/styles.xml | 5 +- 15 files changed, 217 insertions(+), 256 deletions(-) rename app/src/main/java/com/stevenschoen/putionew/{activities => }/LoginActivity.java (98%) delete mode 100644 app/src/main/java/com/stevenschoen/putionew/PutioOpenFileService.java create mode 100644 app/src/main/java/com/stevenschoen/putionew/PutioOpenFileService.kt delete mode 100644 app/src/main/java/com/stevenschoen/putionew/activities/FileFinished.java create mode 100644 app/src/main/java/com/stevenschoen/putionew/files/FileFinishedActivity.kt diff --git a/app/build.gradle b/app/build.gradle index dd8b7c5e..eabe325f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3c04c95a..3ecafbbf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,9 +60,10 @@ android:name="android.app.default_searchable" android:value=".activities.Putio"/> - + { + 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 + } +} \ No newline at end of file diff --git a/app/src/main/java/com/stevenschoen/putionew/PutioUtils.java b/app/src/main/java/com/stevenschoen/putionew/PutioUtils.java index c5cd1ef1..4ee673b9 100644 --- a/app/src/main/java/com/stevenschoen/putionew/PutioUtils.java +++ b/app/src/main/java/com/stevenschoen/putionew/PutioUtils.java @@ -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; @@ -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(); diff --git a/app/src/main/java/com/stevenschoen/putionew/activities/FileFinished.java b/app/src/main/java/com/stevenschoen/putionew/activities/FileFinished.java deleted file mode 100644 index 42ec0e8a..00000000 --- a/app/src/main/java/com/stevenschoen/putionew/activities/FileFinished.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.stevenschoen.putionew.activities; - -import android.app.Activity; -import android.os.Bundle; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.Button; -import android.widget.TextView; - -import com.stevenschoen.putionew.PutioUtils; -import com.stevenschoen.putionew.R; - -public class FileFinished extends Activity { - private int mode; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setTheme(R.style.Putio_Dialog); - setContentView(R.layout.dialog_filefinished); - - TextView textBody = (TextView) findViewById(R.id.text_downloadfinished_body); - textBody.setText(String.format(getString(R.string.downloadfinishedbody), getIntent().getExtras().getString("filename"))); - - mode = getIntent().getExtras().getInt("mode"); - - Button buttonAction = (Button) findViewById(R.id.button_filefinished_action); - switch (mode) { - case PutioUtils.ACTION_OPEN: - buttonAction.setText(getString(R.string.open)); - break; - } - buttonAction.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - int id = getIntent().getExtras().getInt("id"); - switch (mode) { - case PutioUtils.ACTION_OPEN: - PutioUtils.openDownloadedId(id, FileFinished.this); - break; - } - - finish(); - } - }); - - Button buttonOk = (Button) findViewById(R.id.button_filefinished_ok); - buttonOk.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - finish(); - } - }); - } -} \ No newline at end of file diff --git a/app/src/main/java/com/stevenschoen/putionew/files/FileFinishedActivity.kt b/app/src/main/java/com/stevenschoen/putionew/files/FileFinishedActivity.kt new file mode 100644 index 00000000..55147354 --- /dev/null +++ b/app/src/main/java/com/stevenschoen/putionew/files/FileFinishedActivity.kt @@ -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(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() } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/stevenschoen/putionew/files/FileListFragment.kt b/app/src/main/java/com/stevenschoen/putionew/files/FileListFragment.kt index 9733db41..920ca03d 100644 --- a/app/src/main/java/com/stevenschoen/putionew/files/FileListFragment.kt +++ b/app/src/main/java/com/stevenschoen/putionew/files/FileListFragment.kt @@ -154,7 +154,7 @@ abstract class FileListFragment : 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!") } @@ -198,7 +198,6 @@ abstract class FileListFragment : Rx } override fun onDownloadSelected() { selectionDownloadFiles() - filesAdapter!!.clearChecked() } override fun onCopyLinkSelected() { selectionCopyLinks() @@ -243,6 +242,7 @@ abstract class FileListFragment : 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() @@ -255,6 +255,7 @@ abstract class FileListFragment : Rx error.printStackTrace() Toast.makeText(context, R.string.network_error, Toast.LENGTH_SHORT).show() }) + filesAdapter!!.clearChecked() } override fun onCanceled() { } } diff --git a/app/src/main/java/com/stevenschoen/putionew/tv/TvActivity.java b/app/src/main/java/com/stevenschoen/putionew/tv/TvActivity.java index 4bd6874e..34eac996 100644 --- a/app/src/main/java/com/stevenschoen/putionew/tv/TvActivity.java +++ b/app/src/main/java/com/stevenschoen/putionew/tv/TvActivity.java @@ -5,10 +5,10 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import com.stevenschoen.putionew.LoginActivity; import com.stevenschoen.putionew.PutioApplication; import com.stevenschoen.putionew.PutioUtils; import com.stevenschoen.putionew.R; -import com.stevenschoen.putionew.activities.LoginActivity; /** * Created by simonreggiani on 15-01-10. diff --git a/app/src/main/res/layout/dialog_filefinished.xml b/app/src/main/res/layout/dialog_filefinished.xml index f6ddb25f..0e9505f8 100644 --- a/app/src/main/res/layout/dialog_filefinished.xml +++ b/app/src/main/res/layout/dialog_filefinished.xml @@ -1,53 +1,39 @@ - + - + - + - +