Skip to content

Removing dependency for activities #3298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.firebase.appdistribution.TaskUtils.safeSetTaskException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.GuardedBy;
Expand Down Expand Up @@ -48,18 +49,22 @@ class AabUpdater {
private AppDistributionReleaseInternal aabReleaseInProgress;

private final Object updateAabLock = new Object();
private final Context context;

AabUpdater() {
AabUpdater(@NonNull Context context) {
this(
context,
FirebaseAppDistributionLifecycleNotifier.getInstance(),
new HttpsUrlConnectionFactory(),
Executors.newSingleThreadExecutor());
}

AabUpdater(
@NonNull Context context,
@NonNull FirebaseAppDistributionLifecycleNotifier lifecycleNotifier,
@NonNull HttpsUrlConnectionFactory httpsUrlConnectionFactory,
@NonNull Executor executor) {
this.context = context;
this.lifecycleNotifier = lifecycleNotifier;
this.httpsUrlConnectionFactory = httpsUrlConnectionFactory;
lifecycleNotifier.addOnActivityStartedListener(this::onActivityStarted);
Expand Down Expand Up @@ -134,16 +139,6 @@ private static boolean isRedirectResponse(int responseCode) {
}

private void redirectToPlayForAabUpdate(String downloadUrl) {
synchronized (updateAabLock) {
// TODO(rachelprince): change this to getCurrentNonNullActivity
if (lifecycleNotifier.getCurrentActivity() == null) {
safeSetTaskException(
cachedUpdateTask,
new FirebaseAppDistributionException(ErrorMessages.APP_BACKGROUNDED, DOWNLOAD_FAILURE));
return;
}
}

// The 302 redirect is obtained here to open the play store directly and avoid opening chrome
executor.execute( // Execute the network calls on a background thread
() -> {
Expand All @@ -161,7 +156,7 @@ private void redirectToPlayForAabUpdate(String downloadUrl) {
LogWrapper.getInstance().v(TAG + "Redirecting to play");

synchronized (updateAabLock) {
lifecycleNotifier.getCurrentActivity().startActivity(updateIntent);
context.startActivity(updateIntent);
cachedUpdateTask.updateProgress(
UpdateProgress.builder()
.setApkBytesDownloaded(-1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ApkUpdater {
private TaskCompletionSource<File> downloadTaskCompletionSource;
private final Executor taskExecutor; // Executor to run task listeners on a background thread
private final FirebaseApp firebaseApp;
private final Context context;
private final ApkInstaller apkInstaller;
private final FirebaseAppDistributionNotificationsManager appDistributionNotificationsManager;
private final HttpsUrlConnectionFactory httpsUrlConnectionFactory;
Expand Down Expand Up @@ -81,6 +82,7 @@ public ApkUpdater(
this.apkInstaller = apkInstaller;
this.appDistributionNotificationsManager = appDistributionNotificationsManager;
this.httpsUrlConnectionFactory = httpsUrlConnectionFactory;
this.context = firebaseApp.getApplicationContext();
}

UpdateTaskImpl updateApk(
Expand Down Expand Up @@ -179,7 +181,7 @@ private void makeApkDownloadRequest(

long bytesDownloaded = downloadToDisk(connection, responseLength, fileName, showNotification);

File apkFile = firebaseApp.getApplicationContext().getFileStreamPath(fileName);
File apkFile = context.getFileStreamPath(fileName);
validateJarFile(apkFile, responseLength, showNotification, bytesDownloaded);

postUpdateProgress(responseLength, bytesDownloaded, UpdateStatus.DOWNLOADED, showNotification);
Expand All @@ -193,7 +195,6 @@ private static boolean isResponseSuccess(int responseCode) {
private long downloadToDisk(
HttpsURLConnection connection, long totalSize, String fileName, boolean showNotification)
throws FirebaseAppDistributionException {
Context context = firebaseApp.getApplicationContext();
context.deleteFile(fileName);
int fileCreationMode =
VERSION.SDK_INT >= VERSION_CODES.N ? Context.MODE_PRIVATE : Context.MODE_WORLD_READABLE;
Expand Down Expand Up @@ -241,7 +242,6 @@ private void validateJarFile(

private String getApkFileName() {
try {
Context context = firebaseApp.getApplicationContext();
String applicationName =
context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
return applicationName + ".apk";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public class FirebaseAppDistribution {
new FirebaseAppDistributionTesterApiClient(),
firebaseInstallationsApiProvider),
new ApkUpdater(firebaseApp, new ApkInstaller()),
new AabUpdater(),
new AabUpdater(firebaseApp.getApplicationContext()),
signInStorage,
lifecycleNotifier);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class NewReleaseFetcher {
private final FirebaseApp firebaseApp;
private final FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient;
private final Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider;
private final Context context;
// Maintain an in-memory mapping from source file to APK hash to avoid re-calculating the hash
private static final ConcurrentMap<String, String> cachedApkHashes = new ConcurrentHashMap<>();

Expand All @@ -60,10 +61,11 @@ class NewReleaseFetcher {
@NonNull FirebaseApp firebaseApp,
@NonNull FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient,
@NonNull Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider) {
this.firebaseApp = firebaseApp;
this.firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient;
this.firebaseInstallationsApiProvider = firebaseInstallationsApiProvider;
this.taskExecutor = Executors.newSingleThreadExecutor();
this(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

firebaseApp,
firebaseAppDistributionTesterApiClient,
firebaseInstallationsApiProvider,
Executors.newSingleThreadExecutor());
}

NewReleaseFetcher(
Expand All @@ -75,6 +77,7 @@ class NewReleaseFetcher {
this.firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient;
this.firebaseInstallationsApiProvider = firebaseInstallationsApiProvider;
this.taskExecutor = executor;
this.context = firebaseApp.getApplicationContext();
}

@NonNull
Expand Down Expand Up @@ -151,19 +154,17 @@ private long parseBuildVersion(String buildVersion) throws FirebaseAppDistributi

private boolean isOlderBuildVersion(long newReleaseBuildVersion)
throws FirebaseAppDistributionException {
return newReleaseBuildVersion < getInstalledAppVersionCode(firebaseApp.getApplicationContext());
return newReleaseBuildVersion < getInstalledAppVersionCode(context);
}

private boolean isNewerBuildVersion(long newReleaseBuildVersion)
throws FirebaseAppDistributionException {
return newReleaseBuildVersion > getInstalledAppVersionCode(firebaseApp.getApplicationContext());
return newReleaseBuildVersion > getInstalledAppVersionCode(context);
}

private boolean hasDifferentAppVersionName(AppDistributionReleaseInternal newRelease)
throws FirebaseAppDistributionException {
return !newRelease
.getDisplayVersion()
.equals(getInstalledAppVersionName(firebaseApp.getApplicationContext()));
return !newRelease.getDisplayVersion().equals(getInstalledAppVersionName(context));
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Task<Void> signInTester() {
firebaseInstallationsApiProvider
.get()
.getId()
.addOnSuccessListener(getFidGenerationOnSuccessListener(currentActivity))
.addOnSuccessListener(getFidGenerationOnSuccessListener())
.addOnFailureListener(
e -> {
LogWrapper.getInstance().e(TAG + "Fid retrieval failed.", e);
Expand Down Expand Up @@ -179,7 +179,7 @@ private void setSuccessfulSignInResult() {
}
}

private OnSuccessListener<String> getFidGenerationOnSuccessListener(Activity currentActivity) {
private OnSuccessListener<String> getFidGenerationOnSuccessListener() {
return fid -> {
Context context = firebaseApp.getApplicationContext();
Uri uri =
Expand All @@ -190,7 +190,7 @@ private OnSuccessListener<String> getFidGenerationOnSuccessListener(Activity cur
fid,
getApplicationName(context),
context.getPackageName()));
openSignInFlowInBrowser(currentActivity, uri);
openSignInFlowInBrowser(context, uri);
};
}

Expand All @@ -203,22 +203,22 @@ private static String getApplicationName(Context context) {
}
}

private void openSignInFlowInBrowser(Activity currentActivity, Uri uri) {
private void openSignInFlowInBrowser(Context applicationContext, Uri uri) {
LogWrapper.getInstance().v(TAG + "Opening sign in flow in browser at " + uri);
if (supportsCustomTabs(firebaseApp.getApplicationContext())) {
if (supportsCustomTabs(applicationContext)) {
// If we can launch a chrome view, try that.
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder().build();
Intent intent = customTabsIntent.intent;
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
customTabsIntent.launchUrl(currentActivity, uri);
customTabsIntent.launchUrl(applicationContext, uri);

} else {
// If we can't launch a chrome view try to launch anything that can handle a URL.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
currentActivity.startActivity(browserIntent);
applicationContext.startActivity(browserIntent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import android.app.Activity;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import com.google.firebase.FirebaseApp;
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -84,7 +85,11 @@ public void setup() throws IOException, FirebaseAppDistributionException {

aabUpdater =
Mockito.spy(
new AabUpdater(mockLifecycleNotifier, mockHttpsUrlConnectionFactory, testExecutor));
new AabUpdater(
ApplicationProvider.getApplicationContext(),
mockLifecycleNotifier,
mockHttpsUrlConnectionFactory,
testExecutor));
when(mockLifecycleNotifier.getCurrentActivity()).thenReturn(activity);
}

Expand Down