Skip to content

Commit 55ffdd5

Browse files
manny-jimenezManny Jimenez
andauthored
Removing dependency for activities (#3298)
* Removing dependancy for activities * Fixing some tests * Fixing tests and reverting APK changes * Removing comments * Removing left over line. * Responding to feedback * Changing context name * Lint fixes * Cleaning up test Co-authored-by: Manny Jimenez <[email protected]>
1 parent e01e144 commit 55ffdd5

File tree

6 files changed

+34
-33
lines changed

6 files changed

+34
-33
lines changed

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/AabUpdater.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.firebase.appdistribution.TaskUtils.safeSetTaskException;
2020

2121
import android.app.Activity;
22+
import android.content.Context;
2223
import android.content.Intent;
2324
import android.net.Uri;
2425
import androidx.annotation.GuardedBy;
@@ -48,18 +49,22 @@ class AabUpdater {
4849
private AppDistributionReleaseInternal aabReleaseInProgress;
4950

5051
private final Object updateAabLock = new Object();
52+
private final Context context;
5153

52-
AabUpdater() {
54+
AabUpdater(@NonNull Context context) {
5355
this(
56+
context,
5457
FirebaseAppDistributionLifecycleNotifier.getInstance(),
5558
new HttpsUrlConnectionFactory(),
5659
Executors.newSingleThreadExecutor());
5760
}
5861

5962
AabUpdater(
63+
@NonNull Context context,
6064
@NonNull FirebaseAppDistributionLifecycleNotifier lifecycleNotifier,
6165
@NonNull HttpsUrlConnectionFactory httpsUrlConnectionFactory,
6266
@NonNull Executor executor) {
67+
this.context = context;
6368
this.lifecycleNotifier = lifecycleNotifier;
6469
this.httpsUrlConnectionFactory = httpsUrlConnectionFactory;
6570
lifecycleNotifier.addOnActivityStartedListener(this::onActivityStarted);
@@ -134,16 +139,6 @@ private static boolean isRedirectResponse(int responseCode) {
134139
}
135140

136141
private void redirectToPlayForAabUpdate(String downloadUrl) {
137-
synchronized (updateAabLock) {
138-
// TODO(rachelprince): change this to getCurrentNonNullActivity
139-
if (lifecycleNotifier.getCurrentActivity() == null) {
140-
safeSetTaskException(
141-
cachedUpdateTask,
142-
new FirebaseAppDistributionException(ErrorMessages.APP_BACKGROUNDED, DOWNLOAD_FAILURE));
143-
return;
144-
}
145-
}
146-
147142
// The 302 redirect is obtained here to open the play store directly and avoid opening chrome
148143
executor.execute( // Execute the network calls on a background thread
149144
() -> {
@@ -161,7 +156,7 @@ private void redirectToPlayForAabUpdate(String downloadUrl) {
161156
LogWrapper.getInstance().v(TAG + "Redirecting to play");
162157

163158
synchronized (updateAabLock) {
164-
lifecycleNotifier.getCurrentActivity().startActivity(updateIntent);
159+
context.startActivity(updateIntent);
165160
cachedUpdateTask.updateProgress(
166161
UpdateProgress.builder()
167162
.setApkBytesDownloaded(-1)

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/ApkUpdater.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ApkUpdater {
5151
private TaskCompletionSource<File> downloadTaskCompletionSource;
5252
private final Executor taskExecutor; // Executor to run task listeners on a background thread
5353
private final FirebaseApp firebaseApp;
54+
private final Context context;
5455
private final ApkInstaller apkInstaller;
5556
private final FirebaseAppDistributionNotificationsManager appDistributionNotificationsManager;
5657
private final HttpsUrlConnectionFactory httpsUrlConnectionFactory;
@@ -81,6 +82,7 @@ public ApkUpdater(
8182
this.apkInstaller = apkInstaller;
8283
this.appDistributionNotificationsManager = appDistributionNotificationsManager;
8384
this.httpsUrlConnectionFactory = httpsUrlConnectionFactory;
85+
this.context = firebaseApp.getApplicationContext();
8486
}
8587

8688
UpdateTaskImpl updateApk(
@@ -179,7 +181,7 @@ private void makeApkDownloadRequest(
179181

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

182-
File apkFile = firebaseApp.getApplicationContext().getFileStreamPath(fileName);
184+
File apkFile = context.getFileStreamPath(fileName);
183185
validateJarFile(apkFile, responseLength, showNotification, bytesDownloaded);
184186

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

242243
private String getApkFileName() {
243244
try {
244-
Context context = firebaseApp.getApplicationContext();
245245
String applicationName =
246246
context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
247247
return applicationName + ".apk";

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/FirebaseAppDistribution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class FirebaseAppDistribution {
100100
new FirebaseAppDistributionTesterApiClient(),
101101
firebaseInstallationsApiProvider),
102102
new ApkUpdater(firebaseApp, new ApkInstaller()),
103-
new AabUpdater(),
103+
new AabUpdater(firebaseApp.getApplicationContext()),
104104
signInStorage,
105105
lifecycleNotifier);
106106
}

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/NewReleaseFetcher.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class NewReleaseFetcher {
5050
private final FirebaseApp firebaseApp;
5151
private final FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient;
5252
private final Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider;
53+
private final Context context;
5354
// Maintain an in-memory mapping from source file to APK hash to avoid re-calculating the hash
5455
private static final ConcurrentMap<String, String> cachedApkHashes = new ConcurrentHashMap<>();
5556

@@ -60,10 +61,11 @@ class NewReleaseFetcher {
6061
@NonNull FirebaseApp firebaseApp,
6162
@NonNull FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient,
6263
@NonNull Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider) {
63-
this.firebaseApp = firebaseApp;
64-
this.firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient;
65-
this.firebaseInstallationsApiProvider = firebaseInstallationsApiProvider;
66-
this.taskExecutor = Executors.newSingleThreadExecutor();
64+
this(
65+
firebaseApp,
66+
firebaseAppDistributionTesterApiClient,
67+
firebaseInstallationsApiProvider,
68+
Executors.newSingleThreadExecutor());
6769
}
6870

6971
NewReleaseFetcher(
@@ -75,6 +77,7 @@ class NewReleaseFetcher {
7577
this.firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient;
7678
this.firebaseInstallationsApiProvider = firebaseInstallationsApiProvider;
7779
this.taskExecutor = executor;
80+
this.context = firebaseApp.getApplicationContext();
7881
}
7982

8083
@NonNull
@@ -151,19 +154,17 @@ private long parseBuildVersion(String buildVersion) throws FirebaseAppDistributi
151154

152155
private boolean isOlderBuildVersion(long newReleaseBuildVersion)
153156
throws FirebaseAppDistributionException {
154-
return newReleaseBuildVersion < getInstalledAppVersionCode(firebaseApp.getApplicationContext());
157+
return newReleaseBuildVersion < getInstalledAppVersionCode(context);
155158
}
156159

157160
private boolean isNewerBuildVersion(long newReleaseBuildVersion)
158161
throws FirebaseAppDistributionException {
159-
return newReleaseBuildVersion > getInstalledAppVersionCode(firebaseApp.getApplicationContext());
162+
return newReleaseBuildVersion > getInstalledAppVersionCode(context);
160163
}
161164

162165
private boolean hasDifferentAppVersionName(AppDistributionReleaseInternal newRelease)
163166
throws FirebaseAppDistributionException {
164-
return !newRelease
165-
.getDisplayVersion()
166-
.equals(getInstalledAppVersionName(firebaseApp.getApplicationContext()));
167+
return !newRelease.getDisplayVersion().equals(getInstalledAppVersionName(context));
167168
}
168169

169170
@VisibleForTesting

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/TesterSignInManager.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public Task<Void> signInTester() {
141141
firebaseInstallationsApiProvider
142142
.get()
143143
.getId()
144-
.addOnSuccessListener(getFidGenerationOnSuccessListener(currentActivity))
144+
.addOnSuccessListener(getFidGenerationOnSuccessListener())
145145
.addOnFailureListener(
146146
e -> {
147147
LogWrapper.getInstance().e(TAG + "Fid retrieval failed.", e);
@@ -179,7 +179,7 @@ private void setSuccessfulSignInResult() {
179179
}
180180
}
181181

182-
private OnSuccessListener<String> getFidGenerationOnSuccessListener(Activity currentActivity) {
182+
private OnSuccessListener<String> getFidGenerationOnSuccessListener() {
183183
return fid -> {
184184
Context context = firebaseApp.getApplicationContext();
185185
Uri uri =
@@ -190,7 +190,7 @@ private OnSuccessListener<String> getFidGenerationOnSuccessListener(Activity cur
190190
fid,
191191
getApplicationName(context),
192192
context.getPackageName()));
193-
openSignInFlowInBrowser(currentActivity, uri);
193+
openSignInFlowInBrowser(context, uri);
194194
};
195195
}
196196

@@ -203,22 +203,22 @@ private static String getApplicationName(Context context) {
203203
}
204204
}
205205

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

216216
} else {
217217
// If we can't launch a chrome view try to launch anything that can handle a URL.
218218
Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
219219
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
220220
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
221-
currentActivity.startActivity(browserIntent);
221+
applicationContext.startActivity(browserIntent);
222222
}
223223
}
224224

firebase-app-distribution/src/test/java/com/google/firebase/appdistribution/AabUpdaterTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import android.app.Activity;
2424
import android.net.Uri;
25+
import androidx.test.core.app.ApplicationProvider;
2526
import com.google.firebase.FirebaseApp;
2627
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
2728
import java.io.ByteArrayInputStream;
@@ -84,7 +85,11 @@ public void setup() throws IOException, FirebaseAppDistributionException {
8485

8586
aabUpdater =
8687
Mockito.spy(
87-
new AabUpdater(mockLifecycleNotifier, mockHttpsUrlConnectionFactory, testExecutor));
88+
new AabUpdater(
89+
ApplicationProvider.getApplicationContext(),
90+
mockLifecycleNotifier,
91+
mockHttpsUrlConnectionFactory,
92+
testExecutor));
8893
when(mockLifecycleNotifier.getCurrentActivity()).thenReturn(activity);
8994
}
9095

0 commit comments

Comments
 (0)