diff --git a/src/PrefsAct.scala b/src/PrefsAct.scala index 53b1e9f..f93518d 100644 --- a/src/PrefsAct.scala +++ b/src/PrefsAct.scala @@ -21,24 +21,17 @@ class PrefsAct extends PreferenceActivity { def exportPrefs() { val filename = "profile-%s.aprs".format(new SimpleDateFormat("yyyyMMdd-HHmm").format(new Date())) - val directory = new File(Environment.getExternalStorageDirectory(), "APRSdroid") + val directory = UIHelper.getExportDirectory(this) val file = new File(directory, filename) try { + directory.mkdirs() val prefs = PreferenceManager.getDefaultSharedPreferences(this) val json = new JSONObject(prefs.getAll) val fo = new PrintWriter(file) fo.println(json.toString(2)) fo.close() - // TODO: implement FileProvider for Android N+ - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) - startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND) - .setType("text/plain") - .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)) - .putExtra(Intent.EXTRA_SUBJECT, filename), - file.toString())) - else - Toast.makeText(this, file.toString, Toast.LENGTH_LONG).show() + UIHelper.shareFile(this, file, filename) } catch { case e : Exception => Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show() } diff --git a/src/UIHelper.scala b/src/UIHelper.scala index 58a2ed9..09141e0 100644 --- a/src/UIHelper.scala +++ b/src/UIHelper.scala @@ -18,6 +18,29 @@ import java.util.Date import android.content.pm.PackageManager import android.provider.Settings +import androidx.core.content.FileProvider + +object UIHelper +{ + def getExportDirectory(ctx : Context) : File = { + val base = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + } else { + Environment.getExternalStorageDirectory() + } + return new File(base, "APRSdroid") + } + + def shareFile(ctx : Context, file : File, filename : String) { + ctx.startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND) + .setType("text/plain") + .putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(ctx, "org.aprsdroid.fileprovider", file)) + .putExtra(Intent.EXTRA_SUBJECT, filename), + file.toString())) + } + +} + trait UIHelper extends Activity with LoadingIndicator with PermissionHelper @@ -409,7 +432,7 @@ trait UIHelper extends Activity } class LogExporter(storage : StorageDatabase, call : String) extends MyAsyncTask[Unit, String] { val filename = "aprsdroid-%s.log".format(new SimpleDateFormat("yyyyMMdd-HHmm").format(new Date())) - val directory = new File(Environment.getExternalStorageDirectory(), "APRSdroid") + val directory = UIHelper.getExportDirectory(UIHelper.this) val file = new File(directory, filename) override def doInBackground1(params : Array[String]) : String = { @@ -445,14 +468,8 @@ trait UIHelper extends Activity onStopLoading() if (error != null) Toast.makeText(UIHelper.this, error, Toast.LENGTH_SHORT).show() - else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) - startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND) - .setType("text/plain") - .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)) - .putExtra(Intent.EXTRA_SUBJECT, filename), - file.toString())) - else - Toast.makeText(UIHelper.this, file.toString(), Toast.LENGTH_LONG).show() + else + UIHelper.shareFile(UIHelper.this, file, filename) } } }