Skip to content

Commit

Permalink
Directory chooser: Better navigation, added "Select default storage" …
Browse files Browse the repository at this point in the history
…button
xroche committed Apr 11, 2017
1 parent c53a2b7 commit bc167d1
Showing 3 changed files with 80 additions and 12 deletions.
76 changes: 64 additions & 12 deletions app/src/main/java/com/httrack/android/FileChooserActivity.java
Original file line number Diff line number Diff line change
@@ -8,6 +8,9 @@
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.Pair;
import android.view.Gravity;
import android.view.View;
@@ -24,6 +27,8 @@ public class FileChooserActivity extends Activity implements

protected File projectRootFile;
protected File defaultRootFile;
protected File sdcardRootFile;

protected final List<Pair<String, File>> files = new ArrayList<Pair<String, File>>();

// Handler to execute code in UI thread
@@ -38,30 +43,41 @@ private int dpToPx(final double dp) {
return px;
}

// Set file directory
private void setFile(final File file) {
final EditText base = EditText.class.cast(findViewById(R.id.fieldBasePath));
final String path = file.getAbsolutePath();
base.setText(path);
base.setSelection(path.length());
}

/*
* Create all tabs for main options menu.
*/
private void refreshDir() {
// Set text
final EditText base = EditText.class.cast(findViewById(R.id.fieldBasePath));
final String path = projectRootFile.getAbsolutePath();
base.setText(projectRootFile.getAbsolutePath());
base.setSelection(path.length());
// Clear list
files.clear();

// Find parent
final File parent = projectRootFile != null && projectRootFile.exists() ? projectRootFile.getParentFile() : null;
if (parent != null && projectRootFile != null && !parent.getAbsolutePath().equals(projectRootFile.getAbsolutePath())) {
files.add(new Pair<String, File>(getString(R.string.ellipsis), parent));
Log.d("httrack", "found parent of " + projectRootFile.getAbsolutePath() + ": " + parent.getAbsolutePath());
} else {
files.add(new Pair<String, File>(getString(R.string.ellipsis), new File("/")));
Log.d("httrack", "could not find found parent of " + projectRootFile.getAbsolutePath() + ", using /");
}

// List directory
final File[] list = projectRootFile != null && projectRootFile.exists() ? projectRootFile
.listFiles() : defaultRootFile.listFiles();
if (list != null) {
files.clear();
final File parent = projectRootFile.getParentFile();
if (parent != null) {
files.add(new Pair<String, File>(getString(R.string.ellipsis), parent));
}
for (final File file : list) {
if (file.isDirectory() && !file.isHidden() /* && file.canWrite() */) {
files.add(new Pair<String, File>(file.getName(), file));
}
}

}

if (files != null) {
@@ -125,15 +141,43 @@ protected void onCreate(final Bundle savedInstanceState) {
// Fetch args from parent
final Bundle extras = getIntent().getExtras();
projectRootFile = File.class.cast(extras
.get("com.httrack.android.rootFile"));
.get("com.httrack.android.rootFile"));
defaultRootFile = File.class.cast(extras
.get("com.httrack.android.defaultHTTrackPath"));
.get("com.httrack.android.defaultHTTrackPath"));
sdcardRootFile = File.class.cast(extras
.get("com.httrack.android.sdcardHTTrackPath"));
if (projectRootFile == null || defaultRootFile == null) {
throw new RuntimeException("internal error");
}

// Initial dir
setFile(projectRootFile);
refreshDir();

final EditText base = EditText.class.cast(findViewById(R.id.fieldBasePath));
base.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(final Editable s) {
// Refresh in realtime
final File f = new File(s.toString());
if (f.exists() && f.isDirectory() && !f.equals(projectRootFile)) {
projectRootFile = f;
refreshDir();
}
}

// NOOP
@Override
public void onTextChanged(final CharSequence s, final int start,
final int before, final int count) {
}

// NOOP
@Override
public void beforeTextChanged(final CharSequence s, final int start,
final int count, final int after) {
}
});
}

@Override
@@ -146,10 +190,12 @@ public void onClick(final View v) {

if (position >= 0 && files != null && position < files.size()) {
projectRootFile = files.get(position).second;
Log.d("httrack", "clicked " + projectRootFile.getAbsolutePath());
}
handlerUI.post(new Runnable() {
@Override
public void run() {
setFile(projectRootFile);
refreshDir();
}
});
@@ -164,8 +210,14 @@ public void onClickApply(final View v) {
final Intent intent = new Intent();
intent.putExtra("com.httrack.android.rootFile", path);
setResult(Activity.RESULT_OK, intent);
Log.d("httrack", "applied " + projectRootFile.getAbsolutePath());

// Finish activity
finish();
}

public void onClickDefaultStorage(final View v) {
setFile(defaultRootFile);
refreshDir();
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_file_chooser.xml
Original file line number Diff line number Diff line change
@@ -28,6 +28,21 @@
</EditText>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal" >

<Button
android:id="@+id/buttonDefaultStorage"
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickDefaultStorage"
android:text="@string/default_storage" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -203,5 +203,6 @@
<string name="load_default">Load default options</string>
<string name="save_default">Save default options</string>
<string name="reset_default">Reset to default options</string>
<string name="default_storage">Select default storage</string>

</resources>

0 comments on commit bc167d1

Please sign in to comment.