Skip to content

Commit

Permalink
Recent projects and directories in file selector (#499)
Browse files Browse the repository at this point in the history
Implement sections in file selector that show 3 recent directories and 5 projects.
  • Loading branch information
marioba committed Mar 5, 2019
1 parent e12c290 commit 83b5ddd
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
20 changes: 20 additions & 0 deletions android/res/layout/list_separator.xml
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AAAAAA"
android:gravity="center_vertical">

<TextView
android:id="@+id/separator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="8dp"
android:textSize="18dp"
android:text=""
android:textAllCaps="false"
android:textColor="#FFF"
android:visibility="visible" />

</RelativeLayout>
2 changes: 2 additions & 0 deletions android/res/values/strings.xml
Expand Up @@ -12,4 +12,6 @@
<string name="inaccessible">inaccessible</string>
<string name="loading">Loading</string>
<string name="select_project">Select a QGIS project</string>
<string name="recent_projects">Recent projects</string>
<string name="recent_directories">Recent directories</string>
</resources>
84 changes: 76 additions & 8 deletions android/src/ch/opengis/qfield/QFieldProjectActivity.java
Expand Up @@ -12,6 +12,8 @@
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.Window;
Expand All @@ -20,17 +22,20 @@
import android.widget.ListView;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;

public class QFieldProjectActivity extends ListActivity{

private static final String TAG = "QField Project Activity";
private String path;
private SharedPreferences sharedPreferences;

@Override
public void onCreate(Bundle bundle) {
Log.d(TAG, "onCreate() ");
super.onCreate(bundle);

sharedPreferences = getPreferences(Context.MODE_PRIVATE);
setContentView(R.layout.list_projects);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80CC28")));

Expand All @@ -43,8 +48,8 @@ public void onCreate(Bundle bundle) {
Log.d(TAG, "externalStorageDirectory: " + externalStorageDirectory);
if (externalStorageDirectory != null){
values.add(new QFieldProjectListItem(externalStorageDirectory, getString(R.string.primary_storage),
R.drawable.tablet));
}
R.drawable.tablet, QFieldProjectListItem.TYPE_ITEM));
}

File[] externalFilesDirs = getExternalFilesDirs(null);
Log.d(TAG, "externalFilesDirs: " + Arrays.toString(externalFilesDirs));
Expand All @@ -53,15 +58,43 @@ public void onCreate(Bundle bundle) {
// Don't add a external storage path if already included in the primary one
if(externalStorageDirectory != null){
if (!file.getAbsolutePath().contains(externalStorageDirectory.getAbsolutePath())){
values.add(new QFieldProjectListItem(file, getString(R.string.secondary_storage), R.drawable.card));
values.add(new QFieldProjectListItem(file, getString(R.string.secondary_storage), R.drawable.card, QFieldProjectListItem.TYPE_ITEM));
}
}else{
values.add(new QFieldProjectListItem(file, getString(R.string.secondary_storage), R.drawable.card));
values.add(new QFieldProjectListItem(file, getString(R.string.secondary_storage), R.drawable.card, QFieldProjectListItem.TYPE_ITEM));
}
}
}

setTitle(getString(R.string.select_project));
Collections.sort(values);

String lastUsedDirs = sharedPreferences.getString("LastUsedDirectories", null);
Log.d(TAG, "lastUsedDirs: " + lastUsedDirs);
if (lastUsedDirs != null){
String[] lastUsedDirsArray = lastUsedDirs.split("--;--");
values.add(new QFieldProjectListItem(null, getString(R.string.recent_directories), 0, QFieldProjectListItem.TYPE_SEPARATOR));

for (int i=lastUsedDirsArray.length-1; i>=0; i--) {
File f = new File(lastUsedDirsArray[i]);
if(f.exists()){
values.add(new QFieldProjectListItem(f, f.getName(), R.drawable.directory, QFieldProjectListItem.TYPE_ITEM));
}
}
}

String lastUsedProjects = sharedPreferences.getString("LastUsedProjects", null);
if (lastUsedProjects != null){
String[] lastUsedProjectsArray = lastUsedProjects.split("--;--");
values.add(new QFieldProjectListItem(null, getString(R.string.recent_projects), 0, QFieldProjectListItem.TYPE_SEPARATOR));

for (int i=lastUsedProjectsArray.length-1; i>=0; i--) {
File f = new File(lastUsedProjectsArray[i]);
if (f.exists()){
values.add(new QFieldProjectListItem(f, f.getName(), R.drawable.icon, QFieldProjectListItem.TYPE_ITEM));
}
}
}

}else{ // Over the roots
Log.d(TAG, "extra path: " + getIntent().getStringExtra("path"));
Expand All @@ -78,16 +111,15 @@ public void onCreate(Bundle bundle) {
if (file.getName().startsWith(".")) {
continue;
}else if (file.getName().toLowerCase().endsWith(".qgs")){
values.add(new QFieldProjectListItem(file, file.getName(), R.drawable.icon));
values.add(new QFieldProjectListItem(file, file.getName(), R.drawable.icon, QFieldProjectListItem.TYPE_ITEM));
}else if (file.isDirectory()){
values.add(new QFieldProjectListItem(file, file.getName(), R.drawable.directory));
values.add(new QFieldProjectListItem(file, file.getName(), R.drawable.directory, QFieldProjectListItem.TYPE_ITEM));
}
}
}
Collections.sort(values);
}

