Skip to content

Commit

Permalink
#1844 Fix export account OPML
Browse files Browse the repository at this point in the history
  • Loading branch information
sictiru committed Feb 5, 2024
1 parent 2d2955a commit e2a5012
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.newsblur.activity

import android.app.Activity
import android.app.DownloadManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Bundle
import android.webkit.MimeTypeMap
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar
import com.newsblur.R
import com.newsblur.databinding.ActivityImportExportBinding
import com.newsblur.network.APIConstants
import com.newsblur.network.APIManager
import com.newsblur.service.NBSyncService
import com.newsblur.util.FeedUtils
import com.newsblur.util.FileDownloader
import com.newsblur.util.NBScope
import com.newsblur.util.UIUtils
import com.newsblur.util.executeAsyncTask
Expand All @@ -32,19 +38,39 @@ class ImportExportActivity : NbActivity() {

private lateinit var binding: ActivityImportExportBinding

private var fileDownloaderId: Long? = null

private val filePickResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
handleFilePickResult(result.data)
}
}

private val onOpmlExportReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1L)
if (id == fileDownloaderId) {
context?.let {
val msg = "${it.getString(R.string.newsblur_opml)} download completed"
Toast.makeText(it, msg, Toast.LENGTH_SHORT).show()
}
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityImportExportBinding.inflate(layoutInflater)
setContentView(binding.root)

setupUI()
setupListeners()

ContextCompat.registerReceiver(this, onOpmlExportReceiver, IntentFilter().apply {
addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
}, ContextCompat.RECEIVER_EXPORTED)
}

private fun setupUI() {
Expand All @@ -67,8 +93,9 @@ class ImportExportActivity : NbActivity() {
}

private fun exportOpmlFile() {
val exportOpmlUrl = APIConstants.buildUrl(APIConstants.PATH_EXPORT_OPML)
UIUtils.handleUri(this, Uri.parse(exportOpmlUrl))
fileDownloaderId = FileDownloader.exportOpml(this)
val msg = "${getString(R.string.newsblur_opml)} download started"
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}

private fun importOpmlFile(uri: Uri) {
Expand Down Expand Up @@ -128,4 +155,9 @@ class ImportExportActivity : NbActivity() {
override fun handleUpdate(updateType: Int) {
// ignore
}

override fun onDestroy() {
unregisterReceiver(onOpmlExportReceiver)
super.onDestroy()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.newsblur.util

import android.app.DownloadManager
import android.content.Context
import android.os.Environment
import android.webkit.MimeTypeMap
import androidx.core.net.toUri
import com.newsblur.R
import com.newsblur.network.APIConstants

object FileDownloader {

fun exportOpml(context: Context): Long {
val manager = context.getSystemService(DownloadManager::class.java)
val url = APIConstants.buildUrl(APIConstants.PATH_EXPORT_OPML)
val userName = PrefsUtils.getUserName(context)
val cookie = PrefsUtils.getCookie(context)

val file = StringBuilder().apply {
append(context.getString(R.string.newsbluropml))
userName?.let { append("-$userName") }
append(".xml")
}.toString()

val request = DownloadManager.Request(url.toUri())
.setMimeType(MimeTypeMap.getSingleton().getMimeTypeFromExtension(".xml"))
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.addRequestHeader("Cookie", cookie)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file)
.setTitle(context.getString(R.string.newsblur_opml))
return manager.enqueue(request)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ public static UserDetails getUserDetails(Context context) {
return user;
}

@Nullable
public static String getUserName(Context context) {
SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, 0);
return preferences.getString(PrefConstants.USER_USERNAME, null);
}

private static void saveUserImage(final Context context, String pictureUrl) {
Bitmap bitmap;
try {
Expand Down Expand Up @@ -1079,8 +1085,13 @@ public static void setSpacingStyle(Context context, SpacingStyle spacingStyle) {
* which gets saved when a user is authenticated.
*/
public static boolean hasCookie(Context context) {
return getCookie(context) != null;
}

@Nullable
public static String getCookie(Context context) {
SharedPreferences preferences = context.getSharedPreferences(PrefConstants.PREFERENCES, Context.MODE_PRIVATE);
return preferences.getString(PrefConstants.PREF_COOKIE, null) != null;
return preferences.getString(PrefConstants.PREF_COOKIE, null);
}

public static MarkStoryReadBehavior getMarkStoryReadBehavior(Context context) {
Expand Down
2 changes: 2 additions & 0 deletions clients/android/NewsBlur/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@
<string name="menu_newsletters">Newsletters…</string>
<string name="menu_shortcuts">Shortcuts…</string>

<string name="newsblur_opml">NewsBlur OPML</string>
<string name="newsbluropml">NewsBlur-OPML</string>
<string name="import_export_title">Import/Export OPML</string>
<string name="notifications_title">Notifications</string>

Expand Down

0 comments on commit e2a5012

Please sign in to comment.