2018-08-05 12:09:46 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2018-08-14 05:53:24 +00:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
|
2018-10-29 10:46:49 +00:00
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
2018-08-14 05:53:24 +00:00
|
|
|
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
|
2018-10-29 10:46:49 +00:00
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-14 05:53:24 +00:00
|
|
|
|
2021-01-01 07:56:36 +00:00
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
2018-08-14 05:53:24 +00:00
|
|
|
*/
|
|
|
|
|
2020-01-05 14:33:47 +00:00
|
|
|
import android.app.RecoverableSecurityException;
|
2019-08-17 08:26:36 +00:00
|
|
|
import android.content.ActivityNotFoundException;
|
2019-09-22 08:29:59 +00:00
|
|
|
import android.content.BroadcastReceiver;
|
2018-12-09 17:49:52 +00:00
|
|
|
import android.content.Context;
|
2020-01-05 14:33:47 +00:00
|
|
|
import android.content.DialogInterface;
|
2019-06-30 14:55:15 +00:00
|
|
|
import android.content.Intent;
|
2019-09-22 08:29:59 +00:00
|
|
|
import android.content.IntentFilter;
|
2020-01-05 14:33:47 +00:00
|
|
|
import android.content.IntentSender;
|
2020-06-14 16:34:13 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2018-08-11 16:13:22 +00:00
|
|
|
import android.content.res.Configuration;
|
2019-09-22 08:29:59 +00:00
|
|
|
import android.net.Uri;
|
2020-01-05 14:33:47 +00:00
|
|
|
import android.os.Build;
|
2018-08-11 16:13:22 +00:00
|
|
|
import android.os.Bundle;
|
2020-08-23 15:34:14 +00:00
|
|
|
import android.os.Handler;
|
2019-09-22 08:29:59 +00:00
|
|
|
import android.text.TextUtils;
|
2018-08-12 17:51:57 +00:00
|
|
|
import android.view.LayoutInflater;
|
2018-08-06 08:39:28 +00:00
|
|
|
import android.view.View;
|
2018-08-12 17:51:57 +00:00
|
|
|
import android.view.ViewGroup;
|
2018-08-05 12:09:46 +00:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2020-06-13 07:42:54 +00:00
|
|
|
import android.widget.ScrollView;
|
2020-05-23 08:57:57 +00:00
|
|
|
import android.widget.TextView;
|
2019-08-17 08:26:36 +00:00
|
|
|
import android.widget.Toast;
|
2018-08-05 12:09:46 +00:00
|
|
|
|
2020-09-17 14:22:42 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2019-06-30 14:55:15 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2020-01-05 14:33:47 +00:00
|
|
|
import androidx.annotation.RequiresApi;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.appcompat.app.ActionBar;
|
2020-01-05 14:33:47 +00:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
2019-09-22 08:29:59 +00:00
|
|
|
import androidx.documentfile.provider.DocumentFile;
|
2018-08-08 06:55:47 +00:00
|
|
|
import androidx.fragment.app.Fragment;
|
2020-09-17 14:22:42 +00:00
|
|
|
import androidx.fragment.app.FragmentResultListener;
|
2018-08-22 05:18:31 +00:00
|
|
|
import androidx.lifecycle.Lifecycle;
|
2019-09-22 08:29:59 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2019-05-11 20:10:49 +00:00
|
|
|
import java.util.HashMap;
|
2019-09-22 08:29:59 +00:00
|
|
|
import java.util.List;
|
2019-05-11 20:10:49 +00:00
|
|
|
import java.util.Map;
|
|
|
|
|
2020-05-23 08:57:57 +00:00
|
|
|
import static android.app.ActionBar.DISPLAY_SHOW_CUSTOM;
|
2019-09-22 08:29:59 +00:00
|
|
|
import static android.app.Activity.RESULT_OK;
|
|
|
|
|
2019-01-15 17:41:55 +00:00
|
|
|
public class FragmentBase extends Fragment {
|
2019-08-18 08:03:14 +00:00
|
|
|
private String title = null;
|
2018-08-06 12:05:43 +00:00
|
|
|
private String subtitle = " ";
|
2018-08-22 05:18:31 +00:00
|
|
|
private boolean finish = false;
|
2020-06-02 13:25:30 +00:00
|
|
|
private boolean finished = false;
|
2020-09-17 20:40:38 +00:00
|
|
|
private String requestKey = null;
|
2018-08-05 12:09:46 +00:00
|
|
|
|
2020-06-13 07:42:54 +00:00
|
|
|
private int scrollTo = 0;
|
2019-09-22 08:29:59 +00:00
|
|
|
|
2020-09-17 20:40:38 +00:00
|
|
|
private static int requestSequence = 0;
|
|
|
|
|
2019-09-22 08:29:59 +00:00
|
|
|
private static final int REQUEST_ATTACHMENT = 51;
|
|
|
|
private static final int REQUEST_ATTACHMENTS = 52;
|
2020-01-05 14:33:47 +00:00
|
|
|
private static final int REQUEST_RECOVERABLE_PERMISSION = 53;
|
2019-09-22 08:29:59 +00:00
|
|
|
|
|
|
|
static final String ACTION_STORE_ATTACHMENT = BuildConfig.APPLICATION_ID + ".STORE_ATTACHMENT";
|
|
|
|
static final String ACTION_STORE_ATTACHMENTS = BuildConfig.APPLICATION_ID + ".STORE_ATTACHMENTS";
|
|
|
|
|
2019-08-18 08:03:14 +00:00
|
|
|
protected void setTitle(int resid) {
|
|
|
|
setTitle(getString(resid));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setTitle(String title) {
|
|
|
|
this.title = title;
|
|
|
|
updateSubtitle();
|
|
|
|
}
|
|
|
|
|
2018-08-05 12:09:46 +00:00
|
|
|
protected void setSubtitle(int resid) {
|
|
|
|
setSubtitle(getString(resid));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void setSubtitle(String subtitle) {
|
|
|
|
this.subtitle = subtitle;
|
|
|
|
updateSubtitle();
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:42:54 +00:00
|
|
|
void scrollTo(int resid) {
|
|
|
|
scrollTo = resid;
|
|
|
|
scrollTo();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void scrollTo() {
|
|
|
|
if (scrollTo == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
View view = getView();
|
|
|
|
if (view == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
final ScrollView scroll = view.findViewById(R.id.scroll);
|
|
|
|
if (scroll == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
final View child = scroll.findViewById(scrollTo);
|
|
|
|
if (child == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scrollTo = 0;
|
|
|
|
|
|
|
|
scroll.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
scroll.scrollTo(0, child.getTop());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-11 05:41:20 +00:00
|
|
|
@Override
|
|
|
|
public void startActivity(Intent intent) {
|
2019-09-09 12:58:31 +00:00
|
|
|
try {
|
2020-09-26 05:43:07 +00:00
|
|
|
Log.i("Start intent=" + intent);
|
|
|
|
Log.logExtras(intent);
|
2019-09-09 12:58:31 +00:00
|
|
|
super.startActivity(intent);
|
|
|
|
} catch (ActivityNotFoundException ex) {
|
2020-06-14 16:34:13 +00:00
|
|
|
Log.w(ex);
|
2021-01-30 10:13:29 +00:00
|
|
|
Helper.reportNoViewer(getContext(), intent);
|
2020-07-03 15:49:01 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
ToastEx.makeText(getContext(), Log.formatThrowable(ex), Toast.LENGTH_LONG).show();
|
2019-09-09 12:58:31 +00:00
|
|
|
}
|
2019-07-11 05:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void startActivityForResult(Intent intent, int requestCode) {
|
2019-08-17 08:26:36 +00:00
|
|
|
try {
|
2020-09-26 05:43:07 +00:00
|
|
|
Log.i("Start intent=" + intent + " request=" + requestCode);
|
|
|
|
Log.logExtras(intent);
|
2019-08-17 08:26:36 +00:00
|
|
|
super.startActivityForResult(intent, requestCode);
|
|
|
|
} catch (ActivityNotFoundException ex) {
|
2020-06-14 16:34:13 +00:00
|
|
|
Log.w(ex);
|
2021-01-30 10:13:29 +00:00
|
|
|
Helper.reportNoViewer(getContext(), intent);
|
2020-07-03 15:49:01 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
ToastEx.makeText(getContext(), Log.formatThrowable(ex), Toast.LENGTH_LONG).show();
|
2019-08-17 08:26:36 +00:00
|
|
|
}
|
2019-07-11 05:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 05:18:31 +00:00
|
|
|
protected void finish() {
|
2020-06-02 13:25:30 +00:00
|
|
|
if (finished)
|
|
|
|
return;
|
|
|
|
finished = true;
|
|
|
|
|
2019-07-27 06:02:00 +00:00
|
|
|
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
|
2019-10-12 15:16:53 +00:00
|
|
|
getParentFragmentManager().popBackStack();
|
2018-08-22 05:18:31 +00:00
|
|
|
else
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
|
2018-08-24 06:43:05 +00:00
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle outState) {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Save instance " + this);
|
2019-04-11 07:47:49 +00:00
|
|
|
int before = Helper.getSize(outState);
|
2019-03-02 07:35:12 +00:00
|
|
|
outState.putString("fair:subtitle", subtitle);
|
2020-09-17 20:40:38 +00:00
|
|
|
outState.putString("fair:requestKey", requestKey);
|
2019-04-11 17:30:39 +00:00
|
|
|
super.onSaveInstanceState(outState);
|
2019-04-11 07:47:49 +00:00
|
|
|
int after = Helper.getSize(outState);
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Saved instance " + this + " size=" + before + "/" + after);
|
2019-05-11 20:10:49 +00:00
|
|
|
|
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("name", this.getClass().getName());
|
|
|
|
crumb.put("before", Integer.toString(before));
|
|
|
|
crumb.put("after", Integer.toString(after));
|
2021-03-21 19:14:00 +00:00
|
|
|
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
|
2019-08-11 08:18:37 +00:00
|
|
|
for (String key : outState.keySet()) {
|
|
|
|
Object value = outState.get(key);
|
|
|
|
crumb.put(key, value == null ? "" : value.getClass().getName());
|
|
|
|
}
|
2019-08-12 11:07:14 +00:00
|
|
|
Log.breadcrumb("onSaveInstanceState", crumb);
|
2019-05-11 20:10:49 +00:00
|
|
|
|
2019-04-11 07:47:49 +00:00
|
|
|
for (String key : outState.keySet())
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Saved " + this + " " + key + "=" + outState.get(key));
|
2018-08-24 06:43:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 20:40:38 +00:00
|
|
|
public String getRequestKey() {
|
|
|
|
if (requestKey == null)
|
|
|
|
requestKey = getClass().getName() + "_" + (++requestSequence);
|
|
|
|
return requestKey;
|
|
|
|
}
|
|
|
|
|
2018-08-11 16:13:22 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Create " + this + " saved=" + (savedInstanceState != null));
|
2018-08-11 16:13:22 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
2020-09-17 15:04:00 +00:00
|
|
|
|
2021-04-17 12:29:53 +00:00
|
|
|
Bundle args = getArguments();
|
|
|
|
if (args == null)
|
|
|
|
setArguments(new Bundle());
|
|
|
|
|
2020-09-17 20:40:38 +00:00
|
|
|
if (savedInstanceState != null) {
|
2019-03-02 07:35:12 +00:00
|
|
|
subtitle = savedInstanceState.getString("fair:subtitle");
|
2020-09-17 20:40:38 +00:00
|
|
|
requestKey = savedInstanceState.getString("fair:requestKey");
|
|
|
|
}
|
2020-09-17 14:22:42 +00:00
|
|
|
|
2020-09-17 15:04:00 +00:00
|
|
|
// https://developer.android.com/training/basics/fragments/pass-data-between
|
2020-09-17 20:40:38 +00:00
|
|
|
getParentFragmentManager().setFragmentResultListener(getRequestKey(), this, new FragmentResultListener() {
|
2020-09-17 14:22:42 +00:00
|
|
|
@Override
|
|
|
|
public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle result) {
|
2020-10-02 10:16:25 +00:00
|
|
|
try {
|
2020-10-02 12:50:06 +00:00
|
|
|
result.setClassLoader(ApplicationEx.class.getClassLoader());
|
2020-10-02 10:16:25 +00:00
|
|
|
int requestCode = result.getInt("requestCode");
|
|
|
|
int resultCode = result.getInt("resultCode");
|
|
|
|
|
|
|
|
Intent data = new Intent();
|
|
|
|
data.putExtra("args", result);
|
|
|
|
onActivityResult(requestCode, resultCode, data);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
/*
|
|
|
|
android.os.BadParcelableException: ClassNotFoundException when unmarshalling: eu.faircode.email.FragmentMessages$MessageTarget
|
|
|
|
at android.os.Parcel.readParcelableCreator(Parcel.java:2839)
|
|
|
|
at android.os.Parcel.readParcelable(Parcel.java:2765)
|
|
|
|
at android.os.Parcel.readValue(Parcel.java:2668)
|
|
|
|
at android.os.Parcel.readListInternal(Parcel.java:3098)
|
|
|
|
at android.os.Parcel.readArrayList(Parcel.java:2319)
|
|
|
|
at android.os.Parcel.readValue(Parcel.java:2689)
|
|
|
|
at android.os.Parcel.readArrayMapInternal(Parcel.java:3037)
|
|
|
|
at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:288)
|
|
|
|
at android.os.BaseBundle.unparcel(BaseBundle.java:232)
|
|
|
|
at android.os.BaseBundle.getInt(BaseBundle.java:1017)
|
|
|
|
*/
|
|
|
|
}
|
2020-09-17 14:22:42 +00:00
|
|
|
}
|
|
|
|
});
|
2020-09-17 15:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
Log.d("Create view " + this + (savedInstanceState != null));
|
|
|
|
return super.onCreateView(inflater, container, savedInstanceState);
|
|
|
|
}
|
2020-09-17 14:22:42 +00:00
|
|
|
|
2020-09-17 15:04:00 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
Log.d("Activity " + this + " saved=" + (savedInstanceState != null));
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
2020-06-13 07:42:54 +00:00
|
|
|
scrollTo();
|
2018-08-11 16:13:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 12:09:46 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Resume " + this);
|
2018-08-05 12:09:46 +00:00
|
|
|
super.onResume();
|
|
|
|
updateSubtitle();
|
2018-08-22 05:18:31 +00:00
|
|
|
if (finish) {
|
2019-10-12 15:16:53 +00:00
|
|
|
getParentFragmentManager().popBackStack();
|
2018-08-22 05:18:31 +00:00
|
|
|
finish = false;
|
|
|
|
}
|
2019-09-22 08:29:59 +00:00
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
|
|
|
|
IntentFilter iff = new IntentFilter();
|
|
|
|
iff.addAction(ACTION_STORE_ATTACHMENT);
|
|
|
|
iff.addAction(ACTION_STORE_ATTACHMENTS);
|
|
|
|
lbm.registerReceiver(receiver, iff);
|
2018-08-05 12:09:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 16:13:22 +00:00
|
|
|
@Override
|
|
|
|
public void onPause() {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Pause " + this);
|
2018-08-11 16:13:22 +00:00
|
|
|
super.onPause();
|
2019-09-22 08:29:59 +00:00
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
|
|
|
|
lbm.unregisterReceiver(receiver);
|
2018-08-11 16:13:22 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 14:55:15 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
2019-10-08 08:48:31 +00:00
|
|
|
String action = (data == null ? null : data.getAction());
|
2019-06-30 14:55:15 +00:00
|
|
|
Log.i("Result class=" + this.getClass().getSimpleName() +
|
2019-10-08 08:48:31 +00:00
|
|
|
" action=" + action + " request=" + requestCode + " result=" + resultCode);
|
2019-07-01 08:59:38 +00:00
|
|
|
Log.logExtras(data);
|
2020-03-27 13:35:13 +00:00
|
|
|
if (data != null)
|
|
|
|
Log.i("data=" + data.getData());
|
2019-06-30 14:55:15 +00:00
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
2019-09-22 08:29:59 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
switch (requestCode) {
|
|
|
|
case REQUEST_ATTACHMENT:
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
onSaveAttachment(data);
|
|
|
|
break;
|
|
|
|
case REQUEST_ATTACHMENTS:
|
|
|
|
if (resultCode == RESULT_OK && data != null)
|
|
|
|
onSaveAttachments(data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-06-30 14:55:15 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 12:09:46 +00:00
|
|
|
@Override
|
|
|
|
public void onDetach() {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Detach " + this);
|
2018-08-05 12:09:46 +00:00
|
|
|
super.onDetach();
|
|
|
|
|
2021-03-10 11:01:25 +00:00
|
|
|
try {
|
2021-04-08 07:40:21 +00:00
|
|
|
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
2021-03-10 11:01:25 +00:00
|
|
|
View focused = getActivity().getCurrentFocus();
|
2021-04-08 07:40:21 +00:00
|
|
|
if (imm != null && focused != null)
|
|
|
|
imm.hideSoftInputFromWindow(focused.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
|
2021-03-10 11:01:25 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
/*
|
|
|
|
Caused by: java.lang.NullPointerException: Attempt to read from field 'com.android.internal.view.IInputMethodClient com.android.server.inputmethod.InputMethodManagerService$ClientState.client' on a null object reference
|
|
|
|
at android.os.Parcel.createException(Parcel.java:2077)
|
|
|
|
at android.os.Parcel.readException(Parcel.java:2039)
|
|
|
|
at android.os.Parcel.readException(Parcel.java:1987)
|
|
|
|
at com.android.internal.view.IInputMethodManager$Stub$Proxy.hideSoftInput(IInputMethodManager.java:615)
|
|
|
|
at android.view.inputmethod.InputMethodManager.hideSoftInputFromWindow(InputMethodManager.java:1523)
|
|
|
|
at android.view.inputmethod.InputMethodManager.hideSoftInputFromWindow(InputMethodManager.java:1485)
|
|
|
|
at eu.faircode.email.FragmentBase.onDetach(SourceFile:5)
|
|
|
|
at androidx.fragment.app.Fragment.performDetach(SourceFile:3)
|
|
|
|
*/
|
|
|
|
}
|
2018-08-05 12:09:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 16:13:22 +00:00
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d("Config " + this);
|
2018-08-11 16:13:22 +00:00
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2018-12-24 12:27:45 +00:00
|
|
|
Log.i("Destroy " + this);
|
2018-08-11 16:13:22 +00:00
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
2018-12-29 07:24:43 +00:00
|
|
|
@Override
|
|
|
|
public void setHasOptionsMenu(boolean hasMenu) {
|
|
|
|
super.setHasOptionsMenu(!isPane() && hasMenu);
|
|
|
|
}
|
|
|
|
|
2018-08-05 12:09:46 +00:00
|
|
|
private void updateSubtitle() {
|
|
|
|
AppCompatActivity activity = (AppCompatActivity) getActivity();
|
2018-12-29 07:24:43 +00:00
|
|
|
if (activity != null && !isPane()) {
|
2018-08-05 12:09:46 +00:00
|
|
|
ActionBar actionbar = activity.getSupportActionBar();
|
2020-05-23 08:57:57 +00:00
|
|
|
if (actionbar != null)
|
|
|
|
if ((actionbar.getDisplayOptions() & DISPLAY_SHOW_CUSTOM) == 0) {
|
|
|
|
actionbar.setTitle(title == null ? getString(R.string.app_name) : title);
|
|
|
|
actionbar.setSubtitle(subtitle);
|
|
|
|
} else {
|
|
|
|
View custom = actionbar.getCustomView();
|
|
|
|
TextView tvTitle = custom.findViewById(R.id.title);
|
|
|
|
TextView tvSubtitle = custom.findViewById(R.id.subtitle);
|
2021-02-17 11:04:00 +00:00
|
|
|
if (tvTitle != null)
|
|
|
|
tvTitle.setText(title == null ? getString(R.string.app_name) : title);
|
|
|
|
if (tvSubtitle != null)
|
|
|
|
tvSubtitle.setText(subtitle);
|
2020-05-23 08:57:57 +00:00
|
|
|
}
|
2018-08-05 12:09:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-29 07:24:43 +00:00
|
|
|
|
|
|
|
private boolean isPane() {
|
|
|
|
Bundle args = getArguments();
|
|
|
|
return (args != null && args.getBoolean("pane"));
|
|
|
|
}
|
2019-02-07 09:02:40 +00:00
|
|
|
|
|
|
|
boolean hasPermission(String name) {
|
|
|
|
ActivityBase activity = (ActivityBase) getActivity();
|
|
|
|
if (activity == null)
|
|
|
|
return false;
|
|
|
|
return activity.hasPermission(name);
|
|
|
|
}
|
2019-04-17 17:26:39 +00:00
|
|
|
|
2020-01-30 19:52:18 +00:00
|
|
|
void addKeyPressedListener(ActivityBase.IKeyPressedListener listener) {
|
|
|
|
((ActivityBase) getActivity()).addKeyPressedListener(listener, getViewLifecycleOwner());
|
2019-04-17 17:26:39 +00:00
|
|
|
}
|
2019-04-29 18:26:29 +00:00
|
|
|
|
|
|
|
void addBillingListener(ActivityBilling.IBillingListener listener) {
|
|
|
|
((ActivityBilling) getActivity()).addBillingListener(listener, getViewLifecycleOwner());
|
|
|
|
}
|
2019-09-22 08:29:59 +00:00
|
|
|
|
|
|
|
private BroadcastReceiver receiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
|
|
|
if (ACTION_STORE_ATTACHMENT.equals(action))
|
|
|
|
onStoreAttachment(intent);
|
|
|
|
if (ACTION_STORE_ATTACHMENTS.equals(action))
|
|
|
|
onStoreAttachments(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private void onStoreAttachment(Intent intent) {
|
2021-04-17 12:29:53 +00:00
|
|
|
long attachment = intent.getLongExtra("id", -1L);
|
|
|
|
getArguments().putLong("selected_attachment", attachment);
|
2020-09-26 05:43:07 +00:00
|
|
|
Log.i("Save attachment id=" + attachment);
|
|
|
|
|
2019-09-22 08:29:59 +00:00
|
|
|
Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT);
|
|
|
|
create.addCategory(Intent.CATEGORY_OPENABLE);
|
|
|
|
create.setType(intent.getStringExtra("type"));
|
|
|
|
create.putExtra(Intent.EXTRA_TITLE, intent.getStringExtra("name"));
|
2019-12-01 17:19:17 +00:00
|
|
|
Helper.openAdvanced(create);
|
2020-06-14 16:34:13 +00:00
|
|
|
PackageManager pm = getContext().getPackageManager();
|
2020-09-26 05:43:07 +00:00
|
|
|
if (create.resolveActivity(pm) == null) { // system whitelisted
|
|
|
|
Log.w("SAF missing");
|
2019-09-22 08:29:59 +00:00
|
|
|
ToastEx.makeText(getContext(), R.string.title_no_saf, Toast.LENGTH_LONG).show();
|
2020-09-26 05:43:07 +00:00
|
|
|
} else
|
2019-09-22 08:29:59 +00:00
|
|
|
startActivityForResult(Helper.getChooser(getContext(), create), REQUEST_ATTACHMENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onStoreAttachments(Intent intent) {
|
2021-04-17 12:29:53 +00:00
|
|
|
long message = intent.getLongExtra("id", -1L);
|
|
|
|
getArguments().putLong("selected_message", message);
|
2020-09-26 05:43:07 +00:00
|
|
|
Log.i("Save attachments message=" + message);
|
|
|
|
|
2019-09-22 08:29:59 +00:00
|
|
|
Intent tree = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
|
2019-12-01 17:19:17 +00:00
|
|
|
Helper.openAdvanced(tree);
|
2020-06-14 16:34:13 +00:00
|
|
|
PackageManager pm = getContext().getPackageManager();
|
2020-09-26 05:43:07 +00:00
|
|
|
if (tree.resolveActivity(pm) == null) { // system whitelisted
|
|
|
|
Log.w("SAF missing");
|
2019-09-22 08:29:59 +00:00
|
|
|
ToastEx.makeText(getContext(), R.string.title_no_saf, Toast.LENGTH_LONG).show();
|
2020-09-26 05:43:07 +00:00
|
|
|
} else
|
2019-09-22 08:29:59 +00:00
|
|
|
startActivityForResult(Helper.getChooser(getContext(), tree), REQUEST_ATTACHMENTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onSaveAttachment(Intent data) {
|
2021-04-17 12:29:53 +00:00
|
|
|
long attachment = getArguments().getLong("selected_attachment", -1L);
|
|
|
|
|
2019-09-22 08:29:59 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", attachment);
|
|
|
|
args.putParcelable("uri", data.getData());
|
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void onExecute(Context context, Bundle args) throws Throwable {
|
|
|
|
long id = args.getLong("id");
|
|
|
|
Uri uri = args.getParcelable("uri");
|
|
|
|
|
2019-11-03 19:07:26 +00:00
|
|
|
if (!"content".equals(uri.getScheme())) {
|
2019-09-22 08:29:59 +00:00
|
|
|
Log.w("Save attachment uri=" + uri);
|
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
|
|
|
|
}
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(id);
|
|
|
|
if (attachment == null)
|
|
|
|
return null;
|
|
|
|
File file = attachment.getFile(context);
|
|
|
|
|
|
|
|
OutputStream os = null;
|
|
|
|
InputStream is = null;
|
|
|
|
try {
|
2020-09-27 15:24:32 +00:00
|
|
|
os = context.getContentResolver().openOutputStream(uri);
|
2019-09-22 08:29:59 +00:00
|
|
|
is = new FileInputStream(file);
|
|
|
|
|
|
|
|
byte[] buffer = new byte[Helper.BUFFER_SIZE];
|
|
|
|
int read;
|
|
|
|
while ((read = is.read(buffer)) != -1)
|
|
|
|
os.write(buffer, 0, read);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (os != null)
|
|
|
|
os.close();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (is != null)
|
|
|
|
is.close();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Void data) {
|
|
|
|
ToastEx.makeText(getContext(), R.string.title_attachment_saved, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2020-02-20 10:43:08 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
|
2020-01-21 07:16:56 +00:00
|
|
|
if (ex instanceof RecoverableSecurityException) {
|
|
|
|
handle((RecoverableSecurityException) ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:45:34 +00:00
|
|
|
if (ex instanceof IllegalArgumentException ||
|
|
|
|
ex instanceof FileNotFoundException ||
|
|
|
|
ex instanceof SecurityException)
|
2019-09-22 08:29:59 +00:00
|
|
|
ToastEx.makeText(getContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
|
|
|
|
else
|
2019-12-06 07:50:46 +00:00
|
|
|
Log.unexpectedError(getParentFragmentManager(), ex);
|
2019-09-22 08:29:59 +00:00
|
|
|
}
|
|
|
|
}.execute(this, args, "attachment:save");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onSaveAttachments(Intent data) {
|
2021-04-17 12:29:53 +00:00
|
|
|
long message = getArguments().getLong("selected_message", -1L);
|
|
|
|
|
2019-09-22 08:29:59 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", message);
|
|
|
|
args.putParcelable("uri", data.getData());
|
|
|
|
|
|
|
|
new SimpleTask<Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void onExecute(Context context, Bundle args) throws Throwable {
|
|
|
|
long id = args.getLong("id");
|
|
|
|
Uri uri = args.getParcelable("uri");
|
|
|
|
|
2020-02-20 10:45:34 +00:00
|
|
|
if (!"content".equals(uri.getScheme())) {
|
|
|
|
Log.w("Save attachment uri=" + uri);
|
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
|
|
|
|
}
|
|
|
|
|
2019-09-22 08:29:59 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
DocumentFile tree = DocumentFile.fromTreeUri(context, uri);
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
|
|
|
|
for (EntityAttachment attachment : attachments) {
|
|
|
|
File file = attachment.getFile(context);
|
|
|
|
|
|
|
|
String name = Helper.sanitizeFilename(attachment.name);
|
|
|
|
if (TextUtils.isEmpty(name))
|
|
|
|
name = Long.toString(attachment.id);
|
2020-05-09 15:37:32 +00:00
|
|
|
DocumentFile document = tree.createFile(attachment.getMimeType(), name);
|
2019-09-22 08:29:59 +00:00
|
|
|
if (document == null)
|
2020-02-14 17:10:22 +00:00
|
|
|
throw new FileNotFoundException("Could not save " + uri + ":" + name);
|
2019-09-22 08:29:59 +00:00
|
|
|
|
|
|
|
OutputStream os = null;
|
|
|
|
InputStream is = null;
|
|
|
|
try {
|
2020-10-05 06:11:01 +00:00
|
|
|
os = context.getContentResolver().openOutputStream(document.getUri());
|
2019-09-22 08:29:59 +00:00
|
|
|
is = new FileInputStream(file);
|
|
|
|
|
|
|
|
byte[] buffer = new byte[Helper.BUFFER_SIZE];
|
|
|
|
int read;
|
|
|
|
while ((read = is.read(buffer)) != -1)
|
|
|
|
os.write(buffer, 0, read);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (os != null)
|
|
|
|
os.close();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (is != null)
|
|
|
|
is.close();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onExecuted(Bundle args, Void data) {
|
|
|
|
ToastEx.makeText(getContext(), R.string.title_attachments_saved, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2020-10-05 06:11:01 +00:00
|
|
|
Log.w(ex);
|
2020-02-20 10:43:08 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
|
2020-01-21 07:16:56 +00:00
|
|
|
if (ex instanceof RecoverableSecurityException) {
|
|
|
|
handle((RecoverableSecurityException) ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:45:34 +00:00
|
|
|
if (ex instanceof IllegalArgumentException ||
|
|
|
|
ex instanceof FileNotFoundException ||
|
|
|
|
ex instanceof SecurityException)
|
2020-02-14 17:10:22 +00:00
|
|
|
ToastEx.makeText(getContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
|
|
|
|
else
|
|
|
|
Log.unexpectedError(getParentFragmentManager(), ex);
|
2019-09-22 08:29:59 +00:00
|
|
|
}
|
|
|
|
}.execute(this, args, "attachments:save");
|
|
|
|
}
|
2020-01-05 14:33:47 +00:00
|
|
|
|
2020-02-20 10:43:08 +00:00
|
|
|
@RequiresApi(api = Build.VERSION_CODES.Q)
|
2020-01-05 14:33:47 +00:00
|
|
|
private void handle(RecoverableSecurityException ex) {
|
|
|
|
new AlertDialog.Builder(getContext())
|
|
|
|
.setMessage(ex.getMessage())
|
|
|
|
.setPositiveButton(ex.getUserAction().getTitle(), new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
try {
|
|
|
|
startIntentSenderForResult(
|
|
|
|
ex.getUserAction().getActionIntent().getIntentSender(),
|
|
|
|
REQUEST_RECOVERABLE_PERMISSION,
|
|
|
|
null, 0, 0, 0, null);
|
|
|
|
} catch (IntentSender.SendIntentException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
|
|
|
.show();
|
|
|
|
}
|
2020-08-23 15:34:14 +00:00
|
|
|
|
|
|
|
Handler getMainHandler() {
|
|
|
|
return ApplicationEx.getMainHandler();
|
|
|
|
}
|
2018-08-05 12:09:46 +00:00
|
|
|
}
|