Attach snackbar to visible view

pull/146/head
M66B 6 years ago
parent 2cce8f73f9
commit bd087e7ab8

@ -25,6 +25,7 @@ import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.Arrays;
@ -32,6 +33,7 @@ import java.util.List;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
abstract class ActivityBase extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private static String[] restart = new String[]{
@ -100,6 +102,17 @@ abstract class ActivityBase extends AppCompatActivity implements SharedPreferenc
finish();
}
protected View getVisibleView() {
for (Fragment fragment : getSupportFragmentManager().getFragments())
if (fragment.getUserVisibleHint()) {
Log.i(Helper.TAG, "Visible fragment=" + fragment.getClass().getName());
return fragment.getView();
}
Log.i(Helper.TAG, "Visible activity=" + this.getClass().getName());
return findViewById(android.R.id.content);
}
private List<IBackPressedListener> backPressedListeners = new ArrayList<>();
public void addBackPressedListener(IBackPressedListener listener) {

@ -30,7 +30,6 @@ import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
@ -122,10 +121,6 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
}
};
private View getView() {
return findViewById(android.R.id.content);
}
private void onPurchase(Intent intent) {
if (Helper.isPlayStoreInstall(this)) {
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
@ -136,7 +131,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
String text = Helper.getBillingResponseText(responseCode);
Log.i(Helper.TAG, "IAB launch billing flow response=" + text);
if (responseCode != BillingClient.BillingResponse.OK)
Snackbar.make(getView(), text, Snackbar.LENGTH_LONG).show();
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
} else
Helper.view(this, this, getIntentPro());
}
@ -153,10 +148,10 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("pro", true).apply();
Log.i(Helper.TAG, "Response valid");
Snackbar.make(getView(), R.string.title_pro_valid, Snackbar.LENGTH_LONG).show();
Snackbar.make(getVisibleView(), R.string.title_pro_valid, Snackbar.LENGTH_LONG).show();
} else {
Log.i(Helper.TAG, "Response invalid");
Snackbar.make(getView(), R.string.title_pro_invalid, Snackbar.LENGTH_LONG).show();
Snackbar.make(getVisibleView(), R.string.title_pro_invalid, Snackbar.LENGTH_LONG).show();
}
} catch (NoSuchAlgorithmException ex) {
Log.e(Helper.TAG, Log.getStackTraceString(ex));
@ -175,7 +170,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
backoff = 4;
queryPurchases();
} else
Snackbar.make(getView(), text, Snackbar.LENGTH_LONG).show();
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
}
@Override
@ -199,7 +194,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
if (responseCode == BillingClient.BillingResponse.OK)
checkPurchases(purchases);
else
Snackbar.make(getView(), text, Snackbar.LENGTH_LONG).show();
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
}
private void queryPurchases() {
@ -209,7 +204,7 @@ abstract class ActivityBilling extends ActivityBase implements PurchasesUpdatedL
if (result.getResponseCode() == BillingClient.BillingResponse.OK)
checkPurchases(result.getPurchasesList());
else
Snackbar.make(getView(), text, Snackbar.LENGTH_LONG).show();
Snackbar.make(getVisibleView(), text, Snackbar.LENGTH_LONG).show();
}
private void checkPurchases(List<Purchase> purchases) {

@ -1266,7 +1266,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
// Show undo snackbar
final Snackbar snackbar = Snackbar.make(
view,
getVisibleView(),
getString(R.string.title_moving, result.target.getDisplayName(this)),
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.title_undo, new View.OnClickListener() {
@ -1371,7 +1371,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(view, R.string.title_no_saf, Snackbar.LENGTH_LONG).show();
Snackbar.make(getVisibleView(), R.string.title_no_saf, Snackbar.LENGTH_LONG).show();
else
startActivityForResult(create, REQUEST_ATTACHMENT);
}
@ -1384,7 +1384,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
decrypt(data, intent.getLongExtra("id", -1));
} else {
Snackbar snackbar = Snackbar.make(view, R.string.title_no_openpgp, Snackbar.LENGTH_LONG);
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
@ -1547,7 +1547,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
Snackbar.make(getVisibleView(), ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Helper.unexpectedError(ActivityView.this, ActivityView.this, ex);
}

Loading…
Cancel
Save