Skip to content

Commit

Permalink
Profile Import: direct JSON to config
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0rg committed Dec 12, 2017
1 parent d377bdf commit 3ee13d9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
20 changes: 20 additions & 0 deletions AndroidManifest.xml
Expand Up @@ -92,6 +92,26 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".ProfileImportActivity" android:label="@string/profile_import_activity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file"
android:mimeType="*/*"
android:host="*"
android:pathPattern=".*\\.aprs" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="content"
android:mimeType="*/*"
android:host="*"
android:pathPattern=".*\\.aprs" />
</intent-filter>
</activity>
<activity android:name=".KeyfileImportActivity" android:label="@string/ssl_import_activity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
Expand Down
5 changes: 5 additions & 0 deletions res/values/strings.xml
Expand Up @@ -384,6 +384,11 @@
<string name="ssl_expired">Your certificate has expired!</string>
<string name="ssl_expire_in">Your certificate will expire in %d days!</string>

<!-- Config import -->
<string name="profile_import_activity">Profile import</string>
<string name="profile_import_done">Import successful!</string>
<string name="profile_import_error">Error importing profile: %s!</string>

<!-- (USB) Serial TNC settings -->
<string name="p_serial_baudrate">Baud Rate</string>
<string name="p_serial_baudrate_summary">Data rate of the serial port</string>
Expand Down
57 changes: 57 additions & 0 deletions src/ProfileImportActivity.scala
@@ -0,0 +1,57 @@
package org.aprsdroid.app

import android.app.Activity
import android.content._
import android.os.Bundle
import android.preference.PreferenceManager
import android.text.InputType
import android.util.Log
import android.widget.{EditText, Toast}

import java.io.File
import java.util.Scanner

import org.json._

import scala.collection.JavaConversions._ // for enumeration of config items

class ProfileImportActivity extends Activity {
val TAG = "APRSdroid.ProfileImport"

override def onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
Log.d(TAG, "created: " + getIntent())
import_config()
}

def import_config() {
try {
// parse stream into string, http://stackoverflow.com/a/5445161/539443
val scanner = new Scanner(getContentResolver().openInputStream(getIntent.getData())).useDelimiter("\\A")
val config_string = scanner.next()
val config = new JSONObject(config_string)
val prefsedit = PreferenceManager.getDefaultSharedPreferences(this).edit()

val keys = config.keys()
while (keys.hasNext()) {
val item = keys.next().asInstanceOf[String]
val value = config.get(item)
Log.d(TAG, "reading: " + item + " = " + value + "/" + value.getClass())

// Hack: too complicated to figure out scala match rules for native types
value.getClass().getSimpleName() match {
case "String" => prefsedit.putString(item, config.getString(item))
case "Boolean" => prefsedit.putBoolean(item, config.getBoolean(item))
case "Int" => prefsedit.putInt(item, config.getInt(item))
}
}
prefsedit.commit()
Toast.makeText(this, R.string.profile_import_done, Toast.LENGTH_SHORT).show()
} catch {
case e : Exception =>
Toast.makeText(this, getString(R.string.profile_import_error, e.getMessage()), Toast.LENGTH_LONG).show()
e.printStackTrace()
}
finish()
}
}

0 comments on commit 3ee13d9

Please sign in to comment.