2019-06-30 15:44:53 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-01-05 17:32:53 +00:00
|
|
|
Copyright 2018-2020 by Marcel Bokhorst (M66B)
|
2019-06-30 15:44:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.content.Context;
|
2020-03-23 12:44:29 +00:00
|
|
|
import android.content.SharedPreferences;
|
2019-06-30 15:44:53 +00:00
|
|
|
import android.os.Bundle;
|
2019-12-08 16:22:55 +00:00
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
2019-06-30 15:44:53 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2020-03-23 12:44:29 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.AutoCompleteTextView;
|
2019-12-08 16:22:55 +00:00
|
|
|
import android.widget.ImageButton;
|
2019-07-27 16:37:29 +00:00
|
|
|
import android.widget.TextView;
|
2019-06-30 15:44:53 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2019-06-30 20:27:49 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2020-02-01 20:23:22 +00:00
|
|
|
import androidx.constraintlayout.widget.Group;
|
2020-03-23 12:44:29 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2019-06-30 15:44:53 +00:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
2020-03-23 12:44:29 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2019-06-30 15:44:53 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static android.app.Activity.RESULT_OK;
|
|
|
|
|
2019-09-11 19:43:27 +00:00
|
|
|
public class FragmentDialogFolder extends FragmentDialogBase {
|
2019-12-08 16:22:55 +00:00
|
|
|
private int result = 0;
|
|
|
|
|
2020-03-23 12:44:29 +00:00
|
|
|
private static final int MAX_SELECTED_FOLDERS = 5;
|
|
|
|
|
2019-06-30 15:44:53 +00:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
2019-07-30 17:22:28 +00:00
|
|
|
final String title = getArguments().getString("title");
|
|
|
|
final long account = getArguments().getLong("account");
|
|
|
|
final long[] disabled = getArguments().getLongArray("disabled");
|
2019-06-30 15:44:53 +00:00
|
|
|
|
2020-03-23 12:44:29 +00:00
|
|
|
List<String> selected_folders = new ArrayList<>();
|
|
|
|
|
|
|
|
final Context context = getContext();
|
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
String json = prefs.getString("selected_folders", "[]");
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSONArray jarray = new JSONArray(json);
|
|
|
|
for (int i = 0; i < jarray.length(); i++)
|
|
|
|
selected_folders.add((String) jarray.get(i));
|
|
|
|
} catch (JSONException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
final View dview = LayoutInflater.from(context).inflate(R.layout.dialog_folder_select, null);
|
2019-07-27 16:37:29 +00:00
|
|
|
final TextView tvNoFolder = dview.findViewById(R.id.tvNoFolder);
|
2020-03-23 12:44:29 +00:00
|
|
|
final AutoCompleteTextView etSearch = dview.findViewById(R.id.etSearch);
|
2019-12-08 16:22:55 +00:00
|
|
|
final ImageButton ibNext = dview.findViewById(R.id.ibNext);
|
2019-06-30 15:44:53 +00:00
|
|
|
final RecyclerView rvFolder = dview.findViewById(R.id.rvFolder);
|
|
|
|
final ContentLoadingProgressBar pbWait = dview.findViewById(R.id.pbWait);
|
2020-02-01 20:23:22 +00:00
|
|
|
final Group grpReady = dview.findViewById(R.id.grpReady);
|
2019-06-30 15:44:53 +00:00
|
|
|
|
2020-03-23 12:44:29 +00:00
|
|
|
etSearch.setThreshold(1);
|
|
|
|
etSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onFocusChange(View v, boolean hasFocus) {
|
|
|
|
if (hasFocus)
|
2020-04-04 12:09:14 +00:00
|
|
|
try {
|
|
|
|
etSearch.showDropDown();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
/*
|
|
|
|
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
|
|
|
|
at android.view.ViewRootImpl.setView(ViewRootImpl.java:958)
|
|
|
|
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:381)
|
|
|
|
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:100)
|
|
|
|
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1467)
|
|
|
|
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1316)
|
|
|
|
at android.widget.ListPopupWindow.show(ListPopupWindow.java:740)
|
|
|
|
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1226)
|
|
|
|
at eu.faircode.email.FragmentDialogFolder$1.onFocusChange(SourceFile:91)
|
|
|
|
at android.view.View.onFocusChanged(View.java:7573)
|
|
|
|
at android.widget.TextView.onFocusChanged(TextView.java:10958)
|
|
|
|
at android.widget.EditText.onFocusChanged(EditText.java:277)
|
|
|
|
at android.widget.AutoCompleteTextView.onFocusChanged(AutoCompleteTextView.java:1125)
|
|
|
|
*/
|
|
|
|
}
|
2020-03-23 12:44:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ArrayAdapter<String> frequent =
|
|
|
|
new ArrayAdapter<>(context, R.layout.spinner_item1_dropdown, android.R.id.text1, selected_folders);
|
|
|
|
etSearch.setAdapter(frequent);
|
|
|
|
|
2019-06-30 15:44:53 +00:00
|
|
|
rvFolder.setHasFixedSize(false);
|
2020-03-23 12:44:29 +00:00
|
|
|
final LinearLayoutManager llm = new LinearLayoutManager(context);
|
2019-06-30 15:44:53 +00:00
|
|
|
rvFolder.setLayoutManager(llm);
|
|
|
|
|
2020-03-23 12:44:29 +00:00
|
|
|
final AdapterFolder adapter = new AdapterFolder(context, getViewLifecycleOwner(),
|
2020-04-15 18:53:46 +00:00
|
|
|
account, false, false, false, new AdapterFolder.IFolderSelectedListener() {
|
2019-07-30 17:22:28 +00:00
|
|
|
@Override
|
|
|
|
public void onFolderSelected(TupleFolderEx folder) {
|
2020-03-23 12:44:29 +00:00
|
|
|
String name = folder.getDisplayName(context, folder.parent_ref);
|
|
|
|
selected_folders.remove(name);
|
|
|
|
selected_folders.add(0, name);
|
|
|
|
while (selected_folders.size() > MAX_SELECTED_FOLDERS)
|
|
|
|
selected_folders.remove(MAX_SELECTED_FOLDERS);
|
|
|
|
JSONArray jarray = new JSONArray(selected_folders);
|
|
|
|
prefs.edit().putString("selected_folders", jarray.toString()).apply();
|
|
|
|
|
2019-07-30 17:22:28 +00:00
|
|
|
Bundle args = getArguments();
|
|
|
|
args.putLong("folder", folder.id);
|
|
|
|
|
|
|
|
sendResult(RESULT_OK);
|
|
|
|
dismiss();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
rvFolder.setAdapter(adapter);
|
|
|
|
|
2019-12-08 16:22:55 +00:00
|
|
|
etSearch.addTextChangedListener(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
|
|
result = 0;
|
|
|
|
String query = s.toString().toLowerCase();
|
2020-02-05 12:37:36 +00:00
|
|
|
adapter.search(query, result, new AdapterFolder.ISearchResult() {
|
|
|
|
@Override
|
|
|
|
public void onFound(int pos, boolean hasNext) {
|
|
|
|
ibNext.setEnabled(hasNext);
|
|
|
|
llm.scrollToPositionWithOffset(pos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotFound() {
|
|
|
|
ibNext.setEnabled(false);
|
|
|
|
}
|
|
|
|
});
|
2019-12-08 16:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ibNext.setEnabled(false);
|
|
|
|
ibNext.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
result++;
|
|
|
|
String query = etSearch.getText().toString();
|
2020-02-05 12:37:36 +00:00
|
|
|
adapter.search(query, result, new AdapterFolder.ISearchResult() {
|
|
|
|
@Override
|
|
|
|
public void onFound(int pos, boolean hasNext) {
|
|
|
|
ibNext.setEnabled(hasNext);
|
|
|
|
llm.scrollToPositionWithOffset(pos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNotFound() {
|
|
|
|
ibNext.setEnabled(false);
|
|
|
|
}
|
|
|
|
});
|
2019-12-08 16:22:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-30 15:44:53 +00:00
|
|
|
Bundle args = new Bundle();
|
2019-07-30 17:22:28 +00:00
|
|
|
args.putLong("account", account);
|
2019-06-30 15:44:53 +00:00
|
|
|
|
|
|
|
new SimpleTask<List<TupleFolderEx>>() {
|
|
|
|
@Override
|
2019-07-27 16:37:29 +00:00
|
|
|
protected void onPreExecute(Bundle args) {
|
|
|
|
tvNoFolder.setVisibility(View.GONE);
|
2020-02-01 20:23:22 +00:00
|
|
|
grpReady.setVisibility(View.GONE);
|
2019-07-27 16:37:29 +00:00
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
pbWait.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-07-31 06:51:18 +00:00
|
|
|
protected List<TupleFolderEx> onExecute(Context context, Bundle args) {
|
2019-06-30 15:44:53 +00:00
|
|
|
long account = args.getLong("account");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
return db.folder().getFoldersEx(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(final Bundle args, List<TupleFolderEx> folders) {
|
2019-07-30 18:57:22 +00:00
|
|
|
if (folders == null || folders.size() == 0)
|
2019-07-27 16:37:29 +00:00
|
|
|
tvNoFolder.setVisibility(View.VISIBLE);
|
|
|
|
else {
|
2019-07-30 17:22:28 +00:00
|
|
|
adapter.setDisabled(Helper.fromLongArray(disabled));
|
|
|
|
adapter.set(folders);
|
2020-02-01 20:23:22 +00:00
|
|
|
grpReady.setVisibility(View.VISIBLE);
|
2019-07-27 16:37:29 +00:00
|
|
|
}
|
2019-06-30 15:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-12-06 07:50:46 +00:00
|
|
|
Log.unexpectedError(getParentFragmentManager(), ex);
|
2019-06-30 15:44:53 +00:00
|
|
|
}
|
2019-09-11 12:03:59 +00:00
|
|
|
}.execute(this, args, "folder:select");
|
2019-06-30 15:44:53 +00:00
|
|
|
|
2020-03-23 12:44:29 +00:00
|
|
|
return new AlertDialog.Builder(context)
|
2019-06-30 15:44:53 +00:00
|
|
|
.setTitle(title)
|
|
|
|
.setView(dview)
|
2019-09-12 11:23:32 +00:00
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
2019-06-30 15:44:53 +00:00
|
|
|
.create();
|
|
|
|
}
|
|
|
|
}
|