mirror of https://github.com/M66B/FairEmail.git
parent
24a6a1fd8e
commit
baab4cba36
@ -0,0 +1,232 @@
|
||||
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-2020 by Marcel Bokhorst (M66B)
|
||||
*/
|
||||
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.text.Html;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.text.style.ImageSpan;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ActivitySignature extends ActivityBase {
|
||||
private EditTextCompose etText;
|
||||
private BottomNavigationView style_bar;
|
||||
private BottomNavigationView bottom_navigation;
|
||||
|
||||
private static final int REQUEST_IMAGE = 1;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
getSupportActionBar().setSubtitle(getString(R.string.title_edit_signature));
|
||||
setContentView(R.layout.activity_signature);
|
||||
|
||||
etText = findViewById(R.id.etText);
|
||||
style_bar = findViewById(R.id.style_bar);
|
||||
bottom_navigation = findViewById(R.id.bottom_navigation);
|
||||
|
||||
etText.setSelectionListener(new EditTextCompose.ISelection() {
|
||||
@Override
|
||||
public void onSelected(boolean selection) {
|
||||
style_bar.setVisibility(selection ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
});
|
||||
|
||||
style_bar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
|
||||
@Override
|
||||
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
||||
return onActionStyle(item.getItemId());
|
||||
}
|
||||
});
|
||||
|
||||
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
|
||||
@Override
|
||||
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_insert_image:
|
||||
insertImage();
|
||||
return true;
|
||||
case R.id.action_delete:
|
||||
delete();
|
||||
return true;
|
||||
case R.id.action_save:
|
||||
save();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
style_bar.setVisibility(View.GONE);
|
||||
|
||||
setResult(RESULT_CANCELED, new Intent());
|
||||
|
||||
load();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
super.onNewIntent(intent);
|
||||
setIntent(intent);
|
||||
load();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
try {
|
||||
switch (requestCode) {
|
||||
case REQUEST_IMAGE:
|
||||
if (resultCode == RESULT_OK && data != null)
|
||||
onImageSelected(data.getData());
|
||||
break;
|
||||
}
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void load() {
|
||||
String html = getIntent().getStringExtra("html");
|
||||
if (html == null)
|
||||
etText.setText(null);
|
||||
else
|
||||
etText.setText(HtmlHelper.fromHtml(html, new Html.ImageGetter() {
|
||||
@Override
|
||||
public Drawable getDrawable(String source) {
|
||||
return getDrawableByUri(Uri.parse(source));
|
||||
}
|
||||
}, null));
|
||||
}
|
||||
|
||||
private void delete() {
|
||||
Intent result = new Intent();
|
||||
result.putExtra("html", (String) null);
|
||||
setResult(RESULT_OK, result);
|
||||
finish();
|
||||
}
|
||||
|
||||
private void save() {
|
||||
etText.clearComposingText();
|
||||
String html = HtmlHelper.toHtml(etText.getText());
|
||||
Intent result = new Intent();
|
||||
result.putExtra("html", html);
|
||||
setResult(RESULT_OK, result);
|
||||
finish();
|
||||
}
|
||||
|
||||
private void insertImage() {
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
intent.setType("image/*");
|
||||
Helper.openAdvanced(intent);
|
||||
startActivityForResult(intent, REQUEST_IMAGE);
|
||||
}
|
||||
|
||||
private boolean onActionStyle(int action) {
|
||||
Log.i("Style action=" + action);
|
||||
|
||||
if (action == R.id.menu_link) {
|
||||
Uri uri = null;
|
||||
|
||||
ClipboardManager cbm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (cbm != null && cbm.hasPrimaryClip()) {
|
||||
String link = cbm.getPrimaryClip().getItemAt(0).coerceToText(this).toString();
|
||||
uri = Uri.parse(link);
|
||||
if (uri.getScheme() == null)
|
||||
uri = null;
|
||||
}
|
||||
|
||||
View view = LayoutInflater.from(this).inflate(R.layout.dialog_insert_link, null);
|
||||
EditText etLink = view.findViewById(R.id.etLink);
|
||||
TextView tvInsecure = view.findViewById(R.id.tvInsecure);
|
||||
|
||||
etLink.setText(uri == null ? "https://" : uri.toString());
|
||||
tvInsecure.setVisibility(View.GONE);
|
||||
|
||||
new AlertDialog.Builder(this)
|
||||
.setView(view)
|
||||
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
String link = etLink.getText().toString();
|
||||
StyleHelper.apply(R.id.menu_link, etText, link);
|
||||
}
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.show();
|
||||
|
||||
return true;
|
||||
} else
|
||||
return StyleHelper.apply(action, etText);
|
||||
}
|
||||
|
||||
private void onImageSelected(Uri uri) {
|
||||
getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
|
||||
int start = etText.getSelectionStart();
|
||||
SpannableStringBuilder ssb = new SpannableStringBuilder(etText.getText());
|
||||
ssb.insert(start, " ");
|
||||
ImageSpan is = new ImageSpan(getDrawableByUri(uri), uri.toString(), ImageSpan.ALIGN_BASELINE);
|
||||
ssb.setSpan(is, start, start + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||||
etText.setText(ssb);
|
||||
}
|
||||
|
||||
private Drawable getDrawableByUri(Uri uri) {
|
||||
Drawable d;
|
||||
try {
|
||||
Log.i("Loading image source=" + uri);
|
||||
InputStream inputStream = getContentResolver().openInputStream(uri);
|
||||
d = Drawable.createFromStream(inputStream, uri.toString());
|
||||
} catch (FileNotFoundException ex) {
|
||||
Log.w(ex);
|
||||
d = getResources().getDrawable(R.drawable.baseline_broken_image_24);
|
||||
}
|
||||
|
||||
int w = Helper.dp2pixels(this, d.getIntrinsicWidth());
|
||||
int h = Helper.dp2pixels(this, d.getIntrinsicHeight());
|
||||
|
||||
d.setBounds(0, 0, w, h);
|
||||
return d;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="eu.faircode.email.ActivitySetup">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:layout_behavior="eu.faircode.email.BehaviorBottomPadding">
|
||||
|
||||
<eu.faircode.email.EditTextCompose
|
||||
android:id="@+id/etText"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_margin="6dp"
|
||||
android:background="@null"
|
||||
android:fontFamily="monospace"
|
||||
android:gravity="top"
|
||||
android:hint="@string/title_edit_signature_text"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="textCapSentences|textMultiLine|textAutoCorrect"
|
||||
android:isScrollContainer="true"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Small"
|
||||
app:layout_constraintBottom_toTopOf="@+id/style_bar"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<com.google.android.material.bottomnavigation.BottomNavigationView
|
||||
android:id="@+id/style_bar"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="36dp"
|
||||
android:background="?attr/colorActionBackground"
|
||||
app:itemIconTint="@color/action_foreground"
|
||||
app:itemTextColor="@color/action_foreground"
|
||||
app:labelVisibilityMode="unlabeled"
|
||||
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:menu="@menu/action_signature_style" />
|
||||
|
||||
<com.google.android.material.bottomnavigation.BottomNavigationView
|
||||
android:id="@+id/bottom_navigation"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="?attr/colorPrimary"
|
||||
app:itemIconTint="@color/action_foreground"
|
||||
app:itemTextColor="@color/action_foreground"
|
||||
app:labelVisibilityMode="labeled"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:menu="@menu/action_signature" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item
|
||||
android:id="@+id/action_insert_image"
|
||||
android:icon="@drawable/baseline_image_24"
|
||||
android:title="@string/title_edit_signature_image" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_delete"
|
||||
android:icon="@drawable/baseline_delete_24"
|
||||
android:title="@string/title_delete" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_save"
|
||||
android:icon="@drawable/baseline_save_alt_24"
|
||||
android:title="@string/title_save" />
|
||||
</menu>
|
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item
|
||||
android:id="@+id/menu_bold"
|
||||
android:icon="@drawable/baseline_format_bold_24"
|
||||
android:title="@string/title_style_bold" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_italic"
|
||||
android:icon="@drawable/baseline_format_italic_24"
|
||||
android:title="@string/title_style_italic" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_underline"
|
||||
android:icon="@drawable/baseline_format_underlined_24"
|
||||
android:title="@string/title_style_underline" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_size"
|
||||
android:icon="@drawable/baseline_format_size_24"
|
||||
android:title="@string/title_style_size" />
|
||||
|
||||
<item
|
||||
android:id="@+id/menu_link"
|
||||
android:icon="@drawable/baseline_insert_link_24"
|
||||
android:title="@string/title_style_link" />
|
||||
</menu>
|
Loading…
Reference in new issue