diff --git a/simplefilepicker/.gitignore b/simplefilepicker/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/simplefilepicker/.gitignore @@ -0,0 +1 @@ +/build diff --git a/simplefilepicker/src/main/kotlin/com/simplemobiletools/filepicker/views/Breadcrumbs.kt b/simplefilepicker/src/main/kotlin/com/simplemobiletools/filepicker/views/Breadcrumbs.kt new file mode 100644 index 00000000..6ebcaf5c --- /dev/null +++ b/simplefilepicker/src/main/kotlin/com/simplemobiletools/filepicker/views/Breadcrumbs.kt @@ -0,0 +1,166 @@ +package com.simplemobiletools.filepicker.views + +import android.content.Context +import android.graphics.Point +import android.os.Environment +import android.util.AttributeSet +import android.view.LayoutInflater +import android.view.View +import android.view.WindowManager +import android.widget.LinearLayout +import com.simplemobiletools.filepicker.R +import com.simplemobiletools.filepicker.models.FileDirItem +import kotlinx.android.synthetic.main.breadcrumb_item.view.* + +class Breadcrumbs(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs), View.OnClickListener { + private var mDeviceWidth: Int = 0 + + private var mInflater: LayoutInflater? = null + private var mListener: BreadcrumbsListener? = null + + init { + init(context) + } + + private fun init(context: Context) { + mInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + val display = (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay + val deviceDisplay = Point() + display.getSize(deviceDisplay) + mDeviceWidth = deviceDisplay.x + } + + fun setListener(listener: BreadcrumbsListener) { + mListener = listener + } + + override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { + val paddingTop = paddingTop + val paddingLeft = paddingLeft + val paddingRight = paddingRight + val childRight = measuredWidth - paddingRight + val childBottom = measuredHeight - paddingBottom + val childHeight = childBottom - paddingTop + + val usableWidth = mDeviceWidth - paddingLeft - paddingRight + var maxHeight = 0 + var curWidth: Int + var curHeight: Int + var curLeft = paddingLeft + var curTop = paddingTop + + val cnt = childCount + for (i in 0..cnt - 1) { + val child = getChildAt(i) + + child.measure(View.MeasureSpec.makeMeasureSpec(usableWidth, View.MeasureSpec.AT_MOST), + View.MeasureSpec.makeMeasureSpec(childHeight, View.MeasureSpec.AT_MOST)) + curWidth = child.measuredWidth + curHeight = child.measuredHeight + + if (curLeft + curWidth >= childRight) { + curLeft = paddingLeft + curTop += maxHeight + maxHeight = 0 + } + + child.layout(curLeft, curTop, curLeft + curWidth, curTop + curHeight) + if (maxHeight < curHeight) + maxHeight = curHeight + + curLeft += curWidth + } + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val usableWidth = mDeviceWidth - paddingLeft - paddingRight + var width = 0 + var rowHeight = 0 + var lines = 1 + + val cnt = childCount + for (i in 0..cnt - 1) { + val child = getChildAt(i) + measureChild(child, widthMeasureSpec, heightMeasureSpec) + width += child.measuredWidth + rowHeight = child.measuredHeight + + if (width / usableWidth > 0) { + lines++ + width = child.measuredWidth + } + } + + val parentWidth = View.MeasureSpec.getSize(widthMeasureSpec) + val calculatedHeight = paddingTop + paddingBottom + rowHeight * lines + setMeasuredDimension(parentWidth, calculatedHeight) + } + + fun setInitialBreadcrumb(fullPath: String) { + val showFullPath = false//Config.newInstance(context).showFullPath + val basePath = Environment.getExternalStorageDirectory().toString() + var tempPath = fullPath + var currPath = basePath + if (!showFullPath) { + tempPath = fullPath.replace(basePath, context.getString(R.string.initial_breadcrumb) + "/") + } else { + currPath = "/" + } + + removeAllViewsInLayout() + val dirs = tempPath.split("/".toRegex()).dropLastWhile(String::isEmpty).toTypedArray() + for (i in dirs.indices) { + val dir = dirs[i] + if (i > 0) { + currPath += dir + "/" + } else if (showFullPath) { + addRootFolder() + } + + if (dir.isEmpty()) + continue + + val item = FileDirItem(currPath, dir, true, 0, 0) + addBreadcrumb(item, i > 0 || showFullPath) + } + + if (dirs.size == 0 && showFullPath) { + addRootFolder() + } + } + + fun addBreadcrumb(item: FileDirItem, addPrefix: Boolean) { + val view = mInflater!!.inflate(R.layout.breadcrumb_item, null, false) + + var textToAdd = item.name + if (addPrefix) + textToAdd = " -> " + textToAdd + view.breadcrumb_text.text = textToAdd + addView(view) + view.setOnClickListener(this) + + view.tag = item + } + + fun removeBreadcrumb() { + removeView(getChildAt(childCount - 1)) + } + + private fun addRootFolder() { + val item = FileDirItem("/", " / ", true, 0, 0) + addBreadcrumb(item, false) + } + + override fun onClick(v: View) { + val cnt = childCount + for (i in 0..cnt - 1) { + if (getChildAt(i) != null && getChildAt(i) == v) { + mListener?.breadcrumbClicked(i) + } + } + } + + interface BreadcrumbsListener { + fun breadcrumbClicked(id: Int) + } +} diff --git a/simplefilepicker/src/main/res/layout/breadcrumb_item.xml b/simplefilepicker/src/main/res/layout/breadcrumb_item.xml new file mode 100644 index 00000000..4f89712f --- /dev/null +++ b/simplefilepicker/src/main/res/layout/breadcrumb_item.xml @@ -0,0 +1,6 @@ + + diff --git a/simplefilepicker/src/main/res/values-de/strings.xml b/simplefilepicker/src/main/res/values-de/strings.xml index dfb6da1b..cac400d1 100644 --- a/simplefilepicker/src/main/res/values-de/strings.xml +++ b/simplefilepicker/src/main/res/values-de/strings.xml @@ -1,6 +1,7 @@ Simple File Picker Ziel auswählen + home OK Abbrechen diff --git a/simplefilepicker/src/main/res/values-it/strings.xml b/simplefilepicker/src/main/res/values-it/strings.xml index d72f3240..4c11a683 100644 --- a/simplefilepicker/src/main/res/values-it/strings.xml +++ b/simplefilepicker/src/main/res/values-it/strings.xml @@ -1,6 +1,7 @@ Simple File Picker Seleziona destinazione + home OK Annulla diff --git a/simplefilepicker/src/main/res/values-ja/strings.xml b/simplefilepicker/src/main/res/values-ja/strings.xml index c8f28f67..aa4b4d80 100644 --- a/simplefilepicker/src/main/res/values-ja/strings.xml +++ b/simplefilepicker/src/main/res/values-ja/strings.xml @@ -1,6 +1,7 @@ Simple File Picker 宛先を選択 + ホーム OK Cancel diff --git a/simplefilepicker/src/main/res/values-pt-rPT/strings.xml b/simplefilepicker/src/main/res/values-pt-rPT/strings.xml index da0ccc83..40c78896 100644 --- a/simplefilepicker/src/main/res/values-pt-rPT/strings.xml +++ b/simplefilepicker/src/main/res/values-pt-rPT/strings.xml @@ -1,6 +1,7 @@ Simple File Picker Selecionar destino + início OK Cancelar diff --git a/simplefilepicker/src/main/res/values-sv/strings.xml b/simplefilepicker/src/main/res/values-sv/strings.xml index a0b5d804..b3d89f2a 100644 --- a/simplefilepicker/src/main/res/values-sv/strings.xml +++ b/simplefilepicker/src/main/res/values-sv/strings.xml @@ -1,6 +1,7 @@ Simple File Picker Välj mål + home OK Cancel diff --git a/simplefilepicker/src/main/res/values/strings.xml b/simplefilepicker/src/main/res/values/strings.xml index de1ed588..2bf173bf 100644 --- a/simplefilepicker/src/main/res/values/strings.xml +++ b/simplefilepicker/src/main/res/values/strings.xml @@ -1,6 +1,7 @@ Simple File Picker Select destination + home OK Cancel