Skip to content

[Prototype] Migrate app distro to dagger. #4380

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 0 additions & 2 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ concurrency:
cancel-in-progress: true
on:
pull_request:
branches:
- '*'
push:
branches:
- master
Expand Down
18 changes: 17 additions & 1 deletion firebase-appdistribution/firebase-appdistribution.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

plugins {
id 'firebase-library'
id 'firebase-vendor'
}

firebaseLibrary {
Expand Down Expand Up @@ -44,17 +45,23 @@ android {
dependencies {
implementation 'org.jetbrains:annotations:15.0'
implementation project(':firebase-appdistribution-api')
implementation project(':firebase-annotations')
implementation project(':firebase-components')
implementation project(':firebase-installations-interop')
implementation project(':firebase-common')
testImplementation project(path: ':firebase-appdistribution')
runtimeOnly project(':firebase-installations')

implementation 'javax.inject:javax.inject:1'
vendor ('com.google.dagger:dagger:2.43.2') {
exclude group: "javax.inject", module: "javax.inject"
}
annotationProcessor 'com.google.dagger:dagger-compiler:2.43.2'

testImplementation 'junit:junit:4.13.2'
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'org.mockito:mockito-inline:3.4.0'
androidTestImplementation "org.mockito:mockito-android:3.4.0"
testImplementation 'androidx.test:core:1.2.0'

implementation 'com.google.android.gms:play-services-tasks:18.0.1'
Expand All @@ -64,4 +71,13 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation "androidx.browser:browser:1.3.0"
implementation "androidx.constraintlayout:constraintlayout:2.1.4"

androidTestImplementation project(':integ-testing')
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "androidx.annotation:annotation:1.0.0"
androidTestImplementation 'org.mockito:mockito-core:2.25.0'
androidTestImplementation 'org.mockito:mockito-inline:2.25.0'
}
26 changes: 26 additions & 0 deletions firebase-appdistribution/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2022 Google LLC -->
<!-- -->
<!-- Licensed under the Apache License, Version 2.0 (the "License"); -->
<!-- you may not use this file except in compliance with the License. -->
<!-- You may obtain a copy of the License at -->
<!-- -->
<!-- http://www.apache.org/licenses/LICENSE-2.0 -->
<!-- -->
<!-- Unless required by applicable law or agreed to in writing, software -->
<!-- distributed under the License is distributed on an "AS IS" BASIS, -->
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -->
<!-- See the License for the specific language governing permissions and -->
<!-- limitations under the License. -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.firebase.installations">

<application>
<uses-library android:name="android.test.runner"/>
</application>

<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.google.firebase.appdistribution" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.firebase.appdistribution;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.appdistribution.internal.FirebaseAppDistributionProxy;
import com.google.firebase.testing.integ.StrictModeRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class StrictModeTest {

@Rule public StrictModeRule strictMode = new StrictModeRule();

@Test
public void initializingFirebaseAppdistribution_shouldNotViolateStrictMode() {
strictMode.runOnMainThread(
() -> {
FirebaseApp app =
FirebaseApp.initializeApp(
ApplicationProvider.getApplicationContext(),
new FirebaseOptions.Builder()
.setApiKey("api")
.setProjectId("123")
.setApplicationId("appId")
.build(),
"hello");
app.get(FirebaseAppDistribution.class);
app.get(FirebaseAppDistributionProxy.class);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
import static com.google.firebase.appdistribution.impl.TaskUtils.runAsyncInTask;
import static com.google.firebase.appdistribution.impl.TaskUtils.safeSetTaskException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.appdistribution.FirebaseAppDistribution;
import com.google.firebase.appdistribution.FirebaseAppDistributionException;
import com.google.firebase.appdistribution.UpdateStatus;
import java.io.IOException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.inject.Inject;
import javax.net.ssl.HttpsURLConnection;

/** Class that handles updateApp functionality for AABs in {@link FirebaseAppDistribution}. */
Expand All @@ -40,7 +40,7 @@ class AabUpdater {

private final FirebaseAppDistributionLifecycleNotifier lifecycleNotifier;
private final HttpsUrlConnectionFactory httpsUrlConnectionFactory;
private final Executor executor;
private final Executor blockingExecutor;

private final Object updateAabLock = new Object();

Expand All @@ -53,23 +53,15 @@ class AabUpdater {
@GuardedBy("updateAabLock")
private boolean hasBeenSentToPlayForCurrentTask = false;

// TODO(b/258264924): Migrate to go/firebase-android-executors
@SuppressLint("ThreadPoolCreation")
AabUpdater() {
this(
FirebaseAppDistributionLifecycleNotifier.getInstance(),
new HttpsUrlConnectionFactory(),
Executors.newSingleThreadExecutor());
}

@Inject
AabUpdater(
@NonNull FirebaseAppDistributionLifecycleNotifier lifecycleNotifier,
@NonNull HttpsUrlConnectionFactory httpsUrlConnectionFactory,
@NonNull Executor executor) {
@NonNull @Blocking Executor blockingExecutor) {
this.lifecycleNotifier = lifecycleNotifier;
this.httpsUrlConnectionFactory = httpsUrlConnectionFactory;
lifecycleNotifier.addOnActivityStartedListener(this::onActivityStarted);
this.executor = executor;
this.blockingExecutor = blockingExecutor;
}

@VisibleForTesting
Expand Down Expand Up @@ -97,13 +89,13 @@ UpdateTaskImpl updateAab(@NonNull AppDistributionReleaseInternal newRelease) {
hasBeenSentToPlayForCurrentTask = false;

// On a background thread, fetch the redirect URL and open it in the Play app
runAsyncInTask(executor, () -> fetchDownloadRedirectUrl(newRelease.getDownloadUrl()))
runAsyncInTask(blockingExecutor, () -> fetchDownloadRedirectUrl(newRelease.getDownloadUrl()))
.onSuccessTask(
executor,
blockingExecutor,
redirectUrl ->
lifecycleNotifier.consumeForegroundActivity(
activity -> openRedirectUrlInPlay(redirectUrl, activity)))
.addOnFailureListener(executor, this::setUpdateTaskCompletionError);
.addOnFailureListener(blockingExecutor, this::setUpdateTaskCompletionError);

return cachedUpdateTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.appdistribution.impl;

import androidx.annotation.Nullable;
import com.google.firebase.FirebaseApp;
import dagger.MembersInjector;
import javax.inject.Inject;

/** Firebase Component used by activities to request dependency injection into them. */
class ActivityInjector {
private final MembersInjector<InstallActivity> installActivityInjector;

@Inject
ActivityInjector(MembersInjector<InstallActivity> installActivityInjector) {
this.installActivityInjector = installActivityInjector;
}

@SuppressWarnings("FirebaseUseExplicitDependencies")
static void injectForApp(@Nullable String firebaseAppName, InstallActivity activity) {
getApp(firebaseAppName)
.get(ActivityInjector.class)
.installActivityInjector
.injectMembers(activity);
}

private static FirebaseApp getApp(@Nullable String name) {
if (name == null) {
return FirebaseApp.getInstance();
}
return FirebaseApp.getInstance(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,30 @@
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.firebase.appdistribution.FirebaseAppDistribution;
import com.google.firebase.appdistribution.FirebaseAppDistributionException;
import javax.inject.Inject;
import javax.inject.Named;

/** Class that handles installing APKs in {@link FirebaseAppDistribution}. */
class ApkInstaller {
private static final String TAG = "ApkInstaller:";
private final String appName;
private final FirebaseAppDistributionLifecycleNotifier lifeCycleNotifier;

@GuardedBy("installTaskLock")
private TaskCompletionSource<Void> installTaskCompletionSource;

private final Object installTaskLock = new Object();

ApkInstaller(FirebaseAppDistributionLifecycleNotifier lifeCycleNotifier) {
@Inject
ApkInstaller(
@Named("appName") String appName,
FirebaseAppDistributionLifecycleNotifier lifeCycleNotifier) {
this.appName = appName;
this.lifeCycleNotifier = lifeCycleNotifier;
lifeCycleNotifier.addOnActivityStartedListener(this::onActivityStarted);
lifeCycleNotifier.addOnActivityDestroyedListener(this::onActivityDestroyed);
}

ApkInstaller() {
this(FirebaseAppDistributionLifecycleNotifier.getInstance());
}

void onActivityStarted(@Nullable Activity activity) {
synchronized (installTaskLock) {
if (installTaskCompletionSource == null
Expand Down Expand Up @@ -78,6 +81,7 @@ Task<Void> installApk(String path, Activity currentActivity) {
private void startInstallActivity(String path, Activity currentActivity) {
Intent intent = new Intent(currentActivity, InstallActivity.class);
intent.putExtra("INSTALL_PATH", path);
intent.putExtra("FIREBASE_APP_NAME", appName);
currentActivity.startActivity(intent);
LogWrapper.getInstance().v(TAG + "Prompting tester with install activity ");
}
Expand Down
Loading