properly close all activities when service is stopped

This commit is contained in:
Akinwale Ariwodola 2020-05-27 10:52:25 +01:00
parent e473981063
commit 2bacba2c87
7 changed files with 89 additions and 21 deletions

View file

@ -16,8 +16,8 @@ android {
applicationId "io.lbry.browser" applicationId "io.lbry.browser"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 29 targetSdkVersion 29
versionCode 1507 versionCode 1508
versionName "0.15.7" versionName "0.15.8"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View file

@ -10,7 +10,6 @@ import android.os.Bundle;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.text.HtmlCompat; 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.Lbry;
import io.lbry.browser.utils.LbryAnalytics; import io.lbry.browser.utils.LbryAnalytics;
import io.lbry.browser.utils.Lbryio; import io.lbry.browser.utils.Lbryio;
import io.lbry.lbrysdk.LbrynetService;
public class FirstRunActivity extends AppCompatActivity { public class FirstRunActivity extends AppCompatActivity {
private BroadcastReceiver sdkReadyReceiver; private BroadcastReceiver sdkReceiver;
private BroadcastReceiver authReceiver; private BroadcastReceiver authReceiver;
@Override @Override
@ -45,19 +45,26 @@ public class FirstRunActivity extends AppCompatActivity {
registerAuthReceiver(); registerAuthReceiver();
if (!Lbry.SDK_READY) { if (!Lbry.SDK_READY) {
findViewById(R.id.welcome_wait_container).setVisibility(View.VISIBLE); 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 { } else {
authenticate(); 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() { public void onResume() {
@ -122,7 +129,7 @@ public class FirstRunActivity extends AppCompatActivity {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
Helper.unregisterReceiver(authReceiver, this); Helper.unregisterReceiver(authReceiver, this);
Helper.unregisterReceiver(sdkReadyReceiver, this); Helper.unregisterReceiver(sdkReceiver, this);
super.onDestroy(); super.onDestroy();
} }

View file

@ -921,7 +921,6 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
applyNavbarSigninPadding(); applyNavbarSigninPadding();
checkFirstRun(); checkFirstRun();
checkNowPlaying(); checkNowPlaying();
fetchRewards();
// check (and start) the LBRY SDK service // check (and start) the LBRY SDK service
serviceRunning = isServiceRunning(this, LbrynetService.class); serviceRunning = isServiceRunning(this, LbrynetService.class);
@ -1422,13 +1421,18 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstRunCompleted = sp.getBoolean(PREFERENCE_KEY_INTERNAL_FIRST_RUN_COMPLETED, false); boolean firstRunCompleted = sp.getBoolean(PREFERENCE_KEY_INTERNAL_FIRST_RUN_COMPLETED, false);
if (!firstRunCompleted) { if (!firstRunCompleted) {
findViewById(R.id.drawer_layout).setVisibility(View.INVISIBLE);
startActivity(new Intent(this, FirstRunActivity.class)); startActivity(new Intent(this, FirstRunActivity.class));
} else if (!appStarted) { return;
}
if (!appStarted) {
// first run completed, startup // first run completed, startup
startup(); startup();
return; return;
} }
fetchRewards();
if (getSupportFragmentManager().getBackStackEntryCount() == 0) { if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
openFragment(FollowingFragment.class, false, NavMenuItem.ID_ITEM_FOLLOWING); openFragment(FollowingFragment.class, false, NavMenuItem.ID_ITEM_FOLLOWING);
} }
@ -1492,7 +1496,11 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
initFloatingWalletBalance(); initFloatingWalletBalance();
checkAndClaimNewAndroidReward(); SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstRunCompleted = sp.getBoolean(PREFERENCE_KEY_INTERNAL_FIRST_RUN_COMPLETED, false);
if (firstRunCompleted) {
checkAndClaimNewAndroidReward();
}
} }
public void checkAndClaimNewAndroidReward() { public void checkAndClaimNewAndroidReward() {
@ -2196,6 +2204,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener
hideActionBar(); hideActionBar();
lockDrawer(); lockDrawer();
findViewById(R.id.splash_view).setVisibility(View.VISIBLE); findViewById(R.id.splash_view).setVisibility(View.VISIBLE);
findViewById(R.id.drawer_layout).setVisibility(View.VISIBLE);
LbryAnalytics.setCurrentScreen(MainActivity.this, "Splash", "Splash"); LbryAnalytics.setCurrentScreen(MainActivity.this, "Splash", "Splash");
initStartupStages(); initStartupStages();
} }

View file

@ -1,6 +1,9 @@
package io.lbry.browser; package io.lbry.browser;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color; import android.graphics.Color;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
@ -18,8 +21,10 @@ import io.lbry.browser.listener.SignInListener;
import io.lbry.browser.listener.WalletSyncListener; import io.lbry.browser.listener.WalletSyncListener;
import io.lbry.browser.model.lbryinc.User; import io.lbry.browser.model.lbryinc.User;
import io.lbry.browser.tasks.lbryinc.FetchCurrentUserTask; import io.lbry.browser.tasks.lbryinc.FetchCurrentUserTask;
import io.lbry.browser.utils.Helper;
import io.lbry.browser.utils.LbryAnalytics; import io.lbry.browser.utils.LbryAnalytics;
import io.lbry.browser.utils.Lbryio; import io.lbry.browser.utils.Lbryio;
import io.lbry.lbrysdk.LbrynetService;
public class VerificationActivity extends FragmentActivity implements SignInListener, WalletSyncListener { 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_REWARDS = 2;
public static final int VERIFICATION_FLOW_WALLET = 3; public static final int VERIFICATION_FLOW_WALLET = 3;
private BroadcastReceiver sdkReceiver;
private String email; private String email;
private boolean signedIn; private boolean signedIn;
private int flow; private int flow;
@ -54,6 +60,19 @@ public class VerificationActivity extends FragmentActivity implements SignInList
return; 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); setContentView(R.layout.activity_verification);
ViewPager2 viewPager = findViewById(R.id.verification_pager); ViewPager2 viewPager = findViewById(R.id.verification_pager);
viewPager.setUserInputEnabled(false); viewPager.setUserInputEnabled(false);
@ -269,4 +288,10 @@ public class VerificationActivity extends FragmentActivity implements SignInList
public void onWalletSyncFailed(Exception error) { public void onWalletSyncFailed(Exception error) {
findViewById(R.id.verification_close_button).setVisibility(View.VISIBLE); findViewById(R.id.verification_close_button).setVisibility(View.VISIBLE);
} }
@Override
public void onDestroy() {
Helper.unregisterReceiver(sdkReceiver, this);
super.onDestroy();
}
} }

View file

@ -102,6 +102,7 @@ public class PublishFormFragment extends BaseFragment implements
private static final int MAX_VIDEO_DIMENSION = 1920; private static final int MAX_VIDEO_DIMENSION = 1920;
private static final int MAX_BITRATE = 5000000; // 5mbps private static final int MAX_BITRATE = 5000000; // 5mbps
private boolean storageRefusedOnce;
private static final int SUGGESTED_LIMIT = 8; private static final int SUGGESTED_LIMIT = 8;
private boolean editMode; private boolean editMode;
@ -1394,7 +1395,10 @@ public class PublishFormFragment extends BaseFragment implements
@Override @Override
public void onStoragePermissionRefused() { 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; launchPickerPending = false;
} }

View file

@ -53,6 +53,7 @@ public class PublishFragment extends BaseFragment implements
CameraPermissionListener, FilePickerListener, StoragePermissionListener { CameraPermissionListener, FilePickerListener, StoragePermissionListener {
private boolean cameraPreviewInitialized; private boolean cameraPreviewInitialized;
private boolean storageRefusedOnce;
private PreviewView cameraPreview; private PreviewView cameraPreview;
private RecyclerView galleryGrid; private RecyclerView galleryGrid;
private GalleryGridAdapter adapter; private GalleryGridAdapter adapter;
@ -399,8 +400,16 @@ public class PublishFragment extends BaseFragment implements
@Override @Override
public void onStoragePermissionRefused() { public void onStoragePermissionRefused() {
Snackbar.make(getView(), R.string.storage_permission_rationale_videos, Snackbar.LENGTH_LONG). if (!storageRefusedOnce) {
setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show(); 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() { public String getSuggestedPublishUrl() {

View file

@ -25,6 +25,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import io.lbry.browser.BuildConfig;
import io.lbry.browser.MainActivity; import io.lbry.browser.MainActivity;
import io.lbry.browser.exceptions.LbryioRequestException; import io.lbry.browser.exceptions.LbryioRequestException;
import io.lbry.browser.exceptions.LbryioResponseException; import io.lbry.browser.exceptions.LbryioResponseException;
@ -81,6 +82,10 @@ public final class Lbryio {
authToken = getAuthToken(context); 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); String url = String.format("%s/%s/%s", CONNECTION_STRING, resource, action);
if (Helper.METHOD_GET.equalsIgnoreCase(method)) { if (Helper.METHOD_GET.equalsIgnoreCase(method)) {
Uri.Builder uriBuilder = Uri.parse(url).buildUpon(); Uri.Builder uriBuilder = Uri.parse(url).buildUpon();
@ -94,6 +99,9 @@ public final class Lbryio {
} }
url = uriBuilder.build().toString(); 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); Request.Builder builder = new Request.Builder().url(url);
if (Helper.METHOD_POST.equalsIgnoreCase(method)) { if (Helper.METHOD_POST.equalsIgnoreCase(method)) {
@ -142,6 +150,9 @@ public final class Lbryio {
} }
generatingAuthToken = true; generatingAuthToken = true;
if (BuildConfig.DEBUG) {
Log.d(TAG, "Generating a new auth token");
}
Map<String, String> options = new HashMap<>(); Map<String, String> options = new HashMap<>();
options.put("auth_token", ""); options.put("auth_token", "");
@ -150,6 +161,9 @@ public final class Lbryio {
Response response = Lbryio.call("user", "new", options, "post", context); Response response = Lbryio.call("user", "new", options, "post", context);
try { try {
JSONObject json = (JSONObject) parseResponse(response); 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)) { if (!json.has(AUTH_TOKEN_PARAM)) {
throw new LbryioResponseException("auth_token was not set in the response"); throw new LbryioResponseException("auth_token was not set in the response");
} }