Remove dependency on getUserVisibleHint

This commit is contained in:
M66B 2019-06-19 10:15:47 +02:00
parent db0c857e29
commit ccbd430166
3 changed files with 21 additions and 61 deletions

View File

@ -25,12 +25,9 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
@ -158,19 +155,6 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
return Helper.hasPermission(this, name);
}
protected View getVisibleView() {
for (Fragment fragment : getSupportFragmentManager().getFragments())
if (fragment.getUserVisibleHint()) {
View view = fragment.getView();
Log.i("Visible fragment=" + fragment.getClass().getName() + " view=" + view);
if (view != null && !(view instanceof ScrollView)) // Snackbar cannot be attached to ScrollView
return view;
}
Log.i("Visible activity=" + this.getClass().getName());
return findViewById(android.R.id.content);
}
void addBackPressedListener(final IBackPressedListener listener, LifecycleOwner owner) {
Log.i("Adding back listener=" + listener);
backPressedListeners.add(listener);

View File

@ -29,10 +29,9 @@ import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Base64;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
@ -53,7 +52,6 @@ import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.google.android.material.snackbar.Snackbar;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
@ -168,7 +166,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
String text = getBillingResponseText(result);
Log.i("IAB launch billing flow response=" + text);
if (result.getResponseCode() != BillingClient.BillingResponseCode.OK)
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityBilling.this, text, Toast.LENGTH_LONG).show();
} else
Helper.view(this, this, getIntentPro());
}
@ -188,19 +186,10 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
.putBoolean("play_store", false)
.apply();
Log.i("Response valid");
Snackbar snackbar = Snackbar.make(getVisibleView(), R.string.title_pro_valid, Snackbar.LENGTH_LONG);
snackbar.setAction(R.string.title_check, new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, new FragmentPro()).addToBackStack("pro");
fragmentTransaction.commit();
}
});
snackbar.show();
Toast.makeText(ActivityBilling.this, R.string.title_pro_valid, Toast.LENGTH_LONG).show();
} else {
Log.i("Response invalid");
Snackbar.make(getVisibleView(), R.string.title_pro_invalid, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityBilling.this, R.string.title_pro_invalid, Toast.LENGTH_LONG).show();
}
} catch (NoSuchAlgorithmException ex) {
Log.e(ex);
@ -223,7 +212,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
backoff = 4;
queryPurchases();
} else
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityBilling.this, text, Toast.LENGTH_LONG).show();
}
@Override
@ -251,7 +240,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
checkPurchases(purchases);
else
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityBilling.this, text, Toast.LENGTH_LONG).show();
}
private void queryPurchases() {
@ -262,7 +251,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
if (result.getResponseCode() == BillingClient.BillingResponseCode.OK)
checkPurchases(result.getPurchasesList());
else
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityBilling.this, text, Toast.LENGTH_LONG).show();
}
interface IBillingListener {
@ -337,11 +326,11 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
} else {
Log.w("Invalid signature");
Snackbar.make(getVisibleView(), R.string.title_pro_invalid, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityBilling.this, R.string.title_pro_invalid, Toast.LENGTH_LONG).show();
}
} catch (Throwable ex) {
Log.e(ex);
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityBilling.this, Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
editor.apply();

View File

@ -69,7 +69,6 @@ import androidx.recyclerview.widget.RecyclerView;
import com.android.colorpicker.ColorPickerDialog;
import com.android.colorpicker.ColorPickerSwatch;
import com.google.android.material.snackbar.Snackbar;
import org.json.JSONArray;
import org.json.JSONObject;
@ -689,10 +688,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, Helper.formatThrowable(ex), Toast.LENGTH_LONG).show();
}
}.execute(this, new Bundle(), "crash:log");
}
@ -815,7 +811,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
protected void onException(Bundle args, Throwable ex) {
if (args.getBoolean("always"))
if (ex instanceof IllegalArgumentException || ex instanceof IOException)
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, ex.getMessage(), Toast.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivityView.this, ActivityView.this, ex);
}
@ -996,7 +992,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, ex.getMessage(), Toast.LENGTH_LONG).show();
else
Toast.makeText(ActivityView.this, ex.toString(), Toast.LENGTH_LONG).show();
}
@ -1120,7 +1116,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
create.setType("*/*");
create.putExtra(Intent.EXTRA_TITLE, "email.eml");
if (create.resolveActivity(getPackageManager()) == null)
Snackbar.make(getVisibleView(), R.string.title_no_saf, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, R.string.title_no_saf, Toast.LENGTH_LONG).show();
else
startActivityForResult(Helper.getChooser(this, create), REQUEST_RAW);
}
@ -1170,7 +1166,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
create.setType(intent.getStringExtra("type"));
create.putExtra(Intent.EXTRA_TITLE, intent.getStringExtra("name"));
if (create.resolveActivity(getPackageManager()) == null)
Snackbar.make(getVisibleView(), R.string.title_no_saf, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, R.string.title_no_saf, Toast.LENGTH_LONG).show();
else
startActivityForResult(Helper.getChooser(this, create), REQUEST_ATTACHMENT);
}
@ -1180,7 +1176,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
Intent tree = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
//tree.putExtra("android.content.extra.SHOW_ADVANCED", true);
if (tree.resolveActivity(getPackageManager()) == null)
Snackbar.make(getVisibleView(), R.string.title_no_saf, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, R.string.title_no_saf, Toast.LENGTH_LONG).show();
else
startActivityForResult(Helper.getChooser(this, tree), REQUEST_ATTACHMENTS);
}
@ -1312,17 +1308,8 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
data.setAction(OpenPgpApi.ACTION_DECRYPT_VERIFY);
decrypt(data, intent.getLongExtra("id", -1));
} else {
Snackbar snackbar = Snackbar.make(getVisibleView(), R.string.title_no_openpgp, Snackbar.LENGTH_LONG);
if (Helper.getIntentOpenKeychain().resolveActivity(getPackageManager()) != null)
snackbar.setAction(R.string.title_fix, new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(Helper.getIntentOpenKeychain());
}
});
snackbar.show();
}
} else
Toast.makeText(ActivityView.this, R.string.title_no_openpgp, Toast.LENGTH_LONG).show();
}
private void onShowPro(Intent intent) {
@ -1471,7 +1458,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
resid = R.string.title_signature_valid;
else
resid = R.string.title_signature_invalid;
Snackbar.make(getVisibleView(), resid, Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, resid, Toast.LENGTH_LONG).show();
break;
@ -1505,7 +1492,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, ex.getMessage(), Toast.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivityView.this, ActivityView.this, ex);
}
@ -1600,7 +1587,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, ex.getMessage(), Toast.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivityView.this, ActivityView.this, ex);
}
@ -1673,7 +1660,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
Toast.makeText(ActivityView.this, ex.getMessage(), Toast.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivityView.this, ActivityView.this, ex);
}