Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

WIP: Client API integration #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.freedombox.freedombox">
package="org.freedombox.freedombox">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name=".applications.FreedomBoxApp"
android:allowBackup="true"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/FreedomBoxTheme">

<activity android:name=".views.activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".views.activities.LauncherActivity">
</activity>
<activity android:name=".views.activities.LauncherActivity" />

<activity android:name=".views.activities.DiscoveryActivity"/>
<activity android:name=".views.activities.DiscoveryActivity" />

<activity android:name=".views.activities.SetupActivity"/>
<activity android:name=".views.activities.SetupActivity" />

</application>
</manifest>
2 changes: 1 addition & 1 deletion app/src/main/java/org/freedombox/freedombox/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

package org.freedombox.freedombox

const val DEFAULT_IP = "http://10.42.0.1"
const val DEFAULT_IP = "https://10.42.0.1:4430/plinth/api/1/services"
const val SERVICES_URL = "services.json"
const val APP_RESPONSE = "appResponse"
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package org.freedombox.freedombox.modules
import android.app.Application
import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import dagger.Module
import dagger.Provides
import org.freedombox.freedombox.utils.ImageRenderer
Expand All @@ -42,4 +44,7 @@ class AppModule(val application: Application) {
@Singleton
fun provideImageRenderer(): ImageRenderer =
ImageRenderer(application.applicationContext)

@Provides
fun provideGson() : Gson = GsonBuilder().setPrettyPrinting().create();
}
38 changes: 23 additions & 15 deletions app/src/main/java/org/freedombox/freedombox/utils/ImageRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,31 @@ import org.freedombox.freedombox.svg.SvgDecoder
import org.freedombox.freedombox.svg.SvgDrawableTranscoder
import java.io.InputStream

