2020-01-01 19:18:54 +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/>.
|
|
|
|
|
2022-01-01 08:46:36 +00:00
|
|
|
Copyright 2018-2022 by Marcel Bokhorst (M66B)
|
2020-01-01 19:18:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
import android.content.Context;
|
2020-07-08 14:47:15 +00:00
|
|
|
import android.content.DialogInterface;
|
2020-01-01 19:18:54 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2020-04-08 12:28:57 +00:00
|
|
|
import android.graphics.Color;
|
2020-01-01 19:18:54 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.Button;
|
2020-04-08 10:08:50 +00:00
|
|
|
import android.widget.CheckBox;
|
2020-04-08 12:28:57 +00:00
|
|
|
import android.widget.CompoundButton;
|
2020-07-08 16:38:05 +00:00
|
|
|
import android.widget.ImageView;
|
2020-04-08 12:28:57 +00:00
|
|
|
import android.widget.RadioButton;
|
2020-01-01 19:18:54 +00:00
|
|
|
import android.widget.Spinner;
|
2020-04-08 12:28:57 +00:00
|
|
|
import android.widget.TextView;
|
2020-01-01 19:18:54 +00:00
|
|
|
|
|
|
|
import androidx.constraintlayout.widget.Group;
|
2020-07-08 16:38:05 +00:00
|
|
|
import androidx.core.graphics.ColorUtils;
|
2020-01-01 19:18:54 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2020-07-08 14:47:15 +00:00
|
|
|
import com.flask.colorpicker.ColorPickerView;
|
|
|
|
import com.flask.colorpicker.builder.ColorPickerClickListener;
|
|
|
|
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
|
|
|
|
|
2020-01-01 19:18:54 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class ActivityWidget extends ActivityBase {
|
|
|
|
private int appWidgetId;
|
|
|
|
|
|
|
|
private Spinner spAccount;
|
2020-04-08 10:08:50 +00:00
|
|
|
private CheckBox cbSemiTransparent;
|
2021-03-26 07:48:03 +00:00
|
|
|
private ViewButtonColor btnColor;
|
2020-04-08 12:28:57 +00:00
|
|
|
private View inOld;
|
|
|
|
private View inNew;
|
|
|
|
private RadioButton rbOld;
|
|
|
|
private RadioButton rbNew;
|
2020-01-01 19:18:54 +00:00
|
|
|
private Button btnSave;
|
|
|
|
private ContentLoadingProgressBar pbWait;
|
|
|
|
private Group grpReady;
|
|
|
|
|
|
|
|
private ArrayAdapter<EntityAccount> adapterAccount;
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
2021-08-22 11:56:32 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
2021-08-22 11:29:37 +00:00
|
|
|
long account = prefs.getLong("widget." + appWidgetId + ".account", -1L);
|
|
|
|
boolean semi = prefs.getBoolean("widget." + appWidgetId + ".semi", true);
|
|
|
|
int background = prefs.getInt("widget." + appWidgetId + ".background", Color.TRANSPARENT);
|
|
|
|
int layout = prefs.getInt("widget." + appWidgetId + ".layout", 1 /* new */);
|
|
|
|
|
2020-01-01 19:18:54 +00:00
|
|
|
getSupportActionBar().setSubtitle(R.string.title_widget_title_count);
|
|
|
|
setContentView(R.layout.activity_widget);
|
|
|
|
|
|
|
|
spAccount = findViewById(R.id.spAccount);
|
2020-04-08 10:08:50 +00:00
|
|
|
cbSemiTransparent = findViewById(R.id.cbSemiTransparent);
|
2020-07-08 14:47:15 +00:00
|
|
|
btnColor = findViewById(R.id.btnColor);
|
2020-04-08 12:28:57 +00:00
|
|
|
inOld = findViewById(R.id.inOld);
|
|
|
|
inNew = findViewById(R.id.inNew);
|
|
|
|
rbOld = findViewById(R.id.rbOld);
|
|
|
|
rbNew = findViewById(R.id.rbNew);
|
2020-01-01 19:18:54 +00:00
|
|
|
btnSave = findViewById(R.id.btnSave);
|
|
|
|
pbWait = findViewById(R.id.pbWait);
|
|
|
|
grpReady = findViewById(R.id.grpReady);
|
|
|
|
|
|
|
|
final Intent resultValue = new Intent();
|
|
|
|
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
|
|
|
|
|
2020-04-08 12:28:57 +00:00
|
|
|
cbSemiTransparent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
2021-03-26 13:38:51 +00:00
|
|
|
setBackground();
|
2020-07-08 14:47:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
btnColor.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
2020-10-02 11:11:59 +00:00
|
|
|
int editTextColor = Helper.resolveColor(ActivityWidget.this, android.R.attr.editTextColor);
|
|
|
|
|
2020-07-08 14:47:15 +00:00
|
|
|
ColorPickerDialogBuilder
|
|
|
|
.with(ActivityWidget.this)
|
|
|
|
.setTitle(R.string.title_widget_background)
|
|
|
|
.showColorEdit(true)
|
2020-10-02 11:11:59 +00:00
|
|
|
.setColorEditTextColor(editTextColor)
|
2020-07-08 14:47:15 +00:00
|
|
|
.wheelType(ColorPickerView.WHEEL_TYPE.FLOWER)
|
|
|
|
.density(6)
|
|
|
|
.lightnessSliderOnly()
|
|
|
|
.setPositiveButton(android.R.string.ok, new ColorPickerClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int selectedColor, Integer[] allColors) {
|
2021-03-26 07:48:03 +00:00
|
|
|
btnColor.setColor(selectedColor);
|
2020-07-08 14:47:15 +00:00
|
|
|
setBackground();
|
|
|
|
}
|
|
|
|
})
|
2021-04-13 07:46:23 +00:00
|
|
|
.setNegativeButton(R.string.title_transparent, new DialogInterface.OnClickListener() {
|
2020-07-08 14:47:15 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2021-04-13 07:46:23 +00:00
|
|
|
cbSemiTransparent.setChecked(false);
|
2021-03-26 07:48:03 +00:00
|
|
|
btnColor.setColor(Color.TRANSPARENT);
|
2020-07-08 14:47:15 +00:00
|
|
|
setBackground();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.build()
|
|
|
|
.show();
|
2020-04-08 12:28:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
rbOld.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
if (isChecked)
|
|
|
|
rbNew.setChecked(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
rbNew.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
if (isChecked)
|
|
|
|
rbOld.setChecked(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-01-01 19:18:54 +00:00
|
|
|
btnSave.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
EntityAccount account = (EntityAccount) spAccount.getSelectedItem();
|
|
|
|
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
if (account != null && account.id > 0)
|
|
|
|
editor.putString("widget." + appWidgetId + ".name", account.name);
|
|
|
|
else
|
|
|
|
editor.remove("widget." + appWidgetId + ".name");
|
|
|
|
editor.putLong("widget." + appWidgetId + ".account", account == null ? -1L : account.id);
|
2020-04-08 10:08:50 +00:00
|
|
|
editor.putBoolean("widget." + appWidgetId + ".semi", cbSemiTransparent.isChecked());
|
2021-03-26 07:48:03 +00:00
|
|
|
editor.putInt("widget." + appWidgetId + ".background", btnColor.getColor());
|
2020-04-08 12:28:57 +00:00
|
|
|
editor.putInt("widget." + appWidgetId + ".layout", rbNew.isChecked() ? 1 : 0);
|
2021-03-29 07:18:04 +00:00
|
|
|
editor.putInt("widget." + appWidgetId + ".version", BuildConfig.VERSION_CODE);
|
2020-01-01 19:18:54 +00:00
|
|
|
editor.apply();
|
|
|
|
|
|
|
|
Widget.init(ActivityWidget.this, appWidgetId);
|
|
|
|
|
|
|
|
setResult(RESULT_OK, resultValue);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-08-22 11:29:37 +00:00
|
|
|
// Initialize
|
2020-04-08 12:28:57 +00:00
|
|
|
((TextView) inOld.findViewById(R.id.tvCount)).setText("12");
|
|
|
|
((TextView) inNew.findViewById(R.id.tvCount)).setText("12");
|
|
|
|
|
2021-08-22 11:29:37 +00:00
|
|
|
cbSemiTransparent.setChecked(semi);
|
|
|
|
btnColor.setColor(background);
|
|
|
|
rbOld.setChecked(layout != 1);
|
|
|
|
rbNew.setChecked(layout == 1);
|
2021-03-26 07:48:03 +00:00
|
|
|
setBackground();
|
|
|
|
|
2020-01-01 19:18:54 +00:00
|
|
|
grpReady.setVisibility(View.GONE);
|
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
setResult(RESULT_CANCELED, resultValue);
|
|
|
|
|
2021-08-22 11:29:37 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
|
2020-01-01 19:18:54 +00:00
|
|
|
new SimpleTask<List<EntityAccount>>() {
|
|
|
|
@Override
|
|
|
|
protected List<EntityAccount> onExecute(Context context, Bundle args) {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2022-01-27 13:43:55 +00:00
|
|
|
return db.account().getSynchronizingAccounts(null);
|
2020-01-01 19:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, List<EntityAccount> accounts) {
|
2020-03-27 12:26:15 +00:00
|
|
|
if (accounts == null)
|
|
|
|
accounts = new ArrayList<>();
|
|
|
|
|
2020-01-01 19:18:54 +00:00
|
|
|
EntityAccount all = new EntityAccount();
|
|
|
|
all.id = -1L;
|
|
|
|
all.name = getString(R.string.title_widget_account_all);
|
|
|
|
all.primary = false;
|
|
|
|
accounts.add(0, all);
|
|
|
|
|
|
|
|
adapterAccount.addAll(accounts);
|
|
|
|
|
2021-08-22 11:29:37 +00:00
|
|
|
for (int i = 0; i < accounts.size(); i++)
|
|
|
|
if (accounts.get(i).id.equals(account)) {
|
|
|
|
spAccount.setSelection(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:18:54 +00:00
|
|
|
grpReady.setVisibility(View.VISIBLE);
|
|
|
|
pbWait.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
|
|
|
Log.unexpectedError(getSupportFragmentManager(), ex);
|
|
|
|
}
|
2021-08-22 11:29:37 +00:00
|
|
|
}.execute(this, args, "widget:accounts");
|
2020-01-01 19:18:54 +00:00
|
|
|
}
|
2020-07-08 14:47:15 +00:00
|
|
|
|
|
|
|
private void setBackground() {
|
2021-03-26 13:38:51 +00:00
|
|
|
boolean semi = cbSemiTransparent.isChecked();
|
|
|
|
int background = btnColor.getColor();
|
|
|
|
if (background == Color.TRANSPARENT) {
|
2021-04-12 17:57:54 +00:00
|
|
|
if (semi) {
|
|
|
|
inOld.setBackgroundResource(R.drawable.widget_background);
|
|
|
|
inNew.setBackgroundResource(R.drawable.widget_background);
|
|
|
|
} else {
|
|
|
|
inOld.setBackgroundColor(background);
|
|
|
|
inNew.setBackgroundColor(background);
|
|
|
|
}
|
2020-07-08 14:47:15 +00:00
|
|
|
} else {
|
2020-07-08 16:38:05 +00:00
|
|
|
float lum = (float) ColorUtils.calculateLuminance(background);
|
|
|
|
int color = (lum > 0.7 ? Color.BLACK : getResources().getColor(R.color.colorWidgetForeground));
|
2021-03-26 13:38:51 +00:00
|
|
|
if (semi)
|
|
|
|
background = ColorUtils.setAlphaComponent(background, 127);
|
|
|
|
|
|
|
|
inOld.setBackgroundColor(background);
|
|
|
|
inNew.setBackgroundColor(background);
|
2020-07-08 16:38:05 +00:00
|
|
|
|
|
|
|
((ImageView) inOld.findViewById(R.id.ivMessage)).setColorFilter(color);
|
|
|
|
((TextView) inOld.findViewById(R.id.tvCount)).setTextColor(color);
|
|
|
|
((TextView) inOld.findViewById(R.id.tvAccount)).setTextColor(color);
|
|
|
|
|
|
|
|
((ImageView) inNew.findViewById(R.id.ivMessage)).setColorFilter(color);
|
|
|
|
((TextView) inNew.findViewById(R.id.tvCount)).setTextColor(color);
|
|
|
|
((TextView) inNew.findViewById(R.id.tvAccount)).setTextColor(color);
|
2020-07-08 14:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-01 19:18:54 +00:00
|
|
|
}
|