2018-08-02 13:33:06 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
|
|
/*
|
2018-08-14 05:53:24 +00:00
|
|
|
This file is part of FairEmail.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
2018-08-02 13:33:06 +00:00
|
|
|
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.
|
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-02 13:33:06 +00:00
|
|
|
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
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-12-31 08:04:33 +00:00
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
|
2019-02-28 10:43:46 +00:00
|
|
|
import android.content.Context;
|
2019-02-28 16:48:18 +00:00
|
|
|
import android.content.Intent;
|
2018-09-08 16:32:13 +00:00
|
|
|
import android.content.SharedPreferences;
|
2019-02-28 10:43:46 +00:00
|
|
|
import android.graphics.Color;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.os.Bundle;
|
2018-09-08 16:32:13 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.view.LayoutInflater;
|
2019-02-21 07:34:23 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
|
import android.view.MenuItem;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
2018-09-08 16:32:13 +00:00
|
|
|
import android.widget.ImageButton;
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
2019-02-28 10:43:46 +00:00
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
2018-08-08 06:55:47 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
import androidx.constraintlayout.widget.Group;
|
2018-09-14 08:15:45 +00:00
|
|
|
import androidx.fragment.app.FragmentTransaction;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.lifecycle.Observer;
|
2019-02-28 16:48:18 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2019-02-28 10:43:46 +00:00
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
2018-08-08 06:55:47 +00:00
|
|
|
|
2019-01-15 17:41:55 +00:00
|
|
|
public class FragmentFolders extends FragmentBase {
|
2019-02-28 10:43:46 +00:00
|
|
|
private ViewGroup view;
|
|
|
|
|
private SwipeRefreshLayout swipeRefresh;
|
2018-09-08 16:32:13 +00:00
|
|
|
private ImageButton ibHintActions;
|
2018-12-27 14:48:30 +00:00
|
|
|
private ImageButton ibHintSync;
|
2018-08-02 13:33:06 +00:00
|
|
|
private RecyclerView rvFolder;
|
2018-12-27 12:31:02 +00:00
|
|
|
private ContentLoadingProgressBar pbWait;
|
2018-09-08 16:32:13 +00:00
|
|
|
private Group grpHintActions;
|
2018-12-27 14:48:30 +00:00
|
|
|
private Group grpHintSync;
|
2018-08-02 13:33:06 +00:00
|
|
|
private Group grpReady;
|
|
|
|
|
private FloatingActionButton fab;
|
|
|
|
|
|
2018-09-03 19:08:50 +00:00
|
|
|
private long account;
|
2018-08-02 13:33:06 +00:00
|
|
|
private AdapterFolder adapter;
|
|
|
|
|
|
2019-02-21 07:34:23 +00:00
|
|
|
private Boolean show_hidden = null;
|
|
|
|
|
|
2018-09-03 19:08:50 +00:00
|
|
|
@Override
|
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
// Get arguments
|
|
|
|
|
Bundle args = getArguments();
|
2018-12-02 10:09:21 +01:00
|
|
|
account = args.getLong("account", -1);
|
2018-09-03 19:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
|
@Nullable
|
|
|
|
|
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
2019-02-21 07:34:23 +00:00
|
|
|
setHasOptionsMenu(true);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2019-02-28 10:43:46 +00:00
|
|
|
view = (ViewGroup) inflater.inflate(R.layout.fragment_folders, container, false);
|
2019-02-21 21:17:09 +01:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// Get controls
|
2019-02-28 10:43:46 +00:00
|
|
|
swipeRefresh = view.findViewById(R.id.swipeRefresh);
|
2018-09-08 16:32:13 +00:00
|
|
|
ibHintActions = view.findViewById(R.id.ibHintActions);
|
2018-12-27 14:48:30 +00:00
|
|
|
ibHintSync = view.findViewById(R.id.ibHintSync);
|
2018-08-02 13:33:06 +00:00
|
|
|
rvFolder = view.findViewById(R.id.rvFolder);
|
|
|
|
|
pbWait = view.findViewById(R.id.pbWait);
|
2018-09-08 16:32:13 +00:00
|
|
|
grpHintActions = view.findViewById(R.id.grpHintActions);
|
2018-12-27 14:48:30 +00:00
|
|
|
grpHintSync = view.findViewById(R.id.grpHintSync);
|
2018-08-02 13:33:06 +00:00
|
|
|
grpReady = view.findViewById(R.id.grpReady);
|
|
|
|
|
fab = view.findViewById(R.id.fab);
|
|
|
|
|
|
|
|
|
|
// Wire controls
|
|
|
|
|
|
2018-09-08 16:32:13 +00:00
|
|
|
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
2018-12-27 14:48:30 +00:00
|
|
|
|
2019-02-28 10:43:46 +00:00
|
|
|
int colorPrimary = Helper.resolveColor(getContext(), R.attr.colorPrimary);
|
|
|
|
|
swipeRefresh.setColorSchemeColors(Color.WHITE, Color.WHITE, Color.WHITE);
|
|
|
|
|
swipeRefresh.setProgressBackgroundColorSchemeColor(colorPrimary);
|
|
|
|
|
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onRefresh() {
|
|
|
|
|
onSwipeRefresh();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-08 16:32:13 +00:00
|
|
|
ibHintActions.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
prefs.edit().putBoolean("folder_actions", true).apply();
|
|
|
|
|
grpHintActions.setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-12-27 14:48:30 +00:00
|
|
|
ibHintSync.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
prefs.edit().putBoolean("folder_sync", true).apply();
|
|
|
|
|
grpHintSync.setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
rvFolder.setHasFixedSize(false);
|
|
|
|
|
LinearLayoutManager llm = new LinearLayoutManager(getContext());
|
|
|
|
|
rvFolder.setLayoutManager(llm);
|
|
|
|
|
|
2018-09-08 06:23:53 +00:00
|
|
|
adapter = new AdapterFolder(getContext(), getViewLifecycleOwner());
|
2018-08-02 13:33:06 +00:00
|
|
|
rvFolder.setAdapter(adapter);
|
|
|
|
|
|
|
|
|
|
fab.setOnClickListener(new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
2019-02-28 16:48:18 +00:00
|
|
|
if (account < 0) {
|
|
|
|
|
startActivity(new Intent(getContext(), ActivityCompose.class)
|
|
|
|
|
.putExtra("action", "new")
|
|
|
|
|
.putExtra("account", account)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
|
args.putLong("account", account);
|
|
|
|
|
FragmentFolder fragment = new FragmentFolder();
|
|
|
|
|
fragment.setArguments(args);
|
|
|
|
|
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
|
|
|
|
|
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("folder");
|
|
|
|
|
fragmentTransaction.commit();
|
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-28 16:48:18 +00:00
|
|
|
if (account < 0)
|
|
|
|
|
fab.setOnLongClickListener(new View.OnLongClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
|
new SimpleTask<EntityFolder>() {
|
|
|
|
|
@Override
|
|
|
|
|
protected EntityFolder onExecute(Context context, Bundle args) {
|
|
|
|
|
return DB.getInstance(context).folder().getPrimaryDrafts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onExecuted(Bundle args, EntityFolder drafts) {
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
|
|
|
|
|
lbm.sendBroadcast(
|
|
|
|
|
new Intent(ActivityView.ACTION_VIEW_MESSAGES)
|
|
|
|
|
.putExtra("account", drafts.account)
|
|
|
|
|
.putExtra("folder", drafts.id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
|
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
|
|
|
|
|
}
|
|
|
|
|
}.execute(FragmentFolders.this, new Bundle(), "folders:drafts");
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// Initialize
|
|
|
|
|
grpReady.setVisibility(View.GONE);
|
2018-11-05 09:03:02 +00:00
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
2019-02-09 21:03:53 +00:00
|
|
|
fab.hide();
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-08-08 05:52:57 +00:00
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 07:34:23 +00:00
|
|
|
@Override
|
|
|
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
|
if (show_hidden != null)
|
|
|
|
|
outState.putBoolean("show_hidden", show_hidden);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 05:52:57 +00:00
|
|
|
@Override
|
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
|
|
2019-02-21 07:34:23 +00:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
|
show_hidden = (Boolean) savedInstanceState.get("show_hidden");
|
|
|
|
|
getActivity().invalidateOptionsMenu();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-08 16:32:13 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
|
grpHintActions.setVisibility(prefs.getBoolean("folder_actions", false) ? View.GONE : View.VISIBLE);
|
2018-12-27 14:48:30 +00:00
|
|
|
grpHintSync.setVisibility(prefs.getBoolean("folder_sync", false) ? View.GONE : View.VISIBLE);
|
2018-09-08 16:32:13 +00:00
|
|
|
|
2018-08-07 20:30:45 +00:00
|
|
|
DB db = DB.getInstance(getContext());
|
|
|
|
|
|
|
|
|
|
// Observe account
|
2019-02-28 16:48:18 +00:00
|
|
|
if (account < 0) {
|
2018-12-03 09:04:32 +01:00
|
|
|
setSubtitle(R.string.title_folders_unified);
|
2019-02-28 16:48:18 +00:00
|
|
|
|
|
|
|
|
fab.setImageResource(R.drawable.baseline_edit_24);
|
|
|
|
|
|
|
|
|
|
db.identity().liveComposableIdentities(null).observe(getViewLifecycleOwner(),
|
|
|
|
|
new Observer<List<TupleIdentityEx>>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onChanged(List<TupleIdentityEx> identities) {
|
|
|
|
|
if (identities == null || identities.size() == 0)
|
|
|
|
|
fab.hide();
|
|
|
|
|
else
|
|
|
|
|
fab.show();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else
|
2018-12-02 10:09:21 +01:00
|
|
|
db.account().liveAccount(account).observe(getViewLifecycleOwner(), new Observer<EntityAccount>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onChanged(@Nullable EntityAccount account) {
|
|
|
|
|
setSubtitle(account == null ? null : account.name);
|
2019-02-11 21:46:12 +00:00
|
|
|
if (account == null)
|
2019-02-09 21:03:53 +00:00
|
|
|
fab.hide();
|
|
|
|
|
else
|
|
|
|
|
fab.show();
|
2018-12-02 10:09:21 +01:00
|
|
|
}
|
|
|
|
|
});
|
2018-08-07 20:30:45 +00:00
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
// Observe folders
|
2018-12-02 10:09:21 +01:00
|
|
|
db.folder().liveFolders(account < 0 ? null : account).observe(getViewLifecycleOwner(), new Observer<List<TupleFolderEx>>() {
|
2018-08-02 13:33:06 +00:00
|
|
|
@Override
|
|
|
|
|
public void onChanged(@Nullable List<TupleFolderEx> folders) {
|
2018-08-12 15:31:43 +00:00
|
|
|
if (folders == null) {
|
2018-08-22 05:18:31 +00:00
|
|
|
finish();
|
2018-08-12 15:31:43 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 07:34:23 +00:00
|
|
|
boolean has_hidden = false;
|
2018-09-21 15:19:37 +00:00
|
|
|
for (TupleFolderEx folder : folders)
|
|
|
|
|
if (folder.hide) {
|
2019-02-21 07:34:23 +00:00
|
|
|
has_hidden = true;
|
2018-09-21 15:19:37 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 07:34:23 +00:00
|
|
|
if (has_hidden) {
|
|
|
|
|
if (show_hidden == null) {
|
|
|
|
|
show_hidden = false;
|
|
|
|
|
getActivity().invalidateOptionsMenu();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (show_hidden != null) {
|
|
|
|
|
show_hidden = null;
|
|
|
|
|
getActivity().invalidateOptionsMenu();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
adapter.set(account, show_hidden == null || show_hidden, folders);
|
2018-08-02 13:33:06 +00:00
|
|
|
|
2018-11-05 09:03:02 +00:00
|
|
|
pbWait.setVisibility(View.GONE);
|
2018-08-02 13:33:06 +00:00
|
|
|
grpReady.setVisibility(View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-02-21 07:34:23 +00:00
|
|
|
|
2019-02-28 10:43:46 +00:00
|
|
|
private void onSwipeRefresh() {
|
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
|
args.putLong("account", account);
|
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
new SimpleTask<Void>() {
|
2019-02-28 12:35:19 +00:00
|
|
|
@Override
|
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
|
swipeRefresh.setRefreshing(false);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 10:43:46 +00:00
|
|
|
@Override
|
2019-02-28 19:17:09 +00:00
|
|
|
protected Void onExecute(Context context, Bundle args) {
|
2019-02-28 10:43:46 +00:00
|
|
|
long aid = args.getLong("account");
|
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
if (!Helper.isConnected(context))
|
|
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_no_internet));
|
2019-02-28 12:35:19 +00:00
|
|
|
|
2019-02-28 10:43:46 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
try {
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
|
|
if (aid < 0) {
|
|
|
|
|
// Unified inbox
|
|
|
|
|
List<EntityFolder> folders = db.folder().getFoldersSynchronizingUnified();
|
2019-02-28 19:17:09 +00:00
|
|
|
for (EntityFolder folder : folders)
|
|
|
|
|
EntityOperation.sync(context, folder.id);
|
2019-02-28 10:43:46 +00:00
|
|
|
} else {
|
2019-02-28 19:17:09 +00:00
|
|
|
// Folder list
|
2019-02-28 10:43:46 +00:00
|
|
|
EntityAccount account = db.account().getAccount(aid);
|
2019-02-28 19:17:09 +00:00
|
|
|
if (account.ondemand)
|
|
|
|
|
ServiceUI.fsync(context, aid);
|
|
|
|
|
else
|
|
|
|
|
ServiceSynchronize.reload(getContext(), "refresh folders");
|
2019-02-28 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
|
} finally {
|
|
|
|
|
db.endTransaction();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
return null;
|
2019-02-28 10:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
|
if (ex instanceof IllegalArgumentException)
|
|
|
|
|
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
|
|
|
|
|
else
|
|
|
|
|
Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex);
|
|
|
|
|
}
|
|
|
|
|
}.execute(FragmentFolders.this, args, "folders:refresh");
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 07:34:23 +00:00
|
|
|
@Override
|
|
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
|
inflater.inflate(R.menu.menu_folders, menu);
|
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onPrepareOptionsMenu(Menu menu) {
|
|
|
|
|
MenuItem item = menu.findItem(R.id.menu_show_hidden);
|
|
|
|
|
if (show_hidden != null) {
|
|
|
|
|
item.setTitle(show_hidden ? R.string.title_hide_folders : R.string.title_show_folders);
|
|
|
|
|
item.setIcon(show_hidden ? R.drawable.baseline_visibility_off_24 : R.drawable.baseline_visibility_24);
|
|
|
|
|
}
|
|
|
|
|
item.setVisible(show_hidden != null);
|
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case R.id.menu_show_hidden:
|
|
|
|
|
onMenuShowHidden();
|
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onMenuShowHidden() {
|
|
|
|
|
if (show_hidden == null)
|
|
|
|
|
show_hidden = false;
|
|
|
|
|
else
|
|
|
|
|
show_hidden = !show_hidden;
|
|
|
|
|
getActivity().invalidateOptionsMenu();
|
|
|
|
|
adapter.showHidden(show_hidden);
|
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|