Collections.sort(values);

// Put the data into the list
QFieldProjectListAdapter adapter = new QFieldProjectListAdapter(this, values);
setListAdapter(adapter);
Expand All @@ -98,6 +130,9 @@ protected void onListItemClick(ListView l, View v, int position, long id) {
Log.d(TAG, "onListItemClick ");

QFieldProjectListItem item = (QFieldProjectListItem) getListAdapter().getItem(position);
if (item.getType() == QFieldProjectListItem.TYPE_SEPARATOR){
return;
}
File file = item.getFile();
Log.d(TAG, "file: "+file.getPath());
if (file.isDirectory()) {
Expand All @@ -113,6 +148,39 @@ protected void onListItemClick(ListView l, View v, int position, long id) {

Toast.makeText(this, getString(R.string.loading) + " " + file.getPath(), Toast.LENGTH_LONG).show();
setResult(Activity.RESULT_OK, data);

String lastUsedDirs = sharedPreferences.getString("LastUsedDirectories", null);
ArrayList<String> lastUsedDirsArray = new ArrayList<String>();
if (lastUsedDirs != null){
lastUsedDirsArray = new ArrayList<String>(Arrays.asList(lastUsedDirs.split("--;--")));
}
// If the element is already present, delete it. It will be added again in the last position
lastUsedDirsArray.remove(file.getParent());
if (lastUsedDirsArray.size() >= 3){
lastUsedDirsArray.remove(0);
}
// Add the directory to the array
lastUsedDirsArray.add(file.getParent());

String lastUsedProjects = sharedPreferences.getString("LastUsedProjects", null);
ArrayList<String> lastUsedProjectsArray = new ArrayList<String>();
if (lastUsedProjects != null){
lastUsedProjectsArray = new ArrayList<String>(Arrays.asList(lastUsedProjects.split("--;--")));
}
// If the element is already present, delete it. It will be added again in the last position
lastUsedProjectsArray.remove(file.getPath());
if (lastUsedProjectsArray.size() >= 5){
lastUsedProjectsArray.remove(0);
}
// Add the project path to the array
lastUsedProjectsArray.add(file.getPath());

// Write the recent directories and projects into the shared preferences
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("LastUsedDirectories", TextUtils.join("--;--", lastUsedDirsArray));
editor.putString("LastUsedProjects", TextUtils.join("--;--", lastUsedProjectsArray));
editor.commit();

finish();
}
}
Expand Down
6 changes: 6 additions & 0 deletions android/src/ch/opengis/qfield/QFieldProjectListAdapter.java
Expand Up @@ -42,6 +42,12 @@ public View getView(int position, View convertView, ViewGroup parent) {
imgView.setImageResource(item.getImageId());
imgView.setImageAlpha(172);
titleView.setText(item.getText());

if (item.getType() == QFieldProjectListItem.TYPE_SEPARATOR){
rowView = inflater.inflate(R.layout.list_separator, null);
TextView separatorView = (TextView) rowView.findViewById(R.id.separator);
separatorView.setText(item.getText());
}

return rowView;
}
Expand Down
11 changes: 10 additions & 1 deletion android/src/ch/opengis/qfield/QFieldProjectListItem.java
Expand Up @@ -7,11 +7,16 @@ public class QFieldProjectListItem implements Comparable<QFieldProjectListItem>{
private File file;
private String text;
private int imageId;
private int type;

public QFieldProjectListItem(File file, String text, int imageId){
public static final int TYPE_ITEM = 0;
public static final int TYPE_SEPARATOR = 1;

public QFieldProjectListItem(File file, String text, int imageId, int type){
this.file = file;
this.text = text;
this.imageId = imageId;
this.type = type;
}

public File getFile(){
Expand All @@ -26,6 +31,10 @@ public int getImageId(){
return this.imageId;
}

public int getType(){
return this.type;
}

@Override
public int compareTo(QFieldProjectListItem item){
return this.file.getName().compareToIgnoreCase(item.getFile().getName());
Expand Down

1 comment on commit 83b5ddd

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uploaded test apks for armv7 and x86

Please sign in to comment.