mirror of
https://github.com/SimpleMobileTools/Simple-File-Manager.git
synced 2025-06-05 22:09:15 +02:00
copy a part related to file picking into a separate module
This commit is contained in:
@ -1,174 +0,0 @@
|
||||
package com.simplemobiletools.filemanager;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Point;
|
||||
import android.os.Environment;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.Display;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.simplemobiletools.filemanager.models.FileDirItem;
|
||||
|
||||
public class Breadcrumbs extends LinearLayout implements View.OnClickListener {
|
||||
private int mDeviceWidth;
|
||||
|
||||
private LayoutInflater mInflater;
|
||||
private BreadcrumbsListener mListener;
|
||||
|
||||
public Breadcrumbs(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context);
|
||||
}
|
||||
|
||||
private void init(Context context) {
|
||||
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
|
||||
final Point deviceDisplay = new Point();
|
||||
display.getSize(deviceDisplay);
|
||||
mDeviceWidth = deviceDisplay.x;
|
||||
}
|
||||
|
||||
public void setListener(BreadcrumbsListener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
final int paddingTop = getPaddingTop();
|
||||
final int paddingLeft = getPaddingLeft();
|
||||
final int paddingRight = getPaddingRight();
|
||||
final int childRight = getMeasuredWidth() - paddingRight;
|
||||
final int childBottom = getMeasuredHeight() - getPaddingBottom();
|
||||
final int childHeight = childBottom - paddingTop;
|
||||
|
||||
final int usableWidth = mDeviceWidth - paddingLeft - paddingRight;
|
||||
int maxHeight = 0;
|
||||
int curWidth;
|
||||
int curHeight;
|
||||
int curLeft = paddingLeft;
|
||||
int curTop = paddingTop;
|
||||
|
||||
final int cnt = getChildCount();
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
final View child = getChildAt(i);
|
||||
|
||||
child.measure(MeasureSpec.makeMeasureSpec(usableWidth, MeasureSpec.AT_MOST),
|
||||
MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
|
||||
curWidth = child.getMeasuredWidth();
|
||||
curHeight = child.getMeasuredHeight();
|
||||
|
||||
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
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
final int usableWidth = mDeviceWidth - getPaddingLeft() - getPaddingRight();
|
||||
int width = 0;
|
||||
int rowHeight = 0;
|
||||
int lines = 1;
|
||||
|
||||
final int cnt = getChildCount();
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
final View child = getChildAt(i);
|
||||
measureChild(child, widthMeasureSpec, heightMeasureSpec);
|
||||
width += child.getMeasuredWidth();
|
||||
rowHeight = child.getMeasuredHeight();
|
||||
|
||||
if (width / usableWidth > 0) {
|
||||
lines++;
|
||||
width = child.getMeasuredWidth();
|
||||
}
|
||||
}
|
||||
|
||||
final int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
|
||||
final int calculatedHeight = getPaddingTop() + getPaddingBottom() + (rowHeight * lines);
|
||||
setMeasuredDimension(parentWidth, calculatedHeight);
|
||||
}
|
||||
|
||||
public void setInitialBreadcrumb(String fullPath) {
|
||||
final boolean showFullPath = Config.newInstance(getContext()).getShowFullPath();
|
||||
final String basePath = Environment.getExternalStorageDirectory().toString();
|
||||
String tempPath = fullPath;
|
||||
String currPath = basePath;
|
||||
if (!showFullPath) {
|
||||
tempPath = fullPath.replace(basePath, getContext().getString(R.string.initial_breadcrumb) + "/");
|
||||
} else {
|
||||
currPath = "/";
|
||||
}
|
||||
|
||||
removeAllViewsInLayout();
|
||||
final String[] dirs = tempPath.split("/");
|
||||
for (int i = 0; i < dirs.length; i++) {
|
||||
final String dir = dirs[i];
|
||||
if (i > 0) {
|
||||
currPath += dir + "/";
|
||||
} else if (showFullPath) {
|
||||
addRootFolder();
|
||||
}
|
||||
|
||||
if (dir.isEmpty())
|
||||
continue;
|
||||
|
||||
final FileDirItem item = new FileDirItem(currPath, dir, true, 0, 0);
|
||||
addBreadcrumb(item, i > 0 || showFullPath);
|
||||
}
|
||||
|
||||
if (dirs.length == 0 && showFullPath) {
|
||||
addRootFolder();
|
||||
}
|
||||
}
|
||||
|
||||
public void addBreadcrumb(FileDirItem item, boolean addPrefix) {
|
||||
final View view = mInflater.inflate(R.layout.breadcrumb_item, null, false);
|
||||
final TextView textView = (TextView) view.findViewById(R.id.breadcrumb_text);
|
||||
|
||||
String textToAdd = item.getName();
|
||||
if (addPrefix)
|
||||
textToAdd = " -> " + textToAdd;
|
||||
textView.setText(textToAdd);
|
||||
addView(view);
|
||||
view.setOnClickListener(this);
|
||||
|
||||
view.setTag(item);
|
||||
}
|
||||
|
||||
public void removeBreadcrumb() {
|
||||
removeView(getChildAt(getChildCount() - 1));
|
||||
}
|
||||
|
||||
private void addRootFolder() {
|
||||
final FileDirItem item = new FileDirItem("/", " / ", true, 0, 0);
|
||||
addBreadcrumb(item, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final int cnt = getChildCount();
|
||||
for (int i = 0; i < cnt; i++) {
|
||||
if (getChildAt(i) != null && getChildAt(i).equals(v)) {
|
||||
if (mListener != null) {
|
||||
mListener.breadcrumbClicked(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface BreadcrumbsListener {
|
||||
void breadcrumbClicked(int id);
|
||||
}
|
||||
}
|
@ -0,0 +1,166 @@
|
||||
package com.simplemobiletools.filemanager
|
||||
|
||||
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 android.widget.TextView
|
||||
import com.simplemobiletools.filemanager.models.FileDirItem
|
||||
|
||||
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 = 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)
|
||||
val textView = view.findViewById(R.id.breadcrumb_text) as TextView
|
||||
|
||||
var textToAdd = item.name
|
||||
if (addPrefix)
|
||||
textToAdd = " -> " + textToAdd
|
||||
textView.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)
|
||||
}
|
||||
}
|
@ -77,11 +77,11 @@ class SelectFolderDialog : DialogFragment() {
|
||||
}
|
||||
|
||||
private fun setupBreadcrumbs() {
|
||||
dialog.directory_picker_breadcrumbs.setListener { id ->
|
||||
/*dialog.directory_picker_breadcrumbs.setListener { id ->
|
||||
val item = dialog.directory_picker_breadcrumbs.getChildAt(id).tag as FileDirItem
|
||||
mPath = item.path
|
||||
updateItems()
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
private fun getItems(path: String): List<FileDirItem> {
|
||||
@ -99,10 +99,9 @@ class SelectFolderDialog : DialogFragment() {
|
||||
|
||||
val curPath = file.absolutePath
|
||||
val curName = Utils.getFilename(curPath)
|
||||
val children = getChildren(file)
|
||||
val size = file.length()
|
||||
|
||||
items.add(FileDirItem(curPath, curName, file.isDirectory, children, size))
|
||||
items.add(FileDirItem(curPath, curName, file.isDirectory, getChildren(file), size))
|
||||
}
|
||||
}
|
||||
return items
|
||||
|
Reference in New Issue
Block a user