From 2bacba2c87f8d67659534ff93bee3317b3c746dc Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Wed, 27 May 2020 10:52:25 +0100 Subject: [PATCH] properly close all activities when service is stopped --- app/build.gradle | 4 +-- .../io/lbry/browser/FirstRunActivity.java | 33 +++++++++++-------- .../java/io/lbry/browser/MainActivity.java | 15 +++++++-- .../io/lbry/browser/VerificationActivity.java | 25 ++++++++++++++ .../ui/publish/PublishFormFragment.java | 6 +++- .../browser/ui/publish/PublishFragment.java | 13 ++++++-- .../java/io/lbry/browser/utils/Lbryio.java | 14 ++++++++ 7 files changed, 89 insertions(+), 21 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c4496697..73499098 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,8 +16,8 @@ android { applicationId "io.lbry.browser" minSdkVersion 21 targetSdkVersion 29 - versionCode 1507 - versionName "0.15.7" + versionCode 1508 + versionName "0.15.8" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/io/lbry/browser/FirstRunActivity.java b/app/src/main/java/io/lbry/browser/FirstRunActivity.java index b4d42ee4..df618fe5 100644 --- a/app/src/main/java/io/lbry/browser/FirstRunActivity.java +++ b/app/src/main/java/io/lbry/browser/FirstRunActivity.java @@ -10,7 +10,6 @@ import android.os.Bundle; import android.text.method.LinkMovementMethod; import android.view.View; import android.widget.TextView; -import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.core.text.HtmlCompat; @@ -20,10 +19,11 @@ import io.lbry.browser.utils.Helper; import io.lbry.browser.utils.Lbry; import io.lbry.browser.utils.LbryAnalytics; import io.lbry.browser.utils.Lbryio; +import io.lbry.lbrysdk.LbrynetService; public class FirstRunActivity extends AppCompatActivity { - private BroadcastReceiver sdkReadyReceiver; + private BroadcastReceiver sdkReceiver; private BroadcastReceiver authReceiver; @Override @@ -45,19 +45,26 @@ public class FirstRunActivity extends AppCompatActivity { registerAuthReceiver(); if (!Lbry.SDK_READY) { findViewById(R.id.welcome_wait_container).setVisibility(View.VISIBLE); - IntentFilter filter = new IntentFilter(); - filter.addAction(MainActivity.ACTION_SDK_READY); - sdkReadyReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - // authenticate after we receive the sdk ready event - authenticate(); - } - }; - registerReceiver(sdkReadyReceiver, filter); } else { authenticate(); } + + IntentFilter filter = new IntentFilter(); + filter.addAction(MainActivity.ACTION_SDK_READY); + filter.addAction(LbrynetService.ACTION_STOP_SERVICE); + sdkReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (MainActivity.ACTION_SDK_READY.equals(action)) { + // authenticate after we receive the sdk ready event + authenticate(); + } else if (LbrynetService.ACTION_STOP_SERVICE.equals(action)) { + finish(); + } + } + }; + registerReceiver(sdkReceiver, filter); } public void onResume() { @@ -122,7 +129,7 @@ public class FirstRunActivity extends AppCompatActivity { @Override protected void onDestroy() { Helper.unregisterReceiver(authReceiver, this); - Helper.unregisterReceiver(sdkReadyReceiver, this); + Helper.unregisterReceiver(sdkReceiver, this); super.onDestroy(); } diff --git a/app/src/main/java/io/lbry/browser/MainActivity.java b/app/src/main/java/io/lbry/browser/MainActivity.java index eace68dd..586ea4d4 100644 --- a/app/src/main/java/io/lbry/browser/MainActivity.java +++ b/app/src/main/java/io/lbry/browser/MainActivity.java @@ -921,7 +921,6 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener applyNavbarSigninPadding(); checkFirstRun(); checkNowPlaying(); - fetchRewards(); // check (and start) the LBRY SDK service serviceRunning = isServiceRunning(this, LbrynetService.class); @@ -1422,13 +1421,18 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); boolean firstRunCompleted = sp.getBoolean(PREFERENCE_KEY_INTERNAL_FIRST_RUN_COMPLETED, false); if (!firstRunCompleted) { + findViewById(R.id.drawer_layout).setVisibility(View.INVISIBLE); startActivity(new Intent(this, FirstRunActivity.class)); - } else if (!appStarted) { + return; + } + + if (!appStarted) { // first run completed, startup startup(); return; } + fetchRewards(); if (getSupportFragmentManager().getBackStackEntryCount() == 0) { openFragment(FollowingFragment.class, false, NavMenuItem.ID_ITEM_FOLLOWING); } @@ -1492,7 +1496,11 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener initFloatingWalletBalance(); - checkAndClaimNewAndroidReward(); + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); + boolean firstRunCompleted = sp.getBoolean(PREFERENCE_KEY_INTERNAL_FIRST_RUN_COMPLETED, false); + if (firstRunCompleted) { + checkAndClaimNewAndroidReward(); + } } public void checkAndClaimNewAndroidReward() { @@ -2196,6 +2204,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener hideActionBar(); lockDrawer(); findViewById(R.id.splash_view).setVisibility(View.VISIBLE); + findViewById(R.id.drawer_layout).setVisibility(View.VISIBLE); LbryAnalytics.setCurrentScreen(MainActivity.this, "Splash", "Splash"); initStartupStages(); } diff --git a/app/src/main/java/io/lbry/browser/VerificationActivity.java b/app/src/main/java/io/lbry/browser/VerificationActivity.java index cf819cb6..32324e8d 100644 --- a/app/src/main/java/io/lbry/browser/VerificationActivity.java +++ b/app/src/main/java/io/lbry/browser/VerificationActivity.java @@ -1,6 +1,9 @@ package io.lbry.browser; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.graphics.Color; import android.os.AsyncTask; import android.os.Bundle; @@ -18,8 +21,10 @@ import io.lbry.browser.listener.SignInListener; import io.lbry.browser.listener.WalletSyncListener; import io.lbry.browser.model.lbryinc.User; import io.lbry.browser.tasks.lbryinc.FetchCurrentUserTask; +import io.lbry.browser.utils.Helper; import io.lbry.browser.utils.LbryAnalytics; import io.lbry.browser.utils.Lbryio; +import io.lbry.lbrysdk.LbrynetService; public class VerificationActivity extends FragmentActivity implements SignInListener, WalletSyncListener { @@ -27,6 +32,7 @@ public class VerificationActivity extends FragmentActivity implements SignInList public static final int VERIFICATION_FLOW_REWARDS = 2; public static final int VERIFICATION_FLOW_WALLET = 3; + private BroadcastReceiver sdkReceiver; private String email; private boolean signedIn; private int flow; @@ -54,6 +60,19 @@ public class VerificationActivity extends FragmentActivity implements SignInList return; } + IntentFilter filter = new IntentFilter(); + filter.addAction(LbrynetService.ACTION_STOP_SERVICE); + sdkReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (LbrynetService.ACTION_STOP_SERVICE.equals(action)) { + finish(); + } + } + }; + registerReceiver(sdkReceiver, filter); + setContentView(R.layout.activity_verification); ViewPager2 viewPager = findViewById(R.id.verification_pager); viewPager.setUserInputEnabled(false); @@ -269,4 +288,10 @@ public class VerificationActivity extends FragmentActivity implements SignInList public void onWalletSyncFailed(Exception error) { findViewById(R.id.verification_close_button).setVisibility(View.VISIBLE); } + + @Override + public void onDestroy() { + Helper.unregisterReceiver(sdkReceiver, this); + super.onDestroy(); + } } diff --git a/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java b/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java index 87a1665f..a8cb1580 100644 --- a/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/publish/PublishFormFragment.java @@ -102,6 +102,7 @@ public class PublishFormFragment extends BaseFragment implements private static final int MAX_VIDEO_DIMENSION = 1920; private static final int MAX_BITRATE = 5000000; // 5mbps + private boolean storageRefusedOnce; private static final int SUGGESTED_LIMIT = 8; private boolean editMode; @@ -1394,7 +1395,10 @@ public class PublishFormFragment extends BaseFragment implements @Override public void onStoragePermissionRefused() { - showError(getString(R.string.storage_permission_rationale_images)); + if (!storageRefusedOnce) { + showError(getString(R.string.storage_permission_rationale_images)); + storageRefusedOnce = true; + } launchPickerPending = false; } diff --git a/app/src/main/java/io/lbry/browser/ui/publish/PublishFragment.java b/app/src/main/java/io/lbry/browser/ui/publish/PublishFragment.java index bd4dd9c8..8ec12b4e 100644 --- a/app/src/main/java/io/lbry/browser/ui/publish/PublishFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/publish/PublishFragment.java @@ -53,6 +53,7 @@ public class PublishFragment extends BaseFragment implements CameraPermissionListener, FilePickerListener, StoragePermissionListener { private boolean cameraPreviewInitialized; + private boolean storageRefusedOnce; private PreviewView cameraPreview; private RecyclerView galleryGrid; private GalleryGridAdapter adapter; @@ -399,8 +400,16 @@ public class PublishFragment extends BaseFragment implements @Override public void onStoragePermissionRefused() { - Snackbar.make(getView(), R.string.storage_permission_rationale_videos, Snackbar.LENGTH_LONG). - setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + if (!storageRefusedOnce) { + View root = getView(); + if (root != null) { + Snackbar.make(root, R.string.storage_permission_rationale_videos, Snackbar.LENGTH_LONG). + setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); + Helper.setViewText(noVideosLoaded, R.string.storage_permission_rationale_videos); + checkNoVideosLoaded(); + } + storageRefusedOnce = true; + } } public String getSuggestedPublishUrl() { diff --git a/app/src/main/java/io/lbry/browser/utils/Lbryio.java b/app/src/main/java/io/lbry/browser/utils/Lbryio.java index 8b96f6a7..f94fd3ac 100644 --- a/app/src/main/java/io/lbry/browser/utils/Lbryio.java +++ b/app/src/main/java/io/lbry/browser/utils/Lbryio.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import io.lbry.browser.BuildConfig; import io.lbry.browser.MainActivity; import io.lbry.browser.exceptions.LbryioRequestException; import io.lbry.browser.exceptions.LbryioResponseException; @@ -81,6 +82,10 @@ public final class Lbryio { authToken = getAuthToken(context); } + if (BuildConfig.DEBUG) { + Log.d(TAG, String.format("Using authToken for request: %s", authToken)); + } + String url = String.format("%s/%s/%s", CONNECTION_STRING, resource, action); if (Helper.METHOD_GET.equalsIgnoreCase(method)) { Uri.Builder uriBuilder = Uri.parse(url).buildUpon(); @@ -94,6 +99,9 @@ public final class Lbryio { } url = uriBuilder.build().toString(); } + if (BuildConfig.DEBUG) { + Log.d(TAG, String.format("Request Method: %s, Sending request to URL: %s", method, url)); + } Request.Builder builder = new Request.Builder().url(url); if (Helper.METHOD_POST.equalsIgnoreCase(method)) { @@ -142,6 +150,9 @@ public final class Lbryio { } generatingAuthToken = true; + if (BuildConfig.DEBUG) { + Log.d(TAG, "Generating a new auth token"); + } Map options = new HashMap<>(); options.put("auth_token", ""); @@ -150,6 +161,9 @@ public final class Lbryio { Response response = Lbryio.call("user", "new", options, "post", context); try { JSONObject json = (JSONObject) parseResponse(response); + if (BuildConfig.DEBUG) { + Log.d(TAG, String.format("/user/new response: %s", json.toString(2))); + } if (!json.has(AUTH_TOKEN_PARAM)) { throw new LbryioResponseException("auth_token was not set in the response"); }