2019-07-30 09:36:30 +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/>.
|
|
|
|
|
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
2019-09-20 13:14:04 +00:00
|
|
|
import android.content.Context;
|
2019-07-30 09:36:30 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
2019-11-15 11:59:11 +00:00
|
|
|
import android.widget.AdapterView;
|
2019-09-20 13:14:04 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
2019-07-30 09:36:30 +00:00
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.CheckBox;
|
2019-09-20 13:14:04 +00:00
|
|
|
import android.widget.Spinner;
|
2019-07-30 09:36:30 +00:00
|
|
|
|
2019-09-20 13:14:04 +00:00
|
|
|
import androidx.constraintlayout.widget.Group;
|
2019-07-30 09:36:30 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2019-09-20 13:14:04 +00:00
|
|
|
import java.util.ArrayList;
|
2019-11-15 11:59:11 +00:00
|
|
|
import java.util.Collections;
|
2019-09-20 13:14:04 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2019-11-15 12:58:34 +00:00
|
|
|
public class ActivityWidgetUnified extends ActivityBase {
|
2019-07-30 09:36:30 +00:00
|
|
|
private int appWidgetId;
|
|
|
|
|
2019-09-20 13:14:04 +00:00
|
|
|
private Spinner spAccount;
|
2019-11-15 11:59:11 +00:00
|
|
|
private Spinner spFolder;
|
2019-09-20 13:14:04 +00:00
|
|
|
private CheckBox cbUnseen;
|
|
|
|
private CheckBox cbFlagged;
|
|
|
|
private Button btnSave;
|
|
|
|
private ContentLoadingProgressBar pbWait;
|
|
|
|
private Group grpReady;
|
|
|
|
|
2019-11-15 11:59:11 +00:00
|
|
|
private ArrayAdapter<EntityAccount> adapterAccount;
|
|
|
|
private ArrayAdapter<TupleFolderEx> adapterFolder;
|
2019-09-20 13:14:04 +00:00
|
|
|
|
2019-07-30 09:36:30 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
if (extras == null) {
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
appWidgetId = extras.getInt(
|
|
|
|
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
|
|
|
|
2019-11-15 12:58:34 +00:00
|
|
|
getSupportActionBar().setSubtitle(R.string.title_folder_unified);
|
2019-07-30 09:36:30 +00:00
|
|
|
setContentView(R.layout.activity_widget_unified);
|
|
|
|
|
2019-09-20 13:14:04 +00:00
|
|
|
spAccount = findViewById(R.id.spAccount);
|
2019-11-15 11:59:11 +00:00
|
|
|
spFolder = findViewById(R.id.spFolder);
|
2019-09-20 13:14:04 +00:00
|
|
|
cbUnseen = findViewById(R.id.cbUnseen);
|
|
|
|
cbFlagged = findViewById(R.id.cbFlagged);
|
|
|
|
btnSave = findViewById(R.id.btnSave);
|
|
|
|
pbWait = findViewById(R.id.pbWait);
|
|
|
|
grpReady = findViewById(R.id.grpReady);
|
2019-07-30 09:36:30 +00:00
|
|
|
|
|
|
|
final Intent resultValue = new Intent();
|
|
|
|
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
|
|
|
|
|
|
|
|
btnSave.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2019-09-20 13:14:04 +00:00
|
|
|
EntityAccount account = (EntityAccount) spAccount.getSelectedItem();
|
2019-11-15 11:59:11 +00:00
|
|
|
TupleFolderEx folder = (TupleFolderEx) spFolder.getSelectedItem();
|
2019-09-20 13:14:04 +00:00
|
|
|
|
2019-11-15 12:58:34 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityWidgetUnified.this);
|
2019-07-30 09:36:30 +00:00
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
2019-09-20 13:14:04 +00:00
|
|
|
if (account != null && account.id > 0)
|
2019-11-15 11:59:11 +00:00
|
|
|
if (folder != null && folder.id > 0)
|
2019-11-15 12:58:34 +00:00
|
|
|
editor.putString("widget." + appWidgetId + ".name", folder.getDisplayName(ActivityWidgetUnified.this));
|
2019-11-15 11:59:11 +00:00
|
|
|
else
|
|
|
|
editor.putString("widget." + appWidgetId + ".name", account.name);
|
2019-11-09 12:59:34 +00:00
|
|
|
else
|
|
|
|
editor.remove("widget." + appWidgetId + ".name");
|
2019-09-20 13:14:04 +00:00
|
|
|
editor.putLong("widget." + appWidgetId + ".account", account == null ? -1L : account.id);
|
2019-11-15 11:59:11 +00:00
|
|
|
editor.putLong("widget." + appWidgetId + ".folder", folder == null ? -1L : folder.id);
|
2019-07-30 09:36:30 +00:00
|
|
|
editor.putBoolean("widget." + appWidgetId + ".unseen", cbUnseen.isChecked());
|
|
|
|
editor.putBoolean("widget." + appWidgetId + ".flagged", cbFlagged.isChecked());
|
|
|
|
editor.apply();
|
|
|
|
|
2019-11-15 12:58:34 +00:00
|
|
|
WidgetUnified.init(ActivityWidgetUnified.this, appWidgetId);
|
2019-07-30 09:36:30 +00:00
|
|
|
//WidgetUnified.update(ActivityWidgetUnified.this);
|
|
|
|
|
|
|
|
setResult(RESULT_OK, resultValue);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-15 11:59:11 +00:00
|
|
|
adapterAccount = new ArrayAdapter<>(this, R.layout.spinner_item1, android.R.id.text1, new ArrayList<EntityAccount>());
|
|
|
|
adapterAccount.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
|
|
|
spAccount.setAdapter(adapterAccount);
|
|
|
|
|
|
|
|
adapterFolder = new ArrayAdapter<>(this, R.layout.spinner_item1, android.R.id.text1, new ArrayList<TupleFolderEx>());
|
|
|
|
adapterFolder.setDropDownViewResource(R.layout.spinner_item1_dropdown);
|
|
|
|
spFolder.setAdapter(adapterFolder);
|
|
|
|
|
|
|
|
spAccount.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
EntityAccount account = (EntityAccount) spAccount.getAdapter().getItem(position);
|
|
|
|
setFolders(account.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNothingSelected(AdapterView<?> parent) {
|
|
|
|
setFolders(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setFolders(long account) {
|
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("account", account);
|
|
|
|
|
|
|
|
new SimpleTask<List<TupleFolderEx>>() {
|
|
|
|
@Override
|
|
|
|
protected List<TupleFolderEx> onExecute(Context context, Bundle args) {
|
|
|
|
long account = args.getLong("account");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
List<TupleFolderEx> folders = db.folder().getFoldersEx(account);
|
|
|
|
if (folders.size() > 0)
|
|
|
|
Collections.sort(folders, folders.get(0).getComparator(context));
|
|
|
|
return folders;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, List<TupleFolderEx> folders) {
|
|
|
|
TupleFolderEx unified = new TupleFolderEx();
|
|
|
|
unified.id = -1L;
|
|
|
|
unified.name = getString(R.string.title_widget_folder_unified);
|
|
|
|
folders.add(0, unified);
|
|
|
|
|
|
|
|
adapterFolder.clear();
|
|
|
|
adapterFolder.addAll(folders);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
Helper.unexpectedError(getSupportFragmentManager(), ex);
|
|
|
|
}
|
2019-11-15 12:58:34 +00:00
|
|
|
}.execute(ActivityWidgetUnified.this, args, "widget:folders");
|
2019-11-15 11:59:11 +00:00
|
|
|
}
|
|
|
|
});
|
2019-09-20 13:14:04 +00:00
|
|
|
|
|
|
|
grpReady.setVisibility(View.GONE);
|
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
|
|
|
|
2019-07-30 09:36:30 +00:00
|
|
|
setResult(RESULT_CANCELED, resultValue);
|
2019-09-20 13:14:04 +00:00
|
|
|
|
|
|
|
new SimpleTask<List<EntityAccount>>() {
|
|
|
|
@Override
|
|
|
|
protected List<EntityAccount> onExecute(Context context, Bundle args) {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
return db.account().getSynchronizingAccounts();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, List<EntityAccount> accounts) {
|
|
|
|
EntityAccount all = new EntityAccount();
|
|
|
|
all.id = -1L;
|
|
|
|
all.name = getString(R.string.title_widget_account_all);
|
|
|
|
all.primary = false;
|
|
|
|
accounts.add(0, all);
|
|
|
|
|
2019-11-15 11:59:11 +00:00
|
|
|
adapterAccount.addAll(accounts);
|
2019-09-20 13:14:04 +00:00
|
|
|
|
|
|
|
grpReady.setVisibility(View.VISIBLE);
|
|
|
|
pbWait.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
Helper.unexpectedError(getSupportFragmentManager(), ex);
|
|
|
|
}
|
|
|
|
}.execute(this, new Bundle(), "widget:accounts");
|
2019-07-30 09:36:30 +00:00
|
|
|
}
|
|
|
|
}
|