Solve bug of titleFileList

This commit is contained in:
J-Jamet committed 2018-01-14 20:11:13 +01:00
1 parent 42a52b26bf
commit 2ca4e817e9
4 files changed
+139 -63

No files matched your search

@@ -0,0 +1,53 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.fileselect;
import android.net.Uri;
import android.os.AsyncTask;
class DeleteFileHistoryAsyncTask extends AsyncTask<String, Void, Void> {
private AfterDeleteFileHistoryListener afterDeleteFileHistoryListener;
private RecentFileHistory fileHistory;
private FileSelectAdapter adapter;
DeleteFileHistoryAsyncTask(AfterDeleteFileHistoryListener afterDeleteFileHistoryListener, RecentFileHistory fileHistory, FileSelectAdapter adapter) {
this.afterDeleteFileHistoryListener = afterDeleteFileHistoryListener;
this.fileHistory = fileHistory;
this.adapter = adapter;
}
protected Void doInBackground(String... args) {
fileHistory.deleteFile(Uri.parse(args[0]));
return null;
}
protected void onPostExecute(Void v) {
adapter.notifyDataSetChanged();
if (adapter.getItemCount() == 0) {
if(afterDeleteFileHistoryListener != null)
afterDeleteFileHistoryListener.afterDeleteFile();
}
}
public interface AfterDeleteFileHistoryListener {
void afterDeleteFile();
}
}
@@ -20,14 +20,12 @@
package com.keepassdroid.fileselect;
import android.Manifest;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
@@ -68,7 +66,6 @@ import com.kunzisoft.keepass.R;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.URLDecoder;
public class FileSelectActivity extends StylishActivity implements
@@ -81,6 +78,7 @@ public class FileSelectActivity extends StylishActivity implements
private static final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE = 111;
private RecyclerView mListFiles;
private FileSelectAdapter mAdapter;
private View fileListTitle;
public static final int FILE_BROWSE = 1;
public static final int GET_CONTENT = 2;
@@ -102,11 +100,10 @@ public class FileSelectActivity extends StylishActivity implements
fileHistory = App.getFileHistory();
setContentView(R.layout.file_selection);
fileListTitle = findViewById(R.id.file_list_title);
if (fileHistory.hasRecentFiles()) {
recentMode = true;
} else {
findViewById(R.id.file_listtop).setVisibility(View.INVISIBLE);
}
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.app_name));
@@ -236,6 +233,13 @@ public class FileSelectActivity extends StylishActivity implements
}
}
private void updateTitleFileListView() {
if(mAdapter.getItemCount() == 0)
fileListTitle.setVisibility(View.INVISIBLE);
else
fileListTitle.setVisibility(View.VISIBLE);
}
/**
* Create file for database
* @return If not created, return false
@@ -374,6 +378,7 @@ public class FileSelectActivity extends StylishActivity implements
// Add to recent files
fileHistory.createFile(mUri, getFilename());
mAdapter.notifyDataSetChanged();
updateTitleFileListView();
GroupActivity.Launch(FileSelectActivity.this);
}
}
@@ -385,7 +390,24 @@ public class FileSelectActivity extends StylishActivity implements
new View.OnClickListener() {
public void onClick(View view) {
int itemPosition = mListFiles.getChildLayoutPosition(view);
new OpenFileHistoryAsyncTask(FileSelectActivity.this, fileHistory).execute(itemPosition);
new OpenFileHistoryAsyncTask(new OpenFileHistoryAsyncTask.AfterOpenFileHistoryListener() {
@Override
public void afterOpenFile(String fileName, String keyFile) {
try {
PasswordActivity.Launch(FileSelectActivity.this,
fileName, keyFile);
} catch (ContentFileNotFoundException e) {
Toast.makeText(FileSelectActivity.this,
R.string.file_not_found_content, Toast.LENGTH_LONG)
.show();
} catch (FileNotFoundException e) {
Toast.makeText(FileSelectActivity.this,
R.string.FileNotFound, Toast.LENGTH_LONG)
.show();
}
updateTitleFileListView();
}
}, fileHistory).execute(itemPosition);
}
}
);
@@ -395,7 +417,12 @@ public class FileSelectActivity extends StylishActivity implements
@Override
public boolean onFileSelectClearListener(String fileName) {
new DeleteFileHistoryAsyncTask(fileHistory, mAdapter).execute(fileName);
new DeleteFileHistoryAsyncTask(new DeleteFileHistoryAsyncTask.AfterDeleteFileHistoryListener() {
@Override
public void afterDeleteFile() {
updateTitleFileListView();
}
}, fileHistory, mAdapter).execute(fileName);
return true;
}
@@ -530,58 +557,4 @@ public class FileSelectActivity extends StylishActivity implements
&& super.onOptionsItemSelected(item);
}
private static class OpenFileHistoryAsyncTask extends AsyncTask<Integer, Void, Void> {
private WeakReference<Activity> weakActivity;
private RecentFileHistory fileHistory;
private String fileName;
private String keyFile;
OpenFileHistoryAsyncTask(Activity activity, RecentFileHistory fileHistory) {
this.weakActivity = new WeakReference<>(activity);
this.fileHistory = fileHistory;
}
protected Void doInBackground(Integer... args) {
int position = args[0];
fileName = fileHistory.getDatabaseAt(position);
keyFile = fileHistory.getKeyfileAt(position);
return null;
}
protected void onPostExecute(Void v) {
try {
PasswordActivity.Launch(weakActivity.get(), fileName, keyFile);
}
catch (ContentFileNotFoundException e) {
Toast.makeText(weakActivity.get(), R.string.file_not_found_content, Toast.LENGTH_LONG)
.show();
}
catch (FileNotFoundException e) {
Toast.makeText(weakActivity.get(), R.string.FileNotFound, Toast.LENGTH_LONG)
.show();
}
}
}
private static class DeleteFileHistoryAsyncTask extends AsyncTask<String, Void, Void> {
private RecentFileHistory fileHistory;
private FileSelectAdapter adapter;
DeleteFileHistoryAsyncTask(RecentFileHistory fileHistory, FileSelectAdapter adapter) {
this.fileHistory = fileHistory;
this.adapter = adapter;
}
protected java.lang.Void doInBackground(String... args) {
fileHistory.deleteFile(Uri.parse(args[0]));
return null;
}
protected void onPostExecute(Void v) {
adapter.notifyDataSetChanged();
}
}
}
@@ -0,0 +1,50 @@
/*
* Copyright 2018 Jeremy Jamet / Kunzisoft.
*
* This file is part of KeePass DX.
*
* KeePass DX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePass DX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePass DX. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.fileselect;
import android.os.AsyncTask;
class OpenFileHistoryAsyncTask extends AsyncTask<Integer, Void, Void> {
private AfterOpenFileHistoryListener afterOpenFileHistoryListener;
private RecentFileHistory fileHistory;
private String fileName;
private String keyFile;
OpenFileHistoryAsyncTask(AfterOpenFileHistoryListener afterOpenFileHistoryListener, RecentFileHistory fileHistory) {
this.afterOpenFileHistoryListener = afterOpenFileHistoryListener;
this.fileHistory = fileHistory;
}
protected Void doInBackground(Integer... args) {
int position = args[0];
fileName = fileHistory.getDatabaseAt(position);
keyFile = fileHistory.getKeyfileAt(position);
return null;
}
protected void onPostExecute(Void v) {
afterOpenFileHistoryListener.afterOpenFile(fileName, keyFile);
}
public interface AfterOpenFileHistoryListener {
void afterOpenFile(String fileName, String keyFile);
}
}
+1 -1
View File
@@ -84,7 +84,7 @@
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="@+id/file_listtop"
<TextView android:id="@+id/file_list_title"
android:layout_marginTop="@dimen/default_margin"
android:layout_marginLeft="@dimen/default_margin"
android:layout_marginStart="@dimen/default_margin"