FairEmail/app/src/main/java/eu/faircode/email/ActivityAnswer.java

140 lines
5.1 KiB
Java
Raw Normal View History

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)
*/
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.Group;
2021-08-01 15:23:24 +00:00
import java.util.Date;
import java.util.List;
2021-08-01 15:23:24 +00:00
import java.util.concurrent.ExecutorService;
public class ActivityAnswer extends ActivityBase {
2021-08-01 15:23:24 +00:00
private static ExecutorService executor = Helper.getBackgroundExecutor(1, "answer");
@Override
@RequiresApi(api = Build.VERSION_CODES.M)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2021-08-01 16:45:47 +00:00
Intent intent = getIntent();
if (intent == null) {
finish();
2021-08-01 16:45:47 +00:00
return;
}
final CharSequence query = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
final boolean readonly = intent.getBooleanExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, false);
getSupportActionBar().setSubtitle(query == null ? null : query.toString());
View view = LayoutInflater.from(this).inflate(R.layout.activity_answer, null);
setContentView(view);
ListView lvAnswer = view.findViewById(R.id.lvAnswer);
Group grpReady = view.findViewById(R.id.grpReady);
ContentLoadingProgressBar pbWait = view.findViewById(R.id.pbWait);
lvAnswer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
2021-08-01 15:23:24 +00:00
final Context context = adapterView.getContext();
EntityAnswer answer = (EntityAnswer) adapterView.getAdapter().getItem(pos);
2021-08-01 15:23:24 +00:00
executor.submit(new Runnable() {
@Override
public void run() {
try {
DB db = DB.getInstance(context);
db.answer().applyAnswer(answer.id, new Date().getTime());
} catch (Throwable ex) {
Log.e(ex);
}
}
});
2021-08-01 14:37:38 +00:00
String html = answer.getHtml(null);
2021-08-01 15:23:24 +00:00
String text = HtmlHelper.getText(context, html);
ClipboardManager cbm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
cbm.setPrimaryClip(ClipData.newHtmlText(getString(R.string.app_name), text, html));
2021-08-01 15:23:24 +00:00
ToastEx.makeText(context, R.string.title_clipboard_copied, Toast.LENGTH_LONG).show();
2021-08-01 16:45:47 +00:00
if (!readonly) {
Intent result = new Intent();
result.putExtra(Intent.EXTRA_PROCESS_TEXT, text);
setResult(RESULT_OK, result);
}
finish();
}
});
new SimpleTask<List<EntityAnswer>>() {
@Override
protected void onPreExecute(Bundle args) {
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(Bundle args) {
grpReady.setVisibility(View.VISIBLE);
pbWait.setVisibility(View.GONE);
}
@Override
protected List<EntityAnswer> onExecute(Context context, Bundle args) throws Throwable {
DB db = DB.getInstance(context);
return db.answer().getAnswersExternal();
}
@Override
protected void onExecuted(Bundle args, List<EntityAnswer> answers) {
ArrayAdapter<EntityAnswer> adapter = new ArrayAdapter<EntityAnswer>(ActivityAnswer.this,
android.R.layout.simple_list_item_1, answers);
lvAnswer.setAdapter(adapter);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex);
}
}.execute(this, new Bundle(), "answers");
}
2021-08-01 16:45:47 +00:00
static boolean canAnswer(Context context) {
return BuildConfig.DEBUG;
}
}