mirror of https://github.com/M66B/FairEmail.git
parent
15314464c1
commit
b99fcb48f7
@ -0,0 +1,130 @@
|
||||
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;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import androidx.constraintlayout.widget.Group;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ActivityWidget extends ActivityBase {
|
||||
private int appWidgetId;
|
||||
|
||||
private Spinner spAccount;
|
||||
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);
|
||||
|
||||
getSupportActionBar().setSubtitle(R.string.title_widget_title_count);
|
||||
setContentView(R.layout.activity_widget);
|
||||
|
||||
spAccount = findViewById(R.id.spAccount);
|
||||
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);
|
||||
|
||||
btnSave.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
EntityAccount account = (EntityAccount) spAccount.getSelectedItem();
|
||||
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ActivityWidget.this);
|
||||
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);
|
||||
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);
|
||||
|
||||
grpReady.setVisibility(View.GONE);
|
||||
pbWait.setVisibility(View.VISIBLE);
|
||||
|
||||
setResult(RESULT_CANCELED, resultValue);
|
||||
|
||||
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);
|
||||
|
||||
adapterAccount.addAll(accounts);
|
||||
|
||||
grpReady.setVisibility(View.VISIBLE);
|
||||
pbWait.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onException(Bundle args, Throwable ex) {
|
||||
Log.unexpectedError(getSupportFragmentManager(), ex);
|
||||
}
|
||||
}.execute(this, new Bundle(), "widget:accounts");
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:padding="12dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvAccount"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/title_widget_account"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/spAccount"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/tvAccount" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnSave"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:text="@string/title_save"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/spAccount" />
|
||||
|
||||
<eu.faircode.email.ContentLoadingProgressBar
|
||||
android:id="@+id/pbWait"
|
||||
style="@style/Base.Widget.AppCompat.ProgressBar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:indeterminate="true"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<androidx.constraintlayout.widget.Group
|
||||
android:id="@+id/grpReady"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
app:constraint_referenced_ids="tvAccount,spAccount,btnSave" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in new issue