class ImageRenderer(context: Context) {
class ImageRenderer(val context: Context) {
private val requestBuilder: GenericRequestBuilder<Uri, InputStream, SVG, PictureDrawable> = Glide
.with(context)
.using(Glide.buildStreamModelLoader(Uri::class.java, context), InputStream::class.java)
.from(Uri::class.java)
.`as`(SVG::class.java)
.transcode(SvgDrawableTranscoder(), PictureDrawable::class.java)
.sourceEncoder(StreamEncoder())
.cacheDecoder(FileToStreamDecoder<SVG>(SvgDecoder()))
.decoder(SvgDecoder())
.dontAnimate()
.error(R.drawable.ic_logo)
.with(context)
.using(Glide.buildStreamModelLoader(Uri::class.java, context), InputStream::class.java)
.from(Uri::class.java)
.`as`(SVG::class.java)
.transcode(SvgDrawableTranscoder(), PictureDrawable::class.java)
.sourceEncoder(StreamEncoder())
.cacheDecoder(FileToStreamDecoder<SVG>(SvgDecoder()))
.decoder(SvgDecoder())
.dontAnimate()
.error(R.drawable.ic_logo)

fun loadImageFromURL(url: Uri, imageView: ImageView) {
fun loadSvgImageFromURL(url: Uri, imageView: ImageView) {
requestBuilder
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.load(url)
.into(imageView)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.load(url)
.into(imageView)
}

fun loadImageFromURL(url: Uri, imageView: ImageView) {
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.error(R.drawable.ic_logo)
.into(imageView)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,49 @@
package org.freedombox.freedombox.views.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import org.freedombox.freedombox.R

class DiscoveryListAdapter(val context: Context,
class DiscoveryListAdapter(private val context: Context,
private val boxNameList: List<String>,
private val portList: List<String>) : BaseAdapter() {
private val portList: List<String>,
private val itemClickListener: DiscoveryListAdapter.OnItemClickListener) : RecyclerView.Adapter<DiscoveryListAdapter.DiscoveryListItemViewHolder>() {

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
var inflaterView = convertView

if (inflaterView == null) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
inflaterView = inflater.inflate(R.layout.discovery_listview, null)
override fun onBindViewHolder(holder: DiscoveryListItemViewHolder?, position: Int) {
holder.let {
holder?.updateView(boxNameList[position], portList[position])
}
}

val boxName = inflaterView!!.findViewById<TextView>(R.id.boxName)
val port = inflaterView.findViewById<TextView>(R.id.port)
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): DiscoveryListItemViewHolder {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.discovery_listview, null)
return DiscoveryListItemViewHolder(view)
}

boxName.text = boxNameList[position]
port.text = portList[position]
override fun getItemCount(): Int = boxNameList.size

return inflaterView
}
inner class DiscoveryListItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var boxNameTextView: TextView = view.findViewById<TextView>(R.id.boxName) as TextView
var portNumberTextView: TextView = view.findViewById<TextView>(R.id.port) as TextView

override fun getItem(position: Int) = boxNameList[position]
init {
view.setOnClickListener { itemClickListener.onItemClick(adapterPosition) }
}

override fun getItemId(position: Int) = boxNameList[position].hashCode().toLong()
fun updateView(boxName: String, portNumber: String) {
boxNameTextView.text = boxName
portNumberTextView.text = portNumber
}
}

override fun getCount() = boxNameList.size
fun getItem(position: Int) : String = boxNameList.get(position)

interface OnItemClickListener {
fun onItemClick(position: Int)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import kotlinx.android.synthetic.main.app_container.view.appDescription
import kotlinx.android.synthetic.main.app_container.view.appIcon
import kotlinx.android.synthetic.main.app_container.view.appName
import kotlinx.android.synthetic.main.app_container.view.cardHolder
import org.freedombox.freedombox.DEFAULT_IP
import org.freedombox.freedombox.R
import org.freedombox.freedombox.SERVICES_URL
import org.freedombox.freedombox.utils.ImageRenderer
import java.util.Locale

Expand All @@ -40,21 +42,24 @@ class GridAdapter(val context: Context, val imageRenderer: ImageRenderer) : Base

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val inflater = context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rowView = inflater.inflate(R.layout.app_container, null)
val appDetail = items[position].asJsonObject
val locale = Locale.getDefault()

rowView.appName.text = appDetail["label"]
.asJsonObject[locale.language]
.asString
rowView.appDescription.text = appDetail["description"]
.asJsonObject[locale.language]
.asString
rowView.appName.text = appDetail["name"]
.asJsonObject[locale.language]
.asString
rowView.appDescription.text = appDetail["short_description"]?.let {
appDetail["short_description"]
.asJsonObject[locale.language]
.asString
}

val url = listOf(DEFAULT_IP, appDetail["icon"].asString).joinToString(separator = "/")
imageRenderer.loadImageFromURL(
Uri.parse(appDetail["icon"].asString),
rowView.appIcon
Uri.parse(url),
rowView.appIcon
)

rowView.appIcon.setOnClickListener {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,47 @@ import android.content.SharedPreferences
import android.net.nsd.NsdManager
import android.net.nsd.NsdServiceInfo
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.View
import com.google.gson.GsonBuilder
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.android.synthetic.main.fragment_discovery.configuredGroup
import kotlinx.android.synthetic.main.fragment_discovery.configuredListView
import kotlinx.android.synthetic.main.fragment_discovery.discoveredListView
import kotlinx.android.synthetic.main.fragment_discovery.*
import org.freedombox.freedombox.R
import org.freedombox.freedombox.components.AppComponent
import org.freedombox.freedombox.utils.storage.getSharedPreference
import org.freedombox.freedombox.views.activities.LauncherActivity
import org.freedombox.freedombox.views.adapter.DiscoveryListAdapter
import org.freedombox.freedombox.views.adapter.DiscoveryListAdapter.OnItemClickListener
import org.freedombox.freedombox.views.model.ConfigModel
import javax.inject.Inject

class DiscoveryFragment : BaseFragment() {
val TAG = "DISCOVERY_FRAGMENT"

lateinit var adapter: DiscoveryListAdapter
private val TAG: String = DiscoveryFragment::class.java.simpleName

private lateinit var discoveredBoxListAdapter: DiscoveryListAdapter

private val discoveredBoxList = mutableListOf<String>()

val discoveredBoxList = mutableListOf<String>()
private var configuredBoxList = listOf<String>()

val discoveredPortList = mutableListOf<String>()
private val discoveredPortList = mutableListOf<String>()

private var configuredPortList = listOf<String>()

var configuredBoxSetupList = listOf<ConfigModel>()
private var configuredBoxSetupList = listOf<ConfigModel>()

private val SERVICE = "_freedombox._tcp"

@Inject lateinit var sharedPreferences: SharedPreferences

lateinit var nsdManager: NsdManager
private lateinit var nsdManager: NsdManager

private lateinit var discoveryListener: FBXDiscoveryListener

@Inject lateinit var gson: Gson

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)

Expand All @@ -69,45 +74,45 @@ class DiscoveryFragment : BaseFragment() {
nsdManager.discoverServices(SERVICE, NsdManager.PROTOCOL_DNS_SD, discoveryListener)

val configuredBoxesJSON = getSharedPreference(sharedPreferences,
getString(R.string.default_box))
getString(R.string.default_box))


configuredBoxesJSON?.let {
val gson = GsonBuilder().setPrettyPrinting().create()
configuredBoxSetupList += gson.
fromJson<List<ConfigModel>>(configuredBoxesJSON,
object : TypeToken<List<ConfigModel>>() {}.type)
fromJson<List<ConfigModel>>(configuredBoxesJSON,
object : TypeToken<List<ConfigModel>>() {}.type)
for (configModel in configuredBoxSetupList) {
configuredBoxList += configModel.domain
configuredPortList += "80"
}

configuredGroup.visibility = View.VISIBLE

adapter = DiscoveryListAdapter(activity!!.applicationContext,
configuredBoxList,
configuredPortList)
configuredListView.adapter = adapter
val configuredBoxListAdapter = DiscoveryListAdapter(activity!!.applicationContext,
configuredBoxList,
configuredPortList, object : OnItemClickListener {
override fun onItemClick(position: Int) {
val intent = Intent(activity, LauncherActivity::class.java)
intent.putExtra(getString(R.string.current_box), configuredBoxList[position])
startActivity(intent)
}
})
configuredListView.layoutManager = LinearLayoutManager(activity)
configuredListView.adapter = configuredBoxListAdapter
}

configuredListView.setOnItemClickListener { _, _, position, _ ->
discoveredBoxListAdapter = DiscoveryListAdapter(activity!!.applicationContext,
discoveredBoxList,
discoveredPortList, object : OnItemClickListener {
override fun onItemClick(position: Int) {
val intent = Intent(activity, LauncherActivity::class.java)
intent.putExtra(getString(R.string.current_box), configuredBoxList[position])
intent.putExtra(getString(R.string.current_box),
discoveredBoxList[position])
startActivity(intent)
}

}

adapter = DiscoveryListAdapter(activity!!.applicationContext,
discoveredBoxList,
discoveredPortList)
discoveredListView.adapter = adapter

discoveredListView.setOnItemClickListener { _, _, position, _ ->
val intent = Intent(activity, LauncherActivity::class.java)
intent.putExtra(getString(R.string.current_box),
discoveredBoxList[position])
startActivity(intent)
}
})
discoveredListView.layoutManager = LinearLayoutManager(activity)
discoveredListView.adapter = discoveredBoxListAdapter
}

companion object {
Expand Down Expand Up @@ -148,17 +153,16 @@ class DiscoveryFragment : BaseFragment() {
discoveredPortList.add(serviceInfo.port.toString())
}
activity!!.runOnUiThread {
adapter.notifyDataSetChanged()
Log.i(TAG, "runOnUiThread")
discoveredBoxListAdapter.notifyDataSetChanged()
}

}
}

inner class FBXDiscoveryListener : NsdManager.DiscoveryListener {
override fun onServiceFound(serviceInfo: NsdServiceInfo) {
Log.d(TAG, serviceInfo.serviceType)
Log.d(TAG, serviceInfo.serviceName)
Log.d(TAG, String.format("onServiceFound() serviceType %s", serviceInfo.serviceType))
Log.d(TAG, String.format("onServiceFound() serviceName %s", serviceInfo.serviceName))
discoveredBoxList.clear()
discoveredPortList.clear()
nsdManager.resolveService(serviceInfo, FBXResolveListener())
Expand Down
Loading